I have some Objective C code, that I can not find the equivalent for Swift (many symbols are not present). It just lists all serial devices that match a prefix:
+ (NSArray*)serialDevicesWithPrefix:(NSString*)prefix {
NSMutableArray* modems=[NSMutableArray arrayWithCapacity:10];
kern_return_t kernResult;
mach_port_t masterPort ;
CFMutableDictionaryRef classesToMatch;
io_iterator_t matchingServices;
NSString* serialDevice=@"/dev/cu.";
serialDevice=[serialDevice stringByAppendingString:prefix];
// Lets find all serial device types ;
// Get the port for communications to kernal
kernResult = IOMainPort(kIOMainPortDefault, &masterPort) ;
if (kernResult == KERN_SUCCESS) {
// We got a good result!
classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue) ;
if (classesToMatch != NULL) {
// Now, we need to say we are looking for RS232 type of connections
// Each serial device object has a property with key
// kIOSerialBSDTypeKey and a value that is one of
// kIOSerialBSDAllTypes, kIOSerialBSDModemType,
// or kIOSerialBSDRS232Type. You can change the
// matching dictionary to find other types of serial
// devices by changing the last parameter in the above call
// to CFDictionarySetValue.
CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDRS232Type));
kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, &matchingServices);
if (kernResult == KERN_SUCCESS) {
// We did something!
// Matching serveres are the ones that "match" our need, now we have to loop through that and get the names
io_object_t modemService;
while ((modemService = IOIteratorNext(matchingServices))) {
CFTypeRef deviceFilePathAsCFString;
// Get the callout device's path (/dev/cu.xxxxx).
// The callout device should almost always be
// used. You would use the dialin device (/dev/tty.xxxxx) when
// monitoring a serial port for
// incoming calls, for example, a fax listener.
deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(modemService,CFSTR(kIOCalloutDeviceKey),kCFAllocatorDefault,0);
if (deviceFilePathAsCFString) {
// We got it!
NSString* currentModem=(__bridge_transfer NSString *)deviceFilePathAsCFString;
if ([currentModem hasPrefix:serialDevice]) {
[modems addObject:currentModem];
}
}
IOObjectRelease(modemService) ; // We need to release it!
}
// We should release our iterator
IOObjectRelease(matchingServices) ;
}
}
}
return modems ;
}
Does anyone have ideas?