• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 #ifndef OHOS_DM_NATIVE_UTIL_H
17 #define OHOS_DM_NATIVE_UTIL_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "dm_device_info.h"
25 #include "dm_device_profile_info.h"
26 #include "dm_publish_info.h"
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29 #include "json_object.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 enum DMBussinessErrorCode {
34     // Permission verify failed.
35     ERR_NO_PERMISSION = 201,
36     // The caller is not a system application.
37     ERR_NOT_SYSTEM_APP = 202,
38     // Input parameter error.
39     ERR_INVALID_PARAMS = 401,
40     // Failed to execute the function.
41     DM_ERR_FAILED = 11600101,
42     // Failed to obtain the service.
43     DM_ERR_OBTAIN_SERVICE = 11600102,
44     // Authentication invalid.
45     DM_ERR_AUTHENTICALTION_INVALID = 11600103,
46     // Discovery invalid.
47     DM_ERR_DISCOVERY_INVALID = 11600104,
48     // Publish invalid.
49     DM_ERR_PUBLISH_INVALID = 11600105,
50     // Get data from cloud failed.
51     DM_ERR_FROM_CLOUD_FAILED = 11600106,
52     // Need Login.
53     DM_ERR_NEED_LOGIN = 11600107,
54     // The device name contains non-compliant content.
55     DM_ERR_SCAS_CHECK_FAILED = 11600108,
56 };
57 void DeviceBasicInfoToJsArray(const napi_env &env,
58                               const std::vector<DmDeviceBasicInfo> &vecDevInfo,
59                               const int32_t idx, napi_value &arrayResult);
60 void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue,
61                    napi_value &result);
62 void SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str,
63                         napi_value &result);
64 void JsToDmPublishInfo(const napi_env &env, const napi_value &object, DmPublishInfo &info);
65 void JsToBindParam(const napi_env &env, const napi_value &object, std::string &bindParam, int32_t &bindType,
66                    bool &isMetaType);
67 void JsToDmDiscoveryExtra(const napi_env &env, const napi_value &object, std::string &extra);
68 bool JsToDiscoverTargetType(napi_env env, const napi_value &object, int32_t &discoverTargetType);
69 bool IsSystemApp();
70 void InsertMapParames(JsonObject &bindParamObj, std::map<std::string, std::string> &bindParamMap);
71 napi_value CreateBusinessError(napi_env env, int32_t errCode, bool isAsync = true);
72 bool CheckArgsCount(napi_env env, bool assertion, const std::string &message);
73 bool CheckArgsType(napi_env env, bool assertion, const std::string &paramName, const std::string &type);
74 bool IsFunctionType(napi_env env, napi_value value);
75 void DmDeviceBasicToJsObject(napi_env env, const DmDeviceBasicInfo &vecDevInfo, napi_value &result);
76 bool JsToStringAndCheck(napi_env env, napi_value value, const std::string &valueName, std::string &strValue);
77 
78 void JsObjectToStrVector(const napi_env &env, const napi_value &object, const std::string &fieldStr,
79     std::vector<std::string> &fieldRef);
80 void JsToDmDeviceProfileInfoFilterOptions(const napi_env &env, const napi_value &object,
81     DmDeviceProfileInfoFilterOptions &info);
82 void JsToDmDeviceIconInfoFilterOptions(const napi_env &env, const napi_value &object,
83     DmDeviceIconInfoFilterOptions &info);
84 void DmServiceProfileInfoToJsArray(const napi_env &env, const std::vector<DmServiceProfileInfo> &svrInfos,
85     napi_value &arrayResult);
86 void DmDeviceProfileInfoToJs(const napi_env &env, const DmDeviceProfileInfo &devInfo, napi_value &jsObj);
87 void DmDeviceIconInfoToJs(const napi_env &env, const DmDeviceIconInfo &deviceIconInfo, napi_value &jsObj);
88 void DmDeviceProfileInfoToJsArray(const napi_env &env, const std::vector<DmDeviceProfileInfo> &devInfos,
89     napi_value &arrayResult);
90 bool JsToDmDeviceProfileInfos(const napi_env &env, const napi_value &jsObj, std::vector<DmDeviceProfileInfo> &devInfos);
91 void JsToDiscoveryParam(const napi_env &env, const napi_value &object,
92     std::map<std::string, std::string> &discParam);
93 void JsToDmDeviceNetworkIdFilterOptions(const napi_env &env, const napi_value &object,
94     NetworkIdQueryFilter &info);
95 } // namespace DistributedHardware
96 } // namespace OHOS
97 #endif // OHOS_DM_NATIVE_UTIL_H
98