usb 2.17.0 → 3.0.0-alpha.2

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.
Files changed (207) hide show
  1. package/LICENSE +18 -4
  2. package/README.md +25 -516
  3. package/dist/index.d.ts +67 -13
  4. package/dist/index.js +333 -64
  5. package/index.d.ts +89 -0
  6. package/index.js +583 -0
  7. package/package.json +53 -33
  8. package/CHANGELOG.md +0 -294
  9. package/binding.gyp +0 -128
  10. package/dist/index.js.map +0 -1
  11. package/dist/usb/bindings.d.ts +0 -266
  12. package/dist/usb/bindings.js +0 -10
  13. package/dist/usb/bindings.js.map +0 -1
  14. package/dist/usb/capability.d.ts +0 -13
  15. package/dist/usb/capability.js +0 -17
  16. package/dist/usb/capability.js.map +0 -1
  17. package/dist/usb/descriptors.d.ts +0 -128
  18. package/dist/usb/descriptors.js +0 -3
  19. package/dist/usb/descriptors.js.map +0 -1
  20. package/dist/usb/device.d.ts +0 -100
  21. package/dist/usb/device.js +0 -297
  22. package/dist/usb/device.js.map +0 -1
  23. package/dist/usb/endpoint.d.ts +0 -94
  24. package/dist/usb/endpoint.js +0 -219
  25. package/dist/usb/endpoint.js.map +0 -1
  26. package/dist/usb/index.d.ts +0 -31
  27. package/dist/usb/index.js +0 -116
  28. package/dist/usb/index.js.map +0 -1
  29. package/dist/usb/interface.d.ts +0 -80
  30. package/dist/usb/interface.js +0 -133
  31. package/dist/usb/interface.js.map +0 -1
  32. package/dist/webusb/index.d.ts +0 -64
  33. package/dist/webusb/index.js +0 -295
  34. package/dist/webusb/index.js.map +0 -1
  35. package/dist/webusb/webusb-device.d.ts +0 -54
  36. package/dist/webusb/webusb-device.js +0 -434
  37. package/dist/webusb/webusb-device.js.map +0 -1
  38. package/libusb/.clang-tidy +0 -34
  39. package/libusb/.codespellrc +0 -3
  40. package/libusb/.private/README.txt +0 -5
  41. package/libusb/.private/appveyor_build.sh +0 -26
  42. package/libusb/.private/bm.sh +0 -54
  43. package/libusb/.private/ci-build.sh +0 -92
  44. package/libusb/.private/ci-container-build.sh +0 -67
  45. package/libusb/.private/post-rewrite.sh +0 -32
  46. package/libusb/.private/pre-commit.sh +0 -52
  47. package/libusb/.private/wbs.txt +0 -43
  48. package/libusb/.travis.yml +0 -58
  49. package/libusb/AUTHORS +0 -231
  50. package/libusb/COPYING +0 -504
  51. package/libusb/ChangeLog +0 -365
  52. package/libusb/HACKING +0 -25
  53. package/libusb/INSTALL_WIN.txt +0 -52
  54. package/libusb/KEYS +0 -123
  55. package/libusb/Makefile.am +0 -50
  56. package/libusb/NEWS +0 -2
  57. package/libusb/PORTING +0 -94
  58. package/libusb/README +0 -29
  59. package/libusb/README.git +0 -41
  60. package/libusb/TODO +0 -2
  61. package/libusb/Xcode/common.xcconfig +0 -92
  62. package/libusb/Xcode/config.h +0 -31
  63. package/libusb/Xcode/debug.xcconfig +0 -32
  64. package/libusb/Xcode/libusb.xcconfig +0 -21
  65. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +0 -1391
  66. package/libusb/Xcode/libusb_debug.xcconfig +0 -21
  67. package/libusb/Xcode/libusb_release.xcconfig +0 -21
  68. package/libusb/Xcode/release.xcconfig +0 -30
  69. package/libusb/android/README +0 -152
  70. package/libusb/android/config.h +0 -55
  71. package/libusb/android/examples/unrooted_android.c +0 -301
  72. package/libusb/android/examples/unrooted_android.h +0 -36
  73. package/libusb/android/jni/Android.mk +0 -23
  74. package/libusb/android/jni/Application.mk +0 -40
  75. package/libusb/android/jni/examples.mk +0 -168
  76. package/libusb/android/jni/libusb.mk +0 -60
  77. package/libusb/android/jni/tests.mk +0 -45
  78. package/libusb/appveyor.yml +0 -108
  79. package/libusb/autogen.sh +0 -10
  80. package/libusb/bootstrap.sh +0 -10
  81. package/libusb/configure.ac +0 -450
  82. package/libusb/doc/Makefile.in +0 -22
  83. package/libusb/doc/doxygen.cfg.in +0 -2571
  84. package/libusb/doc/libusb.png +0 -0
  85. package/libusb/examples/Makefile.am +0 -12
  86. package/libusb/examples/dpfp.c +0 -711
  87. package/libusb/examples/ezusb.c +0 -846
  88. package/libusb/examples/ezusb.h +0 -109
  89. package/libusb/examples/fxload.c +0 -310
  90. package/libusb/examples/hotplugtest.c +0 -147
  91. package/libusb/examples/listdevs.c +0 -73
  92. package/libusb/examples/sam3u_benchmark.c +0 -228
  93. package/libusb/examples/testlibusb.c +0 -312
  94. package/libusb/examples/xusb.c +0 -1254
  95. package/libusb/libusb/Makefile.am +0 -98
  96. package/libusb/libusb/Makefile.am.extra +0 -26
  97. package/libusb/libusb/core.c +0 -2925
  98. package/libusb/libusb/descriptor.c +0 -1558
  99. package/libusb/libusb/hotplug.c +0 -489
  100. package/libusb/libusb/io.c +0 -2865
  101. package/libusb/libusb/libusb-1.0.def +0 -199
  102. package/libusb/libusb/libusb-1.0.rc +0 -53
  103. package/libusb/libusb/libusb.h +0 -2421
  104. package/libusb/libusb/libusbi.h +0 -1535
  105. package/libusb/libusb/os/darwin_usb.c +0 -2977
  106. package/libusb/libusb/os/darwin_usb.h +0 -156
  107. package/libusb/libusb/os/emscripten_webusb.cpp +0 -875
  108. package/libusb/libusb/os/events_posix.c +0 -340
  109. package/libusb/libusb/os/events_posix.h +0 -62
  110. package/libusb/libusb/os/events_windows.c +0 -214
  111. package/libusb/libusb/os/events_windows.h +0 -46
  112. package/libusb/libusb/os/haiku_pollfs.cpp +0 -372
  113. package/libusb/libusb/os/haiku_usb.h +0 -113
  114. package/libusb/libusb/os/haiku_usb_backend.cpp +0 -532
  115. package/libusb/libusb/os/haiku_usb_raw.cpp +0 -231
  116. package/libusb/libusb/os/haiku_usb_raw.h +0 -188
  117. package/libusb/libusb/os/linux_netlink.c +0 -401
  118. package/libusb/libusb/os/linux_udev.c +0 -321
  119. package/libusb/libusb/os/linux_usbfs.c +0 -2829
  120. package/libusb/libusb/os/linux_usbfs.h +0 -221
  121. package/libusb/libusb/os/netbsd_usb.c +0 -617
  122. package/libusb/libusb/os/null_usb.c +0 -111
  123. package/libusb/libusb/os/openbsd_usb.c +0 -700
  124. package/libusb/libusb/os/sunos_usb.c +0 -1619
  125. package/libusb/libusb/os/sunos_usb.h +0 -79
  126. package/libusb/libusb/os/threads_posix.c +0 -126
  127. package/libusb/libusb/os/threads_posix.h +0 -98
  128. package/libusb/libusb/os/threads_windows.c +0 -40
  129. package/libusb/libusb/os/threads_windows.h +0 -113
  130. package/libusb/libusb/os/windows_common.c +0 -923
  131. package/libusb/libusb/os/windows_common.h +0 -424
  132. package/libusb/libusb/os/windows_usbdk.c +0 -724
  133. package/libusb/libusb/os/windows_usbdk.h +0 -106
  134. package/libusb/libusb/os/windows_winusb.c +0 -4766
  135. package/libusb/libusb/os/windows_winusb.h +0 -787
  136. package/libusb/libusb/strerror.c +0 -223
  137. package/libusb/libusb/sync.c +0 -342
  138. package/libusb/libusb/version.h +0 -18
  139. package/libusb/libusb/version_nano.h +0 -1
  140. package/libusb/libusb-1.0.pc.in +0 -11
  141. package/libusb/msvc/Base.props +0 -60
  142. package/libusb/msvc/Configuration.Application.props +0 -7
  143. package/libusb/msvc/Configuration.Base.props +0 -47
  144. package/libusb/msvc/Configuration.DynamicLibrary.props +0 -21
  145. package/libusb/msvc/Configuration.StaticLibrary.props +0 -7
  146. package/libusb/msvc/ProjectConfigurations.Base.props +0 -69
  147. package/libusb/msvc/build_all.ps1 +0 -17
  148. package/libusb/msvc/config.h +0 -58
  149. package/libusb/msvc/dpfp.vcxproj +0 -33
  150. package/libusb/msvc/dpfp_threaded.vcxproj +0 -38
  151. package/libusb/msvc/fxload.vcxproj +0 -46
  152. package/libusb/msvc/getopt/getopt.c +0 -1060
  153. package/libusb/msvc/getopt/getopt.h +0 -180
  154. package/libusb/msvc/getopt/getopt1.c +0 -188
  155. package/libusb/msvc/getopt.vcxproj +0 -33
  156. package/libusb/msvc/hotplugtest.vcxproj +0 -32
  157. package/libusb/msvc/init_context.vcxproj +0 -35
  158. package/libusb/msvc/libusb.sln +0 -542
  159. package/libusb/msvc/libusb_dll.vcxproj +0 -61
  160. package/libusb/msvc/libusb_static.vcxproj +0 -49
  161. package/libusb/msvc/listdevs.vcxproj +0 -32
  162. package/libusb/msvc/sam3u_benchmark.vcxproj +0 -33
  163. package/libusb/msvc/set_option.vcxproj +0 -35
  164. package/libusb/msvc/stress.vcxproj +0 -35
  165. package/libusb/msvc/stress_mt.vcxproj +0 -33
  166. package/libusb/msvc/testlibusb.vcxproj +0 -32
  167. package/libusb/msvc/xusb.vcxproj +0 -38
  168. package/libusb/tests/Makefile.am +0 -40
  169. package/libusb/tests/init_context.c +0 -153
  170. package/libusb/tests/libusb_testlib.h +0 -76
  171. package/libusb/tests/macos.c +0 -130
  172. package/libusb/tests/set_option.c +0 -253
  173. package/libusb/tests/stress.c +0 -172
  174. package/libusb/tests/stress_mt.c +0 -267
  175. package/libusb/tests/testlib.c +0 -184
  176. package/libusb/tests/umockdev.c +0 -1175
  177. package/libusb/tests/webusb-test-shim/index.js +0 -12
  178. package/libusb/tests/webusb-test-shim/package-lock.json +0 -50
  179. package/libusb/tests/webusb-test-shim/package.json +0 -10
  180. package/libusb.gypi +0 -154
  181. package/libusb_config/config.h +0 -1
  182. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  183. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  184. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  185. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  186. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  187. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  188. package/prebuilds/linux-ia32/node.napi.node +0 -0
  189. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  190. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  191. package/prebuilds/win32-arm64/node.napi.node +0 -0
  192. package/prebuilds/win32-ia32/node.napi.node +0 -0
  193. package/prebuilds/win32-x64/node.napi.node +0 -0
  194. package/src/device.cc +0 -439
  195. package/src/helpers.h +0 -64
  196. package/src/hotplug/hotplug.h +0 -22
  197. package/src/hotplug/libusb.cc +0 -90
  198. package/src/hotplug/windows.cc +0 -168
  199. package/src/node_usb.cc +0 -314
  200. package/src/node_usb.h +0 -131
  201. package/src/thread_name.cc +0 -79
  202. package/src/thread_name.h +0 -11
  203. package/src/transfer.cc +0 -143
  204. package/src/uv_async_queue.h +0 -41
  205. package/test/usb.coffee +0 -250
  206. package/test/webusb.coffee +0 -227
  207. package/test/worker.cjs +0 -13
package/src/device.cc DELETED
@@ -1,439 +0,0 @@
1
- #include "node_usb.h"
2
- #include <string.h>
3
-
4
- #define STRUCT_TO_V8(TARGET, STR, NAME) \
5
- TARGET.DefineProperty(Napi::PropertyDescriptor::Value(#NAME, Napi::Number::New(env, (uint32_t) (STR).NAME), CONST_PROP));
6
-
7
- #define CHECK_OPEN() \
8
- if (!self->device_handle){THROW_ERROR("Device is not opened");}
9
-
10
- #define MAX_PORTS 7
11
-
12
- Device::Device(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Device>(info), env(0), device_handle(0), refs_(0), completionQueue(handleCompletion) {
13
- env = info.Env();
14
- device = info[0].As<Napi::External<libusb_device>>().Data();
15
- libusb_ref_device(device);
16
-
17
- std::map<libusb_device*, Device*>& byPtr = env.GetInstanceData<ModuleData>()->byPtr;
18
- byPtr[device] = this;
19
-
20
- DEBUG_LOG("Created device %p", this);
21
- Constructor(info);
22
- }
23
-
24
- Device::~Device() {
25
- DEBUG_LOG("Freed device %p", this);
26
-
27
- ModuleData* instanceData = env.GetInstanceData<ModuleData>();
28
- std::map<libusb_device*, Device*>& byPtr = instanceData->byPtr;
29
-
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
- }
36
-
37
- // Get a V8 instance for a libusb_device: either the existing one from the map,
38
- // or create a new one and add it to the map.
39
- Napi::Object Device::get(Napi::Env env, libusb_device* dev) {
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
- DEBUG_LOG("Found device");
49
- return value;
50
- }
51
- }
52
-
53
- Napi::Object obj = instanceData->deviceConstructor.New({ Napi::External<libusb_device>::New(env, dev) });
54
- return obj;
55
- }
56
-
57
- Napi::Value Device::Constructor(const Napi::CallbackInfo& info) {
58
- Napi::Env env = info.Env();
59
- ENTER_CONSTRUCTOR_POINTER(Device, 1);
60
- auto obj = info.This().As<Napi::Object>();
61
- obj.DefineProperty(Napi::PropertyDescriptor::Value("busNumber", Napi::Number::New(env, libusb_get_bus_number(self->device)), CONST_PROP));
62
- obj.DefineProperty(Napi::PropertyDescriptor::Value("deviceAddress", Napi::Number::New(env, libusb_get_device_address(self->device)), CONST_PROP));
63
-
64
- Napi::Object v8dd = Napi::Object::New(env);
65
- obj.DefineProperty(Napi::PropertyDescriptor::Value("deviceDescriptor", v8dd, CONST_PROP));
66
-
67
- struct libusb_device_descriptor dd;
68
- CHECK_USB(libusb_get_device_descriptor(self->device, &dd));
69
-
70
- STRUCT_TO_V8(v8dd, dd, bLength)
71
- STRUCT_TO_V8(v8dd, dd, bDescriptorType)
72
- STRUCT_TO_V8(v8dd, dd, bcdUSB)
73
- STRUCT_TO_V8(v8dd, dd, bDeviceClass)
74
- STRUCT_TO_V8(v8dd, dd, bDeviceSubClass)
75
- STRUCT_TO_V8(v8dd, dd, bDeviceProtocol)
76
- STRUCT_TO_V8(v8dd, dd, bMaxPacketSize0)
77
- STRUCT_TO_V8(v8dd, dd, idVendor)
78
- STRUCT_TO_V8(v8dd, dd, idProduct)
79
- STRUCT_TO_V8(v8dd, dd, bcdDevice)
80
- STRUCT_TO_V8(v8dd, dd, iManufacturer)
81
- STRUCT_TO_V8(v8dd, dd, iProduct)
82
- STRUCT_TO_V8(v8dd, dd, iSerialNumber)
83
- STRUCT_TO_V8(v8dd, dd, bNumConfigurations)
84
-
85
- uint8_t port_numbers[MAX_PORTS];
86
- int ret = libusb_get_port_numbers(self->device, &port_numbers[0], MAX_PORTS);
87
- if (ret > 0) {
88
- Napi::Array array = Napi::Array::New(env, ret);
89
- for (int i = 0; i < ret; ++ i) {
90
- array.Set(i, Napi::Number::New(env, port_numbers[i]));
91
- }
92
- obj.DefineProperty(Napi::PropertyDescriptor::Value("portNumbers", array, CONST_PROP));
93
- }
94
- return info.This();
95
- }
96
-
97
- Napi::Object Device::cdesc2V8(Napi::Env env, libusb_config_descriptor * cdesc) {
98
- Napi::Object v8cdesc = Napi::Object::New(env);
99
-
100
- STRUCT_TO_V8(v8cdesc, *cdesc, bLength)
101
- STRUCT_TO_V8(v8cdesc, *cdesc, bDescriptorType)
102
- STRUCT_TO_V8(v8cdesc, *cdesc, wTotalLength)
103
- STRUCT_TO_V8(v8cdesc, *cdesc, bNumInterfaces)
104
- STRUCT_TO_V8(v8cdesc, *cdesc, bConfigurationValue)
105
- STRUCT_TO_V8(v8cdesc, *cdesc, iConfiguration)
106
- STRUCT_TO_V8(v8cdesc, *cdesc, bmAttributes)
107
- // Libusb 1.0 typo'd bMaxPower as MaxPower
108
- v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("bMaxPower", Napi::Number::New(env, (uint32_t)cdesc->MaxPower), CONST_PROP));
109
- const auto config_extras = (cdesc->extra && cdesc->extra_length)
110
- ? Napi::Buffer<const char>::Copy(env, (const char*)cdesc->extra, cdesc->extra_length)
111
- : Napi::Buffer<const char>::New(env, 0);
112
-
113
- v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", config_extras, CONST_PROP));
114
-
115
- Napi::Array v8interfaces = Napi::Array::New(env, cdesc->bNumInterfaces);
116
- v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("interfaces", v8interfaces, CONST_PROP));
117
-
118
- for (int idxInterface = 0; idxInterface < cdesc->bNumInterfaces; idxInterface++) {
119
- int numAltSettings = cdesc->interface[idxInterface].num_altsetting;
120
-
121
- Napi::Array v8altsettings = Napi::Array::New(env, numAltSettings);
122
- v8interfaces.Set(idxInterface, v8altsettings);
123
-
124
- for (int idxAltSetting = 0; idxAltSetting < numAltSettings; idxAltSetting++) {
125
- const libusb_interface_descriptor& idesc =
126
- cdesc->interface[idxInterface].altsetting[idxAltSetting];
127
-
128
- Napi::Object v8idesc = Napi::Object::New(env);
129
- v8altsettings.Set(idxAltSetting, v8idesc);
130
-
131
- STRUCT_TO_V8(v8idesc, idesc, bLength)
132
- STRUCT_TO_V8(v8idesc, idesc, bDescriptorType)
133
- STRUCT_TO_V8(v8idesc, idesc, bInterfaceNumber)
134
- STRUCT_TO_V8(v8idesc, idesc, bAlternateSetting)
135
- STRUCT_TO_V8(v8idesc, idesc, bNumEndpoints)
136
- STRUCT_TO_V8(v8idesc, idesc, bInterfaceClass)
137
- STRUCT_TO_V8(v8idesc, idesc, bInterfaceSubClass)
138
- STRUCT_TO_V8(v8idesc, idesc, bInterfaceProtocol)
139
- STRUCT_TO_V8(v8idesc, idesc, iInterface)
140
-
141
- auto interface_extras = (idesc.extra && idesc.extra_length)
142
- ? Napi::Buffer<const char>::Copy(env, (const char*)idesc.extra, idesc.extra_length)
143
- : Napi::Buffer<const char>::New(env, 0);
144
- v8idesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", interface_extras, CONST_PROP));
145
-
146
- Napi::Array v8endpoints = Napi::Array::New(env, idesc.bNumEndpoints);
147
- v8idesc.DefineProperty(Napi::PropertyDescriptor::Value("endpoints", v8endpoints, CONST_PROP));
148
- for (int idxEndpoint = 0; idxEndpoint < idesc.bNumEndpoints; idxEndpoint++){
149
- const libusb_endpoint_descriptor& edesc = idesc.endpoint[idxEndpoint];
150
-
151
- Napi::Object v8edesc = Napi::Object::New(env);
152
- v8endpoints.Set(idxEndpoint, v8edesc);
153
-
154
- STRUCT_TO_V8(v8edesc, edesc, bLength)
155
- STRUCT_TO_V8(v8edesc, edesc, bDescriptorType)
156
- STRUCT_TO_V8(v8edesc, edesc, bEndpointAddress)
157
- STRUCT_TO_V8(v8edesc, edesc, bmAttributes)
158
- STRUCT_TO_V8(v8edesc, edesc, wMaxPacketSize)
159
- STRUCT_TO_V8(v8edesc, edesc, bInterval)
160
- STRUCT_TO_V8(v8edesc, edesc, bRefresh)
161
- STRUCT_TO_V8(v8edesc, edesc, bSynchAddress)
162
- auto endpoint_extras = (edesc.extra && edesc.extra_length)
163
- ? Napi::Buffer<const char>::Copy(env, (const char*)edesc.extra, edesc.extra_length)
164
- : Napi::Buffer<const char>::New(env, 0);
165
- v8edesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", endpoint_extras, CONST_PROP));
166
- }
167
- }
168
- }
169
- return v8cdesc;
170
- }
171
-
172
- Napi::Value Device::GetConfigDescriptor(const Napi::CallbackInfo& info) {
173
- ENTER_METHOD(Device, 0);
174
- libusb_config_descriptor* cdesc;
175
- CHECK_USB(libusb_get_active_config_descriptor(self->device, &cdesc));
176
- Napi::Object v8cdesc = Device::cdesc2V8(env, cdesc);
177
- libusb_free_config_descriptor(cdesc);
178
- return v8cdesc;
179
- }
180
-
181
- Napi::Value Device::GetAllConfigDescriptors(const Napi::CallbackInfo& info) {
182
- ENTER_METHOD(Device, 0);
183
- libusb_config_descriptor * cdesc;
184
- struct libusb_device_descriptor dd;
185
- libusb_get_device_descriptor(self->device, &dd);
186
- Napi::Array v8cdescriptors = Napi::Array::New(env, dd.bNumConfigurations);
187
- for(uint8_t i = 0; i < dd.bNumConfigurations; i++){
188
- // libusb_get_config_descriptor is nonblocking but allocates and those allocates
189
- // may fail
190
- CHECK_USB(libusb_get_config_descriptor(device, i, &cdesc));
191
- v8cdescriptors.Set(i, Device::cdesc2V8(env, cdesc));
192
- libusb_free_config_descriptor(cdesc);
193
- }
194
- return v8cdescriptors;
195
- }
196
-
197
- Napi::Value Device::GetParent(const Napi::CallbackInfo& info) {
198
- ENTER_METHOD(Device, 0);
199
- libusb_device* dev = libusb_get_parent(self->device);
200
- if(dev)
201
- return Device::get(env, dev);
202
- else
203
- return env.Null();
204
- }
205
-
206
- Napi::Value Device::Open(const Napi::CallbackInfo& info) {
207
- ENTER_METHOD(Device, 0);
208
- if (!self->device_handle){
209
- CHECK_USB(libusb_open(self->device, &self->device_handle));
210
- completionQueue.start(info.Env());
211
- }
212
- return env.Undefined();
213
- }
214
-
215
- Napi::Value Device::Close(const Napi::CallbackInfo& info) {
216
- ENTER_METHOD(Device, 0);
217
- if (self->canClose()){
218
- if (self->device_handle){
219
- libusb_close(self->device_handle);
220
- self->device_handle = NULL;
221
- completionQueue.stop();
222
- }
223
- }else{
224
- THROW_ERROR("Can't close device with a pending request");
225
- }
226
- return env.Undefined();
227
- }
228
-
229
- struct Req: Napi::AsyncWorker {
230
- Device* device;
231
- int errcode;
232
-
233
- Req(Device* d, Napi::Function& callback)
234
- : Napi::AsyncWorker(callback), device(d) {
235
- device->ref();
236
- }
237
-
238
- void OnOK() override {
239
- auto env = Env();
240
- Napi::HandleScope scope(env);
241
- device->unref();
242
-
243
- Napi::Value error = env.Undefined();
244
- if (errcode < 0){
245
- error = libusbException(env, errcode).Value();
246
- }
247
- try {
248
- Callback().Call(device->Value(), { error });
249
- }
250
- catch (const Napi::Error& e) {
251
- e.ThrowAsJavaScriptException();
252
- }
253
- }
254
- };
255
-
256
- struct Device_Reset: Req {
257
- Device_Reset(Device* d, Napi::Function& callback): Req(d, callback) {}
258
-
259
- virtual void Execute() {
260
- errcode = libusb_reset_device(device->device_handle);
261
- }
262
- };
263
-
264
- Napi::Value Device::Reset(const Napi::CallbackInfo& info) {
265
- ENTER_METHOD(Device, 1);
266
- CHECK_OPEN();
267
- CALLBACK_ARG(0);
268
- auto baton = new Device_Reset(self, callback);
269
- baton->Queue();
270
- return env.Undefined();
271
- }
272
-
273
- struct Device_Clear_Halt: Req {
274
- Device_Clear_Halt(Device* d, Napi::Function& callback): Req(d, callback) {}
275
-
276
- int endpoint;
277
-
278
- virtual void Execute() {
279
- errcode = libusb_clear_halt(device->device_handle, endpoint);
280
- }
281
- };
282
-
283
- Napi::Value Device::ClearHalt(const Napi::CallbackInfo& info) {
284
- ENTER_METHOD(Device, 2);
285
- int endpoint;
286
- CHECK_OPEN();
287
- INT_ARG(endpoint, 0);
288
- CALLBACK_ARG(1);
289
- auto baton = new Device_Clear_Halt(self, callback);
290
- baton->endpoint = endpoint;
291
- baton->Queue();
292
- return env.Undefined();
293
- }
294
-
295
- Napi::Value Device::IsKernelDriverActive(const Napi::CallbackInfo& info) {
296
- ENTER_METHOD(Device, 1);
297
- CHECK_OPEN();
298
- int interface;
299
- INT_ARG(interface, 0);
300
- int r = libusb_kernel_driver_active(self->device_handle, interface);
301
- CHECK_USB(r);
302
- return Napi::Boolean::New(env, r);
303
- }
304
-
305
- Napi::Value Device::DetachKernelDriver(const Napi::CallbackInfo& info) {
306
- ENTER_METHOD(Device, 1);
307
- CHECK_OPEN();
308
- int interface;
309
- INT_ARG(interface, 0);
310
- CHECK_USB(libusb_detach_kernel_driver(self->device_handle, interface));
311
- return env.Undefined();
312
- }
313
-
314
- Napi::Value Device::AttachKernelDriver(const Napi::CallbackInfo& info) {
315
- ENTER_METHOD(Device, 1);
316
- CHECK_OPEN();
317
- int interface;
318
- INT_ARG(interface, 0);
319
- CHECK_USB(libusb_attach_kernel_driver(self->device_handle, interface));
320
- return env.Undefined();
321
- }
322
-
323
- Napi::Value Device::SetAutoDetachKernelDriver(const Napi::CallbackInfo& info) {
324
- ENTER_METHOD(Device, 1);
325
- CHECK_OPEN();
326
- int enable;
327
- INT_ARG(enable, 0);
328
- CHECK_USB(libusb_set_auto_detach_kernel_driver(self->device_handle, enable));
329
- return env.Undefined();
330
- }
331
-
332
- Napi::Value Device::ClaimInterface(const Napi::CallbackInfo& info) {
333
- ENTER_METHOD(Device, 1);
334
- CHECK_OPEN();
335
- int interface;
336
- INT_ARG(interface, 0);
337
- CHECK_USB(libusb_claim_interface(self->device_handle, interface));
338
- return env.Undefined();
339
- }
340
-
341
- struct Device_ReleaseInterface: Req {
342
- Device_ReleaseInterface(Device* d, Napi::Function& callback): Req(d, callback) {}
343
-
344
- int interface;
345
-
346
- virtual void Execute() {
347
- errcode = libusb_release_interface(device->device_handle, interface);
348
- }
349
- };
350
-
351
- Napi::Value Device::ReleaseInterface(const Napi::CallbackInfo& info) {
352
- ENTER_METHOD(Device, 2);
353
- CHECK_OPEN();
354
- int interface;
355
- INT_ARG(interface, 0);
356
- CALLBACK_ARG(1);
357
- auto baton = new Device_ReleaseInterface(self, callback);
358
- baton->interface = interface;
359
- baton->Queue();
360
- return env.Undefined();
361
- }
362
-
363
- struct Device_SetInterface: Req {
364
- Device_SetInterface(Device* d, Napi::Function& callback): Req(d, callback) {}
365
-
366
- int interface;
367
- int altsetting;
368
-
369
- virtual void Execute() {
370
- errcode = libusb_set_interface_alt_setting(
371
- device->device_handle, interface, altsetting);
372
- }
373
- };
374
-
375
- Napi::Value Device::SetInterface(const Napi::CallbackInfo& info) {
376
- ENTER_METHOD(Device, 3);
377
- CHECK_OPEN();
378
- int interface, altsetting;
379
- INT_ARG(interface, 0);
380
- INT_ARG(altsetting, 1);
381
- CALLBACK_ARG(2);
382
- auto baton = new Device_SetInterface(self, callback);
383
- baton->interface = interface;
384
- baton->altsetting = altsetting;
385
- baton->Queue();
386
- return env.Undefined();
387
- }
388
-
389
- struct Device_SetConfiguration: Req {
390
- Device_SetConfiguration(Device* d, Napi::Function& callback): Req(d, callback) {}
391
-
392
- int desired;
393
-
394
- virtual void Execute() {
395
- errcode = libusb_set_configuration(
396
- device->device_handle, desired);
397
- }
398
- };
399
-
400
- Napi::Value Device::SetConfiguration(const Napi::CallbackInfo& info) {
401
- ENTER_METHOD(Device, 2);
402
- CHECK_OPEN();
403
- int desired;
404
- INT_ARG(desired, 0);
405
- CALLBACK_ARG(1);
406
- auto baton = new Device_SetConfiguration(self, callback);
407
- baton->desired = desired;
408
- baton->Queue();
409
- return env.Undefined();
410
- }
411
-
412
- Napi::Object Device::Init(Napi::Env env, Napi::Object exports) {
413
- auto func = Device::DefineClass(
414
- env,
415
- "Device",
416
- {
417
- Device::InstanceMethod("__getParent", &Device::GetParent),
418
- Device::InstanceMethod("__getConfigDescriptor", &Device::GetConfigDescriptor),
419
- Device::InstanceMethod("__getAllConfigDescriptors", &Device::GetAllConfigDescriptors),
420
- Device::InstanceMethod("__open", &Device::Open),
421
- Device::InstanceMethod("__close", &Device::Close),
422
- Device::InstanceMethod("__clearHalt", &Device::ClearHalt),
423
- Device::InstanceMethod("reset", &Device::Reset),
424
- Device::InstanceMethod("__claimInterface", &Device::ClaimInterface),
425
- Device::InstanceMethod("__releaseInterface", &Device::ReleaseInterface),
426
- Device::InstanceMethod("__setInterface", &Device::SetInterface),
427
- Device::InstanceMethod("__setConfiguration", &Device::SetConfiguration),
428
- Device::InstanceMethod("__isKernelDriverActive", &Device::IsKernelDriverActive),
429
- Device::InstanceMethod("__detachKernelDriver", &Device::DetachKernelDriver),
430
- Device::InstanceMethod("__attachKernelDriver", &Device::AttachKernelDriver),
431
- Device::InstanceMethod("__setAutoDetachKernelDriver", &Device::SetAutoDetachKernelDriver),
432
- });
433
- exports.Set("Device", func);
434
-
435
- ModuleData* instanceData = env.GetInstanceData<ModuleData>();
436
- instanceData->deviceConstructor = Napi::Persistent(func);
437
-
438
- return exports;
439
- }
package/src/helpers.h DELETED
@@ -1,64 +0,0 @@
1
- #include <vector>
2
- #include <napi.h>
3
-
4
- #define THROW_BAD_ARGS(FAIL_MSG) throw Napi::TypeError::New(env, FAIL_MSG);
5
- #define THROW_ERROR(FAIL_MSG) throw Napi::Error::New(env, FAIL_MSG);
6
-
7
- #pragma GCC diagnostic ignored "-Wtype-limits"
8
- #define CHECK_N_ARGS(MIN_ARGS) if ((MIN_ARGS) > 0 && info.Length() < (MIN_ARGS)) { THROW_BAD_ARGS("Expected " #MIN_ARGS " arguments") }
9
-
10
- const napi_property_attributes CONST_PROP = static_cast<napi_property_attributes>(napi_enumerable | napi_configurable);
11
-
12
- #define ENTER_CONSTRUCTOR(MIN_ARGS) \
13
- Napi::HandleScope scope(env); \
14
- if (!info.IsConstructCall()) throw Napi::Error::New(env, "Must be called with `new`!"); \
15
- CHECK_N_ARGS(MIN_ARGS);
16
-
17
- #define ENTER_CONSTRUCTOR_POINTER(CLASS, MIN_ARGS) \
18
- ENTER_CONSTRUCTOR(MIN_ARGS) \
19
- if (!info.Length() || !info[0].IsExternal()){ \
20
- throw Napi::Error::New(env, "This type cannot be created directly!"); \
21
- } \
22
- auto self = this;
23
-
24
- #define ENTER_METHOD(CLASS, MIN_ARGS) \
25
- Napi::Env env = info.Env(); \
26
- Napi::HandleScope scope(env); \
27
- CHECK_N_ARGS(MIN_ARGS); \
28
- auto self = this;
29
-
30
- #define ENTER_ACCESSOR(CLASS) \
31
- Napi::HandleScope scope(env); \
32
- auto self = info.Holder().Unwrap<CLASS>();
33
-
34
- #define UNWRAP_ARG(CLASS, NAME, ARGNO) \
35
- if (!info[ARGNO].IsObject()) \
36
- THROW_BAD_ARGS("Parameter " #NAME " is not an object"); \
37
- auto NAME = Napi::ObjectWrap<CLASS>::Unwrap(info[ARGNO].As<Napi::Object>()); \
38
- if (!NAME) \
39
- THROW_BAD_ARGS("Parameter " #NAME " (" #ARGNO ") is of incorrect type");
40
-
41
- #define STRING_ARG(NAME, N) \
42
- if (info.Length() > N){ \
43
- if (!info[N].IsString()) \
44
- THROW_BAD_ARGS("Parameter " #NAME " (" #N ") should be string"); \
45
- NAME = *String::Utf8Value(info[N].ToString()); \
46
- }
47
-
48
- #define DOUBLE_ARG(NAME, N) \
49
- if (!info[N].IsNumber()) \
50
- THROW_BAD_ARGS("Parameter " #NAME " (" #N ") should be number"); \
51
- NAME = info[N].ToNumber()->Value();
52
-
53
- #define INT_ARG(NAME, N) \
54
- if (!info[N].IsNumber()) \
55
- THROW_BAD_ARGS("Parameter " #NAME " (" #N ") should be number"); \
56
- NAME = info[N].As<Napi::Number>().Int32Value();
57
-
58
- #define BOOL_ARG(NAME, N) \
59
- NAME = false; \
60
- if (info.Length() > N){ \
61
- if (!info[N].IsBoolean()) \
62
- THROW_BAD_ARGS("Parameter " #NAME " (" #N ") should be bool"); \
63
- NAME = info[N].ToBoolean()->Value(); \
64
- }
@@ -1,22 +0,0 @@
1
- #ifndef _USB_HOTPLUG_H
2
- #define _USB_HOTPLUG_H
3
-
4
- #include "../node_usb.h"
5
-
6
- #define HOTPLUG_SUPPORTS_NONE 0
7
- #define HOTPLUG_SUPPORTS_DEVICES 1
8
- #define HOTPLUG_SUPPORTS_IDS 2
9
-
10
- class HotPlugManager {
11
- public:
12
- static std::unique_ptr<HotPlugManager> create();
13
-
14
- virtual int supportedHotplugEvents() = 0;
15
-
16
- virtual void enableHotplug(const Napi::Env& env, ModuleData* instanceData) = 0;
17
- virtual void disableHotplug(const Napi::Env& env, ModuleData* instanceData) = 0;
18
- };
19
-
20
- void handleHotplug(HotPlug* info);
21
-
22
- #endif
@@ -1,90 +0,0 @@
1
- #include "hotplug.h"
2
-
3
- struct HotPlug {
4
- libusb_device* device;
5
- libusb_hotplug_event event;
6
- Napi::ObjectReference* hotplugThis;
7
- };
8
-
9
- int LIBUSB_CALL hotplug_callback(libusb_context* ctx, libusb_device* device, libusb_hotplug_event event, void* user_data) {
10
- libusb_ref_device(device);
11
- ModuleData* instanceData = (ModuleData*)user_data;
12
- instanceData->hotplugQueue.post(new HotPlug {device, event, &instanceData->hotplugThis});
13
- return 0;
14
- }
15
-
16
- class HotPlugManagerLibUsb: public HotPlugManager {
17
- int supportedHotplugEvents() {
18
- int res = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG);
19
-
20
- return res > 0 ? HOTPLUG_SUPPORTS_DEVICES : HOTPLUG_SUPPORTS_NONE;
21
- }
22
-
23
- void enableHotplug(const Napi::Env& env, ModuleData* instanceData) {
24
- libusb_context* usb_context = instanceData->usb_context;
25
- CHECK_USB(libusb_hotplug_register_callback(
26
- usb_context,
27
- (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
28
- (libusb_hotplug_flag)0,
29
- LIBUSB_HOTPLUG_MATCH_ANY,
30
- LIBUSB_HOTPLUG_MATCH_ANY,
31
- LIBUSB_HOTPLUG_MATCH_ANY,
32
- hotplug_callback,
33
- instanceData,
34
- &hotplugHandle
35
- ));
36
- }
37
-
38
- void disableHotplug(const Napi::Env& env, ModuleData* instanceData) {
39
- libusb_context* usb_context = instanceData->usb_context;
40
- libusb_hotplug_deregister_callback(usb_context, hotplugHandle);
41
- }
42
-
43
- libusb_hotplug_callback_handle hotplugHandle;
44
- };
45
-
46
- std::unique_ptr<HotPlugManager> HotPlugManager::create() {
47
- return std::make_unique<HotPlugManagerLibUsb>();
48
- }
49
-
50
- void handleHotplug(HotPlug* info) {
51
- Napi::ObjectReference* hotplugThis = info->hotplugThis;
52
- Napi::Env env = hotplugThis->Env();
53
- Napi::HandleScope scope(env);
54
-
55
- libusb_device* dev = info->device;
56
- libusb_hotplug_event event = info->event;
57
- delete info;
58
-
59
- DEBUG_LOG("HandleHotplug %p %i", dev, event);
60
-
61
- Napi::Object v8dev = Device::get(env, dev);
62
- libusb_unref_device(dev);
63
-
64
- Napi::Object v8VidPid = Napi::Object::New(env);
65
- auto deviceDescriptor = v8dev.Get("deviceDescriptor");
66
- if (deviceDescriptor.IsObject()) {
67
- v8VidPid.Set("idVendor", deviceDescriptor.As<Napi::Object>().Get("idVendor"));
68
- v8VidPid.Set("idProduct", deviceDescriptor.As<Napi::Object>().Get("idProduct"));
69
- }
70
-
71
- Napi::String eventName;
72
- Napi::String changeEventName;
73
- if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
74
- DEBUG_LOG("Device arrived");
75
- eventName = Napi::String::New(env, "attach");
76
- changeEventName = Napi::String::New(env, "attachIds");
77
-
78
- } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
79
- DEBUG_LOG("Device left");
80
- eventName = Napi::String::New(env, "detach");
81
- changeEventName = Napi::String::New(env, "detachIds");
82
-
83
- } else {
84
- DEBUG_LOG("Unhandled hotplug event %d\n", event);
85
- return;
86
- }
87
-
88
- hotplugThis->Get("emit").As<Napi::Function>().MakeCallback(hotplugThis->Value(), { eventName, v8dev });
89
- hotplugThis->Get("emit").As<Napi::Function>().MakeCallback(hotplugThis->Value(), { changeEventName, v8VidPid });
90
- }