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 UdmfConversion::ConvertRecordToSubclass(unifiedData);
83 return status;
84 }
85
GetBatchData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)86 int32_t UdmfServiceProxy::GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
87 {
88 MessageParcel reply;
89 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_BATCH_DATA, reply, query);
90 if (status != E_OK) {
91 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s, intention:%{public}d!", status,
92 query.key.c_str(), query.intention);
93 return status;
94 }
95 if (!ITypesUtil::Unmarshal(reply, unifiedDataSet)) {
96 LOG_ERROR(UDMF_SERVICE, "Unmarshal unifiedDataSet failed!");
97 return E_READ_PARCEL_ERROR;
98 }
99 UdmfConversion::ConvertRecordToSubclass(unifiedDataSet);
100 return status;
101 }
102
UpdateData(const QueryOption & query,UnifiedData & unifiedData)103 int32_t UdmfServiceProxy::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
104 {
105 UdmfConversion::InitValueObject(unifiedData);
106 MessageParcel reply;
107 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::UPDATE_DATA, reply, query, unifiedData);
108 if (status != E_OK) {
109 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
110 return status;
111 }
112 return status;
113 }
114
DeleteData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)115 int32_t UdmfServiceProxy::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
116 {
117 MessageParcel reply;
118 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::DELETE_DATA, reply, query);
119 if (status != E_OK) {
120 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x,key: %{public}s, intention:%{public}d", status, query.key.c_str(),
121 query.intention);
122 return status;
123 }
124 if (!ITypesUtil::Unmarshal(reply, unifiedDataSet)) {
125 LOG_ERROR(UDMF_SERVICE, "Unmarshal unifiedDataSet failed!");
126 return E_READ_PARCEL_ERROR;
127 }
128 UdmfConversion::ConvertRecordToSubclass(unifiedDataSet);
129 LOG_DEBUG(UDMF_SERVICE, "end.");
130 return status;
131 }
132
GetSummary(const QueryOption & query,Summary & summary)133 int32_t UdmfServiceProxy::GetSummary(const QueryOption &query, Summary &summary)
134 {
135 MessageParcel reply;
136 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_SUMMARY, reply, query);
137 if (status != E_OK) {
138 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s!", status, query.key.c_str());
139 return status;
140 }
141 if (!ITypesUtil::Unmarshal(reply, summary)) {
142 LOG_ERROR(UDMF_SERVICE, "Unmarshal summary failed!");
143 return E_READ_PARCEL_ERROR;
144 }
145 LOG_DEBUG(UDMF_SERVICE, "end.");
146 return status;
147 }
148
AddPrivilege(const QueryOption & query,Privilege & privilege)149 int32_t UdmfServiceProxy::AddPrivilege(const QueryOption &query, Privilege &privilege)
150 {
151 MessageParcel reply;
152 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::ADD_PRIVILEGE, reply, query, privilege);
153 if (status != E_OK) {
154 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s", status, query.key.c_str());
155 }
156 LOG_DEBUG(UDMF_SERVICE, "end.");
157 return status;
158 }
159
Sync(const QueryOption & query,const std::vector<std::string> & devices)160 int32_t UdmfServiceProxy::Sync(const QueryOption &query, const std::vector<std::string> &devices)
161 {
162 MessageParcel reply;
163 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::SYNC, reply, query, devices);
164 if (status != E_OK) {
165 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s", status, query.key.c_str());
166 }
167 LOG_DEBUG(UDMF_SERVICE, "end.");
168 return status;
169 }
170
IsRemoteData(const QueryOption & query,bool & result)171 int32_t UdmfServiceProxy::IsRemoteData(const QueryOption &query, bool &result)
172 {
173 MessageParcel reply;
174 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::IS_REMOTE_DATA, reply, query);
175 if (status != E_OK) {
176 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s!", status, query.key.c_str());
177 return status;
178 }
179 if (!ITypesUtil::Unmarshal(reply, result)) {
180 LOG_ERROR(UDMF_SERVICE, "Unmarshal result failed!");
181 return E_READ_PARCEL_ERROR;
182 }
183 return status;
184 }
185
SetAppShareOption(const std::string & intention,int32_t shareOption)186 int32_t UdmfServiceProxy::SetAppShareOption(const std::string &intention, int32_t shareOption)
187 {
188 MessageParcel reply;
189 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::SET_APP_SHARE_OPTION, reply, intention, shareOption);
190 if (status != E_OK) {
191 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
192 return status;
193 }
194 LOG_DEBUG(UDMF_SERVICE, "end.");
195 return status;
196 }
197
GetAppShareOption(const std::string & intention,int32_t & shareOption)198 int32_t UdmfServiceProxy::GetAppShareOption(const std::string &intention, int32_t &shareOption)
199 {
200 MessageParcel reply;
201 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_APP_SHARE_OPTION, reply, intention);
202 if (status != E_OK) {
203 LOG_WARN(UDMF_SERVICE, "status:0x%{public}x!", status);
204 return status;
205 }
206
207 if (!ITypesUtil::Unmarshal(reply, shareOption)) {
208 LOG_ERROR(UDMF_SERVICE, "Unmarshal shareOption failed!");
209 return E_READ_PARCEL_ERROR;
210 }
211 LOG_DEBUG(UDMF_SERVICE, "end.");
212 return status;
213 }
214
RemoveAppShareOption(const std::string & intention)215 int32_t UdmfServiceProxy::RemoveAppShareOption(const std::string &intention)
216 {
217 MessageParcel reply;
218 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::REMOVE_APP_SHARE_OPTION, reply, intention);
219 if (status != E_OK) {
220 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
221 return status;
222 }
223 LOG_DEBUG(UDMF_SERVICE, "end.");
224 return status;
225 }
226
SendRequest(UdmfServiceInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)227 int32_t UdmfServiceProxy::SendRequest(UdmfServiceInterfaceCode code, MessageParcel &data,
228 MessageParcel &reply, MessageOption &option)
229 {
230 sptr<IRemoteObject> remote = Remote();
231 if (remote == nullptr) {
232 return E_IPC;
233 }
234 int err = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
235 LOG_DEBUG(UDMF_SERVICE, "err: %{public}d", err);
236 return err;
237 }
238
ObtainAsynProcess(AsyncProcessInfo & processInfo)239 int32_t UdmfServiceProxy::ObtainAsynProcess(AsyncProcessInfo &processInfo)
240 {
241 MessageParcel reply;
242 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::OBTAIN_ASYN_PROCESS, reply, processInfo);
243 if (status != E_OK) {
244 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x", status);
245 return status;
246 }
247 if (!ITypesUtil::Unmarshal(reply, processInfo)) {
248 LOG_ERROR(UDMF_SERVICE, "Unmarshal AsyncProcessInfo failed!");
249 return E_READ_PARCEL_ERROR;
250 }
251 return E_OK;
252 }
253
ClearAsynProcessByKey(const std::string & businessUdKey)254 int32_t UdmfServiceProxy::ClearAsynProcessByKey(const std::string &businessUdKey)
255 {
256 MessageParcel reply;
257 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::CLEAR_ASYN_PROCESS_BY_KEY, reply, businessUdKey);
258 if (status != E_OK) {
259 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x", status);
260 return status;
261 }
262 return E_OK;
263 }
264 } // namespace UDMF
265 } // namespace OHOS