static PyObject * osxserialports_modems(PyObject *self, PyObject *args) { kern_return_t kernResult; io_iterator_t serialPortIterator; PyObject *ret = NULL; char *argstring; if (!PyArg_ParseTuple(args, "", &argstring)) /* No arguments */ return NULL; kernResult = FindModems(&serialPortIterator); ret = GetModemList(serialPortIterator); IOObjectRelease(serialPortIterator); /* Release the iterator. */ if (EX_OK != kernResult) { Py_XDECREF(ret); return NULL; } else { return ret; } }
int tty_find_device(buffer *dev_name) { kern_return_t kernResult; io_iterator_t serialPortIterator; kernResult = FindModems(&serialPortIterator); if (kernResult != KERN_SUCCESS) { return -1; } kernResult = GetModemPath(serialPortIterator, dev_name); IOObjectRelease(serialPortIterator); // Release the iterator. if (kernResult != KERN_SUCCESS) { return -1; } return 0; }
static stDeviceListItem* GetSerialDevices() { char bsdPath[MAXPATHLEN]; io_iterator_t serialPortIterator; FindModems(&serialPortIterator); kern_return_t kernResult = KERN_FAILURE; Boolean modemFound = false; // Initialize the returned path *bsdPath = '\0'; stDeviceListItem* devices = NULL; stDeviceListItem* lastDevice = NULL; int length = 0; io_service_t modemService; while ((modemService = IOIteratorNext(serialPortIterator))) { CFTypeRef bsdPathAsCFString; bsdPathAsCFString = IORegistryEntrySearchCFProperty( modemService, kIOServicePlane, CFSTR(kIODialinDeviceKey), kCFAllocatorDefault, kIORegistryIterateRecursively); if (bsdPathAsCFString) { Boolean result; // Convert the path from a CFString to a C (NUL-terminated) result = CFStringGetCString((CFStringRef) bsdPathAsCFString, bsdPath, sizeof(bsdPath), kCFStringEncodingUTF8); CFRelease(bsdPathAsCFString); if (result) { stDeviceListItem *deviceListItem = reinterpret_cast<stDeviceListItem*>( malloc(sizeof(stDeviceListItem))); stSerialDevice *serialDevice = &(deviceListItem->value); snprintf(serialDevice->port, sizeof(serialDevice->port), "%s", bsdPath); memset(serialDevice->locationId, 0, sizeof(serialDevice->locationId)); memset(serialDevice->vendorId, 0, sizeof(serialDevice->vendorId)); memset(serialDevice->productId, 0, sizeof(serialDevice->productId)); serialDevice->manufacturer[0] = '\0'; serialDevice->serialNumber[0] = '\0'; deviceListItem->next = NULL; deviceListItem->length = &length; if (devices == NULL) { devices = deviceListItem; } else { lastDevice->next = deviceListItem; } lastDevice = deviceListItem; length++; modemFound = true; kernResult = KERN_SUCCESS; uv_mutex_lock(&list_mutex); io_service_t device = GetUsbDevice(modemService); if (device) { CFStringRef manufacturerAsCFString = (CFStringRef) IORegistryEntryCreateCFProperty(device, CFSTR(kUSBVendorString), kCFAllocatorDefault, 0); if (manufacturerAsCFString) { Boolean result; char manufacturer[MAXPATHLEN]; // Convert from a CFString to a C (NUL-terminated) result = CFStringGetCString(manufacturerAsCFString, manufacturer, sizeof(manufacturer), kCFStringEncodingUTF8); if (result) { snprintf(serialDevice->manufacturer, sizeof(serialDevice->manufacturer), "%s", manufacturer); } CFRelease(manufacturerAsCFString); } CFStringRef serialNumberAsCFString = (CFStringRef) IORegistryEntrySearchCFProperty(device, kIOServicePlane, CFSTR(kUSBSerialNumberString), kCFAllocatorDefault, kIORegistryIterateRecursively); if (serialNumberAsCFString) { Boolean result; char serialNumber[MAXPATHLEN]; // Convert from a CFString to a C (NUL-terminated) result = CFStringGetCString(serialNumberAsCFString, serialNumber, sizeof(serialNumber), kCFStringEncodingUTF8); if (result) { snprintf(serialDevice->serialNumber, sizeof(serialDevice->serialNumber), "%s", serialNumber); } CFRelease(serialNumberAsCFString); } IOCFPlugInInterface **plugInInterface = NULL; SInt32 score; HRESULT res; IOUSBDeviceInterface **deviceInterface = NULL; kernResult = IOCreatePlugInInterfaceForService(device, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score); if ((kIOReturnSuccess != kernResult) || !plugInInterface) { continue; } // Use the plugin interface to retrieve the device interface. res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), reinterpret_cast<LPVOID*> (&deviceInterface)); // Now done with the plugin interface. (*plugInInterface)->Release(plugInInterface); if (res || deviceInterface == NULL) { continue; } // Extract the desired Information ExtractUsbInformation(serialDevice, deviceInterface); // Release the Interface (*deviceInterface)->Release(deviceInterface); // Release the device (void) IOObjectRelease(device); } uv_mutex_unlock(&list_mutex); } } // Release the io_service_t now that we are done with it. (void) IOObjectRelease(modemService); } IOObjectRelease(serialPortIterator); // Release the iterator. return devices; }