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 #define LOG_TAG "UdmfServiceProxy"
16 #include "udmf_service_proxy.h"
17 
18 #include "logger.h"
19 #include "udmf_types_util.h"
20 #include "udmf_conversion.h"
21 
22 namespace OHOS {
23 namespace UDMF {
24 #define IPC_SEND(code, reply, ...)                                                           \
25     ({                                                                                       \
26         int32_t ipcStatus = E_OK;                                                            \
27         do {                                                                                 \
28             MessageParcel request;                                                           \
29             if (!request.WriteInterfaceToken(GetDescriptor())) {                             \
30                 ipcStatus = E_WRITE_PARCEL_ERROR;                                            \
31                 break;                                                                       \
32             }                                                                                \
33             if (!ITypesUtil::Marshal(request, ##__VA_ARGS__)) {                              \
34                 ipcStatus = E_WRITE_PARCEL_ERROR;                                            \
35                 break;                                                                       \
36             }                                                                                \
37             MessageOption option;                                                            \
38             auto result = SendRequest(code, request, reply, option);                         \
39             if (result != 0) {                                                               \
40                 LOG_ERROR(UDMF_SERVICE, "SendRequest failed, result = %{public}d!", result); \
41                 ipcStatus = E_IPC;                                                           \
42                 break;                                                                       \
43             }                                                                                \
44                                                                                              \
45             ITypesUtil::Unmarshal(reply, ipcStatus);                                         \
46         } while (0);                                                                         \
47         ipcStatus;                                                                           \
48     })
49 
UdmfServiceProxy(const sptr<IRemoteObject> & object)50 UdmfServiceProxy::UdmfServiceProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IUdmfService>(object)
51 {
52 }
53 
SetData(CustomOption & option,UnifiedData & unifiedData,std::string & key)54 int32_t UdmfServiceProxy::SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
55 {
56     UdmfConversion::InitValueObject(unifiedData);
57     MessageParcel reply;
58     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::SET_DATA, reply, option, unifiedData);
59     if (status != E_OK) {
60         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
61         return status;
62     }
63     if (!ITypesUtil::Unmarshal(reply, key)) {
64         LOG_ERROR(UDMF_SERVICE, "Unmarshal status failed!");
65         return E_READ_PARCEL_ERROR;
66     }
67     return status;
68 }
69 
GetData(const QueryOption & query,UnifiedData & unifiedData)70 int32_t UdmfServiceProxy::GetData(const QueryOption &query, UnifiedData &unifiedData)
71 {
72     MessageParcel reply;
73     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_DATA, reply, query);
74     if (status != E_OK) {
75         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s!", status, query.key.c_str());
76         return status;
77     }
78     if (!ITypesUtil::Unmarshal(reply, unifiedData)) {
79         LOG_ERROR(UDMF_SERVICE, "Unmarshal UnifiedData failed!");
80         return E_READ_PARCEL_ERROR;
81     }
82     return status;
83 }
84 
GetBatchData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)85 int32_t UdmfServiceProxy::GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
86 {
87     MessageParcel reply;
88     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_BATCH_DATA, reply, query);
89     if (status != E_OK) {
90         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s, intention:%{public}d!", status,
91                   query.key.c_str(), query.intention);
92         return status;
93     }
94     if (!ITypesUtil::Unmarshal(reply, unifiedDataSet)) {
95         LOG_ERROR(UDMF_SERVICE, "Unmarshal unifiedDataSet failed!");
96         return E_READ_PARCEL_ERROR;
97     }
98     return status;
99 }
100 
UpdateData(const QueryOption & query,UnifiedData & unifiedData)101 int32_t UdmfServiceProxy::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
102 {
103     UdmfConversion::InitValueObject(unifiedData);
104     MessageParcel reply;
105     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::UPDATE_DATA, reply, query, unifiedData);
106     if (status != E_OK) {
107         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
108         return status;
109     }
110     return status;
111 }
112 
DeleteData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)113 int32_t UdmfServiceProxy::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
114 {
115     MessageParcel reply;
116     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::DELETE_DATA, reply, query);
117     if (status != E_OK) {
118         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x,key: %{public}s, intention:%{public}d", status, query.key.c_str(),
119                   query.intention);
120         return status;
121     }
122     if (!ITypesUtil::Unmarshal(reply, unifiedDataSet)) {
123         LOG_ERROR(UDMF_SERVICE, "Unmarshal unifiedDataSet failed!");
124         return E_READ_PARCEL_ERROR;
125     }
126     LOG_DEBUG(UDMF_SERVICE, "end.");
127     return status;
128 }
129 
GetSummary(const QueryOption & query,Summary & summary)130 int32_t UdmfServiceProxy::GetSummary(const QueryOption &query, Summary &summary)
131 {
132     MessageParcel reply;
133     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_SUMMARY, reply, query);
134     if (status != E_OK) {
135         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s!", status, query.key.c_str());
136         return status;
137     }
138     if (!ITypesUtil::Unmarshal(reply, summary)) {
139         LOG_ERROR(UDMF_SERVICE, "Unmarshal summary failed!");
140         return E_READ_PARCEL_ERROR;
141     }
142     LOG_DEBUG(UDMF_SERVICE, "end.");
143     return status;
144 }
145 
AddPrivilege(const QueryOption & query,Privilege & privilege)146 int32_t UdmfServiceProxy::AddPrivilege(const QueryOption &query, Privilege &privilege)
147 {
148     MessageParcel reply;
149     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::ADD_PRIVILEGE, reply, query, privilege);
150     if (status != E_OK) {
151         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s", status, query.key.c_str());
152     }
153     LOG_DEBUG(UDMF_SERVICE, "end.");
154     return status;
155 }
156 
Sync(const QueryOption & query,const std::vector<std::string> & devices)157 int32_t UdmfServiceProxy::Sync(const QueryOption &query, const std::vector<std::string> &devices)
158 {
159     MessageParcel reply;
160     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::SYNC, reply, query, devices);
161     if (status != E_OK) {
162         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s", status, query.key.c_str());
163     }
164     LOG_DEBUG(UDMF_SERVICE, "end.");
165     return status;
166 }
167 
IsRemoteData(const QueryOption & query,bool & result)168 int32_t UdmfServiceProxy::IsRemoteData(const QueryOption &query, bool &result)
169 {
170     MessageParcel reply;
171     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::IS_REMOTE_DATA, reply, query);
172     if (status != E_OK) {
173         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s!", status, query.key.c_str());
174         return status;
175     }
176     if (!ITypesUtil::Unmarshal(reply, result)) {
177         LOG_ERROR(UDMF_SERVICE, "Unmarshal result failed!");
178         return E_READ_PARCEL_ERROR;
179     }
180     return status;
181 }
182 
SetAppShareOption(const std::string & intention,int32_t shareOption)183 int32_t UdmfServiceProxy::SetAppShareOption(const std::string &intention, int32_t shareOption)
184 {
185     MessageParcel reply;
186     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::SET_APP_SHARE_OPTION, reply, intention, shareOption);
187     if (status != E_OK) {
188         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
189         return status;
190     }
191     LOG_DEBUG(UDMF_SERVICE, "end.");
192     return status;
193 }
194 
GetAppShareOption(const std::string & intention,int32_t & shareOption)195 int32_t UdmfServiceProxy::GetAppShareOption(const std::string &intention, int32_t &shareOption)
196 {
197     MessageParcel reply;
198     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_APP_SHARE_OPTION, reply, intention);
199     if (status != E_OK) {
200         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
201         return status;
202     }
203 
204     if (!ITypesUtil::Unmarshal(reply, shareOption)) {
205         LOG_ERROR(UDMF_SERVICE, "Unmarshal shareOption failed!");
206         return E_READ_PARCEL_ERROR;
207     }
208     LOG_DEBUG(UDMF_SERVICE, "end.");
209     return status;
210 }
211 
RemoveAppShareOption(const std::string & intention)212 int32_t UdmfServiceProxy::RemoveAppShareOption(const std::string &intention)
213 {
214     MessageParcel reply;
215     int32_t status = IPC_SEND(UdmfServiceInterfaceCode::REMOVE_APP_SHARE_OPTION, reply, intention);
216     if (status != E_OK) {
217         LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
218         return status;
219     }
220     LOG_DEBUG(UDMF_SERVICE, "end.");
221     return status;
222 }
223 
SendRequest(UdmfServiceInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)224 int32_t UdmfServiceProxy::SendRequest(UdmfServiceInterfaceCode code, MessageParcel &data,
225                                       MessageParcel &reply, MessageOption &option)
226 {
227     sptr<IRemoteObject> remote = Remote();
228     if (remote == nullptr) {
229         return E_IPC;
230     }
231     int err = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
232     LOG_DEBUG(UDMF_SERVICE, "err: %{public}d", err);
233     return err;
234 }
235 } // namespace UDMF
236 } // namespace OHOS