Skip to content

Commit d761a7c

Browse files
committed
Use accessors for interface and endpoint lists
1 parent 855cb46 commit d761a7c

File tree

5 files changed

+55
-48
lines changed

5 files changed

+55
-48
lines changed

src/device.cc

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ namespace NodeUsb {
3232
instance_template->SetAccessor(V8STR("busNumber"), Device::BusNumberGetter);
3333
instance_template->SetAccessor(V8SYM("timeout"), Device::TimeoutGetter, Device::TimeoutSetter);
3434
instance_template->SetAccessor(V8STR("configDescriptor"), Device::ConfigDescriptorGetter);
35+
instance_template->SetAccessor(V8STR("interfaces"), Device::InterfacesGetter);
3536

3637
// Bindings to nodejs
3738
NODE_SET_PROTOTYPE_METHOD(t, "reset", Device::Reset);
38-
NODE_SET_PROTOTYPE_METHOD(t, "getInterfaces", Device::GetInterfaces);
3939
NODE_SET_PROTOTYPE_METHOD(t, "controlTransfer", Device::ControlTransfer);
4040

4141
// Make it visible in JavaScript
@@ -56,6 +56,7 @@ namespace NodeUsb {
5656
// free configuration descriptor
5757
close();
5858
v8ConfigDescriptor.Dispose();
59+
v8Interfaces.Dispose();
5960
libusb_free_config_descriptor(config_descriptor);
6061
libusb_unref_device(device);
6162
}
@@ -239,35 +240,36 @@ namespace NodeUsb {
239240
return scope.Close(self->v8ConfigDescriptor);
240241
}
241242

242-
Handle<Value> Device::GetInterfaces(const Arguments& args) {
243-
LOCAL(Device, self, args.This())
243+
Handle<Value> Device::InterfacesGetter(Local<String> property, const AccessorInfo &info) {
244+
LOCAL(Device, self, info.Holder())
244245

245-
LIBUSB_GET_CONFIG_DESCRIPTOR(scope);
246+
if (self->v8Interfaces.IsEmpty()){
247+
LIBUSB_GET_CONFIG_DESCRIPTOR(scope);
246248

247-
Local<Array> r = Array::New();
248-
int idx = 0;
249+
self->v8Interfaces = Persistent<Array>::New(Array::New());
250+
int idx = 0;
249251

250-
// iterate interfaces
251-
int numInterfaces = (*self->config_descriptor).bNumInterfaces;
252+
// iterate interfaces
253+
int numInterfaces = (*self->config_descriptor).bNumInterfaces;
252254

253-
for (int idxInterface = 0; idxInterface < numInterfaces; idxInterface++) {
254-
int numAltSettings = ((*self->config_descriptor).interface[idxInterface]).num_altsetting;
255+
for (int idxInterface = 0; idxInterface < numInterfaces; idxInterface++) {
256+
int numAltSettings = ((*self->config_descriptor).interface[idxInterface]).num_altsetting;
255257

256-
for (int idxAltSetting = 0; idxAltSetting < numAltSettings; idxAltSetting++) {
257-
// passing a pointer of libusb_interface_descriptor does not work. struct is lost by V8
258-
// idx of interface and alt_setting is passed so that Interface class can extract the given interface
259-
Local<Value> args_new_interface[3] = {
260-
args.This(),
261-
Uint32::New(idxInterface),
262-
Uint32::New(idxAltSetting),
263-
};
258+
for (int idxAltSetting = 0; idxAltSetting < numAltSettings; idxAltSetting++) {
259+
Local<Value> args_new_interface[3] = {
260+
info.Holder(),
261+
Uint32::New(idxInterface),
262+
Uint32::New(idxAltSetting),
263+
};
264264

265-
// create new object instance of class NodeUsb::Interface
266-
r->Set(idx++, Interface::constructor_template->GetFunction()->NewInstance(3, args_new_interface));
265+
self->v8Interfaces->Set(idx++,
266+
Interface::constructor_template->GetFunction()->NewInstance(3, args_new_interface)
267+
);
268+
}
267269
}
268270
}
269271

270-
return scope.Close(r);
272+
return scope.Close(self->v8Interfaces);
271273
}
272274

273275
/**

src/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ namespace NodeUsb {
2828
unsigned timeout;
2929

3030
protected:
31-
3231
Persistent<Object> v8ConfigDescriptor;
32+
Persistent<Array> v8Interfaces;
3333

3434
// V8 getter
3535
static Handle<Value> BusNumberGetter(Local<String> property, const AccessorInfo &info);
3636
static Handle<Value> DeviceAddressGetter(Local<String> property, const AccessorInfo &info);
3737
static Handle<Value> TimeoutGetter(Local<String> property, const AccessorInfo &info);
3838
static void TimeoutSetter(Local<String> property, Local<Value> value, const AccessorInfo &info);
3939
static Handle<Value> ConfigDescriptorGetter(Local<String> property, const AccessorInfo &info);
40+
static Handle<Value> InterfacesGetter(Local<String> property, const AccessorInfo &info);
4041

4142
// exposed to V8
4243
static Handle<Value> New(const Arguments& args);
@@ -46,7 +47,6 @@ namespace NodeUsb {
4647
// Reset -> Async
4748
static void EIO_Reset(uv_work_t *req);
4849
static void EIO_After_Reset(uv_work_t *req);
49-
static Handle<Value> GetInterfaces(const Arguments& args);
5050
static Handle<Value> ControlTransfer(const Arguments& args);
5151
static void EIO_ControlTransfer(uv_work_t *req);
5252
static void EIO_After_ControlTransfer(uv_work_t *req);

src/interface.cc

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace NodeUsb {
1818
Interface::~Interface() {
1919
// TODO Close
2020
v8device.Dispose();
21+
v8Endpoints.Dispose();
2122
DEBUG("Interface object destroyed")
2223
}
2324

@@ -40,9 +41,9 @@ namespace NodeUsb {
4041
instance_template->SetAccessor(V8STR("interface"), Interface::IdxInterfaceGetter);
4142
instance_template->SetAccessor(V8STR("altSetting"), Interface::IdxAltSettingGetter);
4243
instance_template->SetAccessor(V8STR("extraData"), Interface::ExtraDataGetter);
44+
instance_template->SetAccessor(V8STR("endpoints"), Interface::EndpointsGetter);
4345

4446
// methods exposed to node.js
45-
NODE_SET_PROTOTYPE_METHOD(t, "getEndpoints", Interface::GetEndpoints);
4647
NODE_SET_PROTOTYPE_METHOD(t, "detachKernelDriver", Interface::DetachKernelDriver);
4748
NODE_SET_PROTOTYPE_METHOD(t, "attachKernelDriver", Interface::AttachKernelDriver);
4849
NODE_SET_PROTOTYPE_METHOD(t, "claim", Interface::Claim);
@@ -247,25 +248,30 @@ namespace NodeUsb {
247248
TRANSFER_REQUEST_FREE(alternate_setting_request, interface);
248249
}
249250

250-
Handle<Value> Interface::GetEndpoints(const Arguments& args) {
251-
LOCAL(Interface, self, args.This())
252-
Local<Array> r = Array::New();
253-
254-
// interate endpoints
255-
int numEndpoints = (*self->descriptor).bNumEndpoints;
256-
257-
for (int i = 0; i < numEndpoints; i++) {
258-
Local<Value> args_new_endpoint[4] = {
259-
Local<Object>::New(self->v8device),
260-
Uint32::New(self->idx_interface),
261-
Uint32::New(self->idx_alt_setting),
262-
Uint32::New(i)
263-
};
251+
Handle<Value> Interface::EndpointsGetter(Local<String> property, const AccessorInfo &info) {
252+
LOCAL(Interface, self, info.Holder())
264253

265-
// create new object instance of class NodeUsb::Endpoint
266-
r->Set(i, Endpoint::constructor_template->GetFunction()->NewInstance(4, args_new_endpoint));
254+
if (self->v8Endpoints.IsEmpty()){
255+
self->v8Endpoints = Persistent<Array>::New(Array::New());
256+
257+
// interate endpoints
258+
int numEndpoints = (*self->descriptor).bNumEndpoints;
259+
260+
for (int i = 0; i < numEndpoints; i++) {
261+
Local<Value> args_new_endpoint[4] = {
262+
Local<Object>::New(self->v8device),
263+
Uint32::New(self->idx_interface),
264+
Uint32::New(self->idx_alt_setting),
265+
Uint32::New(i)
266+
};
267+
268+
// create new object instance of class NodeUsb::Endpoint
269+
self->v8Endpoints->Set(i,
270+
Endpoint::constructor_template->GetFunction()->NewInstance(4, args_new_endpoint)
271+
);
272+
}
267273
}
268274

269-
return r;
275+
return scope.Close(self->v8Endpoints);;
270276
}
271277
}

src/interface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ namespace NodeUsb {
3535
uint32_t idx_interface;
3636
uint32_t idx_alt_setting;
3737

38+
Persistent<Array> v8Endpoints;
39+
3840
// V8 getter
3941
static Handle<Value> IdxInterfaceGetter(Local<String> property, const AccessorInfo &info);
4042
static Handle<Value> IdxAltSettingGetter(Local<String> property, const AccessorInfo &info);
4143
static Handle<Value> ExtraDataGetter(Local<String> property, const AccessorInfo &info);
44+
static Handle<Value> EndpointsGetter(Local<String> property, const AccessorInfo &info);
4245

4346
// exposed to V8
4447
static Handle<Value> New(const Arguments& args);
4548
static Handle<Value> IsKernelDriverActive(const Arguments& args);
4649
static Handle<Value> DetachKernelDriver(const Arguments& args);
4750
static Handle<Value> AttachKernelDriver(const Arguments& args);
4851
static Handle<Value> Claim(const Arguments& args);
49-
static Handle<Value> GetEndpoints(const Arguments& args);
5052

5153
static Handle<Value> Release(const Arguments& args);
5254
static void EIO_Release(uv_work_t *req);

tests/node-usb-test.coffee

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,17 @@ test "Control transfer in", ->
7575

7676
interface = null
7777
test "Get interface", ->
78-
interfaces = device.getInterfaces()
79-
interface = interfaces[0]
78+
interface = device.interfaces[0]
8079
assert.notEqual(interface, undefined, "Interface must be defined")
8180

8281
test "Claim interface", ->
8382
interface.claim()
8483

85-
endpoints = null
8684
inEndpoint = null
8785
outEndpoint = null
8886

8987
test "Get in endpoint", ->
90-
endpoints = interface.getEndpoints()
91-
inEndpoint = endpoints[0]
88+
inEndpoint = interface.endpoints[0]
9289
assert.notEqual(inEndpoint, undefined, "Endpoint must be defined")
9390
assert.equal(inEndpoint.direction, usb.LIBUSB_ENDPOINT_IN)
9491

@@ -103,7 +100,7 @@ test "Read from IN endpoint", ->
103100
wait()
104101

105102
test "Get out endpoint", ->
106-
outEndpoint = endpoints[1]
103+
outEndpoint = interface.endpoints[1]
107104
assert.notEqual(outEndpoint, undefined, "Endpoint must be defined")
108105
assert.equal(outEndpoint.direction, usb.LIBUSB_ENDPOINT_OUT)
109106

0 commit comments

Comments
 (0)