Last active
July 11, 2018 13:44
-
-
Save armadsen/5131559 to your computer and use it in GitHub Desktop.
Example code to get USB device properties for a given instance of ORSSerialPort. Will only work for USB-to-serial adapters, not internal serial ports. Based on https://gist.github.com/rhwood/4124251.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <IOKit/usb/USBSpec.h> | |
#import "ORSSerialPort.h" | |
@interface ORSSerialPort (Attributes) | |
@property (nonatomic, readonly) NSDictionary *ioDeviceAttributes; | |
@property (nonatomic, readonly) NSNumber *vendorID; | |
@property (nonatomic, readonly) NSNumber *productID; | |
@end | |
@implementation ORSSerialPort (Attributes) | |
- (NSDictionary *)ioDeviceAttributes | |
{ | |
NSDictionary *result = nil; | |
io_iterator_t iterator = 0; | |
if (IORegistryEntryCreateIterator(self.IOKitDevice, | |
kIOServicePlane, | |
kIORegistryIterateRecursively + kIORegistryIterateParents, | |
&iterator) != KERN_SUCCESS) return nil; | |
io_object_t device = 0; | |
while ((device = IOIteratorNext(iterator)) && result == nil) | |
{ | |
CFMutableDictionaryRef usbProperties = 0; | |
if (IORegistryEntryCreateCFProperties(device, &usbProperties, kCFAllocatorDefault, kNilOptions) != KERN_SUCCESS) | |
{ | |
IOObjectRelease(device); | |
continue; | |
} | |
NSDictionary *properties = CFBridgingRelease(usbProperties); | |
NSNumber *vendorID = properties[(__bridge NSString *)CFSTR(kUSBVendorID)]; | |
NSNumber *productID = properties[(__bridge NSString *)CFSTR(kUSBProductID)]; | |
if (!vendorID || !productID) { IOObjectRelease(device); continue; } // not a USB device | |
result = properties; | |
IOObjectRelease(device); | |
} | |
IOObjectRelease(iterator); | |
return result; | |
} | |
- (NSNumber *)vendorID; | |
{ | |
return [self ioDeviceAttributes][(__bridge NSString *)CFSTR(kUSBVendorID)]; | |
} | |
- (NSNumber *)productID; | |
{ | |
return [self ioDeviceAttributes][(__bridge NSString *)CFSTR(kUSBProductID)]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment