2022-12-08 08:45:30 -08:00
|
|
|
/* Copyright 2022 Ruslan Sayfutdinov (@KapJI)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "os_detection.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef OS_DETECTION_DEBUG_ENABLE
|
|
|
|
# include "eeconfig.h"
|
|
|
|
# include "eeprom.h"
|
|
|
|
# include "print.h"
|
|
|
|
|
|
|
|
# define STORED_USB_SETUPS 50
|
|
|
|
# define EEPROM_USER_OFFSET (uint8_t*)EECONFIG_SIZE
|
|
|
|
|
|
|
|
uint16_t usb_setups[STORED_USB_SETUPS];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OS_DETECTION_ENABLE
|
|
|
|
struct setups_data_t {
|
2023-04-03 15:48:51 -07:00
|
|
|
uint8_t count;
|
|
|
|
uint8_t cnt_02;
|
|
|
|
uint8_t cnt_04;
|
|
|
|
uint8_t cnt_ff;
|
2023-11-26 20:55:38 -08:00
|
|
|
uint8_t cnt_zoid;
|
2023-04-03 15:48:51 -07:00
|
|
|
uint16_t last_wlength;
|
2022-12-08 08:45:30 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct setups_data_t setups_data = {
|
2023-04-03 15:48:51 -07:00
|
|
|
.count = 0,
|
|
|
|
.cnt_02 = 0,
|
|
|
|
.cnt_04 = 0,
|
|
|
|
.cnt_ff = 0,
|
2023-11-26 20:55:38 -08:00
|
|
|
.cnt_zoid = 0,
|
2022-12-08 08:45:30 -08:00
|
|
|
};
|
|
|
|
|
2023-04-03 15:48:51 -07:00
|
|
|
os_variant_t detected_os = OS_UNSURE;
|
|
|
|
|
2022-12-08 08:45:30 -08:00
|
|
|
// Some collected sequences of wLength can be found in tests.
|
|
|
|
void make_guess(void) {
|
2023-11-26 20:55:38 -08:00
|
|
|
//if (setups_data.count < 3 && setups_data.cnt_zoid != 0) {
|
|
|
|
// return;
|
|
|
|
//}
|
2023-11-29 19:40:46 -08:00
|
|
|
//if (setups_data.cnt_zoid == 0) {
|
|
|
|
// detected_os = OS_UEFI;
|
|
|
|
// return;
|
|
|
|
//}
|
2022-12-08 08:45:30 -08:00
|
|
|
if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
|
2023-04-03 15:48:51 -07:00
|
|
|
detected_os = OS_WINDOWS;
|
2022-12-08 08:45:30 -08:00
|
|
|
return;
|
|
|
|
}
|
2023-11-26 20:55:38 -08:00
|
|
|
// I changed this
|
|
|
|
if (setups_data.count > 3 && setups_data.count == setups_data.cnt_ff) {
|
2022-12-08 08:45:30 -08:00
|
|
|
// Linux has 3 packets with 0xFF.
|
2023-04-03 15:48:51 -07:00
|
|
|
detected_os = OS_LINUX;
|
2022-12-08 08:45:30 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
|
2023-04-03 15:48:51 -07:00
|
|
|
detected_os = OS_MACOS;
|
2022-12-08 08:45:30 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
|
|
|
|
// iOS and iPadOS don't have the last 0xFF packet.
|
2023-04-03 15:48:51 -07:00
|
|
|
detected_os = OS_IOS;
|
2022-12-08 08:45:30 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
|
|
|
|
// This is actually PS5.
|
2023-04-03 15:48:51 -07:00
|
|
|
detected_os = OS_LINUX;
|
2022-12-08 08:45:30 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
|
|
|
|
// This is actually Quest 2 or Nintendo Switch.
|
2023-04-03 15:48:51 -07:00
|
|
|
detected_os = OS_LINUX;
|
2022-12-08 08:45:30 -08:00
|
|
|
return;
|
|
|
|
}
|
2023-11-26 20:55:38 -08:00
|
|
|
if (setups_data.cnt_zoid == 0) {
|
|
|
|
detected_os = OS_UEFI;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void process_zoid(const uint16_t zoid) {
|
|
|
|
setups_data.cnt_zoid = zoid;
|
|
|
|
make_guess();
|
2022-12-08 08:45:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void process_wlength(const uint16_t w_length) {
|
|
|
|
# ifdef OS_DETECTION_DEBUG_ENABLE
|
|
|
|
usb_setups[setups_data.count] = w_length;
|
|
|
|
# endif
|
|
|
|
setups_data.count++;
|
|
|
|
setups_data.last_wlength = w_length;
|
|
|
|
if (w_length == 0x2) {
|
|
|
|
setups_data.cnt_02++;
|
|
|
|
} else if (w_length == 0x4) {
|
|
|
|
setups_data.cnt_04++;
|
|
|
|
} else if (w_length == 0xFF) {
|
|
|
|
setups_data.cnt_ff++;
|
|
|
|
}
|
|
|
|
make_guess();
|
|
|
|
}
|
|
|
|
|
|
|
|
os_variant_t detected_host_os(void) {
|
2023-11-26 20:55:38 -08:00
|
|
|
make_guess();
|
2023-04-03 15:48:51 -07:00
|
|
|
return detected_os;
|
2022-12-08 08:45:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void erase_wlength_data(void) {
|
|
|
|
memset(&setups_data, 0, sizeof(setups_data));
|
2023-04-03 15:48:51 -07:00
|
|
|
detected_os = OS_UNSURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
# if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
|
|
|
|
void slave_update_detected_host_os(os_variant_t os) {
|
|
|
|
detected_os = os;
|
2022-12-08 08:45:30 -08:00
|
|
|
}
|
2023-04-03 15:48:51 -07:00
|
|
|
# endif // defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
|
|
|
|
#endif // OS_DETECTION_ENABLE
|
2022-12-08 08:45:30 -08:00
|
|
|
|
|
|
|
#ifdef OS_DETECTION_DEBUG_ENABLE
|
|
|
|
void print_stored_setups(void) {
|
|
|
|
# ifdef CONSOLE_ENABLE
|
|
|
|
uint8_t cnt = eeprom_read_byte(EEPROM_USER_OFFSET);
|
|
|
|
for (uint16_t i = 0; i < cnt; ++i) {
|
|
|
|
uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
|
|
|
|
xprintf("i: %d, wLength: 0x%02X\n", i, eeprom_read_word(addr));
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_setups_in_eeprom(void) {
|
|
|
|
eeprom_update_byte(EEPROM_USER_OFFSET, setups_data.count);
|
|
|
|
for (uint16_t i = 0; i < setups_data.count; ++i) {
|
|
|
|
uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
|
|
|
|
eeprom_update_word(addr, usb_setups[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // OS_DETECTION_DEBUG_ENABLE
|