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