configure.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "time"
  7. "imuslab.com/dezukvm/dezukvmd/mod/kvmhid"
  8. )
  9. type UsbKvmConfig struct {
  10. ListeningAddress string
  11. USBKVMDevicePath string
  12. AuxMCUDevicePath string
  13. VideoCaptureDevicePath string
  14. AudioCaptureDevicePath string
  15. CaptureResolutionWidth int
  16. CaptureResolutionHeight int
  17. CaptureResolutionFPS int
  18. USBKVMBaudrate int
  19. AuxMCUBaudrate int
  20. }
  21. var (
  22. /* Internal variables for USB-KVM mode only */
  23. usbKVM *kvmhid.Controller
  24. defaultUsbKvmConfig = &UsbKvmConfig{
  25. ListeningAddress: ":9000",
  26. USBKVMDevicePath: "/dev/ttyUSB0",
  27. AuxMCUDevicePath: "/dev/ttyACM0",
  28. VideoCaptureDevicePath: "/dev/video0",
  29. AudioCaptureDevicePath: "/dev/snd/pcmC1D0c",
  30. CaptureResolutionWidth: 1920,
  31. CaptureResolutionHeight: 1080,
  32. CaptureResolutionFPS: 25,
  33. USBKVMBaudrate: 115200,
  34. AuxMCUBaudrate: 115200,
  35. }
  36. )
  37. func loadUsbKvmConfig() (*UsbKvmConfig, error) {
  38. if _, err := os.Stat(USB_KVM_CFG_PATH); os.IsNotExist(err) {
  39. file, err := os.OpenFile(USB_KVM_CFG_PATH, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775)
  40. if err != nil {
  41. return nil, err
  42. }
  43. // Save default config as JSON
  44. enc := json.NewEncoder(file)
  45. enc.SetIndent("", " ")
  46. if err := enc.Encode(defaultUsbKvmConfig); err != nil {
  47. file.Close()
  48. return nil, err
  49. }
  50. file.Close()
  51. return defaultUsbKvmConfig, nil
  52. }
  53. // Load config from file
  54. file, err := os.Open(USB_KVM_CFG_PATH)
  55. if err != nil {
  56. return nil, err
  57. }
  58. cfg := &UsbKvmConfig{}
  59. dec := json.NewDecoder(file)
  60. if err := dec.Decode(cfg); err != nil {
  61. file.Close()
  62. return nil, err
  63. }
  64. file.Close()
  65. return cfg, nil
  66. }
  67. func SetupHIDCommunication(config *UsbKvmConfig) error {
  68. // Initiate the HID controller
  69. usbKVM = kvmhid.NewHIDController(&kvmhid.Config{
  70. PortName: config.USBKVMDevicePath,
  71. BaudRate: config.USBKVMBaudrate,
  72. ScrollSensitivity: 0x01, // Set mouse scroll sensitivity
  73. })
  74. //Start the HID controller
  75. err := usbKVM.Connect()
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. time.Sleep(1 * time.Second) // Wait for the controller to initialize
  80. log.Println("Updating chip baudrate to 115200...")
  81. //Configure the HID controller
  82. err = usbKVM.ConfigureChipTo115200()
  83. if err != nil {
  84. log.Fatalf("Failed to configure chip baudrate: %v", err)
  85. return err
  86. }
  87. time.Sleep(1 * time.Second)
  88. log.Println("Setting chip USB device properties...")
  89. time.Sleep(2 * time.Second) // Wait for the controller to initialize
  90. _, err = usbKVM.WriteChipProperties()
  91. if err != nil {
  92. log.Fatalf("Failed to write chip properties: %v", err)
  93. return err
  94. }
  95. log.Println("Configuration command sent. Unplug the device and plug it back in to apply the changes.")
  96. return nil
  97. }