1/*
2 * Copyright (c) 2023-2024 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_DP_DEVICE_PROFILE_PARCEL_HELPER_H
17#define OHOS_DP_DEVICE_PROFILE_PARCEL_HELPER_H
18
19#include "ipc_types.h"
20#include "distributed_device_profile_log.h"
21
22namespace OHOS {
23namespace DistributedDeviceProfile {
24#define 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 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 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 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 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 SEND_REQUEST(remote, code, data, reply) \
70    do { \
71        MessageOption option; \
72        if ((remote) == nullptr) { \
73            HILOGE("remote is nullptr"); \
74            return DP_IPC_REMOTE_OBJECT_NULLPTR; \
75        } \
76        int32_t errCode = (remote)->SendRequest((code), (data), (reply), option); \
77        if (errCode != DP_SUCCESS) { \
78            HILOGE("transact failed, errCode = %{public}d", errCode); \
79            return errCode; \
80        } \
81        int32_t ret = (reply).ReadInt32(); \
82        if (ret != DP_SUCCESS) { \
83            HILOGE("dp method call fail, errCode = %{public}d", ret); \
84            return ret; \
85        } \
86    } while (0)
87
88#define GET_REMOTE_OBJECT(remote) \
89    do { \
90        (remote) = Remote(); \
91        if ((remote) == nullptr) { \
92            HILOGE("RemoteObject is nullptr!"); \
93            return DP_IPC_REMOTE_OBJECT_NULLPTR; \
94        } \
95    } while (0)
96
97#define WRITE_INTERFACE_TOKEN(data) \
98    do { \
99        if (!(data).WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) { \
100            HILOGE("Write interface token failed!"); \
101            return ERR_FLATTEN_OBJECT; \
102        } \
103    } while (0)
104
105#define WRITE_CHANGE_LISTENER_TOKEN(data) \
106    do { \
107        if (!(data).WriteInterfaceToken(IProfileChangeListener::GetDescriptor())) { \
108            HILOGE("Write interface token failed!"); \
109            return ERR_FLATTEN_OBJECT; \
110        } \
111    } while (0)
112
113#define WRITE_SYNC_CALLBACK_TOKEN(data) \
114    do { \
115        if (!(data).WriteInterfaceToken(ISyncCompletedCallback::GetDescriptor())) { \
116            HILOGE("Write interface token failed!"); \
117            return ERR_FLATTEN_OBJECT; \
118        } \
119    } while (0)
120} // namespace DistributedDeviceProfile
121} // namespace OHOS
122#endif // OHOS_DP_DEVICE_PROFILE_PARCEL_HELPER_H
123