1 /*
2 * Copyright (c) 2022-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 DEVICESTATUS_COMMON_H
17 #define DEVICESTATUS_COMMON_H
18
19 #include <cstdint>
20 #include <type_traits>
21
22 #include "devicestatus_errors.h"
23 #include "fi_log.h"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 #define DEV_RET_IF_NULL_WITH_RET(cond, retval) if (cond) {return (retval);}
29 #define DEV_RET_IF_NULL(cond) if (cond) {return;}
30 #define DEV_RET_IF_NULL_WITH_LOG(cond, loginfo) \
31 do { \
32 if (cond) { \
33 FI_HILOGE("%{public}s "#loginfo" ", __func__); \
34 return; \
35 } \
36 } while (0) \
37
38 #define WRITEBOOL(parcel, data, ...) \
39 do { \
40 if (!(parcel).WriteBool(data)) { \
41 FI_HILOGE("WriteBool "#data" failed"); \
42 return __VA_ARGS__; \
43 } \
44 } while (0)
45
46 #define WRITEINT32(parcel, data, ...) \
47 do { \
48 if (!(parcel).WriteInt32(data)) { \
49 FI_HILOGE("WriteInt32 "#data" failed"); \
50 return __VA_ARGS__; \
51 } \
52 } while (0)
53
54 #define WRITEINT64(parcel, data, ...) \
55 do { \
56 if (!(parcel).WriteInt64(data)) { \
57 FI_HILOGE("WriteInt64 "#data" failed"); \
58 return __VA_ARGS__; \
59 } \
60 } while (0)
61
62 #define WRITEUINT32(parcel, data, ...) \
63 do { \
64 if (!(parcel).WriteUint32(data)) { \
65 FI_HILOGE("WriteUint32 "#data" failed"); \
66 return __VA_ARGS__; \
67 } \
68 } while (0)
69
70 #define WRITEDOUBLE(parcel, data, ...) \
71 do { \
72 if (!(parcel).WriteDouble(data)) { \
73 FI_HILOGE("WriteDouble "#data" failed"); \
74 return __VA_ARGS__; \
75 } \
76 } while (0)
77
78 #define WRITESTRING(parcel, data, ...) \
79 do { \
80 if (!(parcel).WriteString(data)) { \
81 FI_HILOGE("WriteString "#data" failed"); \
82 return __VA_ARGS__; \
83 } \
84 } while (0)
85
86 #define WRITESTRING16(parcel, data, ...) \
87 do { \
88 if (!(parcel).WriteString16(data)) { \
89 FI_HILOGE("WriteString16 "#data" failed"); \
90 return __VA_ARGS__; \
91 } \
92 } while (0)
93
94 #define WRITEREMOTEOBJECT(parcel, data, ...) \
95 do { \
96 if (!(parcel).WriteRemoteObject(data)) { \
97 FI_HILOGE("WriteRemoteObject "#data" failed"); \
98 return __VA_ARGS__; \
99 } \
100 } while (0)
101
102 #define WRITEUINT8VECTOR(parcel, data, ...) \
103 do { \
104 if (!(parcel).WriteUInt8Vector(data)) { \
105 FI_HILOGE("WriteUInt8Vector "#data" failed"); \
106 return __VA_ARGS__; \
107 } \
108 } while (0)
109
110 #define READBOOL(parcel, data, ...) \
111 do { \
112 if (!(parcel).ReadBool(data)) { \
113 FI_HILOGE("ReadBool "#data" failed"); \
114 return __VA_ARGS__; \
115 } \
116 } while (0)
117
118 #define READINT32(parcel, data, ...) \
119 do { \
120 if (!(parcel).ReadInt32(data)) { \
121 FI_HILOGE("ReadInt32 "#data" failed"); \
122 return __VA_ARGS__; \
123 } \
124 } while (0)
125
126 #define READINT64(parcel, data, ...) \
127 do { \
128 if (!(parcel).ReadInt64(data)) { \
129 FI_HILOGE("ReadInt64 "#data" failed"); \
130 return __VA_ARGS__; \
131 } \
132 } while (0)
133
134 #define READUINT32(parcel, data, ...) \
135 do { \
136 if (!(parcel).ReadUint32(data)) { \
137 FI_HILOGE("ReadUint32 "#data" failed"); \
138 return __VA_ARGS__; \
139 } \
140 } while (0)
141
142 #define READDOUBLE(parcel, data, ...) \
143 do { \
144 if (!(parcel).ReadDouble(data)) { \
145 FI_HILOGE("ReadDouble "#data" failed"); \
146 return __VA_ARGS__; \
147 } \
148 } while (0)
149
150 #define READSTRING(parcel, data, ...) \
151 do { \
152 if (!(parcel).ReadString(data)) { \
153 FI_HILOGE("ReadString "#data" failed"); \
154 return __VA_ARGS__; \
155 } \
156 } while (0)
157
158 #define READUINT8VECTOR(parcel, data, ...) \
159 do { \
160 if (!(parcel).ReadUInt8Vector(&data)) { \
161 FI_HILOGE("ReadUInt8Vector "#data" failed"); \
162 return __VA_ARGS__; \
163 } \
164 } while (0)
165
166 #define READSTRING16(parcel, data, ...) \
167 do { \
168 if (!(parcel).ReadString16(data)) { \
169 FI_HILOGE("ReadString16 "#data" failed"); \
170 return __VA_ARGS__; \
171 } \
172 } while (0)
173
174 template<typename E>
DeviceStatusToUnderlying(E e)175 constexpr auto DeviceStatusToUnderlying(E e) noexcept
176 {
177 return static_cast<std::underlying_type_t<E>>(e);
178 }
179 } // namespace DeviceStatus
180 } // namespace Msdp
181 } // namespace OHOS
182
183 #endif // DEVICESTATUS_COMMON_H
184