How to create a program to list all the USB devices in a Mac?

17,934

Solution 1

You can adapt USBPrivateDataSample to your needs, the sample sets up a notifier, lists the currently attached devices, then waits for device attach/detach. If you do, you will want to remove the usbVendor and usbProduct matching dictionaries, so all USB devices are matched.

Alternately, you can use IOServiceGetMatchingServices to get an iterator for all current matching services, using a dictionary created by IOServiceMatching(kIOUSBDeviceClassName).

Here's a short sample (which I've never run):

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

int main(int argc, const char *argv[])
{
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    kern_return_t kr;
    io_service_t device;

    /* set up a matching dictionary for the class */
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    if (matchingDict == NULL)
    {
        return -1; // fail
    }

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

    /* iterate */
    while ((device = IOIteratorNext(iter)))
    {
        /* do something with device, eg. check properties */
        /* ... */
        /* And free the reference taken before continuing to the next item */
        IOObjectRelease(device);
    }

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

Solution 2

You just need to access the IOKit Registry. You may well be able to use the ioreg tool to do this (e.g. run it via system() or popen()). If not then you can at least use it to verify your code:

Info on ioreg tool:

$ man ioreg

Get list of USB devices:

$ ioreg -Src IOUSBDevice

Solution 3

If you run system_profiler SPUSBDataType it'll list all the USB devices connected to the system, you can then interact with that data either by dumping it into a text file or reading it from the command into the application and working with it there.

Share:
17,934
Dileep
Author by

Dileep

Hello Everyone I'm an mobile application developer, my area of interest are iOS Swift Objective C Flutter OSX #happycoding

Updated on June 13, 2022

Comments

  • Dileep
    Dileep about 2 years

    I have a limited exposure to the Mac OS X operating system and now I have started using Xcode and am studying about I/O kit. I need to create a program in Xcode under command line tool in order to list all USB devices connected in a Mac system. Those who have previous experience under this, please help me. If anyone could provide me with sample code then it will be of great use, as I am looking for starting point.

  • Dileep
    Dileep over 12 years
    Thanks for your help. It will be very helpful for verification. But i need a program in xcode(in c++) for listing the usb devices
  • Paul R
    Paul R over 12 years
    Did you read the part about using system() or popen() to execute the ioreg tool from within your code ?
  • Dileep
    Dileep over 12 years
    I read it. but i was looing for something like the code given below by hausturkun. this too is useful. but if you give some sample code like i said it will be the best.
  • Dileep
    Dileep over 12 years
    Thanks for your help. It is very useful and this is similar to what iam looking for. when i build it but this code is showing errors like error: IOServiceGetmatchingServices referenced from _main in main.o ...Similar errors for _IOObjectRelease,_IOServiceMatching,_IOIteratorNext,_kIOMast‌​erPortDefault..I created a project under xcode->Command line tool->standard c++. If you may help, i will be very thankful to you
  • Dileep
    Dileep over 12 years
    Errors which i got while building................. Undefined symbols: "_kIOMasterPortDefault", referenced from: _kIOMasterPortDefault$non_lazy_ptr in main.o "_IOServiceGetMatchingServices", referenced from: _main in main.o "_IOObjectRelease", referenced from: _main in main.o _main in main.o "_IOServiceMatching", referenced from: _main in main.o "_IOIteratorNext", referenced from: _main in main.o ld: symbol(s) not found collect2: ld returned 1 exit status
  • Hasturkun
    Hasturkun over 12 years
    You need to use the I/O Kit framework (ie. IOKit.framework) and Core Foundation frameworks in your project, or this won't link.
  • Dileep
    Dileep over 12 years
    thanks. Now building is successful and program runs too. As i told before i just need to list the usb devices. please help me with some command which will just list usb devices. i was just looking for some sample code like this, this is very helpful to me.
  • Dileep
    Dileep over 12 years
    /***Display the device names ***/.................... io_name_t deviceName; kr = IORegistryEntryGetName(device, deviceName); if (KERN_SUCCESS != kr) { deviceName[0] = '\0'; } printf("\ndeviceName:%s",deviceName);.....................‌​....this gives me device name...Now iam looking for program which is dynamic that is which also shows attaching and detaching of devices..Pls help
  • Hasturkun
    Hasturkun over 12 years
    @DILi: Check out the USBPrivateDataSample I linked to, it uses attach/detach notifications.