10 static int ncusb_match_string(libusb_device_handle
*dev
, int index
, const char* string
)
12 unsigned char tmp
[256];
13 libusb_get_string_descriptor_ascii(dev
, index
, tmp
, 256);
15 return 1; /* NULL matches anything */
16 return (strcmp(string
, (char*) tmp
)==0);
20 struct libusb_device_handle
*ncusb_find_and_open(struct libusb_context
*ctx
,
21 int vendor
, int product
,
22 const char *vendor_name
,
23 const char *product_name
,
26 libusb_device_handle
*found
= NULL
;
28 ssize_t cnt
= libusb_get_device_list(ctx
, &list
);
35 for(i
= 0; i
< cnt
; i
++) {
36 libusb_device
*device
= list
[i
];
37 struct libusb_device_descriptor desc
;
38 libusb_device_handle
*handle
;
40 if ( libusb_get_device_descriptor( device
, &desc
) )
43 if ( desc
.idVendor
== vendor
&& desc
.idProduct
== product
) {
44 if ( libusb_open(device
, &handle
) )
47 if (ncusb_match_string(handle
, desc
.iManufacturer
, vendor_name
) &&
48 ncusb_match_string(handle
, desc
.iProduct
, product_name
) &&
49 ncusb_match_string(handle
, desc
.iSerialNumber
, serial
) )
59 libusb_free_device_list(list
, 1);
65 void check_handle(libusb_device_handle
**h
, int vid
, int pid
, const char* manuf
, const char* product
, const char* serial
)
67 static libusb_context
*ctx
=NULL
;
73 libusb_set_debug(ctx
, LIBUSB_LOG_LEVEL_WARNING
);
74 *h
= ncusb_find_and_open(ctx
, vid
, pid
, manuf
, product
, serial
);
76 fprintf(stderr
, "No USB device %04x:%04x found ;(\n", vid
, pid
);