]> err.no Git - yubikey-personalization/commitdiff
rework _ykusb_open_device() in the ykcore backends
authorKlas Lindfors <klas@yubico.com>
Fri, 30 Nov 2012 11:49:57 +0000 (12:49 +0100)
committerKlas Lindfors <klas@yubico.com>
Fri, 30 Nov 2012 12:39:23 +0000 (13:39 +0100)
it now takes an array of product ids to search for,
let yk_open_first_key() search for all known keys with
a YubiKey interface.

ykcore/ykcore.c
ykcore/ykcore_backend.h
ykcore/ykcore_libusb-1.0.c
ykcore/ykcore_libusb.c
ykcore/ykcore_osx.c
ykcore/ykcore_stub.c
ykcore/ykcore_windows.c

index f3985d228d7db580d0127e094aa3b010220b9aef..02adf7bf758c8fecb240be975f6fd8fada7a4361 100644 (file)
@@ -59,13 +59,10 @@ int yk_release(void)
 
 YK_KEY *yk_open_first_key(void)
 {
-       YK_KEY *yk = _ykusb_open_device(YUBICO_VID, YUBIKEY_PID);
-       int rc = yk_errno;
+       int pids[] = {YUBIKEY_PID, NEO_OTP_PID, NEO_OTP_CCID_PID};
 
-       if(!yk) {
-               yk = _ykusb_open_device(YUBICO_VID, YUBIKEY_NEO_PID);
-               rc = yk_errno;
-       }
+       YK_KEY *yk = _ykusb_open_device(YUBICO_VID, pids, sizeof(pids));
+       int rc = yk_errno;
 
        if (yk) {
                rc = 0;
index b5917cfb9cab52c1825b20dc07ae72ad674ae8ef..b8e1e48ac5c90a6c1e42298bf533247e88dd1e9c 100644 (file)
@@ -39,7 +39,7 @@
 int _ykusb_start(void);
 int _ykusb_stop(void);
 
-void * _ykusb_open_device(int vendor_id, int product_id);
+void * _ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len);
 int _ykusb_close_device(void *);
 
 int _ykusb_read(void *dev, int report_type, int report_number,
index 115606585c6c127e7410861556ebfa1dcdb0132c..e6e04b5750f727c06cbf9d48ab197f95c05686e6 100644 (file)
@@ -157,7 +157,7 @@ extern int _ykusb_stop(void)
        return 0;
 }
 
-void *_ykusb_open_device(int vendor_id, int product_id)
+void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len)
 {
        libusb_device *dev;
        libusb_device_handle *h = NULL;
@@ -173,7 +173,17 @@ void *_ykusb_open_device(int vendor_id, int product_id)
                if (ykl_errno != 0)
                        goto done;
 
-               if (desc.idVendor == vendor_id && desc.idProduct == product_id) {
+               if (desc.idVendor == vendor_id) {
+                       size_t j;
+                       for(j = 0; j < pids_len; j++) {
+                               if (desc.idProduct == product_ids[j]) {
+                                       break;
+                               }
+                       }
+                       if (j == pids_len) {
+                               goto done;
+                       }
+
                        rc = YK_EUSBERR;
                        ykl_errno = libusb_open(dev, &h);
                        if (ykl_errno != 0)
index dac4fc1ef9b0c3c9d30ae2fffafd1c517cd19170..adae378e2949fac51d4284343b62a252649cb3f3 100644 (file)
@@ -145,7 +145,7 @@ extern int _ykusb_stop(void)
        return 1;
 }
 
-void *_ykusb_open_device(int vendor_id, int product_id)
+void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len)
 {
        struct usb_bus *bus;
        struct usb_device *dev;
@@ -155,8 +155,17 @@ void *_ykusb_open_device(int vendor_id, int product_id)
        for (bus = usb_get_busses(); bus; bus = bus->next) {
                rc = YK_ENOKEY;
                for (dev = bus->devices; dev; dev = dev->next)
-                       if (dev->descriptor.idVendor == vendor_id
-                           && dev->descriptor.idProduct == product_id) {
+                       if (dev->descriptor.idVendor == vendor_id) {
+                               size_t j;
+                               for (j = 0; j < pids_len; j++) {
+                                       if (dev->descriptor.idProduct == prduct_ids[j]) {
+                                               break;
+                                       }
+                               }
+                               if(j == pids_len) {
+                                       goto done;
+                               }
+
                                rc = YK_EUSBERR;
                                h = usb_open(dev);
 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
index 839809adcb34d8cdb78f6be4c9ca030c61a6c45c..22abdc0b60a584661b53fdbb761ef78cea591b24 100644 (file)
@@ -68,24 +68,35 @@ static void _ykosx_CopyToCFArray(const void *value, void *context)
        CFArrayAppendValue( ( CFMutableArrayRef ) context, value );
 }
 
-void *_ykusb_open_device(int vendor_id, int product_id)
+void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len)
 {
        void *yk = NULL;
-       CFDictionaryRef dict;
-       CFStringRef keys[2];
-       CFStringRef values[2];
 
        int rc = YK_ENOKEY;
 
+       size_t i;
+
+       CFMutableArrayRef matches = CFArrayCreateMutable( kCFAllocatorDefault, pids_len, kCFTypeArrayCallBacks );
+
        CFNumberRef vendorID = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &vendor_id );
-       CFNumberRef productID = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &product_id );
+       for(i = 0; i < pids_len; i++) {
+               CFDictionaryRef dict;
+               CFStringRef keys[2];
+               CFStringRef values[2];
+
+               CFNumberRef productID = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &product_ids[i] );
 
-       keys[0] = CFSTR( kIOHIDVendorIDKey );  values[0] = (void *) vendorID;
-       keys[1] = CFSTR( kIOHIDProductIDKey ); values[1] = (void *) productID;
+               keys[0] = CFSTR( kIOHIDVendorIDKey );  values[0] = (void *) vendorID;
+               keys[1] = CFSTR( kIOHIDProductIDKey ); values[1] = (void *) productID;
 
-       dict = CFDictionaryCreate( kCFAllocatorDefault, (const void **) &keys, (const void **) &values, 1, NULL, NULL);
+               dict = CFDictionaryCreate( kCFAllocatorDefault, (const void **) &keys, (const void **) &values, 1, NULL, NULL);
+               CFArrayAppendValue( matches, dict );
+
+               CFRelease( productID );
+               CFRelease( dict );
+       }
 
-       IOHIDManagerSetDeviceMatching( ykosxManager, dict );
+       IOHIDManagerSetDeviceMatchingMultiple( ykosxManager, matches );
 
        CFSetRef devSet = IOHIDManagerCopyDevices( ykosxManager );
 
@@ -110,13 +121,12 @@ void *_ykusb_open_device(int vendor_id, int product_id)
                IOHIDManagerScheduleWithRunLoop( ykosxManager, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode );
                IOHIDManagerUnscheduleFromRunLoop( ykosxManager, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode );
 
+               CFRelease( vendorID );
                CFRelease( array );
                CFRelease( devSet );
        }
 
-       CFRelease( dict );
-       CFRelease( vendorID );
-       CFRelease( productID );
+       CFRelease( matches );
 
        if (yk) {
                _ykusb_IOReturn = IOHIDDeviceOpen( yk, 0L );
index ef1671fab0e232a98ddcae4c88fcdbf7aac4d724..083d2be2aad02e733753cba6a93037b85a4442e5 100644 (file)
@@ -46,7 +46,7 @@ int _ykusb_stop(void)
        return 0;
 }
 
-void * _ykusb_open_device(int vendor_id, int product_id)
+void * _ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len)
 {
        yk_errno = YK_ENOTYETIMPL;
        return NULL;
index b08590f79cb78bc067ac66d1896b917fcc0303f9..64f16694163d7bfa032414cd67de380abca1b1ca 100644 (file)
@@ -49,7 +49,7 @@ int _ykusb_stop(void)
        return 1;
 }
 
-void * _ykusb_open_device(int vendor_id, int product_id)
+void * _ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len)
 {
        HDEVINFO hi;
        SP_DEVICE_INTERFACE_DATA di;
@@ -92,7 +92,17 @@ void * _ykusb_open_device(int vendor_id, int product_id)
                                HIDD_ATTRIBUTES devInfo;
 
                                if (HidD_GetAttributes(m_handle, &devInfo)) {
-                                       if (devInfo.VendorID == vendor_id && devInfo.ProductID == product_id) {
+                                       if (devInfo.VendorID == vendor_id) {
+                                               size_t j;
+                                               for (j = 0; j < pids_len; j++) {
+                                                       if (devInfo.ProductID == product_ids[j]) {
+                                                               break;
+                                                       }
+                                               }
+                                               if (j == pids_len) {
+                                                       goto done;
+                                               }
+
                                                ret_handle = m_handle;
                                                free (pi);
                                                goto done;