1f8af9c48Sopenharmony_ci/*
2f8af9c48Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3f8af9c48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f8af9c48Sopenharmony_ci * you may not use this file except in compliance with the License.
5f8af9c48Sopenharmony_ci * You may obtain a copy of the License at
6f8af9c48Sopenharmony_ci *
7f8af9c48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f8af9c48Sopenharmony_ci *
9f8af9c48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f8af9c48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f8af9c48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f8af9c48Sopenharmony_ci * See the License for the specific language governing permissions and
13f8af9c48Sopenharmony_ci * limitations under the License.
14f8af9c48Sopenharmony_ci */
15f8af9c48Sopenharmony_ci
16f8af9c48Sopenharmony_ci#ifndef OHOS_DP_DEVICE_PROFILE_PARCEL_HELPER_H
17f8af9c48Sopenharmony_ci#define OHOS_DP_DEVICE_PROFILE_PARCEL_HELPER_H
18f8af9c48Sopenharmony_ci
19f8af9c48Sopenharmony_ci#include "ipc_types.h"
20f8af9c48Sopenharmony_ci#include "distributed_device_profile_log.h"
21f8af9c48Sopenharmony_ci
22f8af9c48Sopenharmony_cinamespace OHOS {
23f8af9c48Sopenharmony_cinamespace DistributedDeviceProfile {
24f8af9c48Sopenharmony_ci#define WRITE_HELPER(parcel, type, value) \
25f8af9c48Sopenharmony_ci    do { \
26f8af9c48Sopenharmony_ci        bool ret = (parcel).Write##type((value)); \
27f8af9c48Sopenharmony_ci        if (!ret) { \
28f8af9c48Sopenharmony_ci            HILOGE("write value failed!"); \
29f8af9c48Sopenharmony_ci            return ERR_FLATTEN_OBJECT; \
30f8af9c48Sopenharmony_ci        } \
31f8af9c48Sopenharmony_ci    } while (0)
32f8af9c48Sopenharmony_ci
33f8af9c48Sopenharmony_ci#define WRITE_HELPER_NORET(parcel, type, value) \
34f8af9c48Sopenharmony_ci    do { \
35f8af9c48Sopenharmony_ci        bool ret = (parcel).Write##type((value)); \
36f8af9c48Sopenharmony_ci        if (!ret) { \
37f8af9c48Sopenharmony_ci            HILOGE("write value failed!"); \
38f8af9c48Sopenharmony_ci            return; \
39f8af9c48Sopenharmony_ci        } \
40f8af9c48Sopenharmony_ci    } while (0)
41f8af9c48Sopenharmony_ci
42f8af9c48Sopenharmony_ci#define WRITE_HELPER_RET(parcel, type, value, failRet) \
43f8af9c48Sopenharmony_ci    do { \
44f8af9c48Sopenharmony_ci        bool ret = (parcel).Write##type((value)); \
45f8af9c48Sopenharmony_ci        if (!ret) { \
46f8af9c48Sopenharmony_ci            HILOGE("write value failed!"); \
47f8af9c48Sopenharmony_ci            return failRet; \
48f8af9c48Sopenharmony_ci        } \
49f8af9c48Sopenharmony_ci    } while (0)
50f8af9c48Sopenharmony_ci
51f8af9c48Sopenharmony_ci#define READ_HELPER(parcel, type, out) \
52f8af9c48Sopenharmony_ci    do { \
53f8af9c48Sopenharmony_ci        bool ret = (parcel).Read##type((out)); \
54f8af9c48Sopenharmony_ci        if (!ret) { \
55f8af9c48Sopenharmony_ci            HILOGE("read value failed!"); \
56f8af9c48Sopenharmony_ci            return ERR_FLATTEN_OBJECT; \
57f8af9c48Sopenharmony_ci        } \
58f8af9c48Sopenharmony_ci    } while (0)
59f8af9c48Sopenharmony_ci
60f8af9c48Sopenharmony_ci#define READ_HELPER_RET(parcel, type, out, failRet) \
61f8af9c48Sopenharmony_ci    do { \
62f8af9c48Sopenharmony_ci        bool ret = (parcel).Read##type((out)); \
63f8af9c48Sopenharmony_ci        if (!ret) { \
64f8af9c48Sopenharmony_ci            HILOGE("read value failed!"); \
65f8af9c48Sopenharmony_ci            return failRet; \
66f8af9c48Sopenharmony_ci        } \
67f8af9c48Sopenharmony_ci    } while (0)
68f8af9c48Sopenharmony_ci
69f8af9c48Sopenharmony_ci#define SEND_REQUEST(remote, code, data, reply) \
70f8af9c48Sopenharmony_ci    do { \
71f8af9c48Sopenharmony_ci        MessageOption option; \
72f8af9c48Sopenharmony_ci        if ((remote) == nullptr) { \
73f8af9c48Sopenharmony_ci            HILOGE("remote is nullptr"); \
74f8af9c48Sopenharmony_ci            return DP_IPC_REMOTE_OBJECT_NULLPTR; \
75f8af9c48Sopenharmony_ci        } \
76f8af9c48Sopenharmony_ci        int32_t errCode = (remote)->SendRequest((code), (data), (reply), option); \
77f8af9c48Sopenharmony_ci        if (errCode != DP_SUCCESS) { \
78f8af9c48Sopenharmony_ci            HILOGE("transact failed, errCode = %{public}d", errCode); \
79f8af9c48Sopenharmony_ci            return errCode; \
80f8af9c48Sopenharmony_ci        } \
81f8af9c48Sopenharmony_ci        int32_t ret = (reply).ReadInt32(); \
82f8af9c48Sopenharmony_ci        if (ret != DP_SUCCESS) { \
83f8af9c48Sopenharmony_ci            HILOGE("dp method call fail, errCode = %{public}d", ret); \
84f8af9c48Sopenharmony_ci            return ret; \
85f8af9c48Sopenharmony_ci        } \
86f8af9c48Sopenharmony_ci    } while (0)
87f8af9c48Sopenharmony_ci
88f8af9c48Sopenharmony_ci#define GET_REMOTE_OBJECT(remote) \
89f8af9c48Sopenharmony_ci    do { \
90f8af9c48Sopenharmony_ci        (remote) = Remote(); \
91f8af9c48Sopenharmony_ci        if ((remote) == nullptr) { \
92f8af9c48Sopenharmony_ci            HILOGE("RemoteObject is nullptr!"); \
93f8af9c48Sopenharmony_ci            return DP_IPC_REMOTE_OBJECT_NULLPTR; \
94f8af9c48Sopenharmony_ci        } \
95f8af9c48Sopenharmony_ci    } while (0)
96f8af9c48Sopenharmony_ci
97f8af9c48Sopenharmony_ci#define WRITE_INTERFACE_TOKEN(data) \
98f8af9c48Sopenharmony_ci    do { \
99f8af9c48Sopenharmony_ci        if (!(data).WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) { \
100f8af9c48Sopenharmony_ci            HILOGE("Write interface token failed!"); \
101f8af9c48Sopenharmony_ci            return ERR_FLATTEN_OBJECT; \
102f8af9c48Sopenharmony_ci        } \
103f8af9c48Sopenharmony_ci    } while (0)
104f8af9c48Sopenharmony_ci
105f8af9c48Sopenharmony_ci#define WRITE_CHANGE_LISTENER_TOKEN(data) \
106f8af9c48Sopenharmony_ci    do { \
107f8af9c48Sopenharmony_ci        if (!(data).WriteInterfaceToken(IProfileChangeListener::GetDescriptor())) { \
108f8af9c48Sopenharmony_ci            HILOGE("Write interface token failed!"); \
109f8af9c48Sopenharmony_ci            return ERR_FLATTEN_OBJECT; \
110f8af9c48Sopenharmony_ci        } \
111f8af9c48Sopenharmony_ci    } while (0)
112f8af9c48Sopenharmony_ci
113f8af9c48Sopenharmony_ci#define WRITE_SYNC_CALLBACK_TOKEN(data) \
114f8af9c48Sopenharmony_ci    do { \
115f8af9c48Sopenharmony_ci        if (!(data).WriteInterfaceToken(ISyncCompletedCallback::GetDescriptor())) { \
116f8af9c48Sopenharmony_ci            HILOGE("Write interface token failed!"); \
117f8af9c48Sopenharmony_ci            return ERR_FLATTEN_OBJECT; \
118f8af9c48Sopenharmony_ci        } \
119f8af9c48Sopenharmony_ci    } while (0)
120f8af9c48Sopenharmony_ci} // namespace DistributedDeviceProfile
121f8af9c48Sopenharmony_ci} // namespace OHOS
122f8af9c48Sopenharmony_ci#endif // OHOS_DP_DEVICE_PROFILE_PARCEL_HELPER_H
123