Skip to content

Commit

Permalink
Merge pull request #131 from lal12/addfunctions
Browse files Browse the repository at this point in the history
Added allConfigDescriptor Property
  • Loading branch information
kevinmehall committed Feb 18, 2017
2 parents 01f59d6 + 6a3b24c commit 3093b1a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Expand Up @@ -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.
Expand Down
29 changes: 23 additions & 6 deletions src/device.cc
Expand Up @@ -92,12 +92,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<Object> Device::cdesc2V8(libusb_config_descriptor * cdesc){
Local<Object> v8cdesc = Nan::New<Object>();

STRUCT_TO_V8(v8cdesc, *cdesc, bLength)
Expand Down Expand Up @@ -167,11 +162,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<Object> 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<Array> v8cdescriptors = Nan::New<Array>(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){
Expand Down Expand Up @@ -357,6 +373,7 @@ void Device::Init(Local<Object> 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);
Expand Down
2 changes: 2 additions & 0 deletions src/node_usb.h
Expand Up @@ -37,6 +37,8 @@ struct Device: public Nan::ObjectWrap {
~Device();
static void unpin(libusb_device* device);

static Local<Object> cdesc2V8(libusb_config_descriptor * cdesc);

protected:
static std::map<libusb_device*, Nan::Persistent<Object>> byPtr;
Device(libusb_device* d);
Expand Down
6 changes: 6 additions & 0 deletions usb.js
Expand Up @@ -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")
Expand Down

0 comments on commit 3093b1a

Please sign in to comment.