1 /*
2 * Copyright (c) 2022-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 "js_util.h"
17
18 #include "devicestatus_define.h"
19 #include "napi_constants.h"
20 #include "util_napi_error.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "JsUtil"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 namespace {
29 inline constexpr std::string_view GET_BOOLEAN { "napi_get_boolean" };
30 inline constexpr std::string_view COERCE_TO_BOOL { "napi_coerce_to_bool" };
31 inline constexpr std::string_view CREATE_ERROR { "napi_create_error" };
32 } // namespace
33
GetPrepareInfo(sptr<CallbackInfo> cb)34 napi_value JsUtil::GetPrepareInfo(sptr<CallbackInfo> cb)
35 {
36 CHKPP(cb);
37 CHKPP(cb->env);
38 return GetResult(cb->env, cb->data.prepareResult, cb->data.msgInfo);
39 }
40
GetActivateInfo(sptr<CallbackInfo> cb)41 napi_value JsUtil::GetActivateInfo(sptr<CallbackInfo> cb)
42 {
43 CHKPP(cb);
44 CHKPP(cb->env);
45 return GetResult(cb->env, cb->data.activateResult, cb->data.msgInfo);
46 }
47
GetDeactivateInfo(sptr<CallbackInfo> cb)48 napi_value JsUtil::GetDeactivateInfo(sptr<CallbackInfo> cb)
49 {
50 CHKPP(cb);
51 CHKPP(cb->env);
52 return GetResult(cb->env, cb->data.deactivateResult, cb->data.msgInfo);
53 }
54
GetCrossingSwitchStateInfo(sptr<CallbackInfo> cb)55 napi_value JsUtil::GetCrossingSwitchStateInfo(sptr<CallbackInfo> cb)
56 {
57 CHKPP(cb);
58 CHKPP(cb->env);
59 napi_value ret = nullptr;
60 napi_value stateInfo = nullptr;
61 CHKRP(napi_create_int32(cb->env, cb->data.coordinationOpened ? 1 : 0, &ret),
62 CREATE_INT32);
63 CHKRP(napi_coerce_to_bool(cb->env, ret, &stateInfo), COERCE_TO_BOOL);
64 return stateInfo;
65 }
66
GetResult(napi_env env,bool result,const CoordinationMsgInfo & msgInfo)67 napi_value JsUtil::GetResult(napi_env env, bool result, const CoordinationMsgInfo &msgInfo)
68 {
69 CHKPP(env);
70 napi_value object = nullptr;
71 if (result) {
72 napi_get_undefined(env, &object);
73 return object;
74 }
75 std::string errMsg;
76 if (!GetErrMsg(msgInfo, errMsg)) {
77 FI_HILOGE("GetErrMsg failed");
78 return nullptr;
79 }
80 int32_t errCode = GetErrCode(msgInfo);
81 FI_HILOGI("errCode:%{public}d, msg:%{public}s", errCode, errMsg.c_str());
82 napi_value resultCode = nullptr;
83 CHKRP(napi_create_int32(env, errCode, &resultCode), CREATE_INT32);
84 napi_value resultMessage = nullptr;
85 CHKRP(napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &resultMessage),
86 CREATE_STRING_UTF8);
87 CHKRP(napi_create_error(env, nullptr, resultMessage, &object), CREATE_ERROR);
88 CHKRP(napi_set_named_property(env, object, ERR_CODE.c_str(), resultCode), SET_NAMED_PROPERTY);
89 return object;
90 }
91
GetErrMsg(const CoordinationMsgInfo & msgInfo,std::string & msg)92 bool JsUtil::GetErrMsg(const CoordinationMsgInfo &msgInfo, std::string &msg)
93 {
94 auto iter = MSG_MAP.find(msgInfo.msg);
95 if (iter == MSG_MAP.end()) {
96 FI_HILOGE("Error code:%{public}d is not founded in MSG_MAP", static_cast<int32_t> (msgInfo.msg));
97 return false;
98 }
99 msg = iter->second;
100 switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
101 case CoordinationErrCode::COORDINATION_OK: {
102 msg += "Everything is fine";
103 break;
104 }
105 case CoordinationErrCode::OPEN_SESSION_FAILED: {
106 msg += "Open session failed";
107 break;
108 }
109 case CoordinationErrCode::SEND_PACKET_FAILED: {
110 msg += "Send packet failed";
111 break;
112 }
113 case CoordinationErrCode::UNEXPECTED_START_CALL: {
114 msg += "Unexpected start call";
115 break;
116 }
117 case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
118 msg += "Worker thread timeout";
119 break;
120 }
121 default:
122 msg +="Softbus bind failed";
123 }
124 return true;
125 }
126
GetErrCode(const CoordinationMsgInfo & msgInfo)127 int32_t JsUtil::GetErrCode(const CoordinationMsgInfo &msgInfo)
128 {
129 switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
130 case CoordinationErrCode::OPEN_SESSION_FAILED: {
131 return CustomErrCode::OPEN_SESSION_FAILED;
132 }
133 case CoordinationErrCode::SEND_PACKET_FAILED: {
134 return CustomErrCode::SEND_PACKET_FAILED;
135 }
136 case CoordinationErrCode::UNEXPECTED_START_CALL: {
137 return CustomErrCode::UNEXPECTED_START_CALL;
138 }
139 case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
140 return CustomErrCode::WORKER_THREAD_TIMEOUT;
141 }
142 default:
143 return msgInfo.errCode;
144 }
145 }
146
GetCrossingSwitchStateResult(napi_env env,bool result)147 napi_value JsUtil::GetCrossingSwitchStateResult(napi_env env, bool result)
148 {
149 CHKPP(env);
150 napi_value state = nullptr;
151 CHKRP(napi_get_boolean(env, result, &state), GET_BOOLEAN);
152 napi_value object = nullptr;
153 CHKRP(napi_create_object(env, &object), CREATE_OBJECT);
154 CHKRP(napi_set_named_property(env, object, "state", state), SET_NAMED_PROPERTY);
155 return object;
156 }
157
IsSameHandle(napi_env env,napi_value handle,napi_ref ref)158 bool JsUtil::IsSameHandle(napi_env env, napi_value handle, napi_ref ref)
159 {
160 CHKPF(ref);
161 napi_handle_scope scope = nullptr;
162 napi_open_handle_scope(env, &scope);
163 CHKPF(scope);
164 napi_value tempHandler = nullptr;
165 CHKRF_SCOPE(env, napi_get_reference_value(env, ref, &tempHandler), GET_REFERENCE_VALUE, scope);
166 bool isEqual = false;
167 CHKRF_SCOPE(env, napi_strict_equals(env, handle, tempHandler, &isEqual), STRICT_EQUALS, scope);
168 napi_close_handle_scope(env, scope);
169 return isEqual;
170 }
171 } // namespace DeviceStatus
172 } // namespace Msdp
173 } // namespace OHOS
174