• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <sensor_log.h>
21 
22 #include "hilog/log.h"
23 
24 namespace OHOS {
25 namespace Sensors {
26 constexpr uint32_t MAX_SENSOR_COUNT = 200;
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     SERVICE_EXCEPTION = 14500101 // Use this error code when the service is exception.
32 };
33 
34 enum {
35     MODULE_COMMON = 0x00,
36     MODULE_SENSORS_DEVICE = 0x01,
37     MODULE_SENSOR_SERVICE = 0x02,
38     MODULE_SENSORS_UTILS = 0x03,
39     MODULE_SENSORS_NATIVE = 0X04,
40 };
41 
42 // Error code for common
43 constexpr ErrCode COMMON_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_COMMON);
44 
45 enum {
46     ERROR = -1,
47     SUCCESS = 0,
48     COMMON_ERR = COMMON_ERR_OFFSET,
49     FILE_OPEN_FAIL = COMMON_ERR_OFFSET + 1,
50     STREAM_BUF_READ_FAIL = FILE_OPEN_FAIL + 1,
51 };
52 
53 // Error code for device
54 constexpr ErrCode DEVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSORS_DEVICE);
55 
56 enum {
57     DEVICE_ERR = DEVICE_ERR_OFFSET,
58     DEVICE_ENABLE_SENSOR_ERR = DEVICE_ERR_OFFSET + 1,
59     DEVICE_DISABLE_SENSOR_ERR = DEVICE_ENABLE_SENSOR_ERR + 1,
60     DEVICE_RUN_COMMAND_ERR = DEVICE_DISABLE_SENSOR_ERR + 1,
61     DEVICE_SET_SENSOR_CONFIG_ERR = DEVICE_RUN_COMMAND_ERR + 1,
62     DEVICE_NOT_SUPPORT_CMD_ERR = DEVICE_SET_SENSOR_CONFIG_ERR + 1,
63 };
64 
65 // Error code for sensor service
66 constexpr ErrCode SENSOR_SERVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSOR_SERVICE);
67 
68 enum {
69     SENSOR_SERVICE_ERR = SENSOR_SERVICE_ERR_OFFSET,
70     CMD_TYPE_ERR = SENSOR_SERVICE_ERR + 1,
71     SENSOR_DEVICE_INIT_ERR = CMD_TYPE_ERR + 1,
72     CONNECT_SENSOR_HDF_ERR = SENSOR_DEVICE_INIT_ERR + 1,
73     SET_SENSOR_CONFIG_ERR = CONNECT_SENSOR_HDF_ERR + 1,
74     ENABLE_SENSOR_ERR = SET_SENSOR_CONFIG_ERR + 1,
75     DISABLE_SENSOR_ERR = ENABLE_SENSOR_ERR + 1,
76     RUN_COMMAND_ERR = DISABLE_SENSOR_ERR + 1,
77     GET_SENSOR_LIST_ERR = RUN_COMMAND_ERR + 1,
78     SENSOR_ENABLED_ERR = GET_SENSOR_LIST_ERR + 1,
79     UPDATE_SENSOR_CHANNEL_ERR = SENSOR_ENABLED_ERR + 1,
80     CLEAR_SENSOR_CHANNEL_ERR = UPDATE_SENSOR_CHANNEL_ERR + 1,
81     CLEAR_SENSOR_INFO_ERR = CLEAR_SENSOR_CHANNEL_ERR + 1,
82     UPDATE_SENSOR_INFO_ERR = CLEAR_SENSOR_INFO_ERR + 1,
83     CLIENT_PID_INVALID_ERR = UPDATE_SENSOR_INFO_ERR + 1,
84     DESTROY_SENSOR_CHANNEL_ERR = CLIENT_PID_INVALID_ERR + 1,
85     UPDATE_UID_ERR = DESTROY_SENSOR_CHANNEL_ERR + 1,
86     INVALID_POINTER = UPDATE_UID_ERR + 1,
87     NO_EVENT = INVALID_POINTER + 1,
88     COPY_ERR = NO_EVENT + 1,
89     REGIST_PERMISSION_CHANGED_ERR = COPY_ERR + 1,
90     DUMP_PARAM_ERR = REGIST_PERMISSION_CHANGED_ERR + 1,
91     WRITE_PARCEL_ERR = DUMP_PARAM_ERR + 1,
92     READ_PARCEL_ERR = WRITE_PARCEL_ERR + 1,
93     SET_SENSOR_MODE_ERR = READ_PARCEL_ERR + 1,
94     SET_SENSOR_OPTION_ERR = SET_SENSOR_MODE_ERR + 1,
95     REGIST_CALLBACK_ERR = SET_SENSOR_OPTION_ERR + 1,
96     SUSPEND_ERR = REGIST_CALLBACK_ERR + 1,
97     RESUME_ERR = SUSPEND_ERR + 1,
98     RESET_ERR = RESUME_ERR + 1,
99 };
100 
101 // Error code for Sensor utils
102 constexpr ErrCode SENSOR_UTILS_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSORS_UTILS);
103 enum {
104     SENSOR_CHANNEL_SOCKET_CREATE_ERR = SENSOR_UTILS_ERR_OFFSET,
105     SENSOR_CHANNEL_SENDFD_ERR = SENSOR_CHANNEL_SOCKET_CREATE_ERR + 1,
106     SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR = SENSOR_CHANNEL_SENDFD_ERR + 1,
107     SENSOR_CHANNEL_READ_DESCRIPTOR_ERR = SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR + 1,
108     SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT = SENSOR_CHANNEL_READ_DESCRIPTOR_ERR + 1,
109     SENSOR_CHANNEL_SEND_ADDR_ERR = SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT + 1,
110     SENSOR_CHANNEL_SEND_DATA_ERR = SENSOR_CHANNEL_SEND_ADDR_ERR + 1,
111     SENSOR_CHANNEL_RECEIVE_DATA_ERR = SENSOR_CHANNEL_SEND_DATA_ERR + 1,
112     SENSOR_CHANNEL_RECEIVE_ADDR_ERR = SENSOR_CHANNEL_RECEIVE_DATA_ERR + 1,
113     SENSOR_CHANNEL_RESTORE_CB_ERR = SENSOR_CHANNEL_RECEIVE_ADDR_ERR + 1,
114     SENSOR_CHANNEL_RESTORE_FD_ERR = SENSOR_CHANNEL_RESTORE_CB_ERR + 1,
115     SENSOR_CHANNEL_RESTORE_THREAD_ERR = SENSOR_CHANNEL_RESTORE_FD_ERR + 1,
116 };
117 // Error code for Sensor native
118 constexpr ErrCode SENSOR_NATIVE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSORS_NATIVE);
119 
120 enum {
121     SENSOR_NATIVE_SAM_ERR = SENSOR_NATIVE_ERR_OFFSET,
122     SENSOR_NATIVE_GET_SERVICE_ERR = SENSOR_NATIVE_SAM_ERR + 1,
123     SENSOR_NATIVE_REGSITER_CB_ERR = SENSOR_NATIVE_GET_SERVICE_ERR + 1,
124 };
125 
126 class InnerFunctionTracer {
127 public:
InnerFunctionTracer(const OHOS::HiviewDFX::HiLogLabel & label,const char * func)128     InnerFunctionTracer(const OHOS::HiviewDFX::HiLogLabel& label, const char *func)
129         : label_ { label }, func_ { func }
130     {
131         OHOS::HiviewDFX::HiLog::Debug(label_, "in %{public}s, enter", func_);
132     }
~InnerFunctionTracer()133     ~InnerFunctionTracer()
134     {
135         OHOS::HiviewDFX::HiLog::Debug(label_, "in %{public}s, leave", func_);
136     }
137 private:
138     const OHOS::HiviewDFX::HiLogLabel& label_;
139     const char* func_ { nullptr };
140 };
141 
142 #define CALL_LOG_ENTER   InnerFunctionTracer ___innerFuncTracer___ { LABEL, __FUNCTION__ }
143 
144 #ifdef DEBUG_CODE_TEST
145 #define CHKPL(cond, ...) \
146     do { \
147         if ((cond) == nullptr) { \
148             SEN_HILOGW("%{public}s, (%{public}d), CHKPL(%{public}s) is null, do nothing", \
149                 __FILE__, __LINE__, #cond); \
150         } \
151     } while (0)
152 
153 #define CHKPV(cond) \
154     do { \
155         if ((cond) == nullptr) { \
156             SEN_HILOGE("%{public}s, (%{public}d), CHKPV(%{public}s) is null", \
157                 __FILE__, __LINE__, #cond); \
158             return; \
159         } \
160     } while (0)
161 
162 #define CHKPF(cond) \
163     do { \
164         if ((cond) == nullptr) { \
165             SEN_HILOGE("%{public}s, (%{public}d), CHKPF(%{public}s) is null", \
166                 __FILE__, __LINE__, #cond); \
167             return false; \
168         } \
169     } while (0)
170 
171 #define CHKPC(cond) \
172     { \
173         if ((cond) == nullptr) { \
174             SEN_HILOGW("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then continue", \
175                 __FILE__, __LINE__, #cond); \
176             continue; \
177         } \
178     }
179 
180 #define CHKPB(cond) \
181     { \
182         if ((cond) == nullptr) { \
183             SEN_HILOGW("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then break", \
184                 __FILE__, __LINE__, #cond); \
185             break; \
186         } \
187     }
188 
189 #define CHKPR(cond, r) \
190     do { \
191         if ((cond) == nullptr) { \
192             SEN_HILOGE("%{public}s, (%{public}d), CHKPR(%{public}s) is null, return value is %{public}d", \
193                 __FILE__, __LINE__, #cond, r); \
194             return r; \
195         } \
196     } while (0)
197 
198 #define CHKPP(cond) \
199     do { \
200         if ((cond) == nullptr) { \
201             SEN_HILOGE("%{public}s, (%{public}d), CHKPP(%{public}s) is null, return value is null", \
202                 __FILE__, __LINE__, #cond); \
203             return nullptr; \
204         } \
205     } while (0)
206 
207 #define CK(cond, ec) \
208     do { \
209         if (!(cond)) { \
210             SEN_HILOGE("%{public}s, (%{public}d), CK(%{public}s), errCode:%{public}d", \
211                 __FILE__, __LINE__, #cond, ec); \
212         } \
213     } while (0)
214 
215 #else // DEBUG_CODE_TEST
216 #define CHKPL(cond) \
217     do { \
218         if ((cond) == nullptr) { \
219             SEN_HILOGW("CHKPL(%{public}s) is null, do nothing", #cond); \
220         } \
221     } while (0)
222 
223 #define CHKPV(cond) \
224     do { \
225         if ((cond) == nullptr) { \
226             SEN_HILOGE("CHKPV(%{public}s) is null", #cond); \
227             return; \
228         } \
229     } while (0)
230 
231 #define CHKPF(cond) \
232     do { \
233         if ((cond) == nullptr) { \
234             SEN_HILOGE("CHKPF(%{public}s) is null", #cond); \
235             return false; \
236         } \
237     } while (0)
238 
239 #define CHKPC(cond) \
240     { \
241         if ((cond) == nullptr) { \
242             SEN_HILOGW("CHKPC(%{public}s) is null, skip then continue", #cond); \
243             continue; \
244         } \
245     }
246 
247 #define CHKPB(cond) \
248     { \
249         if ((cond) == nullptr) { \
250             SEN_HILOGW("CHKPC(%{public}s) is null, skip then break", #cond); \
251             break; \
252         } \
253     }
254 
255 #define CHKPR(cond, r) \
256     do { \
257         if ((cond) == nullptr) { \
258             SEN_HILOGE("CHKPR(%{public}s) is null, return value is %{public}d", #cond, r); \
259             return r; \
260         } \
261     } while (0)
262 
263 #define CHKCF(cond, message) \
264     do { \
265         if (!(cond)) { \
266             SEN_HILOGE("CK(%{public}s), %{public}s", #cond, message); \
267             return false; \
268         } \
269     } while (0)
270 
271 #define CHKCP(cond, message) \
272     do { \
273         if (!(cond)) { \
274             SEN_HILOGE("(%{public}s)", #message); \
275             return nullptr; \
276         } \
277     } while (0)
278 
279 #define CHKCV(cond, message) \
280     do { \
281         if (!(cond)) { \
282             SEN_HILOGE("CK(%{public}s), %{public}s", #cond, message); \
283             return; \
284         } \
285     } while (0)
286 
287 #define CHKPP(cond) \
288     do { \
289         if ((cond) == nullptr) { \
290             SEN_HILOGE("CHKPP(%{public}s) is null, return value is null", #cond); \
291             return nullptr; \
292         } \
293     } while (0)
294 
295 #define CK(cond, ec) \
296     do { \
297         if (!(cond)) { \
298             SEN_HILOGE("CK(%{public}s), errCode:%{public}d", #cond, ec); \
299         } \
300     } while (0)
301 
302 #define CHKCR(cond, r) \
303     do { \
304         if (!(cond)) { \
305             SEN_HILOGE("CK(%{public}s), errCode:%{public}d", #cond, r); \
306             return r; \
307         } \
308     } while (0)
309 
310 #endif
311 }  // namespace Sensors
312 }  // namespace OHOS
313 #endif  // SENSORS_ERRORS_H
314