• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 inline constexpr std::string_view GET_VALUE_INT32 { "napi_get_value_int32" };
33 } // namespace
34 
GetPrepareInfo(sptr<CallbackInfo> cb)35 napi_value JsUtil::GetPrepareInfo(sptr<CallbackInfo> cb)
36 {
37     CHKPP(cb);
38     CHKPP(cb->env);
39     return GetResult(cb->env, cb->data.prepareResult, cb->data.msgInfo);
40 }
41 
GetActivateInfo(sptr<CallbackInfo> cb)42 napi_value JsUtil::GetActivateInfo(sptr<CallbackInfo> cb)
43 {
44     CHKPP(cb);
45     CHKPP(cb->env);
46     return GetResult(cb->env, cb->data.activateResult, cb->data.msgInfo);
47 }
48 
GetDeactivateInfo(sptr<CallbackInfo> cb)49 napi_value JsUtil::GetDeactivateInfo(sptr<CallbackInfo> cb)
50 {
51     CHKPP(cb);
52     CHKPP(cb->env);
53     return GetResult(cb->env, cb->data.deactivateResult, cb->data.msgInfo);
54 }
55 
GetCrossingSwitchStateInfo(sptr<CallbackInfo> cb)56 napi_value JsUtil::GetCrossingSwitchStateInfo(sptr<CallbackInfo> cb)
57 {
58     CHKPP(cb);
59     CHKPP(cb->env);
60     napi_value ret = nullptr;
61     napi_value stateInfo = nullptr;
62     CHKRP(napi_create_int32(cb->env, cb->data.coordinationOpened ? 1 : 0, &ret),
63         CREATE_INT32);
64     CHKRP(napi_coerce_to_bool(cb->env, ret, &stateInfo), COERCE_TO_BOOL);
65     return stateInfo;
66 }
67 
GetResult(napi_env env,bool result,const CoordinationMsgInfo & msgInfo)68 napi_value JsUtil::GetResult(napi_env env, bool result, const CoordinationMsgInfo &msgInfo)
69 {
70     CHKPP(env);
71     napi_value object = nullptr;
72     if (result) {
73         napi_get_undefined(env, &object);
74         return object;
75     }
76     std::string errMsg;
77     if (!GetErrMsg(msgInfo, errMsg)) {
78         FI_HILOGE("GetErrMsg failed");
79         return nullptr;
80     }
81     int32_t errCode = GetErrCode(msgInfo);
82     FI_HILOGI("errCode:%{public}d, msg:%{public}s", errCode, errMsg.c_str());
83     napi_value resultCode = nullptr;
84     CHKRP(napi_create_int32(env, errCode, &resultCode), CREATE_INT32);
85     napi_value resultMessage = nullptr;
86     CHKRP(napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &resultMessage),
87         CREATE_STRING_UTF8);
88     CHKRP(napi_create_error(env, nullptr, resultMessage, &object), CREATE_ERROR);
89     CHKRP(napi_set_named_property(env, object, ERR_CODE.c_str(), resultCode), SET_NAMED_PROPERTY);
90     return object;
91 }
92 
GetErrMsg(const CoordinationMsgInfo & msgInfo,std::string & msg)93 bool JsUtil::GetErrMsg(const CoordinationMsgInfo &msgInfo, std::string &msg)
94 {
95     auto iter = MSG_MAP.find(msgInfo.msg);
96     if (iter == MSG_MAP.end()) {
97         FI_HILOGE("Error code:%{public}d is not founded in MSG_MAP", static_cast<int32_t> (msgInfo.msg));
98         return false;
99     }
100     msg = iter->second;
101     switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
102         case CoordinationErrCode::COORDINATION_OK: {
103             msg += "Everything is fine";
104             break;
105         }
106         case CoordinationErrCode::OPEN_SESSION_FAILED: {
107             msg += "Open session failed";
108             break;
109         }
110         case CoordinationErrCode::SEND_PACKET_FAILED: {
111             msg += "Send packet failed";
112             break;
113         }
114         case CoordinationErrCode::UNEXPECTED_START_CALL: {
115             msg += "Unexpected start call";
116             break;
117         }
118         case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
119             msg += "Worker thread timeout";
120             break;
121         }
122         case CoordinationErrCode::NOT_AOLLOW_COOPERATE_WHEN_MOTION_DRAGGING: {
123             msg += "Not allow cooperate when motion dragging";
124             break;
125         }
126         default:
127             msg +="Softbus bind failed";
128     }
129     return true;
130 }
131 
GetErrCode(const CoordinationMsgInfo & msgInfo)132 int32_t JsUtil::GetErrCode(const CoordinationMsgInfo &msgInfo)
133 {
134     switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
135         case CoordinationErrCode::OPEN_SESSION_FAILED: {
136             return CustomErrCode::OPEN_SESSION_FAILED;
137         }
138         case CoordinationErrCode::SEND_PACKET_FAILED: {
139             return CustomErrCode::SEND_PACKET_FAILED;
140         }
141         case CoordinationErrCode::UNEXPECTED_START_CALL: {
142             return CustomErrCode::UNEXPECTED_START_CALL;
143         }
144         case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
145             return CustomErrCode::WORKER_THREAD_TIMEOUT;
146         }
147         case CoordinationErrCode::NOT_AOLLOW_COOPERATE_WHEN_MOTION_DRAGGING: {
148             return CustomErrCode::NOT_AOLLOW_COOPERATE_WHEN_MOTION_DRAGGING;
149         }
150         default:
151             return msgInfo.errCode;
152     }
153 }
154 
GetCrossingSwitchStateResult(napi_env env,bool result)155 napi_value JsUtil::GetCrossingSwitchStateResult(napi_env env, bool result)
156 {
157     CHKPP(env);
158     napi_value state = nullptr;
159     CHKRP(napi_get_boolean(env, result, &state), GET_BOOLEAN);
160     napi_value object = nullptr;
161     CHKRP(napi_create_object(env, &object), CREATE_OBJECT);
162     CHKRP(napi_set_named_property(env, object, "state", state), SET_NAMED_PROPERTY);
163     return object;
164 }
165 
IsSameHandle(napi_env env,napi_value handle,napi_ref ref)166 bool JsUtil::IsSameHandle(napi_env env, napi_value handle, napi_ref ref)
167 {
168     CHKPF(ref);
169     napi_handle_scope scope = nullptr;
170     napi_open_handle_scope(env, &scope);
171     CHKPF(scope);
172     napi_value tempHandler = nullptr;
173     CHKRF_SCOPE(env, napi_get_reference_value(env, ref, &tempHandler), GET_REFERENCE_VALUE, scope);
174     bool isEqual = false;
175     CHKRF_SCOPE(env, napi_strict_equals(env, handle, tempHandler, &isEqual), STRICT_EQUALS, scope);
176     napi_close_handle_scope(env, scope);
177     return isEqual;
178 }
179 
GetNamePropertyInt32(const napi_env & env,const napi_value & object,const std::string & name)180 int32_t JsUtil::GetNamePropertyInt32(const napi_env& env, const napi_value& object, const std::string& name)
181 {
182     napi_value napiValue = {};
183     CHKRF(napi_get_named_property(env, object, name.c_str(), &napiValue), GET_NAMED_PROPERTY);
184     napi_valuetype tmpType = napi_undefined;
185     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
186         FI_HILOGE("Call napi_typeof failed");
187         return RET_ERR;
188     }
189     if (tmpType != napi_number) {
190         FI_HILOGE("The value is not number");
191         return RET_ERR;
192     }
193     int32_t ret = 0;
194     CHKRF(napi_get_value_int32(env, napiValue, &ret), GET_VALUE_INT32);
195     return ret;
196 }
197 } // namespace DeviceStatus
198 } // namespace Msdp
199 } // namespace OHOS
200