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