0
votes

I'm writing a node.js JavaScript app to run on a Mac (macOS server) that will communicate with USB devices that are plugged in. The library I need to use to interact with the device takes a path in the form /dev/pathToDevice - e.g. lib.connect('/dev/pathToDevice'); - whereas the only way I have to identify the USB device from within my code is by the vendorId (VID) and productId (PID) obtained via the usb-detection library from NPM on insertion.

How can I, in JavaScript, identify or derive the path to the device from the VID and PID of the inserted USB device, so I can pass that to the library?

2
That's a poor USB library if it won't help you find devices by their vendor ID and product ID.David Grayson

2 Answers

0
votes

You could write a small C++ program that uses libusbp_generic_interface_get_os_filename function from libusbp. You would then call the C program from node.js. Let me know if you need help writing the C++ program. I would probably start with the port_name example form libusbp's source tree.

Or, you could use the lsusb example from libusbp as is. It returns a path like /sys/devices/pci0000:00/0000:00:06.0/usb1/1-1 for each device. That is a directory with a bunch of special files; one of them is busnum and one of them is devnum, which are the two numbers you need to construct a path like /dev/bus/usb/DEVNUM/BUSNUM.

If that works, you might prefer to write a binding for libusbp from node.js instead of using a C executable.

0
votes

Try drivelist , It gives you the mountpaths and its cross platform and you can combine this with usb-detection easily.

var drivelist = require("drivelist")
drivelist.list((error, devices)=> { console.log(devices) })

//Output

[ { 
    //stuff...

    device: '/dev/disk0',
    raw: '/dev/rdisk0',
    mountpoints: [
          {"path":"/","label":"Macintosh HD"},
          {"path":"/private/var/vm","label":"VM"}
    ],
    isRemovable: false,

    //stuff....
]