usb 1.7.2-prebuild

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 (183) hide show
  1. package/.github/workflows/prebuild.yml +62 -0
  2. package/.gitmodules +3 -0
  3. package/LICENSE +7 -0
  4. package/Readme.md +339 -0
  5. package/binding.gyp +90 -0
  6. package/libusb/.gitattributes +11 -0
  7. package/libusb/.private/README.txt +5 -0
  8. package/libusb/.private/bd.cmd +89 -0
  9. package/libusb/.private/bm.sh +54 -0
  10. package/libusb/.private/bwince.cmd +57 -0
  11. package/libusb/.private/post-rewrite.sh +28 -0
  12. package/libusb/.private/pre-commit.sh +42 -0
  13. package/libusb/.private/wbs.txt +61 -0
  14. package/libusb/.private/wbs_wince.txt +42 -0
  15. package/libusb/AUTHORS +78 -0
  16. package/libusb/COPYING +504 -0
  17. package/libusb/ChangeLog +211 -0
  18. package/libusb/INSTALL +234 -0
  19. package/libusb/INSTALL_WIN.txt +73 -0
  20. package/libusb/Makefile.am +28 -0
  21. package/libusb/NEWS +2 -0
  22. package/libusb/PORTING +94 -0
  23. package/libusb/README +28 -0
  24. package/libusb/README.git +41 -0
  25. package/libusb/TODO +2 -0
  26. package/libusb/Xcode/common.xcconfig +49 -0
  27. package/libusb/Xcode/debug.xcconfig +29 -0
  28. package/libusb/Xcode/libusb.xcconfig +21 -0
  29. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +1 -0
  30. package/libusb/Xcode/libusb_debug.xcconfig +21 -0
  31. package/libusb/Xcode/libusb_release.xcconfig +21 -0
  32. package/libusb/Xcode/release.xcconfig +30 -0
  33. package/libusb/android/README +114 -0
  34. package/libusb/android/jni/Android.mk +23 -0
  35. package/libusb/android/jni/Application.mk +24 -0
  36. package/libusb/android/jni/examples.mk +134 -0
  37. package/libusb/android/jni/libusb.mk +54 -0
  38. package/libusb/android/jni/tests.mk +56 -0
  39. package/libusb/autogen.sh +8 -0
  40. package/libusb/bootstrap.sh +19 -0
  41. package/libusb/configure.ac +304 -0
  42. package/libusb/doc/Makefile.am +9 -0
  43. package/libusb/doc/doxygen.cfg.in +1288 -0
  44. package/libusb/doc/libusb.png +0 -0
  45. package/libusb/examples/Makefile.am +19 -0
  46. package/libusb/examples/dpfp.c +506 -0
  47. package/libusb/examples/dpfp_threaded.c +544 -0
  48. package/libusb/examples/ezusb.c +831 -0
  49. package/libusb/examples/ezusb.h +120 -0
  50. package/libusb/examples/fxload.c +287 -0
  51. package/libusb/examples/getopt/getopt.c +1060 -0
  52. package/libusb/examples/getopt/getopt.h +180 -0
  53. package/libusb/examples/getopt/getopt1.c +188 -0
  54. package/libusb/examples/hotplugtest.c +104 -0
  55. package/libusb/examples/listdevs.c +71 -0
  56. package/libusb/examples/sam3u_benchmark.c +193 -0
  57. package/libusb/examples/xusb.c +1129 -0
  58. package/libusb/libusb/Makefile.am +75 -0
  59. package/libusb/libusb/core.c +2342 -0
  60. package/libusb/libusb/descriptor.c +1199 -0
  61. package/libusb/libusb/hotplug.c +327 -0
  62. package/libusb/libusb/hotplug.h +82 -0
  63. package/libusb/libusb/io.c +2631 -0
  64. package/libusb/libusb/libusb-1.0.def +166 -0
  65. package/libusb/libusb/libusb-1.0.rc +61 -0
  66. package/libusb/libusb/libusb.h +1998 -0
  67. package/libusb/libusb/libusbi.h +1040 -0
  68. package/libusb/libusb/os/darwin_usb.c +2009 -0
  69. package/libusb/libusb/os/darwin_usb.h +162 -0
  70. package/libusb/libusb/os/linux_netlink.c +369 -0
  71. package/libusb/libusb/os/linux_udev.c +307 -0
  72. package/libusb/libusb/os/linux_usbfs.c +2695 -0
  73. package/libusb/libusb/os/linux_usbfs.h +192 -0
  74. package/libusb/libusb/os/netbsd_usb.c +738 -0
  75. package/libusb/libusb/os/openbsd_usb.c +832 -0
  76. package/libusb/libusb/os/poll_posix.c +51 -0
  77. package/libusb/libusb/os/poll_posix.h +11 -0
  78. package/libusb/libusb/os/poll_windows.c +796 -0
  79. package/libusb/libusb/os/poll_windows.h +131 -0
  80. package/libusb/libusb/os/threads_posix.c +82 -0
  81. package/libusb/libusb/os/threads_posix.h +50 -0
  82. package/libusb/libusb/os/threads_windows.c +212 -0
  83. package/libusb/libusb/os/threads_windows.h +87 -0
  84. package/libusb/libusb/os/wince_usb.c +1032 -0
  85. package/libusb/libusb/os/wince_usb.h +131 -0
  86. package/libusb/libusb/os/windows_common.h +108 -0
  87. package/libusb/libusb/os/windows_usb.c +5347 -0
  88. package/libusb/libusb/os/windows_usb.h +971 -0
  89. package/libusb/libusb/strerror.c +199 -0
  90. package/libusb/libusb/sync.c +307 -0
  91. package/libusb/libusb/version.h +18 -0
  92. package/libusb/libusb/version_nano.h +1 -0
  93. package/libusb/libusb-1.0.pc.in +11 -0
  94. package/libusb/msvc/config.h +50 -0
  95. package/libusb/msvc/ddk_build.cmd +175 -0
  96. package/libusb/msvc/errno.h +102 -0
  97. package/libusb/msvc/fxload_2010.vcxproj +170 -0
  98. package/libusb/msvc/fxload_2010.vcxproj.filters +25 -0
  99. package/libusb/msvc/fxload_2012.vcxproj +174 -0
  100. package/libusb/msvc/fxload_2012.vcxproj.filters +25 -0
  101. package/libusb/msvc/fxload_2013.vcxproj +174 -0
  102. package/libusb/msvc/fxload_sources +23 -0
  103. package/libusb/msvc/getopt_2005.vcproj +288 -0
  104. package/libusb/msvc/getopt_2010.vcxproj +131 -0
  105. package/libusb/msvc/getopt_2010.vcxproj.filters +26 -0
  106. package/libusb/msvc/getopt_2012.vcxproj +136 -0
  107. package/libusb/msvc/getopt_2012.vcxproj.filters +26 -0
  108. package/libusb/msvc/getopt_2013.vcxproj +136 -0
  109. package/libusb/msvc/getopt_sources +20 -0
  110. package/libusb/msvc/hotplugtest_2010.vcxproj +163 -0
  111. package/libusb/msvc/hotplugtest_2010.vcxproj.filters +14 -0
  112. package/libusb/msvc/hotplugtest_2012.vcxproj +167 -0
  113. package/libusb/msvc/hotplugtest_2012.vcxproj.filters +14 -0
  114. package/libusb/msvc/hotplugtest_2013.vcxproj +167 -0
  115. package/libusb/msvc/hotplugtest_sources +20 -0
  116. package/libusb/msvc/inttypes.h +295 -0
  117. package/libusb/msvc/libusb.dsw +71 -0
  118. package/libusb/msvc/libusb_2005.sln +95 -0
  119. package/libusb/msvc/libusb_2010.sln +94 -0
  120. package/libusb/msvc/libusb_2012.sln +94 -0
  121. package/libusb/msvc/libusb_2013.sln +100 -0
  122. package/libusb/msvc/libusb_2015.sln +100 -0
  123. package/libusb/msvc/libusb_dll.dsp +194 -0
  124. package/libusb/msvc/libusb_dll_2005.vcproj +436 -0
  125. package/libusb/msvc/libusb_dll_2010.vcxproj +170 -0
  126. package/libusb/msvc/libusb_dll_2010.vcxproj.filters +81 -0
  127. package/libusb/msvc/libusb_dll_2012.vcxproj +175 -0
  128. package/libusb/msvc/libusb_dll_2012.vcxproj.filters +84 -0
  129. package/libusb/msvc/libusb_dll_2013.vcxproj +175 -0
  130. package/libusb/msvc/libusb_dll_wince.vcproj +1243 -0
  131. package/libusb/msvc/libusb_sources +38 -0
  132. package/libusb/msvc/libusb_static.dsp +174 -0
  133. package/libusb/msvc/libusb_static_2005.vcproj +362 -0
  134. package/libusb/msvc/libusb_static_2010.vcxproj +156 -0
  135. package/libusb/msvc/libusb_static_2010.vcxproj.filters +74 -0
  136. package/libusb/msvc/libusb_static_2012.vcxproj +160 -0
  137. package/libusb/msvc/libusb_static_2012.vcxproj.filters +74 -0
  138. package/libusb/msvc/libusb_static_2013.vcxproj +160 -0
  139. package/libusb/msvc/libusb_static_wince.vcproj +1185 -0
  140. package/libusb/msvc/libusb_wince.sln +246 -0
  141. package/libusb/msvc/listdevs.dsp +103 -0
  142. package/libusb/msvc/listdevs_2005.vcproj +360 -0
  143. package/libusb/msvc/listdevs_2010.vcxproj +165 -0
  144. package/libusb/msvc/listdevs_2010.vcxproj.filters +14 -0
  145. package/libusb/msvc/listdevs_2012.vcxproj +169 -0
  146. package/libusb/msvc/listdevs_2012.vcxproj.filters +14 -0
  147. package/libusb/msvc/listdevs_2013.vcxproj +169 -0
  148. package/libusb/msvc/listdevs_sources +19 -0
  149. package/libusb/msvc/listdevs_wince.vcproj +1120 -0
  150. package/libusb/msvc/missing.c +80 -0
  151. package/libusb/msvc/missing.h +32 -0
  152. package/libusb/msvc/stdint.h +256 -0
  153. package/libusb/msvc/stress_2005.vcproj +390 -0
  154. package/libusb/msvc/stress_2010.vcxproj +167 -0
  155. package/libusb/msvc/stress_2010.vcxproj.filters +25 -0
  156. package/libusb/msvc/stress_2012.vcxproj +171 -0
  157. package/libusb/msvc/stress_2012.vcxproj.filters +25 -0
  158. package/libusb/msvc/stress_2013.vcxproj +171 -0
  159. package/libusb/msvc/stress_wince.vcproj +1128 -0
  160. package/libusb/msvc/xusb.dsp +102 -0
  161. package/libusb/msvc/xusb_2005.vcproj +344 -0
  162. package/libusb/msvc/xusb_2010.vcxproj +163 -0
  163. package/libusb/msvc/xusb_2010.vcxproj.filters +14 -0
  164. package/libusb/msvc/xusb_2012.vcxproj +167 -0
  165. package/libusb/msvc/xusb_2012.vcxproj.filters +14 -0
  166. package/libusb/msvc/xusb_2013.vcxproj +167 -0
  167. package/libusb/msvc/xusb_sources +20 -0
  168. package/libusb/msvc/xusb_wince.vcproj +1120 -0
  169. package/libusb/tests/Makefile.am +6 -0
  170. package/libusb/tests/libusb_testlib.h +107 -0
  171. package/libusb/tests/stress.c +160 -0
  172. package/libusb/tests/testlib.c +281 -0
  173. package/libusb.gypi +136 -0
  174. package/libusb_config/config.h +1 -0
  175. package/package.json +69 -0
  176. package/src/device.cc +412 -0
  177. package/src/helpers.h +64 -0
  178. package/src/node_usb.cc +319 -0
  179. package/src/node_usb.h +120 -0
  180. package/src/transfer.cc +148 -0
  181. package/src/uv_async_queue.h +33 -0
  182. package/test/usb.coffee +191 -0
  183. package/usb.js +524 -0
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "usb",
3
+ "description": "Library to access USB devices",
4
+ "version": "1.7.2-prebuild",
5
+ "engines": {
6
+ "node": ">=10"
7
+ },
8
+ "keywords": [
9
+ "usb",
10
+ "hardware"
11
+ ],
12
+ "contributors": [
13
+ {
14
+ "name": "Kevin Mehall",
15
+ "email": "km@kevinmehall.net",
16
+ "url": "http://kevinmehall.net"
17
+ },
18
+ {
19
+ "name": "Tim Ryan",
20
+ "email": "tim@technical.io",
21
+ "url": "http://timryan.org"
22
+ },
23
+ {
24
+ "name": "Rob Moran",
25
+ "email": "github@thegecko.org",
26
+ "url": "https://github.com/thegecko"
27
+ },
28
+ {
29
+ "name": "Christopher Klein"
30
+ }
31
+ ],
32
+ "main": "./usb",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/thegecko/node-usb.git"
36
+ },
37
+ "scripts": {
38
+ "install": "node-gyp-build",
39
+ "test": "mocha --require coffeescript/register --grep Module test/*",
40
+ "full-test": "mocha --require coffeescript/register test/*",
41
+ "valgrind": "coffee -c test/usb.coffee; valgrind --leak-check=full --show-possibly-lost=no node --expose-gc --trace-gc node_modules/mocha/bin/_mocha -R spec",
42
+ "prebuild": "prebuildify --napi --strip",
43
+ "prebuild-darwin": "prebuildify --napi --strip --arch x64+arm64",
44
+ "prebuild-win32-x86": "prebuildify --napi --strip",
45
+ "prebuild-win32-x64": "prebuildify --napi --strip",
46
+ "prebuild-linux-x64": "prebuildify-cross -i ghcr.io/node-usb/centos7-devtoolset7 -i ghcr.io/node-usb/alpine --napi --strip --tag-libc",
47
+ "prebuild-linux-arm": "prebuildify-cross -i ghcr.io/node-usb/linux-arm64 -i ghcr.io/node-usb/linux-armv7 -i ghcr.io/node-usb/linux-armv6 --napi --strip --tag-armv",
48
+ "prebuild-android-arm": "prebuildify-cross -i ghcr.io/node-usb/android-arm64 -i ghcr.io/node-usb/android-armv7 --napi --strip --tag-armv",
49
+ "prebuild-download": "prebuildify-ci download"
50
+ },
51
+ "dependencies": {
52
+ "node-addon-api": "^4.2.0",
53
+ "node-gyp-build": "^4.3.0"
54
+ },
55
+ "devDependencies": {
56
+ "coffeescript": "~2.4.1",
57
+ "mocha": "^8.3.2",
58
+ "node-gyp": "^7.1.2",
59
+ "prebuildify": "^4.2.1",
60
+ "prebuildify-ci": "^1.0.5",
61
+ "prebuildify-cross": "^4.0.1"
62
+ },
63
+ "license": "MIT",
64
+ "binary": {
65
+ "napi_versions": [
66
+ 4
67
+ ]
68
+ }
69
+ }
package/src/device.cc ADDED
@@ -0,0 +1,412 @@
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
+ Napi::FunctionReference Device::constructor;
13
+
14
+ Device::Device(const Napi::CallbackInfo & info) : Napi::ObjectWrap<Device>(info), device_handle(0), refs_(0)
15
+ #ifndef USE_POLL
16
+ , completionQueue(handleCompletion)
17
+ #endif
18
+ {
19
+ device = info[0].As<Napi::External<libusb_device>>().Data();
20
+ libusb_ref_device(device);
21
+ byPtr.insert(std::make_pair(device, this));
22
+ #ifndef USE_POLL
23
+ completionQueue.start(info.Env());
24
+ #endif
25
+ DEBUG_LOG("Created device %p", this);
26
+ Constructor(info);
27
+ }
28
+
29
+ Device::~Device(){
30
+ DEBUG_LOG("Freed device %p", this);
31
+ #ifndef USE_POLL
32
+ completionQueue.stop();
33
+ #endif
34
+ byPtr.erase(device);
35
+ libusb_close(device_handle);
36
+ libusb_unref_device(device);
37
+ }
38
+
39
+ // Map pinning each libusb_device to a particular V8 instance
40
+ std::map<libusb_device*, Device*> Device::byPtr;
41
+
42
+ // Get a V8 instance for a libusb_device: either the existing one from the map,
43
+ // or create a new one and add it to the map.
44
+ Napi::Object Device::get(napi_env env, libusb_device* dev){
45
+ auto it = byPtr.find(dev);
46
+ if (it != byPtr.end()){
47
+ return it->second->Value();
48
+ } else {
49
+ Napi::Object obj = Device::constructor.New({ Napi::External<libusb_device>::New(env, dev) });
50
+ return obj;
51
+ }
52
+ }
53
+
54
+ Napi::Value Device::Constructor(const Napi::CallbackInfo& info) {
55
+ Napi::Env env = info.Env();
56
+ ENTER_CONSTRUCTOR_POINTER(Device, 1);
57
+ auto obj = info.This().As<Napi::Object>();
58
+ obj.DefineProperty(Napi::PropertyDescriptor::Value("busNumber", Napi::Number::New(env, libusb_get_bus_number(self->device)), CONST_PROP));
59
+ obj.DefineProperty(Napi::PropertyDescriptor::Value("deviceAddress", Napi::Number::New(env, libusb_get_device_address(self->device)), CONST_PROP));
60
+
61
+ Napi::Object v8dd = Napi::Object::New(env);
62
+ obj.DefineProperty(Napi::PropertyDescriptor::Value("deviceDescriptor", v8dd, CONST_PROP));
63
+
64
+ struct libusb_device_descriptor dd;
65
+ CHECK_USB(libusb_get_device_descriptor(self->device, &dd));
66
+
67
+ STRUCT_TO_V8(v8dd, dd, bLength)
68
+ STRUCT_TO_V8(v8dd, dd, bDescriptorType)
69
+ STRUCT_TO_V8(v8dd, dd, bcdUSB)
70
+ STRUCT_TO_V8(v8dd, dd, bDeviceClass)
71
+ STRUCT_TO_V8(v8dd, dd, bDeviceSubClass)
72
+ STRUCT_TO_V8(v8dd, dd, bDeviceProtocol)
73
+ STRUCT_TO_V8(v8dd, dd, bMaxPacketSize0)
74
+ STRUCT_TO_V8(v8dd, dd, idVendor)
75
+ STRUCT_TO_V8(v8dd, dd, idProduct)
76
+ STRUCT_TO_V8(v8dd, dd, bcdDevice)
77
+ STRUCT_TO_V8(v8dd, dd, iManufacturer)
78
+ STRUCT_TO_V8(v8dd, dd, iProduct)
79
+ STRUCT_TO_V8(v8dd, dd, iSerialNumber)
80
+ STRUCT_TO_V8(v8dd, dd, bNumConfigurations)
81
+
82
+ uint8_t port_numbers[MAX_PORTS];
83
+ int ret = libusb_get_port_numbers(self->device, &port_numbers[0], MAX_PORTS);
84
+ if (ret > 0) {
85
+ Napi::Array array = Napi::Array::New(env, ret);
86
+ for (int i = 0; i < ret; ++ i) {
87
+ array.Set(i, Napi::Number::New(env, port_numbers[i]));
88
+ }
89
+ obj.DefineProperty(Napi::PropertyDescriptor::Value("portNumbers", array, CONST_PROP));
90
+ }
91
+ return info.This();
92
+ }
93
+
94
+ Napi::Object Device::cdesc2V8(napi_env env, libusb_config_descriptor * cdesc){
95
+ Napi::Object v8cdesc = Napi::Object::New(env);
96
+
97
+ STRUCT_TO_V8(v8cdesc, *cdesc, bLength)
98
+ STRUCT_TO_V8(v8cdesc, *cdesc, bDescriptorType)
99
+ STRUCT_TO_V8(v8cdesc, *cdesc, wTotalLength)
100
+ STRUCT_TO_V8(v8cdesc, *cdesc, bNumInterfaces)
101
+ STRUCT_TO_V8(v8cdesc, *cdesc, bConfigurationValue)
102
+ STRUCT_TO_V8(v8cdesc, *cdesc, iConfiguration)
103
+ STRUCT_TO_V8(v8cdesc, *cdesc, bmAttributes)
104
+ // Libusb 1.0 typo'd bMaxPower as MaxPower
105
+ v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("bMaxPower", Napi::Number::New(env, (uint32_t)cdesc->MaxPower), CONST_PROP));
106
+
107
+ v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", Napi::Buffer<const char>::Copy(env, (const char*)cdesc->extra, cdesc->extra_length), CONST_PROP));
108
+
109
+ Napi::Array v8interfaces = Napi::Array::New(env, cdesc->bNumInterfaces);
110
+ v8cdesc.DefineProperty(Napi::PropertyDescriptor::Value("interfaces", v8interfaces, CONST_PROP));
111
+
112
+ for (int idxInterface = 0; idxInterface < cdesc->bNumInterfaces; idxInterface++) {
113
+ int numAltSettings = cdesc->interface[idxInterface].num_altsetting;
114
+
115
+ Napi::Array v8altsettings = Napi::Array::New(env, numAltSettings);
116
+ v8interfaces.Set(idxInterface, v8altsettings);
117
+
118
+ for (int idxAltSetting = 0; idxAltSetting < numAltSettings; idxAltSetting++) {
119
+ const libusb_interface_descriptor& idesc =
120
+ cdesc->interface[idxInterface].altsetting[idxAltSetting];
121
+
122
+ Napi::Object v8idesc = Napi::Object::New(env);
123
+ v8altsettings.Set(idxAltSetting, v8idesc);
124
+
125
+ STRUCT_TO_V8(v8idesc, idesc, bLength)
126
+ STRUCT_TO_V8(v8idesc, idesc, bDescriptorType)
127
+ STRUCT_TO_V8(v8idesc, idesc, bInterfaceNumber)
128
+ STRUCT_TO_V8(v8idesc, idesc, bAlternateSetting)
129
+ STRUCT_TO_V8(v8idesc, idesc, bNumEndpoints)
130
+ STRUCT_TO_V8(v8idesc, idesc, bInterfaceClass)
131
+ STRUCT_TO_V8(v8idesc, idesc, bInterfaceSubClass)
132
+ STRUCT_TO_V8(v8idesc, idesc, bInterfaceProtocol)
133
+ STRUCT_TO_V8(v8idesc, idesc, iInterface)
134
+
135
+ v8idesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", Napi::Buffer<const char>::Copy(env, (const char*)idesc.extra, idesc.extra_length), CONST_PROP));
136
+
137
+ Napi::Array v8endpoints = Napi::Array::New(env, idesc.bNumEndpoints);
138
+ v8idesc.DefineProperty(Napi::PropertyDescriptor::Value("endpoints", v8endpoints, CONST_PROP));
139
+ for (int idxEndpoint = 0; idxEndpoint < idesc.bNumEndpoints; idxEndpoint++){
140
+ const libusb_endpoint_descriptor& edesc = idesc.endpoint[idxEndpoint];
141
+
142
+ Napi::Object v8edesc = Napi::Object::New(env);
143
+ v8endpoints.Set(idxEndpoint, v8edesc);
144
+
145
+ STRUCT_TO_V8(v8edesc, edesc, bLength)
146
+ STRUCT_TO_V8(v8edesc, edesc, bDescriptorType)
147
+ STRUCT_TO_V8(v8edesc, edesc, bEndpointAddress)
148
+ STRUCT_TO_V8(v8edesc, edesc, bmAttributes)
149
+ STRUCT_TO_V8(v8edesc, edesc, wMaxPacketSize)
150
+ STRUCT_TO_V8(v8edesc, edesc, bInterval)
151
+ STRUCT_TO_V8(v8edesc, edesc, bRefresh)
152
+ STRUCT_TO_V8(v8edesc, edesc, bSynchAddress)
153
+
154
+ v8edesc.DefineProperty(Napi::PropertyDescriptor::Value("extra", Napi::Buffer<const char>::Copy(env, (const char*)edesc.extra, edesc.extra_length), CONST_PROP));
155
+ }
156
+ }
157
+ }
158
+ return v8cdesc;
159
+ }
160
+
161
+ Napi::Value Device::GetConfigDescriptor(const Napi::CallbackInfo& info) {
162
+ ENTER_METHOD(Device, 0);
163
+ libusb_config_descriptor* cdesc;
164
+ CHECK_USB(libusb_get_active_config_descriptor(self->device, &cdesc));
165
+ Napi::Object v8cdesc = Device::cdesc2V8(env, cdesc);
166
+ libusb_free_config_descriptor(cdesc);
167
+ return v8cdesc;
168
+ }
169
+
170
+ Napi::Value Device::GetAllConfigDescriptors(const Napi::CallbackInfo& info){
171
+ ENTER_METHOD(Device, 0);
172
+ libusb_config_descriptor * cdesc;
173
+ struct libusb_device_descriptor dd;
174
+ libusb_get_device_descriptor(self->device, &dd);
175
+ Napi::Array v8cdescriptors = Napi::Array::New(env, dd.bNumConfigurations);
176
+ for(uint8_t i = 0; i < dd.bNumConfigurations; i++){
177
+ libusb_get_config_descriptor(device, i, &cdesc);
178
+ v8cdescriptors.Set(i, Device::cdesc2V8(env, cdesc));
179
+ libusb_free_config_descriptor(cdesc);
180
+ }
181
+ return v8cdescriptors;
182
+ }
183
+
184
+ Napi::Value Device::GetParent(const Napi::CallbackInfo& info){
185
+ ENTER_METHOD(Device, 0);
186
+ libusb_device* dev = libusb_get_parent(self->device);
187
+ if(dev)
188
+ return Device::get(env, dev);
189
+ else
190
+ return env.Null();
191
+ }
192
+
193
+ Napi::Value Device::Open(const Napi::CallbackInfo& info) {
194
+ ENTER_METHOD(Device, 0);
195
+ if (!self->device_handle){
196
+ CHECK_USB(libusb_open(self->device, &self->device_handle));
197
+ }
198
+ return env.Undefined();
199
+ }
200
+
201
+ Napi::Value Device::Close(const Napi::CallbackInfo& info) {
202
+ ENTER_METHOD(Device, 0);
203
+ if (self->canClose()){
204
+ libusb_close(self->device_handle);
205
+ self->device_handle = NULL;
206
+ }else{
207
+ THROW_ERROR("Can't close device with a pending request");
208
+ }
209
+ return env.Undefined();
210
+ }
211
+
212
+ struct Req: Napi::AsyncWorker {
213
+ Device* device;
214
+ int errcode;
215
+
216
+ Req(Device* d, Napi::Function& callback)
217
+ : Napi::AsyncWorker(callback), device(d) {
218
+ device->ref();
219
+ }
220
+
221
+ void OnOK() override {
222
+ auto env = Env();
223
+ Napi::HandleScope scope(env);
224
+ device->unref();
225
+
226
+ Napi::Value error = env.Undefined();
227
+ if (errcode < 0){
228
+ error = libusbException(env, errcode).Value();
229
+ }
230
+ try {
231
+ Callback().Call(device->Value(), { error });
232
+ }
233
+ catch (const Napi::Error& e) {
234
+ Napi::Error::Fatal("", e.what());
235
+ }
236
+ }
237
+ };
238
+
239
+ struct Device_Reset: Req {
240
+ Device_Reset(Device* d, Napi::Function& callback): Req(d, callback) {}
241
+
242
+ virtual void Execute() {
243
+ errcode = libusb_reset_device(device->device_handle);
244
+ }
245
+ };
246
+
247
+ Napi::Value Device::Reset(const Napi::CallbackInfo& info) {
248
+ ENTER_METHOD(Device, 1);
249
+ CHECK_OPEN();
250
+ CALLBACK_ARG(0);
251
+ auto baton = new Device_Reset(self, callback);
252
+ baton->Queue();
253
+ return env.Undefined();
254
+ }
255
+
256
+ struct Device_Clear_Halt: Req {
257
+ Device_Clear_Halt(Device* d, Napi::Function& callback): Req(d, callback) {}
258
+
259
+ int endpoint;
260
+
261
+ virtual void Execute() {
262
+ errcode = libusb_clear_halt(device->device_handle, endpoint);
263
+ }
264
+ };
265
+
266
+ Napi::Value Device::ClearHalt(const Napi::CallbackInfo& info) {
267
+ ENTER_METHOD(Device, 2);
268
+ int endpoint;
269
+ CHECK_OPEN();
270
+ INT_ARG(endpoint, 0);
271
+ CALLBACK_ARG(1);
272
+ auto baton = new Device_Clear_Halt(self, callback);
273
+ baton->endpoint = endpoint;
274
+ baton->Queue();
275
+ return env.Undefined();
276
+ }
277
+
278
+ Napi::Value Device::IsKernelDriverActive(const Napi::CallbackInfo& info) {
279
+ ENTER_METHOD(Device, 1);
280
+ CHECK_OPEN();
281
+ int interface;
282
+ INT_ARG(interface, 0);
283
+ int r = libusb_kernel_driver_active(self->device_handle, interface);
284
+ CHECK_USB(r);
285
+ return Napi::Boolean::New(env, r);
286
+ }
287
+
288
+ Napi::Value Device::DetachKernelDriver(const Napi::CallbackInfo& info) {
289
+ ENTER_METHOD(Device, 1);
290
+ CHECK_OPEN();
291
+ int interface;
292
+ INT_ARG(interface, 0);
293
+ CHECK_USB(libusb_detach_kernel_driver(self->device_handle, interface));
294
+ return env.Undefined();
295
+ }
296
+
297
+ Napi::Value Device::AttachKernelDriver(const Napi::CallbackInfo& info) {
298
+ ENTER_METHOD(Device, 1);
299
+ CHECK_OPEN();
300
+ int interface;
301
+ INT_ARG(interface, 0);
302
+ CHECK_USB(libusb_attach_kernel_driver(self->device_handle, interface));
303
+ return env.Undefined();
304
+ }
305
+
306
+ Napi::Value Device::ClaimInterface(const Napi::CallbackInfo& info) {
307
+ ENTER_METHOD(Device, 1);
308
+ CHECK_OPEN();
309
+ int interface;
310
+ INT_ARG(interface, 0);
311
+ CHECK_USB(libusb_claim_interface(self->device_handle, interface));
312
+ return env.Undefined();
313
+ }
314
+
315
+ struct Device_ReleaseInterface: Req {
316
+ Device_ReleaseInterface(Device* d, Napi::Function& callback): Req(d, callback) {}
317
+
318
+ int interface;
319
+
320
+ virtual void Execute() {
321
+ errcode = libusb_release_interface(device->device_handle, interface);
322
+ }
323
+ };
324
+
325
+ Napi::Value Device::ReleaseInterface(const Napi::CallbackInfo& info) {
326
+ ENTER_METHOD(Device, 2);
327
+ CHECK_OPEN();
328
+ int interface;
329
+ INT_ARG(interface, 0);
330
+ CALLBACK_ARG(1);
331
+ auto baton = new Device_ReleaseInterface(self, callback);
332
+ baton->interface = interface;
333
+ baton->Queue();
334
+ return env.Undefined();
335
+ }
336
+
337
+ struct Device_SetInterface: Req {
338
+ Device_SetInterface(Device* d, Napi::Function& callback): Req(d, callback) {}
339
+
340
+ int interface;
341
+ int altsetting;
342
+
343
+ virtual void Execute() {
344
+ errcode = libusb_set_interface_alt_setting(
345
+ device->device_handle, interface, altsetting);
346
+ }
347
+ };
348
+
349
+ Napi::Value Device::SetInterface(const Napi::CallbackInfo& info) {
350
+ ENTER_METHOD(Device, 3);
351
+ CHECK_OPEN();
352
+ int interface, altsetting;
353
+ INT_ARG(interface, 0);
354
+ INT_ARG(altsetting, 1);
355
+ CALLBACK_ARG(2);
356
+ auto baton = new Device_SetInterface(self, callback);
357
+ baton->interface = interface;
358
+ baton->altsetting = altsetting;
359
+ baton->Queue();
360
+ return env.Undefined();
361
+ }
362
+
363
+ struct Device_SetConfiguration: Req {
364
+ Device_SetConfiguration(Device* d, Napi::Function& callback): Req(d, callback) {}
365
+
366
+ int desired;
367
+
368
+ virtual void Execute() {
369
+ errcode = libusb_set_configuration(
370
+ device->device_handle, desired);
371
+ }
372
+ };
373
+
374
+ Napi::Value Device::SetConfiguration(const Napi::CallbackInfo& info) {
375
+ ENTER_METHOD(Device, 2);
376
+ CHECK_OPEN();
377
+ int desired;
378
+ INT_ARG(desired, 0);
379
+ CALLBACK_ARG(1);
380
+ auto baton = new Device_SetConfiguration(self, callback);
381
+ baton->desired = desired;
382
+ baton->Queue();
383
+ return env.Undefined();
384
+ }
385
+
386
+ Napi::Object Device::Init(Napi::Env env, Napi::Object exports) {
387
+ auto func = Device::DefineClass(
388
+ env,
389
+ "Device",
390
+ {
391
+ Device::InstanceMethod("__getParent", &Device::GetParent),
392
+ Device::InstanceMethod("__getConfigDescriptor", &Device::GetConfigDescriptor),
393
+ Device::InstanceMethod("__getAllConfigDescriptors", &Device::GetAllConfigDescriptors),
394
+ Device::InstanceMethod("__open", &Device::Open),
395
+ Device::InstanceMethod("__close", &Device::Close),
396
+ Device::InstanceMethod("__clearHalt", &Device::ClearHalt),
397
+ Device::InstanceMethod("reset", &Device::Reset),
398
+ Device::InstanceMethod("__claimInterface", &Device::ClaimInterface),
399
+ Device::InstanceMethod("__releaseInterface", &Device::ReleaseInterface),
400
+ Device::InstanceMethod("__setInterface", &Device::SetInterface),
401
+ Device::InstanceMethod("__setConfiguration", &Device::SetConfiguration),
402
+ Device::InstanceMethod("__isKernelDriverActive", &Device::IsKernelDriverActive),
403
+ Device::InstanceMethod("__detachKernelDriver", &Device::DetachKernelDriver),
404
+ Device::InstanceMethod("__attachKernelDriver", &Device::AttachKernelDriver),
405
+ });
406
+ exports.Set("Device", func);
407
+
408
+ Device::constructor = Napi::Persistent(func);
409
+ Device::constructor.SuppressDestruct();
410
+
411
+ return exports;
412
+ }
package/src/helpers.h ADDED
@@ -0,0 +1,64 @@
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
+ }