• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const int32_t INDEX_0 = 0;
44 const int32_t INDEX_1 = 1;
45 const int32_t INDEX_2 = 2;
46 const int32_t INDEX_3 = 3;
47 const int32_t PARAM_COUNT_0 = 0;
48 const int32_t PARAM_COUNT_1 = 1;
49 const int32_t PARAM_COUNT_2 = 2;
50 const int32_t PARAM_COUNT_3 = 3;
51 const int32_t PARAM_COUNT_4 = 4;
52 const int32_t STR_DEFAULT_SIZE = 256;
53 const 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] = {0};
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] = {0};
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] = {0};
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         napi_get_boolean(env, false, &result);
471     }
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] = {0};
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] = {0};
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 
__anon593695fc0102(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 
__anon593695fc0202(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] = {0};
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] = {0};
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 
596     napi_value result;
597     napi_create_int32(env, numFuncs, &result);
598 
599     return result;
600 }
601 
CoreUsbFunctionsToString(napi_env env,napi_callback_info info)602 static napi_value CoreUsbFunctionsToString(napi_env env, napi_callback_info info)
603 {
604     size_t argc = PARAM_COUNT_1;
605     napi_value argv[PARAM_COUNT_1] = {0};
606 
607     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
608     USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
609 
610     napi_valuetype type;
611     NAPI_CHECK(env, napi_typeof(env, argv[INDEX_0], &type), "Get args 1 type failed");
612     USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of funcs must be number.");
613 
614     int32_t funcs;
615     napi_get_value_int32(env, argv[INDEX_0], &funcs);
616     std::string strFuncs = g_usbClient.UsbFunctionsToString(funcs);
617 
618     napi_value result;
619     napi_create_string_utf8(env, strFuncs.c_str(), NAPI_AUTO_LENGTH, &result);
620 
621     return result;
622 }
623 
__anon593695fc0302(napi_env env, void *data) 624 static auto g_setCurrentFunctionExecute = [](napi_env env, void *data) {
625     USBFunctionAsyncContext *asyncContext = reinterpret_cast<USBFunctionAsyncContext *>(data);
626     int32_t ret = g_usbClient.SetCurrentFunctions(asyncContext->functions);
627     if (ret == UEC_OK) {
628         asyncContext->status = napi_ok;
629     } else {
630         asyncContext->status = napi_generic_failure;
631     }
632 };
633 
__anon593695fc0402(napi_env env, napi_status status, void *data) 634 static auto g_setCurrentFunctionComplete = [](napi_env env, napi_status status, void *data) {
635     USBFunctionAsyncContext *asyncContext = reinterpret_cast<USBFunctionAsyncContext *>(data);
636     napi_value queryResult = nullptr;
637     napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult);
638 
639     ProcessPromise(env, *asyncContext, queryResult);
640     napi_delete_async_work(env, asyncContext->work);
641     delete asyncContext;
642 };
643 
CoreSetCurrentFunctions(napi_env env,napi_callback_info info)644 static napi_value CoreSetCurrentFunctions(napi_env env, napi_callback_info info)
645 {
646     size_t argc = PARAM_COUNT_1;
647     napi_value argv[PARAM_COUNT_1] = {0};
648 
649     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
650     USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
651 
652     napi_valuetype type;
653     NAPI_CHECK(env, napi_typeof(env, argv[INDEX_0], &type), "Get args 1 type failed");
654     USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of funcs must be number.");
655 
656     int32_t funcs = 0;
657     napi_get_value_int32(env, argv[INDEX_0], &funcs);
658 
659     auto asyncContext = new (std::nothrow) USBFunctionAsyncContext();
660     if (asyncContext == nullptr) {
661         USB_HILOGE(MODULE_JS_NAPI, "Create USBFunctionAsyncContext failed");
662         return nullptr;
663     }
664 
665     asyncContext->env = env;
666     asyncContext->functions = funcs;
667     napi_value result = nullptr;
668     napi_create_promise(env, &asyncContext->deferred, &result);
669 
670     napi_value resource = nullptr;
671     napi_create_string_utf8(env, "SetCurrentFunctions", NAPI_AUTO_LENGTH, &resource);
672 
673     napi_create_async_work(env, nullptr, resource, g_setCurrentFunctionExecute, g_setCurrentFunctionComplete,
674         reinterpret_cast<void *>(asyncContext), &asyncContext->work);
675     napi_queue_async_work(env, asyncContext->work);
676 
677     return result;
678 }
679 
CoreGetCurrentFunctions(napi_env env,napi_callback_info info)680 static napi_value CoreGetCurrentFunctions(napi_env env, napi_callback_info info)
681 {
682     size_t argc = PARAM_COUNT_1;
683     napi_value argv[PARAM_COUNT_1] = {0};
684     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
685     USB_ASSERT(env, (argc == PARAM_COUNT_0), SYSPARAM_INVALID_INPUT, "The function takes no arguments.");
686 
687     int32_t cfuncs;
688     int32_t ret = g_usbClient.GetCurrentFunctions(cfuncs);
689     napi_value result;
690     if (ret != UEC_OK) {
691         napi_get_undefined(env, &result);
692         USB_HILOGE(MODULE_JS_NAPI, "end call get ports failed ret : %{public}d", ret);
693         return result;
694     }
695     napi_create_int32(env, cfuncs, &result);
696 
697     return result;
698 }
699 
CoreGetPorts(napi_env env,napi_callback_info info)700 static napi_value CoreGetPorts(napi_env env, napi_callback_info info)
701 {
702     size_t argc = PARAM_COUNT_1;
703     napi_value argv[PARAM_COUNT_1] = {0};
704     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
705     USB_ASSERT(env, (argc == PARAM_COUNT_0), SYSPARAM_INVALID_INPUT, "The function takes no arguments.");
706 
707     std::vector<UsbPort> ports;
708     int32_t ret = g_usbClient.GetPorts(ports);
709     napi_value result;
710     if (ret != UEC_OK) {
711         napi_get_undefined(env, &result);
712         USB_HILOGE(MODULE_JS_NAPI, "end call get ports failed ret : %{public}d", ret);
713         return result;
714     }
715 
716     napi_create_array(env, &result);
717     for (uint32_t i = 0; i < ports.size(); ++i) {
718         napi_value port;
719         napi_create_object(env, &port);
720         NapiUtil::SetValueInt32(env, "id", ports[i].id, port);
721         NapiUtil::SetValueInt32(env, "supportedModes", ports[i].supportedModes, port);
722         napi_value usbPortStatus;
723         napi_create_object(env, &usbPortStatus);
724         NapiUtil::SetValueInt32(env, "currentMode", ports[i].usbPortStatus.currentMode, usbPortStatus);
725         NapiUtil::SetValueInt32(env, "currentPowerRole", ports[i].usbPortStatus.currentPowerRole, usbPortStatus);
726         NapiUtil::SetValueInt32(env, "currentDataRole", ports[i].usbPortStatus.currentDataRole, usbPortStatus);
727         napi_set_named_property(env, port, "status", usbPortStatus);
728         napi_set_element(env, result, i, port);
729     }
730 
731     return result;
732 }
733 
734 /* ============================================= Usb Port ============================================= */
735 
PortGetSupportedModes(napi_env env,napi_callback_info info)736 static napi_value PortGetSupportedModes(napi_env env, napi_callback_info info)
737 {
738     size_t argc = PARAM_COUNT_1;
739     napi_value args[PARAM_COUNT_1] = {0};
740 
741     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), "Get call back info failed");
742     USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
743 
744     napi_valuetype type;
745     NAPI_CHECK(env, napi_typeof(env, args[INDEX_0], &type), "Get args 1 type failed");
746     USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of portId must be number.");
747 
748     int32_t id = 0;
749     int32_t result = 0;
750     napi_get_value_int32(env, args[INDEX_0], &id);
751     int32_t ret = g_usbClient.GetSupportedModes(id, result);
752     if (ret) {
753         USB_HILOGD(MODULE_JS_NAPI, "false ret = %{public}d", ret);
754     }
755     napi_value napiValue = nullptr;
756     NAPI_CHECK(env, napi_create_int32(env, result, &napiValue), "Create int32 failed");
757 
758     return napiValue;
759 }
760 
__anon593695fc0502(napi_env env, void *data) 761 static auto g_setPortRoleExecute = [](napi_env env, void *data) {
762     USBPortRoleAsyncContext *asyncContext = reinterpret_cast<USBPortRoleAsyncContext *>(data);
763     int32_t ret = g_usbClient.SetPortRole(asyncContext->portId, asyncContext->powerRole, asyncContext->dataRole);
764     if (ret == UEC_OK) {
765         asyncContext->status = napi_ok;
766     } else {
767         asyncContext->status = napi_generic_failure;
768     }
769 };
770 
__anon593695fc0602(napi_env env, napi_status status, void *data) 771 static auto g_setPortRoleComplete = [](napi_env env, napi_status status, void *data) {
772     USBPortRoleAsyncContext *asyncContext = reinterpret_cast<USBPortRoleAsyncContext *>(data);
773     napi_value queryResult = nullptr;
774 
775     napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult);
776 
777     ProcessPromise(env, *asyncContext, queryResult);
778     napi_delete_async_work(env, asyncContext->work);
779     delete asyncContext;
780 };
781 
PortSetPortRole(napi_env env,napi_callback_info info)782 static napi_value PortSetPortRole(napi_env env, napi_callback_info info)
783 {
784     size_t argc = PARAM_COUNT_3;
785     napi_value args[PARAM_COUNT_3] = {0};
786 
787     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), "Get call back info failed");
788     USB_ASSERT(env, (argc >= PARAM_COUNT_3), SYSPARAM_INVALID_INPUT, "The function takes three arguments.");
789 
790     napi_valuetype type;
791     NAPI_CHECK(env, napi_typeof(env, args[INDEX_0], &type), "Get args 1 type failed");
792     USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of portId must be number.");
793     NAPI_CHECK(env, napi_typeof(env, args[INDEX_1], &type), "Get args 2 type failed");
794     USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of powerRole must be number.");
795     NAPI_CHECK(env, napi_typeof(env, args[INDEX_2], &type), "Get args 3 type failed");
796     USB_ASSERT(env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of dataRole must be number.");
797 
798     int32_t id = 0;
799     napi_get_value_int32(env, args[INDEX_0], &id);
800     int32_t powerRole = 0;
801     napi_get_value_int32(env, args[INDEX_1], &powerRole);
802     int32_t dataRole = 0;
803     napi_get_value_int32(env, args[INDEX_2], &dataRole);
804 
805     auto asyncContext = new (std::nothrow) USBPortRoleAsyncContext();
806     if (asyncContext == nullptr) {
807         USB_HILOGE(MODULE_JS_NAPI, "Create USBPortRoleAsyncContext failed");
808         return nullptr;
809     }
810 
811     asyncContext->env = env;
812     asyncContext->portId = id;
813     asyncContext->dataRole = dataRole;
814     asyncContext->powerRole = powerRole;
815 
816     napi_value result = nullptr;
817     napi_create_promise(env, &asyncContext->deferred, &result);
818 
819     napi_value resource = nullptr;
820     napi_create_string_utf8(env, "PortSetPortRole", NAPI_AUTO_LENGTH, &resource);
821 
822     napi_create_async_work(env, nullptr, resource, g_setPortRoleExecute, g_setPortRoleComplete,
823         reinterpret_cast<void *>(asyncContext), &asyncContext->work);
824     napi_queue_async_work(env, asyncContext->work);
825 
826     return result;
827 }
828 
PipeClaimInterface(napi_env env,napi_callback_info info)829 static napi_value PipeClaimInterface(napi_env env, napi_callback_info info)
830 {
831     size_t argc = PARAM_COUNT_3;
832     napi_value argv[PARAM_COUNT_3] = {0};
833 
834     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
835     USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function at least takes two arguments.");
836 
837     napi_value obj = argv[INDEX_0];
838     napi_valuetype type;
839     napi_typeof(env, obj, &type);
840     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
841 
842     USBDevicePipe pipe;
843     ParseUsbDevicePipe(env, obj, pipe);
844 
845     UsbInterface interface;
846     napi_value obj2 = argv[INDEX_1];
847     napi_typeof(env, obj2, &type);
848     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of iface must be USBInterface.");
849     ParseInterfaceObj(env, obj2, interface);
850 
851     bool isForce = false;
852     if (argc >= PARAM_COUNT_3) {
853         napi_typeof(env, argv[INDEX_2], &type);
854         USB_ASSERT(env, type == napi_boolean, SYSPARAM_INVALID_INPUT, "The type of force must be boolean.");
855         napi_get_value_bool(env, argv[INDEX_2], &isForce);
856     }
857 
858     int32_t ret = pipe.ClaimInterface(interface, isForce);
859     USB_HILOGD(MODULE_JS_NAPI, "pipe call ClaimInterface ret: %{public}d", ret);
860     napi_value result;
861     napi_create_int32(env, ret, &result);
862 
863     return result;
864 }
865 
PipeReleaseInterface(napi_env env,napi_callback_info info)866 static napi_value PipeReleaseInterface(napi_env env, napi_callback_info info)
867 {
868     size_t argc = PARAM_COUNT_2;
869     napi_value argv[PARAM_COUNT_2] = {0};
870 
871     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
872     USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function takes two arguments.");
873 
874     napi_value obj = argv[INDEX_0];
875     napi_valuetype type;
876     napi_typeof(env, obj, &type);
877     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
878 
879     USBDevicePipe pipe;
880     ParseUsbDevicePipe(env, obj, pipe);
881 
882     UsbInterface interface;
883     napi_value obj2 = argv[INDEX_1];
884     napi_typeof(env, obj2, &type);
885     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of iface must be USBInterface.");
886     ParseInterfaceObj(env, obj2, interface);
887     int32_t ret = pipe.ReleaseInterface(interface);
888     USB_HILOGD(MODULE_JS_NAPI, "pipe call PipeReleaseInterface ret: %{public}d", ret);
889     napi_value result;
890     napi_create_int32(env, ret, &result);
891 
892     return result;
893 }
894 
PipeSetInterface(napi_env env,napi_callback_info info)895 static napi_value PipeSetInterface(napi_env env, napi_callback_info info)
896 {
897     size_t argc = PARAM_COUNT_2;
898     napi_value argv[PARAM_COUNT_2] = {0};
899     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
900     USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function takes two arguments.");
901 
902     napi_value pipeObj = argv[INDEX_0];
903     napi_valuetype type;
904     napi_typeof(env, pipeObj, &type);
905     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
906 
907     USBDevicePipe pipe;
908     ParseUsbDevicePipe(env, pipeObj, pipe);
909 
910     napi_value interfaceObj = argv[INDEX_1];
911     napi_typeof(env, interfaceObj, &type);
912     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of iface must be USBInterface.");
913 
914     UsbInterface interface;
915     ParseInterfaceObj(env, interfaceObj, interface);
916     int32_t ret = g_usbClient.SetInterface(pipe, interface);
917     napi_value result;
918     napi_create_int32(env, ret, &result);
919 
920     return result;
921 }
922 
PipeSetConfiguration(napi_env env,napi_callback_info info)923 static napi_value PipeSetConfiguration(napi_env env, napi_callback_info info)
924 {
925     size_t argc = PARAM_COUNT_2;
926     napi_value argv[PARAM_COUNT_2] = {0};
927     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
928     USB_ASSERT(env, (argc >= PARAM_COUNT_2), SYSPARAM_INVALID_INPUT, "The function takes two arguments.");
929 
930     napi_valuetype type;
931     napi_value pipeObj = argv[INDEX_0];
932 
933     napi_typeof(env, pipeObj, &type);
934     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
935     USBDevicePipe pipe;
936     ParseUsbDevicePipe(env, pipeObj, pipe);
937 
938     napi_value configObj = argv[INDEX_1];
939     napi_typeof(env, configObj, &type);
940     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of config must be USBConfig.");
941     USBConfig config;
942     ParseConfigObj(env, configObj, config);
943 
944     int32_t ret = g_usbClient.SetConfiguration(pipe, config);
945     napi_value result;
946     napi_create_int32(env, ret, &result);
947 
948     return result;
949 }
950 
PipeGetRawDescriptors(napi_env env,napi_callback_info info)951 static napi_value PipeGetRawDescriptors(napi_env env, napi_callback_info info)
952 {
953     size_t argc = PARAM_COUNT_1;
954     napi_value argv[PARAM_COUNT_1] = {0};
955 
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_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
958     napi_value obj = argv[INDEX_0];
959     napi_valuetype type;
960     napi_typeof(env, obj, &type);
961     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
962 
963     USBDevicePipe pipe;
964     ParseUsbDevicePipe(env, obj, pipe);
965 
966     napi_value result;
967     std::vector<uint8_t> bufferData;
968     int32_t ret = g_usbClient.GetRawDescriptors(pipe, bufferData);
969     if (ret == UEC_OK) {
970         NapiUtil::Uint8ArrayToJsValue(env, bufferData, bufferData.size(), result);
971     } else {
972         napi_get_undefined(env, &result);
973     }
974 
975     return result;
976 }
977 
PipeGetFileDescriptor(napi_env env,napi_callback_info info)978 static napi_value PipeGetFileDescriptor(napi_env env, napi_callback_info info)
979 {
980     size_t argc = PARAM_COUNT_1;
981     napi_value argv[PARAM_COUNT_1] = {0};
982 
983     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
984     USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
985     napi_value obj = argv[INDEX_0];
986     napi_valuetype type;
987     napi_typeof(env, obj, &type);
988     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
989 
990     USBDevicePipe pipe;
991     ParseUsbDevicePipe(env, obj, pipe);
992 
993     int32_t fd = -1;
994     napi_value result;
995     g_usbClient.GetFileDescriptor(pipe, fd);
996     napi_create_int32(env, fd, &result);
997 
998     return result;
999 }
1000 
__anon593695fc0702(napi_env env, void *data) 1001 static auto g_controlTransferExecute = [](napi_env env, void *data) {
1002     USBControlTransferAsyncContext *asyncContext = (USBControlTransferAsyncContext *)data;
1003     std::vector<uint8_t> bufferData(asyncContext->buffer, asyncContext->buffer + asyncContext->bufferLength);
1004     if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
1005         delete[] asyncContext->buffer;
1006         asyncContext->buffer = nullptr;
1007     }
1008 
1009     const UsbCtrlTransfer tctrl = {
1010         asyncContext->reqType, asyncContext->request, asyncContext->value, asyncContext->index, asyncContext->timeOut};
1011     int32_t ret;
1012     do {
1013         ret = asyncContext->pipe.ControlTransfer(tctrl, bufferData);
1014         if (ret != UEC_OK) {
1015             USB_HILOGE(MODULE_JS_NAPI, "ControlTransferExecute failed");
1016             break;
1017         }
1018 
1019         if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_IN) {
1020             ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), bufferData.size());
1021         }
1022     } while (0);
1023 
1024     if (ret == UEC_OK) {
1025         asyncContext->status = napi_ok;
1026         asyncContext->dataSize = bufferData.size();
1027     } else {
1028         asyncContext->status = napi_generic_failure;
1029         asyncContext->dataSize = 0;
1030     }
1031 };
1032 
__anon593695fc0802(napi_env env, napi_status status, void *data) 1033 static auto g_controlTransferComplete = [](napi_env env, napi_status status, void *data) {
1034     USBControlTransferAsyncContext *asyncContext = reinterpret_cast<USBControlTransferAsyncContext *>(data);
1035     napi_value queryResult = nullptr;
1036 
1037     if (asyncContext->status == napi_ok) {
1038         napi_create_int32(env, asyncContext->dataSize, &queryResult);
1039     } else {
1040         USB_HILOGD(MODULE_JS_NAPI, "ControlTransfer failed");
1041         napi_create_int32(env, -1, &queryResult);
1042     }
1043     ProcessPromise(env, *asyncContext, queryResult);
1044     napi_delete_async_work(env, asyncContext->work);
1045     delete asyncContext;
1046 };
1047 
GetControlTransferParam(napi_env env,napi_callback_info info)1048 static std::tuple<bool, USBDevicePipe, PipeControlParam, int32_t> GetControlTransferParam(
1049     napi_env env, napi_callback_info info)
1050 {
1051     size_t argc = PARAM_COUNT_3;
1052     napi_value argv[PARAM_COUNT_3] = {0};
1053     napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1054     if (status != napi_ok) {
1055         USB_HILOGE(MODULE_JS_NAPI, "ControlTransfer failed to get cb info\n");
1056         return {false, {}, {}, {}};
1057     }
1058 
1059     if (argc < PARAM_COUNT_2) {
1060         USB_HILOGE(MODULE_JS_NAPI, "The function at least takes two arguments.\n");
1061         ThrowBusinessError(env, SYSPARAM_INVALID_INPUT, "The function at least takes two arguments.");
1062         return {false, {}, {}, {}};
1063     }
1064 
1065     // pipe param
1066     napi_valuetype type;
1067     napi_typeof(env, argv[INDEX_0], &type);
1068     if (type != napi_object) {
1069         USB_HILOGE(MODULE_JS_NAPI, "index 0 wrong argument type, object expected.\n");
1070         ThrowBusinessError(env, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
1071         return {false, {}, {}, {}};
1072     }
1073 
1074     USBDevicePipe pipe;
1075     ParseUsbDevicePipe(env, argv[INDEX_0], pipe);
1076 
1077     // control params
1078     PipeControlParam controlParam = {0};
1079     ParsePipeControlParam(env, argv[INDEX_1], controlParam);
1080 
1081     // timeOut param
1082     int32_t timeOut = 0;
1083     if (argc > PARAM_COUNT_2) {
1084         napi_typeof(env, argv[INDEX_2], &type);
1085         if (type != napi_number) {
1086             USB_HILOGE(MODULE_JS_NAPI, "index 2 wrong argument type, number expected.");
1087             ThrowBusinessError(env, SYSPARAM_INVALID_INPUT, "The type of timeOut must be positive number.");
1088             return {false, {}, {}, {}};
1089         }
1090         napi_get_value_int32(env, argv[INDEX_2], &timeOut);
1091     }
1092 
1093     return {true, pipe, controlParam, timeOut};
1094 }
1095 
PipeControlTransfer(napi_env env,napi_callback_info info)1096 static napi_value PipeControlTransfer(napi_env env, napi_callback_info info)
1097 {
1098     auto [res, pipe, controlParam, timeOut] = GetControlTransferParam(env, info);
1099     if (!res) {
1100         USB_HILOGE(MODULE_JS_NAPI, "GetControlTransferParam failed.");
1101         return nullptr;
1102     }
1103 
1104     auto asyncContext = new (std::nothrow) USBControlTransferAsyncContext();
1105     if (asyncContext == nullptr) {
1106         USB_HILOGE(MODULE_JS_NAPI, "New USBControlTransferAsyncContext failed.");
1107         return nullptr;
1108     }
1109 
1110     asyncContext->env = env;
1111     asyncContext->pipe = pipe;
1112     asyncContext->request = controlParam.request;
1113     asyncContext->target = controlParam.target;
1114     asyncContext->reqType = controlParam.reqType;
1115     asyncContext->value = controlParam.value;
1116     asyncContext->index = controlParam.index;
1117 
1118     if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
1119         uint8_t *nativeArraybuffer = new (std::nothrow) uint8_t[controlParam.dataLength];
1120         if (nativeArraybuffer == nullptr) {
1121             USB_HILOGE(MODULE_JS_NAPI, "new failed");
1122             delete asyncContext;
1123             return nullptr;
1124         }
1125 
1126         errno_t ret = memcpy_s(nativeArraybuffer, controlParam.dataLength, controlParam.data, controlParam.dataLength);
1127         if (ret != EOK) {
1128             USB_HILOGE(MODULE_JS_NAPI, "memcpy_s failed\n");
1129             delete asyncContext;
1130             delete[] nativeArraybuffer;
1131             return nullptr;
1132         }
1133         asyncContext->buffer = nativeArraybuffer;
1134     } else {
1135         asyncContext->buffer = controlParam.data;
1136     }
1137 
1138     asyncContext->bufferLength = controlParam.dataLength;
1139     asyncContext->timeOut = timeOut;
1140     napi_value result = nullptr;
1141     napi_create_promise(env, &asyncContext->deferred, &result);
1142 
1143     napi_value resource = nullptr;
1144     napi_create_string_utf8(env, "PipeControlTransfer", NAPI_AUTO_LENGTH, &resource);
1145 
1146     napi_create_async_work(env, nullptr, resource, g_controlTransferExecute, g_controlTransferComplete,
1147         reinterpret_cast<void *>(asyncContext), &asyncContext->work);
1148     napi_queue_async_work(env, asyncContext->work);
1149 
1150     return result;
1151 }
1152 
__anon593695fc0902(napi_env env, void *data) 1153 static auto g_bulkTransferExecute = [](napi_env env, void *data) {
1154     USBBulkTransferAsyncContext *asyncContext = reinterpret_cast<USBBulkTransferAsyncContext *>(data);
1155     std::vector<uint8_t> bufferData(asyncContext->buffer, asyncContext->buffer + asyncContext->bufferLength);
1156     if (asyncContext->endpoint.GetDirection() == USB_ENDPOINT_DIR_OUT) {
1157         delete[] asyncContext->buffer;
1158         asyncContext->buffer = nullptr;
1159     }
1160 
1161     int32_t ret;
1162     do {
1163         ret = asyncContext->pipe.BulkTransfer(asyncContext->endpoint, bufferData, asyncContext->timeOut);
1164         if (ret != UEC_OK) {
1165             USB_HILOGE(MODULE_JS_NAPI, "ControlTransferExecute failed");
1166             break;
1167         }
1168 
1169         if (asyncContext->endpoint.GetDirection() == USB_ENDPOINT_DIR_IN) {
1170             ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), bufferData.size());
1171         }
1172     } while (0);
1173 
1174     USB_HILOGD(MODULE_JS_NAPI, "call pipe result %{public}d", ret);
1175     if (ret == UEC_OK) {
1176         asyncContext->status = napi_ok;
1177         asyncContext->dataSize = bufferData.size();
1178     } else {
1179         asyncContext->status = napi_generic_failure;
1180         asyncContext->dataSize = 0;
1181     }
1182 };
1183 
__anon593695fc0a02(napi_env env, napi_status status, void *data) 1184 static auto g_bulkTransferComplete = [](napi_env env, napi_status status, void *data) {
1185     USBBulkTransferAsyncContext *asyncContext = reinterpret_cast<USBBulkTransferAsyncContext *>(data);
1186     napi_value queryResult = nullptr;
1187     if (asyncContext->status == napi_ok) {
1188         napi_create_int32(env, asyncContext->dataSize, &queryResult);
1189     } else {
1190         USB_HILOGE(MODULE_JS_NAPI, "BulkTransfer failed");
1191         napi_create_int32(env, -1, &queryResult);
1192     }
1193     ProcessPromise(env, *asyncContext, queryResult);
1194     napi_delete_async_work(env, asyncContext->work);
1195     delete asyncContext;
1196 };
1197 
GetBulkTransferParams(napi_env env,napi_callback_info info,USBBulkTransferAsyncContext & asyncContext)1198 static bool GetBulkTransferParams(napi_env env, napi_callback_info info, USBBulkTransferAsyncContext &asyncContext)
1199 {
1200     size_t argc = PARAM_COUNT_4;
1201     napi_value argv[PARAM_COUNT_4] = {0};
1202     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
1203     USB_ASSERT_RETURN_FALSE(
1204         env, (argc >= PARAM_COUNT_3), SYSPARAM_INVALID_INPUT, "The function at least takes three arguments.");
1205 
1206     napi_valuetype type;
1207     USBDevicePipe pipe;
1208     napi_typeof(env, argv[INDEX_0], &type);
1209     USB_ASSERT_RETURN_FALSE(
1210         env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
1211     ParseUsbDevicePipe(env, argv[INDEX_0], pipe);
1212 
1213     USBEndpoint ep;
1214     napi_typeof(env, argv[INDEX_1], &type);
1215     USB_ASSERT_RETURN_FALSE(
1216         env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of endpoint must be USBEndpoint.");
1217     ParseEndpointObj(env, argv[INDEX_1], ep);
1218 
1219     int32_t timeOut = 0;
1220     if (argc > PARAM_COUNT_3) {
1221         napi_typeof(env, argv[INDEX_3], &type);
1222         USB_ASSERT_RETURN_FALSE(
1223             env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of timeOut must be number.");
1224         napi_get_value_int32(env, argv[INDEX_3], &timeOut);
1225     }
1226 
1227     uint8_t *buffer = nullptr;
1228     size_t offset = 0;
1229     size_t bufferSize = 0;
1230     bool hasBuffer = NapiUtil::JsUint8ArrayParse(env, argv[INDEX_2], &buffer, bufferSize, offset);
1231     if (!hasBuffer) {
1232         USB_HILOGE(MODULE_JS_NAPI, "BulkTransfer wrong argument, buffer is null");
1233         return false;
1234     }
1235     asyncContext.env = env;
1236     asyncContext.pipe = pipe;
1237     asyncContext.endpoint = ep;
1238 
1239     if (ep.GetDirection() == USB_ENDPOINT_DIR_OUT) {
1240         uint8_t *nativeArraybuffer = new (std::nothrow) uint8_t[bufferSize];
1241         RETURN_IF_WITH_RET(nativeArraybuffer == nullptr, false);
1242 
1243         errno_t ret = memcpy_s(nativeArraybuffer, bufferSize, buffer, bufferSize);
1244         if (ret != EOK) {
1245             USB_HILOGE(MODULE_JS_NAPI, "memcpy_s failed\n");
1246             delete[] nativeArraybuffer;
1247             return false;
1248         }
1249 
1250         asyncContext.buffer = nativeArraybuffer;
1251     } else {
1252         asyncContext.buffer = buffer;
1253     }
1254     asyncContext.bufferLength = bufferSize;
1255     asyncContext.timeOut = timeOut;
1256     return true;
1257 }
1258 
PipeBulkTransfer(napi_env env,napi_callback_info info)1259 static napi_value PipeBulkTransfer(napi_env env, napi_callback_info info)
1260 {
1261     auto asyncContext = new (std::nothrow) USBBulkTransferAsyncContext();
1262     if (asyncContext == nullptr) {
1263         USB_HILOGE(MODULE_JS_NAPI, "Create USBBulkTransferAsyncContext failed.");
1264         return nullptr;
1265     }
1266 
1267     napi_value result = nullptr;
1268     napi_create_promise(env, &asyncContext->deferred, &result);
1269     if (!GetBulkTransferParams(env, info, *asyncContext)) {
1270         USB_HILOGE(MODULE_JS_NAPI, "end call invalid arg");
1271         asyncContext->status = napi_invalid_arg;
1272         napi_value queryResult = nullptr;
1273         napi_create_int32(env, -1, &queryResult);
1274         ProcessPromise(env, *asyncContext, queryResult);
1275         delete asyncContext;
1276         return result;
1277     }
1278 
1279     napi_value resource = nullptr;
1280     napi_create_string_utf8(env, "PipeBulkTransfer", NAPI_AUTO_LENGTH, &resource);
1281 
1282     napi_status status = napi_create_async_work(env, nullptr, resource, g_bulkTransferExecute, g_bulkTransferComplete,
1283         reinterpret_cast<void *>(asyncContext), &asyncContext->work);
1284     if (status != napi_ok) {
1285         USB_HILOGE(MODULE_JS_NAPI, "create async work failed");
1286         return result;
1287     }
1288     napi_queue_async_work(env, asyncContext->work);
1289 
1290     return result;
1291 }
1292 
PipeClose(napi_env env,napi_callback_info info)1293 static napi_value PipeClose(napi_env env, napi_callback_info info)
1294 {
1295     size_t argc = PARAM_COUNT_1;
1296     napi_value argv[PARAM_COUNT_1] = {0};
1297 
1298     NAPI_CHECK(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), "Get call back info failed");
1299     USB_ASSERT(env, (argc >= PARAM_COUNT_1), SYSPARAM_INVALID_INPUT, "The function takes one argument.");
1300 
1301     napi_value obj = argv[INDEX_0];
1302     napi_valuetype type;
1303     napi_typeof(env, obj, &type);
1304     USB_ASSERT(env, type == napi_object, SYSPARAM_INVALID_INPUT, "The type of pipe must be USBDevicePipe.");
1305 
1306     USBDevicePipe pipe;
1307     ParseUsbDevicePipe(env, obj, pipe);
1308     int32_t ret = pipe.Close();
1309     napi_value result;
1310     napi_create_int32(env, ret, &result);
1311 
1312     return result;
1313 }
1314 
GetVersion(napi_env env,napi_callback_info info)1315 static napi_value GetVersion(napi_env env, napi_callback_info info)
1316 {
1317     auto version = g_usbClient.GetVersion();
1318     USB_HILOGD(MODULE_JS_NAPI, "version is %{public}s", version.c_str());
1319     napi_value result;
1320     napi_create_string_utf8(env, version.c_str(), NAPI_AUTO_LENGTH, &result);
1321     return result;
1322 }
1323 
ToInt32Value(napi_env env,int32_t value)1324 static napi_value ToInt32Value(napi_env env, int32_t value)
1325 {
1326     napi_value staticValue = nullptr;
1327     napi_create_int32(env, value, &staticValue);
1328     return staticValue;
1329 }
1330 
DeclareEnum(napi_env env,napi_value exports)1331 static napi_value DeclareEnum(napi_env env, napi_value exports)
1332 {
1333     napi_property_descriptor desc[] = {
1334         /* Declare Enum PowerRoleType */
1335         DECLARE_NAPI_STATIC_PROPERTY("NONE", ToInt32Value(env, NONE)),
1336         DECLARE_NAPI_STATIC_PROPERTY("SOURCE", ToInt32Value(env, SOURCE)),
1337         DECLARE_NAPI_STATIC_PROPERTY("SINK", ToInt32Value(env, SINK)),
1338 
1339         /* Declare Enum DataRoleType */
1340         DECLARE_NAPI_STATIC_PROPERTY("HOST", ToInt32Value(env, HOST)),
1341         DECLARE_NAPI_STATIC_PROPERTY("DEVICE", ToInt32Value(env, DEVICE)),
1342 
1343         /* Declare Enum PortModeType */
1344         DECLARE_NAPI_STATIC_PROPERTY("UFP", ToInt32Value(env, UFP)),
1345         DECLARE_NAPI_STATIC_PROPERTY("DFP", ToInt32Value(env, DFP)),
1346         DECLARE_NAPI_STATIC_PROPERTY("DRP", ToInt32Value(env, DRP)),
1347         DECLARE_NAPI_STATIC_PROPERTY("NUM_MODES", ToInt32Value(env, NUM_MODES)),
1348 
1349         /* Declare Enum USBRequestTargetType */
1350         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_DEVICE", ToInt32Value(env, USB_REQUEST_TARGET_DEVICE)),
1351         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_INTERFACE", ToInt32Value(env, USB_REQUEST_TARGET_INTERFACE)),
1352         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_ENDPOINT", ToInt32Value(env, USB_REQUEST_TARGET_ENDPOINT)),
1353         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TARGET_OTHER", ToInt32Value(env, USB_REQUEST_TARGET_OTHER)),
1354 
1355         /* Declare Enum USBControlRequestType */
1356         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TYPE_STANDARD", ToInt32Value(env, USB_REQUEST_TYPE_STANDARD)),
1357         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TYPE_CLASS", ToInt32Value(env, USB_REQUEST_TYPE_CLASS)),
1358         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_TYPE_VENDOR", ToInt32Value(env, USB_REQUEST_TYPE_VENDOR)),
1359 
1360         /* Declare Enum USBRequestDirection */
1361         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_DIR_TO_DEVICE", ToInt32Value(env, USB_REQUEST_DIR_TO_DEVICE)),
1362         DECLARE_NAPI_STATIC_PROPERTY("USB_REQUEST_DIR_FROM_DEVICE", ToInt32Value(env, USB_REQUEST_DIR_FROM_DEVICE)),
1363 
1364         /* Declare Enum FunctionType */
1365         DECLARE_NAPI_STATIC_PROPERTY("ACM", ToInt32Value(env, ACM)),
1366         DECLARE_NAPI_STATIC_PROPERTY("ECM", ToInt32Value(env, ECM)),
1367         DECLARE_NAPI_STATIC_PROPERTY("HDC", ToInt32Value(env, HDC)),
1368         DECLARE_NAPI_STATIC_PROPERTY("MTP", ToInt32Value(env, MTP)),
1369         DECLARE_NAPI_STATIC_PROPERTY("PTP", ToInt32Value(env, PTP)),
1370         DECLARE_NAPI_STATIC_PROPERTY("RNDIS", ToInt32Value(env, RNDIS)),
1371         DECLARE_NAPI_STATIC_PROPERTY("MIDI", ToInt32Value(env, MIDI)),
1372         DECLARE_NAPI_STATIC_PROPERTY("AUDIO_SOURCE", ToInt32Value(env, AUDIO_SOURCE)),
1373         DECLARE_NAPI_STATIC_PROPERTY("NCM", ToInt32Value(env, NCM)),
1374         DECLARE_NAPI_STATIC_PROPERTY("STORAGE", ToInt32Value(env, STORAGE)),
1375     };
1376     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1377     return exports;
1378 }
1379 
1380 EXTERN_C_START
1381 /*
1382  * function for module exports
1383  */
UsbInit(napi_env env,napi_value exports)1384 napi_value UsbInit(napi_env env, napi_value exports)
1385 {
1386     USB_HILOGD(MODULE_JS_NAPI, "enter");
1387 
1388     napi_property_descriptor desc[] = {
1389         /* usb core */
1390         DECLARE_NAPI_FUNCTION("getDevices", CoreGetDevices),
1391         DECLARE_NAPI_FUNCTION("connectDevice", CoreConnectDevice),
1392         DECLARE_NAPI_FUNCTION("hasRight", CoreHasRight),
1393         DECLARE_NAPI_FUNCTION("requestRight", CoreRequestRight),
1394         DECLARE_NAPI_FUNCTION("usbFunctionsFromString", CoreUsbFunctionsFromString),
1395         DECLARE_NAPI_FUNCTION("usbFunctionsToString", CoreUsbFunctionsToString),
1396         DECLARE_NAPI_FUNCTION("setCurrentFunctions", CoreSetCurrentFunctions),
1397         DECLARE_NAPI_FUNCTION("getCurrentFunctions", CoreGetCurrentFunctions),
1398         DECLARE_NAPI_FUNCTION("getPorts", CoreGetPorts),
1399 
1400         /* usb port */
1401         DECLARE_NAPI_FUNCTION("getSupportedModes", PortGetSupportedModes),
1402         DECLARE_NAPI_FUNCTION("setPortRoles", PortSetPortRole),
1403 
1404         /* usb device pipe */
1405         DECLARE_NAPI_FUNCTION("claimInterface", PipeClaimInterface),
1406         DECLARE_NAPI_FUNCTION("releaseInterface", PipeReleaseInterface),
1407         DECLARE_NAPI_FUNCTION("bulkTransfer", PipeBulkTransfer),
1408         DECLARE_NAPI_FUNCTION("controlTransfer", PipeControlTransfer),
1409         DECLARE_NAPI_FUNCTION("setInterface", PipeSetInterface),
1410         DECLARE_NAPI_FUNCTION("setConfiguration", PipeSetConfiguration),
1411         DECLARE_NAPI_FUNCTION("getRawDescriptor", PipeGetRawDescriptors),
1412         DECLARE_NAPI_FUNCTION("getFileDescriptor", PipeGetFileDescriptor),
1413         DECLARE_NAPI_FUNCTION("closePipe", PipeClose),
1414 
1415         /* fort test get usb service version */
1416         DECLARE_NAPI_FUNCTION("getVersion", GetVersion),
1417         DECLARE_NAPI_FUNCTION("addRight", DeviceAddRight),
1418         DECLARE_NAPI_FUNCTION("removeRight", DeviceRemoveRight),
1419     };
1420     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1421 
1422     DeclareEnum(env, exports);
1423 
1424     USB_HILOGD(MODULE_JS_NAPI, "return");
1425 
1426     return exports;
1427 }
1428 EXTERN_C_END
1429