1// +build darwin freebsd openbsd netbsd dragonfly 2 3package termios 4 5import ( 6 "time" 7 8 "golang.org/x/sys/unix" 9) 10 11const ( 12 FREAD = 0x0001 13 FWRITE = 0x0002 14 15 IXON = 0x00000200 16 IXOFF = 0x00000400 17 IXANY = 0x00000800 18 CCTS_OFLOW = 0x00010000 19 CRTS_IFLOW = 0x00020000 20 CRTSCTS = CCTS_OFLOW | CRTS_IFLOW 21) 22 23// Tcgetattr gets the current serial port settings. 24func Tcgetattr(fd uintptr, argp *unix.Termios) error { 25 return unix.IoctlSetTermios(int(fd), unix.TIOCGETA, argp) 26} 27 28// Tcsetattr sets the current serial port settings. 29func Tcsetattr(fd, opt uintptr, argp *unix.Termios) error { 30 switch opt { 31 case TCSANOW: 32 opt = unix.TIOCSETA 33 case TCSADRAIN: 34 opt = unix.TIOCSETAW 35 case TCSAFLUSH: 36 opt = unix.TIOCSETAF 37 default: 38 return unix.EINVAL 39 } 40 return unix.IoctlSetTermios(int(fd), uint(opt), argp) 41} 42 43// Tcsendbreak function transmits a continuous stream of zero-valued bits for 44// four-tenths of a second to the terminal referenced by fildes. The duration 45// parameter is ignored in this implementation. 46func Tcsendbreak(fd, duration uintptr) error { 47 if err := unix.IoctlSetInt(int(fd), unix.TIOCSBRK, 0); err != nil { 48 return err 49 } 50 time.Sleep(4 / 10 * time.Second) 51 return unix.IoctlSetInt(int(fd), unix.TIOCCBRK, 0) 52} 53 54// Tcdrain waits until all output written to the terminal referenced by fd has been transmitted to the terminal. 55func Tcdrain(fd uintptr) error { 56 return unix.IoctlSetInt(int(fd), unix.TIOCDRAIN, 0) 57} 58 59// Tcflush discards data written to the object referred to by fd but not transmitted, or data received but not read, depending on the value of which. 60func Tcflush(fd, which uintptr) error { 61 var com int 62 switch which { 63 case unix.TCIFLUSH: 64 com = FREAD 65 case unix.TCOFLUSH: 66 com = FWRITE 67 case unix.TCIOFLUSH: 68 com = FREAD | FWRITE 69 default: 70 return unix.EINVAL 71 } 72 return unix.IoctlSetPointerInt(int(fd), unix.TIOCFLUSH, com) 73} 74 75// Cfgetispeed returns the input baud rate stored in the termios structure. 76func Cfgetispeed(attr *unix.Termios) uint32 { return uint32(attr.Ispeed) } 77 78// Cfgetospeed returns the output baud rate stored in the termios structure. 79func Cfgetospeed(attr *unix.Termios) uint32 { return uint32(attr.Ospeed) } 80 81// Tiocinq returns the number of bytes in the input buffer. 82func Tiocinq(fd uintptr) (int, error) { 83 return 0, nil 84} 85 86// Tiocoutq return the number of bytes in the output buffer. 87func Tiocoutq(fd uintptr) (int, error) { 88 return unix.IoctlGetInt(int(fd), unix.TIOCOUTQ) 89} 90