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
@@ -1,168 +0,0 @@
1
- #include "hotplug.h"
2
-
3
- // Include Windows headers
4
- #include <windows.h>
5
- #include <initguid.h>
6
- #include <Cfgmgr32.h>
7
- #include <usbiodef.h>
8
-
9
- #include <locale>
10
- #include <codecvt>
11
- #include <cwctype>
12
-
13
- #define VID_TAG L"VID_"
14
- #define PID_TAG L"PID_"
15
-
16
- struct HotPlug {
17
- int vid;
18
- int pid;
19
- CM_NOTIFY_ACTION event;
20
- Napi::ObjectReference* hotplugThis;
21
- };
22
-
23
- void extractVidPid(wchar_t *buf, int *vid, int *pid)
24
- {
25
- // Example input: \\?\USB#VID_0FD9&PID_0060#000000000000#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
26
-
27
- *vid = 0;
28
- *pid = 0;
29
-
30
- if (buf == NULL)
31
- {
32
- return;
33
- }
34
-
35
- auto string = std::wstring(buf);
36
- std::transform(string.begin(), string.end(), string.begin(), std::towupper);
37
-
38
- wchar_t* temp = new wchar_t[5];
39
- temp[4] = L'\0';
40
-
41
- const wchar_t *vidStr = wcsstr(string.data(), VID_TAG);
42
- const wchar_t *pidStr = wcsstr(string.data(), PID_TAG);
43
-
44
- if (vidStr != nullptr)
45
- {
46
- memcpy(temp, vidStr + wcslen(VID_TAG), 4 * sizeof(wchar_t));
47
- *vid = wcstol(temp, NULL, 16);
48
- }
49
-
50
- if (pidStr != nullptr)
51
- {
52
- memcpy(temp, pidStr + wcslen(PID_TAG), 4 * sizeof(wchar_t));
53
- *pid = wcstol(temp, NULL, 16);
54
- }
55
- }
56
-
57
- DWORD WINAPI MyCMInterfaceNotification(HCMNOTIFICATION hNotify, PVOID Context, CM_NOTIFY_ACTION Action, PCM_NOTIFY_EVENT_DATA EventData, DWORD EventDataSize)
58
- {
59
- switch (Action)
60
- {
61
- case CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL:
62
- case CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL:
63
- {
64
- ModuleData* instanceData = (ModuleData*)Context;
65
-
66
- int vid = 0;
67
- int pid = 0;
68
- extractVidPid(EventData->u.DeviceInterface.SymbolicLink, &vid, &pid);
69
-
70
- instanceData->hotplugQueue.post(new HotPlug {vid, pid, Action, &instanceData->hotplugThis});
71
- break;
72
- }
73
- default:
74
- break;
75
- }
76
- return ERROR_SUCCESS;
77
- }
78
-
79
- class HotPlugManagerWindows : public HotPlugManager
80
- {
81
- public:
82
- HotPlugManagerWindows()
83
- : hcm(nullptr)
84
- {
85
- cmNotifyFilter = { 0 };
86
- cmNotifyFilter.cbSize = sizeof(cmNotifyFilter);
87
- cmNotifyFilter.Flags = 0;
88
- cmNotifyFilter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE;
89
- cmNotifyFilter.u.DeviceInterface.ClassGuid = GUID_DEVINTERFACE_USB_DEVICE;
90
- }
91
-
92
- int supportedHotplugEvents()
93
- {
94
- return HOTPLUG_SUPPORTS_IDS;
95
- }
96
-
97
- void enableHotplug(const Napi::Env &env, ModuleData *instanceData)
98
- {
99
- if (isRunning)
100
- {
101
- return;
102
- }
103
-
104
- isRunning = true;
105
-
106
- auto res = CM_Register_Notification(&cmNotifyFilter, (PVOID)instanceData, (PCM_NOTIFY_CALLBACK)&MyCMInterfaceNotification, &hcm);
107
- if (res != CR_SUCCESS)
108
- {
109
- isRunning = false;
110
- THROW_ERROR("RegisterNotification failed")
111
- }
112
- }
113
-
114
- void disableHotplug(const Napi::Env &env, ModuleData *instanceData)
115
- {
116
- if (isRunning)
117
- {
118
- isRunning = false;
119
-
120
- if (hcm) {
121
- CM_Unregister_Notification(hcm);
122
- hcm = nullptr;
123
- }
124
- }
125
- }
126
-
127
- private:
128
- std::atomic<bool> isRunning = {false};
129
- HCMNOTIFICATION hcm;
130
- CM_NOTIFY_FILTER cmNotifyFilter;
131
- };
132
-
133
- std::unique_ptr<HotPlugManager> HotPlugManager::create()
134
- {
135
- return std::make_unique<HotPlugManagerWindows>();
136
- }
137
-
138
- void handleHotplug(HotPlug* info) {
139
- Napi::ObjectReference* hotplugThis = info->hotplugThis;
140
- Napi::Env env = hotplugThis->Env();
141
- Napi::HandleScope scope(env);
142
-
143
- int vid = info->vid;
144
- int pid = info->pid;
145
- Napi::Object v8VidPid = Napi::Object::New(env);
146
- v8VidPid.Set("idVendor", Napi::Number::New(env, vid));
147
- v8VidPid.Set("idProduct", Napi::Number::New(env, pid));
148
- CM_NOTIFY_ACTION event = info->event;
149
- delete info;
150
-
151
- DEBUG_LOG("HandleHotplug %i %i %i", vid, pid, event);
152
-
153
- Napi::String eventName;
154
- if (CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL == event) {
155
- DEBUG_LOG("Device arrived");
156
- eventName = Napi::String::New(env, "attachIds");
157
-
158
- } else if (CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL == event) {
159
- DEBUG_LOG("Device left");
160
- eventName = Napi::String::New(env, "detachIds");
161
-
162
- } else {
163
- DEBUG_LOG("Unhandled hotplug event %d\n", event);
164
- return;
165
- }
166
-
167
- hotplugThis->Get("emit").As<Napi::Function>().MakeCallback(hotplugThis->Value(), { eventName, v8VidPid });
168
- }
package/src/node_usb.cc DELETED
@@ -1,314 +0,0 @@
1
- #include "node_usb.h"
2
- #include "thread_name.h"
3
- #include "hotplug/hotplug.h"
4
-
5
- Napi::Value SetDebugLevel(const Napi::CallbackInfo& info);
6
- Napi::Value UseUsbDkBackend(const Napi::CallbackInfo& info);
7
- Napi::Value GetDeviceList(const Napi::CallbackInfo& info);
8
- Napi::Value GetLibusbCapability(const Napi::CallbackInfo& info);
9
- Napi::Value SupportedHotplugEvents(const Napi::CallbackInfo& info);
10
- Napi::Value EnableHotplugEvents(const Napi::CallbackInfo& info);
11
- Napi::Value DisableHotplugEvents(const Napi::CallbackInfo& info);
12
- Napi::Value RefHotplugEvents(const Napi::CallbackInfo& info);
13
- Napi::Value UnrefHotplugEvents(const Napi::CallbackInfo& info);
14
- void initConstants(Napi::Object target);
15
-
16
- void USBThreadFn(ModuleData* instanceData) {
17
- SetThreadName("node-usb events");
18
- libusb_context* usb_context = instanceData->usb_context;
19
-
20
- while(true) {
21
- if (instanceData->handlingEvents == false) {
22
- break;
23
- }
24
- libusb_handle_events(usb_context);
25
- }
26
- }
27
-
28
- ModuleData::ModuleData(libusb_context* usb_context) : usb_context(usb_context), hotplugQueue(handleHotplug) {
29
- handlingEvents = true;
30
- usb_thread = std::thread(USBThreadFn, this);
31
- hotplugManager = HotPlugManager::create();
32
- }
33
-
34
- ModuleData::~ModuleData() {
35
- handlingEvents = false;
36
- libusb_interrupt_event_handler(usb_context);
37
- usb_thread.join();
38
-
39
- if (usb_context != nullptr) {
40
- libusb_exit(usb_context);
41
- usb_context = nullptr;
42
- }
43
- }
44
-
45
- Napi::Object Init(Napi::Env env, Napi::Object exports) {
46
- Napi::HandleScope scope(env);
47
- initConstants(exports);
48
-
49
- // Initialize libusb. On error, halt initialization.
50
- libusb_context* usb_context = nullptr;
51
- int res = libusb_init(&usb_context);
52
-
53
- exports.Set("INIT_ERROR", Napi::Number::New(env, res));
54
- if (res != 0) {
55
- return exports;
56
- }
57
-
58
- env.SetInstanceData(new ModuleData(usb_context));
59
-
60
- Device::Init(env, exports);
61
- Transfer::Init(env, exports);
62
-
63
- exports.Set("setDebugLevel", Napi::Function::New(env, SetDebugLevel));
64
- exports.Set("useUsbDkBackend", Napi::Function::New(env, UseUsbDkBackend));
65
- exports.Set("getDeviceList", Napi::Function::New(env, GetDeviceList));
66
- exports.Set("_getLibusbCapability", Napi::Function::New(env, GetLibusbCapability));
67
- exports.Set("_supportedHotplugEvents", Napi::Function::New(env, SupportedHotplugEvents));
68
- exports.Set("_enableHotplugEvents", Napi::Function::New(env, EnableHotplugEvents));
69
- exports.Set("_disableHotplugEvents", Napi::Function::New(env, DisableHotplugEvents));
70
- exports.Set("refHotplugEvents", Napi::Function::New(env, RefHotplugEvents));
71
- exports.Set("unrefHotplugEvents", Napi::Function::New(env, UnrefHotplugEvents));
72
- return exports;
73
- }
74
-
75
- NODE_API_MODULE(usb_bindings, Init)
76
-
77
- Napi::Value SetDebugLevel(const Napi::CallbackInfo& info) {
78
- Napi::Env env = info.Env();
79
- Napi::HandleScope scope(env);
80
- if (info.Length() != 1 || !info[0].IsNumber() || info[0].As<Napi::Number>().Uint32Value() > 4) {
81
- THROW_BAD_ARGS("Usb::SetDebugLevel argument is invalid. [uint:[0-4]]!")
82
- }
83
-
84
- libusb_context* usb_context = env.GetInstanceData<ModuleData>()->usb_context;
85
- libusb_set_option(usb_context, LIBUSB_OPTION_LOG_LEVEL, info[0].As<Napi::Number>().Int32Value());
86
- return env.Undefined();
87
- }
88
-
89
- Napi::Value UseUsbDkBackend(const Napi::CallbackInfo& info) {
90
- Napi::Env env = info.Env();
91
- Napi::HandleScope scope(env);
92
-
93
- libusb_context* usb_context = env.GetInstanceData<ModuleData>()->usb_context;
94
- libusb_set_option(usb_context, LIBUSB_OPTION_USE_USBDK);
95
- return env.Undefined();
96
- }
97
-
98
- Napi::Value GetDeviceList(const Napi::CallbackInfo& info) {
99
- Napi::Env env = info.Env();
100
- Napi::HandleScope scope(env);
101
- libusb_device** devs;
102
-
103
- libusb_context* usb_context = env.GetInstanceData<ModuleData>()->usb_context;
104
- int cnt = libusb_get_device_list(usb_context, &devs);
105
- CHECK_USB(cnt);
106
-
107
- Napi::Array arr = Napi::Array::New(env, cnt);
108
-
109
- for(int i = 0; i < cnt; i++) {
110
- // DEBUG_LOG("Address %u", libusb_get_device_address(devs[i]));
111
- arr.Set(i, Device::get(env, devs[i]));
112
- }
113
- libusb_free_device_list(devs, true);
114
- return arr;
115
- }
116
-
117
- Napi::Value GetLibusbCapability(const Napi::CallbackInfo& info) {
118
- Napi::Env env = info.Env();
119
-
120
- if (info.Length() != 1 || !info[0].IsNumber()) {
121
- THROW_BAD_ARGS("Usb::GetLibusbCapability argument is invalid!")
122
- }
123
-
124
- int res = libusb_has_capability(info[0].As<Napi::Number>().Int32Value());
125
- return Napi::Number::New(env, res);
126
- }
127
-
128
- Napi::Value SupportedHotplugEvents(const Napi::CallbackInfo& info) {
129
- Napi::Env env = info.Env();
130
- Napi::HandleScope scope(env);
131
- ModuleData* instanceData = env.GetInstanceData<ModuleData>();
132
-
133
- int res = instanceData->hotplugManager->supportedHotplugEvents();
134
- return Napi::Number::New(env, res);
135
- }
136
-
137
- Napi::Value EnableHotplugEvents(const Napi::CallbackInfo& info) {
138
- Napi::Env env = info.Env();
139
- Napi::HandleScope scope(env);
140
- ModuleData* instanceData = env.GetInstanceData<ModuleData>();
141
-
142
- if (!instanceData->hotplugEnabled) {
143
- instanceData->hotplugThis.Reset(info.This().As<Napi::Object>(), 1);
144
-
145
- // Start queue, then enable hotplug events
146
- instanceData->hotplugQueue.start(env);
147
- instanceData->hotplugManager->enableHotplug(env, instanceData);
148
-
149
- instanceData->hotplugEnabled = true;
150
- }
151
- return env.Undefined();
152
- }
153
-
154
- Napi::Value DisableHotplugEvents(const Napi::CallbackInfo& info) {
155
- Napi::Env env = info.Env();
156
- Napi::HandleScope scope(env);
157
- ModuleData* instanceData = env.GetInstanceData<ModuleData>();
158
-
159
- if (instanceData->hotplugEnabled) {
160
-
161
- // Disable events, then stop queue
162
- instanceData->hotplugManager->disableHotplug(env, instanceData);
163
- instanceData->hotplugQueue.stop();
164
-
165
- instanceData->hotplugEnabled = false;
166
- }
167
- return env.Undefined();
168
- }
169
-
170
- Napi::Value RefHotplugEvents(const Napi::CallbackInfo& info) {
171
- Napi::Env env = info.Env();
172
- Napi::HandleScope scope(env);
173
- ModuleData* instanceData = env.GetInstanceData<ModuleData>();
174
-
175
- if (instanceData->hotplugEnabled) {
176
- instanceData->hotplugQueue.ref(env);
177
- }
178
- return env.Undefined();
179
- }
180
-
181
- Napi::Value UnrefHotplugEvents(const Napi::CallbackInfo& info) {
182
- Napi::Env env = info.Env();
183
- Napi::HandleScope scope(env);
184
- ModuleData* instanceData = env.GetInstanceData<ModuleData>();
185
-
186
- if (instanceData->hotplugEnabled) {
187
- instanceData->hotplugQueue.unref(env);
188
- }
189
- return env.Undefined();
190
- }
191
-
192
- #define DEFINE_CONSTANT(OBJ, VALUE) \
193
- OBJ.DefineProperty(Napi::PropertyDescriptor::Value(#VALUE, Napi::Number::New(OBJ.Env(), VALUE), static_cast<napi_property_attributes>(napi_enumerable | napi_configurable)));
194
-
195
- void initConstants(Napi::Object target){
196
- DEFINE_CONSTANT(target, LIBUSB_CLASS_PER_INTERFACE);
197
- DEFINE_CONSTANT(target, LIBUSB_CLASS_AUDIO);
198
- DEFINE_CONSTANT(target, LIBUSB_CLASS_COMM);
199
- DEFINE_CONSTANT(target, LIBUSB_CLASS_HID);
200
- DEFINE_CONSTANT(target, LIBUSB_CLASS_PRINTER);
201
- DEFINE_CONSTANT(target, LIBUSB_CLASS_PTP);
202
- DEFINE_CONSTANT(target, LIBUSB_CLASS_MASS_STORAGE);
203
- DEFINE_CONSTANT(target, LIBUSB_CLASS_HUB);
204
- DEFINE_CONSTANT(target, LIBUSB_CLASS_DATA);
205
- DEFINE_CONSTANT(target, LIBUSB_CLASS_WIRELESS);
206
- DEFINE_CONSTANT(target, LIBUSB_CLASS_APPLICATION);
207
- DEFINE_CONSTANT(target, LIBUSB_CLASS_VENDOR_SPEC);
208
- // libusb_standard_request
209
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_GET_STATUS);
210
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_CLEAR_FEATURE);
211
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_SET_FEATURE);
212
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_SET_ADDRESS );
213
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_GET_DESCRIPTOR);
214
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_SET_DESCRIPTOR);
215
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_GET_CONFIGURATION);
216
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_SET_CONFIGURATION );
217
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_GET_INTERFACE);
218
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_SET_INTERFACE);
219
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_SYNCH_FRAME);
220
- // libusb_descriptor_type
221
- DEFINE_CONSTANT(target, LIBUSB_DT_DEVICE);
222
- DEFINE_CONSTANT(target, LIBUSB_DT_CONFIG);
223
- DEFINE_CONSTANT(target, LIBUSB_DT_STRING);
224
- DEFINE_CONSTANT(target, LIBUSB_DT_BOS);
225
- DEFINE_CONSTANT(target, LIBUSB_DT_INTERFACE);
226
- DEFINE_CONSTANT(target, LIBUSB_DT_ENDPOINT);
227
- DEFINE_CONSTANT(target, LIBUSB_DT_HID);
228
- DEFINE_CONSTANT(target, LIBUSB_DT_REPORT);
229
- DEFINE_CONSTANT(target, LIBUSB_DT_PHYSICAL);
230
- DEFINE_CONSTANT(target, LIBUSB_DT_HUB);
231
- // libusb_endpoint_direction
232
- DEFINE_CONSTANT(target, LIBUSB_ENDPOINT_IN);
233
- DEFINE_CONSTANT(target, LIBUSB_ENDPOINT_OUT);
234
- // libusb_transfer_type
235
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_TYPE_CONTROL);
236
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_TYPE_ISOCHRONOUS);
237
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_TYPE_BULK);
238
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_TYPE_INTERRUPT);
239
- // libusb_iso_sync_type
240
- DEFINE_CONSTANT(target, LIBUSB_ISO_SYNC_TYPE_NONE);
241
- DEFINE_CONSTANT(target, LIBUSB_ISO_SYNC_TYPE_ASYNC);
242
- DEFINE_CONSTANT(target, LIBUSB_ISO_SYNC_TYPE_ADAPTIVE);
243
- DEFINE_CONSTANT(target, LIBUSB_ISO_SYNC_TYPE_SYNC);
244
- // libusb_iso_usage_type
245
- DEFINE_CONSTANT(target, LIBUSB_ISO_USAGE_TYPE_DATA);
246
- DEFINE_CONSTANT(target, LIBUSB_ISO_USAGE_TYPE_FEEDBACK);
247
- DEFINE_CONSTANT(target, LIBUSB_ISO_USAGE_TYPE_IMPLICIT);
248
- // libusb_transfer_status
249
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_COMPLETED);
250
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_ERROR);
251
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_TIMED_OUT);
252
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_CANCELLED);
253
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_STALL);
254
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_NO_DEVICE);
255
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_OVERFLOW);
256
- // libusb_transfer_flags
257
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_SHORT_NOT_OK);
258
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_FREE_BUFFER);
259
- DEFINE_CONSTANT(target, LIBUSB_TRANSFER_FREE_TRANSFER);
260
- // libusb_request_type
261
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_TYPE_STANDARD);
262
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_TYPE_CLASS);
263
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_TYPE_VENDOR);
264
- DEFINE_CONSTANT(target, LIBUSB_REQUEST_TYPE_RESERVED);
265
- // libusb_request_recipient
266
- DEFINE_CONSTANT(target, LIBUSB_RECIPIENT_DEVICE);
267
- DEFINE_CONSTANT(target, LIBUSB_RECIPIENT_INTERFACE);
268
- DEFINE_CONSTANT(target, LIBUSB_RECIPIENT_ENDPOINT);
269
- DEFINE_CONSTANT(target, LIBUSB_RECIPIENT_OTHER);
270
-
271
- DEFINE_CONSTANT(target, LIBUSB_CONTROL_SETUP_SIZE);
272
- DEFINE_CONSTANT(target, LIBUSB_DT_BOS_SIZE);
273
-
274
- // libusb_capability
275
- DEFINE_CONSTANT(target, LIBUSB_CAP_HAS_CAPABILITY);
276
- DEFINE_CONSTANT(target, LIBUSB_CAP_HAS_HOTPLUG);
277
- DEFINE_CONSTANT(target, LIBUSB_CAP_HAS_HID_ACCESS);
278
- DEFINE_CONSTANT(target, LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
279
-
280
- // libusb_error
281
- // Input/output error
282
- DEFINE_CONSTANT(target, LIBUSB_ERROR_IO);
283
- // Invalid parameter
284
- DEFINE_CONSTANT(target, LIBUSB_ERROR_INVALID_PARAM);
285
- // Access denied (insufficient permissions)
286
- DEFINE_CONSTANT(target, LIBUSB_ERROR_ACCESS);
287
- // No such device (it may have been disconnected)
288
- DEFINE_CONSTANT(target, LIBUSB_ERROR_NO_DEVICE);
289
- // Entity not found
290
- DEFINE_CONSTANT(target, LIBUSB_ERROR_NOT_FOUND);
291
- // Resource busy
292
- DEFINE_CONSTANT(target, LIBUSB_ERROR_BUSY);
293
- // Operation timed out
294
- DEFINE_CONSTANT(target, LIBUSB_ERROR_TIMEOUT);
295
- // Overflow
296
- DEFINE_CONSTANT(target, LIBUSB_ERROR_OVERFLOW);
297
- // Pipe error
298
- DEFINE_CONSTANT(target, LIBUSB_ERROR_PIPE);
299
- // System call interrupted (perhaps due to signal)
300
- DEFINE_CONSTANT(target, LIBUSB_ERROR_INTERRUPTED);
301
- // Insufficient memory
302
- DEFINE_CONSTANT(target, LIBUSB_ERROR_NO_MEM);
303
- // Operation not supported or unimplemented on this platform
304
- DEFINE_CONSTANT(target, LIBUSB_ERROR_NOT_SUPPORTED);
305
- // Other error
306
- DEFINE_CONSTANT(target, LIBUSB_ERROR_OTHER);
307
- }
308
-
309
- Napi::Error libusbException(Napi::Env env, int errorno) {
310
- const char* err = libusb_error_name(errorno);
311
- Napi::Error e = Napi::Error::New(env, err);
312
- e.Set("errno", (double)errorno);
313
- return e;
314
- }
package/src/node_usb.h DELETED
@@ -1,131 +0,0 @@
1
- #ifndef SRC_NODE_USB_H
2
- #define SRC_NODE_USB_H
3
-
4
- #include <assert.h>
5
- #include <string>
6
- #include <map>
7
-
8
- #ifdef _WIN32
9
- #include <WinSock2.h>
10
- #endif
11
-
12
- #include <thread>
13
- #include <atomic>
14
- #include <libusb.h>
15
- #include <napi.h>
16
-
17
- #include "helpers.h"
18
- #include "uv_async_queue.h"
19
-
20
- struct Transfer;
21
-
22
- struct HotPlug;
23
- class HotPlugManager;
24
-
25
- Napi::Error libusbException(Napi::Env env, int errorno);
26
- void handleCompletion(Transfer* self);
27
-
28
- struct Device: public Napi::ObjectWrap<Device> {
29
- Napi::Env env;
30
- libusb_device* device;
31
- libusb_device_handle* device_handle;
32
-
33
- int refs_;
34
- UVQueue<Transfer*> completionQueue;
35
-
36
- static Napi::Object Init(Napi::Env env, Napi::Object exports);
37
- static Napi::Object get(Napi::Env env, libusb_device* handle);
38
-
39
- inline void ref(){ refs_ = Ref();}
40
- inline void unref(){ refs_ = Unref();}
41
- inline bool canClose(){return refs_ == 0;}
42
-
43
- Device(const Napi::CallbackInfo& info);
44
- ~Device();
45
-
46
- static Napi::Object cdesc2V8(Napi::Env env, libusb_config_descriptor * cdesc);
47
-
48
- Napi::Value GetConfigDescriptor(const Napi::CallbackInfo& info);
49
- Napi::Value GetAllConfigDescriptors(const Napi::CallbackInfo& info);
50
- Napi::Value SetConfiguration(const Napi::CallbackInfo& info);
51
-
52
- Napi::Value GetParent(const Napi::CallbackInfo& info);
53
- Napi::Value Open(const Napi::CallbackInfo& info);
54
- Napi::Value Reset(const Napi::CallbackInfo& info);
55
- Napi::Value Close(const Napi::CallbackInfo& info);
56
-
57
- Napi::Value IsKernelDriverActive(const Napi::CallbackInfo& info);
58
- Napi::Value DetachKernelDriver(const Napi::CallbackInfo& info);
59
- Napi::Value AttachKernelDriver(const Napi::CallbackInfo& info);
60
- Napi::Value SetAutoDetachKernelDriver(const Napi::CallbackInfo& info);
61
-
62
- Napi::Value ClaimInterface(const Napi::CallbackInfo& info);
63
- Napi::Value SetInterface(const Napi::CallbackInfo& info);
64
- Napi::Value ReleaseInterface(const Napi::CallbackInfo& info);
65
-
66
- Napi::Value ClearHalt(const Napi::CallbackInfo& info);
67
- protected:
68
- Napi::Value Constructor(const Napi::CallbackInfo& info);
69
- };
70
-
71
- struct ModuleData {
72
- libusb_context* usb_context;
73
- std::thread usb_thread;
74
- std::atomic<bool> handlingEvents;
75
-
76
- bool hotplugEnabled = 0;
77
- std::unique_ptr<HotPlugManager> hotplugManager;
78
- UVQueue<HotPlug*> hotplugQueue;
79
- Napi::ObjectReference hotplugThis;
80
- std::map<libusb_device*, Device*> byPtr;
81
- Napi::FunctionReference deviceConstructor;
82
-
83
- ModuleData(libusb_context* usb_context);
84
- ~ModuleData();
85
- };
86
-
87
- struct Transfer: public Napi::ObjectWrap<Transfer> {
88
- libusb_transfer* transfer;
89
- Device* device;
90
- Napi::ObjectReference v8buffer;
91
- Napi::FunctionReference v8callback;
92
-
93
- static Napi::Object Init(Napi::Env env, Napi::Object exports);
94
-
95
- inline void ref(){Ref();}
96
- inline void unref(){Unref();}
97
-
98
- Transfer(const Napi::CallbackInfo& info);
99
- ~Transfer();
100
-
101
- Napi::Value Submit(const Napi::CallbackInfo& info);
102
- Napi::Value Cancel(const Napi::CallbackInfo& info);
103
- private:
104
- Napi::Value Constructor(const Napi::CallbackInfo& info);
105
- };
106
-
107
-
108
-
109
- #define CHECK_USB(r) \
110
- if (r < LIBUSB_SUCCESS) { \
111
- throw libusbException(env, r); \
112
- }
113
-
114
- #define CALLBACK_ARG(CALLBACK_ARG_IDX) \
115
- Napi::Function callback; \
116
- if (info.Length() > (CALLBACK_ARG_IDX)) { \
117
- if (!info[CALLBACK_ARG_IDX].IsFunction()) { \
118
- throw Napi::TypeError::New(env, "Argument " #CALLBACK_ARG_IDX " must be a function"); \
119
- } \
120
- callback = info[CALLBACK_ARG_IDX].As<Napi::Function>(); \
121
- } \
122
-
123
- #ifdef DEBUG
124
- #define DEBUG_HEADER fprintf(stderr, "node-usb [%s:%s() %d]: ", __FILE__, __FUNCTION__, __LINE__);
125
- #define DEBUG_FOOTER fprintf(stderr, "\n");
126
- #define DEBUG_LOG(...) DEBUG_HEADER fprintf(stderr, __VA_ARGS__); DEBUG_FOOTER
127
- #else
128
- #define DEBUG_LOG(...)
129
- #endif
130
-
131
- #endif
@@ -1,79 +0,0 @@
1
- // Definition of a SetThreadName function with an emphasis in "portability",
2
- // meaning we prefer cautiously failing over creating build errors (or crashes)
3
- // on exotic platforms. To do this, we only include/link against very
4
- // few & widely available symbols.
5
- #include "thread_name.h"
6
-
7
- #if defined(__linux__)
8
-
9
- // For Linux use the prctl API directly. prctl symbol
10
- // should be available under any libc.
11
- #include <sys/prctl.h>
12
-
13
- // Define here to avoid relying on kernel headers being present
14
- #define PR_SET_NAME 15 /* Set process name */
15
-
16
- bool SetThreadName(const char* name) {
17
- return prctl(PR_SET_NAME, name, 0, 0, 0) >= 0;
18
- }
19
-
20
- #elif defined(__APPLE__)
21
-
22
- // For MacOS use the dynamic linker because I don't
23
- // want to take any risks
24
- #include <dlfcn.h>
25
-
26
- extern "C" typedef int (*SetNameFn)(const char*);
27
-
28
- bool SetThreadName(const char* name) {
29
- auto pthread_setname_np = reinterpret_cast<SetNameFn>(
30
- dlsym(RTLD_DEFAULT, "pthread_setname_np"));
31
- if (pthread_setname_np == nullptr)
32
- return false;
33
- return pthread_setname_np(name) == 0;
34
- }
35
-
36
- #elif defined(_WIN32)
37
-
38
- // For Windows, we use the new SetThreadDescription API which
39
- // is only available in newish versions. To avoid taking any
40
- // risks (and because on certain versions it's the only
41
- // option to access the API), we use the dynamic linker
42
- #include <windows.h>
43
-
44
- extern "C" typedef HRESULT (WINAPI *SetThreadDescriptionFn)(HANDLE, PCWSTR);
45
-
46
- static SetThreadDescriptionFn RetrieveSymbol(const char* objectName) {
47
- auto mod = GetModuleHandleA(objectName);
48
- if (mod == nullptr) return nullptr;
49
- auto symbol = GetProcAddress(mod, "SetThreadDescription");
50
- return reinterpret_cast<SetThreadDescriptionFn>(symbol);
51
- }
52
-
53
- #include <locale>
54
- #include <codecvt>
55
- #include <string>
56
-
57
- bool SetThreadName(const char* name) {
58
- auto SetThreadDescription = RetrieveSymbol("Kernel32.dll");
59
- // apparently, MSDN is wrong and the symbol is defined in
60
- // KernelBase.dll, so try that too
61
- if (SetThreadDescription == nullptr)
62
- SetThreadDescription = RetrieveSymbol("KernelBase.dll");
63
-
64
- if (SetThreadDescription == nullptr) return false;
65
-
66
- std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
67
- std::wstring wide_name = converter.from_bytes(name);
68
-
69
- auto result = SetThreadDescription(GetCurrentThread(), wide_name.c_str());
70
- return SUCCEEDED(result);
71
- }
72
-
73
- #else
74
-
75
- bool SetThreadName(const char* name) {
76
- return false;
77
- }
78
-
79
- #endif
package/src/thread_name.h DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * Sets the name of the calling thread to `name`, which is
3
- * a NUL terminated UTF-8 string. Returns true if success,
4
- * false if error or unsupported platform.
5
- *
6
- * Since this is an OS-dependent operation, the requirements
7
- * of `name` may vary depending on platform. For example on
8
- * Linux, the maximum length (excluding the terminator) is
9
- * 16 bytes, and UTF-8 isn't strictly required (only conventional).
10
- */
11
- bool SetThreadName(const char* name);