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 "napi/native_common.h"
18 #include "usb_ddk_api.h"
19 #include "usb_ddk_types.h"
20 #include "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 g_configIndex = 0;
40 static uint8_t g_interfaceIndex = 0;
41 static uint64_t g_interfaceHandle = 0;
42 static uint8_t g_settingIndex = 0;
43 static uint32_t g_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 continue;
62 }
63 return { true, intDesc[setIdx].interfaceDescriptor.bInterfaceNumber,
64 epDesc[epIdx].endpointDescriptor.bEndpointAddress, epDesc[epIdx].endpointDescriptor.wMaxPacketSize };
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_CALL(env, 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_CALL(env, napi_get_boolean(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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
106 int64_t deviceId64;
107 NAPI_CALL(env, 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_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
124 int64_t deviceId64;
125 NAPI_CALL(env, 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_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
140 int64_t deviceId64;
141 NAPI_CALL(env, 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, g_configIndex, &config);
147 OH_Usb_FreeConfigDescriptor(config);
148 napi_value result = nullptr;
149 NAPI_CALL(env, 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, g_configIndex, &config);
158 OH_Usb_FreeConfigDescriptor(config);
159 napi_value result = nullptr;
160 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
169 int64_t deviceId64;
170 NAPI_CALL(env, 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, g_configIndex, nullptr);
176 napi_value result = nullptr;
177 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
186 int64_t deviceId64;
187 NAPI_CALL(env, 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, g_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_CALL(env, napi_get_boolean(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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
205 int64_t deviceId64;
206 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
211 napi_value result = nullptr;
212 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
220 napi_value result = nullptr;
221 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
230 int64_t deviceId64;
231 NAPI_CALL(env, 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, g_interfaceIndex, nullptr);
236 napi_value result = nullptr;
237 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
246 int64_t deviceId64;
247 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
252 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
253 int32_t returnValue = OH_Usb_ReleaseInterface(g_interfaceHandle);
254 napi_value result = nullptr;
255 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
264 int64_t deviceId64;
265 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
270 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
271 int32_t returnValue = OH_Usb_SelectInterfaceSetting(g_interfaceHandle, g_settingIndex);
272 napi_value result = nullptr;
273 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
282 int64_t deviceId64;
283 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
288 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
289 OH_Usb_Release();
290 int32_t returnValue = OH_Usb_SelectInterfaceSetting(g_interfaceHandle, g_settingIndex);
291 napi_value result = nullptr;
292 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
301 int64_t deviceId64;
302 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
307 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "OH_Usb_ClaimInterface failed");
308 int32_t usbSelectInterfaceSettingReturnValue = OH_Usb_SelectInterfaceSetting(g_interfaceHandle, g_settingIndex);
309 NAPI_ASSERT(env, usbSelectInterfaceSettingReturnValue == PARAM_0, "OH_Usb_SelectInterfaceSetting failed");
310 int32_t returnValue = OH_Usb_GetCurrentInterfaceSetting(g_interfaceHandle, &g_settingIndex);
311 napi_value result = nullptr;
312 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
321 int64_t deviceId64;
322 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
327 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "OH_Usb_ClaimInterface failed");
328 int32_t usbSelectInterfaceSettingReturnValue = OH_Usb_SelectInterfaceSetting(g_interfaceHandle, g_settingIndex);
329 NAPI_ASSERT(env, usbSelectInterfaceSettingReturnValue == PARAM_0, "OH_Usb_SelectInterfaceSetting failed");
330 OH_Usb_Release();
331 int32_t returnValue = OH_Usb_GetCurrentInterfaceSetting(g_interfaceHandle, &g_settingIndex);
332 napi_value result = nullptr;
333 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
342 int64_t deviceId64;
343 NAPI_CALL(env, 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, g_interfaceIndex, &g_interfaceHandle);
348 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
349 int32_t usbSelectInterfaceSettingReturnValue = OH_Usb_SelectInterfaceSetting(g_interfaceHandle, g_settingIndex);
350 NAPI_ASSERT(env, usbSelectInterfaceSettingReturnValue == PARAM_0, "Usb_ClaimInterface failed");
351 int32_t returnValue = OH_Usb_GetCurrentInterfaceSetting(g_interfaceHandle, nullptr);
352 napi_value result = nullptr;
353 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
362 int64_t deviceId64;
363 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, &setup, UINT32_MAX, data, &dataLen);
378 napi_value result = nullptr;
379 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
388 int64_t deviceId64;
389 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, &setup, g_timeout, data, &dataLen);
400 napi_value result = nullptr;
401 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
410 int64_t deviceId64;
411 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, nullptr, g_timeout, data, &dataLen);
420 napi_value result = nullptr;
421 NAPI_CALL(env, 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_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, &setup, g_timeout, nullptr, &dataLen);
440 napi_value result = nullptr;
441 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
450 int64_t deviceId64;
451 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, &setup, g_timeout, &data, nullptr);
460 napi_value result = nullptr;
461 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
470 int64_t deviceId64;
471 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, &setup, g_timeout, &data, dataLen);
481 napi_value result = nullptr;
482 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
491 int64_t deviceId64;
492 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, &setup, g_timeout, &data, dataLen);
503 napi_value result = nullptr;
504 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
513 int64_t deviceId64;
514 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, nullptr, g_timeout, &data, dataLen);
523 napi_value result = nullptr;
524 NAPI_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
533 int64_t deviceId64;
534 NAPI_CALL(env, 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, g_interfaceIndex, &g_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(g_interfaceHandle, &setup, g_timeout, nullptr, dataLen);
543 napi_value result = nullptr;
544 NAPI_CALL(env, 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 = static_cast<uint32_t>(deviceId >> PARAM_48);
550 uint32_t devNum = static_cast<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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
559 int64_t deviceId64;
560 NAPI_CALL(env, 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, g_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, g_interfaceIndex, &g_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 = g_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_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
595 int64_t deviceId64;
596 NAPI_CALL(env, 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, g_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 = g_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_CALL(env, 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_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
628 int64_t deviceId64;
629 NAPI_CALL(env, 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, g_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 int32_t returnValue = OH_Usb_SendPipeRequest(nullptr, devMemMap);
645 OH_Usb_DestroyDeviceMemMap(devMemMap);
646 napi_value result = nullptr;
647 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
648 return result;
649 }
650
UsbSendPipeRequestFour(napi_env env,napi_callback_info info)651 static napi_value UsbSendPipeRequestFour(napi_env env, napi_callback_info info)
652 {
653 size_t argc = PARAM_1;
654 napi_value args[PARAM_1] = {nullptr};
655 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
656 int64_t deviceId64;
657 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
658 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
659 int32_t usbInitReturnValue = OH_Usb_Init();
660 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
661 struct UsbDeviceDescriptor devDesc;
662 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
663 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
664 struct UsbDdkConfigDescriptor *config = nullptr;
665 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, g_configIndex, &config);
666 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
667 OH_Usb_FreeConfigDescriptor(config);
668 struct UsbDeviceMemMap *devMemMap = nullptr;
669 size_t bufferLen = PARAM_10;
670 int32_t usbCreateDeviceMemMapReturnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
671 NAPI_ASSERT(env, usbCreateDeviceMemMapReturnValue == PARAM_0, "OH_Usb_CreateDeviceMemMap failed");
672 struct UsbRequestPipe pipe;
673 pipe.interfaceHandle = g_interfaceHandle;
674 pipe.endpoint = ENDPOINT;
675 pipe.timeout = UINT32_MAX;
676 int32_t returnValue = OH_Usb_SendPipeRequest(&pipe, nullptr);
677 OH_Usb_DestroyDeviceMemMap(devMemMap);
678 napi_value result = nullptr;
679 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
680 return result;
681 }
682
UsbCreateDeviceMemMapOne(napi_env env,napi_callback_info info)683 static napi_value UsbCreateDeviceMemMapOne(napi_env env, napi_callback_info info)
684 {
685 size_t argc = PARAM_1;
686 napi_value args[PARAM_1] = {nullptr};
687 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
688 int64_t deviceId64;
689 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
690 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
691 int32_t usbInitReturnValue = OH_Usb_Init();
692 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
693 struct UsbDeviceDescriptor devDesc;
694 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
695 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
696 struct UsbDdkConfigDescriptor *config = nullptr;
697 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, g_configIndex, &config);
698 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
699 OH_Usb_FreeConfigDescriptor(config);
700 struct UsbDeviceMemMap *devMemMap = nullptr;
701 size_t bufferLen = PARAM_10;
702 int32_t returnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
703 OH_Usb_DestroyDeviceMemMap(devMemMap);
704 napi_value result = nullptr;
705 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
706 return result;
707 }
708
UsbCreateDeviceMemMapTwo(napi_env env,napi_callback_info info)709 static napi_value UsbCreateDeviceMemMapTwo(napi_env env, napi_callback_info info)
710 {
711 size_t argc = PARAM_1;
712 napi_value args[PARAM_1] = {nullptr};
713 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
714 int64_t deviceId64;
715 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
716 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
717 int32_t usbInitReturnValue = OH_Usb_Init();
718 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
719 struct UsbDeviceDescriptor devDesc;
720 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
721 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
722 struct UsbDdkConfigDescriptor *config = nullptr;
723 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, g_configIndex, &config);
724 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
725 OH_Usb_FreeConfigDescriptor(config);
726 size_t bufferLen = PARAM_10;
727 int32_t returnValue = OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, nullptr);
728 napi_value result = nullptr;
729 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
730 return result;
731 }
732
UsbDestroyDeviceMemMap(napi_env env,napi_callback_info info)733 static napi_value UsbDestroyDeviceMemMap(napi_env env, napi_callback_info info)
734 {
735 size_t argc = PARAM_1;
736 napi_value args[PARAM_1] = {nullptr};
737 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
738 int64_t deviceId64;
739 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
740 uint64_t deviceId = static_cast<uint64_t>(deviceId64);
741 int32_t usbInitReturnValue = OH_Usb_Init();
742 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
743 struct UsbDeviceDescriptor devDesc;
744 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
745 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
746 struct UsbDdkConfigDescriptor *config = nullptr;
747 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, g_configIndex, &config);
748 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
749 OH_Usb_FreeConfigDescriptor(config);
750 struct UsbDeviceMemMap *devMemMap = nullptr;
751 size_t bufferLen = PARAM_10;
752 OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMemMap);
753 OH_Usb_DestroyDeviceMemMap(devMemMap);
754 napi_value result = nullptr;
755 NAPI_CALL(env, napi_get_boolean(env, true, &result));
756 return result;
757 }
758
UsbSendPipeRequestWithAshmemOne(napi_env env,napi_callback_info info)759 static napi_value UsbSendPipeRequestWithAshmemOne(napi_env env, napi_callback_info info)
760 {
761 size_t argc = PARAM_1;
762 napi_value args[PARAM_1] = {nullptr};
763 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
764 int64_t deviceId64;
765 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
766 uint64_t deviceId = JsDeviceIdToNative(static_cast<uint64_t>(deviceId64));
767 int32_t usbInitReturnValue = OH_Usb_Init();
768 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
769 struct UsbDeviceDescriptor devDesc;
770 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
771 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
772 struct UsbDdkConfigDescriptor *config = nullptr;
773 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, g_configIndex, &config);
774 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
775 auto [result1, interface1, endpoint1, maxPktSize1] = GetEndpointInfo(config);
776 OH_Usb_FreeConfigDescriptor(config);
777 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, g_interfaceIndex, &g_interfaceHandle);
778 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
779 size_t bufferLen = PARAM_10;
780 const uint8_t name[100] = "TestAshmem";
781 DDK_Ashmem *ashmem = nullptr;
782 int32_t createAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
783 NAPI_ASSERT(env, createAshmemValue == PARAM_0, "OH_DDK_CreateAshmem failed");
784 const uint8_t ashmemMapType = 0x03;
785 int32_t mapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
786 NAPI_ASSERT(env, mapAshmemValue == PARAM_0, "OH_DDK_MapAshmem failed");
787 struct UsbRequestPipe pipe;
788 pipe.interfaceHandle = g_interfaceHandle;
789 pipe.endpoint = endpoint1;
790 pipe.timeout = UINT32_MAX;
791 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(&pipe, ashmem);
792 OH_DDK_DestroyAshmem(ashmem);
793 napi_value result = nullptr;
794 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
795 return result;
796 }
797
UsbSendPipeRequestWithAshmemTwo(napi_env env,napi_callback_info info)798 static napi_value UsbSendPipeRequestWithAshmemTwo(napi_env env, napi_callback_info info)
799 {
800 size_t argc = PARAM_1;
801 napi_value args[PARAM_1] = {nullptr};
802 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
803 size_t bufferLen = PARAM_10;
804 const uint8_t name[100] = "TestAshmem";
805 DDK_Ashmem *ashmem = nullptr;
806 int32_t createAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
807 NAPI_ASSERT(env, createAshmemValue == PARAM_0, "OH_DDK_CreateAshmem failed");
808 const uint8_t ashmemMapType = 0x03;
809 int32_t mapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
810 NAPI_ASSERT(env, mapAshmemValue == PARAM_0, "OH_DDK_MapAshmem failed");
811 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(nullptr, ashmem);
812 OH_DDK_DestroyAshmem(ashmem);
813 napi_value result = nullptr;
814 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
815 return result;
816 }
817
UsbSendPipeRequestWithAshmemThree(napi_env env,napi_callback_info info)818 static napi_value UsbSendPipeRequestWithAshmemThree(napi_env env, napi_callback_info info)
819 {
820 size_t argc = PARAM_1;
821 napi_value args[PARAM_1] = {nullptr};
822 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
823 int64_t deviceId64;
824 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
825 uint64_t deviceId = JsDeviceIdToNative(static_cast<uint64_t>(deviceId64));
826 int32_t usbInitReturnValue = OH_Usb_Init();
827 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
828 struct UsbDeviceDescriptor devDesc;
829 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
830 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
831 struct UsbDdkConfigDescriptor *config = nullptr;
832 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, g_configIndex, &config);
833 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
834 auto [result1, interface1, endpoint1, maxPktSize1] = GetEndpointInfo(config);
835 OH_Usb_FreeConfigDescriptor(config);
836 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, g_interfaceIndex, &g_interfaceHandle);
837 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
838 struct UsbRequestPipe pipe;
839 pipe.interfaceHandle = g_interfaceHandle;
840 pipe.endpoint = endpoint1;
841 pipe.timeout = UINT32_MAX;
842 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(&pipe, nullptr);
843 napi_value result = nullptr;
844 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
845 return result;
846 }
847
UsbSendPipeRequestWithAshmemFour(napi_env env,napi_callback_info info)848 static napi_value UsbSendPipeRequestWithAshmemFour(napi_env env, napi_callback_info info)
849 {
850 size_t argc = PARAM_1;
851 napi_value args[PARAM_1] = {nullptr};
852 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
853 int64_t deviceId64;
854 NAPI_CALL(env, napi_get_value_int64(env, args[PARAM_0], &deviceId64));
855 uint64_t deviceId = JsDeviceIdToNative(static_cast<uint64_t>(deviceId64));
856 int32_t usbInitReturnValue = OH_Usb_Init();
857 NAPI_ASSERT(env, usbInitReturnValue == PARAM_0, "OH_Usb_Init failed");
858 struct UsbDeviceDescriptor devDesc;
859 int32_t usbGetDeviceDescriptorReturnValue = OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
860 NAPI_ASSERT(env, usbGetDeviceDescriptorReturnValue == PARAM_0, "OH_Usb_GetDeviceDescriptor failed");
861 struct UsbDdkConfigDescriptor *config = nullptr;
862 int32_t usbGetConfigDescriptorReturnValue = OH_Usb_GetConfigDescriptor(deviceId, g_configIndex, &config);
863 NAPI_ASSERT(env, usbGetConfigDescriptorReturnValue == PARAM_0, "OH_Usb_GetConfigDescriptor failed");
864 OH_Usb_FreeConfigDescriptor(config);
865 int32_t usbClaimInterfaceValue = OH_Usb_ClaimInterface(deviceId, g_interfaceIndex, &g_interfaceHandle);
866 NAPI_ASSERT(env, usbClaimInterfaceValue == PARAM_0, "Usb_ClaimInterface failed");
867 OH_Usb_Release();
868 size_t bufferLen = PARAM_10;
869 const uint8_t name[100] = "TestAshmem";
870 DDK_Ashmem *ashmem = nullptr;
871 int32_t createAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
872 NAPI_ASSERT(env, createAshmemValue == PARAM_0, "OH_DDK_CreateAshmem failed");
873 const uint8_t ashmemMapType = 0x03;
874 int32_t mapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
875 NAPI_ASSERT(env, mapAshmemValue == PARAM_0, "OH_DDK_MapAshmem failed");
876 struct UsbRequestPipe pipe;
877 pipe.interfaceHandle = g_interfaceHandle;
878 pipe.endpoint = ENDPOINT;
879 pipe.timeout = UINT32_MAX;
880 int32_t returnValue = OH_Usb_SendPipeRequestWithAshmem(&pipe, ashmem);
881 OH_DDK_DestroyAshmem(ashmem);
882 napi_value result = nullptr;
883 NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
884 return result;
885 }
886
887 EXTERN_C_START
Init(napi_env env,napi_value exports)888 static napi_value Init(napi_env env, napi_value exports)
889 {
890 napi_property_descriptor desc[] = {
891 DECLARE_NAPI_FUNCTION("usbInit", UsbInit),
892 DECLARE_NAPI_FUNCTION("usbRelease", UsbRelease),
893 DECLARE_NAPI_FUNCTION("usbGetDeviceDescriptorOne", UsbGetDeviceDescriptorOne),
894 DECLARE_NAPI_FUNCTION("usbGetDeviceDescriptorTwo", UsbGetDeviceDescriptorTwo),
895 DECLARE_NAPI_FUNCTION("usbGetConfigDescriptorOne", UsbGetConfigDescriptorOne),
896 DECLARE_NAPI_FUNCTION("usbGetConfigDescriptorTwo", UsbGetConfigDescriptorTwo),
897 DECLARE_NAPI_FUNCTION("usbGetConfigDescriptorThree", UsbGetConfigDescriptorThree),
898 DECLARE_NAPI_FUNCTION("usbFreeConfigDescriptor", UsbFreeConfigDescriptor),
899 DECLARE_NAPI_FUNCTION("usbClaimInterfaceOne", UsbClaimInterfaceOne),
900 DECLARE_NAPI_FUNCTION("usbClaimInterfaceTwo", UsbClaimInterfaceTwo),
901 DECLARE_NAPI_FUNCTION("usbClaimInterfaceThree", UsbClaimInterfaceThree),
902 DECLARE_NAPI_FUNCTION("usbReleaseInterface", UsbReleaseInterface),
903 DECLARE_NAPI_FUNCTION("usbSelectInterfaceSettingOne", UsbSelectInterfaceSettingOne),
904 DECLARE_NAPI_FUNCTION("usbSelectInterfaceSettingTwo", UsbSelectInterfaceSettingTwo),
905 DECLARE_NAPI_FUNCTION("usbGetCurrentInterfaceSettingOne", UsbGetCurrentInterfaceSettingOne),
906 DECLARE_NAPI_FUNCTION("usbGetCurrentInterfaceSettingTwo", UsbGetCurrentInterfaceSettingTwo),
907 DECLARE_NAPI_FUNCTION("usbGetCurrentInterfaceSettingThree", UsbGetCurrentInterfaceSettingThree),
908 DECLARE_NAPI_FUNCTION("usbSendControlReadRequestOne", UsbSendControlReadRequestOne),
909 DECLARE_NAPI_FUNCTION("usbSendControlReadRequestTwo", UsbSendControlReadRequestTwo),
910 DECLARE_NAPI_FUNCTION("usbSendControlReadRequestThree", UsbSendControlReadRequestThree),
911 DECLARE_NAPI_FUNCTION("usbSendControlReadRequestFour", UsbSendControlReadRequestFour),
912 DECLARE_NAPI_FUNCTION("usbSendControlReadRequestFive", UsbSendControlReadRequestFive),
913 DECLARE_NAPI_FUNCTION("usbSendControlWriteRequestOne", UsbSendControlWriteRequestOne),
914 DECLARE_NAPI_FUNCTION("usbSendControlWriteRequestTwo", UsbSendControlWriteRequestTwo),
915 DECLARE_NAPI_FUNCTION("usbSendControlWriteRequestThree", UsbSendControlWriteRequestThree),
916 DECLARE_NAPI_FUNCTION("usbSendControlWriteRequestFour", UsbSendControlWriteRequestFour),
917 DECLARE_NAPI_FUNCTION("usbSendPipeRequestOne", UsbSendPipeRequestOne),
918 DECLARE_NAPI_FUNCTION("usbSendPipeRequestTwo", UsbSendPipeRequestTwo),
919 DECLARE_NAPI_FUNCTION("usbSendPipeRequestThree", UsbSendPipeRequestThree),
920 DECLARE_NAPI_FUNCTION("usbSendPipeRequestFour", UsbSendPipeRequestFour),
921 DECLARE_NAPI_FUNCTION("usbCreateDeviceMemMapOne", UsbCreateDeviceMemMapOne),
922 DECLARE_NAPI_FUNCTION("usbCreateDeviceMemMapTwo", UsbCreateDeviceMemMapTwo),
923 DECLARE_NAPI_FUNCTION("usbDestroyDeviceMemMap", UsbDestroyDeviceMemMap),
924 DECLARE_NAPI_FUNCTION("usbSendPipeRequestWithAshmemOne", UsbSendPipeRequestWithAshmemOne),
925 DECLARE_NAPI_FUNCTION("usbSendPipeRequestWithAshmemTwo", UsbSendPipeRequestWithAshmemTwo),
926 DECLARE_NAPI_FUNCTION("usbSendPipeRequestWithAshmemThree", UsbSendPipeRequestWithAshmemThree),
927 DECLARE_NAPI_FUNCTION("usbSendPipeRequestWithAshmemFour", UsbSendPipeRequestWithAshmemFour),
928 };
929
930 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
931 return exports;
932 }
933 EXTERN_C_END
934
935 static napi_module demoModule = {
936 .nm_version = 1,
937 .nm_flags = 0,
938 .nm_filename = nullptr,
939 .nm_register_func = Init,
940 .nm_modname = "libusb_ddk_js_test",
941 .nm_priv = ((void *)0),
942 .reserved = { 0 },
943 };
944
RegisterModule(void)945 extern "C" __attribute__((constructor)) void RegisterModule(void)
946 {
947 napi_module_register(&demoModule);
948 }
949