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 #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_publish_info.h"
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28 #include "nlohmann/json.hpp"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 enum DMBussinessErrorCode {
33     // Permission verify failed.
34     ERR_NO_PERMISSION = 201,
35     //The caller is not a system application.
36     ERR_NOT_SYSTEM_APP = 202,
37     // Input parameter error.
38     ERR_INVALID_PARAMS = 401,
39     // Failed to execute the function.
40     DM_ERR_FAILED = 11600101,
41     // Failed to obtain the service.
42     DM_ERR_OBTAIN_SERVICE = 11600102,
43     // Authentication invalid.
44     DM_ERR_AUTHENTICALTION_INVALID = 11600103,
45     // Discovery invalid.
46     DM_ERR_DISCOVERY_INVALID = 11600104,
47     // Publish invalid.
48     DM_ERR_PUBLISH_INVALID = 11600105,
49 };
50 void DeviceBasicInfoToJsArray(const napi_env &env,
51                               const std::vector<DmDeviceBasicInfo> &vecDevInfo,
52                               const int32_t idx, napi_value &arrayResult);
53 void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue,
54                    napi_value &result);
55 void SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str,
56                         napi_value &result);
57 void JsToDmPublishInfo(const napi_env &env, const napi_value &object, DmPublishInfo &info);
58 void JsToBindParam(const napi_env &env, const napi_value &object, std::string &bindParam, int32_t &bindType,
59                    bool &isMetaType);
60 void JsToDmDiscoveryExtra(const napi_env &env, const napi_value &object, std::string &extra);
61 bool JsToDiscoverTargetType(napi_env env, const napi_value &object, int32_t &discoverTargetType);
62 bool IsSystemApp();
63 void InsertMapParames(nlohmann::json &bindParamObj, std::map<std::string, std::string> &bindParamMap);
64 napi_value CreateBusinessError(napi_env env, int32_t errCode, bool isAsync = true);
65 bool CheckArgsCount(napi_env env, bool assertion, const std::string &message);
66 bool CheckArgsType(napi_env env, bool assertion, const std::string &paramName, const std::string &type);
67 bool IsFunctionType(napi_env env, napi_value value);
68 void DmDeviceBasicToJsObject(napi_env env, const DmDeviceBasicInfo &vecDevInfo, napi_value &result);
69 bool JsToStringAndCheck(napi_env env, napi_value value, const std::string &valueName, std::string &strValue);
70 } // namespace DistributedHardware
71 } // namespace OHOS
72 #endif // OHOS_DM_NATIVE_UTIL_H
73