• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef SENSORS_ERRORS_H
17 #define SENSORS_ERRORS_H
18 
19 #include <errors.h>
20 
21 #include "hilog/log.h"
22 
23 #include "miscdevice_log.h"
24 
25 namespace OHOS {
26 namespace Sensors {
27 // Error code for user
28 enum ErrorCode : int32_t {
29     PERMISSION_DENIED = 201, // Use this error code when permission is denied.
30     PARAMETER_ERROR = 401, // Use this error code when the input parameter type or range does not match.
31     IS_NOT_SUPPORTED = 801, // Use this error code when capability not supported.
32     DEVICE_OPERATION_FAILED = 14600101, // Use this error code when operating the device fail.
33 };
34 
35 enum {
36     MODULE_COMMON = 0x00,
37     MODULE_MISCDEVICE_DEVICE = 0x01,
38     MODULE_MISCDEVICE_UTILS = 0x02,
39     MODULE_MISCDEVICE_SERVICE = 0x03,
40     MODULE_MISCDEVICE_NATIVE = 0X04,
41 };
42 
43 // Error code for common
44 constexpr ErrCode COMMON_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_COMMON);
45 
46 enum {
47     ERROR = -1,
48     SUCCESS = 0,
49     COMMON_ERR = COMMON_ERR_OFFSET,
50 };
51 
52 // Error code for adapter
53 constexpr ErrCode DEVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_MISCDEVICE_DEVICE);
54 
55 enum {
56     DEVICE_ERR = DEVICE_ERR_OFFSET,
57     DEVICE_ENABLE_SENSOR_ERR = DEVICE_ERR_OFFSET + 1,
58     DEVICE_DISABLE_SENSOR_ERR = DEVICE_ENABLE_SENSOR_ERR + 1,
59     DEVICE_RUN_COMMAND_ERR = DEVICE_DISABLE_SENSOR_ERR + 1,
60     DEVICE_SET_SENSOR_CONFIG_ERR = DEVICE_RUN_COMMAND_ERR + 1,
61     DEVICE_NOT_SUPPORT_CMD_ERR = DEVICE_SET_SENSOR_CONFIG_ERR + 1,
62 };
63 
64 // Error code for miscdevice service
65 constexpr ErrCode MISCDEVICE_SERVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_MISCDEVICE_SERVICE);
66 
67 enum {
68     LIGHT_HDF_CONNECT_ERR = MISCDEVICE_SERVICE_ERR_OFFSET,
69     LIGHT_ID_NOT_SUPPORT = LIGHT_HDF_CONNECT_ERR + 1,
70     LIGHT_ERR = LIGHT_ID_NOT_SUPPORT + 1,
71     LIGHT_PLAY_EFFECT_ERROR = LIGHT_ERR + 1,
72     LIGHT_STOP_EFFECT_ERROR = LIGHT_PLAY_EFFECT_ERROR + 1,
73     LIGHT_END_ERROR = LIGHT_STOP_EFFECT_ERROR,
74     VIBRATOR_HDF_CONNECT_ERR = LIGHT_END_ERROR + 1,
75     VIBRATOR_ON_ERR = VIBRATOR_HDF_CONNECT_ERR + 1,
76     VIBRATOR_OFF_ERR = VIBRATOR_ON_ERR + 1,
77     VIBRATOR_PLAY_EFFECT_ERR = VIBRATOR_OFF_ERR + 1,
78     VIBRATOR_STOP_EFFECT_ERR = VIBRATOR_PLAY_EFFECT_ERR + 1,
79     VIBRATOR_SET_PARA_ERR = VIBRATOR_STOP_EFFECT_ERR + 1,
80     DUMP_PARAM_ERR = VIBRATOR_SET_PARA_ERR + 1,
81     WRITE_MSG_ERR = DUMP_PARAM_ERR + 1,
82     READ_MSG_ERR = WRITE_MSG_ERR + 1,
83 };
84 
85 // Error code for Sensor native
86 constexpr ErrCode MISCDEVICE_NATIVE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_MISCDEVICE_NATIVE);
87 
88 enum {
89     MISCDEVICE_NATIVE_SAM_ERR = MISCDEVICE_NATIVE_ERR_OFFSET,
90     MISC_NATIVE_GET_SERVICE_ERR = MISCDEVICE_NATIVE_SAM_ERR + 1,
91     MISC_NATIVE_SAM_ERR = MISC_NATIVE_GET_SERVICE_ERR + 1,
92 };
93 
94 class InnerFunctionTracer {
95 public:
InnerFunctionTracer(const OHOS::HiviewDFX::HiLogLabel & label,const char * func)96     InnerFunctionTracer(const OHOS::HiviewDFX::HiLogLabel& label, const char *func)
97         : label_ { label }, func_ { func }
98     {
99         OHOS::HiviewDFX::HiLog::Debug(label_, "in %{public}s, enter", func_);
100     }
~InnerFunctionTracer()101     ~InnerFunctionTracer()
102     {
103         OHOS::HiviewDFX::HiLog::Debug(label_, "in %{public}s, leave", func_);
104     }
105 private:
106     const OHOS::HiviewDFX::HiLogLabel& label_;
107     const char* func_ { nullptr };
108 };
109 
110 #define CALL_LOG_ENTER   InnerFunctionTracer ___innerFuncTracer___ { LABEL, __FUNCTION__ }
111 
112 #ifdef DEBUG_CODE_TEST
113 #define CHKPL(cond, ...) \
114     do { \
115         if ((cond) == nullptr) { \
116             MISC_HILOGW("%{public}s, (%{public}d), CHKPL(%{public}s) is null, do nothing", \
117                 __FILE__, __LINE__, #cond); \
118         } \
119     } while (0)
120 
121 #define CHKPV(cond) \
122     do { \
123         if ((cond) == nullptr) { \
124             MISC_HILOGE("%{public}s, (%{public}d), CHKPV(%{public}s) is null", \
125                 __FILE__, __LINE__, #cond); \
126             return; \
127         } \
128     } while (0)
129 
130 #define CHKPF(cond) \
131     do { \
132         if ((cond) == nullptr) { \
133             MISC_HILOGE("%{public}s, (%{public}d), CHKPF(%{public}s) is null", \
134                 __FILE__, __LINE__, #cond); \
135             return false; \
136         } \
137     } while (0)
138 
139 #define CHKPC(cond) \
140     { \
141         if ((cond) == nullptr) { \
142             MISC_HILOGW("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then continue", \
143                 __FILE__, __LINE__, #cond); \
144             continue; \
145         } \
146     }
147 
148 #define CHKPB(cond) \
149     { \
150         if ((cond) == nullptr) { \
151             MISC_HILOGW("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then break", \
152                 __FILE__, __LINE__, #cond); \
153             break; \
154         } \
155     }
156 
157 #define CHKPR(cond, r) \
158     do { \
159         if ((cond) == nullptr) { \
160             MISC_HILOGE("%{public}s, (%{public}d), CHKPR(%{public}s) is null, return value is %{public}d", \
161                 __FILE__, __LINE__, #cond, r); \
162             return r; \
163         } \
164     } while (0)
165 
166 #define CHKPP(cond) \
167     do { \
168         if ((cond) == nullptr) { \
169             MISC_HILOGE("%{public}s, (%{public}d), CHKPP(%{public}s) is null, return value is null", \
170                 __FILE__, __LINE__, #cond); \
171             return nullptr; \
172         } \
173     } while (0)
174 
175 #define CK(cond, ec) \
176     do { \
177         if (!(cond)) { \
178             MISC_HILOGE("%{public}s, (%{public}d), CK(%{public}s), errCode:%{public}d", \
179                 __FILE__, __LINE__, #cond, ec); \
180         } \
181     } while (0)
182 
183 #else // DEBUG_CODE_TEST
184 #define CHKPL(cond) \
185     do { \
186         if ((cond) == nullptr) { \
187             MISC_HILOGW("CHKPL(%{public}s) is null, do nothing", #cond); \
188         } \
189     } while (0)
190 
191 #define CHKPV(cond) \
192     do { \
193         if ((cond) == nullptr) { \
194             MISC_HILOGE("CHKPV(%{public}s) is null", #cond); \
195             return; \
196         } \
197     } while (0)
198 
199 #define CHKPF(cond) \
200     do { \
201         if ((cond) == nullptr) { \
202             MISC_HILOGE("CHKPF(%{public}s) is null", #cond); \
203             return false; \
204         } \
205     } while (0)
206 
207 #define CHKPC(cond) \
208     { \
209         if ((cond) == nullptr) { \
210             MISC_HILOGW("CHKPC(%{public}s) is null, skip then continue", #cond); \
211             continue; \
212         } \
213     }
214 
215 #define CHKPB(cond) \
216     { \
217         if ((cond) == nullptr) { \
218             MISC_HILOGW("CHKPC(%{public}s) is null, skip then break", #cond); \
219             break; \
220         } \
221     }
222 
223 #define CHKPR(cond, r) \
224     do { \
225         if ((cond) == nullptr) { \
226             MISC_HILOGE("CHKPR(%{public}s) is null, return value is %{public}d", #cond, r); \
227             return r; \
228         } \
229     } while (0)
230 
231 #define CHKPS(cond) \
232     do { \
233         if ((cond) == nullptr) { \
234             MISC_HILOGW("CHKPS(%{public}s) is null, skip then break", #cond); \
235             return ""; \
236         } \
237     } while (0)
238 
239 #define CHKPP(cond) \
240     do { \
241         if ((cond) == nullptr) { \
242             MISC_HILOGE("CHKPP(%{public}s) is null, return value is null", #cond); \
243             return nullptr; \
244         } \
245     } while (0)
246 
247 #define CK(cond, ec) \
248     do { \
249         if (!(cond)) { \
250             MISC_HILOGE("CK(%{public}s), errCode:%{public}d", #cond, ec); \
251         } \
252     } while (0)
253 
254 #define CHKCR(cond, r, errDesc) \
255     do { \
256         if (!(cond)) { \
257             MISC_HILOGE("%{public}s, errCode:%{public}d", #errDesc, r); \
258             return r; \
259         } \
260     } while (0)
261 
262 #define CHKCP(cond, errDesc) \
263     do { \
264         if (!(cond)) { \
265             MISC_HILOGE("%{public}s", #errDesc); \
266             return nullptr; \
267         } \
268     } while (0)
269 
270 #define CHKCF(cond, errDesc) \
271     do { \
272         if (!(cond)) { \
273             MISC_HILOGE("%{public}s", #errDesc); \
274             return false; \
275         } \
276     } while (0)
277 
278 #define CHKCV(cond, errDesc) \
279     do { \
280         if (!(cond)) { \
281             MISC_HILOGE("%{public}s", #errDesc); \
282             return; \
283         } \
284     } while (0)
285 #endif
286 }  // namespace Sensors
287 }  // namespace OHOS
288 #endif  // SENSORS_ERRORS_H
289