-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
}
for(i = 0; i < cnt; i++) {
- int err = 0;
libusb_device *device = list[i];
struct libusb_device_descriptor desc;
libusb_device_handle *handle;
- err = libusb_open(device, &handle);
- if (err)
- continue;
- int r = libusb_get_device_descriptor( device, &desc );
- if (r) {
- libusb_close(handle);
+ if ( libusb_get_device_descriptor( device, &desc ) )
continue;
- }
- if ( desc.idVendor == vendor && desc.idProduct == product &&
- ncusb_match_string(handle, desc.iManufacturer, vendor_name) &&
- ncusb_match_string(handle, desc.iProduct, product_name) &&
- ncusb_match_string(handle, desc.iSerialNumber, serial)
- )
- {
- found = handle;
+ if ( desc.idVendor == vendor && desc.idProduct == product ) {
+ if ( libusb_open(device, &handle) )
+ continue;
+
+ if (ncusb_match_string(handle, desc.iManufacturer, vendor_name) &&
+ ncusb_match_string(handle, desc.iProduct, product_name) &&
+ ncusb_match_string(handle, desc.iSerialNumber, serial) )
+ {
+ found = handle;
+ }
}
if (found)
void check_handle(libusb_device_handle **h, int vid, int pid, const char* manuf, const char* product, const char* serial)
{
+ static libusb_context *ctx=NULL;
+
if (*h)
return;
- libusb_init(NULL);
- libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_WARNING);
- *h = ncusb_find_and_open(NULL, vid, pid, manuf, product, serial);
+ if (!ctx)
+ libusb_init(&ctx);
+ libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_WARNING);
+ *h = ncusb_find_and_open(ctx, vid, pid, manuf, product, serial);
if (!(*h)) {
- fprintf(stderr, "No PL2303 USB device %04x:%04x found ;(\n", vid, pid);
+ fprintf(stderr, "No USB device %04x:%04x found ;(\n", vid, pid);
exit(1);
}
}