remdesaux.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package kvmaux
  2. /*
  3. RemdesAux - Auxiliary MCU Control for RemdeskVM
  4. This module provides functions to interact with the auxiliary MCU (CH552G)
  5. used in RemdeskVM for managing USB switching and power/reset button simulation.
  6. */
  7. import (
  8. "bufio"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/tarm/serial"
  13. )
  14. type USB_mass_storage_side int
  15. const (
  16. USB_MASS_STORAGE_KVM USB_mass_storage_side = iota
  17. USB_MASS_STORAGE_REMOTE
  18. )
  19. type AuxMcu struct {
  20. usb_mass_storage_side USB_mass_storage_side
  21. port *serial.Port
  22. reader *bufio.Reader
  23. mu sync.Mutex
  24. }
  25. // NewAuxOutbandController initializes a new AuxMcu instance
  26. func NewAuxOutbandController(portName string, baudRate int) (*AuxMcu, error) {
  27. c := &serial.Config{
  28. Name: portName,
  29. Baud: baudRate,
  30. ReadTimeout: time.Second * 2,
  31. }
  32. port, err := serial.OpenPort(c)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &AuxMcu{
  37. usb_mass_storage_side: USB_MASS_STORAGE_KVM, //Default to KVM side, defined in MCU firmware
  38. port: port,
  39. reader: bufio.NewReader(port),
  40. }, nil
  41. }
  42. func (c *AuxMcu) Close() error {
  43. c.mu.Lock()
  44. defer c.mu.Unlock()
  45. if c.port != nil {
  46. return c.port.Close()
  47. }
  48. return nil
  49. }
  50. // sendCommand writes a single byte command to the serial port
  51. func (c *AuxMcu) sendCommand(cmd byte) error {
  52. c.mu.Lock()
  53. defer c.mu.Unlock()
  54. _, err := c.port.Write([]byte{cmd})
  55. return err
  56. }
  57. // SwitchUSBToKVM switches USB mass storage to KVM side
  58. func (c *AuxMcu) SwitchUSBToKVM() error {
  59. c.usb_mass_storage_side = USB_MASS_STORAGE_KVM
  60. return c.sendCommand('m')
  61. }
  62. // SwitchUSBToRemote switches USB mass storage to remote computer
  63. func (c *AuxMcu) SwitchUSBToRemote() error {
  64. c.usb_mass_storage_side = USB_MASS_STORAGE_REMOTE
  65. return c.sendCommand('n')
  66. }
  67. // PressPowerButton simulates pressing the power button
  68. func (c *AuxMcu) PressPowerButton() error {
  69. return c.sendCommand('p')
  70. }
  71. // ReleasePowerButton simulates releasing the power button
  72. func (c *AuxMcu) ReleasePowerButton() error {
  73. return c.sendCommand('s')
  74. }
  75. // PressResetButton simulates pressing the reset button
  76. func (c *AuxMcu) PressResetButton() error {
  77. return c.sendCommand('r')
  78. }
  79. // ReleaseResetButton simulates releasing the reset button
  80. func (c *AuxMcu) ReleaseResetButton() error {
  81. return c.sendCommand('d')
  82. }
  83. // GetUUID requests the device UUID and returns it as a string
  84. func (c *AuxMcu) GetUUID() (string, error) {
  85. if err := c.sendCommand('u'); err != nil {
  86. return "", err
  87. }
  88. line, err := c.reader.ReadString('\n')
  89. if err != nil {
  90. return "", err
  91. }
  92. line = strings.TrimSpace(line)
  93. return line, nil
  94. }
  95. func (c *AuxMcu) GetUSBMassStorageSide() USB_mass_storage_side {
  96. c.mu.Lock()
  97. defer c.mu.Unlock()
  98. return c.usb_mass_storage_side
  99. }