From 6a3b24c8c85dd6772713dca368e215d3eeeef303 Mon Sep 17 00:00:00 2001 From: lal12 Date: Mon, 1 Aug 2016 15:21:03 +0200 Subject: [PATCH] Added allConfigDescriptor Property Added property to access all of the device's config descriptors. Moved twice existing code to a static method. --- Readme.md | 3 +++ src/device.cc | 29 +++++++++++++++++++++++------ src/node_usb.h | 2 ++ usb.js | 6 ++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Readme.md b/Readme.md index f2fcbcdf..e98409f8 100644 --- a/Readme.md +++ b/Readme.md @@ -91,6 +91,9 @@ Object with properties for the fields of the configuration descriptor: - bMaxPower - extra (Buffer containing any extra data or additional descriptors) +### .allConfigDescriptors + Contains all config descriptors of the device (same structure as .configDescriptor above) + ### .open() Open the device. All methods below require the device to be open before use. diff --git a/src/device.cc b/src/device.cc index ddeade2f..cb4e7c9a 100644 --- a/src/device.cc +++ b/src/device.cc @@ -93,12 +93,7 @@ static NAN_METHOD(deviceConstructor) { info.GetReturnValue().Set(info.This()); } -NAN_METHOD(Device_GetConfigDescriptor) { - ENTER_METHOD(Device, 0); - - libusb_config_descriptor* cdesc; - CHECK_USB(libusb_get_active_config_descriptor(self->device, &cdesc)); - +Local Device::cdesc2V8(libusb_config_descriptor * cdesc){ Local v8cdesc = Nan::New(); STRUCT_TO_V8(v8cdesc, *cdesc, bLength) @@ -168,11 +163,32 @@ NAN_METHOD(Device_GetConfigDescriptor) { } } } + return v8cdesc; +} +NAN_METHOD(Device_GetConfigDescriptor) { + ENTER_METHOD(Device, 0); + libusb_config_descriptor* cdesc; + CHECK_USB(libusb_get_active_config_descriptor(self->device, &cdesc)); + Local v8cdesc = Device::cdesc2V8(cdesc); libusb_free_config_descriptor(cdesc); info.GetReturnValue().Set(v8cdesc); } +NAN_METHOD(Device_GetAllConfigDescriptors){ + ENTER_METHOD(Device, 0); + libusb_config_descriptor * cdesc; + struct libusb_device_descriptor dd; + libusb_get_device_descriptor(self->device, &dd); + Local v8cdescriptors = Nan::New(dd.bNumConfigurations); + for(uint8_t i = 0; i < dd.bNumConfigurations; i++){ + libusb_get_config_descriptor(self->device, i, &cdesc); + v8cdescriptors->Set(i, Device::cdesc2V8(cdesc)); + libusb_free_config_descriptor(cdesc); + } + info.GetReturnValue().Set(v8cdescriptors); +} + NAN_METHOD(Device_Open) { ENTER_METHOD(Device, 0); if (!self->device_handle){ @@ -358,6 +374,7 @@ void Device::Init(Local target){ tpl->InstanceTemplate()->SetInternalFieldCount(1); Nan::SetPrototypeMethod(tpl, "__getConfigDescriptor", Device_GetConfigDescriptor); + Nan::SetPrototypeMethod(tpl, "__getAllConfigDescriptors", Device_GetAllConfigDescriptors); Nan::SetPrototypeMethod(tpl, "__open", Device_Open); Nan::SetPrototypeMethod(tpl, "__close", Device_Close); Nan::SetPrototypeMethod(tpl, "reset", Device_Reset::begin); diff --git a/src/node_usb.h b/src/node_usb.h index 50603be6..5111bde1 100644 --- a/src/node_usb.h +++ b/src/node_usb.h @@ -37,6 +37,8 @@ struct Device: public Nan::ObjectWrap { ~Device(); static void unpin(libusb_device* device); + static Local cdesc2V8(libusb_config_descriptor * cdesc); + protected: static std::map> byPtr; Device(libusb_device* d); diff --git a/usb.js b/usb.js index f0c1b105..8b617e62 100644 --- a/usb.js +++ b/usb.js @@ -50,6 +50,12 @@ Object.defineProperty(usb.Device.prototype, "configDescriptor", { } }); +Object.defineProperty(usb.Device.prototype, "allConfigDescriptors", { + get: function() { + return this._allConfigDescriptors || (this._allConfigDescriptors = this.__getAllConfigDescriptors()) + } +}); + usb.Device.prototype.interface = function(addr){ if (!this.interfaces){ throw new Error("Device must be open before searching for interfaces")