From 122ca4846e46ae04e0d3599ad31207930fa76866 Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Thu, 8 Nov 2012 12:09:02 +0100 Subject: [PATCH 1/4] optimize osx serialport.list --- serialport.js | 108 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 44 deletions(-) diff --git a/serialport.js b/serialport.js index 23b392d..1c2d3bb 100644 --- a/serialport.js +++ b/serialport.js @@ -152,7 +152,7 @@ SerialPort.prototype.write = function (buffer, callback) { SerialPort.prototype.close = function (callback) { var self = this; - + var fd = this.fd; this.fd = 0; @@ -198,6 +198,7 @@ function listUnix (callback) { } return console.log(err); } + var dirName = "/dev/serial/by-id"; async.map(files, function (file, callback) { var fileName = path.join(dirName, file); @@ -211,43 +212,43 @@ function listUnix (callback) { manufacturer: undefined, pnpId: file }); - }, callback); - // Suspect code per ticket: #104 removed for deeper inspection. - // fs.readdir("/dev/serial/by-path", function(err_path, paths) { - // if (err_path) { - // if (err.errno === 34) return callback(null, []); - // return console.log(err); - // } - - // var dirName, items; - // //check if multiple devices of the same id are connected - // if (files.length !== paths.length) { - // dirName = "/dev/serial/by-path"; - // items = paths; - // } else { - // dirName = "/dev/serial/by-id"; - // items = files; - // } - - // async.map(items, function (file, callback) { - // var fileName = path.join(dirName, file); - // fs.readlink(fileName, function (err, link) { - // if (err) { - // return callback(err); - // } - // link = path.resolve(dirName, link); - // callback(null, { - // comName: link, - // manufacturer: undefined, - // pnpId: file - // }); - // }); - // }, callback); - }); + }); + }, callback); }); } function listOSX (callback) { + function getBSDDeviceName(deviceType, callback) { + child_process.exec('/usr/sbin/system_profiler SPNetworkDataType', function (err, stdout, stderr) { + if (err) { + return callback(err); + } + + stderr = stderr.trim(); + if (stderr.length > 0) { + return callback(new Error(stderr)); + } + + var lines = stdout.split('\n'); + var name = null; + var currentItem = {}; + for (var i = 0, len = lines.length; i < len; i ++) { + var line = lines[i].trim(); + line = line.replace(/\s+/, ' '); + var m; + + if (m = line.match(/^BSD Device Name: (.+)$/)) { + var header = lines[i - 4]; + var re = new RegExp(deviceType + ':'); + if (re.test(header)) { + name = m[1]; + } + } + } + callback(null, name); + }); + } + child_process.exec('/usr/sbin/system_profiler SPUSBDataType', function (err, stdout, stderr) { if (err) { return callback(err); @@ -259,33 +260,52 @@ function listOSX (callback) { } var lines = stdout.split('\n'); - var items = []; var currentItem = {}; - lines.forEach(function (line) { - line = line.trim(); + for (var i = 0, len = lines.length; i < len; i ++) { + var line = lines[i].trim(); line = line.replace(/\s+/, ' '); var m; - if (m = line.match(/^Serial Number: (.+)$/)) { + if (m = line.match(/^Product ID: (.+)$/)) { + currentItem['productId'] = m[1]; + + var header = lines[i - 2]; + if (/:/.test(header)) { + header = header.trim(); + header = header.substring(0, header.length - 1); + currentItem['deviceType'] = header; + } + } else if (m = line.match(/^Vendor ID: (.+)$/)) { + currentItem['vendorId'] = m[1]; + } else if (m = line.match(/^Serial Number: (.+)$/)) { currentItem['serialNumber'] = m[1]; } else if (m = line.match(/^Location ID: (.+)$/)) { currentItem['locationId'] = m[1]; - } else if (m = line.match(/^Product ID: (.+)$/)) { - currentItem['productId'] = m[1]; - } else if (m = line.match(/^Vendor ID: (.+)$/)) { - currentItem['vendorId'] = m[1]; } else if (m = line.match(/^Manufacturer: (.+)$/)) { currentItem['manufacturer'] = m[1]; } else if (/^$/.test(line)) { if ('serialNumber' in currentItem) { - currentItem['comName'] = "/dev/cu.usbmodem" + currentItem['locationId'].substring(2, 6) + '1'; + currentItem['comName'] = "/dev/cu.usbserial-" + currentItem['serialNumber']; + items.push(currentItem); + currentItem = {}; + } else if ('productId' in currentItem && 'vendorId' in currentItem) { items.push(currentItem); currentItem = {}; } } + } + + async.forEach(items, function(item, callback) { + getBSDDeviceName(item.deviceType, function(err, name) { + if (name) { + item.comName = '/dev/cu.' + name; + } + callback(err); + }); + }, function(err) { + callback(null, items); }); - callback(null, items); }); } From 49fcadd7e5f0c633eb3eba5ead066a595a42744d Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Mon, 12 Nov 2012 15:55:21 +0100 Subject: [PATCH 2/4] native list approach for mac --- serialport.js | 96 +------------- src/serialport.cpp | 3 + src/serialport.h | 3 + src/serialport_unix.cpp | 270 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 277 insertions(+), 95 deletions(-) diff --git a/serialport.js b/serialport.js index 1c2d3bb..c8d3cf5 100644 --- a/serialport.js +++ b/serialport.js @@ -217,103 +217,11 @@ function listUnix (callback) { }); } -function listOSX (callback) { - function getBSDDeviceName(deviceType, callback) { - child_process.exec('/usr/sbin/system_profiler SPNetworkDataType', function (err, stdout, stderr) { - if (err) { - return callback(err); - } - - stderr = stderr.trim(); - if (stderr.length > 0) { - return callback(new Error(stderr)); - } - - var lines = stdout.split('\n'); - var name = null; - var currentItem = {}; - for (var i = 0, len = lines.length; i < len; i ++) { - var line = lines[i].trim(); - line = line.replace(/\s+/, ' '); - var m; - - if (m = line.match(/^BSD Device Name: (.+)$/)) { - var header = lines[i - 4]; - var re = new RegExp(deviceType + ':'); - if (re.test(header)) { - name = m[1]; - } - } - } - callback(null, name); - }); - } - - child_process.exec('/usr/sbin/system_profiler SPUSBDataType', function (err, stdout, stderr) { - if (err) { - return callback(err); - } - - stderr = stderr.trim(); - if (stderr.length > 0) { - return callback(new Error(stderr)); - } - - var lines = stdout.split('\n'); - var items = []; - var currentItem = {}; - for (var i = 0, len = lines.length; i < len; i ++) { - var line = lines[i].trim(); - line = line.replace(/\s+/, ' '); - var m; - - if (m = line.match(/^Product ID: (.+)$/)) { - currentItem['productId'] = m[1]; - - var header = lines[i - 2]; - if (/:/.test(header)) { - header = header.trim(); - header = header.substring(0, header.length - 1); - currentItem['deviceType'] = header; - } - } else if (m = line.match(/^Vendor ID: (.+)$/)) { - currentItem['vendorId'] = m[1]; - } else if (m = line.match(/^Serial Number: (.+)$/)) { - currentItem['serialNumber'] = m[1]; - } else if (m = line.match(/^Location ID: (.+)$/)) { - currentItem['locationId'] = m[1]; - } else if (m = line.match(/^Manufacturer: (.+)$/)) { - currentItem['manufacturer'] = m[1]; - } else if (/^$/.test(line)) { - if ('serialNumber' in currentItem) { - currentItem['comName'] = "/dev/cu.usbserial-" + currentItem['serialNumber']; - items.push(currentItem); - currentItem = {}; - } else if ('productId' in currentItem && 'vendorId' in currentItem) { - items.push(currentItem); - currentItem = {}; - } - } - } - - async.forEach(items, function(item, callback) { - getBSDDeviceName(item.deviceType, function(err, name) { - if (name) { - item.comName = '/dev/cu.' + name; - } - callback(err); - }); - }, function(err) { - callback(null, items); - }); - }); -} - exports.list = function (callback) { if (process.platform === 'win32') { SerialPortBinding.list(callback); } else if (process.platform === 'darwin') { - listOSX(callback); + SerialPortBinding.list(callback); } else { listUnix(callback); } @@ -342,4 +250,4 @@ SerialPort.prototype.flush = function (callback) { }; module.exports.SerialPort = SerialPort; -module.exports.parsers = parsers; +module.exports.parsers = parsers; \ No newline at end of file diff --git a/src/serialport.cpp b/src/serialport.cpp index d1430b8..2280e95 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -231,6 +231,9 @@ void EIO_AfterList(uv_work_t* req) { item->Set(v8::String::New("comName"), v8::String::New((*it)->comName.c_str())); item->Set(v8::String::New("manufacturer"), v8::String::New((*it)->manufacturer.c_str())); item->Set(v8::String::New("pnpId"), v8::String::New((*it)->pnpId.c_str())); + item->Set(v8::String::New("locationId"), v8::String::New((*it)->locationId.c_str())); + item->Set(v8::String::New("vendorId"), v8::String::New((*it)->vendorId.c_str())); + item->Set(v8::String::New("productId"), v8::String::New((*it)->productId.c_str())); results->Set(i, item); } argv[0] = v8::Undefined(); diff --git a/src/serialport.h b/src/serialport.h index 67b227c..b90356e 100644 --- a/src/serialport.h +++ b/src/serialport.h @@ -96,6 +96,9 @@ public: std::string comName; std::string manufacturer; std::string pnpId; + std::string locationId; + std::string vendorId; + std::string productId; }; struct ListBaton { diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 2b5d268..63e192c 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -8,6 +8,11 @@ #ifdef __APPLE__ #include +#include +#include +#include +#include +#include #endif #if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4) #include @@ -64,6 +69,21 @@ int ToBaudConstant(int baudRate) { return -1; } +#ifdef __APPLE__ +typedef struct SerialDevice { + char port[MAXPATHLEN]; + char locationId[MAXPATHLEN]; + char vendorId[MAXPATHLEN]; + char productId[MAXPATHLEN]; +} stSerialDevice; + +typedef struct DeviceListItem { + struct SerialDevice value; + struct DeviceListItem *next; + int* length; +} stDeviceListItem; +#endif + int ToDataBitsConstant(int dataBits) { switch (dataBits) { case 8: default: return CS8; @@ -192,7 +212,7 @@ void EIO_Open(uv_work_t* req) { #if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4) speed_t speed = data->baudRate; - if ( ioctl( fd, IOSSIOSPEED, &speed ) == -1 ) + if ( ioctl( fd, IOSSIOSPEED, &speed ) == -1 ) { printf( "Error %d calling ioctl( ..., IOSSIOSPEED, ... )\n", errno ); } @@ -217,8 +237,256 @@ void EIO_Close(uv_work_t* req) { close(data->fd); } +#ifdef __APPLE__ + +// Function prototypes +static kern_return_t FindModems(io_iterator_t *matchingServices); +static io_registry_entry_t GetUsbDevice(char *pathName); +static stDeviceListItem* GetSerialDevices(); + + +static kern_return_t FindModems(io_iterator_t *matchingServices) +{ + kern_return_t kernResult; + CFMutableDictionaryRef classesToMatch; + + classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); + if (classesToMatch != NULL) + { + CFDictionarySetValue(classesToMatch, + CFSTR(kIOSerialBSDTypeKey), + CFSTR(kIOSerialBSDAllTypes)); + } + + kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, matchingServices); + + return kernResult; +} + +static io_registry_entry_t GetUsbDevice(char* pathName) +{ + io_registry_entry_t device = NULL; + + CFMutableDictionaryRef classesToMatch = IOServiceMatching(kIOUSBDeviceClassName); + if (classesToMatch != NULL) + { + io_iterator_t matchingServices; + kern_return_t kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &matchingServices); + if (KERN_SUCCESS == kernResult) + { + io_service_t service; + Boolean deviceFound = false; + + while ((service = IOIteratorNext(matchingServices)) && !deviceFound) + { + CFStringRef bsdPathAsCFString = (CFStringRef) IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, kIORegistryIterateRecursively); + + if (bsdPathAsCFString) + { + Boolean result; + char bsdPath[MAXPATHLEN]; + + // Convert the path from a CFString to a C (NUL-terminated) + result = CFStringGetCString(bsdPathAsCFString, + bsdPath, + sizeof(bsdPath), + kCFStringEncodingUTF8); + + CFRelease(bsdPathAsCFString); + + if (result && (strcmp(bsdPath, pathName) == 0)) + { + deviceFound = true; + //memset(bsdPath, 0, sizeof(bsdPath)); + device = service; + } + } + } + } + } + + return device; +} + +static stDeviceListItem* GetSerialDevices() +{ + kern_return_t kernResult = KERN_FAILURE; + io_iterator_t serialPortIterator; + char bsdPath[MAXPATHLEN]; + + kernResult = FindModems(&serialPortIterator); + + io_service_t modemService; + kernResult = KERN_FAILURE; + Boolean modemFound = false; + + // Initialize the returned path + *bsdPath = '\0'; + + stDeviceListItem* devices = NULL; + stDeviceListItem* lastDevice = NULL; + int length = 0; + + while ((modemService = IOIteratorNext(serialPortIterator))) + { + CFTypeRef bsdPathAsCFString; + + bsdPathAsCFString = IORegistryEntrySearchCFProperty(modemService, kIOServicePlane, CFSTR(kIOCalloutDeviceKey), 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 = (stDeviceListItem*) malloc(sizeof(stDeviceListItem)); + stSerialDevice *serialDevice = &(deviceListItem->value); + strcpy(serialDevice->port, bsdPath); + memset(serialDevice->locationId, 0, sizeof(serialDevice->locationId)); + memset(serialDevice->vendorId, 0, sizeof(serialDevice->vendorId)); + memset(serialDevice->productId, 0, sizeof(serialDevice->productId)); + deviceListItem->next = NULL; + deviceListItem->length = &length; + + if (devices == NULL) { + devices = deviceListItem; + } + else { + lastDevice->next = deviceListItem; + } + + lastDevice = deviceListItem; + length++; + + modemFound = true; + kernResult = KERN_SUCCESS; + + io_registry_entry_t device = GetUsbDevice(bsdPath); + + if (device) { + 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), + (LPVOID*) &deviceInterface); + + // Now done with the plugin interface. + (*plugInInterface)->Release(plugInInterface); + + if (res || deviceInterface == NULL) { + continue; + } + + + UInt32 locationID; + kernResult = (*deviceInterface)->GetLocationID(deviceInterface, &locationID); + if (KERN_SUCCESS != kernResult) + { + continue; + } + else + { + snprintf(serialDevice->locationId, 11, "0x%08x", locationID); + } + + UInt16 vendorID; + kernResult = (*deviceInterface)->GetDeviceVendor(deviceInterface, &vendorID); + if (KERN_SUCCESS != kernResult) + { + continue; + } + else + { + snprintf(serialDevice->vendorId, 7, "0x%04x", vendorID); + } + + UInt16 productID; + kernResult = (*deviceInterface)->GetDeviceProduct(deviceInterface, &productID); + if (KERN_SUCCESS != kernResult) + { + continue; + } + else + { + snprintf(serialDevice->productId, 7, "0x%04x", productID); + } + + } + } + } + + // Release the io_service_t now that we are done with it. + (void) IOObjectRelease(modemService); + } + + IOObjectRelease(serialPortIterator); // Release the iterator. + + return devices; +} + +#endif + void EIO_List(uv_work_t* req) { // This code exists in javascript for unix platforms + +#ifdef __APPLE__ + ListBaton* data = static_cast(req->data); + + stDeviceListItem* devices = GetSerialDevices(); + + if ((* devices->length) > 0) + { + stDeviceListItem* next = devices; + + for (int i = 0, len = (* devices->length); i < len; i++) { + stSerialDevice device = (* next).value; + + ListResultItem* resultItem = new ListResultItem(); + resultItem->comName = device.port; + + if (device.locationId != NULL) { + resultItem->locationId = device.locationId; + } + if (device.vendorId != NULL) { + resultItem->vendorId = device.vendorId; + } + if (device.productId != NULL) { + resultItem->productId = device.productId; + } + data->results.push_back(resultItem); + + stDeviceListItem* current = next; + + if ((* next).next != NULL) + { + next = (* next).next; + } + + free(current); + } + + } + +#endif } void EIO_Flush(uv_work_t* req) { From 9162be29fa76df2dd98bd9a49deb72bd1cf25cef Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Wed, 13 Mar 2013 08:38:22 +0100 Subject: [PATCH 3/4] release ressources --- src/serialport_unix.cpp | 93 ++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 63e192c..86c41db 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -1,4 +1,3 @@ - #ifndef WIN32 #include "serialport.h" #include @@ -248,8 +247,8 @@ static stDeviceListItem* GetSerialDevices(); static kern_return_t FindModems(io_iterator_t *matchingServices) { kern_return_t kernResult; - CFMutableDictionaryRef classesToMatch; - + CFMutableDictionaryRef classesToMatch +; classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (classesToMatch != NULL) { @@ -265,7 +264,7 @@ static kern_return_t FindModems(io_iterator_t *matchingServices) static io_registry_entry_t GetUsbDevice(char* pathName) { - io_registry_entry_t device = NULL; + io_registry_entry_t device = 0; CFMutableDictionaryRef classesToMatch = IOServiceMatching(kIOUSBDeviceClassName); if (classesToMatch != NULL) @@ -300,14 +299,46 @@ static io_registry_entry_t GetUsbDevice(char* pathName) //memset(bsdPath, 0, sizeof(bsdPath)); device = service; } + else + { + // Release the object which are no longer needed + (void) IOObjectRelease(service); + } } } - } + // Release the iterator. + IOObjectRelease(matchingServices); + } } return device; } +static void ExtractUsbInformation(stSerialDevice *serialDevice, IOUSBDeviceInterface **deviceInterface) +{ + kern_return_t kernResult = KERN_FAILURE; + UInt32 locationID; + kernResult = (*deviceInterface)->GetLocationID(deviceInterface, &locationID); + if (KERN_SUCCESS == kernResult) + { + snprintf(serialDevice->locationId, 11, "0x%08x", locationID); + } + + UInt16 vendorID; + kernResult = (*deviceInterface)->GetDeviceVendor(deviceInterface, &vendorID); + if (KERN_SUCCESS == kernResult) + { + snprintf(serialDevice->vendorId, 7, "0x%04x", vendorID); + } + + UInt16 productID; + kernResult = (*deviceInterface)->GetDeviceProduct(deviceInterface, &productID); + if (KERN_SUCCESS == kernResult) + { + snprintf(serialDevice->productId, 7, "0x%04x", productID); + } +} + static stDeviceListItem* GetSerialDevices() { kern_return_t kernResult = KERN_FAILURE; @@ -395,41 +426,15 @@ static stDeviceListItem* GetSerialDevices() if (res || deviceInterface == NULL) { continue; } - - - UInt32 locationID; - kernResult = (*deviceInterface)->GetLocationID(deviceInterface, &locationID); - if (KERN_SUCCESS != kernResult) - { - continue; - } - else - { - snprintf(serialDevice->locationId, 11, "0x%08x", locationID); - } - - UInt16 vendorID; - kernResult = (*deviceInterface)->GetDeviceVendor(deviceInterface, &vendorID); - if (KERN_SUCCESS != kernResult) - { - continue; - } - else - { - snprintf(serialDevice->vendorId, 7, "0x%04x", vendorID); - } - - UInt16 productID; - kernResult = (*deviceInterface)->GetDeviceProduct(deviceInterface, &productID); - if (KERN_SUCCESS != kernResult) - { - continue; - } - else - { - snprintf(serialDevice->productId, 7, "0x%04x", productID); - } + // Extract the desired Information + ExtractUsbInformation(serialDevice, deviceInterface); + + // Release the Interface + (*deviceInterface)->Release(deviceInterface); + + // Release the device + (void) IOObjectRelease(device); } } } @@ -453,11 +458,11 @@ void EIO_List(uv_work_t* req) { stDeviceListItem* devices = GetSerialDevices(); - if ((* devices->length) > 0) + if (*(devices->length) > 0) { stDeviceListItem* next = devices; - for (int i = 0, len = (* devices->length); i < len; i++) { + for (int i = 0, len = *(devices->length); i < len; i++) { stSerialDevice device = (* next).value; ListResultItem* resultItem = new ListResultItem(); @@ -476,9 +481,9 @@ void EIO_List(uv_work_t* req) { stDeviceListItem* current = next; - if ((* next).next != NULL) + if (next->next != NULL) { - next = (* next).next; + next = next->next; } free(current); @@ -495,4 +500,4 @@ void EIO_Flush(uv_work_t* req) { data->result = tcflush(data->fd, TCIFLUSH); } -#endif +#endif \ No newline at end of file From b1f2cf5c032a374302ae8fbf7c1dd9a4030a3343 Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Wed, 13 Mar 2013 14:30:50 +0100 Subject: [PATCH 4/4] lock getUsbDevice for mac os --- src/serialport_unix.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 86c41db..d1e2d36 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -12,6 +12,10 @@ #include #include #include + +uv_mutex_t list_mutex; +Boolean lockInitialised = FALSE; + #endif #if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4) #include @@ -19,7 +23,6 @@ #include #endif - int ToBaudConstant(int baudRate); int ToDataBitsConstant(int dataBits); int ToStopBitsConstant(SerialPortStopBits stopBits); @@ -247,8 +250,7 @@ static stDeviceListItem* GetSerialDevices(); static kern_return_t FindModems(io_iterator_t *matchingServices) { kern_return_t kernResult; - CFMutableDictionaryRef classesToMatch -; + CFMutableDictionaryRef classesToMatch; classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (classesToMatch != NULL) { @@ -279,7 +281,7 @@ static io_registry_entry_t GetUsbDevice(char* pathName) while ((service = IOIteratorNext(matchingServices)) && !deviceFound) { CFStringRef bsdPathAsCFString = (CFStringRef) IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, kIORegistryIterateRecursively); - + if (bsdPathAsCFString) { Boolean result; @@ -308,7 +310,7 @@ static io_registry_entry_t GetUsbDevice(char* pathName) } // Release the iterator. IOObjectRelease(matchingServices); - } + } } return device; @@ -400,8 +402,10 @@ static stDeviceListItem* GetSerialDevices() modemFound = true; kernResult = KERN_SUCCESS; + uv_mutex_lock(&list_mutex); + io_registry_entry_t device = GetUsbDevice(bsdPath); - + if (device) { IOCFPlugInInterface **plugInInterface = NULL; SInt32 score; @@ -436,11 +440,13 @@ static stDeviceListItem* GetSerialDevices() // 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); + (void) IOObjectRelease(modemService); } IOObjectRelease(serialPortIterator); // Release the iterator. @@ -454,6 +460,12 @@ void EIO_List(uv_work_t* req) { // This code exists in javascript for unix platforms #ifdef __APPLE__ + if(!lockInitialised) + { + uv_mutex_init(&list_mutex); + lockInitialised = TRUE; + } + ListBaton* data = static_cast(req->data); stDeviceListItem* devices = GetSerialDevices();