1 /*
2 * Copyright (C) 2022 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 "wifi_napi_errcode.h"
17 #include <map>
18 #include "wifi_logger.h"
19 #include "wifi_errcode.h"
20
21 namespace OHOS {
22 namespace Wifi {
23 DEFINE_WIFILOG_LABEL("WifiNAPIErrCode");
24 static std::map<int32_t, int32_t> errCodeMap = {
25 { ErrCode::WIFI_OPT_SUCCESS, WifiNapiErrCode::WIFI_ERRCODE_SUCCESS },
26 { ErrCode::WIFI_OPT_FAILED, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
27 { ErrCode::WIFI_OPT_NOT_SUPPORTED, WifiNapiErrCode::WIFI_ERRCODE_NOT_SUPPORTED },
28 { ErrCode::WIFI_OPT_INVALID_PARAM, WifiNapiErrCode::WIFI_ERRCODE_INVALID_PARAM },
29 { ErrCode::WIFI_OPT_FORBID_AIRPLANE, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
30 { ErrCode::WIFI_OPT_FORBID_POWSAVING, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
31 { ErrCode::WIFI_OPT_PERMISSION_DENIED, WifiNapiErrCode::WIFI_ERRCODE_PERMISSION_DENIED },
32 { ErrCode::WIFI_OPT_NON_SYSTEMAPP, WifiNapiErrCode::WIFI_ERRCODE_NON_SYSTEMAPP },
33 { ErrCode::WIFI_OPT_OPEN_FAIL_WHEN_CLOSING, WifiNapiErrCode::WIFI_ERRCODE_OPEN_FAIL_WHEN_CLOSING },
34 { ErrCode::WIFI_OPT_OPEN_SUCC_WHEN_OPENED, WifiNapiErrCode::WIFI_ERRCODE_CLOSE_FAIL_WHEN_OPENING },
35 { ErrCode::WIFI_OPT_CLOSE_FAIL_WHEN_OPENING, WifiNapiErrCode::WIFI_ERRCODE_CLOSE_FAIL_WHEN_OPENING },
36 { ErrCode::WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
37 { ErrCode::WIFI_OPT_STA_NOT_OPENED, WifiNapiErrCode::WIFI_ERRCODE_WIFI_NOT_OPENED },
38 { ErrCode::WIFI_OPT_SCAN_NOT_OPENED, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
39 { ErrCode::WIFI_OPT_AP_NOT_OPENED, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
40 { ErrCode::WIFI_OPT_INVALID_CONFIG, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
41 { ErrCode::WIFI_OPT_P2P_NOT_OPENED, WifiNapiErrCode::WIFI_ERRCODE_WIFI_NOT_OPENED },
42 { ErrCode::WIFI_OPT_P2P_MAC_NOT_FOUND, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
43 { ErrCode::WIFI_OPT_P2P_ERR_MAC_FORMAT, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
44 { ErrCode::WIFI_OPT_P2P_ERR_INTENT, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
45 { ErrCode::WIFI_OPT_P2P_ERR_SIZE_NW_NAME, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
46 { ErrCode::WIFI_OPT_MOVING_FREEZE_CTRL, WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED },
47 { ErrCode::WIFI_OPT_USER_DOES_NOT_RESPOND, WifiNapiErrCode::WIFI_ERRCODE_USER_DOES_NOT_RESPOND },
48 { ErrCode::WIFI_OPT_USER_REFUSE_THE_ACTION, WifiNapiErrCode::WIFI_ERRCODE_USER_REFUSE_THE_ACTION },
49 { ErrCode::WIFI_OPT_INVALID_PARAM_NEW, WifiNapiErrCode::WIFI_ERRCODE_PARAM_VALIDATION_FAILED },
50 };
51
52 static std::map<int32_t, std::string> napiErrMsgMap {
53 { WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED, "Operation failed." },
54 { WifiNapiErrCode::WIFI_ERRCODE_WIFI_NOT_OPENED, "Wi-Fi STA disabled." },
55 { WifiNapiErrCode::WIFI_ERRCODE_PERMISSION_DENIED, "Permission denied." },
56 { WifiNapiErrCode::WIFI_ERRCODE_NON_SYSTEMAPP, "non-system application." },
57 { WifiNapiErrCode::WIFI_ERRCODE_INVALID_PARAM, "Parameter error." },
58 { WifiNapiErrCode::WIFI_ERRCODE_NOT_SUPPORTED, "Capability not supported." },
59 { WifiNapiErrCode::WIFI_ERRCODE_OPEN_FAIL_WHEN_CLOSING, "Operation failed because the service is being closed." },
60 { WifiNapiErrCode::WIFI_ERRCODE_CLOSE_FAIL_WHEN_OPENING, "Operation failed because the service is being opened." },
61 { WifiNapiErrCode::WIFI_ERRCODE_USER_DOES_NOT_RESPOND, "The user does not respond." },
62 { WifiNapiErrCode::WIFI_ERRCODE_USER_REFUSE_THE_ACTION, "The user refused the action." },
63 { WifiNapiErrCode::WIFI_ERRCODE_PARAM_VALIDATION_FAILED, "Parameter validation failed." },
64 };
65
NapiGetUndefined(const napi_env & env)66 static napi_value NapiGetUndefined(const napi_env &env)
67 {
68 napi_value undefined = nullptr;
69 napi_get_undefined(env, &undefined);
70 return undefined;
71 }
72
GetNapiErrCode(const napi_env & env,const int32_t errCodeIn,const int32_t sysCap=0)73 static int32_t GetNapiErrCode(const napi_env &env, const int32_t errCodeIn, const int32_t sysCap = 0)
74 {
75 auto iter = errCodeMap.find(errCodeIn);
76 if (iter == errCodeMap.end()) {
77 return WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED + sysCap;
78 }
79 if (iter->second == WifiNapiErrCode::WIFI_ERRCODE_PERMISSION_DENIED ||
80 iter->second == WifiNapiErrCode::WIFI_ERRCODE_INVALID_PARAM ||
81 iter->second == WifiNapiErrCode::WIFI_ERRCODE_NOT_SUPPORTED ||
82 iter->second == WifiNapiErrCode::WIFI_ERRCODE_NON_SYSTEMAPP) {
83 return iter->second;
84 }
85 return iter->second + sysCap;
86 }
87
GetNapiErrMsg(const napi_env & env,const int32_t errCode,int sysCap)88 static std::string GetNapiErrMsg(const napi_env &env, const int32_t errCode, int sysCap)
89 {
90 if (errCode == ErrCode::WIFI_OPT_SUCCESS) {
91 return "";
92 }
93
94 int32_t napiErrCode = GetNapiErrCode(env, errCode);
95 auto iter = napiErrMsgMap.find(napiErrCode);
96 if (iter != napiErrMsgMap.end()) {
97 std::string errMessage = "BussinessError ";
98 napiErrCode = GetNapiErrCode(env, errCode, sysCap);
99 errMessage.append(std::to_string(napiErrCode)).append(": ").append(iter->second);
100 return errMessage;
101 }
102 return "Inner error.";
103 }
104
105 #ifdef ENABLE_NAPI_WIFI_MANAGER
NapiGetNull(const napi_env & env)106 static napi_value NapiGetNull(const napi_env &env)
107 {
108 napi_value res = nullptr;
109 napi_get_null(env, &res);
110 return res;
111 }
112
GetCallbackErrorValue(napi_env env,const int32_t errCode,const std::string errMsg)113 static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg)
114 {
115 napi_value businessError = nullptr;
116 napi_value eCode = nullptr;
117 napi_value eMsg = nullptr;
118 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
119 NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), errMsg.length(), &eMsg));
120 NAPI_CALL(env, napi_create_object(env, &businessError));
121 NAPI_CALL(env, napi_set_named_property(env, businessError, "code", eCode));
122 NAPI_CALL(env, napi_set_named_property(env, businessError, "message", eMsg));
123 return businessError;
124 }
125 #endif
126
HandleCallbackErrCode(const napi_env & env,const AsyncContext & info)127 void HandleCallbackErrCode(const napi_env &env, const AsyncContext &info)
128 {
129 WIFI_LOGI("HandleCallbackErrCode, errCode = %{public}d", (int)info.errorCode);
130 constexpr int RESULT_PARAMS_NUM = 2;
131 napi_value undefine = NapiGetUndefined(env);
132 napi_value callback = nullptr;
133 napi_value result[RESULT_PARAMS_NUM] = {nullptr};
134 result[1] = info.result;
135 if (info.errorCode == ErrCode::WIFI_OPT_SUCCESS) {
136 #ifdef ENABLE_NAPI_WIFI_MANAGER
137 result[0] = NapiGetUndefined(env);
138 #else
139 napi_create_uint32(env, info.errorCode, &result[0]);
140 #endif
141 napi_get_reference_value(env, info.callback[0], &callback);
142 napi_call_function(env, nullptr, callback, RESULT_PARAMS_NUM, result, &undefine);
143 } else {
144 napi_ref errCb = info.callback[1];
145 if (!errCb) {
146 WIFI_LOGE("Get callback func[1] is null");
147 errCb = info.callback[0];
148 }
149 napi_get_reference_value(env, errCb, &callback);
150 #ifdef ENABLE_NAPI_WIFI_MANAGER
151 std::string errMsg = GetNapiErrMsg(env, info.errorCode, info.sysCap);
152 int32_t errCodeInfo = GetNapiErrCode(env, info.errorCode, info.sysCap);
153 result[0] = GetCallbackErrorValue(env, errCodeInfo, errMsg);
154 #else
155 napi_create_uint32(env, info.errorCode, &result[0]);
156 #endif
157 napi_call_function(env, nullptr, callback, RESULT_PARAMS_NUM, result, &undefine);
158 }
159 }
160
HandlePromiseErrCode(const napi_env & env,const AsyncContext & info)161 void HandlePromiseErrCode( const napi_env &env, const AsyncContext &info)
162 {
163 WIFI_LOGD("HandlePromiseErrCode, errCode = %{public}d", (int)info.errorCode);
164 if (info.errorCode == ErrCode::WIFI_OPT_SUCCESS) {
165 napi_resolve_deferred(env, info.deferred, info.result);
166 } else {
167 #ifdef ENABLE_NAPI_WIFI_MANAGER
168 int32_t errCodeInfo = GetNapiErrCode(env, info.errorCode, info.sysCap);
169 std::string errMsg = GetNapiErrMsg(env, info.errorCode, info.sysCap);
170 napi_value businessError = nullptr;
171 napi_value eCode = nullptr;
172 napi_value eMsg = nullptr;
173 napi_value eData = NapiGetNull(env);
174 napi_create_int32(env, errCodeInfo, &eCode);
175 napi_create_string_utf8(env, errMsg.c_str(), errMsg.length(), &eMsg);
176 napi_create_object(env, &businessError);
177 napi_set_named_property(env, businessError, "code", eCode);
178 napi_set_named_property(env, businessError, "message", eMsg);
179 napi_set_named_property(env, businessError, "data", eData);
180 napi_reject_deferred(env, info.deferred, businessError);
181 #else
182 napi_reject_deferred(info.env, info.deferred, info.result);
183 #endif
184 }
185 }
186
HandleSyncErrCode(const napi_env & env,int32_t errCode,int32_t sysCap)187 void HandleSyncErrCode(const napi_env &env, int32_t errCode, int32_t sysCap)
188 {
189 WIFI_LOGI("HandleSyncErrCode, errCode = %{public}d", (int)errCode);
190 if (errCode == ErrCode::WIFI_OPT_SUCCESS) {
191 return;
192 }
193 std::string errMsg = GetNapiErrMsg(env, errCode, sysCap);
194 int32_t errCodeInfo = GetNapiErrCode(env, errCode, sysCap);
195 if (errMsg != "") {
196 napi_throw_error(env, std::to_string(errCodeInfo).c_str(), errMsg.c_str());
197 }
198 }
199 } // namespace Wifi
200 } // namespace OHOS
201