Debian Codegraph @

serial_linux.go

raw symbols SCIP
1 package serial
2
3 import (
4 "fmt"
5 "syscall"
6 "unsafe"
7 )
8
9 var baudRates = map[int]uint32{
10 50: syscall.B50,
11 75: syscall.B75,
12 110: syscall.B110,
13 134: syscall.B134,
14 150: syscall.B150,
15 200: syscall.B200,
16 300: syscall.B300,
17 600: syscall.B600,
18 1200: syscall.B1200,
19 1800: syscall.B1800,
20 2400: syscall.B2400,
21 4800: syscall.B4800,
22 9600: syscall.B9600,
23 19200: syscall.B19200,
24 38400: syscall.B38400,
25 57600: syscall.B57600,
26 115200: syscall.B115200,
27 230400: syscall.B230400,
28 460800: syscall.B460800,
29 500000: syscall.B500000,
30 576000: syscall.B576000,
31 921600: syscall.B921600,
32 1000000: syscall.B1000000,
33 1152000: syscall.B1152000,
34 1500000: syscall.B1500000,
35 2000000: syscall.B2000000,
36 2500000: syscall.B2500000,
37 3000000: syscall.B3000000,
38 3500000: syscall.B3500000,
39 4000000: syscall.B4000000,
40 }
41
42 var charSizes = map[int]uint32{
43 5: syscall.CS5,
44 6: syscall.CS6,
45 7: syscall.CS7,
46 8: syscall.CS8,
47 }
48
49 // syscallSelect is a wapper for syscall.Select that only returns error.
50 func syscallSelect(n int, r *syscall.FdSet, w *syscall.FdSet, e *syscall.FdSet, tv *syscall.Timeval) error {
51 _, err := syscall.Select(n, r, w, e, tv)
52 return err
53 }
54
55 // tcsetattr sets terminal file descriptor parameters.
56 // See man tcsetattr(3).
57 func tcsetattr(fd int, termios *syscall.Termios) (err error) {
58 r, _, errno := syscall.Syscall(uintptr(syscall.SYS_IOCTL),
59 uintptr(fd), uintptr(syscall.TCSETS), uintptr(unsafe.Pointer(termios)))
60 if errno != 0 {
61 err = errno
62 return
63 }
64 if r != 0 {
65 err = fmt.Errorf("tcsetattr failed %v", r)
66 }
67 return
68 }
69
70 // tcgetattr gets terminal file descriptor parameters.
71 // See man tcgetattr(3).
72 func tcgetattr(fd int, termios *syscall.Termios) (err error) {
73 r, _, errno := syscall.Syscall(uintptr(syscall.SYS_IOCTL),
74 uintptr(fd), uintptr(syscall.TCGETS), uintptr(unsafe.Pointer(termios)))
75 if errno != 0 {
76 err = errno
77 return
78 }
79 if r != 0 {
80 err = fmt.Errorf("tcgetattr failed %v", r)
81 return
82 }
83 return
84 }
85
86 // fdget returns index and offset of fd in fds.
87 func fdget(fd int, fds *syscall.FdSet) (index, offset int) {
88 index = fd / (syscall.FD_SETSIZE / len(fds.Bits)) % len(fds.Bits)
89 offset = fd % (syscall.FD_SETSIZE / len(fds.Bits))
90 return
91 }
92
93 // fdset implements FD_SET macro.
94 func fdset(fd int, fds *syscall.FdSet) {
95 idx, pos := fdget(fd, fds)
96 fds.Bits[idx] = 1 << uint(pos)
97 }
98
99 // fdisset implements FD_ISSET macro.
100 func fdisset(fd int, fds *syscall.FdSet) bool {
101 idx, pos := fdget(fd, fds)
102 return fds.Bits[idx]&(1<<uint(pos)) != 0
103 }