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 OHOS_DEVICE_PROFILE_PARCEL_HELPER_H
17#define OHOS_DEVICE_PROFILE_PARCEL_HELPER_H
18
19#include "ipc_types.h"
20#include "device_profile_log.h"
21
22namespace OHOS {
23namespace DeviceProfile {
24#define PARCEL_WRITE_HELPER(parcel, type, value) \
25    do { \
26        bool ret = parcel.Write##type((value)); \
27        if (!ret) { \
28            HILOGE("write value failed!"); \
29            return ERR_FLATTEN_OBJECT; \
30        } \
31    } while (0)
32
33#define PARCEL_WRITE_HELPER_NORET(parcel, type, value) \
34    do { \
35        bool ret = parcel.Write##type((value)); \
36        if (!ret) { \
37            HILOGE("write value failed!"); \
38            return; \
39        } \
40    } while (0)
41
42#define PARCEL_WRITE_HELPER_RET(parcel, type, value, failRet) \
43    do { \
44        bool ret = parcel.Write##type((value)); \
45        if (!ret) { \
46            HILOGE("write value failed!"); \
47            return failRet; \
48        } \
49    } while (0)
50
51#define PARCEL_READ_HELPER(parcel, type, out) \
52    do { \
53        bool ret = parcel.Read##type((out)); \
54        if (!ret) { \
55            HILOGE("read value failed!"); \
56            return ERR_FLATTEN_OBJECT; \
57        } \
58    } while (0)
59
60#define PARCEL_READ_HELPER_RET(parcel, type, out, failRet) \
61    do { \
62        bool ret = parcel.Read##type((out)); \
63        if (!ret) { \
64            HILOGE("read value failed!"); \
65            return failRet; \
66        } \
67    } while (0)
68
69#define PARCEL_READ_HELPER_NORET(parcel, type, out) \
70    do { \
71        bool ret = parcel.Read##type((out)); \
72        if (!ret) { \
73            HILOGW("read value failed!"); \
74        } \
75    } while (0)
76
77#define PARCEL_WRITE_REPLY_NOERROR(reply, type, result) \
78    do { \
79        bool ret = reply.Write##type(result); \
80        if (!ret) { \
81            HILOGW("write reply failed!"); \
82        } \
83        return ERR_OK; \
84    } while (0)
85
86#define PARCEL_TRANSACT_SYNC_RET_INT(remote, code, data, reply) \
87    do { \
88        MessageOption option; \
89        int32_t errCode = remote->SendRequest(code, data, reply, option); \
90        if (errCode != ERR_OK) { \
91            HILOGE("transact failed, errCode = %{public}d", errCode); \
92            return errCode; \
93        } \
94        HILOGI("transact succeeded"); \
95        return ERR_OK; \
96    } while (0)
97
98#define PARCEL_TRANSACT_SYNC_NORET(remote, code, data, reply) \
99    do { \
100        MessageOption option; \
101        int32_t errCode = remote->SendRequest(code, data, reply, option); \
102        if (errCode != ERR_OK) { \
103            HILOGE("transact failed, errCode = %{public}d", errCode); \
104            return; \
105        } \
106        HILOGI("transact succeeded"); \
107    } while (0)
108} // namespace DeviceProfile
109} // namespace OHOS
110#endif // OHOS_DEVICE_PROFILE_PARCEL_HELPER_H