kvm_uuid_service.ino 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. kvm_uuid_service.ino
  3. author: tobychui
  4. This file contains code that uniquely
  5. identify a USB-KVM downstream device for
  6. multi-device setups.
  7. The UUID can change during power cycle,
  8. as soon as it is unique amoung other
  9. IP-KVM it is good to go.
  10. */
  11. #define UUID_SIZE 16 // 16-byte UUID
  12. uint8_t device_uuid[UUID_SIZE];
  13. void init_device_uuid() {
  14. // Use an unused analog pin as noise source (floating pin gives better entropy)
  15. const uint8_t noise_pin = 11; // P1.1, but could also be 14, 15, or 32
  16. pinMode(noise_pin, INPUT);
  17. for (uint8_t i = 0; i < UUID_SIZE; i++) {
  18. // Mix analog noise with millis() and loop index
  19. int noise = analogRead(noise_pin); // 0–1023
  20. uint8_t entropy = (uint8_t)(noise ^ (millis() >> (i % 8)) ^ (rand() & 0xFF));
  21. device_uuid[i] = entropy;
  22. delay(5); // small delay to let ADC vary
  23. }
  24. }
  25. void print_device_uuid() {
  26. for (uint8_t i = 0; i < UUID_SIZE; i++) {
  27. USBSerial_print(device_uuid[i], HEX);
  28. }
  29. USBSerial_println();
  30. }
  31. void renew_device_uuid() {
  32. init_device_uuid();
  33. }