Bugfix access rights to USB devices
authorPeter Henn <Peter.Henn@web.de>
Tue, 1 Aug 2017 08:59:46 +0000 (08:59 +0000)
committerPeter Henn <Peter.Henn@web.de>
Tue, 1 Aug 2017 08:59:46 +0000 (08:59 +0000)
- cleanup USB search device code
- prevent opening any USB device, which has not matching VID/PID
- use context pointer instead of NULL

usb.c

diff --git a/usb.c b/usb.c
index 905df10..ad9a09c 100644 (file)
--- a/usb.c
+++ b/usb.c
@@ -1,4 +1,3 @@
-
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -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);
        }
 }