remdeshid.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package remdeshid
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/tarm/serial"
  6. )
  7. type Config struct {
  8. /* Bindings and callback */
  9. OnWriteError func(error) // Callback for when an error occurs while writing to USBKVM device
  10. OnReadError func(error) // Callback for when an error occurs while reading from USBKVM device
  11. OnDataHandler func([]byte, error) // Callback for when data is received from USBKVM device
  12. /* Serial port configs */
  13. PortName string
  14. BaudRate int
  15. }
  16. // Controller is a struct that represents a HID controller
  17. type Controller struct {
  18. Config *Config
  19. serialPort *serial.Port
  20. serialRunning bool
  21. readStopChan chan bool
  22. writeStopChan chan bool
  23. writeQueue chan []byte
  24. }
  25. func NewHIDController(config *Config) *Controller {
  26. return &Controller{
  27. Config: config,
  28. serialRunning: false,
  29. }
  30. }
  31. // Connect opens the serial port and starts reading from it
  32. func (c *Controller) Connect() error {
  33. // Open the serial port
  34. config := &serial.Config{
  35. Name: c.Config.PortName,
  36. Baud: c.Config.BaudRate,
  37. Size: 8,
  38. Parity: serial.ParityNone,
  39. }
  40. port, err := serial.OpenPort(config)
  41. if err != nil {
  42. return err
  43. }
  44. c.serialPort = port
  45. c.readStopChan = make(chan bool)
  46. //Start reading from the serial port
  47. go func() {
  48. buf := make([]byte, 128)
  49. for {
  50. select {
  51. case <-c.readStopChan:
  52. return
  53. default:
  54. n, err := port.Read(buf)
  55. if err != nil {
  56. if c.Config.OnReadError != nil {
  57. c.Config.OnReadError(err)
  58. } else {
  59. log.Println(err.Error())
  60. }
  61. c.readStopChan = nil
  62. return
  63. }
  64. if n > 0 {
  65. if c.Config.OnDataHandler != nil {
  66. c.Config.OnDataHandler(buf[:n], nil)
  67. } else {
  68. fmt.Print("Received bytes: ")
  69. for i := 0; i < n; i++ {
  70. fmt.Printf("0x%02X ", buf[i])
  71. }
  72. fmt.Println()
  73. }
  74. }
  75. }
  76. }
  77. }()
  78. //Create a loop to write to the serial port
  79. c.writeStopChan = make(chan bool)
  80. c.writeQueue = make(chan []byte, 10)
  81. c.serialRunning = true
  82. go func() {
  83. for {
  84. select {
  85. case data := <-c.writeQueue:
  86. _, err := port.Write(data)
  87. if err != nil {
  88. if c.Config.OnWriteError != nil {
  89. c.Config.OnWriteError(err)
  90. } else {
  91. log.Println(err.Error())
  92. }
  93. }
  94. case <-c.writeStopChan:
  95. c.serialRunning = false
  96. return
  97. }
  98. }
  99. }()
  100. //Send over an opr queue reset signal
  101. err = c.Send([]byte{OPR_TYPE_DATA_RESET})
  102. if err != nil {
  103. return err
  104. }
  105. //Reset keyboard press state
  106. err = c.Send([]byte{OPR_TYPE_KEYBOARD_WRITE, SUBTYPE_KEYBOARD_SPECIAL_RESET, 0x00})
  107. if err != nil {
  108. return err
  109. }
  110. //Reset mouse press state
  111. err = c.Send([]byte{OPR_TYPE_MOUSE_WRITE, SUBTYPE_MOUSE_RESET, 0x00})
  112. if err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. func (c *Controller) Send(data []byte) error {
  118. if !c.serialRunning {
  119. return fmt.Errorf("serial port is not running")
  120. }
  121. c.writeQueue <- data
  122. return nil
  123. }
  124. func (c *Controller) Close() {
  125. if c.readStopChan != nil {
  126. c.readStopChan <- true
  127. }
  128. if c.writeStopChan != nil {
  129. c.writeStopChan <- true
  130. }
  131. c.serialPort.Close()
  132. }