1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi/native_api.h"
17 #include "native_common.h"
18 #include "usb/usb_ddk_api.h"
19 #include "usb/usb_ddk_types.h"
20 #include "ddk/ddk_api.h"
21 #include <cstdlib>
22 #include <js_native_api_types.h>
23 #include <tuple>
24 #include <unistd.h>
25
26 #define ENDPOINT 0
27 #define SLEEP 2
28 #define PARAM_8 8
29 #define USB_DDK_TEST_BUF_SIZE 100
30 #define USB_DDK_ENDPOINT_DIR_MASK 0x80
31 #define USB_DDK_DIR_IN 0x80
32 #define USB_DDK_ENDPOINT_XFERTYPE_MASK 0x03
33 #define USB_DDK_ENDPOINT_XFER_INT 0x03
34 #define PARAM_0 0
35 #define PARAM_1 1
36 #define PARAM_10 10
37 #define PARAM_32 32
38 #define PARAM_48 48
39 static uint8_t configIndex = 0;
40 static uint8_t interfaceIndex = 0;
41 static uint64_t interfaceHandle = 0;
42 static uint8_t settingIndex = 0;
43 static uint32_t timeout = 1000;
44
45
IsInterruptInEndpoint(const UsbEndpointDescriptor &epDesc)46 static bool IsInterruptInEndpoint(const UsbEndpointDescriptor &epDesc)
47 {
48 return (((epDesc.bEndpointAddress & USB_DDK_ENDPOINT_DIR_MASK) == USB_DDK_DIR_IN) &&
49 ((epDesc.bmAttributes & USB_DDK_ENDPOINT_XFERTYPE_MASK) == USB_DDK_ENDPOINT_XFER_INT));
50 }
51
FindForEachInterface(const UsbDdkInterface &interface)52 static std::tuple<bool, uint8_t, uint8_t, uint16_t> FindForEachInterface(const UsbDdkInterface &interface)
53 {
54 struct UsbDdkInterfaceDescriptor *intDesc = interface.altsetting;
55 uint32_t numSetting = interface.numAltsetting;
56 for (uint32_t setIdx = PARAM_0; setIdx < numSetting; ++setIdx) {
57 uint32_t numEp = intDesc[setIdx].interfaceDescriptor.bNumEndpoints;
58 struct UsbDdkEndpointDescriptor *epDesc = intDesc[setIdx].endPoint;
59 for (uint32_t epIdx = PARAM_0; epIdx < numEp; ++epIdx) {
60 if (IsInterruptInEndpoint(epDesc[epIdx].endpointDescriptor)) {
61 return {true, intDesc[setIdx].interfaceDescriptor.bInterfaceNumber,
62 epDesc[epIdx].endpointDescriptor.bEndpointAddress,
63 epDesc[epIdx].endpointDescriptor.wMaxPacketSize};
64 }
65 }
66 }
67
68 return {false, {}, {}, {}};
69 }
70
GetEndpointInfo(const struct UsbDdkConfigDescriptor *config)71 static std::tuple<bool, uint8_t, uint8_t, uint16_t> GetEndpointInfo(const struct UsbDdkConfigDescriptor *config)
72 {
73 for (uint32_t intIdx = PARAM_0; intIdx < config->configDescriptor.bNumInterfaces; ++intIdx) {
74 auto result = FindForEachInterface(config->interface[intIdx]);
75 if (std::get<0>(result)) {
76 return result;
77 }
78 }
79 return {false, {}, {}, {}};
80 }
81
UsbInit(napi_env env, napi_callback_info info)82 static napi_value UsbInit(napi_env env, napi_callback_info info)
83 {
84 int32_t returnValue = OH_Usb_Init();
85 OH_Usb_Release();
86 napi_value result = nullptr;
87 napi_create_int32(env, returnValue, &result);
88 return result;
89 }
90
UsbRelease(napi_env env, napi_callback_info info)91 static napi_value UsbRelease(napi_env env, napi_callback_info info)
92 {
93 int32_t usbInitReturnValue = OH_Usb_Init();
94 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
95 OH_Usb_Release();
96 napi_value result = nullptr;
97 napi_create_int32(env, true, &result);
98 return result;
99 }
100
UsbGetDeviceDescriptorOne(napi_env env, napi_callback_info info)101 static napi_value UsbGetDeviceDescriptorOne(napi_env env, napi_callback_info info)
102 {
103 size_t argc = PARAM_1;
104 napi_value args[PARAM_1] = {nullptr};
105 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
106 int64_t deviceId64;
107 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
108 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
109
110 int32_t usbInitReturnValue = OH_Usb_Init();
111 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
112 struct UsbDeviceDescriptor devDesc;
113 int32_t returnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
114 napi_value result = nullptr;
115 napi_create_int32(env, returnValue, &result);
116 return result;
117 }
118
UsbGetDeviceDescriptorTwo(napi_env env, napi_callback_info info)119 static napi_value UsbGetDeviceDescriptorTwo(napi_env env, napi_callback_info info)
120 {
121 size_t argc = PARAM_1;
122 napi_value args[PARAM_1] = {nullptr};
123 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
124 int64_t deviceId64;
125 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
126 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
127 int32_t usbInitReturnValue = OH_Usb_Init();
128 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
129 int32_t returnValue = OH_Usb_GetDeviceDescriptor(deviceId, nullptr);
130 napi_value result = nullptr;
131 napi_create_int32(env, returnValue, &result);
132 return result;
133 }
134
UsbGetConfigDescriptorOne(napi_env env, napi_callback_info info)135 static napi_value UsbGetConfigDescriptorOne(napi_env env, napi_callback_info info)
136 {
137 size_t argc = PARAM_1;
138 napi_value args[PARAM_1] = {nullptr};
139 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
140 int64_t deviceId64;
141 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
142 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
143 int32_t usbInitReturnValue = OH_Usb_Init();
144 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
145 struct UsbDdkConfigDescriptor *config = nullptr;
146 int32_t returnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
147 OH_Usb_FreeConfigDescriptor(config);
148 napi_value result = nullptr;
149 napi_create_int32(env, returnValue, &result);
150 return result;
151 }
152
UsbGetConfigDescriptorTwo(napi_env env, napi_callback_info info)153 static napi_value UsbGetConfigDescriptorTwo(napi_env env, napi_callback_info info)
154 {
155 struct UsbDdkConfigDescriptor *config = nullptr;
156 uint64_t errorId = PARAM_1;
157 int32_t returnValue = OH_Usb_GetConfigDescriptor(errorId, configIndex, &config);
158 OH_Usb_FreeConfigDescriptor(config);
159 napi_value result = nullptr;
160 napi_create_int32(env, returnValue, &result);
161 return result;
162 }
163
UsbGetConfigDescriptorThree(napi_env env, napi_callback_info info)164 static napi_value UsbGetConfigDescriptorThree(napi_env env, napi_callback_info info)
165 {
166 size_t argc = PARAM_1;
167 napi_value args[PARAM_1] = {nullptr};
168 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
169 int64_t deviceId64;
170 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
171 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
172 int32_t usbInitReturnValue = OH_Usb_Init();
173 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
174
175 int32_t returnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, nullptr);
176 napi_value result = nullptr;
177 napi_create_int32(env, returnValue, &result);
178 return result;
179 }
180
UsbFreeConfigDescriptor(napi_env env, napi_callback_info info)181 static napi_value UsbFreeConfigDescriptor(napi_env env, napi_callback_info info)
182 {
183 size_t argc = PARAM_1;
184 napi_value args[PARAM_1] = {nullptr};
185 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
186 int64_t deviceId64;
187 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
188 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
189 int32_t usbInitReturnValue = OH_Usb_Init();
190 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
191 struct UsbDdkConfigDescriptor *config = nullptr;
192 int32_t returnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
193 NAPI_ASSERT(env, returnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
194 OH_Usb_FreeConfigDescriptor(config);
195 napi_value result = nullptr;
196 napi_create_int32(env, true, &result);
197 return result;
198 }
199
UsbClaimInterfaceOne(napi_env env, napi_callback_info info)200 static napi_value UsbClaimInterfaceOne(napi_env env, napi_callback_info info)
201 {
202 size_t argc = PARAM_1;
203 napi_value args[PARAM_1] = {nullptr};
204 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
205 int64_t deviceId64;
206 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
207 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
208 int32_t usbInitReturnValue = OH_Usb_Init();
209 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
210 int32_t returnValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
211 napi_value result = nullptr;
212 napi_create_int32(env, returnValue, &result);
213 return result;
214 }
215
UsbClaimInterfaceTwo(napi_env env, napi_callback_info info)216 static napi_value UsbClaimInterfaceTwo(napi_env env, napi_callback_info info)
217 {
218 uint64_t errorId = PARAM_1;
219 int32_t returnValue = OH_Usb_ClaimInterface(errorId, interfaceIndex, &interfaceHandle);
220 napi_value result = nullptr;
221 napi_create_int32(env, returnValue, &result);
222 return result;
223 }
224
UsbClaimInterfaceThree(napi_env env, napi_callback_info info)225 static napi_value UsbClaimInterfaceThree(napi_env env, napi_callback_info info)
226 {
227 size_t argc = PARAM_1;
228 napi_value args[PARAM_1] = {nullptr};
229 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
230 int64_t deviceId64;
231 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
232 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
233 int32_t usbInitReturnValue = OH_Usb_Init();
234 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
235 int32_t returnValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, nullptr);
236 napi_value result = nullptr;
237 napi_create_int32(env, returnValue, &result);
238 return result;
239 }
240
UsbReleaseInterface(napi_env env, napi_callback_info info)241 static napi_value UsbReleaseInterface(napi_env env, napi_callback_info info)
242 {
243 size_t argc = PARAM_1;
244 napi_value args[PARAM_1] = {nullptr};
245 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
246 int64_t deviceId64;
247 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
248 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
249 int32_t usbInitReturnValue = OH_Usb_Init();
250 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
251 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
252 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
253 int32_t returnValue = OH_Usb_ReleaseInterface(interfaceHandle);
254 napi_value result = nullptr;
255 napi_create_int32(env, returnValue, &result);
256 return result;
257 }
258
UsbSelectInterfaceSettingOne(napi_env env, napi_callback_info info)259 static napi_value UsbSelectInterfaceSettingOne(napi_env env, napi_callback_info info)
260 {
261 size_t argc = PARAM_1;
262 napi_value args[PARAM_1] = {nullptr};
263 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
264 int64_t deviceId64;
265 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
266 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
267 int32_t usbInitReturnValue = OH_Usb_Init();
268 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
269 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
270 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
271 int32_t returnValue = OH_Usb_SelectInterfaceSetting(interfaceHandle, settingIndex);
272 napi_value result = nullptr;
273 napi_create_int32(env, returnValue, &result);
274 return result;
275 }
276
UsbSelectInterfaceSettingTwo(napi_env env, napi_callback_info info)277 static napi_value UsbSelectInterfaceSettingTwo(napi_env env, napi_callback_info info)
278 {
279 size_t argc = PARAM_1;
280 napi_value args[PARAM_1] = {nullptr};
281 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
282 int64_t deviceId64;
283 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
284 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
285 int32_t usbInitReturnValue = OH_Usb_Init();
286 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
287 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
288 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
289 OH_Usb_Release();
290 int32_t returnValue = OH_Usb_SelectInterfaceSetting(interfaceHandle, settingIndex);
291 napi_value result = nullptr;
292 napi_create_int32(env, returnValue, &result);
293 return result;
294 }
295
UsbGetCurrentInterfaceSettingOne(napi_env env, napi_callback_info info)296 static napi_value UsbGetCurrentInterfaceSettingOne(napi_env env, napi_callback_info info)
297 {
298 size_t argc = PARAM_1;
299 napi_value args[PARAM_1] = {nullptr};
300 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
301 int64_t deviceId64;
302 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
303 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
304 int32_t usbInitReturnValue = OH_Usb_Init();
305 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
306 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
307 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "OH_Usb_ClaimInterface failed");
308 int32_t usbSelectInterfaceSettingReturnValue = OH_Usb_SelectInterfaceSetting(interfaceHandle, settingIndex);
309 NAPI_ASSERT(env, usbSelectInterfaceSettingReturnValue == PARAM_0, "OH_Usb_SelectInterfaceSetting failed");
310 int32_t returnValue = OH_Usb_GetCurrentInterfaceSetting(interfaceHandle, &settingIndex);
311 napi_value result = nullptr;
312 napi_create_int32(env, returnValue, &result);
313 return result;
314 }
315
UsbGetCurrentInterfaceSettingTwo(napi_env env, napi_callback_info info)316 static napi_value UsbGetCurrentInterfaceSettingTwo(napi_env env, napi_callback_info info)
317 {
318 size_t argc = PARAM_1;
319 napi_value args[PARAM_1] = {nullptr};
320 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
321 int64_t deviceId64;
322 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
323 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
324 int32_t usbInitReturnValue = OH_Usb_Init();
325 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
326 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
327 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "OH_Usb_ClaimInterface failed");
328 int32_t usbSelectInterfaceSettingReturnValue = OH_Usb_SelectInterfaceSetting(interfaceHandle, settingIndex);
329 NAPI_ASSERT(env, usbSelectInterfaceSettingReturnValue == PARAM_0, "OH_Usb_SelectInterfaceSetting failed");
330 OH_Usb_Release();
331 int32_t returnValue = OH_Usb_GetCurrentInterfaceSetting(interfaceHandle, &settingIndex);
332 napi_value result = nullptr;
333 napi_create_int32(env, returnValue, &result);
334 return result;
335 }
336
UsbGetCurrentInterfaceSettingThree(napi_env env, napi_callback_info info)337 static napi_value UsbGetCurrentInterfaceSettingThree(napi_env env, napi_callback_info info)
338 {
339 size_t argc = PARAM_1;
340 napi_value args[PARAM_1] = {nullptr};
341 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
342 int64_t deviceId64;
343 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
344 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
345 int32_t usbInitReturnValue = OH_Usb_Init();
346 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
347 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
348 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
349 int32_t usbSelectInterfaceSettingReturnValue = OH_Usb_SelectInterfaceSetting(interfaceHandle, settingIndex);
350 NAPI_ASSERT(env, usbSelectInterfaceSettingReturnValue == PARAM_0, "Usb_ClaimInterface failed");
351 int32_t returnValue = OH_Usb_GetCurrentInterfaceSetting(interfaceHandle, nullptr);
352 napi_value result = nullptr;
353 napi_create_int32(env, returnValue, &result);
354 return result;
355 }
356
UsbSendControlReadRequestOne(napi_env env, napi_callback_info info)357 static napi_value UsbSendControlReadRequestOne(napi_env env, napi_callback_info info)
358 {
359 size_t argc = PARAM_1;
360 napi_value args[PARAM_1] = {nullptr};
361 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
362 int64_t deviceId64;
363 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
364 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
365 int32_t usbInitReturnValue = OH_Usb_Init();
366 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
367 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
368 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
369 struct UsbControlRequestSetup setup;
370 uint8_t data[USB_DDK_TEST_BUF_SIZE] = {PARAM_0};
371 uint32_t dataLen = USB_DDK_TEST_BUF_SIZE;
372 setup.bmRequestType = 0x80;
373 setup.bRequest = 0x06;
374 setup.wValue = (0x03 << PARAM_8) | 0x01;
375 setup.wIndex = 0x409;
376 setup.wLength = dataLen;
377 int32_t returnValue = OH_Usb_SendControlReadRequest(interfaceHandle, &setup, UINT32_MAX, data, &dataLen);
378 napi_value result = nullptr;
379 napi_create_int32(env, returnValue, &result);
380 return result;
381 }
382
UsbSendControlReadRequestTwo(napi_env env, napi_callback_info info)383 static napi_value UsbSendControlReadRequestTwo(napi_env env, napi_callback_info info)
384 {
385 size_t argc = PARAM_1;
386 napi_value args[PARAM_1] = {nullptr};
387 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
388 int64_t deviceId64;
389 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
390 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
391 int32_t usbInitReturnValue = OH_Usb_Init();
392 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
393 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
394 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
395 OH_Usb_Release();
396 struct UsbControlRequestSetup setup;
397 uint8_t data[USB_DDK_TEST_BUF_SIZE] = {PARAM_0};
398 uint32_t dataLen = USB_DDK_TEST_BUF_SIZE;
399 int32_t returnValue = OH_Usb_SendControlReadRequest(interfaceHandle, &setup, timeout, data, &dataLen);
400 napi_value result = nullptr;
401 napi_create_int32(env, returnValue, &result);
402 return result;
403 }
404
UsbSendControlReadRequestThree(napi_env env, napi_callback_info info)405 static napi_value UsbSendControlReadRequestThree(napi_env env, napi_callback_info info)
406 {
407 size_t argc = PARAM_1;
408 napi_value args[PARAM_1] = {nullptr};
409 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
410 int64_t deviceId64;
411 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
412 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
413 int32_t usbInitReturnValue = OH_Usb_Init();
414 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
415 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
416 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
417 uint8_t data[USB_DDK_TEST_BUF_SIZE] = {PARAM_0};
418 uint32_t dataLen = USB_DDK_TEST_BUF_SIZE;
419 int32_t returnValue = OH_Usb_SendControlReadRequest(interfaceHandle, nullptr, timeout, data, &dataLen);
420 napi_value result = nullptr;
421 napi_create_int32(env, returnValue, &result);
422 return result;
423 }
424
UsbSendControlReadRequestFour(napi_env env, napi_callback_info info)425 static napi_value UsbSendControlReadRequestFour(napi_env env, napi_callback_info info)
426 {
427 size_t argc = PARAM_1;
428 napi_value args[PARAM_1] = {nullptr};
429 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
430 int64_t deviceId64;
431 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
432 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
433 int32_t usbInitReturnValue = OH_Usb_Init();
434 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
435 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
436 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
437 struct UsbControlRequestSetup setup;
438 uint32_t dataLen = PARAM_10;
439 int32_t returnValue = OH_Usb_SendControlReadRequest(interfaceHandle, &setup, timeout, nullptr, &dataLen);
440 napi_value result = nullptr;
441 napi_create_int32(env, returnValue, &result);
442 return result;
443 }
444
UsbSendControlReadRequestFive(napi_env env, napi_callback_info info)445 static napi_value UsbSendControlReadRequestFive(napi_env env, napi_callback_info info)
446 {
447 size_t argc = PARAM_1;
448 napi_value args[PARAM_1] = {nullptr};
449 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
450 int64_t deviceId64;
451 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
452 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
453 int32_t usbInitReturnValue = OH_Usb_Init();
454 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
455 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
456 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
457 struct UsbControlRequestSetup setup;
458 uint8_t data = PARAM_10;
459 int32_t returnValue = OH_Usb_SendControlReadRequest(interfaceHandle, &setup, timeout, &data, nullptr);
460 napi_value result = nullptr;
461 napi_create_int32(env, returnValue, &result);
462 return result;
463 }
464
UsbSendControlWriteRequestOne(napi_env env, napi_callback_info info)465 static napi_value UsbSendControlWriteRequestOne(napi_env env, napi_callback_info info)
466 {
467 size_t argc = PARAM_1;
468 napi_value args[PARAM_1] = {nullptr};
469 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
470 int64_t deviceId64;
471 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
472 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
473 int32_t usbInitReturnValue = OH_Usb_Init();
474 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
475 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
476 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
477 struct UsbControlRequestSetup setup;
478 uint8_t data = PARAM_10;
479 uint32_t dataLen = PARAM_10;
480 int32_t returnValue = OH_Usb_SendControlWriteRequest(interfaceHandle, &setup, timeout, &data, dataLen);
481 napi_value result = nullptr;
482 napi_create_int32(env, returnValue, &result);
483 return result;
484 }
485
UsbSendControlWriteRequestTwo(napi_env env, napi_callback_info info)486 static napi_value UsbSendControlWriteRequestTwo(napi_env env, napi_callback_info info)
487 {
488 size_t argc = PARAM_1;
489 napi_value args[PARAM_1] = {nullptr};
490 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
491 int64_t deviceId64;
492 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
493 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
494 int32_t usbInitReturnValue = OH_Usb_Init();
495 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
496 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
497 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
498 OH_Usb_Release();
499 struct UsbControlRequestSetup setup;
500 uint8_t data = PARAM_10;
501 uint32_t dataLen = PARAM_10;
502 int32_t returnValue = OH_Usb_SendControlWriteRequest(interfaceHandle, &setup, timeout, &data, dataLen);
503 napi_value result = nullptr;
504 napi_create_int32(env, returnValue, &result);
505 return result;
506 }
507
UsbSendControlWriteRequestThree(napi_env env, napi_callback_info info)508 static napi_value UsbSendControlWriteRequestThree(napi_env env, napi_callback_info info)
509 {
510 size_t argc = PARAM_1;
511 napi_value args[PARAM_1] = {nullptr};
512 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
513 int64_t deviceId64;
514 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
515 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
516 int32_t usbInitReturnValue = OH_Usb_Init();
517 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
518 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
519 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
520 uint8_t data = PARAM_10;
521 uint32_t dataLen = PARAM_10;
522 int32_t returnValue = OH_Usb_SendControlWriteRequest(interfaceHandle, nullptr, timeout, &data, dataLen);
523 napi_value result = nullptr;
524 napi_create_int32(env, returnValue, &result);
525 return result;
526 }
527
UsbSendControlWriteRequestFour(napi_env env, napi_callback_info info)528 static napi_value UsbSendControlWriteRequestFour(napi_env env, napi_callback_info info)
529 {
530 size_t argc = PARAM_1;
531 napi_value args[PARAM_1] = {nullptr};
532 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
533 int64_t deviceId64;
534 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
535 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
536 int32_t usbInitReturnValue = OH_Usb_Init();
537 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
538 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
539 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
540 struct UsbControlRequestSetup setup;
541 uint32_t dataLen = PARAM_10;
542 int32_t returnValue = OH_Usb_SendControlWriteRequest(interfaceHandle, &setup, timeout, nullptr, dataLen);
543 napi_value result = nullptr;
544 napi_create_int32(env, returnValue, &result);
545 return result;
546 }
JsDeviceIdToNative(uint64_t deviceId)547 uint64_t JsDeviceIdToNative(uint64_t deviceId)
548 {
549 uint32_t busNum = (uint32_t)(deviceId >> PARAM_48);
550 uint32_t devNum = (uint32_t)((deviceId & 0x0000FFFF00000000) >> PARAM_32);
551 return (((static_cast<uint64_t>(busNum)) << PARAM_32) | devNum);
552 }
553
UsbSendPipeRequestOne(napi_env env, napi_callback_info info)554 static napi_value UsbSendPipeRequestOne(napi_env env, napi_callback_info info)
555 {
556 size_t argc = PARAM_1;
557 napi_value args[PARAM_1] = {nullptr};
558 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
559 int64_t deviceId64;
560 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
561 uint64_t deviceId = JsDeviceIdToNative(static_cast<uint64_t>(deviceId64));
562 int32_t usbInitReturnValue = OH_Usb_Init();
563 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
564 struct UsbDeviceDescriptor devDesc;
565 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
566 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
567 struct UsbDdkConfigDescriptor *config = nullptr;
568 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
569 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
570 auto [result1, interface1, endpoint1, maxPktSize1] = GetEndpointInfo(config);
571 OH_Usb_FreeConfigDescriptor(config);
572 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
573 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
574 struct UsbDeviceMemMap *devMemMap = nullptr;
575 size_t bufferLen = PARAM_10;
576 int32_t usbCreateDeviceMemMapReturnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
577 NAPI_ASSERT(env, usbCreateDeviceMemMapReturnValue == PARAM_0, "OH_Usb_CreateDeviceMemMap failed");
578 NAPI_ASSERT(env, devMemMap != nullptr, "OH_Usb_CreateDeviceMemMap failed");
579 struct UsbRequestPipe pipe;
580 pipe.interfaceHandle = interfaceHandle;
581 pipe.endpoint = endpoint1;
582 pipe.timeout = UINT32_MAX;
583 int32_t returnValue = OH_Usb_SendPipeRequest(&pipe, devMemMap);
584 OH_Usb_DestroyDeviceMemMap(devMemMap);
585 napi_value result = nullptr;
586 napi_create_int32(env, returnValue, &result);
587 return result;
588 }
589
UsbSendPipeRequestTwo(napi_env env, napi_callback_info info)590 static napi_value UsbSendPipeRequestTwo(napi_env env, napi_callback_info info)
591 {
592 size_t argc = PARAM_1;
593 napi_value args[PARAM_1] = {nullptr};
594 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
595 int64_t deviceId64;
596 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
597 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
598 int32_t usbInitReturnValue = OH_Usb_Init();
599 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
600 struct UsbDeviceDescriptor devDesc;
601 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
602 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
603 struct UsbDdkConfigDescriptor *config = nullptr;
604 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
605 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
606 OH_Usb_FreeConfigDescriptor(config);
607 struct UsbDeviceMemMap *devMemMap = nullptr;
608 size_t bufferLen = PARAM_10;
609 int32_t usbCreateDeviceMemMapReturnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
610 NAPI_ASSERT(env, usbCreateDeviceMemMapReturnValue == PARAM_0, "OH_Usb_CreateDeviceMemMap failed");
611 OH_Usb_Release();
612 struct UsbRequestPipe pipe;
613 pipe.interfaceHandle = interfaceHandle;
614 pipe.endpoint = ENDPOINT;
615 pipe.timeout = UINT32_MAX;
616 int32_t returnValue = OH_Usb_SendPipeRequest(&pipe, devMemMap);
617 OH_Usb_DestroyDeviceMemMap(devMemMap);
618 napi_value result = nullptr;
619 napi_create_int32(env, returnValue, &result);
620 return result;
621 }
622
UsbSendPipeRequestThree(napi_env env, napi_callback_info info)623 static napi_value UsbSendPipeRequestThree(napi_env env, napi_callback_info info)
624 {
625 size_t argc = PARAM_1;
626 napi_value args[PARAM_1] = {nullptr};
627 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
628 int64_t deviceId64;
629 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
630 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
631 int32_t usbInitReturnValue = OH_Usb_Init();
632 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
633 struct UsbDeviceDescriptor devDesc;
634 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
635 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
636 struct UsbDdkConfigDescriptor *config = nullptr;
637 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
638 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
639 OH_Usb_FreeConfigDescriptor(config);
640 struct UsbDeviceMemMap *devMemMap = nullptr;
641 size_t bufferLen = PARAM_10;
642 int32_t usbCreateDeviceMemMapReturnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
643 NAPI_ASSERT(env, usbCreateDeviceMemMapReturnValue == PARAM_0, "OH_Usb_CreateDeviceMemMap failed");
644 struct UsbRequestPipe pipe;
645 pipe.interfaceHandle = interfaceHandle;
646 pipe.endpoint = ENDPOINT;
647 pipe.timeout = UINT32_MAX;
648 int32_t returnValue = OH_Usb_SendPipeRequest(nullptr, devMemMap);
649 OH_Usb_DestroyDeviceMemMap(devMemMap);
650 napi_value result = nullptr;
651 napi_create_int32(env, returnValue, &result);
652 return result;
653 }
654
UsbSendPipeRequestFour(napi_env env, napi_callback_info info)655 static napi_value UsbSendPipeRequestFour(napi_env env, napi_callback_info info)
656 {
657 size_t argc = PARAM_1;
658 napi_value args[PARAM_1] = {nullptr};
659 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
660 int64_t deviceId64;
661 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
662 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
663 int32_t usbInitReturnValue = OH_Usb_Init();
664 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
665 struct UsbDeviceDescriptor devDesc;
666 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
667 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
668 struct UsbDdkConfigDescriptor *config = nullptr;
669 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
670 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
671 OH_Usb_FreeConfigDescriptor(config);
672 struct UsbDeviceMemMap *devMemMap = nullptr;
673 size_t bufferLen = PARAM_10;
674 int32_t usbCreateDeviceMemMapReturnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
675 NAPI_ASSERT(env, usbCreateDeviceMemMapReturnValue == PARAM_0, "OH_Usb_CreateDeviceMemMap failed");
676 struct UsbRequestPipe pipe;
677 pipe.interfaceHandle = interfaceHandle;
678 pipe.endpoint = ENDPOINT;
679 pipe.timeout = UINT32_MAX;
680 int32_t returnValue = OH_Usb_SendPipeRequest(&pipe, nullptr);
681 OH_Usb_DestroyDeviceMemMap(devMemMap);
682 napi_value result = nullptr;
683 napi_create_int32(env, returnValue, &result);
684 return result;
685 }
686
UsbCreateDeviceMemMapOne(napi_env env, napi_callback_info info)687 static napi_value UsbCreateDeviceMemMapOne(napi_env env, napi_callback_info info)
688 {
689 size_t argc = PARAM_1;
690 napi_value args[PARAM_1] = {nullptr};
691 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
692 int64_t deviceId64;
693 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
694 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
695 int32_t usbInitReturnValue = OH_Usb_Init();
696 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
697 struct UsbDeviceDescriptor devDesc;
698 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
699 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
700 struct UsbDdkConfigDescriptor *config = nullptr;
701 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
702 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
703 OH_Usb_FreeConfigDescriptor(config);
704 struct UsbDeviceMemMap *devMemMap = nullptr;
705 size_t bufferLen = PARAM_10;
706 int32_t returnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
707 OH_Usb_DestroyDeviceMemMap(devMemMap);
708 napi_value result = nullptr;
709 napi_create_int32(env, returnValue, &result);
710 return result;
711 }
712
UsbCreateDeviceMemMapTwo(napi_env env, napi_callback_info info)713 static napi_value UsbCreateDeviceMemMapTwo(napi_env env, napi_callback_info info)
714 {
715 size_t argc = PARAM_1;
716 napi_value args[PARAM_1] = {nullptr};
717 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
718 int64_t deviceId64;
719 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
720 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
721 int32_t usbInitReturnValue = OH_Usb_Init();
722 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
723 struct UsbDeviceDescriptor devDesc;
724 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
725 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
726 struct UsbDdkConfigDescriptor *config = nullptr;
727 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
728 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
729 OH_Usb_FreeConfigDescriptor(config);
730 size_t bufferLen = PARAM_10;
731 int32_t returnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, nullptr);
732 napi_value result = nullptr;
733 napi_create_int32(env, returnValue, &result);
734 return result;
735 }
736
UsbDestroyDeviceMemMap(napi_env env, napi_callback_info info)737 static napi_value UsbDestroyDeviceMemMap(napi_env env, napi_callback_info info)
738 {
739 size_t argc = PARAM_1;
740 napi_value args[PARAM_1] = {nullptr};
741 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
742 int64_t deviceId64;
743 napi_get_value_int64(env, args[PARAM_0], &deviceId64);
744 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
745 int32_t usbInitReturnValue = OH_Usb_Init();
746 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
747 struct UsbDeviceDescriptor devDesc;
748 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
749 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
750 struct UsbDdkConfigDescriptor *config = nullptr;
751 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
752 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
753 OH_Usb_FreeConfigDescriptor(config);
754 struct UsbDeviceMemMap *devMemMap = nullptr;
755 size_t bufferLen = PARAM_10;
756 OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
757 OH_Usb_DestroyDeviceMemMap(devMemMap);
758 napi_value result = nullptr;
759 napi_create_int32(env, true, &result);
760 return result;
761 }
762
UsbSendPipeRequestWithAshmemOne(napi_env env, napi_callback_info info)763 static napi_value UsbSendPipeRequestWithAshmemOne(napi_env env, napi_callback_info info)
764 {
765 size_t argc = PARAM_1;
766 napi_value args[PARAM_1] = {nullptr};
767 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
768 int64_t deviceId64;
769 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
770 uint64_t deviceId = JsDeviceIdToNative(static_cast<uint64_t>(deviceId64));
771 int32_t usbInitReturnValue = OH_Usb_Init();
772 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
773 struct UsbDeviceDescriptor devDesc;
774 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
775 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
776 struct UsbDdkConfigDescriptor *config = nullptr;
777 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
778 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
779 auto [result1, interface1, endpoint1, maxPktSize1] = GetEndpointInfo(config);
780 OH_Usb_FreeConfigDescriptor(config);
781 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
782 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
783 size_t bufferLen = PARAM_10;
784 const uint8_t name[100] = "TestAshmem";
785 DDK_Ashmem *ashmem = nullptr;
786 int32_t createAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
787 NAPI_ASSERT(env, createAshmemValue == PARAM_0, "OH_DDK_CreateAshmem failed");
788 const uint8_t ashmemMapType = 0x03;
789 int32_t mapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
790 NAPI_ASSERT(env, mapAshmemValue == PARAM_0, "OH_DDK_MapAshmem failed");
791 struct UsbRequestPipe pipe;
792 pipe.interfaceHandle = interfaceHandle;
793 pipe.endpoint = endpoint1;
794 pipe.timeout = UINT32_MAX;
795 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(&pipe, ashmem);
796 OH_DDK_DestroyAshmem(ashmem);
797 napi_value result = nullptr;
798 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
799 return result;
800 }
801
UsbSendPipeRequestWithAshmemTwo(napi_env env, napi_callback_info info)802 static napi_value UsbSendPipeRequestWithAshmemTwo(napi_env env, napi_callback_info info)
803 {
804 size_t argc = PARAM_1;
805 napi_value args[PARAM_1] = {nullptr};
806 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
807 size_t bufferLen = PARAM_10;
808 const uint8_t name[100] = "TestAshmem";
809 DDK_Ashmem *ashmem = nullptr;
810 int32_t createAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
811 NAPI_ASSERT(env, createAshmemValue == PARAM_0, "OH_DDK_CreateAshmem failed");
812 const uint8_t ashmemMapType = 0x03;
813 int32_t mapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
814 NAPI_ASSERT(env, mapAshmemValue == PARAM_0, "OH_DDK_MapAshmem failed");
815 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(nullptr, ashmem);
816 OH_DDK_DestroyAshmem(ashmem);
817 napi_value result = nullptr;
818 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
819 return result;
820 }
821
UsbSendPipeRequestWithAshmemThree(napi_env env, napi_callback_info info)822 static napi_value UsbSendPipeRequestWithAshmemThree(napi_env env, napi_callback_info info)
823 {
824 size_t argc = PARAM_1;
825 napi_value args[PARAM_1] = {nullptr};
826 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
827 int64_t deviceId64;
828 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
829 uint64_t deviceId = JsDeviceIdToNative(static_cast<uint64_t>(deviceId64));
830 int32_t usbInitReturnValue = OH_Usb_Init();
831 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
832 struct UsbDeviceDescriptor devDesc;
833 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
834 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
835 struct UsbDdkConfigDescriptor *config = nullptr;
836 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
837 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
838 auto [result1, interface1, endpoint1, maxPktSize1] = GetEndpointInfo(config);
839 OH_Usb_FreeConfigDescriptor(config);
840 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
841 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
842 struct UsbRequestPipe pipe;
843 pipe.interfaceHandle = interfaceHandle;
844 pipe.endpoint = endpoint1;
845 pipe.timeout = UINT32_MAX;
846 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(&pipe, nullptr);
847 napi_value result = nullptr;
848 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
849 return result;
850 }
851
UsbSendPipeRequestWithAshmemFour(napi_env env, napi_callback_info info)852 static napi_value UsbSendPipeRequestWithAshmemFour(napi_env env, napi_callback_info info)
853 {
854 size_t argc = PARAM_1;
855 napi_value args[PARAM_1] = {nullptr};
856 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
857 int64_t deviceId64;
858 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
859 uint64_t deviceId = JsDeviceIdToNative(static_cast<uint64_t>(deviceId64));
860 int32_t usbInitReturnValue = OH_Usb_Init();
861 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
862 struct UsbDeviceDescriptor devDesc;
863 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
864 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
865 struct UsbDdkConfigDescriptor *config = nullptr;
866 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, configIndex, &config);
867 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
868 OH_Usb_FreeConfigDescriptor(config);
869 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
870 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
871 OH_Usb_Release();
872 size_t bufferLen = PARAM_10;
873 const uint8_t name[100] = "TestAshmem";
874 DDK_Ashmem *ashmem = nullptr;
875 int32_t createAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
876 NAPI_ASSERT(env, createAshmemValue == PARAM_0, "OH_DDK_CreateAshmem failed");
877 const uint8_t ashmemMapType = 0x03;
878 int32_t mapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
879 NAPI_ASSERT(env, mapAshmemValue == PARAM_0, "OH_DDK_MapAshmem failed");
880 struct UsbRequestPipe pipe;
881 pipe.interfaceHandle = interfaceHandle;
882 pipe.endpoint = ENDPOINT;
883 pipe.timeout = UINT32_MAX;
884 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(&pipe, ashmem);
885 OH_DDK_DestroyAshmem(ashmem);
886 napi_value result = nullptr;
887 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
888 return result;
889 }
890
891 EXTERN_C_START
Init(napi_env env, napi_value exports)892 static napi_value Init(napi_env env, napi_value exports)
893 {
894 napi_property_descriptor desc[] = {
895 {"usbInit", nullptr, UsbInit, nullptr, nullptr, nullptr, napi_default, nullptr},
896 {"usbRelease", nullptr, UsbRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
897 {"usbGetDeviceDescriptorOne", nullptr, UsbGetDeviceDescriptorOne, nullptr, nullptr, nullptr, napi_default,
898 nullptr},
899 {"usbGetDeviceDescriptorTwo", nullptr, UsbGetDeviceDescriptorTwo, nullptr, nullptr, nullptr, napi_default,
900 nullptr},
901 {"usbGetConfigDescriptorOne", nullptr, UsbGetConfigDescriptorOne, nullptr, nullptr, nullptr, napi_default,
902 nullptr},
903 {"usbGetConfigDescriptorTwo", nullptr, UsbGetConfigDescriptorTwo, nullptr, nullptr, nullptr, napi_default,
904 nullptr},
905 {"usbGetConfigDescriptorThree", nullptr, UsbGetConfigDescriptorThree, nullptr, nullptr, nullptr, napi_default,
906 nullptr},
907 {"usbFreeConfigDescriptor", nullptr, UsbFreeConfigDescriptor, nullptr, nullptr, nullptr, napi_default,
908 nullptr},
909 {"usbClaimInterfaceOne", nullptr, UsbClaimInterfaceOne, nullptr, nullptr, nullptr, napi_default, nullptr},
910 {"usbClaimInterfaceTwo", nullptr, UsbClaimInterfaceTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
911 {"usbClaimInterfaceThree", nullptr, UsbClaimInterfaceThree, nullptr, nullptr, nullptr, napi_default, nullptr},
912 {"usbReleaseInterface", nullptr, UsbReleaseInterface, nullptr, nullptr, nullptr, napi_default, nullptr},
913 {"usbSelectInterfaceSettingOne", nullptr, UsbSelectInterfaceSettingOne, nullptr, nullptr, nullptr,
914 napi_default, nullptr},
915 {"usbSelectInterfaceSettingTwo", nullptr, UsbSelectInterfaceSettingTwo, nullptr, nullptr, nullptr,
916 napi_default, nullptr},
917 {"usbGetCurrentInterfaceSettingOne", nullptr, UsbGetCurrentInterfaceSettingOne, nullptr, nullptr, nullptr,
918 napi_default, nullptr},
919 {"usbGetCurrentInterfaceSettingTwo", nullptr, UsbGetCurrentInterfaceSettingTwo, nullptr, nullptr, nullptr,
920 napi_default, nullptr},
921 {"usbGetCurrentInterfaceSettingThree", nullptr, UsbGetCurrentInterfaceSettingThree, nullptr, nullptr, nullptr,
922 napi_default, nullptr},
923 {"usbSendControlReadRequestOne", nullptr, UsbSendControlReadRequestOne, nullptr, nullptr, nullptr,
924 napi_default, nullptr},
925 {"usbSendControlReadRequestTwo", nullptr, UsbSendControlReadRequestTwo, nullptr, nullptr, nullptr,
926 napi_default, nullptr},
927 {"usbSendControlReadRequestThree", nullptr, UsbSendControlReadRequestThree, nullptr, nullptr, nullptr,
928 napi_default, nullptr},
929 {"usbSendControlReadRequestFour", nullptr, UsbSendControlReadRequestFour, nullptr, nullptr, nullptr,
930 napi_default, nullptr},
931 {"usbSendControlReadRequestFive", nullptr, UsbSendControlReadRequestFive, nullptr, nullptr, nullptr,
932 napi_default, nullptr},
933 {"usbSendControlWriteRequestOne", nullptr, UsbSendControlWriteRequestOne, nullptr, nullptr, nullptr,
934 napi_default, nullptr},
935 {"usbSendControlWriteRequestTwo", nullptr, UsbSendControlWriteRequestTwo, nullptr, nullptr, nullptr,
936 napi_default, nullptr},
937 {"usbSendControlWriteRequestThree", nullptr, UsbSendControlWriteRequestThree, nullptr, nullptr, nullptr,
938 napi_default, nullptr},
939 {"usbSendControlWriteRequestFour", nullptr, UsbSendControlWriteRequestFour, nullptr, nullptr, nullptr,
940 napi_default, nullptr},
941 {"usbSendPipeRequestOne", nullptr, UsbSendPipeRequestOne, nullptr, nullptr, nullptr, napi_default, nullptr},
942 {"usbSendPipeRequestTwo", nullptr, UsbSendPipeRequestTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
943 {"usbSendPipeRequestThree", nullptr, UsbSendPipeRequestThree, nullptr, nullptr, nullptr, napi_default,
944 nullptr},
945 {"usbSendPipeRequestFour", nullptr, UsbSendPipeRequestFour, nullptr, nullptr, nullptr, napi_default, nullptr},
946 {"usbCreateDeviceMemMapOne", nullptr, UsbCreateDeviceMemMapOne, nullptr, nullptr, nullptr, napi_default,
947 nullptr},
948 {"usbCreateDeviceMemMapTwo", nullptr, UsbCreateDeviceMemMapTwo, nullptr, nullptr, nullptr, napi_default,
949 nullptr},
950 {"usbDestroyDeviceMemMap", nullptr, UsbDestroyDeviceMemMap, nullptr, nullptr, nullptr, napi_default, nullptr},
951 {"usbSendPipeRequestWithAshmemOne", nullptr, UsbSendPipeRequestWithAshmemOne, nullptr, nullptr, nullptr,
952 napi_default, nullptr},
953 {"usbSendPipeRequestWithAshmemTwo", nullptr, UsbSendPipeRequestWithAshmemTwo, nullptr, nullptr, nullptr,
954 napi_default, nullptr},
955 {"usbSendPipeRequestWithAshmemThree", nullptr, UsbSendPipeRequestWithAshmemThree, nullptr, nullptr, nullptr,
956 napi_default, nullptr},
957 {"usbSendPipeRequestWithAshmemFour", nullptr, UsbSendPipeRequestWithAshmemFour, nullptr, nullptr, nullptr,
958 napi_default, nullptr}
959 };
960
961 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
962 return exports;
963 }
964 EXTERN_C_END
965
966 static napi_module demoModule = {
967 .nm_version = 1,
968 .nm_flags = 0,
969 .nm_filename = nullptr,
970 .nm_register_func = Init,
971 .nm_modname = "libusbddk",
972 .nm_priv = ((void *)0),
973 .reserved = {0},
974 };
975
RegisterModule(void)976 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
977