3

Is there a way, using IOKit or something similar that does not involve downloading additional packages from the internet, that I can use to read a USB device's product name?

This is my current code...

func printSerialPaths(portIterator: io_iterator_t) {
    var serialService: io_object_t
    repeat {
        serialService = IOIteratorNext(portIterator)
        if (serialService != 0) {
            var key: CFString! = "IOCalloutDevice"
            var bsdPathAsCFtring: AnyObject? = IORegistryEntryCreateCFProperty(serialService, key, kCFAllocatorDefault, 0).takeUnretainedValue()
            var bsdPath = bsdPathAsCFtring as! String?
            if let path = bsdPath {
                print(path)
            }

            var deviceNameCString: [CChar] = [CChar](count: 128, repeatedValue: 0)
            let deviceNameResult = IORegistryEntryGetName(serialService, &deviceNameCString)
            let deviceName = String.fromCString(&deviceNameCString)!
            print("usb Device Name: \(deviceName)")


        }
    } while serialService != 0;
}

I have also tried using other CFStrings, such as "Product Name" in the IORegistryEntryCreateCFProperty() command as I've seen suggested elsewhere with no luck. If replacing that is all I need, where can I find the documentation for the rest of these strings?

The product name that I'm talking about is highlighted below. I'm not sure what its technical name would be.

enter image description here

bmike
  • 917
  • 2
  • 22
  • 41
crait
  • 156
  • 1
  • 4
  • 15

1 Answers1

1

If the io_service_t handle is to an IOUSBDevice/IOUSBHostDevice, it should have a property named "USB Product Name" (symbolic constant kUSBProductString, at least in C) - I believe that's what you're after. You can query it with IORegistryEntryCreateCFProperty() as you're already doing for the "IOCalloutDevice" property, which by the way is defined as the symbolic constant kIOCalloutDeviceKey.

If those constants do not exist in Swift when importing the IOKit module, just define your own constants and file a bug (Radar) with Apple about the omission.

pmdj
  • 22,018
  • 3
  • 52
  • 103
  • That includes a bunch of good places to start researching this, but unfortunately, I've come up with nothing.`kUSBProductString` doesn't exist in Swift. Searching for others, I have found stuff like `kUSBVendorName`, but they seem to only work in other languages like Objective-C and are causing me fatal errors. – crait Aug 31 '16 at 04:14
  • 1
    In that case, it should be pretty safe to define your own versions of these constants in Swift, and just use those. They're not going to change, except that OSX 10.11 introduced new ones, and sets the property values for both the old and new keys. (This is to do with the new USB stack, although the reason why Apple created new property names is not clear.) – pmdj Aug 31 '16 at 06:56
  • 3
    Using a bridging header helps me get most constants - they're not always accessible if I just import IOKit in Swift. http://stackoverflow.com/a/39026607/2227743 – Eric Aya Aug 31 '16 at 08:08