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 #include "udmf_service_proxy.h"
17
18 #include "ipc_types.h"
19
20 #include "logger.h"
21 #include "tlv_util.h"
22 #include "udmf_types_util.h"
23
24 namespace OHOS {
25 namespace UDMF {
26 #define IPC_SEND(code, reply, ...) \
27 ({ \
28 int32_t __status = E_OK; \
29 do { \
30 MessageParcel request; \
31 if (!request.WriteInterfaceToken(GetDescriptor())) { \
32 __status = E_WRITE_PARCEL_ERROR; \
33 break; \
34 } \
35 if (!ITypesUtil::Marshal(request, ##__VA_ARGS__)) { \
36 __status = E_WRITE_PARCEL_ERROR; \
37 break; \
38 } \
39 MessageOption option; \
40 auto result = SendRequest(code, request, reply, option); \
41 if (result != 0) { \
42 LOG_ERROR(UDMF_SERVICE, "SendRequest failed, result = %{public}d!", result); \
43 __status = E_IPC; \
44 break; \
45 } \
46 \
47 ITypesUtil::Unmarshal(reply, __status); \
48 } while (0); \
49 __status; \
50 })
51
UdmfServiceProxy(const sptr<IRemoteObject> & object)52 UdmfServiceProxy::UdmfServiceProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IUdmfService>(object)
53 {
54 }
55
SetData(CustomOption & option,UnifiedData & unifiedData,std::string & key)56 int32_t UdmfServiceProxy::SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
57 {
58 MessageParcel reply;
59 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::SET_DATA, reply, option, unifiedData);
60 if (status != E_OK) {
61 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x!", status);
62 return status;
63 }
64 if (!ITypesUtil::Unmarshal(reply, key)) {
65 LOG_ERROR(UDMF_SERVICE, "Unmarshal status failed!");
66 return E_READ_PARCEL_ERROR;
67 }
68 return status;
69 }
70
GetData(const QueryOption & query,UnifiedData & unifiedData)71 int32_t UdmfServiceProxy::GetData(const QueryOption &query, UnifiedData &unifiedData)
72 {
73 MessageParcel reply;
74 int32_t status = IPC_SEND(UdmfServiceInterfaceCode::GET_DATA, reply, query);
75 if (status != E_OK) {
76 LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s!", status, query.key.c_str());
77 return status;
78 }
79 if (!ITypesUtil::Unmarshal(reply, unifiedData)) {
80 LOG_ERROR(UDMF_SERVICE, "Unmarshal UnifiedData failed!");
81 return E_READ_PARCEL_ERROR;
82 }
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 return status;
100 }
101
UpdateData(const QueryOption & query,UnifiedData & unifiedData)102 int32_t UdmfServiceProxy::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
103 {
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}s", status, query.key.c_str(),
119 UD_INTENTION_MAP.at(query.intention).c_str());
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
SendRequest(UdmfServiceInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)168 int32_t UdmfServiceProxy::SendRequest(UdmfServiceInterfaceCode code, MessageParcel &data,
169 MessageParcel &reply, MessageOption &option)
170 {
171 sptr<IRemoteObject> remote = Remote();
172 if (remote == nullptr) {
173 return E_IPC;
174 }
175 int err = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
176 LOG_DEBUG(UDMF_SERVICE, "err: %{public}d", err);
177 return err;
178 }
179 } // namespace UDMF
180 } // namespace OHOS