usb 2.4.2 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitmodules +2 -2
- package/CHANGELOG.md +16 -0
- package/README.md +42 -2
- package/binding.gyp +48 -25
- package/dist/usb/bindings.d.ts +1 -0
- package/dist/usb/endpoint.d.ts +1 -1
- package/dist/usb/endpoint.js +4 -2
- package/dist/usb/endpoint.js.map +1 -1
- package/dist/usb/index.d.ts +6 -0
- package/dist/usb/index.js +82 -53
- package/dist/usb/index.js.map +1 -1
- package/package.json +4 -5
- package/prebuilds/android-arm/node.napi.armv7.node +0 -0
- package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
- package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/linux-ia32/node.napi.node +0 -0
- package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
- package/prebuilds/linux-x64/node.napi.musl.node +0 -0
- package/prebuilds/win32-ia32/node.napi.node +0 -0
- package/prebuilds/win32-x64/node.napi.node +0 -0
- package/src/device.cc +309 -309
- package/src/helpers.h +36 -36
- package/src/hotplug/hotplug.h +22 -0
- package/src/hotplug/libusb.cc +90 -0
- package/src/hotplug/windows.cc +161 -0
- package/src/node_usb.cc +246 -282
- package/src/node_usb.h +64 -67
- package/src/thread_name.cc +1 -1
- package/src/transfer.cc +114 -114
- package/test/usb.coffee +178 -178
- package/test/webusb.coffee +140 -140
- package/tsc/usb/bindings.ts +1 -0
- package/tsc/usb/endpoint.ts +4 -3
- package/tsc/usb/index.ts +73 -32
package/src/device.cc
CHANGED
|
@@ -2,414 +2,414 @@
|
|
|
2
2
|
#include <string.h>
|
|
3
3
|
|
|
4
4
|
#define STRUCT_TO_V8(TARGET, STR, NAME) \
|
|
5
|
-
|
|
5
|
+
TARGET.DefineProperty(Napi::PropertyDescriptor::Value(#NAME, Napi::Number::New(env, (uint32_t) (STR).NAME), CONST_PROP));
|
|
6
6
|
|
|
7
7
|
#define CHECK_OPEN() \
|
|
8
|
-
|
|
8
|
+
if (!self->device_handle){THROW_ERROR("Device is not opened");}
|
|
9
9
|
|
|
10
10
|
#define MAX_PORTS 7
|
|
11
11
|
|
|
12
12
|
Device::Device(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Device>(info), env(0), device_handle(0), refs_(0), completionQueue(handleCompletion) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
env = info.Env();
|
|
14
|
+
device = info[0].As<Napi::External<libusb_device>>().Data();
|
|
15
|
+
libusb_ref_device(device);
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
std::map<libusb_device*, Device*>& byPtr = env.GetInstanceData<ModuleData>()->byPtr;
|
|
18
|
+
byPtr[device] = this;
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
DEBUG_LOG("Created device %p", this);
|
|
21
|
+
Constructor(info);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
Device::~Device() {
|
|
25
|
-
|
|
25
|
+
DEBUG_LOG("Freed device %p", this);
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
ModuleData* instanceData = env.GetInstanceData<ModuleData>();
|
|
28
|
+
std::map<libusb_device*, Device*>& byPtr = instanceData->byPtr;
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
auto it = byPtr.find(device);
|
|
31
|
+
if (it != byPtr.end() && it->second == this)
|
|
32
|
+
byPtr.erase(it);
|
|
33
|
+
libusb_close(device_handle);
|
|
34
|
+
libusb_unref_device(device);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// Get a V8 instance for a libusb_device: either the existing one from the map,
|
|
38
38
|
// or create a new one and add it to the map.
|
|
39
39
|
Napi::Object Device::get(Napi::Env env, libusb_device* dev) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
ModuleData* instanceData = env.GetInstanceData<ModuleData>();
|
|
41
|
+
std::map<libusb_device*, Device*>& byPtr = instanceData->byPtr;
|
|
42
|
+
|
|
43
|
+
auto it = byPtr.find(dev);
|
|
44
|
+
if (it != byPtr.end()) {
|
|
45
|
+
auto value = it->second->Value();
|
|
46
|
+
// JS object may have already been garbage collected
|
|
47
|
+
if (!value.IsEmpty())
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
Napi::Object obj = instanceData->deviceConstructor.New({ Napi::External<libusb_device>::New(env, dev) });
|
|
52
|
+
return obj;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
Napi::Value Device::Constructor(const Napi::CallbackInfo& info) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
56
|
+
Napi::Env env = info.Env();
|
|
57
|
+
ENTER_CONSTRUCTOR_POINTER(Device, 1);
|
|
58
|
+
auto obj = info.This().As<Napi::Object>();
|
|
59
|
+
obj.DefineProperty(Napi::PropertyDescriptor::Value("busNumber", Napi::Number::New(env, libusb_get_bus_number(self->device)), CONST_PROP));
|
|
60
|
+
obj.DefineProperty(Napi::PropertyDescriptor::Value("deviceAddress", Napi::Number::New(env, libusb_get_device_address(self->device)), CONST_PROP));
|
|
61
|
+
|
|
62
|
+
Napi::Object v8dd = Napi::Object::New(env);
|
|
63
|
+
obj.DefineProperty(Napi::PropertyDescriptor::Value("deviceDescriptor", v8dd, CONST_PROP));
|
|
64
|
+
|
|
65
|
+
struct libusb_device_descriptor dd;
|
|
66
|
+
CHECK_USB(libusb_get_device_descriptor(self->device, &dd));
|
|
67
|
+
|
|
68
|
+
STRUCT_TO_V8(v8dd, dd, bLength)
|
|
69
|
+
STRUCT_TO_V8(v8dd, dd, bDescriptorType)
|
|
70
|
+
STRUCT_TO_V8(v8dd, dd, bcdUSB)
|
|
71
|
+
STRUCT_TO_V8(v8dd, dd, bDeviceClass)
|
|
72
|
+
STRUCT_TO_V8(v8dd, dd, bDeviceSubClass)
|
|
73
|
+
STRUCT_TO_V8(v8dd, dd, bDeviceProtocol)
|
|
74
|
+
STRUCT_TO_V8(v8dd, dd, bMaxPacketSize0)
|
|
75
|
+
STRUCT_TO_V8(v8dd, dd, idVendor)
|
|
76
|
+
STRUCT_TO_V8(v8dd, dd, idProduct)
|
|
77
|
+
STRUCT_TO_V8(v8dd, dd, bcdDevice)
|
|
78
|
+
STRUCT_TO_V8(v8dd, dd, iManufacturer)
|
|
79
|
+
STRUCT_TO_V8(v8dd, dd, iProduct)
|
|
80
|
+
STRUCT_TO_V8(v8dd, dd, iSerialNumber)
|
|
81
|
+
STRUCT_TO_V8(v8dd, dd, bNumConfigurations)
|
|
82
|
+
|
|
83
|
+
uint8_t port_numbers[MAX_PORTS];
|
|
84
|
+
int ret = libusb_get_port_numbers(self->device, &port_numbers[0], MAX_PORTS);
|
|
85
|
+
if (ret > 0) {
|
|
86
|
+
Napi::Array array = Napi::Array::New(env, ret);
|
|
87
|
+
for (int i = 0; i < ret; ++ i) {
|
|
88
|
+
array.Set(i, Napi::Number::New(env, port_numbers[i]));
|
|
89
|
+
}
|
|
90
|
+
obj.DefineProperty(Napi::PropertyDescriptor::Value("portNumbers", array, CONST_PROP));
|
|
91
|
+
}
|
|
92
|
+
return info.This();
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
Napi::Object Device::cdesc2V8(Napi::Env env, libusb_config_descriptor * cdesc) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
96
|
+
Napi::Object v8cdesc = Napi::Object::New(env);
|
|
97
|
+
|
|
98
|
+
STRUCT_TO_V8(v8cdesc, *cdesc, bLength)
|
|
99
|
+
STRUCT_TO_V8(v8cdesc, *cdesc, bDescriptorType)
|
|
100
|
+
STRUCT_TO_V8(v8cdesc, *cdesc, wTotalLength)
|
|
101
|
+
STRUCT_TO_V8(v8cdesc, *cdesc, bNumInterfaces)
|
|
102
|
+
STRUCT_TO_V8(v8cdesc, *cdesc, bConfigurationValue)
|
|
103
|
+
STRUCT_TO_V8(v8cdesc, *cdesc, iConfiguration)
|
|
104
|
+
STRUCT_TO_V8(v8cdesc, *cdesc, bmAttributes)
|
|
105
|
+
// Libusb 1.0 typo'd bMaxPower as MaxPower
|
|
106
|
+
v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("bMaxPower", Napi::Number::New(env, (uint32_t)cdesc->MaxPower), CONST_PROP));
|
|
107
|
+
|
|
108
|
+
v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", Napi::Buffer<const char>::Copy(env, (const char*)cdesc->extra, cdesc->extra_length), CONST_PROP));
|
|
109
|
+
|
|
110
|
+
Napi::Array v8interfaces = Napi::Array::New(env, cdesc->bNumInterfaces);
|
|
111
|
+
v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("interfaces", v8interfaces, CONST_PROP));
|
|
112
|
+
|
|
113
|
+
for (int idxInterface = 0; idxInterface < cdesc->bNumInterfaces; idxInterface++) {
|
|
114
|
+
int numAltSettings = cdesc->interface[idxInterface].num_altsetting;
|
|
115
|
+
|
|
116
|
+
Napi::Array v8altsettings = Napi::Array::New(env, numAltSettings);
|
|
117
|
+
v8interfaces.Set(idxInterface, v8altsettings);
|
|
118
|
+
|
|
119
|
+
for (int idxAltSetting = 0; idxAltSetting < numAltSettings; idxAltSetting++) {
|
|
120
|
+
const libusb_interface_descriptor& idesc =
|
|
121
|
+
cdesc->interface[idxInterface].altsetting[idxAltSetting];
|
|
122
|
+
|
|
123
|
+
Napi::Object v8idesc = Napi::Object::New(env);
|
|
124
|
+
v8altsettings.Set(idxAltSetting, v8idesc);
|
|
125
|
+
|
|
126
|
+
STRUCT_TO_V8(v8idesc, idesc, bLength)
|
|
127
|
+
STRUCT_TO_V8(v8idesc, idesc, bDescriptorType)
|
|
128
|
+
STRUCT_TO_V8(v8idesc, idesc, bInterfaceNumber)
|
|
129
|
+
STRUCT_TO_V8(v8idesc, idesc, bAlternateSetting)
|
|
130
|
+
STRUCT_TO_V8(v8idesc, idesc, bNumEndpoints)
|
|
131
|
+
STRUCT_TO_V8(v8idesc, idesc, bInterfaceClass)
|
|
132
|
+
STRUCT_TO_V8(v8idesc, idesc, bInterfaceSubClass)
|
|
133
|
+
STRUCT_TO_V8(v8idesc, idesc, bInterfaceProtocol)
|
|
134
|
+
STRUCT_TO_V8(v8idesc, idesc, iInterface)
|
|
135
|
+
|
|
136
|
+
v8idesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", Napi::Buffer<const char>::Copy(env, (const char*)idesc.extra, idesc.extra_length), CONST_PROP));
|
|
137
|
+
|
|
138
|
+
Napi::Array v8endpoints = Napi::Array::New(env, idesc.bNumEndpoints);
|
|
139
|
+
v8idesc.DefineProperty(Napi::PropertyDescriptor::Value("endpoints", v8endpoints, CONST_PROP));
|
|
140
|
+
for (int idxEndpoint = 0; idxEndpoint < idesc.bNumEndpoints; idxEndpoint++){
|
|
141
|
+
const libusb_endpoint_descriptor& edesc = idesc.endpoint[idxEndpoint];
|
|
142
|
+
|
|
143
|
+
Napi::Object v8edesc = Napi::Object::New(env);
|
|
144
|
+
v8endpoints.Set(idxEndpoint, v8edesc);
|
|
145
|
+
|
|
146
|
+
STRUCT_TO_V8(v8edesc, edesc, bLength)
|
|
147
|
+
STRUCT_TO_V8(v8edesc, edesc, bDescriptorType)
|
|
148
|
+
STRUCT_TO_V8(v8edesc, edesc, bEndpointAddress)
|
|
149
|
+
STRUCT_TO_V8(v8edesc, edesc, bmAttributes)
|
|
150
|
+
STRUCT_TO_V8(v8edesc, edesc, wMaxPacketSize)
|
|
151
|
+
STRUCT_TO_V8(v8edesc, edesc, bInterval)
|
|
152
|
+
STRUCT_TO_V8(v8edesc, edesc, bRefresh)
|
|
153
|
+
STRUCT_TO_V8(v8edesc, edesc, bSynchAddress)
|
|
154
|
+
|
|
155
|
+
v8edesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", Napi::Buffer<const char>::Copy(env, (const char*)edesc.extra, edesc.extra_length), CONST_PROP));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return v8cdesc;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
Napi::Value Device::GetConfigDescriptor(const Napi::CallbackInfo& info) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
163
|
+
ENTER_METHOD(Device, 0);
|
|
164
|
+
libusb_config_descriptor* cdesc;
|
|
165
|
+
CHECK_USB(libusb_get_active_config_descriptor(self->device, &cdesc));
|
|
166
|
+
Napi::Object v8cdesc = Device::cdesc2V8(env, cdesc);
|
|
167
|
+
libusb_free_config_descriptor(cdesc);
|
|
168
|
+
return v8cdesc;
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
Napi::Value Device::GetAllConfigDescriptors(const Napi::CallbackInfo& info) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
172
|
+
ENTER_METHOD(Device, 0);
|
|
173
|
+
libusb_config_descriptor * cdesc;
|
|
174
|
+
struct libusb_device_descriptor dd;
|
|
175
|
+
libusb_get_device_descriptor(self->device, &dd);
|
|
176
|
+
Napi::Array v8cdescriptors = Napi::Array::New(env, dd.bNumConfigurations);
|
|
177
|
+
for(uint8_t i = 0; i < dd.bNumConfigurations; i++){
|
|
178
|
+
libusb_get_config_descriptor(device, i, &cdesc);
|
|
179
|
+
v8cdescriptors.Set(i, Device::cdesc2V8(env, cdesc));
|
|
180
|
+
libusb_free_config_descriptor(cdesc);
|
|
181
|
+
}
|
|
182
|
+
return v8cdescriptors;
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
Napi::Value Device::GetParent(const Napi::CallbackInfo& info) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
186
|
+
ENTER_METHOD(Device, 0);
|
|
187
|
+
libusb_device* dev = libusb_get_parent(self->device);
|
|
188
|
+
if(dev)
|
|
189
|
+
return Device::get(env, dev);
|
|
190
|
+
else
|
|
191
|
+
return env.Null();
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
Napi::Value Device::Open(const Napi::CallbackInfo& info) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
195
|
+
ENTER_METHOD(Device, 0);
|
|
196
|
+
if (!self->device_handle){
|
|
197
|
+
CHECK_USB(libusb_open(self->device, &self->device_handle));
|
|
198
|
+
completionQueue.start(info.Env());
|
|
199
|
+
}
|
|
200
|
+
return env.Undefined();
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
Napi::Value Device::Close(const Napi::CallbackInfo& info) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
204
|
+
ENTER_METHOD(Device, 0);
|
|
205
|
+
if (self->canClose()){
|
|
206
|
+
libusb_close(self->device_handle);
|
|
207
|
+
self->device_handle = NULL;
|
|
208
|
+
completionQueue.stop();
|
|
209
|
+
}else{
|
|
210
|
+
THROW_ERROR("Can't close device with a pending request");
|
|
211
|
+
}
|
|
212
|
+
return env.Undefined();
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
struct Req: Napi::AsyncWorker {
|
|
216
|
-
|
|
217
|
-
|
|
216
|
+
Device* device;
|
|
217
|
+
int errcode;
|
|
218
218
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
219
|
+
Req(Device* d, Napi::Function& callback)
|
|
220
|
+
: Napi::AsyncWorker(callback), device(d) {
|
|
221
|
+
device->ref();
|
|
222
|
+
}
|
|
223
223
|
|
|
224
224
|
void OnOK() override {
|
|
225
|
-
|
|
225
|
+
auto env = Env();
|
|
226
226
|
Napi::HandleScope scope(env);
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
227
|
+
device->unref();
|
|
228
|
+
|
|
229
|
+
Napi::Value error = env.Undefined();
|
|
230
|
+
if (errcode < 0){
|
|
231
|
+
error = libusbException(env, errcode).Value();
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
Callback().Call(device->Value(), { error });
|
|
235
|
+
}
|
|
236
|
+
catch (const Napi::Error& e) {
|
|
237
|
+
Napi::Error::Fatal("", e.what());
|
|
238
|
+
}
|
|
239
239
|
}
|
|
240
240
|
};
|
|
241
241
|
|
|
242
242
|
struct Device_Reset: Req {
|
|
243
|
-
|
|
243
|
+
Device_Reset(Device* d, Napi::Function& callback): Req(d, callback) {}
|
|
244
244
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
245
|
+
virtual void Execute() {
|
|
246
|
+
errcode = libusb_reset_device(device->device_handle);
|
|
247
|
+
}
|
|
248
248
|
};
|
|
249
249
|
|
|
250
250
|
Napi::Value Device::Reset(const Napi::CallbackInfo& info) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
251
|
+
ENTER_METHOD(Device, 1);
|
|
252
|
+
CHECK_OPEN();
|
|
253
|
+
CALLBACK_ARG(0);
|
|
254
|
+
auto baton = new Device_Reset(self, callback);
|
|
255
|
+
baton->Queue();
|
|
256
|
+
return env.Undefined();
|
|
257
257
|
}
|
|
258
258
|
|
|
259
259
|
struct Device_Clear_Halt: Req {
|
|
260
|
-
|
|
260
|
+
Device_Clear_Halt(Device* d, Napi::Function& callback): Req(d, callback) {}
|
|
261
261
|
|
|
262
|
-
|
|
262
|
+
int endpoint;
|
|
263
263
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
264
|
+
virtual void Execute() {
|
|
265
|
+
errcode = libusb_clear_halt(device->device_handle, endpoint);
|
|
266
|
+
}
|
|
267
267
|
};
|
|
268
268
|
|
|
269
269
|
Napi::Value Device::ClearHalt(const Napi::CallbackInfo& info) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
270
|
+
ENTER_METHOD(Device, 2);
|
|
271
|
+
int endpoint;
|
|
272
|
+
CHECK_OPEN();
|
|
273
|
+
INT_ARG(endpoint, 0);
|
|
274
|
+
CALLBACK_ARG(1);
|
|
275
|
+
auto baton = new Device_Clear_Halt(self, callback);
|
|
276
|
+
baton->endpoint = endpoint;
|
|
277
|
+
baton->Queue();
|
|
278
|
+
return env.Undefined();
|
|
279
279
|
}
|
|
280
280
|
|
|
281
281
|
Napi::Value Device::IsKernelDriverActive(const Napi::CallbackInfo& info) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
282
|
+
ENTER_METHOD(Device, 1);
|
|
283
|
+
CHECK_OPEN();
|
|
284
|
+
int interface;
|
|
285
|
+
INT_ARG(interface, 0);
|
|
286
|
+
int r = libusb_kernel_driver_active(self->device_handle, interface);
|
|
287
|
+
CHECK_USB(r);
|
|
288
|
+
return Napi::Boolean::New(env, r);
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
Napi::Value Device::DetachKernelDriver(const Napi::CallbackInfo& info) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
292
|
+
ENTER_METHOD(Device, 1);
|
|
293
|
+
CHECK_OPEN();
|
|
294
|
+
int interface;
|
|
295
|
+
INT_ARG(interface, 0);
|
|
296
|
+
CHECK_USB(libusb_detach_kernel_driver(self->device_handle, interface));
|
|
297
|
+
return env.Undefined();
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
Napi::Value Device::AttachKernelDriver(const Napi::CallbackInfo& info) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
301
|
+
ENTER_METHOD(Device, 1);
|
|
302
|
+
CHECK_OPEN();
|
|
303
|
+
int interface;
|
|
304
|
+
INT_ARG(interface, 0);
|
|
305
|
+
CHECK_USB(libusb_attach_kernel_driver(self->device_handle, interface));
|
|
306
|
+
return env.Undefined();
|
|
307
307
|
}
|
|
308
308
|
|
|
309
309
|
Napi::Value Device::ClaimInterface(const Napi::CallbackInfo& info) {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
310
|
+
ENTER_METHOD(Device, 1);
|
|
311
|
+
CHECK_OPEN();
|
|
312
|
+
int interface;
|
|
313
|
+
INT_ARG(interface, 0);
|
|
314
|
+
CHECK_USB(libusb_claim_interface(self->device_handle, interface));
|
|
315
|
+
return env.Undefined();
|
|
316
316
|
}
|
|
317
317
|
|
|
318
318
|
struct Device_ReleaseInterface: Req {
|
|
319
|
-
|
|
319
|
+
Device_ReleaseInterface(Device* d, Napi::Function& callback): Req(d, callback) {}
|
|
320
320
|
|
|
321
|
-
|
|
321
|
+
int interface;
|
|
322
322
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
323
|
+
virtual void Execute() {
|
|
324
|
+
errcode = libusb_release_interface(device->device_handle, interface);
|
|
325
|
+
}
|
|
326
326
|
};
|
|
327
327
|
|
|
328
328
|
Napi::Value Device::ReleaseInterface(const Napi::CallbackInfo& info) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
329
|
+
ENTER_METHOD(Device, 2);
|
|
330
|
+
CHECK_OPEN();
|
|
331
|
+
int interface;
|
|
332
|
+
INT_ARG(interface, 0);
|
|
333
|
+
CALLBACK_ARG(1);
|
|
334
|
+
auto baton = new Device_ReleaseInterface(self, callback);
|
|
335
|
+
baton->interface = interface;
|
|
336
|
+
baton->Queue();
|
|
337
|
+
return env.Undefined();
|
|
338
338
|
}
|
|
339
339
|
|
|
340
340
|
struct Device_SetInterface: Req {
|
|
341
|
-
|
|
341
|
+
Device_SetInterface(Device* d, Napi::Function& callback): Req(d, callback) {}
|
|
342
342
|
|
|
343
|
-
|
|
344
|
-
|
|
343
|
+
int interface;
|
|
344
|
+
int altsetting;
|
|
345
345
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
346
|
+
virtual void Execute() {
|
|
347
|
+
errcode = libusb_set_interface_alt_setting(
|
|
348
|
+
device->device_handle, interface, altsetting);
|
|
349
|
+
}
|
|
350
350
|
};
|
|
351
351
|
|
|
352
352
|
Napi::Value Device::SetInterface(const Napi::CallbackInfo& info) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
353
|
+
ENTER_METHOD(Device, 3);
|
|
354
|
+
CHECK_OPEN();
|
|
355
|
+
int interface, altsetting;
|
|
356
|
+
INT_ARG(interface, 0);
|
|
357
|
+
INT_ARG(altsetting, 1);
|
|
358
|
+
CALLBACK_ARG(2);
|
|
359
|
+
auto baton = new Device_SetInterface(self, callback);
|
|
360
|
+
baton->interface = interface;
|
|
361
|
+
baton->altsetting = altsetting;
|
|
362
|
+
baton->Queue();
|
|
363
|
+
return env.Undefined();
|
|
364
364
|
}
|
|
365
365
|
|
|
366
366
|
struct Device_SetConfiguration: Req {
|
|
367
|
-
|
|
367
|
+
Device_SetConfiguration(Device* d, Napi::Function& callback): Req(d, callback) {}
|
|
368
368
|
|
|
369
|
-
|
|
369
|
+
int desired;
|
|
370
370
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
371
|
+
virtual void Execute() {
|
|
372
|
+
errcode = libusb_set_configuration(
|
|
373
|
+
device->device_handle, desired);
|
|
374
|
+
}
|
|
375
375
|
};
|
|
376
376
|
|
|
377
377
|
Napi::Value Device::SetConfiguration(const Napi::CallbackInfo& info) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
378
|
+
ENTER_METHOD(Device, 2);
|
|
379
|
+
CHECK_OPEN();
|
|
380
|
+
int desired;
|
|
381
|
+
INT_ARG(desired, 0);
|
|
382
|
+
CALLBACK_ARG(1);
|
|
383
|
+
auto baton = new Device_SetConfiguration(self, callback);
|
|
384
|
+
baton->desired = desired;
|
|
385
|
+
baton->Queue();
|
|
386
|
+
return env.Undefined();
|
|
387
387
|
}
|
|
388
388
|
|
|
389
389
|
Napi::Object Device::Init(Napi::Env env, Napi::Object exports) {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
390
|
+
auto func = Device::DefineClass(
|
|
391
|
+
env,
|
|
392
|
+
"Device",
|
|
393
|
+
{
|
|
394
|
+
Device::InstanceMethod("__getParent", &Device::GetParent),
|
|
395
|
+
Device::InstanceMethod("__getConfigDescriptor", &Device::GetConfigDescriptor),
|
|
396
|
+
Device::InstanceMethod("__getAllConfigDescriptors", &Device::GetAllConfigDescriptors),
|
|
397
|
+
Device::InstanceMethod("__open", &Device::Open),
|
|
398
|
+
Device::InstanceMethod("__close", &Device::Close),
|
|
399
|
+
Device::InstanceMethod("__clearHalt", &Device::ClearHalt),
|
|
400
|
+
Device::InstanceMethod("reset", &Device::Reset),
|
|
401
|
+
Device::InstanceMethod("__claimInterface", &Device::ClaimInterface),
|
|
402
|
+
Device::InstanceMethod("__releaseInterface", &Device::ReleaseInterface),
|
|
403
|
+
Device::InstanceMethod("__setInterface", &Device::SetInterface),
|
|
404
|
+
Device::InstanceMethod("__setConfiguration", &Device::SetConfiguration),
|
|
405
|
+
Device::InstanceMethod("__isKernelDriverActive", &Device::IsKernelDriverActive),
|
|
406
|
+
Device::InstanceMethod("__detachKernelDriver", &Device::DetachKernelDriver),
|
|
407
|
+
Device::InstanceMethod("__attachKernelDriver", &Device::AttachKernelDriver),
|
|
408
|
+
});
|
|
409
|
+
exports.Set("Device", func);
|
|
410
|
+
|
|
411
|
+
ModuleData* instanceData = env.GetInstanceData<ModuleData>();
|
|
412
|
+
instanceData->deviceConstructor = Napi::Persistent(func);
|
|
413
|
+
|
|
414
|
+
return exports;
|
|
415
415
|
}
|