Debian Codegraph @

serial_bsd.go

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