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