You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.8 KiB

  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. os_variant_t detected_os;
  34. };
  35. struct setups_data_t setups_data = {
  36. .count = 0,
  37. .cnt_02 = 0,
  38. .cnt_04 = 0,
  39. .cnt_ff = 0,
  40. .detected_os = OS_UNSURE,
  41. };
  42. // Some collected sequences of wLength can be found in tests.
  43. void make_guess(void) {
  44. if (setups_data.count < 3) {
  45. return;
  46. }
  47. if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
  48. setups_data.detected_os = OS_WINDOWS;
  49. return;
  50. }
  51. if (setups_data.count == setups_data.cnt_ff) {
  52. // Linux has 3 packets with 0xFF.
  53. setups_data.detected_os = OS_LINUX;
  54. return;
  55. }
  56. if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
  57. setups_data.detected_os = OS_MACOS;
  58. return;
  59. }
  60. if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
  61. // iOS and iPadOS don't have the last 0xFF packet.
  62. setups_data.detected_os = OS_IOS;
  63. return;
  64. }
  65. if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
  66. // This is actually PS5.
  67. setups_data.detected_os = OS_LINUX;
  68. return;
  69. }
  70. if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
  71. // This is actually Quest 2 or Nintendo Switch.
  72. setups_data.detected_os = OS_LINUX;
  73. return;
  74. }
  75. }
  76. void process_wlength(const uint16_t w_length) {
  77. # ifdef OS_DETECTION_DEBUG_ENABLE
  78. usb_setups[setups_data.count] = w_length;
  79. # endif
  80. setups_data.count++;
  81. setups_data.last_wlength = w_length;
  82. if (w_length == 0x2) {
  83. setups_data.cnt_02++;
  84. } else if (w_length == 0x4) {
  85. setups_data.cnt_04++;
  86. } else if (w_length == 0xFF) {
  87. setups_data.cnt_ff++;
  88. }
  89. make_guess();
  90. }
  91. os_variant_t detected_host_os(void) {
  92. return setups_data.detected_os;
  93. }
  94. void erase_wlength_data(void) {
  95. memset(&setups_data, 0, sizeof(setups_data));
  96. }
  97. #endif // OS_DETECTION_ENABLE
  98. #ifdef OS_DETECTION_DEBUG_ENABLE
  99. void print_stored_setups(void) {
  100. # ifdef CONSOLE_ENABLE
  101. uint8_t cnt = eeprom_read_byte(EEPROM_USER_OFFSET);
  102. for (uint16_t i = 0; i < cnt; ++i) {
  103. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  104. xprintf("i: %d, wLength: 0x%02X\n", i, eeprom_read_word(addr));
  105. }
  106. # endif
  107. }
  108. void store_setups_in_eeprom(void) {
  109. eeprom_update_byte(EEPROM_USER_OFFSET, setups_data.count);
  110. for (uint16_t i = 0; i < setups_data.count; ++i) {
  111. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  112. eeprom_update_word(addr, usb_setups[i]);
  113. }
  114. }
  115. #endif // OS_DETECTION_DEBUG_ENABLE