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,340 +0,0 @@
1
- /*
2
- * libusb event abstraction on POSIX platforms
3
- *
4
- * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
5
- *
6
- * This library is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU Lesser General Public
8
- * License as published by the Free Software Foundation; either
9
- * version 2.1 of the License, or (at your option) any later version.
10
- *
11
- * This library is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- * Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public
17
- * License along with this library; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
- */
20
-
21
- #include "libusbi.h"
22
-
23
- #include <errno.h>
24
- #include <fcntl.h>
25
- #ifdef HAVE_EVENTFD
26
- #include <sys/eventfd.h>
27
- #endif
28
- #ifdef HAVE_TIMERFD
29
- #include <sys/timerfd.h>
30
- #endif
31
-
32
- #ifdef __EMSCRIPTEN__
33
- /* On Emscripten `pipe` does not conform to the spec and does not block
34
- * until events are available, which makes it unusable for event system
35
- * and often results in deadlocks when `pipe` is in a loop like it is
36
- * in libusb.
37
- *
38
- * Therefore use a custom event system based on browser event emitters. */
39
- #include <emscripten.h>
40
- #include <emscripten/atomic.h>
41
- #include <emscripten/threading.h>
42
-
43
- EM_ASYNC_JS(void, em_libusb_wait_async, (const _Atomic int* ptr, int expected_value, int timeout), {
44
- await Atomics.waitAsync(HEAP32, ptr >> 2, expected_value, timeout).value;
45
- });
46
-
47
- static void em_libusb_wait(const _Atomic int *ptr, int expected_value, int timeout)
48
- {
49
- if (emscripten_is_main_runtime_thread()) {
50
- em_libusb_wait_async(ptr, expected_value, timeout);
51
- } else {
52
- emscripten_atomic_wait_u32((int*)ptr, expected_value, 1000000LL * timeout);
53
- }
54
- }
55
- #endif
56
- #include <unistd.h>
57
-
58
- #ifdef HAVE_EVENTFD
59
- #define EVENT_READ_FD(e) ((e)->eventfd)
60
- #define EVENT_WRITE_FD(e) ((e)->eventfd)
61
- #else
62
- #define EVENT_READ_FD(e) ((e)->pipefd[0])
63
- #define EVENT_WRITE_FD(e) ((e)->pipefd[1])
64
- #endif
65
-
66
- #ifdef HAVE_NFDS_T
67
- typedef nfds_t usbi_nfds_t;
68
- #else
69
- typedef unsigned int usbi_nfds_t;
70
- #endif
71
-
72
- int usbi_create_event(usbi_event_t *event)
73
- {
74
- #ifdef HAVE_EVENTFD
75
- event->eventfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
76
- if (event->eventfd == -1) {
77
- usbi_err(NULL, "failed to create eventfd, errno=%d", errno);
78
- return LIBUSB_ERROR_OTHER;
79
- }
80
-
81
- return 0;
82
- #else
83
- #if defined(HAVE_PIPE2)
84
- int ret = pipe2(event->pipefd, O_CLOEXEC);
85
- #else
86
- int ret = pipe(event->pipefd);
87
- #endif
88
-
89
- if (ret != 0) {
90
- usbi_err(NULL, "failed to create pipe, errno=%d", errno);
91
- return LIBUSB_ERROR_OTHER;
92
- }
93
-
94
- #if !defined(HAVE_PIPE2) && defined(FD_CLOEXEC)
95
- ret = fcntl(event->pipefd[0], F_GETFD);
96
- if (ret == -1) {
97
- usbi_err(NULL, "failed to get pipe fd flags, errno=%d", errno);
98
- goto err_close_pipe;
99
- }
100
- ret = fcntl(event->pipefd[0], F_SETFD, ret | FD_CLOEXEC);
101
- if (ret == -1) {
102
- usbi_err(NULL, "failed to set pipe fd flags, errno=%d", errno);
103
- goto err_close_pipe;
104
- }
105
-
106
- ret = fcntl(event->pipefd[1], F_GETFD);
107
- if (ret == -1) {
108
- usbi_err(NULL, "failed to get pipe fd flags, errno=%d", errno);
109
- goto err_close_pipe;
110
- }
111
- ret = fcntl(event->pipefd[1], F_SETFD, ret | FD_CLOEXEC);
112
- if (ret == -1) {
113
- usbi_err(NULL, "failed to set pipe fd flags, errno=%d", errno);
114
- goto err_close_pipe;
115
- }
116
- #endif
117
-
118
- ret = fcntl(event->pipefd[1], F_GETFL);
119
- if (ret == -1) {
120
- usbi_err(NULL, "failed to get pipe fd status flags, errno=%d", errno);
121
- goto err_close_pipe;
122
- }
123
- ret = fcntl(event->pipefd[1], F_SETFL, ret | O_NONBLOCK);
124
- if (ret == -1) {
125
- usbi_err(NULL, "failed to set pipe fd status flags, errno=%d", errno);
126
- goto err_close_pipe;
127
- }
128
-
129
- return 0;
130
-
131
- err_close_pipe:
132
- close(event->pipefd[1]);
133
- close(event->pipefd[0]);
134
- return LIBUSB_ERROR_OTHER;
135
- #endif
136
- }
137
-
138
- void usbi_destroy_event(usbi_event_t *event)
139
- {
140
- #ifdef HAVE_EVENTFD
141
- if (close(event->eventfd) == -1)
142
- usbi_warn(NULL, "failed to close eventfd, errno=%d", errno);
143
- #else
144
- if (close(event->pipefd[1]) == -1)
145
- usbi_warn(NULL, "failed to close pipe write end, errno=%d", errno);
146
- if (close(event->pipefd[0]) == -1)
147
- usbi_warn(NULL, "failed to close pipe read end, errno=%d", errno);
148
- #endif
149
- }
150
-
151
- void usbi_signal_event(usbi_event_t *event)
152
- {
153
- uint64_t dummy = 1;
154
- ssize_t r;
155
-
156
- r = write(EVENT_WRITE_FD(event), &dummy, sizeof(dummy));
157
- if (r != sizeof(dummy))
158
- usbi_warn(NULL, "event write failed");
159
- #ifdef __EMSCRIPTEN__
160
- event->has_event = 1;
161
- emscripten_atomic_notify(&event->has_event, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
162
- #endif
163
- }
164
-
165
- void usbi_clear_event(usbi_event_t *event)
166
- {
167
- uint64_t dummy;
168
- ssize_t r;
169
-
170
- r = read(EVENT_READ_FD(event), &dummy, sizeof(dummy));
171
- if (r != sizeof(dummy))
172
- usbi_warn(NULL, "event read failed");
173
- #ifdef __EMSCRIPTEN__
174
- event->has_event = 0;
175
- #endif
176
- }
177
-
178
- #ifdef HAVE_TIMERFD
179
- int usbi_create_timer(usbi_timer_t *timer)
180
- {
181
- timer->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
182
- if (timer->timerfd == -1) {
183
- usbi_warn(NULL, "failed to create timerfd, errno=%d", errno);
184
- return LIBUSB_ERROR_OTHER;
185
- }
186
-
187
- return 0;
188
- }
189
-
190
- void usbi_destroy_timer(usbi_timer_t *timer)
191
- {
192
- if (close(timer->timerfd) == -1)
193
- usbi_warn(NULL, "failed to close timerfd, errno=%d", errno);
194
- }
195
-
196
- int usbi_arm_timer(usbi_timer_t *timer, const struct timespec *timeout)
197
- {
198
- const struct itimerspec it = { { 0, 0 }, { timeout->tv_sec, timeout->tv_nsec } };
199
-
200
- if (timerfd_settime(timer->timerfd, TFD_TIMER_ABSTIME, &it, NULL) == -1) {
201
- usbi_warn(NULL, "failed to arm timerfd, errno=%d", errno);
202
- return LIBUSB_ERROR_OTHER;
203
- }
204
-
205
- return 0;
206
- }
207
-
208
- int usbi_disarm_timer(usbi_timer_t *timer)
209
- {
210
- const struct itimerspec it = { { 0, 0 }, { 0, 0 } };
211
-
212
- if (timerfd_settime(timer->timerfd, 0, &it, NULL) == -1) {
213
- usbi_warn(NULL, "failed to disarm timerfd, errno=%d", errno);
214
- return LIBUSB_ERROR_OTHER;
215
- }
216
-
217
- return 0;
218
- }
219
- #endif
220
-
221
- int usbi_alloc_event_data(struct libusb_context *ctx)
222
- {
223
- struct usbi_event_source *ievent_source;
224
- struct pollfd *fds;
225
- size_t i = 0;
226
-
227
- if (ctx->event_data) {
228
- free(ctx->event_data);
229
- ctx->event_data = NULL;
230
- }
231
-
232
- ctx->event_data_cnt = 0;
233
- for_each_event_source(ctx, ievent_source)
234
- ctx->event_data_cnt++;
235
-
236
- fds = calloc(ctx->event_data_cnt, sizeof(*fds));
237
- if (!fds)
238
- return LIBUSB_ERROR_NO_MEM;
239
-
240
- for_each_event_source(ctx, ievent_source) {
241
- fds[i].fd = ievent_source->data.os_handle;
242
- fds[i].events = ievent_source->data.poll_events;
243
- i++;
244
- }
245
-
246
- ctx->event_data = fds;
247
- return 0;
248
- }
249
-
250
- int usbi_wait_for_events(struct libusb_context *ctx,
251
- struct usbi_reported_events *reported_events, int timeout_ms)
252
- {
253
- struct pollfd *fds = ctx->event_data;
254
- usbi_nfds_t nfds = (usbi_nfds_t)ctx->event_data_cnt;
255
- int internal_fds, num_ready;
256
-
257
- usbi_dbg(ctx, "poll() %u fds with timeout in %dms", (unsigned int)nfds, timeout_ms);
258
- #ifdef __EMSCRIPTEN__
259
- /* Emscripten's poll doesn't actually block, so we need to use an
260
- * out-of-band waiting signal. */
261
- em_libusb_wait(&ctx->event.has_event, 0, timeout_ms);
262
- /* Emscripten ignores timeout_ms, but set it to 0 for future-proofing
263
- * in case they ever implement real poll. */
264
- timeout_ms = 0;
265
- #endif
266
- num_ready = poll(fds, nfds, timeout_ms);
267
- usbi_dbg(ctx, "poll() returned %d", num_ready);
268
- if (num_ready == 0) {
269
- if (usbi_using_timer(ctx))
270
- goto done;
271
- return LIBUSB_ERROR_TIMEOUT;
272
- } else if (num_ready == -1) {
273
- if (errno == EINTR)
274
- return LIBUSB_ERROR_INTERRUPTED;
275
- usbi_err(ctx, "poll() failed, errno=%d", errno);
276
- return LIBUSB_ERROR_IO;
277
- }
278
-
279
- /* fds[0] is always the internal signalling event */
280
- if (fds[0].revents) {
281
- reported_events->event_triggered = 1;
282
- num_ready--;
283
- } else {
284
- reported_events->event_triggered = 0;
285
- }
286
-
287
- #ifdef HAVE_OS_TIMER
288
- /* on timer configurations, fds[1] is the timer */
289
- if (usbi_using_timer(ctx) && fds[1].revents) {
290
- reported_events->timer_triggered = 1;
291
- num_ready--;
292
- } else {
293
- reported_events->timer_triggered = 0;
294
- }
295
- #endif
296
-
297
- if (!num_ready)
298
- goto done;
299
-
300
- /* the backend will never need to attempt to handle events on the
301
- * library's internal file descriptors, so we determine how many are
302
- * in use internally for this context and skip these when passing any
303
- * remaining pollfds to the backend. */
304
- internal_fds = usbi_using_timer(ctx) ? 2 : 1;
305
- fds += internal_fds;
306
- nfds -= internal_fds;
307
-
308
- usbi_mutex_lock(&ctx->event_data_lock);
309
- if (ctx->event_flags & USBI_EVENT_EVENT_SOURCES_MODIFIED) {
310
- struct usbi_event_source *ievent_source;
311
-
312
- for_each_removed_event_source(ctx, ievent_source) {
313
- usbi_nfds_t n;
314
-
315
- for (n = 0; n < nfds; n++) {
316
- if (ievent_source->data.os_handle != fds[n].fd)
317
- continue;
318
- if (!fds[n].revents)
319
- continue;
320
- /* pollfd was removed between the creation of the fds array and
321
- * here. remove triggered revent as it is no longer relevant. */
322
- usbi_dbg(ctx, "fd %d was removed, ignoring raised events", fds[n].fd);
323
- fds[n].revents = 0;
324
- num_ready--;
325
- break;
326
- }
327
- }
328
- }
329
- usbi_mutex_unlock(&ctx->event_data_lock);
330
-
331
- if (num_ready) {
332
- assert(num_ready > 0);
333
- reported_events->event_data = fds;
334
- reported_events->event_data_count = (unsigned int)nfds;
335
- }
336
-
337
- done:
338
- reported_events->num_ready = num_ready;
339
- return LIBUSB_SUCCESS;
340
- }
@@ -1,62 +0,0 @@
1
- /*
2
- * libusb event abstraction on POSIX platforms
3
- *
4
- * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
5
- *
6
- * This library is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU Lesser General Public
8
- * License as published by the Free Software Foundation; either
9
- * version 2.1 of the License, or (at your option) any later version.
10
- *
11
- * This library is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- * Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public
17
- * License along with this library; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
- */
20
-
21
- #ifndef LIBUSB_EVENTS_POSIX_H
22
- #define LIBUSB_EVENTS_POSIX_H
23
-
24
- #include <poll.h>
25
-
26
- typedef int usbi_os_handle_t;
27
- #define USBI_OS_HANDLE_FORMAT_STRING "fd %d"
28
-
29
- #ifdef HAVE_EVENTFD
30
- typedef struct usbi_event {
31
- int eventfd;
32
- } usbi_event_t;
33
- #define USBI_EVENT_OS_HANDLE(e) ((e)->eventfd)
34
- #define USBI_EVENT_POLL_EVENTS POLLIN
35
- #define USBI_INVALID_EVENT { -1 }
36
- #else
37
- typedef struct usbi_event {
38
- int pipefd[2];
39
- #ifdef __EMSCRIPTEN__
40
- _Atomic int has_event;
41
- #endif
42
- } usbi_event_t;
43
- #define USBI_EVENT_OS_HANDLE(e) ((e)->pipefd[0])
44
- #define USBI_EVENT_POLL_EVENTS POLLIN
45
- #define USBI_INVALID_EVENT { { -1, -1 } }
46
- #endif
47
-
48
- #ifdef HAVE_TIMERFD
49
- #define HAVE_OS_TIMER 1
50
- typedef struct usbi_timer {
51
- int timerfd;
52
- } usbi_timer_t;
53
- #define USBI_TIMER_OS_HANDLE(t) ((t)->timerfd)
54
- #define USBI_TIMER_POLL_EVENTS POLLIN
55
-
56
- static inline int usbi_timer_valid(usbi_timer_t *timer)
57
- {
58
- return timer->timerfd >= 0;
59
- }
60
- #endif
61
-
62
- #endif
@@ -1,214 +0,0 @@
1
- /*
2
- * libusb event abstraction on Microsoft Windows
3
- *
4
- * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
5
- *
6
- * This library is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU Lesser General Public
8
- * License as published by the Free Software Foundation; either
9
- * version 2.1 of the License, or (at your option) any later version.
10
- *
11
- * This library is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- * Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public
17
- * License along with this library; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
- */
20
-
21
- #include <config.h>
22
-
23
- #include "libusbi.h"
24
- #include "windows_common.h"
25
-
26
- int usbi_create_event(usbi_event_t *event)
27
- {
28
- event->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
29
- if (event->hEvent == NULL) {
30
- usbi_err(NULL, "CreateEvent failed: %s", windows_error_str(0));
31
- return LIBUSB_ERROR_OTHER;
32
- }
33
-
34
- return 0;
35
- }
36
-
37
- void usbi_destroy_event(usbi_event_t *event)
38
- {
39
- if (!CloseHandle(event->hEvent))
40
- usbi_warn(NULL, "CloseHandle failed: %s", windows_error_str(0));
41
- }
42
-
43
- void usbi_signal_event(usbi_event_t *event)
44
- {
45
- if (!SetEvent(event->hEvent))
46
- usbi_warn(NULL, "SetEvent failed: %s", windows_error_str(0));
47
- }
48
-
49
- void usbi_clear_event(usbi_event_t *event)
50
- {
51
- if (!ResetEvent(event->hEvent))
52
- usbi_warn(NULL, "ResetEvent failed: %s", windows_error_str(0));
53
- }
54
-
55
- #ifdef HAVE_OS_TIMER
56
- int usbi_create_timer(usbi_timer_t *timer)
57
- {
58
- timer->hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
59
- if (timer->hTimer == NULL) {
60
- usbi_warn(NULL, "CreateWaitableTimer failed: %s", windows_error_str(0));
61
- return LIBUSB_ERROR_OTHER;
62
- }
63
-
64
- return 0;
65
- }
66
-
67
- void usbi_destroy_timer(usbi_timer_t *timer)
68
- {
69
- if (!CloseHandle(timer->hTimer))
70
- usbi_warn(NULL, "CloseHandle failed: %s", windows_error_str(0));
71
- }
72
-
73
- int usbi_arm_timer(usbi_timer_t *timer, const struct timespec *timeout)
74
- {
75
- struct timespec systime, remaining;
76
- FILETIME filetime;
77
- LARGE_INTEGER dueTime;
78
-
79
- /* Transfer timeouts are based on the monotonic clock and the waitable
80
- * timers on the system clock. This requires a conversion between the
81
- * two, so we calculate the remaining time relative to the monotonic
82
- * clock and calculate an absolute system time for the timer expiration.
83
- * Note that if the timeout has already passed, the remaining time will
84
- * be negative and thus an absolute system time in the past will be set.
85
- * This works just as intended because the timer becomes signalled
86
- * immediately. */
87
- usbi_get_monotonic_time(&systime);
88
-
89
- TIMESPEC_SUB(timeout, &systime, &remaining);
90
-
91
- GetSystemTimeAsFileTime(&filetime);
92
- dueTime.LowPart = filetime.dwLowDateTime;
93
- dueTime.HighPart = filetime.dwHighDateTime;
94
- dueTime.QuadPart += (remaining.tv_sec * 10000000LL) + (remaining.tv_nsec / 100LL);
95
-
96
- if (!SetWaitableTimer(timer->hTimer, &dueTime, 0, NULL, NULL, FALSE)) {
97
- usbi_warn(NULL, "SetWaitableTimer failed: %s", windows_error_str(0));
98
- return LIBUSB_ERROR_OTHER;
99
- }
100
-
101
- return 0;
102
- }
103
-
104
- int usbi_disarm_timer(usbi_timer_t *timer)
105
- {
106
- LARGE_INTEGER dueTime;
107
-
108
- /* A manual-reset waitable timer will stay in the signalled state until
109
- * another call to SetWaitableTimer() is made. It is possible that the
110
- * timer has already expired by the time we come in to disarm it, so to
111
- * be entirely sure the timer is disarmed and not in the signalled state,
112
- * we will set it with an impossibly large expiration and immediately
113
- * cancel. */
114
- dueTime.QuadPart = LLONG_MAX;
115
- if (!SetWaitableTimer(timer->hTimer, &dueTime, 0, NULL, NULL, FALSE)) {
116
- usbi_warn(NULL, "SetWaitableTimer failed: %s", windows_error_str(0));
117
- return LIBUSB_ERROR_OTHER;
118
- }
119
-
120
- if (!CancelWaitableTimer(timer->hTimer)) {
121
- usbi_warn(NULL, "SetWaitableTimer failed: %s", windows_error_str(0));
122
- return LIBUSB_ERROR_OTHER;
123
- }
124
-
125
- return 0;
126
- }
127
- #endif
128
-
129
- int usbi_alloc_event_data(struct libusb_context *ctx)
130
- {
131
- struct usbi_event_source *ievent_source;
132
- HANDLE *handles;
133
- size_t i = 0;
134
-
135
- /* Event sources are only added during usbi_io_init(). We should not
136
- * be running this function again if the event data has already been
137
- * allocated. */
138
- if (ctx->event_data) {
139
- usbi_warn(ctx, "program assertion failed - event data already allocated");
140
- return LIBUSB_ERROR_OTHER;
141
- }
142
-
143
- ctx->event_data_cnt = 0;
144
- for_each_event_source(ctx, ievent_source)
145
- ctx->event_data_cnt++;
146
-
147
- /* We only expect up to two HANDLEs to wait on, one for the internal
148
- * signalling event and the other for the timer. */
149
- if (ctx->event_data_cnt != 1 && ctx->event_data_cnt != 2) {
150
- usbi_err(ctx, "program assertion failed - expected exactly 1 or 2 HANDLEs");
151
- return LIBUSB_ERROR_OTHER;
152
- }
153
-
154
- handles = calloc(ctx->event_data_cnt, sizeof(HANDLE));
155
- if (!handles)
156
- return LIBUSB_ERROR_NO_MEM;
157
-
158
- for_each_event_source(ctx, ievent_source) {
159
- handles[i] = ievent_source->data.os_handle;
160
- i++;
161
- }
162
-
163
- ctx->event_data = handles;
164
- return 0;
165
- }
166
-
167
- int usbi_wait_for_events(struct libusb_context *ctx,
168
- struct usbi_reported_events *reported_events, int timeout_ms)
169
- {
170
- HANDLE *handles = ctx->event_data;
171
- DWORD num_handles = (DWORD)ctx->event_data_cnt;
172
- DWORD result;
173
-
174
- usbi_dbg(ctx, "WaitForMultipleObjects() for %lu HANDLEs with timeout in %dms", ULONG_CAST(num_handles), timeout_ms);
175
- result = WaitForMultipleObjects(num_handles, handles, FALSE, (DWORD)timeout_ms);
176
- usbi_dbg(ctx, "WaitForMultipleObjects() returned %lu", ULONG_CAST(result));
177
- if (result == WAIT_TIMEOUT) {
178
- if (usbi_using_timer(ctx))
179
- goto done;
180
- return LIBUSB_ERROR_TIMEOUT;
181
- } else if (result == WAIT_FAILED) {
182
- usbi_err(ctx, "WaitForMultipleObjects() failed: %s", windows_error_str(0));
183
- return LIBUSB_ERROR_IO;
184
- }
185
-
186
- result -= WAIT_OBJECT_0;
187
-
188
- /* handles[0] is always the internal signalling event */
189
- if (result == 0)
190
- reported_events->event_triggered = 1;
191
- else
192
- reported_events->event_triggered = 0;
193
-
194
- #ifdef HAVE_OS_TIMER
195
- /* on timer configurations, handles[1] is the timer */
196
- if (usbi_using_timer(ctx)) {
197
- /* The WaitForMultipleObjects() function reports the index of
198
- * the first object that became signalled. If the internal
199
- * signalling event was reported, we need to also check and
200
- * report whether the timer is in the signalled state. */
201
- if (result == 1 || WaitForSingleObject(handles[1], 0) == WAIT_OBJECT_0)
202
- reported_events->timer_triggered = 1;
203
- else
204
- reported_events->timer_triggered = 0;
205
- } else {
206
- reported_events->timer_triggered = 0;
207
- }
208
- #endif
209
-
210
- done:
211
- /* no events are ever reported to the backend */
212
- reported_events->num_ready = 0;
213
- return LIBUSB_SUCCESS;
214
- }
@@ -1,46 +0,0 @@
1
- /*
2
- * libusb event abstraction on Microsoft Windows
3
- *
4
- * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
5
- *
6
- * This library is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU Lesser General Public
8
- * License as published by the Free Software Foundation; either
9
- * version 2.1 of the License, or (at your option) any later version.
10
- *
11
- * This library is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- * Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public
17
- * License along with this library; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
- */
20
-
21
- #ifndef LIBUSB_EVENTS_WINDOWS_H
22
- #define LIBUSB_EVENTS_WINDOWS_H
23
-
24
- typedef HANDLE usbi_os_handle_t;
25
- #define USBI_OS_HANDLE_FORMAT_STRING "HANDLE %p"
26
-
27
- typedef struct usbi_event {
28
- HANDLE hEvent;
29
- } usbi_event_t;
30
- #define USBI_EVENT_OS_HANDLE(e) ((e)->hEvent)
31
- #define USBI_EVENT_POLL_EVENTS 0
32
- #define USBI_INVALID_EVENT { INVALID_HANDLE_VALUE }
33
-
34
- #define HAVE_OS_TIMER 1
35
- typedef struct usbi_timer {
36
- HANDLE hTimer;
37
- } usbi_timer_t;
38
- #define USBI_TIMER_OS_HANDLE(t) ((t)->hTimer)
39
- #define USBI_TIMER_POLL_EVENTS 0
40
-
41
- static inline int usbi_timer_valid(usbi_timer_t *timer)
42
- {
43
- return timer->hTimer != NULL;
44
- }
45
-
46
- #endif