1 /*
2 * Copyright (c) 2021-2023 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 <unistd.h>
17
18 #include <sys/time.h>
19
20 #include <cstdio>
21 #include <cstdlib>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25
26 #include "hilog_wrapper.h"
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29 #include "napi_common.h"
30 #include "napi_util.h"
31 #include "securec.h"
32 #include "usb_async_context.h"
33 #include "usb_device_pipe.h"
34 #include "usb_endpoint.h"
35 #include "usb_errors.h"
36 #include "usb_napi_errors.h"
37 #include "usb_srv_client.h"
38
39 using namespace OHOS;
40 using namespace OHOS::USB;
41 using namespace OHOS::HDI::Usb::V1_0;
42
43 static constexpr int32_t INDEX_0 = 0;
44 static constexpr int32_t INDEX_1 = 1;
45 static constexpr int32_t INDEX_2 = 2;
46 static constexpr int32_t INDEX_3 = 3;
47 static constexpr int32_t PARAM_COUNT_0 = 0;
48 static constexpr int32_t PARAM_COUNT_1 = 1;
49 static constexpr int32_t PARAM_COUNT_2 = 2;
50 static constexpr int32_t PARAM_COUNT_3 = 3;
51 static constexpr int32_t PARAM_COUNT_4 = 4;
52 static constexpr int32_t STR_DEFAULT_SIZE = 256;
53 static constexpr int32_t DEFAULT_DESCRIPTION_SIZE = 32;
54
ParseUsbDevicePipe(const napi_env env,const napi_value & obj,USBDevicePipe & pipe)55 static void ParseUsbDevicePipe(const napi_env env, const napi_value &obj, USBDevicePipe &pipe)
56 {
57 napi_valuetype valueType;
58 napi_typeof(env, obj, &valueType);
59 USB_ASSERT_RETURN_VOID(
60 env, valueType == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
61
62 int32_t busNum = 0;
63 NapiUtil::JsObjectToInt(env, obj, "busNum", busNum);
64 pipe.SetBusNum(static_cast<uint8_t>(busNum));
65 int32_t devAddr = 0;
66 NapiUtil::JsObjectToInt(env, obj, "devAddress", devAddr);
67 pipe.SetDevAddr(static_cast<uint8_t>(devAddr));
68 }
69
ProcessPromise(const napi_env env,const USBAsyncContext & asyncContext,napi_value & result)70 static void ProcessPromise(const napi_env env, const USBAsyncContext &asyncContext, napi_value &result)
71 {
72 if (asyncContext.deferred) {
73 if (asyncContext.status == napi_ok) {
74 napi_resolve_deferred(env, asyncContext.deferred, result);
75 } else {
76 napi_reject_deferred(env, asyncContext.deferred, result);
77 }
78 }
79 }
80
CreateUsbDevicePipe(const napi_env env,napi_value & obj,const USBDevicePipe & pipe)81 static void CreateUsbDevicePipe(const napi_env env, napi_value &obj, const USBDevicePipe &pipe)
82 {
83 napi_create_object(env, &obj);
84 NapiUtil::SetValueInt32(env, "busNum", pipe.GetBusNum(), obj);
85 NapiUtil::SetValueInt32(env, "devAddress", pipe.GetDevAddr(), obj);
86 }
87
CtoJSUsbEndpoint(const napi_env & env,napi_value & obj,const USBEndpoint & usbEndpoint)88 static void CtoJSUsbEndpoint(const napi_env &env, napi_value &obj, const USBEndpoint &usbEndpoint)
89 {
90 napi_create_object(env, &obj);
91 NapiUtil::SetValueUint32(env, "address", usbEndpoint.GetAddress(), obj);
92 NapiUtil::SetValueUint32(env, "attributes", usbEndpoint.GetAttributes(), obj);
93 NapiUtil::SetValueInt32(env, "interval", usbEndpoint.GetInterval(), obj);
94 NapiUtil::SetValueInt32(env, "maxPacketSize", usbEndpoint.GetMaxPacketSize(), obj);
95 NapiUtil::SetValueUint32(env, "direction", usbEndpoint.GetDirection(), obj);
96 NapiUtil::SetValueUint32(env, "number", usbEndpoint.GetEndpointNumber(), obj);
97 NapiUtil::SetValueUint32(env, "type", usbEndpoint.GetType(), obj);
98 NapiUtil::SetValueInt32(env, "interfaceId", usbEndpoint.GetInterfaceId(), obj);
99 }
100
CtoJSUsbInterface(const napi_env & env,napi_value & obj,const UsbInterface & usbInterface)101 static void CtoJSUsbInterface(const napi_env &env, napi_value &obj, const UsbInterface &usbInterface)
102 {
103 napi_create_object(env, &obj);
104 NapiUtil::SetValueInt32(env, "id", usbInterface.GetId(), obj);
105 NapiUtil::SetValueInt32(env, "protocol", usbInterface.GetProtocol(), obj);
106 NapiUtil::SetValueInt32(env, "clazz", usbInterface.GetClass(), obj);
107 NapiUtil::SetValueInt32(env, "subClass", usbInterface.GetSubClass(), obj);
108 NapiUtil::SetValueInt32(env, "alternateSetting", usbInterface.GetAlternateSetting(), obj);
109 NapiUtil::SetValueUtf8String(env, "name", usbInterface.GetName(), obj);
110
111 napi_value arr;
112 napi_create_array(env, &arr);
113 for (int32_t i = 0; i < usbInterface.GetEndpointCount(); ++i) {
114 auto usbEndpoint = usbInterface.GetEndpoint(i);
115 if (!usbEndpoint.has_value()) {
116 USB_HILOGE(MODULE_JS_NAPI, "GetEndpoint failed i=%{public}d", i);
117 return;
118 }
119
120 napi_value objTmp;
121 CtoJSUsbEndpoint(env, objTmp, usbEndpoint.value());
122 napi_set_element(env, arr, i, objTmp);
123 }
124 napi_set_named_property(env, obj, "endpoints", arr);
125 }
126
CtoJSUsbConfig(const napi_env & env,napi_value & obj,const USBConfig & usbConfig)127 static void CtoJSUsbConfig(const napi_env &env, napi_value &obj, const USBConfig &usbConfig)
128 {
129 napi_create_object(env, &obj);
130 NapiUtil::SetValueInt32(env, "id", usbConfig.GetId(), obj);
131 NapiUtil::SetValueUint32(env, "attributes", usbConfig.GetAttributes(), obj);
132 NapiUtil::SetValueBool(env, "isRemoteWakeup", usbConfig.IsRemoteWakeup(), obj);
133 NapiUtil::SetValueBool(env, "isSelfPowered", usbConfig.IsSelfPowered(), obj);
134 NapiUtil::SetValueInt32(env, "maxPower", usbConfig.GetMaxPower(), obj);
135 NapiUtil::SetValueUtf8String(env, "name", usbConfig.GetName(), obj);
136 napi_value arr;
137 napi_create_array(env, &arr);
138 for (uint32_t i = 0; i < usbConfig.GetInterfaceCount(); ++i) {
139 UsbInterface usbInterface;
140 usbConfig.GetInterface(i, usbInterface);
141 napi_value objTmp;
142 CtoJSUsbInterface(env, objTmp, usbInterface);
143 napi_set_element(env, arr, i, objTmp);
144 }
145 napi_set_named_property(env, obj, "interfaces", arr);
146 }
147
CtoJSUsbDevice(const napi_env & env,napi_value & obj,const UsbDevice & usbDevice)148 static void CtoJSUsbDevice(const napi_env &env, napi_value &obj, const UsbDevice &usbDevice)
149 {
150 napi_create_object(env, &obj);
151 NapiUtil::SetValueUtf8String(env, "name", usbDevice.GetName(), obj);
152 NapiUtil::SetValueUtf8String(env, "serial", usbDevice.GetmSerial(), obj);
153 NapiUtil::SetValueUtf8String(env, "manufacturerName", usbDevice.GetManufacturerName(), obj);
154 NapiUtil::SetValueUtf8String(env, "productName", usbDevice.GetProductName(), obj);
155 NapiUtil::SetValueUtf8String(env, "version", usbDevice.GetVersion(), obj);
156 NapiUtil::SetValueInt32(env, "vendorId", usbDevice.GetVendorId(), obj);
157 NapiUtil::SetValueInt32(env, "productId", usbDevice.GetProductId(), obj);
158 NapiUtil::SetValueInt32(env, "clazz", usbDevice.GetClass(), obj);
159 NapiUtil::SetValueInt32(env, "subClass", usbDevice.GetSubclass(), obj);
160 NapiUtil::SetValueInt32(env, "protocol", usbDevice.GetProtocol(), obj);
161 NapiUtil::SetValueInt32(env, "devAddress", usbDevice.GetDevAddr(), obj);
162 NapiUtil::SetValueInt32(env, "busNum", usbDevice.GetBusNum(), obj);
163 napi_value arr;
164 napi_create_array(env, &arr);
165 for (int32_t i = 0; i < usbDevice.GetConfigCount(); ++i) {
166 USBConfig usbConfig;
167 usbDevice.GetConfig(i, usbConfig);
168 napi_value objTmp;
169 CtoJSUsbConfig(env, objTmp, usbConfig);
170 napi_set_element(env, arr, i, objTmp);
171 }
172 napi_set_named_property(env, obj, "configs", arr);
173 }
174
175 static UsbSrvClient &g_usbClient = UsbSrvClient::GetInstance();
176
177 /* ============================================= Parsers ============================================= */
178 // js to c
ParseEndpointObj(const napi_env env,const napi_value endpointObj,USBEndpoint & ep)179 static void ParseEndpointObj(const napi_env env, const napi_value endpointObj, USBEndpoint &ep)
180 {
181 int32_t address = 0;
182 NapiUtil::JsObjectToInt(env, endpointObj, "address", address);
183 int32_t attributes = 0;
184 NapiUtil::JsObjectToInt(env, endpointObj, "attributes", attributes);
185 int32_t interval = 0;
186 NapiUtil::JsObjectToInt(env, endpointObj, "interval", interval);
187 int32_t maxPacketSize = 0;
188 NapiUtil::JsObjectToInt(env, endpointObj, "maxPacketSize", maxPacketSize);
189 int32_t interfaceId = 0;
190 NapiUtil::JsObjectToInt(env, endpointObj, "interfaceId", interfaceId);
191 ep = USBEndpoint(address, attributes, interval, maxPacketSize);
192 ep.SetInterfaceId(interfaceId);
193 }
194
ParseEndpointsObjs(const napi_env env,const napi_value interfaceObj,std::vector<USBEndpoint> & eps)195 static bool ParseEndpointsObjs(const napi_env env, const napi_value interfaceObj, std::vector<USBEndpoint> &eps)
196 {
197 napi_value endpointsObjs;
198 bool isGetObjSuccess = NapiUtil::JsObjectGetProperty(env, interfaceObj, "endpoints", endpointsObjs);
199 USB_ASSERT_RETURN_FALSE(
200 env, isGetObjSuccess == true, SYSPARAM_INVALID_INPUT, "The interface should have the endpoints property.");
201
202 bool result = false;
203 NAPI_CHECK_RETURN_FALSE(napi_is_array(env, endpointsObjs, &result), "Get endpoints type failed");
204 USB_ASSERT_RETURN_FALSE(env, result == true, SYSPARAM_INVALID_INPUT, "The type of endpoints must be array.");
205
206 uint32_t endpointCount = 0;
207 NAPI_CHECK_RETURN_FALSE(napi_get_array_length(env, endpointsObjs, &endpointCount), "Get array length failed");
208
209 for (uint32_t k = 0; k < endpointCount; ++k) {
210 napi_value endpointObj;
211 NAPI_CHECK_RETURN_FALSE(napi_get_element(env, endpointsObjs, k, &endpointObj), "Get endpoints element failed");
212 USBEndpoint ep;
213 ParseEndpointObj(env, endpointObj, ep);
214 eps.push_back(ep);
215 }
216
217 return true;
218 }
219
220 struct PipeControlParam {
221 int32_t request;
222 int32_t target;
223 uint32_t reqType;
224 int32_t value;
225 int32_t index;
226 uint8_t *data;
227 size_t dataLength;
228 };
229
ParsePipeControlParam(const napi_env env,const napi_value jsObj,PipeControlParam & controlParam)230 static void ParsePipeControlParam(const napi_env env, const napi_value jsObj, PipeControlParam &controlParam)
231 {
232 int32_t request = 0;
233 NapiUtil::JsObjectToInt(env, jsObj, "request", request);
234 int32_t target = 0;
235 NapiUtil::JsObjectToInt(env, jsObj, "target", target);
236 uint32_t reqType = 0;
237 NapiUtil::JsObjectToUint(env, jsObj, "reqType", reqType);
238 int32_t value = 0;
239 NapiUtil::JsObjectToInt(env, jsObj, "value", value);
240 int32_t index = 0;
241 NapiUtil::JsObjectToInt(env, jsObj, "index", index);
242
243 napi_value dataValue;
244 bool hasProperty = NapiUtil::JsObjectGetProperty(env, jsObj, "data", dataValue);
245 USB_ASSERT_RETURN_VOID(
246 env, hasProperty == true, SYSPARAM_INVALID_INPUT, "The controlParam should have the data property.");
247
248 uint8_t *data = nullptr;
249 size_t dataLength = 0;
250 size_t offset = 0;
251 NapiUtil::JsUint8ArrayParse(env, dataValue, &data, dataLength, offset);
252 controlParam.request = request;
253 controlParam.target = target;
254 controlParam.reqType = reqType;
255 controlParam.value = value;
256 controlParam.index = index;
257 controlParam.data = data;
258 controlParam.dataLength = dataLength;
259 }
260
ParseInterfaceObj(const napi_env env,const napi_value interfaceObj,UsbInterface & interface)261 static void ParseInterfaceObj(const napi_env env, const napi_value interfaceObj, UsbInterface &interface)
262 {
263 int32_t id = 0;
264 NapiUtil::JsObjectToInt(env, interfaceObj, "id", id);
265 int32_t protocol = 0;
266 NapiUtil::JsObjectToInt(env, interfaceObj, "protocol", protocol);
267 int32_t clzz = 0;
268 NapiUtil::JsObjectToInt(env, interfaceObj, "clazz", clzz);
269 int32_t subClass = 0;
270 NapiUtil::JsObjectToInt(env, interfaceObj, "subClass", subClass);
271 int32_t alternateSetting = 0;
272 NapiUtil::JsObjectToInt(env, interfaceObj, "alternateSetting", alternateSetting);
273 std::string name;
274 NapiUtil::JsObjectToString(env, interfaceObj, "name", DEFAULT_DESCRIPTION_SIZE, name);
275 std::vector<USBEndpoint> eps;
276
277 bool ret = ParseEndpointsObjs(env, interfaceObj, eps);
278 if (!ret) {
279 USB_HILOGE(MODULE_JS_NAPI, "Parse endpointers error.");
280 return;
281 }
282
283 interface = UsbInterface(id, protocol, clzz, subClass, alternateSetting, name, eps);
284 }
285
ParseInterfacesObjs(const napi_env env,const napi_value configObj,std::vector<UsbInterface> & interfaces)286 static bool ParseInterfacesObjs(const napi_env env, const napi_value configObj, std::vector<UsbInterface> &interfaces)
287 {
288 napi_value interfacesObjs;
289 bool isGetObjSuccess = NapiUtil::JsObjectGetProperty(env, configObj, "interfaces", interfacesObjs);
290 USB_ASSERT_RETURN_FALSE(
291 env, isGetObjSuccess == true, SYSPARAM_INVALID_INPUT, "The config should have the interfaces property.");
292
293 bool result = false;
294 NAPI_CHECK_RETURN_FALSE(napi_is_array(env, interfacesObjs, &result), "Get interfaces type failed");
295 USB_ASSERT_RETURN_FALSE(env, result == true, SYSPARAM_INVALID_INPUT, "The type of interfaces must be array.");
296
297 uint32_t interfaceCount = 0;
298 NAPI_CHECK_RETURN_FALSE(napi_get_array_length(env, interfacesObjs, &interfaceCount), "Get array length failed");
299
300 for (uint32_t i = 0; i < interfaceCount; ++i) {
301 napi_value interfaceObj;
302 NAPI_CHECK_RETURN_FALSE(
303 napi_get_element(env, interfacesObjs, i, &interfaceObj), "Get interfaces element failed");
304
305 UsbInterface interface;
306 ParseInterfaceObj(env, interfaceObj, interface);
307 interfaces.push_back(interface);
308 }
309
310 return true;
311 }
312
ParseConfigObj(const napi_env env,const napi_value configObj,USBConfig & config)313 static void ParseConfigObj(const napi_env env, const napi_value configObj, USBConfig &config)
314 {
315 int32_t id = 0;
316 NapiUtil::JsObjectToInt(env, configObj, "id", id);
317 int32_t attributes = 0;
318 NapiUtil::JsObjectToInt(env, configObj, "attributes", attributes);
319 int32_t maxPower = 0;
320 NapiUtil::JsObjectToInt(env, configObj, "maxPower", maxPower);
321 std::string name;
322 NapiUtil::JsObjectToString(env, configObj, "name", DEFAULT_DESCRIPTION_SIZE, name);
323
324 std::vector<UsbInterface> interfaces;
325 bool ret = ParseInterfacesObjs(env, configObj, interfaces);
326 if (!ret) {
327 USB_HILOGE(MODULE_JS_NAPI, "Parse interfaces error.");
328 return;
329 }
330
331 config = USBConfig(id, attributes, name, maxPower, interfaces);
332 }
333
ParseConfigsObjs(const napi_env env,const napi_value deviceObj,std::vector<USBConfig> & configs)334 static void ParseConfigsObjs(const napi_env env, const napi_value deviceObj, std::vector<USBConfig> &configs)
335 {
336 napi_value configsObj;
337 bool hasProperty = NapiUtil::JsObjectGetProperty(env, deviceObj, "configs", configsObj);
338 USB_ASSERT_RETURN_VOID(
339 env, hasProperty == true, SYSPARAM_INVALID_INPUT, "The device should have the configs property.");
340 napi_valuetype valueType;
341 napi_typeof(env, configsObj, &valueType);
342 USB_ASSERT_RETURN_VOID(
343 env, valueType == napi_object, SYSPARAM_INVALID_INPUT, "The type of configs must be object.");
344
345 uint32_t configCount = 0;
346 napi_get_array_length(env, configsObj, &configCount);
347 for (uint32_t i = 0; i < configCount; ++i) {
348 napi_value configObj;
349 napi_get_element(env, configsObj, i, &configObj);
350 USBConfig config;
351 ParseConfigObj(env, configObj, config);
352 configs.push_back(config);
353 }
354 }
355
ParseDeviceObj(const napi_env env,const napi_value deviceObj,UsbDevice & dev)356 static void ParseDeviceObj(const napi_env env, const napi_value deviceObj, UsbDevice &dev)
357 {
358 std::string name;
359 NapiUtil::JsObjectToString(env, deviceObj, "name", DEFAULT_DESCRIPTION_SIZE, name);
360 std::string manufacturerName;
361 NapiUtil::JsObjectToString(env, deviceObj, "manufacturerName", DEFAULT_DESCRIPTION_SIZE, manufacturerName);
362 std::string productName;
363 NapiUtil::JsObjectToString(env, deviceObj, "productName", DEFAULT_DESCRIPTION_SIZE, productName);
364 std::string version;
365 NapiUtil::JsObjectToString(env, deviceObj, "version", DEFAULT_DESCRIPTION_SIZE, version);
366 int32_t devAddr = 0;
367 NapiUtil::JsObjectToInt(env, deviceObj, "devAddress", devAddr);
368 int32_t busNum = 0;
369 NapiUtil::JsObjectToInt(env, deviceObj, "busNum", busNum);
370 int32_t vendorId = 0;
371 NapiUtil::JsObjectToInt(env, deviceObj, "vendorId", vendorId);
372 int32_t productId = 0;
373 NapiUtil::JsObjectToInt(env, deviceObj, "productId", productId);
374 int32_t clazz = 0;
375 NapiUtil::JsObjectToInt(env, deviceObj, "clazz", clazz);
376 int32_t subClass = 0;
377 NapiUtil::JsObjectToInt(env, deviceObj, "subClass", subClass);
378 int32_t protocol = 0;
379 NapiUtil::JsObjectToInt(env, deviceObj, "protocol", protocol);
380 std::vector<USBConfig> configs;
381 ParseConfigsObjs(env, deviceObj, configs);
382 dev = UsbDevice(name, manufacturerName, productName, version, devAddr, busNum, vendorId, productId, clazz, subClass,
383 protocol, configs);
384 }
385
386 /* ============================================= Usb Core ============================================= */
387
CoreGetDevices(napi_env env,napi_callback_info info)388 static napi_value CoreGetDevices(napi_env env, napi_callback_info info)
389 {
390 size_t argc = PARAM_COUNT_1;
391 napi_value argv[PARAM_COUNT_1] = {nullptr};
392 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
393 USB_ASSERT(env, (argc == PARAM_COUNT_0), SYSPARAM_INVALID_INPUT, "The function takes no arguments.");
394
395 std::vector<UsbDevice> deviceList;
396 int32_t ret = g_usbClient.GetDevices(deviceList);
397 napi_value result;
398 if (ret != UEC_OK) {
399 napi_get_undefined(env, &result);
400 USB_HILOGE(MODULE_JS_NAPI, "end call get device failed ret : %{public}d", ret);
401 return result;
402 }
403
404 napi_create_array(env, &result);
405 int32_t i = 0;
406 for (const auto &ent1 : deviceList) {
407 napi_value element;
408 napi_create_object(env, &element);
409 napi_value device;
410 CtoJSUsbDevice(env, device, ent1);
411 napi_set_element(env, result, i, device);
412 ++i;
413 }
414
415 return result;
416 }
417
CoreConnectDevice(napi_env env,napi_callback_info info)418 static napi_value CoreConnectDevice(napi_env env, napi_callback_info info)
419 {
420 size_t argc = PARAM_COUNT_1;
421 napi_value argv[PARAM_COUNT_1] = {nullptr};
422 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
423 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
424
425 napi_value deviceObj = argv[INDEX_0];
426 napi_valuetype type;
427 NAPI_CHECK(env, napi_typeof(env, deviceObj, &type), "Get deviceObj type failed");
428 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of device must be USBDevice.");
429 UsbDevice dev;
430 ParseDeviceObj(env, deviceObj, dev);
431
432 USBDevicePipe pipe;
433 int32_t ret = g_usbClient.OpenDevice(dev, pipe);
434 napi_value pipObj = nullptr;
435 if (ret == UEC_OK) {
436 CreateUsbDevicePipe(env, pipObj, pipe);
437 } else if (ret == UEC_SERVICE_PERMISSION_DENIED || ret == UEC_INTERFACE_PERMISSION_DENIED) {
438 ThrowBusinessError(env, USB_DEVICE_PERMISSION_DENIED, "need call requestRight to get permission");
439 } else {
440 napi_get_undefined(env, &pipObj);
441 }
442
443 return pipObj;
444 }
445
DeviceAddRight(napi_env env,napi_callback_info info)446 static napi_value DeviceAddRight(napi_env env, napi_callback_info info)
447 {
448 size_t argc = PARAM_COUNT_2;
449 napi_value argv[PARAM_COUNT_2] = {nullptr};
450 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
451 USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function takes two argument.");
452
453 napi_valuetype type;
454 NAPI_CHECK(env, napi_typeof(env, argv[INDEX_0], &type), "Get args 1 type failed");
455 USB_ASSERT(env, type == napi_string, SYSPARAM_INVALID_INPUT, "The type of bundleName must be string.");
456 std::string bundleName;
457 NapiUtil::JsValueToString(env, argv[INDEX_0], STR_DEFAULT_SIZE, bundleName);
458
459 NAPI_CHECK(env, napi_typeof(env, argv[INDEX_1], &type), "Get args 2 type failed");
460 USB_ASSERT(env, type == napi_string, SYSPARAM_INVALID_INPUT, "The type of deviceName must be string.");
461 std::string deviceName;
462 NapiUtil::JsValueToString(env, argv[INDEX_1], STR_DEFAULT_SIZE, deviceName);
463
464 napi_value result;
465 int32_t ret = g_usbClient.AddRight(bundleName, deviceName);
466 USB_HILOGD(MODULE_JS_NAPI, "Device call AddRight ret: %{public}d", ret);
467 if (ret == UEC_OK) {
468 napi_get_boolean(env, true, &result);
469 } else {
470 USB_ASSERT_RETURN_UNDEF(env, (ret != UEC_SERVICE_PERMISSION_DENIED_SYSAPI), USB_SYSAPI_PERMISSION_DENIED, "");
471 napi_get_boolean(env, false, &result);
472 }
473 return result;
474 }
475
DeviceRemoveRight(napi_env env,napi_callback_info info)476 static napi_value DeviceRemoveRight(napi_env env, napi_callback_info info)
477 {
478 size_t argc = PARAM_COUNT_1;
479 napi_value argv[PARAM_COUNT_1] = {nullptr};
480 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
481 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
482
483 napi_valuetype type;
484 NAPI_CHECK(env, napi_typeof(env, argv[INDEX_0], &type), "Get args 1 type failed");
485 USB_ASSERT(env, type == napi_string, SYSPARAM_INVALID_INPUT, "The type of deviceName must be string.");
486 std::string deviceName;
487 NapiUtil::JsValueToString(env, argv[INDEX_0], STR_DEFAULT_SIZE, deviceName);
488
489 napi_value result;
490 int32_t ret = g_usbClient.RemoveRight(deviceName);
491 USB_HILOGD(MODULE_JS_NAPI, "Device call RemoveRight ret: %{public}d", ret);
492 if (ret == UEC_OK) {
493 napi_get_boolean(env, true, &result);
494 } else {
495 napi_get_boolean(env, false, &result);
496 }
497
498 return result;
499 }
500
CoreHasRight(napi_env env,napi_callback_info info)501 static napi_value CoreHasRight(napi_env env, napi_callback_info info)
502 {
503 size_t argc = PARAM_COUNT_1;
504 napi_value args[PARAM_COUNT_1] = {nullptr};
505 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), "Get call back info failed");
506 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
507
508 napi_valuetype type;
509 NAPI_CHECK(env, napi_typeof(env, args[INDEX_0], &type), "Get args 1 type failed");
510 USB_ASSERT(env, type == napi_string, SYSPARAM_INVALID_INPUT, "The type of deviceName must be string");
511 std::string deviceName;
512 NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, deviceName);
513
514 bool result = g_usbClient.HasRight(deviceName);
515 USB_HILOGD(MODULE_JS_NAPI, "client called result %{public}d", result);
516
517 napi_value napiValue = nullptr;
518 napi_get_boolean(env, result, &napiValue);
519
520 return napiValue;
521 }
522
__anon4ef8f19d0102(napi_env env, void *data) 523 static auto g_requestRightExecute = [](napi_env env, void *data) {
524 USBRightAsyncContext *asyncContext = reinterpret_cast<USBRightAsyncContext *>(data);
525 int32_t ret = g_usbClient.RequestRight(asyncContext->deviceName);
526 if (ret == UEC_OK) {
527 asyncContext->status = napi_ok;
528 } else {
529 asyncContext->status = napi_generic_failure;
530 }
531 };
532
__anon4ef8f19d0202(napi_env env, napi_status status, void *data) 533 static auto g_requestRightComplete = [](napi_env env, napi_status status, void *data) {
534 USBRightAsyncContext *asyncContext = reinterpret_cast<USBRightAsyncContext *>(data);
535 napi_value queryResult = nullptr;
536 napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult);
537
538 ProcessPromise(env, *asyncContext, queryResult);
539 napi_delete_async_work(env, asyncContext->work);
540 delete asyncContext;
541 };
542
CoreRequestRight(napi_env env,napi_callback_info info)543 static napi_value CoreRequestRight(napi_env env, napi_callback_info info)
544 {
545 size_t argc = PARAM_COUNT_1;
546 napi_value args[PARAM_COUNT_1] = {nullptr};
547 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), "Get call back info failed");
548 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
549
550 napi_valuetype type;
551 NAPI_CHECK(env, napi_typeof(env, args[INDEX_0], &type), "Get args 1 type failed");
552 USB_ASSERT(env, type == napi_string, SYSPARAM_INVALID_INPUT, "The type of deviceName must be string.");
553 std::string deviceName;
554 NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, deviceName);
555
556 auto asyncContext = new (std::nothrow) USBRightAsyncContext();
557 if (asyncContext == nullptr) {
558 USB_HILOGE(MODULE_JS_NAPI, "Create USBRightAsyncContext failed.");
559 return nullptr;
560 }
561
562 asyncContext->env = env;
563 asyncContext->deviceName = deviceName;
564
565 napi_value result = nullptr;
566 napi_create_promise(env, &asyncContext->deferred, &result);
567
568 napi_value resource = nullptr;
569 napi_create_string_utf8(env, "RequestRight", NAPI_AUTO_LENGTH, &resource);
570
571 napi_create_async_work(env, nullptr, resource, g_requestRightExecute, g_requestRightComplete,
572 reinterpret_cast<void *>(asyncContext), &asyncContext->work);
573 napi_queue_async_work(env, asyncContext->work);
574
575 return result;
576 }
577
CoreUsbFunctionsFromString(napi_env env,napi_callback_info info)578 static napi_value CoreUsbFunctionsFromString(napi_env env, napi_callback_info info)
579 {
580 size_t argc = PARAM_COUNT_1;
581 napi_value argv[PARAM_COUNT_1] = {nullptr};
582
583 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
584 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
585
586 napi_valuetype type;
587 NAPI_CHECK(env, napi_typeof(env, argv[INDEX_0], &type), "Get args 1 type failed");
588 USB_ASSERT(env, type == napi_string, SYSPARAM_INVALID_INPUT, "The type of funcs must be string.");
589
590 // get value string argument of napi converted.
591 std::string funcs;
592 NapiUtil::JsValueToString(env, argv[INDEX_0], STR_DEFAULT_SIZE, funcs);
593
594 int32_t numFuncs = g_usbClient.UsbFunctionsFromString(funcs);
595 USB_HILOGI(MODULE_JS_NAPI, "usb functions from string failed ret = %{public}d", numFuncs);
596 USB_ASSERT_RETURN_UNDEF(env, (numFuncs != UEC_SERVICE_PERMISSION_DENIED_SYSAPI), USB_SYSAPI_PERMISSION_DENIED, "");
597
598 napi_value result;
599 napi_create_int32(env, numFuncs, &result);
600
601 return result;
602 }
603
CoreUsbFunctionsToString(napi_env env,napi_callback_info info)604 static napi_value CoreUsbFunctionsToString(napi_env env, napi_callback_info info)
605 {
606 size_t argc = PARAM_COUNT_1;
607 napi_value argv[PARAM_COUNT_1] = {nullptr};
608
609 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
610 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
611
612 napi_valuetype type;
613 NAPI_CHECK(env, napi_typeof(env, argv[INDEX_0], &type), "Get args 1 type failed");
614 USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of funcs must be number.");
615
616 int32_t funcs;
617 napi_get_value_int32(env, argv[INDEX_0], &funcs);
618 std::string strFuncs = g_usbClient.UsbFunctionsToString(funcs);
619 USB_ASSERT_RETURN_UNDEF(env, (strFuncs != PERMISSION_DENIED_SYSAPI), USB_SYSAPI_PERMISSION_DENIED, "");
620 napi_value result;
621 napi_create_string_utf8(env, strFuncs.c_str(), NAPI_AUTO_LENGTH, &result);
622
623 return result;
624 }
625
__anon4ef8f19d0302(napi_env env, void *data) 626 static auto g_setCurrentFunctionExecute = [](napi_env env, void *data) {
627 USBFunctionAsyncContext *asyncContext = reinterpret_cast<USBFunctionAsyncContext *>(data);
628 int32_t ret = g_usbClient.SetCurrentFunctions(asyncContext->functions);
629 asyncContext->errCode = ret;
630 };
631
__anon4ef8f19d0402(napi_env env, napi_status status, void *data) 632 static auto g_setCurrentFunctionComplete = [](napi_env env, napi_status status, void *data) {
633 USBFunctionAsyncContext *asyncContext = reinterpret_cast<USBFunctionAsyncContext *>(data);
634 napi_value queryResult = nullptr;
635
636 if (asyncContext->errCode == UEC_OK) {
637 asyncContext->status = napi_ok;
638 napi_get_boolean(env, true, &queryResult);
639 } else if (asyncContext->errCode == UEC_SERVICE_PERMISSION_DENIED_SYSAPI) {
640 asyncContext->status = napi_generic_failure;
641 queryResult = CreateBusinessError((env), USB_SYSAPI_PERMISSION_DENIED, "");
642 } else if (asyncContext->errCode == UEC_SERVICE_PERMISSION_CHECK_HDC) {
643 asyncContext->status = napi_generic_failure;
644 queryResult = CreateBusinessError((env), USB_HDC_PERMISSION_DENIED, "");
645 } else {
646 asyncContext->status = napi_generic_failure;
647 napi_get_boolean(env, false, &queryResult);
648 }
649 ProcessPromise(env, *asyncContext, queryResult);
650 napi_delete_async_work(env, asyncContext->work);
651 delete asyncContext;
652 };
653
CoreSetCurrentFunctions(napi_env env,napi_callback_info info)654 static napi_value CoreSetCurrentFunctions(napi_env env, napi_callback_info info)
655 {
656 size_t argc = PARAM_COUNT_1;
657 napi_value argv[PARAM_COUNT_1] = {nullptr};
658
659 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
660 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
661
662 napi_valuetype type;
663 NAPI_CHECK(env, napi_typeof(env, argv[INDEX_0], &type), "Get args 1 type failed");
664 USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of funcs must be number.");
665
666 int32_t funcs = 0;
667 napi_get_value_int32(env, argv[INDEX_0], &funcs);
668
669 auto asyncContext = new (std::nothrow) USBFunctionAsyncContext();
670 if (asyncContext == nullptr) {
671 USB_HILOGE(MODULE_JS_NAPI, "Create USBFunctionAsyncContext failed");
672 return nullptr;
673 }
674
675 asyncContext->env = env;
676 asyncContext->functions = funcs;
677 napi_value result = nullptr;
678 napi_create_promise(env, &asyncContext->deferred, &result);
679
680 napi_value resource = nullptr;
681 napi_create_string_utf8(env, "SetCurrentFunctions", NAPI_AUTO_LENGTH, &resource);
682
683 napi_create_async_work(env, nullptr, resource, g_setCurrentFunctionExecute, g_setCurrentFunctionComplete,
684 reinterpret_cast<void *>(asyncContext), &asyncContext->work);
685 napi_queue_async_work(env, asyncContext->work);
686
687 return result;
688 }
689
CoreGetCurrentFunctions(napi_env env,napi_callback_info info)690 static napi_value CoreGetCurrentFunctions(napi_env env, napi_callback_info info)
691 {
692 size_t argc = PARAM_COUNT_1;
693 napi_value argv[PARAM_COUNT_1] = {nullptr};
694 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
695 USB_ASSERT(env, (argc == PARAM_COUNT_0), SYSPARAM_INVALID_INPUT, "The function takes no arguments.");
696
697 int32_t cfuncs;
698 int32_t ret = g_usbClient.GetCurrentFunctions(cfuncs);
699 napi_value result;
700 USB_HILOGI(MODULE_JS_NAPI, "get current functions failed ret = %{public}d", ret);
701 USB_ASSERT_RETURN_UNDEF(env, (ret != UEC_SERVICE_PERMISSION_DENIED_SYSAPI), USB_SYSAPI_PERMISSION_DENIED, "");
702
703 if (ret != UEC_OK) {
704 napi_get_undefined(env, &result);
705 USB_HILOGE(MODULE_JS_NAPI, "end call get ports failed ret : %{public}d", ret);
706 return result;
707 }
708 napi_create_int32(env, cfuncs, &result);
709
710 return result;
711 }
712
CoreGetPorts(napi_env env,napi_callback_info info)713 static napi_value CoreGetPorts(napi_env env, napi_callback_info info)
714 {
715 size_t argc = PARAM_COUNT_1;
716 napi_value argv[PARAM_COUNT_1] = {nullptr};
717 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
718 USB_ASSERT(env, (argc == PARAM_COUNT_0), SYSPARAM_INVALID_INPUT, "The function takes no arguments.");
719
720 std::vector<UsbPort> ports;
721 int32_t ret = g_usbClient.GetPorts(ports);
722 napi_value result;
723 USB_HILOGI(MODULE_JS_NAPI, "get ports failed ret : %{public}d", ret);
724 USB_ASSERT_RETURN_UNDEF(env, (ret != UEC_SERVICE_PERMISSION_DENIED_SYSAPI), USB_SYSAPI_PERMISSION_DENIED, "");
725
726 if (ret != UEC_OK) {
727 napi_get_undefined(env, &result);
728 USB_HILOGE(MODULE_JS_NAPI, "end call get ports failed ret : %{public}d", ret);
729 return result;
730 }
731
732 napi_create_array(env, &result);
733 for (uint32_t i = 0; i < ports.size(); ++i) {
734 napi_value port;
735 napi_create_object(env, &port);
736 NapiUtil::SetValueInt32(env, "id", ports[i].id, port);
737 NapiUtil::SetValueInt32(env, "supportedModes", ports[i].supportedModes, port);
738 napi_value usbPortStatus;
739 napi_create_object(env, &usbPortStatus);
740 NapiUtil::SetValueInt32(env, "currentMode", ports[i].usbPortStatus.currentMode, usbPortStatus);
741 NapiUtil::SetValueInt32(env, "currentPowerRole", ports[i].usbPortStatus.currentPowerRole, usbPortStatus);
742 NapiUtil::SetValueInt32(env, "currentDataRole", ports[i].usbPortStatus.currentDataRole, usbPortStatus);
743 napi_set_named_property(env, port, "status", usbPortStatus);
744 napi_set_element(env, result, i, port);
745 }
746
747 return result;
748 }
749
750 /* ============================================= Usb Port ============================================= */
751
PortGetSupportedModes(napi_env env,napi_callback_info info)752 static napi_value PortGetSupportedModes(napi_env env, napi_callback_info info)
753 {
754 size_t argc = PARAM_COUNT_1;
755 napi_value args[PARAM_COUNT_1] = {nullptr};
756
757 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), "Get call back info failed");
758 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
759
760 napi_valuetype type;
761 NAPI_CHECK(env, napi_typeof(env, args[INDEX_0], &type), "Get args 1 type failed");
762 USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of portId must be number.");
763
764 int32_t id = 0;
765 int32_t result = 0;
766 napi_get_value_int32(env, args[INDEX_0], &id);
767 int32_t ret = g_usbClient.GetSupportedModes(id, result);
768 USB_HILOGI(MODULE_JS_NAPI, "get supported modes failed ret = %{public}d", ret);
769 USB_ASSERT_RETURN_UNDEF(env, (ret != UEC_SERVICE_PERMISSION_DENIED_SYSAPI), USB_SYSAPI_PERMISSION_DENIED, "");
770
771 if (ret) {
772 USB_HILOGD(MODULE_JS_NAPI, "false ret = %{public}d", ret);
773 }
774 napi_value napiValue = nullptr;
775 NAPI_CHECK(env, napi_create_int32(env, result, &napiValue), "Create int32 failed");
776
777 return napiValue;
778 }
779
__anon4ef8f19d0502(napi_env env, void *data) 780 static auto g_setPortRoleExecute = [](napi_env env, void *data) {
781 USBPortRoleAsyncContext *asyncContext = reinterpret_cast<USBPortRoleAsyncContext *>(data);
782 int32_t ret = g_usbClient.SetPortRole(asyncContext->portId, asyncContext->powerRole, asyncContext->dataRole);
783 asyncContext->errCode = ret;
784 };
785
__anon4ef8f19d0602(napi_env env, napi_status status, void *data) 786 static auto g_setPortRoleComplete = [](napi_env env, napi_status status, void *data) {
787 USBPortRoleAsyncContext *asyncContext = reinterpret_cast<USBPortRoleAsyncContext *>(data);
788 napi_value queryResult = nullptr;
789
790 if (asyncContext->errCode == UEC_OK) {
791 asyncContext->status = napi_ok;
792 napi_get_boolean(env, true, &queryResult);
793 } else if (asyncContext->errCode == UEC_SERVICE_PERMISSION_DENIED_SYSAPI) {
794 asyncContext->status = napi_generic_failure;
795 queryResult = CreateBusinessError((env), USB_SYSAPI_PERMISSION_DENIED, "");
796 } else if (asyncContext->errCode == UEC_SERVICE_NOT_SUPPORT_SWITCH_PORT) {
797 asyncContext->status = napi_generic_failure;
798 queryResult = CreateBusinessError((env), USB_NOT_SUPPORT_SWITCH_PORT, "");
799 } else {
800 asyncContext->status = napi_generic_failure;
801 napi_get_boolean(env, false, &queryResult);
802 }
803 ProcessPromise(env, *asyncContext, queryResult);
804 napi_delete_async_work(env, asyncContext->work);
805 delete asyncContext;
806 };
807
PortSetPortRole(napi_env env,napi_callback_info info)808 static napi_value PortSetPortRole(napi_env env, napi_callback_info info)
809 {
810 size_t argc = PARAM_COUNT_3;
811 napi_value args[PARAM_COUNT_3] = {nullptr};
812
813 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), "Get call back info failed");
814 USB_ASSERT(env, (argc >= PARAM_COUNT_3), SYSPARAM_INVALID_INPUT, "The function takes three arguments.");
815
816 napi_valuetype type;
817 NAPI_CHECK(env, napi_typeof(env, args[INDEX_0], &type), "Get args 1 type failed");
818 USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of portId must be number.");
819 NAPI_CHECK(env, napi_typeof(env, args[INDEX_1], &type), "Get args 2 type failed");
820 USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of powerRole must be number.");
821 NAPI_CHECK(env, napi_typeof(env, args[INDEX_2], &type), "Get args 3 type failed");
822 USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of dataRole must be number.");
823
824 int32_t id = 0;
825 napi_get_value_int32(env, args[INDEX_0], &id);
826 int32_t powerRole = 0;
827 napi_get_value_int32(env, args[INDEX_1], &powerRole);
828 int32_t dataRole = 0;
829 napi_get_value_int32(env, args[INDEX_2], &dataRole);
830
831 auto asyncContext = new (std::nothrow) USBPortRoleAsyncContext();
832 if (asyncContext == nullptr) {
833 USB_HILOGE(MODULE_JS_NAPI, "Create USBPortRoleAsyncContext failed");
834 return nullptr;
835 }
836
837 asyncContext->env = env;
838 asyncContext->portId = id;
839 asyncContext->dataRole = dataRole;
840 asyncContext->powerRole = powerRole;
841
842 napi_value result = nullptr;
843 napi_create_promise(env, &asyncContext->deferred, &result);
844
845 napi_value resource = nullptr;
846 napi_create_string_utf8(env, "PortSetPortRole", NAPI_AUTO_LENGTH, &resource);
847
848 napi_create_async_work(env, nullptr, resource, g_setPortRoleExecute, g_setPortRoleComplete,
849 reinterpret_cast<void *>(asyncContext), &asyncContext->work);
850 napi_queue_async_work(env, asyncContext->work);
851
852 return result;
853 }
854
PipeClaimInterface(napi_env env,napi_callback_info info)855 static napi_value PipeClaimInterface(napi_env env, napi_callback_info info)
856 {
857 size_t argc = PARAM_COUNT_3;
858 napi_value argv[PARAM_COUNT_3] = {nullptr};
859
860 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
861 USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function at least takes two arguments.");
862
863 napi_value obj = argv[INDEX_0];
864 napi_valuetype type;
865 napi_typeof(env, obj, &type);
866 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
867
868 USBDevicePipe pipe;
869 ParseUsbDevicePipe(env, obj, pipe);
870
871 UsbInterface interface;
872 napi_value obj2 = argv[INDEX_1];
873 napi_typeof(env, obj2, &type);
874 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of iface must be USBInterface.");
875 ParseInterfaceObj(env, obj2, interface);
876
877 bool isForce = false;
878 if (argc >= PARAM_COUNT_3) {
879 napi_typeof(env, argv[INDEX_2], &type);
880 if (type == napi_boolean) {
881 napi_get_value_bool(env, argv[INDEX_2], &isForce);
882 } else {
883 USB_HILOGW(MODULE_JS_NAPI, "The type of force must be boolean.");
884 }
885 }
886
887 int32_t ret = pipe.ClaimInterface(interface, isForce);
888 USB_HILOGD(MODULE_JS_NAPI, "pipe call ClaimInterface ret: %{public}d", ret);
889 napi_value result;
890 napi_create_int32(env, ret, &result);
891
892 return result;
893 }
894
PipeReleaseInterface(napi_env env,napi_callback_info info)895 static napi_value PipeReleaseInterface(napi_env env, napi_callback_info info)
896 {
897 size_t argc = PARAM_COUNT_2;
898 napi_value argv[PARAM_COUNT_2] = {nullptr};
899
900 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
901 USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function takes two arguments.");
902
903 napi_value obj = argv[INDEX_0];
904 napi_valuetype type;
905 napi_typeof(env, obj, &type);
906 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
907
908 USBDevicePipe pipe;
909 ParseUsbDevicePipe(env, obj, pipe);
910
911 UsbInterface interface;
912 napi_value obj2 = argv[INDEX_1];
913 napi_typeof(env, obj2, &type);
914 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of iface must be USBInterface.");
915 ParseInterfaceObj(env, obj2, interface);
916 int32_t ret = pipe.ReleaseInterface(interface);
917 USB_HILOGD(MODULE_JS_NAPI, "pipe call PipeReleaseInterface ret: %{public}d", ret);
918 napi_value result;
919 napi_create_int32(env, ret, &result);
920
921 return result;
922 }
923
PipeSetInterface(napi_env env,napi_callback_info info)924 static napi_value PipeSetInterface(napi_env env, napi_callback_info info)
925 {
926 size_t argc = PARAM_COUNT_2;
927 napi_value argv[PARAM_COUNT_2] = {nullptr};
928 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
929 USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function takes two arguments.");
930
931 napi_value pipeObj = argv[INDEX_0];
932 napi_valuetype type;
933 napi_typeof(env, pipeObj, &type);
934 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
935
936 USBDevicePipe pipe;
937 ParseUsbDevicePipe(env, pipeObj, pipe);
938
939 napi_value interfaceObj = argv[INDEX_1];
940 napi_typeof(env, interfaceObj, &type);
941 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of iface must be USBInterface.");
942
943 UsbInterface interface;
944 ParseInterfaceObj(env, interfaceObj, interface);
945 int32_t ret = g_usbClient.SetInterface(pipe, interface);
946 napi_value result;
947 napi_create_int32(env, ret, &result);
948
949 return result;
950 }
951
PipeSetConfiguration(napi_env env,napi_callback_info info)952 static napi_value PipeSetConfiguration(napi_env env, napi_callback_info info)
953 {
954 size_t argc = PARAM_COUNT_2;
955 napi_value argv[PARAM_COUNT_2] = {nullptr};
956 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
957 USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function takes two arguments.");
958
959 napi_valuetype type;
960 napi_value pipeObj = argv[INDEX_0];
961
962 napi_typeof(env, pipeObj, &type);
963 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
964 USBDevicePipe pipe;
965 ParseUsbDevicePipe(env, pipeObj, pipe);
966
967 napi_value configObj = argv[INDEX_1];
968 napi_typeof(env, configObj, &type);
969 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of config must be USBConfig.");
970 USBConfig config;
971 ParseConfigObj(env, configObj, config);
972
973 int32_t ret = g_usbClient.SetConfiguration(pipe, config);
974 napi_value result;
975 napi_create_int32(env, ret, &result);
976
977 return result;
978 }
979
PipeGetRawDescriptors(napi_env env,napi_callback_info info)980 static napi_value PipeGetRawDescriptors(napi_env env, napi_callback_info info)
981 {
982 size_t argc = PARAM_COUNT_1;
983 napi_value argv[PARAM_COUNT_1] = {nullptr};
984
985 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
986 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
987 napi_value obj = argv[INDEX_0];
988 napi_valuetype type;
989 napi_typeof(env, obj, &type);
990 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
991
992 USBDevicePipe pipe;
993 ParseUsbDevicePipe(env, obj, pipe);
994
995 napi_value result;
996 std::vector<uint8_t> bufferData;
997 int32_t ret = g_usbClient.GetRawDescriptors(pipe, bufferData);
998 if (ret == UEC_OK) {
999 NapiUtil::Uint8ArrayToJsValue(env, bufferData, bufferData.size(), result);
1000 } else {
1001 napi_get_undefined(env, &result);
1002 }
1003
1004 return result;
1005 }
1006
PipeGetFileDescriptor(napi_env env,napi_callback_info info)1007 static napi_value PipeGetFileDescriptor(napi_env env, napi_callback_info info)
1008 {
1009 size_t argc = PARAM_COUNT_1;
1010 napi_value argv[PARAM_COUNT_1] = {nullptr};
1011
1012 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
1013 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
1014 napi_value obj = argv[INDEX_0];
1015 napi_valuetype type;
1016 napi_typeof(env, obj, &type);
1017 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
1018
1019 USBDevicePipe pipe;
1020 ParseUsbDevicePipe(env, obj, pipe);
1021
1022 int32_t fd = -1;
1023 napi_value result;
1024 g_usbClient.GetFileDescriptor(pipe, fd);
1025 napi_create_int32(env, fd, &result);
1026
1027 return result;
1028 }
1029
__anon4ef8f19d0702(napi_env env, void *data) 1030 static auto g_controlTransferExecute = [](napi_env env, void *data) {
1031 USBControlTransferAsyncContext *asyncContext = (USBControlTransferAsyncContext *)data;
1032 std::vector<uint8_t> bufferData(asyncContext->buffer, asyncContext->buffer + asyncContext->bufferLength);
1033 if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
1034 delete[] asyncContext->buffer;
1035 asyncContext->buffer = nullptr;
1036 }
1037
1038 const UsbCtrlTransfer tctrl = {
1039 asyncContext->reqType, asyncContext->request, asyncContext->value, asyncContext->index, asyncContext->timeOut};
1040 int32_t ret;
1041 do {
1042 ret = asyncContext->pipe.ControlTransfer(tctrl, bufferData);
1043 if (ret != UEC_OK) {
1044 USB_HILOGE(MODULE_JS_NAPI, "ControlTransferExecute failed");
1045 break;
1046 }
1047
1048 if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_IN) {
1049 ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), bufferData.size());
1050 }
1051 } while (0);
1052
1053 if (ret == UEC_OK) {
1054 asyncContext->status = napi_ok;
1055 asyncContext->dataSize = bufferData.size();
1056 } else {
1057 asyncContext->status = napi_generic_failure;
1058 asyncContext->dataSize = 0;
1059 }
1060 };
1061
__anon4ef8f19d0802(napi_env env, napi_status status, void *data) 1062 static auto g_controlTransferComplete = [](napi_env env, napi_status status, void *data) {
1063 USBControlTransferAsyncContext *asyncContext = reinterpret_cast<USBControlTransferAsyncContext *>(data);
1064 napi_value queryResult = nullptr;
1065
1066 if (asyncContext->status == napi_ok) {
1067 napi_create_int32(env, asyncContext->dataSize, &queryResult);
1068 } else {
1069 USB_HILOGD(MODULE_JS_NAPI, "ControlTransfer failed");
1070 napi_create_int32(env, -1, &queryResult);
1071 }
1072 ProcessPromise(env, *asyncContext, queryResult);
1073 napi_delete_async_work(env, asyncContext->work);
1074 delete asyncContext;
1075 };
1076
GetControlTransferParam(napi_env env,napi_callback_info info)1077 static std::tuple<bool, USBDevicePipe, PipeControlParam, int32_t> GetControlTransferParam(
1078 napi_env env, napi_callback_info info)
1079 {
1080 size_t argc = PARAM_COUNT_3;
1081 napi_value argv[PARAM_COUNT_3] = {nullptr};
1082 napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1083 if (status != napi_ok) {
1084 USB_HILOGE(MODULE_JS_NAPI, "ControlTransfer failed to get cb info\n");
1085 return {false, {}, {}, {}};
1086 }
1087
1088 if (argc < PARAM_COUNT_2) {
1089 USB_HILOGE(MODULE_JS_NAPI, "The function at least takes two arguments.\n");
1090 ThrowBusinessError(env, SYSPARAM_INVALID_INPUT, "The function at least takes two arguments.");
1091 return {false, {}, {}, {}};
1092 }
1093
1094 // pipe param
1095 napi_valuetype type;
1096 napi_typeof(env, argv[INDEX_0], &type);
1097 if (type != napi_object) {
1098 USB_HILOGE(MODULE_JS_NAPI, "index 0 wrong argument type, object expected.\n");
1099 ThrowBusinessError(env, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
1100 return {false, {}, {}, {}};
1101 }
1102
1103 USBDevicePipe pipe;
1104 ParseUsbDevicePipe(env, argv[INDEX_0], pipe);
1105
1106 // control params
1107 PipeControlParam controlParam = {0};
1108 ParsePipeControlParam(env, argv[INDEX_1], controlParam);
1109
1110 // timeOut param
1111 int32_t timeOut = 0;
1112 if (argc > PARAM_COUNT_2) {
1113 napi_typeof(env, argv[INDEX_2], &type);
1114 if (type == napi_number) {
1115 napi_get_value_int32(env, argv[INDEX_2], &timeOut);
1116 } else {
1117 USB_HILOGW(MODULE_JS_NAPI, "index 2 wrong argument type, number expected.");
1118 }
1119 }
1120
1121 return {true, pipe, controlParam, timeOut};
1122 }
1123
PipeControlTransfer(napi_env env,napi_callback_info info)1124 static napi_value PipeControlTransfer(napi_env env, napi_callback_info info)
1125 {
1126 auto [res, pipe, controlParam, timeOut] = GetControlTransferParam(env, info);
1127 if (!res) {
1128 USB_HILOGE(MODULE_JS_NAPI, "GetControlTransferParam failed.");
1129 return nullptr;
1130 }
1131
1132 auto asyncContext = new (std::nothrow) USBControlTransferAsyncContext();
1133 if (asyncContext == nullptr) {
1134 USB_HILOGE(MODULE_JS_NAPI, "New USBControlTransferAsyncContext failed.");
1135 return nullptr;
1136 }
1137
1138 asyncContext->env = env;
1139 asyncContext->pipe = pipe;
1140 asyncContext->request = controlParam.request;
1141 asyncContext->target = controlParam.target;
1142 asyncContext->reqType = controlParam.reqType;
1143 asyncContext->value = controlParam.value;
1144 asyncContext->index = controlParam.index;
1145
1146 if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
1147 uint8_t *nativeArrayBuffer = new (std::nothrow) uint8_t[controlParam.dataLength];
1148 if (nativeArrayBuffer == nullptr) {
1149 USB_HILOGE(MODULE_JS_NAPI, "new failed");
1150 delete asyncContext;
1151 return nullptr;
1152 }
1153
1154 errno_t ret = memcpy_s(nativeArrayBuffer, controlParam.dataLength, controlParam.data, controlParam.dataLength);
1155 if (ret != EOK) {
1156 USB_HILOGE(MODULE_JS_NAPI, "memcpy_s failed\n");
1157 delete asyncContext;
1158 delete[] nativeArrayBuffer;
1159 return nullptr;
1160 }
1161 asyncContext->buffer = nativeArrayBuffer;
1162 } else {
1163 asyncContext->buffer = controlParam.data;
1164 }
1165
1166 asyncContext->bufferLength = controlParam.dataLength;
1167 asyncContext->timeOut = timeOut;
1168 napi_value result = nullptr;
1169 napi_create_promise(env, &asyncContext->deferred, &result);
1170
1171 napi_value resource = nullptr;
1172 napi_create_string_utf8(env, "PipeControlTransfer", NAPI_AUTO_LENGTH, &resource);
1173
1174 napi_create_async_work(env, nullptr, resource, g_controlTransferExecute, g_controlTransferComplete,
1175 reinterpret_cast<void *>(asyncContext), &asyncContext->work);
1176 napi_queue_async_work(env, asyncContext->work);
1177
1178 return result;
1179 }
1180
__anon4ef8f19d0902(napi_env env, void *data) 1181 static auto g_bulkTransferExecute = [](napi_env env, void *data) {
1182 USBBulkTransferAsyncContext *asyncContext = reinterpret_cast<USBBulkTransferAsyncContext *>(data);
1183 std::vector<uint8_t> bufferData(asyncContext->buffer, asyncContext->buffer + asyncContext->bufferLength);
1184 if (asyncContext->endpoint.GetDirection() == USB_ENDPOINT_DIR_OUT) {
1185 delete[] asyncContext->buffer;
1186 asyncContext->buffer = nullptr;
1187 }
1188
1189 int32_t ret;
1190 do {
1191 ret = asyncContext->pipe.BulkTransfer(asyncContext->endpoint, bufferData, asyncContext->timeOut);
1192 if (ret != UEC_OK) {
1193 USB_HILOGE(MODULE_JS_NAPI, "ControlTransferExecute failed");
1194 break;
1195 }
1196
1197 if (asyncContext->endpoint.GetDirection() == USB_ENDPOINT_DIR_IN) {
1198 ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), bufferData.size());
1199 }
1200 } while (0);
1201
1202 USB_HILOGD(MODULE_JS_NAPI, "call pipe result %{public}d", ret);
1203 if (ret == UEC_OK) {
1204 asyncContext->status = napi_ok;
1205 asyncContext->dataSize = bufferData.size();
1206 } else {
1207 asyncContext->status = napi_generic_failure;
1208 asyncContext->dataSize = 0;
1209 }
1210 };
1211
__anon4ef8f19d0a02(napi_env env, napi_status status, void *data) 1212 static auto g_bulkTransferComplete = [](napi_env env, napi_status status, void *data) {
1213 USBBulkTransferAsyncContext *asyncContext = reinterpret_cast<USBBulkTransferAsyncContext *>(data);
1214 napi_value queryResult = nullptr;
1215 if (asyncContext->status == napi_ok) {
1216 napi_create_int32(env, asyncContext->dataSize, &queryResult);
1217 } else {
1218 USB_HILOGE(MODULE_JS_NAPI, "BulkTransfer failed");
1219 napi_create_int32(env, -1, &queryResult);
1220 }
1221 ProcessPromise(env, *asyncContext, queryResult);
1222 napi_delete_async_work(env, asyncContext->work);
1223 delete asyncContext;
1224 };
1225
GetDescriptorOnBulkTransferParam(napi_env env,napi_value data,USBBulkTransferAsyncContext & asyncContext,const USBEndpoint & ep)1226 static bool GetDescriptorOnBulkTransferParam(napi_env env, napi_value data,
1227 USBBulkTransferAsyncContext &asyncContext, const USBEndpoint &ep)
1228 {
1229 uint8_t *buffer = nullptr;
1230 size_t offset = 0;
1231 size_t bufferSize = 0;
1232 bool hasBuffer = NapiUtil::JsUint8ArrayParse(env, data, &buffer, bufferSize, offset);
1233 if (!hasBuffer) {
1234 USB_HILOGE(MODULE_JS_NAPI, "BulkTransfer wrong argument, buffer is null");
1235 return false;
1236 }
1237 asyncContext.env = env;
1238 asyncContext.endpoint = ep;
1239
1240 if (ep.GetDirection() == USB_ENDPOINT_DIR_OUT) {
1241 uint8_t *nativeArrayBuffer = new (std::nothrow) uint8_t[bufferSize];
1242 RETURN_IF_WITH_RET(nativeArrayBuffer == nullptr, false);
1243
1244 errno_t ret = memcpy_s(nativeArrayBuffer, bufferSize, buffer, bufferSize);
1245 if (ret != EOK) {
1246 USB_HILOGE(MODULE_JS_NAPI, "memcpy_s failed\n");
1247 delete[] nativeArrayBuffer;
1248 return false;
1249 }
1250
1251 asyncContext.buffer = nativeArrayBuffer;
1252 } else {
1253 asyncContext.buffer = buffer;
1254 }
1255 asyncContext.bufferLength = bufferSize;
1256 return true;
1257 }
1258
GetBulkTransferParams(napi_env env,napi_callback_info info,USBBulkTransferAsyncContext & asyncContext)1259 static bool GetBulkTransferParams(napi_env env, napi_callback_info info, USBBulkTransferAsyncContext &asyncContext)
1260 {
1261 size_t argc = PARAM_COUNT_4;
1262 napi_value argv[PARAM_COUNT_4] = {nullptr};
1263 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
1264 USB_ASSERT_RETURN_FALSE(
1265 env, (argc >= PARAM_COUNT_3), SYSPARAM_INVALID_INPUT, "The function at least takes three arguments.");
1266
1267 napi_valuetype type;
1268 USBDevicePipe pipe;
1269 napi_typeof(env, argv[INDEX_0], &type);
1270 USB_ASSERT_RETURN_FALSE(
1271 env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
1272 ParseUsbDevicePipe(env, argv[INDEX_0], pipe);
1273 asyncContext.pipe = pipe;
1274
1275 USBEndpoint ep;
1276 napi_typeof(env, argv[INDEX_1], &type);
1277 USB_ASSERT_RETURN_FALSE(
1278 env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of endpoint must be USBEndpoint.");
1279 ParseEndpointObj(env, argv[INDEX_1], ep);
1280
1281 int32_t timeOut = 0;
1282 if (argc > PARAM_COUNT_3) {
1283 napi_typeof(env, argv[INDEX_3], &type);
1284 if (type == napi_number) {
1285 napi_get_value_int32(env, argv[INDEX_3], &timeOut);
1286 } else {
1287 USB_HILOGW(MODULE_JS_NAPI, "The type of timeOut must be number.");
1288 }
1289 }
1290
1291 if (!GetDescriptorOnBulkTransferParam(env, argv[INDEX_2], asyncContext, ep)) {
1292 USB_HILOGE(MODULE_JS_NAPI, "get asyncContext failed.");
1293 return false;
1294 }
1295 asyncContext.timeOut = timeOut;
1296 return true;
1297 }
1298
PipeBulkTransfer(napi_env env,napi_callback_info info)1299 static napi_value PipeBulkTransfer(napi_env env, napi_callback_info info)
1300 {
1301 auto asyncContext = new (std::nothrow) USBBulkTransferAsyncContext();
1302 if (asyncContext == nullptr) {
1303 USB_HILOGE(MODULE_JS_NAPI, "Create USBBulkTransferAsyncContext failed.");
1304 return nullptr;
1305 }
1306
1307 napi_value result = nullptr;
1308 napi_create_promise(env, &asyncContext->deferred, &result);
1309 if (!GetBulkTransferParams(env, info, *asyncContext)) {
1310 USB_HILOGE(MODULE_JS_NAPI, "end call invalid arg");
1311 asyncContext->status = napi_invalid_arg;
1312 napi_value queryResult = nullptr;
1313 napi_create_int32(env, -1, &queryResult);
1314 ProcessPromise(env, *asyncContext, queryResult);
1315 delete asyncContext;
1316 return result;
1317 }
1318
1319 napi_value resource = nullptr;
1320 napi_create_string_utf8(env, "PipeBulkTransfer", NAPI_AUTO_LENGTH, &resource);
1321
1322 napi_status status = napi_create_async_work(env, nullptr, resource, g_bulkTransferExecute, g_bulkTransferComplete,
1323 reinterpret_cast<void *>(asyncContext), &asyncContext->work);
1324 if (status != napi_ok) {
1325 USB_HILOGE(MODULE_JS_NAPI, "create async work failed");
1326 return result;
1327 }
1328 napi_queue_async_work(env, asyncContext->work);
1329
1330 return result;
1331 }
1332
PipeClose(napi_env env,napi_callback_info info)1333 static napi_value PipeClose(napi_env env, napi_callback_info info)
1334 {
1335 size_t argc = PARAM_COUNT_1;
1336 napi_value argv[PARAM_COUNT_1] = {nullptr};
1337
1338 NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
1339 USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
1340
1341 napi_value obj = argv[INDEX_0];
1342 napi_valuetype type;
1343 napi_typeof(env, obj, &type);
1344 USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
1345
1346 USBDevicePipe pipe;
1347 ParseUsbDevicePipe(env, obj, pipe);
1348 int32_t ret = pipe.Close();
1349 napi_value result;
1350 napi_create_int32(env, ret, &result);
1351
1352 return result;
1353 }
1354
GetVersion(napi_env env,napi_callback_info info)1355 static napi_value GetVersion(napi_env env, napi_callback_info info)
1356 {
1357 auto version = g_usbClient.GetVersion();
1358 USB_HILOGD(MODULE_JS_NAPI, "version is %{public}s", version.c_str());
1359 napi_value result;
1360 napi_create_string_utf8(env, version.c_str(), NAPI_AUTO_LENGTH, &result);
1361 return result;
1362 }
1363
ToInt32Value(napi_env env,int32_t value)1364 static napi_value ToInt32Value(napi_env env, int32_t value)
1365 {
1366 napi_value staticValue = nullptr;
1367 napi_create_int32(env, value, &staticValue);
1368 return staticValue;
1369 }
1370
DeclareEnum(napi_env env,napi_value exports)1371 static napi_value DeclareEnum(napi_env env, napi_value exports)
1372 {
1373 napi_property_descriptor desc[] = {
1374 /* Declare Enum PowerRoleType */
1375 DECLARE_NAPI_STATIC_PROPERTY("NONE", ToInt32Value(env, NONE)),
1376 DECLARE_NAPI_STATIC_PROPERTY("SOURCE", ToInt32Value(env, SOURCE)),
1377 DECLARE_NAPI_STATIC_PROPERTY("SINK", ToInt32Value(env, SINK)),
1378
1379 /* Declare Enum DataRoleType */
1380 DECLARE_NAPI_STATIC_PROPERTY("HOST", ToInt32Value(env, HOST)),
1381 DECLARE_NAPI_STATIC_PROPERTY("DEVICE", ToInt32Value(env, DEVICE)),
1382
1383 /* Declare Enum PortModeType */
1384 DECLARE_NAPI_STATIC_PROPERTY("UFP", ToInt32Value(env, UFP)),
1385 DECLARE_NAPI_STATIC_PROPERTY("DFP", ToInt32Value(env, DFP)),
1386 DECLARE_NAPI_STATIC_PROPERTY("DRP", ToInt32Value(env, DRP)),
1387 DECLARE_NAPI_STATIC_PROPERTY("NUM_MODES", ToInt32Value(env, NUM_MODES)),
1388
1389 /* Declare Enum USBRequestTargetType */
1390 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_DEVICE", ToInt32Value(env, USB_REQUEST_TARGET_DEVICE)),
1391 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_INTERFACE", ToInt32Value(env, USB_REQUEST_TARGET_INTERFACE)),
1392 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_ENDPOINT", ToInt32Value(env, USB_REQUEST_TARGET_ENDPOINT)),
1393 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_OTHER", ToInt32Value(env, USB_REQUEST_TARGET_OTHER)),
1394
1395 /* Declare Enum USBControlRequestType */
1396 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TYPE_STANDARD", ToInt32Value(env, USB_REQUEST_TYPE_STANDARD)),
1397 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TYPE_CLASS", ToInt32Value(env, USB_REQUEST_TYPE_CLASS)),
1398 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TYPE_VENDOR", ToInt32Value(env, USB_REQUEST_TYPE_VENDOR)),
1399
1400 /* Declare Enum USBRequestDirection */
1401 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_DIR_TO_DEVICE", ToInt32Value(env, USB_REQUEST_DIR_TO_DEVICE)),
1402 DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_DIR_FROM_DEVICE", ToInt32Value(env, USB_REQUEST_DIR_FROM_DEVICE)),
1403
1404 /* Declare Enum FunctionType */
1405 DECLARE_NAPI_STATIC_PROPERTY("ACM", ToInt32Value(env, ACM)),
1406 DECLARE_NAPI_STATIC_PROPERTY("ECM", ToInt32Value(env, ECM)),
1407 DECLARE_NAPI_STATIC_PROPERTY("HDC", ToInt32Value(env, HDC)),
1408 DECLARE_NAPI_STATIC_PROPERTY("MTP", ToInt32Value(env, MTP)),
1409 DECLARE_NAPI_STATIC_PROPERTY("PTP", ToInt32Value(env, PTP)),
1410 DECLARE_NAPI_STATIC_PROPERTY("RNDIS", ToInt32Value(env, RNDIS)),
1411 DECLARE_NAPI_STATIC_PROPERTY("MIDI", ToInt32Value(env, MIDI)),
1412 DECLARE_NAPI_STATIC_PROPERTY("AUDIO_SOURCE", ToInt32Value(env, AUDIO_SOURCE)),
1413 DECLARE_NAPI_STATIC_PROPERTY("NCM", ToInt32Value(env, NCM)),
1414 DECLARE_NAPI_STATIC_PROPERTY("STORAGE", ToInt32Value(env, STORAGE)),
1415 };
1416 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1417 return exports;
1418 }
1419
1420 EXTERN_C_START
1421 /*
1422 * function for module exports
1423 */
UsbInit(napi_env env,napi_value exports)1424 napi_value UsbInit(napi_env env, napi_value exports)
1425 {
1426 USB_HILOGD(MODULE_JS_NAPI, "enter");
1427
1428 napi_property_descriptor desc[] = {
1429 /* usb core */
1430 DECLARE_NAPI_FUNCTION("getDevices", CoreGetDevices),
1431 DECLARE_NAPI_FUNCTION("connectDevice", CoreConnectDevice),
1432 DECLARE_NAPI_FUNCTION("hasRight", CoreHasRight),
1433 DECLARE_NAPI_FUNCTION("requestRight", CoreRequestRight),
1434 DECLARE_NAPI_FUNCTION("usbFunctionsFromString", CoreUsbFunctionsFromString),
1435 DECLARE_NAPI_FUNCTION("usbFunctionsToString", CoreUsbFunctionsToString),
1436 DECLARE_NAPI_FUNCTION("setCurrentFunctions", CoreSetCurrentFunctions),
1437 DECLARE_NAPI_FUNCTION("getCurrentFunctions", CoreGetCurrentFunctions),
1438 DECLARE_NAPI_FUNCTION("getPorts", CoreGetPorts),
1439
1440 /* usb port */
1441 DECLARE_NAPI_FUNCTION("getSupportedModes", PortGetSupportedModes),
1442 DECLARE_NAPI_FUNCTION("setPortRoles", PortSetPortRole),
1443
1444 /* usb device pipe */
1445 DECLARE_NAPI_FUNCTION("claimInterface", PipeClaimInterface),
1446 DECLARE_NAPI_FUNCTION("releaseInterface", PipeReleaseInterface),
1447 DECLARE_NAPI_FUNCTION("bulkTransfer", PipeBulkTransfer),
1448 DECLARE_NAPI_FUNCTION("controlTransfer", PipeControlTransfer),
1449 DECLARE_NAPI_FUNCTION("setInterface", PipeSetInterface),
1450 DECLARE_NAPI_FUNCTION("setConfiguration", PipeSetConfiguration),
1451 DECLARE_NAPI_FUNCTION("getRawDescriptor", PipeGetRawDescriptors),
1452 DECLARE_NAPI_FUNCTION("getFileDescriptor", PipeGetFileDescriptor),
1453 DECLARE_NAPI_FUNCTION("closePipe", PipeClose),
1454
1455 /* fort test get usb service version */
1456 DECLARE_NAPI_FUNCTION("getVersion", GetVersion),
1457 DECLARE_NAPI_FUNCTION("addRight", DeviceAddRight),
1458 DECLARE_NAPI_FUNCTION("removeRight", DeviceRemoveRight),
1459 };
1460 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1461
1462 DeclareEnum(env, exports);
1463
1464 USB_HILOGD(MODULE_JS_NAPI, "return");
1465
1466 return exports;
1467 }
1468 EXTERN_C_END
1469