while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
printf("failed to get device descriptor\n");
return;
}
int ret = libusb_open(dev, &handle);
if (LIBUSB_SUCCESS == ret) {
if (desc.iProduct) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string)); //显示产品信息
if (ret > 0)
printf(", %s", string);
}
}
printf("\n");
if (handle)
libusb_close(handle);
}
}
//列出系统所有的USB设备.
//包括:bus number. device number, vid, pid, product info
int list_devices(void)
{
libusb_device **devs;
int r;
ssize_t cnt;
for (i = 0; i < desc->bNumConfigurations; i++) {
struct libusb_config_descriptor *config;
int ret = libusb_get_config_descriptor(dev, i, &config);
if (LIBUSB_SUCCESS != ret) {
printf("Couldn't retrieve descriptors\n");
continue;
}
r = libusb_init(&ctx); //initialize the library for the session we just declared
if(r < 0) {
printf("libusb init faild, err:%s\n", libusb_error_name(r)); //there was an error
return -1;
}
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
printf("hotplug capabilites are not supported on this platform.\n");
libusb_exit (NULL);
return -1;
}