os_detection.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright 2022 Ruslan Sayfutdinov (@KapJI)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "os_detection.h"
  17. #include <string.h>
  18. #ifdef OS_DETECTION_DEBUG_ENABLE
  19. # include "eeconfig.h"
  20. # include "eeprom.h"
  21. # include "print.h"
  22. # define STORED_USB_SETUPS 50
  23. # define EEPROM_USER_OFFSET (uint8_t*)EECONFIG_SIZE
  24. uint16_t usb_setups[STORED_USB_SETUPS];
  25. #endif
  26. #ifdef OS_DETECTION_ENABLE
  27. struct setups_data_t {
  28. uint8_t count;
  29. uint8_t cnt_02;
  30. uint8_t cnt_04;
  31. uint8_t cnt_ff;
  32. uint16_t last_wlength;
  33. };
  34. struct setups_data_t setups_data = {
  35. .count = 0,
  36. .cnt_02 = 0,
  37. .cnt_04 = 0,
  38. .cnt_ff = 0,
  39. };
  40. os_variant_t detected_os = OS_UNSURE;
  41. // Some collected sequences of wLength can be found in tests.
  42. void make_guess(void) {
  43. if (setups_data.count < 3) {
  44. return;
  45. }
  46. if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
  47. detected_os = OS_WINDOWS;
  48. return;
  49. }
  50. if (setups_data.count == setups_data.cnt_ff) {
  51. // Linux has 3 packets with 0xFF.
  52. detected_os = OS_LINUX;
  53. return;
  54. }
  55. if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
  56. detected_os = OS_MACOS;
  57. return;
  58. }
  59. if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
  60. // iOS and iPadOS don't have the last 0xFF packet.
  61. detected_os = OS_IOS;
  62. return;
  63. }
  64. if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
  65. // This is actually PS5.
  66. detected_os = OS_LINUX;
  67. return;
  68. }
  69. if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
  70. // This is actually Quest 2 or Nintendo Switch.
  71. detected_os = OS_LINUX;
  72. return;
  73. }
  74. }
  75. void process_wlength(const uint16_t w_length) {
  76. # ifdef OS_DETECTION_DEBUG_ENABLE
  77. usb_setups[setups_data.count] = w_length;
  78. # endif
  79. setups_data.count++;
  80. setups_data.last_wlength = w_length;
  81. if (w_length == 0x2) {
  82. setups_data.cnt_02++;
  83. } else if (w_length == 0x4) {
  84. setups_data.cnt_04++;
  85. } else if (w_length == 0xFF) {
  86. setups_data.cnt_ff++;
  87. }
  88. make_guess();
  89. }
  90. os_variant_t detected_host_os(void) {
  91. return detected_os;
  92. }
  93. void erase_wlength_data(void) {
  94. memset(&setups_data, 0, sizeof(setups_data));
  95. detected_os = OS_UNSURE;
  96. }
  97. # if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
  98. void slave_update_detected_host_os(os_variant_t os) {
  99. detected_os = os;
  100. }
  101. # endif // defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
  102. #endif // OS_DETECTION_ENABLE
  103. #ifdef OS_DETECTION_DEBUG_ENABLE
  104. void print_stored_setups(void) {
  105. # ifdef CONSOLE_ENABLE
  106. uint8_t cnt = eeprom_read_byte(EEPROM_USER_OFFSET);
  107. for (uint16_t i = 0; i < cnt; ++i) {
  108. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  109. xprintf("i: %d, wLength: 0x%02X\n", i, eeprom_read_word(addr));
  110. }
  111. # endif
  112. }
  113. void store_setups_in_eeprom(void) {
  114. eeprom_update_byte(EEPROM_USER_OFFSET, setups_data.count);
  115. for (uint16_t i = 0; i < setups_data.count; ++i) {
  116. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  117. eeprom_update_word(addr, usb_setups[i]);
  118. }
  119. }
  120. #endif // OS_DETECTION_DEBUG_ENABLE