1 /*
2  * Copyright (c) 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 "dm_native_util.h"
17 
18 #include "dm_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 #include "ipc_skeleton.h"
22 #include "js_native_api.h"
23 #include "tokenid_kit.h"
24 
25 using namespace OHOS::Security::AccessToken;
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30 const std::string ERR_MESSAGE_NO_PERMISSION = "Permission verify failed.";
31 const std::string ERR_MESSAGE_NOT_SYSTEM_APP = "The caller is not a system application.";
32 const std::string ERR_MESSAGE_INVALID_PARAMS = "Input parameter error.";
33 const std::string ERR_MESSAGE_FAILED = "Failed to execute the function.";
34 const std::string ERR_MESSAGE_OBTAIN_SERVICE = "Failed to obtain the service.";
35 const std::string ERR_MESSAGE_AUTHENTICALTION_INVALID = "Authentication invalid.";
36 const std::string ERR_MESSAGE_DISCOVERY_INVALID = "Discovery invalid.";
37 const std::string ERR_MESSAGE_PUBLISH_INVALID = "Publish invalid.";
38 
39 const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_ONE = -1;
40 const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_TWO = -2;
41 const int32_t DM_NAPI_DESCRIPTION_BUF_LENGTH = 16384;
42 const int32_t DM_NAPI_BUF_LENGTH = 256;
43 
JsObjectToString(const napi_env &env, const napi_value &object, const std::string &fieldStr, char *dest, const int32_t destLen)44 void JsObjectToString(const napi_env &env, const napi_value &object, const std::string &fieldStr,
45                       char *dest, const int32_t destLen)
46 {
47     bool hasProperty = false;
48     NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty));
49     if (hasProperty) {
50         napi_value field = nullptr;
51         napi_valuetype valueType = napi_undefined;
52 
53         napi_get_named_property(env, object, fieldStr.c_str(), &field);
54         NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType));
55         if (!CheckArgsType(env, valueType == napi_string, fieldStr.c_str(), "string")) {
56             return;
57         }
58         size_t result = 0;
59         NAPI_CALL_RETURN_VOID(env, napi_get_value_string_utf8(env, field, dest, destLen, &result));
60     } else {
61         LOGE("devicemanager napi js to str no property: %{public}s", fieldStr.c_str());
62     }
63 }
64 
JsObjectToBool(const napi_env &env, const napi_value &object, const std::string &fieldStr, bool &fieldRef)65 void JsObjectToBool(const napi_env &env, const napi_value &object, const std::string &fieldStr,
66                     bool &fieldRef)
67 {
68     bool hasProperty = false;
69     NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty));
70     if (hasProperty) {
71         napi_value field = nullptr;
72         napi_valuetype valueType = napi_undefined;
73 
74         napi_get_named_property(env, object, fieldStr.c_str(), &field);
75         NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType));
76         if (!CheckArgsType(env, valueType == napi_boolean, fieldStr.c_str(), "bool")) {
77             return;
78         }
79         napi_get_value_bool(env, field, &fieldRef);
80     } else {
81         LOGE("devicemanager napi js to bool no property: %{public}s", fieldStr.c_str());
82     }
83 }
84 
JsObjectToInt(const napi_env &env, const napi_value &object, const std::string &fieldStr, int32_t &fieldRef)85 void JsObjectToInt(const napi_env &env, const napi_value &object, const std::string &fieldStr,
86                    int32_t &fieldRef)
87 {
88     bool hasProperty = false;
89     NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty));
90     if (hasProperty) {
91         napi_value field = nullptr;
92         napi_valuetype valueType = napi_undefined;
93 
94         napi_get_named_property(env, object, fieldStr.c_str(), &field);
95         NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType));
96         if (!CheckArgsType(env, valueType == napi_number, fieldStr.c_str(), "number")) {
97             return;
98         }
99         napi_get_value_int32(env, field, &fieldRef);
100     } else {
101         LOGE("devicemanager napi js to int no property: %{public}s", fieldStr.c_str());
102     }
103 }
104 
GetDeviceTypeById(DmDeviceType type)105 std::string GetDeviceTypeById(DmDeviceType type)
106 {
107     const static std::pair<DmDeviceType, std::string> mapArray[] = {
108         {DEVICE_TYPE_UNKNOWN, DEVICE_TYPE_UNKNOWN_STRING},
109         {DEVICE_TYPE_PHONE, DEVICE_TYPE_PHONE_STRING},
110         {DEVICE_TYPE_PAD, DEVICE_TYPE_PAD_STRING},
111         {DEVICE_TYPE_TV, DEVICE_TYPE_TV_STRING},
112         {DEVICE_TYPE_CAR, DEVICE_TYPE_CAR_STRING},
113         {DEVICE_TYPE_WATCH, DEVICE_TYPE_WATCH_STRING},
114         {DEVICE_TYPE_WIFI_CAMERA, DEVICE_TYPE_WIFICAMERA_STRING},
115         {DEVICE_TYPE_PC, DEVICE_TYPE_PC_STRING},
116         {DEVICE_TYPE_SMART_DISPLAY, DEVICE_TYPE_SMART_DISPLAY_STRING},
117         {DEVICE_TYPE_2IN1, DEVICE_TYPE_2IN1_STRING},
118     };
119     for (const auto& item : mapArray) {
120         if (item.first == type) {
121             return item.second;
122         }
123     }
124     return DEVICE_TYPE_UNKNOWN_STRING;
125 }
126 
CheckArgsVal(napi_env env, bool assertion, const std::string &param, const std::string &msg)127 bool CheckArgsVal(napi_env env, bool assertion, const std::string &param, const std::string &msg)
128 {
129     if (!(assertion)) {
130         std::string errMsg = ERR_MESSAGE_INVALID_PARAMS + "The value of " + param + ": " + msg;
131         napi_throw_error(env, std::to_string(ERR_INVALID_PARAMS).c_str(), errMsg.c_str());
132         return false;
133     }
134     return true;
135 }
136 }
137 
GenerateBusinessError(napi_env env, int32_t err, const std::string &msg)138 napi_value GenerateBusinessError(napi_env env, int32_t err, const std::string &msg)
139 {
140     napi_value businessError = nullptr;
141     NAPI_CALL(env, napi_create_object(env, &businessError));
142     napi_value errorCode = nullptr;
143     NAPI_CALL(env, napi_create_int32(env, err, &errorCode));
144     napi_value errorMessage = nullptr;
145     NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), NAPI_AUTO_LENGTH, &errorMessage));
146     NAPI_CALL(env, napi_set_named_property(env, businessError, "code", errorCode));
147     NAPI_CALL(env, napi_set_named_property(env, businessError, "message", errorMessage));
148 
149     return businessError;
150 }
151 
CheckArgsCount(napi_env env, bool assertion, const std::string &message)152 bool CheckArgsCount(napi_env env, bool assertion, const std::string &message)
153 {
154     if (!(assertion)) {
155         std::string errMsg = ERR_MESSAGE_INVALID_PARAMS + message;
156         napi_throw_error(env, std::to_string(ERR_INVALID_PARAMS).c_str(), errMsg.c_str());
157         return false;
158     }
159     return true;
160 }
161 
CheckArgsType(napi_env env, bool assertion, const std::string &paramName, const std::string &type)162 bool CheckArgsType(napi_env env, bool assertion, const std::string &paramName, const std::string &type)
163 {
164     if (!(assertion)) {
165         std::string errMsg = ERR_MESSAGE_INVALID_PARAMS + "The type of " + paramName +
166                 " must be " + type;
167         napi_throw_error(env, std::to_string(ERR_INVALID_PARAMS).c_str(), errMsg.c_str());
168         return false;
169     }
170     return true;
171 }
172 
CreateErrorForCall(napi_env env, int32_t code, const std::string &errMsg, bool isAsync)173 napi_value CreateErrorForCall(napi_env env, int32_t code, const std::string &errMsg, bool isAsync)
174 {
175     LOGI("CreateErrorForCall code:%{public}d, message:%{public}s", code, errMsg.c_str());
176     napi_value error = nullptr;
177     if (isAsync) {
178         napi_throw_error(env, std::to_string(code).c_str(), errMsg.c_str());
179     } else {
180         error = GenerateBusinessError(env, code, errMsg);
181     }
182     return error;
183 }
184 
CreateBusinessError(napi_env env, int32_t errCode, bool isAsync)185 napi_value CreateBusinessError(napi_env env, int32_t errCode, bool isAsync)
186 {
187     napi_value error = nullptr;
188     switch (errCode) {
189         case ERR_DM_NO_PERMISSION:
190             error = CreateErrorForCall(env, ERR_NO_PERMISSION, ERR_MESSAGE_NO_PERMISSION, isAsync);
191             break;
192         case ERR_DM_DISCOVERY_REPEATED:
193             error = CreateErrorForCall(env, DM_ERR_DISCOVERY_INVALID, ERR_MESSAGE_DISCOVERY_INVALID, isAsync);
194             break;
195         case ERR_DM_PUBLISH_REPEATED:
196             error = CreateErrorForCall(env, DM_ERR_PUBLISH_INVALID, ERR_MESSAGE_PUBLISH_INVALID, isAsync);
197             break;
198         case ERR_DM_AUTH_BUSINESS_BUSY:
199             error = CreateErrorForCall(env, DM_ERR_AUTHENTICALTION_INVALID,
200                 ERR_MESSAGE_AUTHENTICALTION_INVALID, isAsync);
201             break;
202         case ERR_DM_INPUT_PARA_INVALID:
203         case ERR_DM_UNSUPPORTED_AUTH_TYPE:
204             error = CreateErrorForCall(env, ERR_INVALID_PARAMS, ERR_MESSAGE_INVALID_PARAMS, isAsync);
205             break;
206         case ERR_DM_INIT_FAILED:
207             error = CreateErrorForCall(env, DM_ERR_OBTAIN_SERVICE, ERR_MESSAGE_OBTAIN_SERVICE, isAsync);
208             break;
209         case ERR_NOT_SYSTEM_APP:
210             error = CreateErrorForCall(env, ERR_NOT_SYSTEM_APP, ERR_MESSAGE_NOT_SYSTEM_APP, isAsync);
211             break;
212         default:
213             error = CreateErrorForCall(env, DM_ERR_FAILED, ERR_MESSAGE_FAILED, isAsync);
214             break;
215     }
216     return error;
217 }
218 
IsFunctionType(napi_env env, napi_value value)219 bool IsFunctionType(napi_env env, napi_value value)
220 {
221     napi_valuetype eventHandleType = napi_undefined;
222     napi_typeof(env, value, &eventHandleType);
223     return CheckArgsType(env, eventHandleType == napi_function, "callback", "function");
224 }
225 
SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str, napi_value &result)226 void SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str,
227                         napi_value &result)
228 {
229     napi_value value = nullptr;
230     napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &value);
231     napi_set_named_property(env, result, fieldStr.c_str(), value);
232 }
233 
SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue, napi_value &result)234 void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue,
235                    napi_value &result)
236 {
237     napi_value value = nullptr;
238     napi_create_int32(env, intValue, &value);
239     napi_set_named_property(env, result, fieldStr.c_str(), value);
240 }
241 
DeviceBasicInfoToJsArray(const napi_env &env, const std::vector<DmDeviceBasicInfo> &vecDevInfo, const int32_t idx, napi_value &arrayResult)242 void DeviceBasicInfoToJsArray(const napi_env &env,
243                               const std::vector<DmDeviceBasicInfo> &vecDevInfo, const int32_t idx,
244                               napi_value &arrayResult)
245 {
246     napi_value result = nullptr;
247     napi_create_object(env, &result);
248     DmDeviceBasicToJsObject(env, vecDevInfo[idx], result);
249 
250     napi_status status = napi_set_element(env, arrayResult, idx, result);
251     if (status != napi_ok) {
252         LOGE("DmDeviceBasicInfo To JsArray set element error: %{public}d", status);
253     }
254 }
255 
DmDeviceBasicToJsObject(napi_env env, const DmDeviceBasicInfo &devInfo, napi_value &result)256 void DmDeviceBasicToJsObject(napi_env env, const DmDeviceBasicInfo &devInfo, napi_value &result)
257 {
258     SetValueUtf8String(env, "deviceId", devInfo.deviceId, result);
259     SetValueUtf8String(env, "networkId", devInfo.networkId, result);
260     SetValueUtf8String(env, "deviceName", devInfo.deviceName, result);
261     std::string deviceType = GetDeviceTypeById(static_cast<DmDeviceType>(devInfo.deviceTypeId));
262     SetValueUtf8String(env, "deviceType", deviceType.c_str(), result);
263 }
264 
JsToDmPublishInfo(const napi_env &env, const napi_value &object, DmPublishInfo &info)265 void JsToDmPublishInfo(const napi_env &env, const napi_value &object, DmPublishInfo &info)
266 {
267     int32_t publishId = -1;
268     JsObjectToInt(env, object, "publishId", publishId);
269     info.publishId = publishId;
270 
271     int32_t mode = -1;
272     JsObjectToInt(env, object, "mode", mode);
273     info.mode = static_cast<DmDiscoverMode>(mode);
274 
275     int32_t freq = -1;
276     JsObjectToInt(env, object, "freq", freq);
277     info.freq = static_cast<DmExchangeFreq>(freq);
278 
279     JsObjectToBool(env, object, "ranging", info.ranging);
280     return;
281 }
282 
JsToBindParam(const napi_env &env, const napi_value &object, std::string &bindParam, int32_t &bindType, bool &isMetaType)283 void JsToBindParam(const napi_env &env, const napi_value &object, std::string &bindParam,
284                    int32_t &bindType, bool &isMetaType)
285 {
286     int32_t bindTypeTemp = -1;
287     JsObjectToInt(env, object, "bindType", bindTypeTemp);
288     bindType = bindTypeTemp;
289 
290     char appOperation[DM_NAPI_DESCRIPTION_BUF_LENGTH] = "";
291     JsObjectToString(env, object, "appOperation", appOperation, sizeof(appOperation));
292     char customDescription[DM_NAPI_DESCRIPTION_BUF_LENGTH] = "";
293     JsObjectToString(env, object, "customDescription", customDescription, sizeof(customDescription));
294     char targetPkgName[DM_NAPI_BUF_LENGTH] = "";
295     JsObjectToString(env, object, "targetPkgName", targetPkgName, sizeof(targetPkgName));
296     char metaType[DM_NAPI_BUF_LENGTH] = "";
297     JsObjectToString(env, object, "metaType", metaType, sizeof(metaType));
298     std::string metaTypeStr = metaType;
299     isMetaType = !metaTypeStr.empty();
300 
301     char pinCode[DM_NAPI_BUF_LENGTH] = "";
302     JsObjectToString(env, object, "pinCode", pinCode, sizeof(pinCode));
303     char authToken[DM_NAPI_BUF_LENGTH] = "";
304     JsObjectToString(env, object, "authToken", authToken, sizeof(authToken));
305     char brMac[DM_NAPI_BUF_LENGTH] = "";
306     JsObjectToString(env, object, "brMac", brMac, sizeof(brMac));
307     char bleMac[DM_NAPI_BUF_LENGTH] = "";
308     JsObjectToString(env, object, "bleMac", bleMac, sizeof(bleMac));
309     char wifiIP[DM_NAPI_BUF_LENGTH] = "";
310     JsObjectToString(env, object, "wifiIP", wifiIP, sizeof(wifiIP));
311 
312     int32_t wifiPort = -1;
313     JsObjectToInt(env, object, "wifiPort", wifiPort);
314     int32_t bindLevel = 0;
315     JsObjectToInt(env, object, "bindLevel", bindLevel);
316 
317     nlohmann::json jsonObj;
318     jsonObj[AUTH_TYPE] = bindType;
319     jsonObj[APP_OPERATION] = std::string(appOperation);
320     jsonObj[CUSTOM_DESCRIPTION] = std::string(customDescription);
321     jsonObj[PARAM_KEY_TARGET_PKG_NAME] = std::string(targetPkgName);
322     jsonObj[PARAM_KEY_META_TYPE] = metaTypeStr;
323     jsonObj[PARAM_KEY_PIN_CODE] = std::string(pinCode);
324     jsonObj[PARAM_KEY_AUTH_TOKEN] = std::string(authToken);
325     jsonObj[PARAM_KEY_BR_MAC] = std::string(brMac);
326     jsonObj[PARAM_KEY_BLE_MAC] = std::string(bleMac);
327     jsonObj[PARAM_KEY_WIFI_IP] = std::string(wifiIP);
328     jsonObj[PARAM_KEY_WIFI_PORT] = wifiPort;
329     jsonObj[BIND_LEVEL] = bindLevel;
330     bindParam = jsonObj.dump();
331 }
332 
IsSystemApp()333 bool IsSystemApp()
334 {
335     uint64_t tokenId = OHOS::IPCSkeleton::GetSelfTokenID();
336     return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
337 }
338 
JsToDiscoverTargetType(napi_env env, const napi_value &object, int32_t &discoverTargetType)339 bool JsToDiscoverTargetType(napi_env env, const napi_value &object, int32_t &discoverTargetType)
340 {
341     napi_valuetype objectType = napi_undefined;
342     napi_typeof(env, object, &objectType);
343     if (!(CheckArgsType(env, objectType == napi_object, "discoverParameter", "object or undefined"))) {
344         return false;
345     }
346     bool hasProperty = false;
347     napi_has_named_property(env, object, "discoverTargetType", &hasProperty);
348     if (hasProperty) {
349         napi_value field = nullptr;
350         napi_valuetype valueType = napi_undefined;
351         napi_get_named_property(env, object, "discoverTargetType", &field);
352         napi_typeof(env, field, &valueType);
353         if (!CheckArgsType(env, valueType == napi_number, "discoverTargetType", "number")) {
354             return false;
355         }
356         napi_get_value_int32(env, field, &discoverTargetType);
357         return true;
358     }
359     LOGE("discoverTargetType is invalid.");
360     return false;
361 }
362 
JsToDmDiscoveryExtra(const napi_env &env, const napi_value &object, std::string &extra)363 void JsToDmDiscoveryExtra(const napi_env &env, const napi_value &object, std::string &extra)
364 {
365     nlohmann::json jsonObj;
366     int32_t availableStatus = DM_NAPI_DISCOVER_EXTRA_INIT_ONE;
367     JsObjectToInt(env, object, "availableStatus", availableStatus);
368     if (availableStatus != DM_NAPI_DISCOVER_EXTRA_INIT_ONE) {
369         jsonObj["credible"] = availableStatus;
370     }
371 
372     int32_t discoverDistance = DM_NAPI_DISCOVER_EXTRA_INIT_ONE;
373     JsObjectToInt(env, object, "discoverDistance", discoverDistance);
374     if (discoverDistance != DM_NAPI_DISCOVER_EXTRA_INIT_ONE) {
375         jsonObj["range"] = discoverDistance;
376     }
377 
378     int32_t authenticationStatus = DM_NAPI_DISCOVER_EXTRA_INIT_ONE;
379     JsObjectToInt(env, object, "authenticationStatus", authenticationStatus);
380     if (authenticationStatus != DM_NAPI_DISCOVER_EXTRA_INIT_ONE) {
381         jsonObj["isTrusted"] = authenticationStatus;
382     }
383 
384     int32_t authorizationType = DM_NAPI_DISCOVER_EXTRA_INIT_TWO;
385     JsObjectToInt(env, object, "authorizationType", authorizationType);
386     if (authorizationType != DM_NAPI_DISCOVER_EXTRA_INIT_TWO) {
387         jsonObj["authForm"] = authorizationType;
388     }
389 
390     int32_t deviceType = DM_NAPI_DISCOVER_EXTRA_INIT_ONE;
391     JsObjectToInt(env, object, "deviceType", deviceType);
392     if (deviceType != DM_NAPI_DISCOVER_EXTRA_INIT_ONE) {
393         jsonObj["deviceType"] = deviceType;
394     }
395     extra = jsonObj.dump();
396     LOGI("JsToDmDiscoveryExtra, extra :%{public}s", extra.c_str());
397 }
398 
InsertMapParames(nlohmann::json &bindParamObj, std::map<std::string, std::string> &bindParamMap)399 void InsertMapParames(nlohmann::json &bindParamObj, std::map<std::string, std::string> &bindParamMap)
400 {
401     LOGI("Insert map parames start");
402     if (IsInt32(bindParamObj, AUTH_TYPE)) {
403         int32_t authType = bindParamObj[AUTH_TYPE].get<int32_t>();
404         bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TYPE, std::to_string(authType)));
405     }
406     if (IsString(bindParamObj, APP_OPERATION)) {
407         std::string appOperation = bindParamObj[APP_OPERATION].get<std::string>();
408         bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_APP_OPER, appOperation));
409     }
410     if (IsString(bindParamObj, CUSTOM_DESCRIPTION)) {
411         std::string appDescription = bindParamObj[CUSTOM_DESCRIPTION].get<std::string>();
412         bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_APP_DESC, appDescription));
413     }
414     if (IsString(bindParamObj, PARAM_KEY_TARGET_PKG_NAME)) {
415         std::string targetPkgName = bindParamObj[PARAM_KEY_TARGET_PKG_NAME].get<std::string>();
416         bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_TARGET_PKG_NAME, targetPkgName));
417     }
418     if (IsString(bindParamObj, PARAM_KEY_META_TYPE)) {
419         std::string metaType = bindParamObj[PARAM_KEY_META_TYPE].get<std::string>();
420         bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_META_TYPE, metaType));
421     }
422     if (IsString(bindParamObj, PARAM_KEY_PIN_CODE)) {
423         std::string pinCode = bindParamObj[PARAM_KEY_PIN_CODE].get<std::string>();
424         bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_PIN_CODE, pinCode));
425     }
426     if (IsString(bindParamObj, PARAM_KEY_AUTH_TOKEN)) {
427         std::string authToken = bindParamObj[PARAM_KEY_AUTH_TOKEN].get<std::string>();
428         bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TOKEN, authToken));
429     }
430     if (IsInt32(bindParamObj, BIND_LEVEL)) {
431         int32_t bindLevel = bindParamObj[BIND_LEVEL].get<std::int32_t>();
432         bindParamMap.insert(std::pair<std::string, std::string>(BIND_LEVEL, std::to_string(bindLevel)));
433     }
434 }
435 
JsToStringAndCheck(napi_env env, napi_value value, const std::string &valueName, std::string &strValue)436 bool JsToStringAndCheck(napi_env env, napi_value value, const std::string &valueName, std::string &strValue)
437 {
438     napi_valuetype deviceIdType = napi_undefined;
439     napi_typeof(env, value, &deviceIdType);
440     if (!CheckArgsType(env, deviceIdType == napi_string, valueName, "string")) {
441         return false;
442     }
443     size_t valueLen = 0;
444     napi_get_value_string_utf8(env, value, nullptr, 0, &valueLen);
445     if (!CheckArgsVal(env, valueLen > 0, valueName, "len == 0")) {
446         return false;
447     }
448     if (!CheckArgsVal(env, valueLen < DM_NAPI_BUF_LENGTH, valueName, "len >= MAXLEN")) {
449         return false;
450     }
451     char temp[DM_NAPI_BUF_LENGTH] = {0};
452     napi_get_value_string_utf8(env, value, temp, valueLen + 1, &valueLen);
453     strValue = temp;
454     return true;
455 }
456 } // namespace DistributedHardware
457 } // namespace OHOS
458