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