From dff199de91292531a3b6e96f7bd5d655a3e0ca08 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 30 Nov 2017 16:18:38 -0400 Subject: [PATCH] Implement .listDriverlessDevices() Change-Type: minor Signed-off-by: Juan Cruz Viotti --- README.md | 12 ++++++++++ index.js | 10 ++++++++ src/generator.cpp | 59 +++++++++++++++-------------------------------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 7cdac8a..4c4007e 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,18 @@ on such Visual Studio version. Documentation ------------- +### `Object[] .listDriverlessDevices()` + +Return a list of all driverless devices found on the system. + +For example: + +```js +winusbDriverGenerator.listDriverlessDevices().forEach((device) => { + console.log(device) +}) +``` + ### `Boolean .hasDriver(Number vendorId, Number productId)` Check if there is a driver associated with a USB device given its vendor and diff --git a/index.js b/index.js index 43493ee..c047914 100644 --- a/index.js +++ b/index.js @@ -26,3 +26,13 @@ module.exports = require('bindings')({ module_root: __dirname /* eslint-enable camelcase */ }); + +module.exports.hasDriver = (vendorId, productId) => { + for (const device of module.exports.listDriverlessDevices()) { + if (device.vid === vendorId && device.pid === productId) { + return false; + } + } + + return true; +}; diff --git a/src/generator.cpp b/src/generator.cpp index c458f08..5520838 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -46,44 +46,28 @@ generator_install_winusb_inf(struct wdi_device_info *device, return wdi_install_driver(device, path, name, NULL); } -NAN_METHOD(hasDriver) { - if (info.Length() != 2) { - return Nan::ThrowError("This function expects 2 arguments"); - } - - if (!info[0]->IsNumber()) { - return Nan::ThrowError("Product id must be a number"); - } - - if (!info[1]->IsNumber()) { - return Nan::ThrowError("Vendor id must be a number"); - } - - const uint16_t vendor = info[0]->Uint32Value(); - const uint16_t product = info[1]->Uint32Value(); - - std::cout << "Searching for device: " << std::hex - << "0x" << vendor - << ":0x" << product - << std::endl; - wdi_set_log_level(WDI_LOG_LEVEL_WARNING); - - bool found = true; +NAN_METHOD(listDriverlessDevices) { int code = WDI_SUCCESS; struct wdi_device_info *device_list_node; + v8::Local devices = Nan::New(); + wdi_set_log_level(WDI_LOG_LEVEL_WARNING); code = generator_list_driverless_devices(&device_list_node); if (code == WDI_SUCCESS) { + uint32_t index = 0; for (; device_list_node != NULL ; device_list_node = device_list_node->next) { - std::cout << "Found: " << std::hex - << "0x" << device_list_node->vid - << ":0x" << device_list_node->pid - << std::endl; - if (device_list_node->vid == vendor && device_list_node->pid == product) { - found = false; - break; - } + v8::Local device = Nan::New(); + Nan::Set(device, Nan::New("vid").ToLocalChecked(), + Nan::New(static_cast(device_list_node->vid))); + Nan::Set(device, Nan::New("pid").ToLocalChecked(), + Nan::New(static_cast(device_list_node->pid))); + Nan::Set(device, Nan::New("hid").ToLocalChecked(), + Nan::New(device_list_node->hardware_id).ToLocalChecked()); + Nan::Set(device, Nan::New("did").ToLocalChecked(), + Nan::New(device_list_node->device_id).ToLocalChecked()); + Nan::Set(devices, index, device); + index++; } code = wdi_destroy_list(device_list_node); @@ -95,17 +79,12 @@ NAN_METHOD(hasDriver) { // See https://github.com/pbatard/libwdi/wiki/Usage // // If the list of driverless devices is empty, then we - // can assume every device has a driver, including the - // one the user asked about. - } else if (code == WDI_ERROR_NO_DEVICE) { - std::cout << "No driverless device detected. " - "Assuming device has a driver" << std::endl; - found = true; - } else { + // can assume every device has a driver. + } else if (code != WDI_ERROR_NO_DEVICE) { return Nan::ThrowError(wdi_strerror(code)); } - info.GetReturnValue().Set(Nan::New(found)); + info.GetReturnValue().Set(devices); } NAN_METHOD(associate) { @@ -197,7 +176,7 @@ NAN_METHOD(associate) { } NAN_MODULE_INIT(GeneratorInit) { - NAN_EXPORT(target, hasDriver); + NAN_EXPORT(target, listDriverlessDevices); NAN_EXPORT(target, associate); }