Closed pedroos closed 4 years ago
Please see: #373.
As a maintainer of libusb/hidapi
I vote against this PR for a number of reasons:
size_t vendor_id_length = sizeof(vendor_ids) / sizeof(vendor_ids[0]);
(almost) always evaluates to 2
; see here: https://stackoverflow.com/questions/17590226/finding-length-of-array-inside-a-functionhid_enumerate
;Why don't just use simple hid_enumerate
?
It will give you same result, for all supported platforms:
bool device_matches(struct hid_device_info *dev) {
unsigned short vendor_ids[] = ...;
unsigned short product_ids[] = ...;
int products_size = 2;
for (int i = 0; i < products_size; i++) {
if (dev->vendor_id == vendor_ids[i] && dev->product_id == product_ids[i])
return true;
}
return false;
}
// use your device here, or save it somewhere for later
bool use_dev(struct hid_device_info *dev);
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
if (device_matches(cur_dev)) {
use_dev(cur_dev);
}
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
Yeah, I hadn't seen the 0x0 option. Thanks for pointing that out.
This additional function (
enumerate_m()
) works the same asenumerate()
, but enables a set of specific vendor ids and product ids to be specified. This avoids manual filtering of results when more than one vendor id/product id combination needs to be enumerated.