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 #ifndef WIFI_NAPI_ERRCODE_H_
17 #define WIFI_NAPI_ERRCODE_H_
18 
19 #include "wifi_napi_utils.h"
20 #include <string>
21 #include "wifi_napi_errcode.h"
22 
23 namespace OHOS {
24 namespace Wifi {
25 static const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
26 static const std::string BUSINESS_ERROR_PROPERTY_MESSAGE = "message";
27 static const std::string BUSINESS_ERROR_PROPERTY_DATA = "data";
28 
29 enum WifiNapiErrCode {
30     WIFI_ERRCODE_SUCCESS = 0, /* successfully */
31     WIFI_ERRCODE_PERMISSION_DENIED = 201, /* permission denied */
32     WIFI_ERRCODE_NON_SYSTEMAPP = 202, /* not system app */
33     WIFI_ERRCODE_INVALID_PARAM = 401, /* invalid params */
34     WIFI_ERRCODE_NOT_SUPPORTED = 801, /* not supported */
35     WIFI_ERRCODE_OPERATION_FAILED = 1000, /* failed */
36     WIFI_ERRCODE_WIFI_NOT_OPENED  = 1001, /* sta service not opened */
37     WIFI_ERRCODE_OPEN_FAIL_WHEN_CLOSING = 1003, /* forbid when current airplane opened */
38     WIFI_ERRCODE_CLOSE_FAIL_WHEN_OPENING = 1004, /* forbid when current powersaving opened */
39 };
40 
41 #ifdef ENABLE_NAPI_WIFI_MANAGER
42 #ifndef WIFI_NAPI_ASSERT
43 #define WIFI_NAPI_ASSERT(env, cond, errCode, sysCap) \
44 do { \
45     if (!(cond)) { \
46         napi_value res = nullptr; \
47         HandleSyncErrCode(env, errCode, sysCap); \
48         napi_get_undefined(env, &res); \
49         return res; \
50     } \
51 } while (0)
52 #endif
53 
54 #ifndef WIFI_NAPI_RETURN
55 #define WIFI_NAPI_RETURN(env, cond, errCode, sysCap) \
56 do { \
57     napi_value res = nullptr; \
58     if (!(cond)) { \
59         HandleSyncErrCode(env, errCode, sysCap); \
60     } \
61     napi_get_undefined(env, &res); \
62     return res; \
63 } while (0)
64 #endif
65 
66 #else /* #else ENABLE_NAPI_WIFI_MANAGER */
67 
68 #ifndef WIFI_NAPI_ASSERT
69 #define WIFI_NAPI_ASSERT(env, cond, errCode, sysCap) \
70 do { \
71     if (!(cond)) { \
72         napi_value res = nullptr; \
73         napi_get_boolean(env, cond, &res); \
74         return res; \
75     } \
76 } while (0)
77 #endif
78 
79 #ifndef WIFI_NAPI_RETURN
80 #define WIFI_NAPI_RETURN(env, cond, errCode, sysCap) \
81 do { \
82     napi_value res = nullptr; \
83     napi_get_boolean(env, cond, &res); \
84     return res; \
85 } while (0)
86 #endif
87 #endif /* #endif ENABLE_NAPI_WIFI_MANAGER */
88 
89 /**
90  * @brief Thow error code for async-callback function.
91  *
92  * @param env The env.
93  * @param info The input data.
94  */
95 void HandleCallbackErrCode(const napi_env &env, const AsyncContext &info);
96 
97 /**
98  * @brief Thow error code for async-promise function.
99  *
100  * @param env The env.
101  * @param info The input data.
102  */
103 void HandlePromiseErrCode(const napi_env &env, const AsyncContext &info);
104 
105 
106 #ifdef ENABLE_NAPI_WIFI_MANAGER
107 /**
108  * @brief Thow error code for async function.
109  *
110  * @param env The env.
111  * @param errCode The error code.
112  * @param sysCap System capability code.
113  */
114 void HandleSyncErrCode(const napi_env &env, int32_t errCode, int32_t sysCap);
115 #endif
116 }  // namespace Wifi
117 }  // namespace OHOS
118 #endif
119 
120