Skip to content

Commit 0f2cf88

Browse files
authored
RFC: Add HID bus type in hid_device_info (signal11#308)
1 parent ceb41e0 commit 0f2cf88

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

hidapi/hidapi.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,37 @@ extern "C" {
100100
struct hid_device_;
101101
typedef struct hid_device_ hid_device; /**< opaque hidapi structure */
102102

103+
/** @brief HID underlying bus types.
104+
105+
@ingroup API
106+
*/
107+
typedef enum {
108+
/* Unknown bus type */
109+
HID_API_BUS_UNKNOWN = 0x00,
110+
111+
/* USB bus
112+
Specifications:
113+
https://usb.org/hid */
114+
HID_API_BUS_USB = 0x01,
115+
116+
/* Bluetooth or Bluetooth LE bus
117+
Specifications:
118+
https://www.bluetooth.com/specifications/specs/human-interface-device-profile-1-1-1/
119+
https://www.bluetooth.com/specifications/specs/hid-service-1-0/
120+
https://www.bluetooth.com/specifications/specs/hid-over-gatt-profile-1-0/ */
121+
HID_API_BUS_BLUETOOTH = 0x02,
122+
123+
/* I2C bus
124+
Specifications:
125+
https://docs.microsoft.com/previous-versions/windows/hardware/design/dn642101(v=vs.85) */
126+
HID_API_BUS_I2C = 0x03,
127+
128+
/* SPI bus
129+
Specifications:
130+
https://www.microsoft.com/download/details.aspx?id=103325 */
131+
HID_API_BUS_SPI = 0x04,
132+
} hid_bus_type;
133+
103134
/** hidapi info structure */
104135
struct hid_device_info {
105136
/** Platform-specific device path */
@@ -135,6 +166,11 @@ extern "C" {
135166

136167
/** Pointer to the next device */
137168
struct hid_device_info *next;
169+
170+
/** Underlying bus type
171+
Since version 0.13.0, @ref HID_API_VERSION >= HID_API_MAKE_VERSION(0, 13, 0)
172+
*/
173+
hid_bus_type bus_type;
138174
};
139175

140176

hidtest/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void print_device(struct hid_device_info *cur_dev) {
6060
printf(" Release: %hx\n", cur_dev->release_number);
6161
printf(" Interface: %d\n", cur_dev->interface_number);
6262
printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
63+
printf(" Bus type: %d\n", cur_dev->bus_type);
6364
printf("\n");
6465
}
6566

libusb/hid.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,8 @@ static struct hid_device_info * create_device_info_for_device(libusb_device *dev
651651

652652
cur_dev->interface_number = interface_num;
653653

654+
cur_dev->bus_type = HID_API_BUS_USB;
655+
654656
cur_dev->path = make_path(device, config_number, interface_num);
655657

656658
if (!handle) {

linux/hid.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,11 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
672672
break;
673673
}
674674

675-
/* Manufacturer and Product strings */
676675
cur_dev->manufacturer_string = copy_udev_string(usb_dev, "manufacturer");
677676
cur_dev->product_string = copy_udev_string(usb_dev, "product");
678677

679-
/* Release Number */
678+
cur_dev->bus_type = HID_API_BUS_USB;
679+
680680
str = udev_device_get_sysattr_value(usb_dev, "bcdDevice");
681681
cur_dev->release_number = (str)? strtol(str, NULL, 16): 0x0;
682682

@@ -693,11 +693,18 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
693693
break;
694694

695695
case BUS_BLUETOOTH:
696+
cur_dev->manufacturer_string = wcsdup(L"");
697+
cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);
698+
699+
cur_dev->bus_type = HID_API_BUS_BLUETOOTH;
700+
701+
break;
696702
case BUS_I2C:
697-
/* Manufacturer and Product strings */
698703
cur_dev->manufacturer_string = wcsdup(L"");
699704
cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);
700705

706+
cur_dev->bus_type = HID_API_BUS_I2C;
707+
701708
break;
702709

703710
default:
@@ -745,6 +752,7 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
745752
cur_dev->product_string = prev_dev->product_string? wcsdup(prev_dev->product_string): NULL;
746753
cur_dev->usage_page = page;
747754
cur_dev->usage = usage;
755+
cur_dev->bus_type = prev_dev->bus_type;
748756
}
749757
}
750758

mac/hid.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ static struct hid_device_info *create_device_info_with_usage(IOHIDDeviceRef dev,
378378
unsigned short dev_pid;
379379
int BUF_LEN = 256;
380380
wchar_t buf[BUF_LEN];
381+
CFTypeRef transport_prop;
381382

382383
struct hid_device_info *cur_dev;
383384
io_object_t iokit_dev;
@@ -456,6 +457,22 @@ static struct hid_device_info *create_device_info_with_usage(IOHIDDeviceRef dev,
456457
cur_dev->interface_number = -1;
457458
}
458459

460+
/* Bus Type */
461+
transport_prop = IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDTransportKey));
462+
463+
if (transport_prop != NULL && CFGetTypeID(transport_prop) == CFStringGetTypeID()) {
464+
if (CFStringCompare((CFStringRef)transport_prop, CFSTR(kIOHIDTransportUSBValue), 0) == kCFCompareEqualTo) {
465+
cur_dev->bus_type = HID_API_BUS_USB;
466+
/* Match "Bluetooth", "BluetoothLowEnergy" and "Bluetooth Low Energy" strings */
467+
} else if (CFStringHasPrefix((CFStringRef)transport_prop, CFSTR(kIOHIDTransportBluetoothValue))) {
468+
cur_dev->bus_type = HID_API_BUS_BLUETOOTH;
469+
} else if (CFStringCompare((CFStringRef)transport_prop, CFSTR(kIOHIDTransportI2CValue), 0) == kCFCompareEqualTo) {
470+
cur_dev->bus_type = HID_API_BUS_I2C;
471+
} else if (CFStringCompare((CFStringRef)transport_prop, CFSTR(kIOHIDTransportSPIValue), 0) == kCFCompareEqualTo) {
472+
cur_dev->bus_type = HID_API_BUS_SPI;
473+
}
474+
}
475+
459476
return cur_dev;
460477
}
461478

windows/hid.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,43 @@ static void hid_internal_get_info(const wchar_t* interface_path, struct hid_devi
510510
/* Normalize to upper case */
511511
for (wchar_t* p = compatible_id; *p; ++p) *p = towupper(*p);
512512

513+
/* USB devices
514+
https://docs.microsoft.com/windows-hardware/drivers/hid/plug-and-play-support
515+
https://docs.microsoft.com/windows-hardware/drivers/install/standard-usb-identifiers */
516+
if (wcsstr(compatible_id, L"USB") != NULL) {
517+
dev->bus_type = HID_API_BUS_USB;
518+
break;
519+
}
520+
521+
/* Bluetooth devices
522+
https://docs.microsoft.com/windows-hardware/drivers/bluetooth/installing-a-bluetooth-device */
523+
if (wcsstr(compatible_id, L"BTHENUM") != NULL) {
524+
dev->bus_type = HID_API_BUS_BLUETOOTH;
525+
break;
526+
}
527+
513528
/* Bluetooth LE devices */
514529
if (wcsstr(compatible_id, L"BTHLEDEVICE") != NULL) {
515530
/* HidD_GetProductString/HidD_GetManufacturerString/HidD_GetSerialNumberString is not working for BLE HID devices
516531
Request this info via dev node properties instead.
517532
https://docs.microsoft.com/answers/questions/401236/hidd-getproductstring-with-ble-hid-device.html */
518533
hid_internal_get_ble_info(dev, dev_node);
534+
535+
dev->bus_type = HID_API_BUS_BLUETOOTH;
536+
break;
537+
}
538+
539+
/* I2C devices
540+
https://docs.microsoft.com/windows-hardware/drivers/hid/plug-and-play-support-and-power-management */
541+
if (wcsstr(compatible_id, L"PNP0C50") != NULL) {
542+
dev->bus_type = HID_API_BUS_I2C;
543+
break;
544+
}
545+
546+
/* SPI devices
547+
https://docs.microsoft.com/windows-hardware/drivers/hid/plug-and-play-for-spi */
548+
if (wcsstr(compatible_id, L"PNP0C51") != NULL) {
549+
dev->bus_type = HID_API_BUS_SPI;
519550
break;
520551
}
521552
}

0 commit comments

Comments
 (0)