From 85599fcf53232f2c753d8660c70f0b7dd3d4c0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 24 Jan 2023 21:01:21 +0800 Subject: [PATCH] Bluetooth (Windows): add support Limitation: no HLE devices, no battery level --- CMakeLists.txt | 3 +- src/detection/bluetooth/bluetooth_windows.c | 109 ++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/detection/bluetooth/bluetooth_windows.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 47543fab6..d585a6b1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -477,7 +477,7 @@ elseif(WIN32) src/common/processing_windows.c src/detection/battery/battery_windows.c src/detection/bios/bios_windows.c - src/detection/bluetooth/bluetooth_nosupport.c + src/detection/bluetooth/bluetooth_windows.c src/detection/board/board_windows.c src/detection/brightness/brightness_windows.cpp src/detection/chassis/chassis_nosupport.c @@ -682,6 +682,7 @@ if(APPLE) elseif(WIN32) target_compile_definitions(libfastfetch PRIVATE -D_WIN32_WINNT=0x0601) target_link_libraries(libfastfetch + PRIVATE "bthprops" PRIVATE "dwmapi" PRIVATE "gdi32" PRIVATE "iphlpapi" diff --git a/src/detection/bluetooth/bluetooth_windows.c b/src/detection/bluetooth/bluetooth_windows.c new file mode 100644 index 000000000..61af30d67 --- /dev/null +++ b/src/detection/bluetooth/bluetooth_windows.c @@ -0,0 +1,109 @@ +#include "bluetooth.h" +#include "util/windows/unicode.h" + +#include + +void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoothResult* bluetooth) +{ + BLUETOOTH_DEVICE_SEARCH_PARAMS btsp = { + .fReturnConnected = TRUE, + .fReturnRemembered = TRUE, + .fReturnAuthenticated = TRUE, + .fReturnUnknown = TRUE, + .dwSize = sizeof(btsp) + }; + + BLUETOOTH_DEVICE_INFO btdi = { + .dwSize = sizeof(btdi) + }; + HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(&btsp, &btdi); + if(!hFind) + ffStrbufAppendS(&bluetooth->error, "BluetoothFindFirstDevice() failed or no devices found"); + + do { + FFBluetoothDevice* device = ffListAdd(&bluetooth->devices); + ffStrbufInit(&device->name); + ffStrbufInit(&device->address); + ffStrbufInit(&device->type); + device->battery = 0; + device->connected = !!btdi.fConnected; + + ffStrbufSetWS(&device->name, btdi.szName); + for (uint32_t i = 0; i < sizeof(btdi.Address.rgBytes) / sizeof(btdi.Address.rgBytes); ++i) + ffStrbufAppendF(&device->address, "%X:", btdi.Address.rgBytes[i]); + ffStrbufTrimRight(&device->name, ':'); + //https://btprodspecificationrefs.blob.core.windows.net/assigned-numbers/Assigned%20Number%20Types/Assigned%20Numbers.pdf + + if(BitTest(&btdi.ulClassofDevice, 13)) + ffStrbufAppendS(&device->type, "Limited Discoverable Mode, "); + if(BitTest(&btdi.ulClassofDevice, 14)) + ffStrbufAppendS(&device->type, "LE audio, "); + if(BitTest(&btdi.ulClassofDevice, 15)) + ffStrbufAppendS(&device->type, "Reserved for future use, "); + if(BitTest(&btdi.ulClassofDevice, 16)) + ffStrbufAppendS(&device->type, "Positioning, "); + if(BitTest(&btdi.ulClassofDevice, 17)) + ffStrbufAppendS(&device->type, "Networking, "); + if(BitTest(&btdi.ulClassofDevice, 18)) + ffStrbufAppendS(&device->type, "Rendering, "); + if(BitTest(&btdi.ulClassofDevice, 19)) + ffStrbufAppendS(&device->type, "Capturing, "); + if(BitTest(&btdi.ulClassofDevice, 20)) + ffStrbufAppendS(&device->type, "Object Transfer, "); + if(BitTest(&btdi.ulClassofDevice, 21)) + ffStrbufAppendS(&device->type, "Audio, "); + if(BitTest(&btdi.ulClassofDevice, 22)) + ffStrbufAppendS(&device->type, "Telephony, "); + if(BitTest(&btdi.ulClassofDevice, 23)) + ffStrbufAppendS(&device->type, "Information, "); + + if(device->type.length == 0) + { + uint32_t majorDeviceClasses = (btdi.ulClassofDevice >> 8) & ~(UINT32_MAX << 5); + switch(majorDeviceClasses) + { + case 0b00000: + ffStrbufAppendS(&device->type, "Miscellaneous"); + break; + case 0b00001: + ffStrbufAppendS(&device->type, "Computer"); + break; + case 0b00010: + ffStrbufAppendS(&device->type, "Phone"); + break; + case 0b00011: + ffStrbufAppendS(&device->type, "LAN/Network Access point"); + break; + case 0b00100: + ffStrbufAppendS(&device->type, "Audio/Video"); + break; + case 0b00101: + ffStrbufAppendS(&device->type, "Peripheral"); + break; + case 0b00110: + ffStrbufAppendS(&device->type, "Imaging"); + break; + case 0b00111: + ffStrbufAppendS(&device->type, "Wearable"); + break; + case 0b01000: + ffStrbufAppendS(&device->type, "Toy"); + break; + case 0b01001: + ffStrbufAppendS(&device->type, "Health"); + break; + case 0b11111: + ffStrbufAppendS(&device->type, "Uncategorized"); + break; + default: + ffStrbufAppendS(&device->type, "Unknown"); + break; + } + } + else + { + ffStrbufTrimRight(&device->type, ' '); + ffStrbufTrimRight(&device->type, ','); + } + } while (BluetoothFindNextDevice(hFind, &btdi)); +}