termios_bsd.go 2.4 KB

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