Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: Detecting USB devices by Vendor ID

I'm writing a Foundation tool for Mac and trying to detect when Apple devices are connected and disconnected via USB. I found some help in this post along with the USBPrivateDataSample -- but it seems to only be working if I provide both a vendor ID and a product ID. I'd like to eliminate the product ID and find detect all USB events on Apple devices (vendor ID 1452). Any help here?

Here's my code that doesn't seem to detect any devices:

#include <CoreFoundation/CoreFoundation.h>

#include <IOKit/IOKitLib.h>
#include <IOKit/IOMessage.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>

#define kMyVendorID 1452

int list_devices(void);

int list_devices(void){
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    kern_return_t kr;
    io_service_t device;
    CFNumberRef numberRef;
    long usbVendor = kMyVendorID;

    /* set up a matching dictionary for the class */
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);    // Interested in instances of class
    // IOUSBDevice and its subclasses
    if (matchingDict == NULL) {
        fprintf(stderr, "IOServiceMatching returned NULL.\n");
        return -1;
    }

    // We are interested in all USB devices (as opposed to USB interfaces).  The Common Class Specification
    // tells us that we need to specify the idVendor, idProduct, and bcdDevice fields, or, if we're not interested
    // in particular bcdDevices, just the idVendor and idProduct.  Note that if we were trying to match an 
    // IOUSBInterface, we would need to set more values in the matching dictionary (e.g. idVendor, idProduct, 
    // bInterfaceNumber and bConfigurationValue.

    // Create a CFNumber for the idVendor and set the value in the dictionary
    numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbVendor);
    CFDictionarySetValue(matchingDict, 
                         CFSTR(kUSBVendorID), 
                         numberRef);
    CFRelease(numberRef);
    numberRef = NULL;    


    /* Now we have a dictionary, get an iterator.*/
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
    if (kr != KERN_SUCCESS)
        return -1;

    /* iterate */
    while ((device = IOIteratorNext(iter)))
    {
        io_name_t       deviceName;
        CFStringRef     deviceNameAsCFString;   

        /* do something with device, eg. check properties */
        /* ... */
        /* And free the reference taken before continuing to the next item */

        // Get the USB device's name.
        kr = IORegistryEntryGetName(device, deviceName);
        if (KERN_SUCCESS != kr) {
            deviceName[0] = '\0';
        }

        deviceNameAsCFString = CFStringCreateWithCString(kCFAllocatorDefault, deviceName, 
                                                         kCFStringEncodingASCII);

        // Dump our data to stderr just to see what it looks like.
        fprintf(stderr, "deviceName: ");
        CFShow(deviceNameAsCFString);

        IOObjectRelease(device);
    }

    /* Done, release the iterator */
    IOObjectRelease(iter);

    return 1;
}

int main(int argc, char* argv[])
{
    while(1){
        list_devices();
        sleep(1);
    }
    return 0;
}

Of note: If I add a product ID to the matchingDict and plug in such a device it will find the device no problem (without changing the vendor ID). But I can't find it with the vendor ID alone.

like image 498
skandocious Avatar asked Jun 01 '12 02:06

skandocious


People also ask

What is the vendor and Product ID for my USB device?

The Vendor and Product ID are required when developing a device driver in VISA Driver Development Wizard. This article shows you how to find this information for your USB device.

How do I find the device ID of a USB device?

USB Device Viewer offers a convenient method for obtaining device information, such as Product ID or Vendor ID. Install USBView. Launch USBView.exe from C:\Program Files (x86)\Windows Kits\10\Debuggers\x64.

What is the vendor ID of 2N Telekomunikace?

Vendor ID Vendor Name More 08de Vendor Devices 18f8 [Maxxter] Vendor Devices 1c87 2N TELEKOMUNIKACE a.s. Vendor Devices 1630 2Wire, Inc. Vendor Devices 48 more rows ...

How do I get the GUID of a USB device?

To achieve this, there are a few different techniques you can use. If, for example, you want to get the USB device’s GUID and Vendor & Product IDs, you’ll want to use the WMI (Windows Management Interface) to do this. Typically, WMI is quite complicated and very, very code heavy, so we’ll leave this one for a future post.


1 Answers

To get list of all products that belongs to particular vendor you can use wildcards in product ID field .A sample matching condition is as below:

            long vid = 1452; //Apple vendor ID
        CFNumberRef refVendorId = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &vid);
        CFDictionarySetValue (matchingDict, CFSTR ("idVendor"), refVendorId);
        CFRelease(refVendorId);
        //all product by same vendor
        CFDictionarySetValue (matchingDict, CFSTR ("idProduct"), CFSTR("*"));
like image 133
Raviprakash Avatar answered Sep 30 '22 08:09

Raviprakash