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