From: Peter Henn Date: Tue, 1 Aug 2017 08:59:46 +0000 (+0000) Subject: Bugfix access rights to USB devices X-Git-Tag: v0.1~6 X-Git-Url: http://git.linex4red.de/pub/pl2303-ft232-gpio.git/commitdiff_plain/339fc97efde0b67de29c0c5a95f86393268d75d2 Bugfix access rights to USB devices - cleanup USB search device code - prevent opening any USB device, which has not matching VID/PID - use context pointer instead of NULL --- diff --git a/usb.c b/usb.c index 905df10..ad9a09c 100644 --- a/usb.c +++ b/usb.c @@ -1,4 +1,3 @@ - #include #include #include @@ -34,27 +33,23 @@ struct libusb_device_handle *ncusb_find_and_open(struct libusb_context *ctx, } 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) @@ -69,13 +64,16 @@ struct libusb_device_handle *ncusb_find_and_open(struct libusb_context *ctx, 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); } }