node-usb / node-usb

Improved USB library for Node.js
https://node-usb.github.io/node-usb/
MIT License
1.55k stars 273 forks source link

Attaching usb doesn't trigger usb.on('attach') event #481

Closed YiyuanYin closed 1 year ago

YiyuanYin commented 2 years ago

I have an electron application that use this package to listen to the usb attach event. Sometimes attaching a usb can trigger usb.on('attach') event, sometimes it won't. The code checking whether a usb is attached is by finding the new device which has the different deviceAddress from the former one. However, sometimes when a usb is attached, the deviceAddress is the same as the old ones in the device list. Therefore, usb won't emit attach event. 4DC705B7-442E-432e-BDDA-189A3E5D7FBD Have you ever thought about adding some other conditions to decide whether a usb is attached?

thegecko commented 2 years ago

The code checking whether a usb is attached is by finding the new device which has the different deviceAddress from the former one.

The attach/detach events are exposing hotplug events from the libusb library (except on Windows)

Perhaps you need to raise this issue there?

mildsunrise commented 2 years ago

You mentioned "The code checking whether a usb is attached" so I assume you're in Windows. There's #488 to try to implement actual hotplug detection support on Windows.

mildsunrise commented 2 years ago

By the way, I've just realized why this doesn't work... deviceAddress is only unique within the bus the device is connected to. To properly identify the device, we need the busNumber too. Something like the deviceId from WebUSB:

https://github.com/node-usb/node-usb/blob/c42aa55fba09ea65c660d78cdd3779cb29f1d04c/tsc/webusb/index.ts#L347-L353

But I think we can do it even simpler... libusb will never return different libusb_device pointers for the same device. And the bindings will never create two Device instances for the same libusb_device. Therefore we should be able to just say device1 === device2 to compare both lists of devices.

thegecko commented 1 year ago

Closing this issue due to inactivity, please create a new issue as required.