1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#ifndef CONTINUATION_MANAGER_PARCEL_HELPER_H
17686862fbSopenharmony_ci#define CONTINUATION_MANAGER_PARCEL_HELPER_H
18686862fbSopenharmony_ci
19686862fbSopenharmony_ci#include <cinttypes>
20686862fbSopenharmony_ci
21686862fbSopenharmony_ci#include "base/continuationmgr_log.h"
22686862fbSopenharmony_ci
23686862fbSopenharmony_cinamespace OHOS {
24686862fbSopenharmony_cinamespace DistributedSchedule {
25686862fbSopenharmony_ci#define PARCEL_WRITE_HELPER(parcel, type, value) \
26686862fbSopenharmony_ci    do { \
27686862fbSopenharmony_ci        bool ret = parcel.Write##type((value)); \
28686862fbSopenharmony_ci        if (!ret) { \
29686862fbSopenharmony_ci            HILOGE("%{public}s write value failed!", __func__); \
30686862fbSopenharmony_ci            return ERR_FLATTEN_OBJECT; \
31686862fbSopenharmony_ci        } \
32686862fbSopenharmony_ci    } while (0)
33686862fbSopenharmony_ci
34686862fbSopenharmony_ci#define PARCEL_WRITE_HELPER_NORET(parcel, type, value) \
35686862fbSopenharmony_ci    do { \
36686862fbSopenharmony_ci        bool ret = parcel.Write##type((value)); \
37686862fbSopenharmony_ci        if (!ret) { \
38686862fbSopenharmony_ci            HILOGE("%{public}s write value failed!", __func__); \
39686862fbSopenharmony_ci            return; \
40686862fbSopenharmony_ci        } \
41686862fbSopenharmony_ci    } while (0)
42686862fbSopenharmony_ci
43686862fbSopenharmony_ci#define PARCEL_WRITE_HELPER_RET(parcel, type, value, failRet) \
44686862fbSopenharmony_ci    do { \
45686862fbSopenharmony_ci        bool ret = parcel.Write##type((value)); \
46686862fbSopenharmony_ci        if (!ret) { \
47686862fbSopenharmony_ci            HILOGE("%{public}s write value failed!", __func__); \
48686862fbSopenharmony_ci            return failRet; \
49686862fbSopenharmony_ci        } \
50686862fbSopenharmony_ci    } while (0)
51686862fbSopenharmony_ci
52686862fbSopenharmony_ci#define PARCEL_READ_HELPER(parcel, type, out) \
53686862fbSopenharmony_ci    do { \
54686862fbSopenharmony_ci        bool ret = parcel.Read##type((out)); \
55686862fbSopenharmony_ci        if (!ret) { \
56686862fbSopenharmony_ci            HILOGE("%{public}s read value failed!", __func__); \
57686862fbSopenharmony_ci            return ERR_FLATTEN_OBJECT; \
58686862fbSopenharmony_ci        } \
59686862fbSopenharmony_ci    } while (0)
60686862fbSopenharmony_ci
61686862fbSopenharmony_ci#define PARCEL_READ_HELPER_RET(parcel, type, out, failRet) \
62686862fbSopenharmony_ci    do { \
63686862fbSopenharmony_ci        bool ret = parcel.Read##type((out)); \
64686862fbSopenharmony_ci        if (!ret) { \
65686862fbSopenharmony_ci            HILOGE("%{public}s read value failed!", __func__); \
66686862fbSopenharmony_ci            return failRet; \
67686862fbSopenharmony_ci        } \
68686862fbSopenharmony_ci    } while (0)
69686862fbSopenharmony_ci
70686862fbSopenharmony_ci#define PARCEL_READ_HELPER_NORET(parcel, type, out) \
71686862fbSopenharmony_ci    do { \
72686862fbSopenharmony_ci        bool ret = parcel.Read##type((out)); \
73686862fbSopenharmony_ci        if (!ret) { \
74686862fbSopenharmony_ci            HILOGW("%{public}s read value failed!", __func__); \
75686862fbSopenharmony_ci        } \
76686862fbSopenharmony_ci    } while (0)
77686862fbSopenharmony_ci
78686862fbSopenharmony_ci#define PARCEL_TRANSACT_SYNC_RET_INT(remote, code, data, reply) \
79686862fbSopenharmony_ci    do { \
80686862fbSopenharmony_ci        MessageOption option; \
81686862fbSopenharmony_ci        int32_t error = remote->SendRequest(code, data, reply, option); \
82686862fbSopenharmony_ci        if (error != ERR_NONE) { \
83686862fbSopenharmony_ci            HILOGE("%{public}s transact failed, error: %{public}d", __func__, error); \
84686862fbSopenharmony_ci            return error; \
85686862fbSopenharmony_ci        } \
86686862fbSopenharmony_ci        int32_t result = reply.ReadInt32(); \
87686862fbSopenharmony_ci        HILOGD("%{public}s get result from server data = %{public}d", __func__, result); \
88686862fbSopenharmony_ci        return result; \
89686862fbSopenharmony_ci    } while (0)
90686862fbSopenharmony_ci
91686862fbSopenharmony_ci#define PARCEL_TRANSACT_SYNC_NORET(remote, code, data, reply) \
92686862fbSopenharmony_ci    do { \
93686862fbSopenharmony_ci        MessageOption option; \
94686862fbSopenharmony_ci        int32_t error = remote->SendRequest(code, data, reply, option); \
95686862fbSopenharmony_ci        if (error != ERR_NONE) { \
96686862fbSopenharmony_ci            HILOGE("%{public}s transact failed, error: %{public}d", __func__, error); \
97686862fbSopenharmony_ci            return; \
98686862fbSopenharmony_ci        } \
99686862fbSopenharmony_ci        HILOGD("%{public}s transact success!", __func__); \
100686862fbSopenharmony_ci    } while (0)
101686862fbSopenharmony_ci
102686862fbSopenharmony_ci#define PARCEL_WRITE_REPLY_NOERROR(reply, type, result) \
103686862fbSopenharmony_ci    do { \
104686862fbSopenharmony_ci        bool ret = reply.Write##type(result); \
105686862fbSopenharmony_ci        if (!ret) { \
106686862fbSopenharmony_ci            HILOGW("%{public}s write reply failed.", __func__); \
107686862fbSopenharmony_ci        } \
108686862fbSopenharmony_ci        return ERR_NONE; \
109686862fbSopenharmony_ci    } while (0)
110686862fbSopenharmony_ci} // namespace DistributedSchedule
111686862fbSopenharmony_ci} // namespace OHOS
112686862fbSopenharmony_ci
113686862fbSopenharmony_ci#endif /* CONTINUATION_MANAGER_PARCEL_HELPER_H */
114