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