Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Add Serial support for Windows OS #62

Merged
merged 5 commits into from Jun 4, 2018
Merged

Add Serial support for Windows OS #62

merged 5 commits into from Jun 4, 2018

Conversation

ghost
Copy link

@ghost ghost commented May 26, 2018

Fix #36

Add Serial support for Windows OS


Alternative to #50

@ghost
Copy link
Author

ghost commented May 26, 2018

Hi,
@jasondana & @MadLittleMods could you please try this code and tell me what you think about the code.
Regards,

Copy link
Owner

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Seems to work well 👍

Mostly small nits and I am curious if @jasondana has any comments

Here is the output from a Windows 10 PC for example,

find 17 [ { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB Root Hub (USB 3.0)',
    manufacturer: '(Standard USB HUBs)',
    serialNumber: '',
    deviceAddress: 10 },
  { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB Root Hub (USB 3.0)',
    manufacturer: '(Standard USB HUBs)',
    serialNumber: '',
    deviceAddress: 15 },
  { locationId: 0,
    vendorId: 1118,
    productId: 746,
    deviceName: 'Xbox One Controller',
    manufacturer: 'Microsoft',
    serialNumber: '3032363030303739393032363336',
    deviceAddress: 17 },
  { locationId: 0,
    vendorId: 1118,
    productId: 767,
    deviceName: 'USB Input Device',
    manufacturer: '(Standard system devices)',
    serialNumber: '00&00&00009D605980ED7E',
    deviceAddress: 7 },
  { locationId: 0,
    vendorId: 1133,
    productId: 49742,
    deviceName: 'USB Composite Device',
    manufacturer: '(Standard USB Host Controller)',
    serialNumber: '9025267AF90009',
    deviceAddress: 1 },
  { locationId: 0,
    vendorId: 1133,
    productId: 49742,
    deviceName: 'USB Input Device',
    manufacturer: '(Standard system devices)',
    serialNumber: '',
    deviceAddress: 2 },
  { locationId: 0,
    vendorId: 1133,
    productId: 49742,
    deviceName: 'USB Input Device',
    manufacturer: '(Standard system devices)',
    serialNumber: '',
    deviceAddress: 4 },
  { locationId: 0,
    vendorId: 2652,
    productId: 8680,
    deviceName: 'Broadcom BCM20702 Bluetooth 4.0 USB Device',
    manufacturer: 'Broadcom',
    serialNumber: '5CF3707AD674',
    deviceAddress: 9 },
  { locationId: 0,
    vendorId: 6720,
    productId: 257,
    deviceName: 'Generic USB Hub',
    manufacturer: '(Standard USB HUBs)',
    serialNumber: '',
    deviceAddress: 5 },
  { locationId: 0,
    vendorId: 8457,
    productId: 10257,
    deviceName: 'Generic USB Hub',
    manufacturer: '(Standard USB HUBs)',
    serialNumber: '',
    deviceAddress: 16 },
  { locationId: 0,
    vendorId: 8457,
    productId: 10257,
    deviceName: 'Generic USB Hub',
    manufacturer: '(Standard USB HUBs)',
    serialNumber: '',
    deviceAddress: 6 },
  { locationId: 0,
    vendorId: 8457,
    productId: 33040,
    deviceName: 'Generic SuperSpeed USB Hub',
    manufacturer: '(Standard USB HUBs)',
    serialNumber: '',
    deviceAddress: 3 },
  { locationId: 0,
    vendorId: 8457,
    productId: 33040,
    deviceName: 'Generic SuperSpeed USB Hub',
    manufacturer: '(Standard USB HUBs)',
    serialNumber: '',
    deviceAddress: 14 },
  { locationId: 0,
    vendorId: 9494,
    productId: 60,
    deviceName: 'USB Composite Device',
    manufacturer: '(Standard USB Host Controller)',
    serialNumber: '',
    deviceAddress: 8 },
  { locationId: 0,
    vendorId: 9494,
    productId: 60,
    deviceName: 'USB Input Device',
    manufacturer: '(Standard system devices)',
    serialNumber: '',
    deviceAddress: 12 },
  { locationId: 0,
    vendorId: 9494,
    productId: 60,
    deviceName: 'USB Input Device',
    manufacturer: '(Standard system devices)',
    serialNumber: '',
    deviceAddress: 11 },
  { locationId: 0,
    vendorId: 9494,
    productId: 60,
    deviceName: 'USB Input Device',
    manufacturer: '(Standard system devices)',
    serialNumber: '',
    deviceAddress: 13 } ]

// If device has a CM_DEVCAP_UNIQUEID capability, then we assume it has a serial number
if ((dwCap & CM_DEVCAP_UNIQUEID) == CM_DEVCAP_UNIQUEID) {
if (DllSetupDiGetDeviceInstanceId(hDevInfo, pspDevInfoData, buf, buffSize, &nSize)) {
// extract Serial Number
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment about what we are trying to parse

// Format: <device-ID>\<instance-specific-ID>
//
// Ex. `USB\VID_2109&PID_8110\5&376ABA2D&0&21`
//  - `<device-ID>`: `USB\VID_2109&PID_8110`
//  - `<instance-specific-ID>`: `5&376ABA2D&0&21`
//
// [Device instance IDs](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-instance-ids) ->
//  - [Device IDs](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-ids) -> [Hardware IDs](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/hardware-ids) -> [Device identifier formats](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-identifier-formats) -> [Identifiers for USB devices](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-usb-devices)
//     - [Standard USB Identifiers](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/standard-usb-identifiers)
//     - [Special USB Identifiers](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/special-usb-identifiers)
//  - [Instance specific ID](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/instance-ids) 

From #50 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am OK with that

if (DllSetupDiGetDeviceInstanceId(hDevInfo, pspDevInfoData, buf, buffSize, &nSize)) {
// extract Serial Number
string str = buf;
size_t t = str.find_last_of("\\\\");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are parsing strings like USB\VID_046D&PID_C24E\9025267AF90009

Why are we using a double backslash \\\\? It seems to work just fine with str.find_last_of("\\");. According to the docs, we should only need to define the backslash once, http://www.cplusplus.com/reference/string/string/find_last_of/ 🤷‍♂️


Let's rename this variable to size_t serialNumberIndex = ... to make this more clear

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why double backslash?
I have no idea :) just miswritten.

if ((dwCap & CM_DEVCAP_UNIQUEID) == CM_DEVCAP_UNIQUEID) {
if (DllSetupDiGetDeviceInstanceId(hDevInfo, pspDevInfoData, buf, buffSize, &nSize)) {
// extract Serial Number
string str = buf;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this variable to string deviceInstanceId = buf; so it better represents what it holds

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am OK with that

@@ -7,6 +7,7 @@

// Include Windows headers
#include <windows.h>
#include <Cfgmgr32.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed now?

Copy link
Author

@ghost ghost May 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CM_DEVCAP_UNIQUEID is defined in that header file, so we have to include it.
I added a comment near header file for future reference.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it listed here

But the docs seem to indicate it should be 👍

SPDRP_CAPABILITIES

The function retrieves a bitwise OR of the following CM_DEVCAP_Xxx flags in a DWORD. The device capabilities that are represented by these flags correspond to the device capabilities that are represented by the members of the DEVICE_CAPABILITIES structure. The CM_DEVCAP_Xxx constants are defined in Cfgmgr32.h.

https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx

@@ -423,6 +424,21 @@ void ExtractDeviceInfo(HDEVINFO hDevInfo, SP_DEVINFO_DATA* pspDevInfoData, TCHAR
// Use this to extract VID / PID
extractVidPid(buf, resultItem);
}

DWORD dwCap = 0x0;
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_CAPABILITIES, &DataT, (PBYTE)&dwCap, 4, &nSize)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 4 instead of buffSize like the other instances we call DllSetupDiGetDeviceRegistryProperty?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are trying to get a DWORD, so it is exactly 4.

Copy link
Owner

@MadLittleMods MadLittleMods May 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use sizeOf(DWORD) or something similar?

Why do we use buffSize for all other instances? What's different about this one?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it is defined as;
typedef unsigned long DWORD;
And will not change in the future.
But no problem we can use sizeof(DWORD).

Why do we use buffSize for all other instances? What's different about this one?
In other instances we are reading data to buf variable. In this instance we are reading data to dwCapabilities which is a DWORD.

Copy link
Owner

@MadLittleMods MadLittleMods May 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget to push (no sizeof in the code atm)?

Perhaps even better sizeof(dwCapabilities)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -423,6 +424,21 @@ void ExtractDeviceInfo(HDEVINFO hDevInfo, SP_DEVINFO_DATA* pspDevInfoData, TCHAR
// Use this to extract VID / PID
extractVidPid(buf, resultItem);
}

DWORD dwCap = 0x0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dwCapabilities

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am OK with that

DWORD dwCap = 0x0;
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_CAPABILITIES, &DataT, (PBYTE)&dwCap, 4, &nSize)) {
// If device has a CM_DEVCAP_UNIQUEID capability, then we assume it has a serial number
if ((dwCap & CM_DEVCAP_UNIQUEID) == CM_DEVCAP_UNIQUEID) {
Copy link
Owner

@MadLittleMods MadLittleMods May 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our way is better & more clear.

@ghost
Copy link
Author

ghost commented May 28, 2018

Hi,
I reviewed my code & tested again.

@@ -7,6 +7,7 @@

// Include Windows headers
#include <windows.h>
#include <Cfgmgr32.h> // CM_DEVCAP_UNIQUEID
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put the comment above

// Include `CM_DEVCAP_UNIQUEID`

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@@ -7,6 +7,7 @@

// Include Windows headers
#include <windows.h>
#include <Cfgmgr32.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it listed here

But the docs seem to indicate it should be 👍

SPDRP_CAPABILITIES

The function retrieves a bitwise OR of the following CM_DEVCAP_Xxx flags in a DWORD. The device capabilities that are represented by these flags correspond to the device capabilities that are represented by the members of the DEVICE_CAPABILITIES structure. The CM_DEVCAP_Xxx constants are defined in Cfgmgr32.h.

https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx

Copy link
Owner

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates!

One nit comment and question here #62 (comment)

@MadLittleMods
Copy link
Owner

@doganmurat Still a question here, #62 (comment)

@ghost
Copy link
Author

ghost commented May 28, 2018

Hi @MadLittleMods ,
Any other comment or question?

@MadLittleMods
Copy link
Owner

@doganmurat Same comment, #62 (comment)

@ghost
Copy link
Author

ghost commented May 28, 2018

Hi,
You mean header file comment?
It is already in commit.

@MadLittleMods
Copy link
Owner

@doganmurat I linked the specific sizeof comment. We are still using 4 instead of sizeof(dwCapabilities), #62 (comment)

@ghost
Copy link
Author

ghost commented May 30, 2018

Hi,
Commit 13a0166 changes code to sizeof(DWORD), and
last commit 3ea0c32 changes code to sizeof(dwCapabilities).

Please check it.

@MadLittleMods MadLittleMods merged commit 9aaa036 into MadLittleMods:master Jun 4, 2018
@MadLittleMods
Copy link
Owner

@doganmurat Thanks for the contribution 💖

I merged and created v3.1.0, https://github.com/MadLittleMods/node-usb-detection/releases/tag/v3.1.0

I am going to wait for the prebuild binaries to trickle in from Travis and AppVeyor and publish the release to npm in the morning 🚢

@ghost
Copy link
Author

ghost commented Jun 4, 2018

Hi @MadLittleMods ,

Good news! ✌️
Thank you.

@MadLittleMods
Copy link
Owner

@doganmurat v3.1.0 published to npm 🚀, https://www.npmjs.com/package/usb-detection

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants