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