Skip to content

Friendly name support for all platforms #219

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

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
2 changes: 1 addition & 1 deletion lib/bindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { assert, shouldReject } from '../test/assert'
import { makeTestFeature } from '../test/makeTestFeature'
import { BindingInterface, OpenOptions, PortInfo, SetOptions } from '@serialport/bindings-interface'
import { BindingInterface, OpenOptions, PortInfo, SetOptions } from '@noelneu/bindings-interface'
import { autoDetect } from './index'
import { MockBinding } from '@serialport/binding-mock'
import { BindingsError } from './errors'
Expand Down
25 changes: 23 additions & 2 deletions lib/linux-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { spawn } from 'child_process'
import { PortInfo } from '@serialport/bindings-interface'
import { ReadlineParser } from '@serialport/parser-readline'
import fs from 'fs'

// get only serial port names
function checkPathOfDevice(path: string) {
Expand All @@ -14,6 +15,7 @@ function propName(name: string) {
ID_SERIAL_SHORT: 'serialNumber',
ID_VENDOR_ID: 'vendorId',
ID_MODEL_ID: 'productId',
ID_MODEL_ENC: 'friendlyName',
DEVLINKS: 'pnpId',
/**
* Workaround for systemd defect
Expand All @@ -38,7 +40,7 @@ function propVal(name: string, val: string) {
const match = val.match(/\/by-id\/([^\s]+)/)
return (match?.[1]) || undefined
}
if (name === 'manufacturer') {
if (name === 'manufacturer' || name === 'friendlyName') {
return decodeHexEscape(val)
}
if (/^0x/.test(val)) {
Expand All @@ -47,10 +49,24 @@ function propVal(name: string, val: string) {
return val
}

function getActiveDevices(): Set<string> {
try {
/* eslint-disable comma-dangle */
const validPaths = fs.readdirSync('/dev/serial/by-path').map(file =>
fs.realpathSync(`/dev/serial/by-path/${file}`)
)
/* eslint-enable comma-dangle */
return new Set(validPaths)
} catch {
return new Set()
}
}

export function linuxList(spawnCmd: typeof spawn = spawn) {
const ports: PortInfo[] = []
const udevadm = spawnCmd('udevadm', ['info', '-e'])
const lines = udevadm.stdout.pipe(new ReadlineParser())
const validDevices = getActiveDevices()

let skipPort = false
let port: PortInfo = {
Expand All @@ -61,6 +77,7 @@ export function linuxList(spawnCmd: typeof spawn = spawn) {
locationId: undefined,
vendorId: undefined,
productId: undefined,
friendlyName: undefined,
}

lines.on('data', (line: string) => {
Expand All @@ -76,6 +93,7 @@ export function linuxList(spawnCmd: typeof spawn = spawn) {
locationId: undefined,
vendorId: undefined,
productId: undefined,
friendlyName: undefined,
}
skipPort = false
return
Expand Down Expand Up @@ -118,6 +136,9 @@ export function linuxList(spawnCmd: typeof spawn = spawn) {
})
udevadm.on('error', reject)
lines.on('error', reject)
lines.on('finish', () => resolve(ports))
lines.on('finish', () => {
const activePorts = ports.filter(port => validDevices.has(port.path))
resolve(activePorts)
})
})
}
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/debug": "4.1.12",
"@types/mocha": "10.0.10",
"@types/node": "22.13.8",
"@types/sinon": "^17.0.4",
"@typescript-eslint/eslint-plugin": "6.21.0",
"@typescript-eslint/parser": "6.21.0",
"cc": "3.0.1",
Expand Down
27 changes: 27 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->friendlyName[0] = '\0';
serialDevice->serialNumber[0] = '\0';
deviceListItem->next = NULL;
deviceListItem->length = &length;
Expand All @@ -183,6 +184,29 @@ static stDeviceListItem* GetSerialDevices() {
io_service_t device = GetUsbDevice(modemService);

if (device) {

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

if (friendlyNameAsCFString) {
Boolean result;
char friendlyName[MAXPATHLEN];

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

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

CFRelease(friendlyNameAsCFString);
}

CFStringRef manufacturerAsCFString = (CFStringRef) IORegistryEntryCreateCFProperty(device,
CFSTR(kUSBVendorString),
kCFAllocatorDefault,
Expand Down Expand Up @@ -300,6 +324,9 @@ void ListBaton::Execute() {
if (*device.manufacturer) {
resultItem->manufacturer = device.manufacturer;
}
if (*device.friendlyName) {
resultItem->friendlyName = device.friendlyName;
}
if (*device.serialNumber) {
resultItem->serialNumber = device.serialNumber;
}
Expand Down
3 changes: 3 additions & 0 deletions 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 friendlyName;
std::string serialNumber;
std::string pnpId;
std::string locationId;
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, "friendlyName", (*it)->friendlyName.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 friendlyName[MAXPATHLEN];
char serialNumber[MAXPATHLEN];
} stSerialDevice;

Expand Down
6 changes: 3 additions & 3 deletions src/serialport_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,9 @@ void ListBaton::Execute() {
reinterpret_cast<PBYTE>(szBuffer), sizeof(szBuffer), &dwSize)) {
locationId = wcsdup(szBuffer);
}
if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, &deviceInfoData,
SPDRP_FRIENDLYNAME, &dwPropertyRegDataType,
reinterpret_cast<PBYTE>(szBuffer), sizeof(szBuffer), &dwSize)) {
if (SetupDiGetDevicePropertyW(hDevInfo, &deviceInfoData,
&DEVPKEY_Device_BusReportedDeviceDesc, &dwPropertyRegDataType,
reinterpret_cast<PBYTE>(szBuffer), sizeof(szBuffer), &dwSize, 0)) {
friendlyName = wcsdup(szBuffer);
}
if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, &deviceInfoData,
Expand Down