1 /*
2  * Copyright (c) 2022 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 "data_collect_manager_proxy.h"
17 
18 #include "security_guard_define.h"
19 #include "security_guard_log.h"
20 
21 namespace OHOS::Security::SecurityGuard {
DataCollectManagerProxy(const sptr<IRemoteObject> & impl)22 DataCollectManagerProxy::DataCollectManagerProxy(const sptr<IRemoteObject> &impl)
23     : IRemoteProxy<IDataCollectManager>(impl)
24 {
25 }
26 
RequestDataSubmit(int64_t eventId,std::string & version,std::string & time,std::string & content,bool isSync)27 int32_t DataCollectManagerProxy::RequestDataSubmit(int64_t eventId, std::string &version,
28     std::string &time, std::string &content, bool isSync)
29 {
30     SGLOGD("eventId=%{public}" PRId64 ", version=%{public}s", eventId, version.c_str());
31     MessageParcel data;
32     MessageParcel reply;
33     if (!data.WriteInterfaceToken(GetDescriptor())) {
34         SGLOGE("WriteInterfaceToken error");
35         return WRITE_ERR;
36     }
37     data.WriteInt64(eventId);
38     data.WriteString(version);
39     data.WriteString(time);
40     data.WriteString(content);
41 
42     MessageOption option = { isSync ? MessageOption::TF_SYNC : MessageOption::TF_ASYNC };
43     sptr<IRemoteObject> remote = Remote();
44     if (remote == nullptr) {
45         SGLOGE("Remote error");
46         return NULL_OBJECT;
47     }
48     int ret = remote->SendRequest(CMD_DATA_COLLECT, data, reply, option);
49     if (ret != ERR_NONE) {
50         SGLOGE("ret=%{public}d", ret);
51         return ret;
52     }
53     if (isSync) {
54         ret = reply.ReadInt32();
55         SGLOGD("reply=%{public}d", ret);
56     }
57     return ret;
58 }
59 
RequestRiskData(std::string & devId,std::string & eventList,const sptr<IRemoteObject> & callback)60 int32_t DataCollectManagerProxy::RequestRiskData(std::string &devId, std::string &eventList,
61     const sptr<IRemoteObject> &callback)
62 {
63     MessageParcel data;
64     MessageParcel reply;
65 
66     if (!data.WriteInterfaceToken(GetDescriptor())) {
67         SGLOGE("WriteInterfaceToken error");
68         return WRITE_ERR;
69     }
70     data.WriteString(devId);
71     data.WriteString(eventList);
72     data.WriteRemoteObject(callback);
73 
74     MessageOption option = { MessageOption::TF_SYNC };
75     sptr<IRemoteObject> remote = Remote();
76     if (remote == nullptr) {
77         SGLOGE("Remote error");
78         return NULL_OBJECT;
79     }
80     int ret = remote->SendRequest(CMD_DATA_REQUEST, data, reply, option);
81     if (ret != ERR_NONE) {
82         SGLOGE("ret=%{public}d", ret);
83         return ret;
84     }
85     ret = reply.ReadInt32();
86     SGLOGI("reply=%{public}d", ret);
87     return ret;
88 }
89 
Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)90 int32_t DataCollectManagerProxy::Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
91     const sptr<IRemoteObject> &callback)
92 {
93     MessageParcel data;
94     MessageParcel reply;
95 
96     if (!data.WriteInterfaceToken(GetDescriptor())) {
97         SGLOGE("WriteInterfaceToken error");
98         return WRITE_ERR;
99     }
100 
101     if (!data.WriteParcelable(&subscribeInfo)) {
102         SGLOGE("failed to write parcelable for subscribeInfo");
103         return WRITE_ERR;
104     }
105 
106     data.WriteRemoteObject(callback);
107 
108     MessageOption option = { MessageOption::TF_SYNC };
109     int ret = Remote()->SendRequest(CMD_DATA_SUBSCRIBE, data, reply, option);
110     if (ret != ERR_NONE) {
111         SGLOGE("ret=%{public}d", ret);
112         return ret;
113     }
114     ret = reply.ReadInt32();
115     SGLOGD("reply=%{public}d", ret);
116     return ret;
117 }
118 
Unsubscribe(const sptr<IRemoteObject> & callback)119 int32_t DataCollectManagerProxy::Unsubscribe(const sptr<IRemoteObject> &callback)
120 {
121     MessageParcel data;
122     MessageParcel reply;
123 
124     if (!data.WriteInterfaceToken(GetDescriptor())) {
125         SGLOGE("WriteInterfaceToken error");
126         return WRITE_ERR;
127     }
128 
129     data.WriteRemoteObject(callback);
130 
131     MessageOption option = { MessageOption::TF_SYNC };
132     int ret = Remote()->SendRequest(CMD_DATA_UNSUBSCRIBE, data, reply, option);
133     if (ret != ERR_NONE) {
134         SGLOGE("ret=%{public}d", ret);
135         return ret;
136     }
137     ret = reply.ReadInt32();
138     SGLOGD("reply=%{public}d", ret);
139     return ret;
140 }
141 
QuerySecurityEvent(std::vector<SecurityCollector::SecurityEventRuler> rulers,const sptr<IRemoteObject> & callback)142 int32_t DataCollectManagerProxy::QuerySecurityEvent(std::vector<SecurityCollector::SecurityEventRuler> rulers,
143     const sptr<IRemoteObject> &callback)
144 {
145     MessageParcel data;
146     MessageParcel reply;
147 
148     if (!data.WriteInterfaceToken(GetDescriptor())) {
149         SGLOGE("WriteInterfaceToken error");
150         return WRITE_ERR;
151     }
152 
153     if (!data.WriteUint32(rulers.size())) {
154         SGLOGE("failed to WriteInt32 for parcelable vector size");
155         return WRITE_ERR;
156     }
157 
158     for (const auto &ruler : rulers) {
159         if (!data.WriteParcelable(&ruler)) {
160             SGLOGE("failed to WriteParcelable for parcelable");
161             return WRITE_ERR;
162         }
163     }
164 
165     data.WriteRemoteObject(callback);
166 
167     MessageOption option = { MessageOption::TF_SYNC };
168     int ret = Remote()->SendRequest(CMD_SECURITY_EVENT_QUERY, data, reply, option);
169     if (ret != ERR_NONE) {
170         SGLOGE("ret=%{public}d", ret);
171         return ret;
172     }
173     ret = reply.ReadInt32();
174     SGLOGD("reply=%{public}d", ret);
175     return ret;
176 }
177 
CollectorStart(const SecurityCollector::SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)178 int32_t DataCollectManagerProxy::CollectorStart(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
179     const sptr<IRemoteObject> &callback)
180 {
181     MessageParcel data;
182     MessageParcel reply;
183 
184     if (!data.WriteInterfaceToken(GetDescriptor())) {
185         SGLOGE("WriteInterfaceToken error");
186         return WRITE_ERR;
187     }
188 
189     if (!data.WriteParcelable(&subscribeInfo)) {
190         SGLOGE("failed to write parcelable for subscribeInfo");
191         return WRITE_ERR;
192     }
193 
194     data.WriteRemoteObject(callback);
195 
196     MessageOption option = { MessageOption::TF_SYNC };
197     int ret = Remote()->SendRequest(CMD_SECURITY_COLLECTOR_START, data, reply, option);
198     if (ret != ERR_NONE) {
199         SGLOGE("ret=%{public}d", ret);
200         return ret;
201     }
202     ret = reply.ReadInt32();
203     SGLOGD("reply=%{public}d", ret);
204     return ret;
205 }
206 
CollectorStop(const SecurityCollector::SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)207 int32_t DataCollectManagerProxy::CollectorStop(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
208     const sptr<IRemoteObject> &callback)
209 {
210     MessageParcel data;
211     MessageParcel reply;
212 
213     if (!data.WriteInterfaceToken(GetDescriptor())) {
214         SGLOGE("WriteInterfaceToken error");
215         return WRITE_ERR;
216     }
217 
218     if (!data.WriteParcelable(&subscribeInfo)) {
219         SGLOGE("failed to write parcelable for subscribeInfo");
220         return WRITE_ERR;
221     }
222 
223     data.WriteRemoteObject(callback);
224 
225     MessageOption option = { MessageOption::TF_SYNC };
226     int ret = Remote()->SendRequest(CMD_SECURITY_COLLECTOR_STOP, data, reply, option);
227     if (ret != ERR_NONE) {
228         SGLOGE("ret=%{public}d", ret);
229         return ret;
230     }
231     ret = reply.ReadInt32();
232     SGLOGD("reply=%{public}d", ret);
233     return ret;
234 }
235 
ConfigUpdate(const SecurityGuard::SecurityConfigUpdateInfo & updateInfo)236 int32_t DataCollectManagerProxy::ConfigUpdate(const SecurityGuard::SecurityConfigUpdateInfo &updateInfo)
237 {
238     MessageParcel data;
239     MessageParcel reply;
240 
241     if (!data.WriteInterfaceToken(GetDescriptor())) {
242         SGLOGE("WriteInterfaceToken error");
243         return WRITE_ERR;
244     }
245 
246     if (!data.WriteString(updateInfo.GetFileName())) {
247         SGLOGE("failed to write file for config update");
248         return WRITE_ERR;
249     }
250 
251     if (!data.WriteFileDescriptor(updateInfo.GetFd())) {
252         SGLOGE("failed to write file descriptor for config update");
253         return WRITE_ERR;
254     }
255 
256     MessageOption option = { MessageOption::TF_SYNC };
257     int ret = Remote()->SendRequest(CMD_SECURITY_CONFIG_UPDATE, data, reply, option);
258     if (ret != ERR_NONE) {
259         SGLOGE("ret=%{public}d", ret);
260         return ret;
261     }
262     ret = reply.ReadInt32();
263     SGLOGD("reply=%{public}d", ret);
264     return ret;
265 }
266 }