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 };
48
49 static std::map<int32_t, std::string> napiErrMsgMap {
50 { WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED, "Operation failed." },
51 { WifiNapiErrCode::WIFI_ERRCODE_WIFI_NOT_OPENED, "WIFI doesn't open." },
52 { WifiNapiErrCode::WIFI_ERRCODE_PERMISSION_DENIED, "Permission denied." },
53 { WifiNapiErrCode::WIFI_ERRCODE_NON_SYSTEMAPP, "non-system application." },
54 { WifiNapiErrCode::WIFI_ERRCODE_INVALID_PARAM, "Parameter error." },
55 { WifiNapiErrCode::WIFI_ERRCODE_NOT_SUPPORTED, "Capability not supported." },
56 { WifiNapiErrCode::WIFI_ERRCODE_OPEN_FAIL_WHEN_CLOSING, "Failed for wifi is closing." },
57 { WifiNapiErrCode::WIFI_ERRCODE_CLOSE_FAIL_WHEN_OPENING, "Failed for wifi is opening." },
58 };
59
NapiGetUndefined(const napi_env & env)60 static napi_value NapiGetUndefined(const napi_env &env)
61 {
62 napi_value undefined = nullptr;
63 napi_get_undefined(env, &undefined);
64 return undefined;
65 }
66
GetNapiErrCode(const napi_env & env,const int32_t errCodeIn,const int32_t sysCap=0)67 static int32_t GetNapiErrCode(const napi_env &env, const int32_t errCodeIn, const int32_t sysCap = 0)
68 {
69 auto iter = errCodeMap.find(errCodeIn);
70 if (iter == errCodeMap.end()) {
71 return WifiNapiErrCode::WIFI_ERRCODE_OPERATION_FAILED + sysCap;
72 }
73 if (iter->second == WifiNapiErrCode::WIFI_ERRCODE_PERMISSION_DENIED ||
74 iter->second == WifiNapiErrCode::WIFI_ERRCODE_INVALID_PARAM ||
75 iter->second == WifiNapiErrCode::WIFI_ERRCODE_NOT_SUPPORTED ||
76 iter->second == WifiNapiErrCode::WIFI_ERRCODE_NON_SYSTEMAPP) {
77 return iter->second;
78 }
79 return iter->second + sysCap;
80 }
81
GetNapiErrMsg(const napi_env & env,const int32_t errCode,int sysCap)82 static std::string GetNapiErrMsg(const napi_env &env, const int32_t errCode, int sysCap)
83 {
84 if (errCode == ErrCode::WIFI_OPT_SUCCESS) {
85 return "";
86 }
87
88 int32_t napiErrCode = GetNapiErrCode(env, errCode);
89 auto iter = napiErrMsgMap.find(napiErrCode);
90 if (iter != napiErrMsgMap.end()) {
91 std::string errMessage = "BussinessError ";
92 napiErrCode = GetNapiErrCode(env, errCode, sysCap);
93 errMessage.append(std::to_string(napiErrCode)).append(": ").append(iter->second);
94 return errMessage;
95 }
96 return "Inner error.";
97 }
98
99 #ifdef ENABLE_NAPI_WIFI_MANAGER
NapiGetNull(const napi_env & env)100 static napi_value NapiGetNull(const napi_env &env)
101 {
102 napi_value res = nullptr;
103 napi_get_null(env, &res);
104 return res;
105 }
106
GetCallbackErrorValue(napi_env env,const int32_t errCode,const std::string errMsg)107 static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg)
108 {
109 napi_value businessError = nullptr;
110 napi_value eCode = nullptr;
111 napi_value eMsg = nullptr;
112 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
113 NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), errMsg.length(), &eMsg));
114 NAPI_CALL(env, napi_create_object(env, &businessError));
115 NAPI_CALL(env, napi_set_named_property(env, businessError, "code", eCode));
116 NAPI_CALL(env, napi_set_named_property(env, businessError, "message", eMsg));
117 return businessError;
118 }
119 #endif
120
HandleCallbackErrCode(const napi_env & env,const AsyncContext & info)121 void HandleCallbackErrCode(const napi_env &env, const AsyncContext &info)
122 {
123 WIFI_LOGI("HandleCallbackErrCode, errCode = %{public}d", (int)info.errorCode);
124 constexpr int RESULT_PARAMS_NUM = 2;
125 napi_value undefine = NapiGetUndefined(env);
126 napi_value callback = nullptr;
127 napi_value result[RESULT_PARAMS_NUM] = {nullptr};
128 result[1] = info.result;
129 if (info.errorCode == ErrCode::WIFI_OPT_SUCCESS) {
130 #ifdef ENABLE_NAPI_WIFI_MANAGER
131 result[0] = NapiGetUndefined(env);
132 #else
133 napi_create_uint32(env, info.errorCode, &result[0]);
134 #endif
135 napi_get_reference_value(env, info.callback[0], &callback);
136 napi_call_function(env, nullptr, callback, RESULT_PARAMS_NUM, result, &undefine);
137 } else {
138 napi_ref errCb = info.callback[1];
139 if (!errCb) {
140 WIFI_LOGE("Get callback func[1] is null");
141 errCb = info.callback[0];
142 }
143 napi_get_reference_value(env, errCb, &callback);
144 #ifdef ENABLE_NAPI_WIFI_MANAGER
145 std::string errMsg = GetNapiErrMsg(env, info.errorCode, info.sysCap);
146 int32_t errCodeInfo = GetNapiErrCode(env, info.errorCode, info.sysCap);
147 result[0] = GetCallbackErrorValue(env, errCodeInfo, errMsg);
148 #else
149 napi_create_uint32(env, info.errorCode, &result[0]);
150 #endif
151 napi_call_function(env, nullptr, callback, RESULT_PARAMS_NUM, result, &undefine);
152 }
153 }
154
HandlePromiseErrCode(const napi_env & env,const AsyncContext & info)155 void HandlePromiseErrCode( const napi_env &env, const AsyncContext &info)
156 {
157 WIFI_LOGD("HandlePromiseErrCode, errCode = %{public}d", (int)info.errorCode);
158 if (info.errorCode == ErrCode::WIFI_OPT_SUCCESS) {
159 napi_resolve_deferred(env, info.deferred, info.result);
160 } else {
161 #ifdef ENABLE_NAPI_WIFI_MANAGER
162 int32_t errCodeInfo = GetNapiErrCode(env, info.errorCode, info.sysCap);
163 std::string errMsg = GetNapiErrMsg(env, info.errorCode, info.sysCap);
164 napi_value businessError = nullptr;
165 napi_value eCode = nullptr;
166 napi_value eMsg = nullptr;
167 napi_value eData = NapiGetNull(env);
168 napi_create_int32(env, errCodeInfo, &eCode);
169 napi_create_string_utf8(env, errMsg.c_str(), errMsg.length(), &eMsg);
170 napi_create_object(env, &businessError);
171 napi_set_named_property(env, businessError, "code", eCode);
172 napi_set_named_property(env, businessError, "message", eMsg);
173 napi_set_named_property(env, businessError, "data", eData);
174 napi_reject_deferred(env, info.deferred, businessError);
175 #else
176 napi_reject_deferred(info.env, info.deferred, info.result);
177 #endif
178 }
179 }
180
HandleSyncErrCode(const napi_env & env,int32_t errCode,int32_t sysCap)181 void HandleSyncErrCode(const napi_env &env, int32_t errCode, int32_t sysCap)
182 {
183 WIFI_LOGI("HandleSyncErrCode, errCode = %{public}d", static_cast<int>(errCode));
184 if (errCode == ErrCode::WIFI_OPT_SUCCESS) {
185 return;
186 }
187 std::string errMsg = GetNapiErrMsg(env, errCode, sysCap);
188 int32_t errCodeInfo = GetNapiErrCode(env, errCode, sysCap);
189 if (errMsg != "") {
190 napi_throw_error(env, std::to_string(errCodeInfo).c_str(), errMsg.c_str());
191 }
192 }
193 } // namespace Wifi
194 } // namespace OHOS
195