Skip to content

Instantly share code, notes, and snippets.

@neolit123
Created February 13, 2018 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neolit123/e44feaf4babc7c4997f54af014a026e9 to your computer and use it in GitHub Desktop.
Save neolit123/e44feaf4babc7c4997f54af014a026e9 to your computer and use it in GitHub Desktop.
winapi-ble-test1
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <devpkey.h>
#include <bluetoothleapis.h>
static void print_GUID(GUID);
static int probe_GUID(GUID);
int main()
{
GUID iface = GUID_BLUETOOTHLE_DEVICE_INTERFACE;
probe_GUID(iface);
GUID serv = { 0x00001800, 0x0000, 0x1000,{ 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb } };
probe_GUID(serv);
return 0;
}
void print_GUID(GUID guid)
{
printf("GUID: %04x-%02x-%02x-", guid.Data1, guid.Data2, guid.Data3);
for (int i = 0; i < 8; i++) {
printf("%02x", guid.Data4[i]);
}
}
int probe_GUID(GUID guid)
{
printf("probing ");
print_GUID(guid);
puts("");
HDEVINFO devInfoHandle = SetupDiGetClassDevs(
&guid,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (devInfoHandle == INVALID_HANDLE_VALUE) {
printf("SetupDiGetClassDevs error\n");
puts("\nPress any key...");
getchar();
return 1;
}
printf("SetupDiGetClassDevs OK\n");
for (int i = 0;; i++) {
puts("-------------------------------");
SP_DEVICE_INTERFACE_DATA device_interface_data = { 0 };
device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
BOOL success = SetupDiEnumDeviceInterfaces(
devInfoHandle,
NULL,
(LPGUID)&guid,
(DWORD)i,
&device_interface_data);
if (!success) {
printf("SetupDiEnumDeviceInterfaces error %u\n", GetLastError());
break;
}
printf("SetupDiEnumDeviceInterfaces OK %d\n", i);
ULONG required_length = 0;
success = SetupDiGetDeviceInterfaceDetail(
devInfoHandle,
&device_interface_data,
NULL,
0,
&required_length,
NULL);
printf("SetupDiGetDeviceInterfaceDetail required_length: %d\n", required_length);
UINT8 *interface_data = (UINT8 *)calloc(required_length, sizeof(UINT8));
PSP_DEVICE_INTERFACE_DETAIL_DATA device_interface_detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)interface_data;
device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SP_DEVINFO_DATA device_info_data = { 0 };
device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
ULONG actual_length = required_length;
success = SetupDiGetDeviceInterfaceDetail(
devInfoHandle,
&device_interface_data,
device_interface_detail_data,
actual_length,
&required_length,
&device_info_data);
if (!success) {
printf("SetupDiGetDeviceInterfaceDetail error %u\n", GetLastError());
continue;
}
// has to be on the heap?!
WCHAR *path = _wcsdup(device_interface_detail_data->DevicePath);
wprintf(L"path: %s\n", path);
HANDLE deviceHandle;
deviceHandle = CreateFile(path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (deviceHandle == INVALID_HANDLE_VALUE) {
deviceHandle = CreateFile(path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
}
free(path);
if (deviceHandle == INVALID_HANDLE_VALUE) {
printf("CreateFile error: %u\n", GetLastError());
continue;
}
USHORT required_count;
HRESULT hr = BluetoothGATTGetServices(deviceHandle, 0, NULL, &required_count, BLUETOOTH_GATT_FLAG_NONE);
if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) {
printf("BluetoothGATTGetServices error 0x%08x\n", hr);
continue;
}
printf("BluetoothGATTGetServices count: %d\n", required_count);
BTH_LE_GATT_SERVICE *services = (BTH_LE_GATT_SERVICE *)calloc(required_count, sizeof(BTH_LE_GATT_SERVICE));
USHORT actual_count = required_count;
hr = BluetoothGATTGetServices(deviceHandle, actual_count, services, &required_count, BLUETOOTH_GATT_FLAG_NONE);
for (int i = 0; i < actual_count; i++) {
BTH_LE_GATT_SERVICE service = services[i];
_BTH_LE_UUID ServiceUuid = service.ServiceUuid;
printf("----------------\nService %d GUID:\n", i);
if (ServiceUuid.IsShortUuid) {
printf("%04x-0000-1000-8000-00805F9B34FB", ServiceUuid.Value.ShortUuid);
} else {
print_GUID(ServiceUuid.Value.LongUuid);
}
puts("");
USHORT required_count;
HRESULT hr = BluetoothGATTGetCharacteristics(deviceHandle, &service, 0, NULL, &required_count, BLUETOOTH_GATT_FLAG_NONE);
if (HRESULT_FROM_WIN32(ERROR_MORE_DATA) != hr) {
printf("BluetoothGATTGetCharacteristics error sz 0x%08x\n", hr);
continue;
}
BTH_LE_GATT_CHARACTERISTIC *gatt_characteristics = (BTH_LE_GATT_CHARACTERISTIC *)calloc(required_count, sizeof(BTH_LE_GATT_CHARACTERISTIC));
printf("BluetoothGATTGetCharacteristics count: %d\n", required_count);
USHORT actual_count = required_count;
hr = BluetoothGATTGetCharacteristics(deviceHandle, &service, actual_count, gatt_characteristics, &required_count, BLUETOOTH_GATT_FLAG_NONE);
if (S_OK != hr) {
printf("BluetoothGATTGetCharacteristics error 0x%08x\n", hr);
continue;
}
for (int i = 0; i < actual_count; i++) {
BTH_LE_GATT_CHARACTERISTIC gatt_characteristic = gatt_characteristics[i];
if (!gatt_characteristic.IsReadable) {
printf("BTH_LE_GATT_CHARACTERISTIC %d not readable\n", i);
continue;
}
USHORT sz;
hr = BluetoothGATTGetCharacteristicValue(
deviceHandle,
&gatt_characteristic,
0,
NULL,
&sz,
BLUETOOTH_GATT_FLAG_NONE);
if (HRESULT_FROM_WIN32(ERROR_MORE_DATA) != hr) {
printf("BluetoothGATTGetCharacteristicValue error sz 0x%08x\n", hr);
continue;
}
PBTH_LE_GATT_CHARACTERISTIC_VALUE pCharValueBuffer = (PBTH_LE_GATT_CHARACTERISTIC_VALUE)calloc(sz, sizeof(PBTH_LE_GATT_CHARACTERISTIC_VALUE));
hr = BluetoothGATTGetCharacteristicValue(
deviceHandle,
&gatt_characteristic,
(ULONG)sz,
pCharValueBuffer,
NULL,
BLUETOOTH_GATT_FLAG_NONE);
if (hr != S_OK) {
printf("BluetoothGATTGetCharacteristicValue error 0x%08x\n", hr);
free(pCharValueBuffer);
continue;
}
if (pCharValueBuffer->DataSize) {
printf("Printing characterstic %d: %s\n", i, pCharValueBuffer->Data);
for (ULONG i = 0; i < pCharValueBuffer->DataSize; i++) {
printf("%02x ", pCharValueBuffer->Data[i]);
}
puts("");
} else {
printf("pCharValueBuffer->DataSize is zero\n");
}
free(pCharValueBuffer);
}
free(gatt_characteristics);
}
free(interface_data);
free(services);
SetupDiDestroyDeviceInfoList(devInfoHandle);
CloseHandle(deviceHandle);
puts("\nPress any key...");
getchar();
return 0;
}
}
probing GUID: 781aee18-7733-4ce4-add091f41c67b592
SetupDiGetClassDevs OK
-------------------------------
SetupDiEnumDeviceInterfaces OK 0
SetupDiGetDeviceInterfaceDetail required_length: 188
path: \\?\bthle#dev_00802534ce0e#7&1c5de6a8&0&00802534ce0e#{781aee18-7733-4ce4-add0-91f41c67b592}
BluetoothGATTGetServices count: 5
----------------
Service 0 GUID:
1800-0000-1000-8000-00805F9B34FB
BluetoothGATTGetCharacteristics count: 2
BluetoothGATTGetCharacteristicValue error sz 0x80070001
BluetoothGATTGetCharacteristicValue error sz 0x80070001
----------------
Service 1 GUID:
1801-0000-1000-8000-00805F9B34FB
BluetoothGATTGetCharacteristics error sz 0x80070490
----------------
Service 2 GUID:
180a-0000-1000-8000-00805F9B34FB
BluetoothGATTGetCharacteristics count: 1
BluetoothGATTGetCharacteristicValue error sz 0x80070001
----------------
Service 3 GUID:
fefb-0000-1000-8000-00805F9B34FB
BluetoothGATTGetCharacteristics count: 4
BTH_LE_GATT_CHARACTERISTIC 0 not readable
BTH_LE_GATT_CHARACTERISTIC 1 not readable
BTH_LE_GATT_CHARACTERISTIC 2 not readable
BTH_LE_GATT_CHARACTERISTIC 3 not readable
----------------
Service 4 GUID:
GUID: 53544d54-4552-494f-5345525631303030
BluetoothGATTGetCharacteristics count: 4
BTH_LE_GATT_CHARACTERISTIC 0 not readable
BTH_LE_GATT_CHARACTERISTIC 1 not readable
BTH_LE_GATT_CHARACTERISTIC 2 not readable
BTH_LE_GATT_CHARACTERISTIC 3 not readable
Press any key...
probing GUID: 1800-00-1000-800000805f9b34fb
SetupDiGetClassDevs OK
-------------------------------
SetupDiEnumDeviceInterfaces OK 0
SetupDiGetDeviceInterfaceDetail required_length: 300
path: \\?\bthenum#{00001800-0000-1000-8000-00805f9b34fb}_vid&00010075_pid&0100#7&25f92e87&0&e0aa96b3cc83_c00000000#{00001800-0000-1000-8000-00805f9b34fb}
CreateFile error: 50
-------------------------------
SetupDiEnumDeviceInterfaces OK 1
SetupDiGetDeviceInterfaceDetail required_length: 318
path: \\?\bthledevice#{00001800-0000-1000-8000-00805f9b34fb}_dev_vid&01008f_pid&b00d_rev&0100_00802534ce0e#8&5481851&5&0001#{00001800-0000-1000-8000-00805f9b34fb}
BluetoothGATTGetServices count: 1
----------------
Service 0 GUID:
1800-0000-1000-8000-00805F9B34FB
BluetoothGATTGetCharacteristics count: 2
Printing characterstic 0: OSTC+ 15175
4f 53 54 43 2b 20 31 35 31 37 35 00
Printing characterstic 1:
00 00
Press any key...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment