-
-
Save PhilCS/878c446a18b510397444ea770d6d1044 to your computer and use it in GitHub Desktop.
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
-(NSArray*) serialDevicesForIOIterator:(io_iterator_t) iter | |
{ | |
// Temporary fix for missing BSD path bug in Monterey beta - begin part 1 of 2 | |
NSMutableDictionary<NSNumber*,NSString*>* sessionIdToBsdPathDict = [NSMutableDictionary dictionary]; | |
io_iterator_t serialDeviceIterator; | |
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching(kIOSerialBSDServiceValue), &serialDeviceIterator) == KERN_SUCCESS) | |
{ | |
io_registry_entry_t serialDevice; | |
while ((serialDevice = IOIteratorNext(serialDeviceIterator))) | |
{ | |
CFStringRef bsdPathAsCFString = IORegistryEntryCreateCFProperty(serialDevice, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0); | |
if (bsdPathAsCFString != nil) | |
{ | |
io_registry_entry_t parent = serialDevice; | |
io_registry_entry_t oldParent = IO_OBJECT_NULL; | |
while (IORegistryEntryGetParentEntry(parent, kIOServicePlane, &parent) == KERN_SUCCESS) | |
{ | |
IOObjectRelease(oldParent); | |
oldParent = parent; | |
CFNumberRef sessionIdAsCFNumber = IORegistryEntryCreateCFProperty(parent, CFSTR("sessionID"), kCFAllocatorDefault, 0); | |
if (sessionIdAsCFNumber != nil) | |
{ | |
sessionIdToBsdPathDict[(NSNumber*)CFBridgingRelease(sessionIdAsCFNumber)] = (__bridge NSString*)(bsdPathAsCFString); | |
break; | |
} | |
} | |
IOObjectRelease(oldParent); | |
CFBridgingRelease(bsdPathAsCFString); | |
} | |
IOObjectRelease(serialDevice); | |
} | |
IOObjectRelease(serialDeviceIterator); | |
} | |
// Temporary fix - end part 1 of 2 | |
NSMutableArray* devices = [ NSMutableArray array]; | |
io_service_t device; | |
while ((device = IOIteratorNext(iter))) { | |
io_name_t deviceName; | |
kern_return_t kr = IORegistryEntryGetName(device, deviceName); | |
if (KERN_SUCCESS != kr) { | |
deviceName[0] = '\0'; | |
} | |
NSString* deviceNameAsString = [ NSString stringWithCString:deviceName encoding:NSASCIIStringEncoding]; | |
CFStringRef bsdPathAsCFString = IORegistryEntrySearchCFProperty(device, | |
kIOServicePlane, | |
CFSTR(kIOCalloutDeviceKey), | |
kCFAllocatorDefault, | |
kIORegistryIterateRecursively); | |
CFStringRef serialNumberAsCFString = IORegistryEntrySearchCFProperty(device, | |
kIOServicePlane, | |
CFSTR(kUSBSerialNumberString), | |
kCFAllocatorDefault, | |
kIORegistryIterateRecursively); | |
CFNumberRef vendorIdAsCFNumber = IORegistryEntrySearchCFProperty(device, | |
kIOServicePlane, | |
CFSTR("idVendor"), | |
kCFAllocatorDefault, | |
kIORegistryIterateRecursively); | |
CFNumberRef productIdIdAsCFNumber = IORegistryEntrySearchCFProperty(device, | |
kIOServicePlane, | |
CFSTR("idProduct"), | |
kCFAllocatorDefault, | |
kIORegistryIterateRecursively); | |
// Temporary fix - begin part 2 of 2 | |
if (bsdPathAsCFString == nil) | |
{ | |
CFNumberRef sessionIdAsCFNumber = IORegistryEntrySearchCFProperty(device, | |
kIOServicePlane, | |
CFSTR("sessionID"), | |
kCFAllocatorDefault, | |
kIORegistryIterateRecursively); | |
if (sessionIdAsCFNumber != nil) | |
{ | |
NSNumber* sessionID = (NSNumber*)CFBridgingRelease(sessionIdAsCFNumber); | |
if ([sessionIdToBsdPathDict objectForKey:sessionID]) | |
bsdPathAsCFString = (CFStringRef)CFBridgingRetain(sessionIdToBsdPathDict[sessionID]); | |
} | |
} | |
// Temporary fix - end part 2 of 2 | |
NSMutableDictionary* dict = [ NSMutableDictionary dictionary]; | |
if ( bsdPathAsCFString != nil) { | |
dict[@"bsdPath"] = [ NSString stringWithString:(__bridge NSString*)(bsdPathAsCFString)]; | |
CFBridgingRelease(bsdPathAsCFString); | |
} | |
if ( serialNumberAsCFString != nil) { | |
dict[@"serialNumber"] = [ NSString stringWithString:(__bridge NSString*)(serialNumberAsCFString)]; | |
CFBridgingRelease(serialNumberAsCFString); | |
} | |
if ( vendorIdAsCFNumber != nil) { | |
dict[@"vendorId"]=(__bridge NSNumber*)(vendorIdAsCFNumber); | |
CFBridgingRelease(vendorIdAsCFNumber); | |
} | |
if ( productIdIdAsCFNumber != nil) { | |
dict[@"productId"]=(__bridge NSNumber*)(productIdIdAsCFNumber); | |
CFBridgingRelease(productIdIdAsCFNumber); | |
} | |
dict[@"name"]=deviceNameAsString; | |
[devices addObject:dict]; | |
IOObjectRelease(device); | |
} | |
return devices; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment