termios_linux.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package termios
  2. import (
  3. "unsafe"
  4. "golang.org/x/sys/unix"
  5. )
  6. const (
  7. TCSETS = 0x5402
  8. TCSETSW = 0x5403
  9. TCSETSF = 0x5404
  10. TCFLSH = 0x540B
  11. TCSBRK = 0x5409
  12. TCSBRKP = 0x5425
  13. IXON = 0x00000400
  14. IXANY = 0x00000800
  15. IXOFF = 0x00001000
  16. CRTSCTS = 0x80000000
  17. )
  18. // Tcgetattr gets the current serial port settings.
  19. func Tcgetattr(fd uintptr, argp *unix.Termios) error {
  20. return ioctl(fd, unix.TCGETS, uintptr(unsafe.Pointer(argp)))
  21. }
  22. // Tcsetattr sets the current serial port settings.
  23. func Tcsetattr(fd, action uintptr, argp *unix.Termios) error {
  24. var request uintptr
  25. switch action {
  26. case TCSANOW:
  27. request = TCSETS
  28. case TCSADRAIN:
  29. request = TCSETSW
  30. case TCSAFLUSH:
  31. request = TCSETSF
  32. default:
  33. return unix.EINVAL
  34. }
  35. return ioctl(fd, request, uintptr(unsafe.Pointer(argp)))
  36. }
  37. // Tcsendbreak transmits a continuous stream of zero-valued bits for a specific
  38. // duration, if the terminal is using asynchronous serial data transmission. If
  39. // duration is zero, it transmits zero-valued bits for at least 0.25 seconds, and not more that 0.5 seconds.
  40. // If duration is not zero, it sends zero-valued bits for some
  41. // implementation-defined length of time.
  42. func Tcsendbreak(fd, duration uintptr) error {
  43. return ioctl(fd, TCSBRKP, duration)
  44. }
  45. // Tcdrain waits until all output written to the object referred to by fd has been transmitted.
  46. func Tcdrain(fd uintptr) error {
  47. // simulate drain with TCSADRAIN
  48. var attr unix.Termios
  49. if err := Tcgetattr(fd, &attr); err != nil {
  50. return err
  51. }
  52. return Tcsetattr(fd, TCSADRAIN, &attr)
  53. }
  54. // 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 selector.
  55. func Tcflush(fd, selector uintptr) error {
  56. return ioctl(fd, TCFLSH, selector)
  57. }
  58. // Tiocinq returns the number of bytes in the input buffer.
  59. func Tiocinq(fd uintptr, argp *int) error {
  60. return ioctl(fd, unix.TIOCINQ, uintptr(unsafe.Pointer(argp)))
  61. }
  62. // Tiocoutq return the number of bytes in the output buffer.
  63. func Tiocoutq(fd uintptr, argp *int) error {
  64. return ioctl(fd, unix.TIOCOUTQ, uintptr(unsafe.Pointer(argp)))
  65. }
  66. // Cfgetispeed returns the input baud rate stored in the termios structure.
  67. func Cfgetispeed(attr *unix.Termios) uint32 { return attr.Ispeed }
  68. // Cfgetospeed returns the output baud rate stored in the termios structure.
  69. func Cfgetospeed(attr *unix.Termios) uint32 { return attr.Ospeed }