Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add product property to PortInfo #138

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/linux-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ N: ttyMFD0
E: DEVNAME=/dev/ttyMFD0
E: ID_VENDOR_ID=0x2343
E: ID_MODEL_ID=0043
E: ID_MODEL_ENC=some device made by someone
E: ID_MODEL=some device made by someone

P: /devices/unknown
N: rfcomm4
Expand All @@ -84,6 +84,7 @@ const portOutput = [
{
path: '/dev/ttyACM0',
manufacturer: 'Arduino (www.arduino.cc)',
product: '0043',
serialNumber: '752303138333518011C1',
productId: '0043',
vendorId: '2341',
Expand All @@ -95,6 +96,7 @@ const portOutput = [
},
{
path: '/dev/ttyMFD0',
product: 'some device made by someone',
vendorId: '2343',
productId: '0043',
},
Expand Down
4 changes: 4 additions & 0 deletions lib/linux-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function propName(name: string) {
return {
DEVNAME: 'path',
ID_VENDOR_ENC: 'manufacturer',
ID_MODEL: 'product',
ID_SERIAL_SHORT: 'serialNumber',
ID_VENDOR_ID: 'vendorId',
ID_MODEL_ID: 'productId',
Expand All @@ -20,6 +21,7 @@ function propName(name: string) {
* see https://github.com/serialport/bindings-cpp/issues/115
*/
ID_USB_VENDOR_ENC: 'manufacturer',
ID_USB_MODEL: 'product',
ID_USB_SERIAL_SHORT: 'serialNumber',
ID_USB_VENDOR_ID: 'vendorId',
ID_USB_MODEL_ID: 'productId',
Expand Down Expand Up @@ -56,6 +58,7 @@ export function linuxList(spawnCmd: typeof spawn = spawn) {
let port: PortInfo = {
path: '',
manufacturer: undefined,
product: undefined,
serialNumber: undefined,
pnpId: undefined,
locationId: undefined,
Expand All @@ -71,6 +74,7 @@ export function linuxList(spawnCmd: typeof spawn = spawn) {
port = {
path: '',
manufacturer: undefined,
product: undefined,
serialNumber: undefined,
pnpId: undefined,
locationId: undefined,
Expand Down
26 changes: 26 additions & 0 deletions src/darwin_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ static stDeviceListItem* GetSerialDevices() {
memset(serialDevice->vendorId, 0, sizeof(serialDevice->vendorId));
memset(serialDevice->productId, 0, sizeof(serialDevice->productId));
serialDevice->manufacturer[0] = '\0';
serialDevice->product[0] = '\0';
serialDevice->serialNumber[0] = '\0';
deviceListItem->next = NULL;
deviceListItem->length = &length;
Expand Down Expand Up @@ -205,6 +206,28 @@ static stDeviceListItem* GetSerialDevices() {
CFRelease(manufacturerAsCFString);
}

CFStringRef productAsCFString = (CFStringRef) IORegistryEntryCreateCFProperty(device,
CFSTR(kUSBProductString),
kCFAllocatorDefault,
0);

if (productAsCFString) {
Boolean result;
char product[MAXPATHLEN];

// Convert from a CFString to a C (NUL-terminated)
result = CFStringGetCString(productAsCFString,
product,
sizeof(product),
kCFStringEncodingUTF8);

if (result) {
snprintf(serialDevice->product, sizeof(serialDevice->product), "%s", product);
}

CFRelease(productAsCFString);
}

CFStringRef serialNumberAsCFString = (CFStringRef) IORegistryEntrySearchCFProperty(device,
kIOServicePlane,
CFSTR(kUSBSerialNumberString),
Expand Down Expand Up @@ -300,6 +323,9 @@ void ListBaton::Execute() {
if (*device.manufacturer) {
resultItem->manufacturer = device.manufacturer;
}
if (*device.product) {
resultItem->product = device.product;
}
if (*device.serialNumber) {
resultItem->serialNumber = device.serialNumber;
}
Expand Down
5 changes: 4 additions & 1 deletion src/darwin_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void setIfNotEmpty(Napi::Object item, std::string key, const char *value);
struct ListResultItem {
std::string path;
std::string manufacturer;
std::string product;
std::string serialNumber;
std::string pnpId;
std::string locationId;
Expand All @@ -22,7 +23,7 @@ struct ListResultItem {
};

struct ListBaton : public Napi::AsyncWorker {
ListBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:ListBaton"),
ListBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:ListBaton"),
errorString() {}
std::list<ListResultItem*> results;
char errorString[ERROR_STRING_SIZE];
Expand All @@ -38,6 +39,7 @@ struct ListBaton : public Napi::AsyncWorker {

setIfNotEmpty(item, "path", (*it)->path.c_str());
setIfNotEmpty(item, "manufacturer", (*it)->manufacturer.c_str());
setIfNotEmpty(item, "product", (*it)->product.c_str());
setIfNotEmpty(item, "serialNumber", (*it)->serialNumber.c_str());
setIfNotEmpty(item, "pnpId", (*it)->pnpId.c_str());
setIfNotEmpty(item, "locationId", (*it)->locationId.c_str());
Expand All @@ -56,6 +58,7 @@ typedef struct SerialDevice {
char vendorId[MAXPATHLEN];
char productId[MAXPATHLEN];
char manufacturer[MAXPATHLEN];
char product[MAXPATHLEN];
char serialNumber[MAXPATHLEN];
} stSerialDevice;

Expand Down