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