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
25namespace OHOS {
26namespace Msdp {
27namespace 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 WRITEFLOAT(parcel, data, ...) \
79    do { \
80        if (!(parcel).WriteFloat(data)) { \
81            FI_HILOGE("WriteFloat "#data" failed"); \
82            return __VA_ARGS__; \
83        } \
84    } while (0)
85
86#define WRITESTRING(parcel, data, ...) \
87    do { \
88        if (!(parcel).WriteString(data)) { \
89            FI_HILOGE("WriteString "#data" failed"); \
90            return __VA_ARGS__; \
91        } \
92    } while (0)
93
94#define WRITESTRING16(parcel, data, ...) \
95    do { \
96        if (!(parcel).WriteString16(data)) { \
97            FI_HILOGE("WriteString16 "#data" failed"); \
98            return __VA_ARGS__; \
99        } \
100    } while (0)
101
102#define WRITEREMOTEOBJECT(parcel, data, ...) \
103    do { \
104        if (!(parcel).WriteRemoteObject(data)) { \
105            FI_HILOGE("WriteRemoteObject "#data" failed"); \
106            return __VA_ARGS__; \
107        } \
108    } while (0)
109
110#define WRITEUINT8VECTOR(parcel, data, ...) \
111    do { \
112        if (!(parcel).WriteUInt8Vector(data)) { \
113            FI_HILOGE("WriteUInt8Vector "#data" failed"); \
114            return __VA_ARGS__; \
115        } \
116    } while (0)
117
118#define WRITEINT32VECTOR(parcel, data, ...) \
119    do { \
120        if (!(parcel).WriteInt32Vector(data)) { \
121            FI_HILOGE("WriteInt32Vector "#data" failed"); \
122            return __VA_ARGS__; \
123        } \
124    } while (0)
125
126#define WRITEFLOATVECTOR(parcel, data, ...) \
127    do { \
128        if (!(parcel).WriteFloatVector(data)) { \
129            FI_HILOGE("WriteFloatVector "#data" failed"); \
130            return __VA_ARGS__; \
131        } \
132    } while (0)
133
134#define READBOOL(parcel, data, ...) \
135    do { \
136        if (!(parcel).ReadBool(data)) { \
137            FI_HILOGE("ReadBool "#data" failed"); \
138            return __VA_ARGS__; \
139        } \
140    } while (0)
141
142#define READINT32(parcel, data, ...) \
143    do { \
144        if (!(parcel).ReadInt32(data)) { \
145            FI_HILOGE("ReadInt32 "#data" failed"); \
146            return __VA_ARGS__; \
147        } \
148    } while (0)
149
150#define READINT64(parcel, data, ...) \
151    do { \
152        if (!(parcel).ReadInt64(data)) { \
153            FI_HILOGE("ReadInt64 "#data" failed"); \
154            return __VA_ARGS__; \
155        } \
156    } while (0)
157
158#define READUINT32(parcel, data, ...) \
159    do { \
160        if (!(parcel).ReadUint32(data)) { \
161            FI_HILOGE("ReadUint32 "#data" failed"); \
162            return __VA_ARGS__; \
163        } \
164    } while (0)
165
166#define READDOUBLE(parcel, data, ...) \
167    do { \
168        if (!(parcel).ReadDouble(data)) { \
169            FI_HILOGE("ReadDouble "#data" failed"); \
170            return __VA_ARGS__; \
171        } \
172    } while (0)
173
174#define READFLOAT(parcel, data, ...) \
175    do { \
176        if (!(parcel).ReadFloat(data)) { \
177            FI_HILOGE("ReadFloat "#data" failed"); \
178            return __VA_ARGS__; \
179        } \
180    } while (0)
181
182#define READSTRING(parcel, data, ...) \
183    do { \
184        if (!(parcel).ReadString(data)) { \
185            FI_HILOGE("ReadString "#data" failed"); \
186            return __VA_ARGS__; \
187        } \
188    } while (0)
189
190#define READUINT8VECTOR(parcel, data, ...) \
191    do { \
192        if (!(parcel).ReadUInt8Vector(&data)) { \
193                FI_HILOGE("ReadUInt8Vector "#data" failed"); \
194                return __VA_ARGS__; \
195            } \
196    } while (0)
197
198#define READSTRING16(parcel, data, ...) \
199    do { \
200        if (!(parcel).ReadString16(data)) { \
201            FI_HILOGE("ReadString16 "#data" failed"); \
202            return __VA_ARGS__; \
203        } \
204    } while (0)
205
206#define READINT32VECTOR(parcel, data, ...) \
207    do { \
208        if (!(parcel).ReadInt32Vector(&data)) { \
209            FI_HILOGE("ReadInt32Vector "#data" failed"); \
210            return __VA_ARGS__; \
211        } \
212    } while (0)
213
214#define READFLOATVECTOR(parcel, data, ...) \
215    do { \
216        if (!(parcel).ReadFloatVector(&data)) { \
217            FI_HILOGE("ReadFloatVector "#data" failed"); \
218            return __VA_ARGS__; \
219        } \
220    } while (0)
221
222template<typename E>
223constexpr auto DeviceStatusToUnderlying(E e) noexcept
224{
225    return static_cast<std::underlying_type_t<E>>(e);
226}
227} // namespace DeviceStatus
228} // namespace Msdp
229} // namespace OHOS
230
231#endif // DEVICESTATUS_COMMON_H
232