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 "js_util_cooperate.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 "JsUtilCooperate"
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 
GetEnableInfo(sptr<CallbackInfo> cb)34 napi_value JsUtilCooperate::GetEnableInfo(sptr<CallbackInfo> cb)
35 {
36     CHKPP(cb);
37     CHKPP(cb->env);
38     return GetResult(cb->env, cb->data.enableResult, cb->data.msgInfo);
39 }
40 
GetStartInfo(sptr<CallbackInfo> cb)41 napi_value JsUtilCooperate::GetStartInfo(sptr<CallbackInfo> cb)
42 {
43     CHKPP(cb);
44     CHKPP(cb->env);
45     return GetResult(cb->env, cb->data.startResult, cb->data.msgInfo);
46 }
47 
GetStopInfo(sptr<CallbackInfo> cb)48 napi_value JsUtilCooperate::GetStopInfo(sptr<CallbackInfo> cb)
49 {
50     CHKPP(cb);
51     CHKPP(cb->env);
52     return GetResult(cb->env, cb->data.stopResult, cb->data.msgInfo);
53 }
54 
GetStateInfo(sptr<CallbackInfo> cb)55 napi_value JsUtilCooperate::GetStateInfo(sptr<CallbackInfo> cb)
56 {
57     CHKPP(cb);
58     CHKPP(cb->env);
59     napi_value ret = nullptr;
60     napi_value state = 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, &state), COERCE_TO_BOOL);
64     return state;
65 }
66 
GetResult(napi_env env,bool result,const CoordinationMsgInfo & msgInfo)67 napi_value JsUtilCooperate::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     napi_value resultCode = nullptr;
81     int32_t errorCode = GetErrCode(msgInfo);
82     FI_HILOGI("errorCode:%{public}d, msg:%{public}s", errorCode, errMsg.c_str());
83     CHKRP(napi_create_int32(env, errorCode, &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 
GetErrCode(const CoordinationMsgInfo & msgInfo)92 int32_t JsUtilCooperate::GetErrCode(const CoordinationMsgInfo &msgInfo)
93 {
94     switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
95         case CoordinationErrCode::OPEN_SESSION_FAILED: {
96             return CustomErrCode::OPEN_SESSION_FAILED;
97         }
98         case CoordinationErrCode::SEND_PACKET_FAILED: {
99             return CustomErrCode::SEND_PACKET_FAILED;
100         }
101         case CoordinationErrCode::UNEXPECTED_START_CALL: {
102             return CustomErrCode::UNEXPECTED_START_CALL;
103         }
104         case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
105             return CustomErrCode::WORKER_THREAD_TIMEOUT;
106         }
107         default:
108             return msgInfo.errCode;
109     }
110 }
111 
GetErrMsg(const CoordinationMsgInfo & msgInfo,std::string & msg)112 bool JsUtilCooperate::GetErrMsg(const CoordinationMsgInfo &msgInfo, std::string &msg)
113 {
114     auto iter = COOPERATE_MSG_MAP.find(msgInfo.msg);
115     if (iter == COOPERATE_MSG_MAP.end()) {
116         FI_HILOGE("Error code:%{public}d is not founded in COOPERATE_MSG_MAP", static_cast<int32_t> (msgInfo.msg));
117         return false;
118     }
119     msg = iter->second;
120     switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
121         case CoordinationErrCode::COORDINATION_OK: {
122             msg += "Everything is fine";
123             break;
124         }
125         case CoordinationErrCode::OPEN_SESSION_FAILED: {
126             msg += "Open session failed";
127             break;
128         }
129         case CoordinationErrCode::SEND_PACKET_FAILED: {
130             msg += "Send packet failed";
131             break;
132         }
133         case CoordinationErrCode::UNEXPECTED_START_CALL: {
134             msg += "Unexpected start call";
135             break;
136         }
137         case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
138             msg += "Worker thread timeout";
139             break;
140         }
141         default:
142             msg +="Softbus bind failed";
143     }
144     return true;
145 }
146 
GetStateResult(napi_env env,bool result)147 napi_value JsUtilCooperate::GetStateResult(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 JsUtilCooperate::IsSameHandle(napi_env env, napi_value handle, napi_ref ref)
159 {
160     napi_handle_scope scope = nullptr;
161     napi_open_handle_scope(env, &scope);
162     CHKPF(scope);
163     napi_value handlerTemp = nullptr;
164     CHKRF_SCOPE(env, napi_get_reference_value(env, ref, &handlerTemp), GET_REFERENCE_VALUE, scope);
165     bool isSame = false;
166     CHKRF_SCOPE(env, napi_strict_equals(env, handle, handlerTemp, &isSame), STRICT_EQUALS, scope);
167     napi_close_handle_scope(env, scope);
168     return isSame;
169 }
170 } // namespace DeviceStatus
171 } // namespace Msdp
172 } // namespace OHOS
173