| 388 | } |
| 389 | |
| 390 | struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id) |
| 391 | { |
| 392 | libusb_device **devs; |
| 393 | libusb_device *dev; |
| 394 | libusb_device_handle *handle; |
| 395 | ssize_t num_devs; |
| 396 | int i = 0; |
| 397 | |
| 398 | struct hid_device_info *root = NULL; /* return object */ |
| 399 | struct hid_device_info *cur_dev = NULL; |
| 400 | |
| 401 | if(hid_init() < 0) |
| 402 | return NULL; |
| 403 | |
| 404 | num_devs = libusb_get_device_list(usb_context, &devs); |
| 405 | if (num_devs < 0) |
| 406 | return NULL; |
| 407 | while ((dev = devs[i++]) != NULL) { |
| 408 | struct libusb_device_descriptor desc; |
| 409 | struct libusb_config_descriptor *conf_desc = NULL; |
| 410 | int j, k; |
| 411 | int interface_num = 0; |
| 412 | |
| 413 | int res = libusb_get_device_descriptor(dev, &desc); |
| 414 | unsigned short dev_vid = desc.idVendor; |
| 415 | unsigned short dev_pid = desc.idProduct; |
| 416 | |
| 417 | res = libusb_get_active_config_descriptor(dev, &conf_desc); |
| 418 | if (res < 0) |
| 419 | libusb_get_config_descriptor(dev, 0, &conf_desc); |
| 420 | if (conf_desc) { |
| 421 | for (j = 0; j < conf_desc->bNumInterfaces; j++) { |
| 422 | const struct libusb_interface *intf = &conf_desc->interface[j]; |
| 423 | for (k = 0; k < intf->num_altsetting; k++) { |
| 424 | const struct libusb_interface_descriptor *intf_desc; |
| 425 | intf_desc = &intf->altsetting[k]; |
| 426 | if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID) { |
| 427 | interface_num = intf_desc->bInterfaceNumber; |
| 428 | |
| 429 | /* Check the VID/PID against the arguments */ |
| 430 | if ((vendor_id == 0x0 || vendor_id == dev_vid) && |
| 431 | (product_id == 0x0 || product_id == dev_pid)) { |
| 432 | struct hid_device_info *tmp; |
| 433 | |
| 434 | /* VID/PID match. Create the record. */ |
| 435 | tmp = calloc(1, sizeof(struct hid_device_info)); |
| 436 | if (cur_dev) { |
| 437 | cur_dev->next = tmp; |
| 438 | } |
| 439 | else { |
| 440 | root = tmp; |
| 441 | } |
| 442 | cur_dev = tmp; |
| 443 | |
| 444 | /* Fill out the record */ |
| 445 | cur_dev->next = NULL; |
| 446 | cur_dev->path = make_path(dev, interface_num); |
| 447 |
no test coverage detected