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