1 /*
2 * Copyright (c) 2021-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 "sys_event_service_proxy.h"
17
18 #include "errors.h"
19 #include "hilog/log.h"
20 #include "parcelable_vector_rw.h"
21 #include "query_argument.h"
22 #include "ret_code.h"
23
24 #undef LOG_DOMAIN
25 #define LOG_DOMAIN 0xD002D08
26
27 #undef LOG_TAG
28 #define LOG_TAG "HISYSEVENT_SERVICE_PROXY"
29
30 namespace OHOS {
31 namespace HiviewDFX {
AddListener(const std::vector<SysEventRule> & rules,const sptr<ISysEventCallback> & callback)32 int32_t SysEventServiceProxy::AddListener(const std::vector<SysEventRule>& rules,
33 const sptr<ISysEventCallback>& callback)
34 {
35 auto remote = Remote();
36 if (remote == nullptr) {
37 HILOG_ERROR(LOG_CORE, "SysEventService Remote is NULL.");
38 return ERR_REMOTE_SERVICE_IS_NULL;
39 }
40 MessageParcel data;
41 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
42 HILOG_ERROR(LOG_CORE, "write descriptor failed.");
43 return ERR_CAN_NOT_WRITE_DESCRIPTOR;
44 }
45 bool ret = WriteVectorToParcel(data, rules);
46 if (!ret) {
47 HILOG_ERROR(LOG_CORE, "parcel write rules failed.");
48 return ERR_CAN_NOT_WRITE_PARCEL;
49 }
50 if (callback == nullptr) {
51 return ERR_PARCEL_DATA_IS_NULL;
52 }
53 ret = data.WriteRemoteObject(callback->AsObject());
54 if (!ret) {
55 HILOG_ERROR(LOG_CORE, "parcel write callback failed.");
56 return ERR_CAN_NOT_WRITE_REMOTE_OBJECT;
57 }
58 MessageParcel reply;
59 MessageOption option;
60 int32_t res = remote->SendRequest(ADD_SYS_EVENT_LISTENER, data, reply, option);
61 if (res != ERR_OK) {
62 HILOG_ERROR(LOG_CORE, "send request failed, error is %{public}d.", res);
63 return ERR_CAN_NOT_SEND_REQ;
64 }
65 int32_t result;
66 ret = reply.ReadInt32(result);
67 if (!ret) {
68 HILOG_ERROR(LOG_CORE, "parcel read result failed.");
69 return ERR_CAN_NOT_READ_PARCEL;
70 }
71 return result;
72 }
73
RemoveListener(const sptr<ISysEventCallback> & callback)74 int32_t SysEventServiceProxy::RemoveListener(const sptr<ISysEventCallback> &callback)
75 {
76 auto remote = Remote();
77 if (remote == nullptr) {
78 HILOG_ERROR(LOG_CORE, "SysEventService Remote is null.");
79 return ERR_REMOTE_SERVICE_IS_NULL;
80 }
81 MessageParcel data;
82 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
83 HILOG_ERROR(LOG_CORE, "write descriptor failed.");
84 return ERR_CAN_NOT_WRITE_DESCRIPTOR;
85 }
86 if (callback == nullptr) {
87 return ERR_PARCEL_DATA_IS_NULL;
88 }
89 bool ret = data.WriteRemoteObject(callback->AsObject());
90 if (!ret) {
91 HILOG_ERROR(LOG_CORE, "parcel write object in callback failed.");
92 return ERR_CAN_NOT_WRITE_REMOTE_OBJECT;
93 }
94 MessageParcel reply;
95 MessageOption option;
96 int32_t res = remote->SendRequest(REMOVE_SYS_EVENT_LISTENER, data, reply, option);
97 if (res != ERR_OK) {
98 HILOG_ERROR(LOG_CORE, "send request failed, error is %{public}d.", res);
99 return ERR_CAN_NOT_SEND_REQ;
100 }
101 int32_t result;
102 ret = reply.ReadInt32(result);
103 if (!ret) {
104 HILOG_ERROR(LOG_CORE, "parcel read result failed.");
105 return ERR_CAN_NOT_READ_PARCEL;
106 }
107 return result;
108 }
109
Query(const QueryArgument & queryArgument,const std::vector<SysEventQueryRule> & rules,const sptr<IQuerySysEventCallback> & callback)110 int32_t SysEventServiceProxy::Query(const QueryArgument& queryArgument, const std::vector<SysEventQueryRule>& rules,
111 const sptr<IQuerySysEventCallback>& callback)
112 {
113 auto remote = Remote();
114 if (remote == nullptr) {
115 HILOG_ERROR(LOG_CORE, "SysEventService Remote is null.");
116 return ERR_REMOTE_SERVICE_IS_NULL;
117 }
118 MessageParcel data;
119 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
120 HILOG_ERROR(LOG_CORE, "write descriptor failed.");
121 return ERR_CAN_NOT_WRITE_DESCRIPTOR;
122 }
123 if (!data.WriteParcelable(&queryArgument)) {
124 HILOG_ERROR(LOG_CORE, "parcel write query arguments failed.");
125 return ERR_CAN_NOT_WRITE_PARCEL;
126 }
127 bool ret = WriteVectorToParcel(data, rules);
128 if (!ret) {
129 HILOG_ERROR(LOG_CORE, "parcel write query rules failed.");
130 return ERR_CAN_NOT_WRITE_PARCEL;
131 }
132 if (callback == nullptr) {
133 return ERR_PARCEL_DATA_IS_NULL;
134 }
135 ret = data.WriteRemoteObject(callback->AsObject());
136 if (!ret) {
137 HILOG_ERROR(LOG_CORE, "parcel write callback failed.");
138 return ERR_CAN_NOT_WRITE_REMOTE_OBJECT;
139 }
140 MessageParcel reply;
141 MessageOption option;
142 int32_t res = remote->SendRequest(QUERY_SYS_EVENT, data, reply, option);
143 if (res != ERR_OK) {
144 HILOG_ERROR(LOG_CORE, "send request failed, error is %{public}d.", res);
145 return ERR_CAN_NOT_SEND_REQ;
146 }
147 int32_t result;
148 ret = reply.ReadInt32(result);
149 if (!ret) {
150 HILOG_ERROR(LOG_CORE, "parcel read result failed.");
151 return ERR_CAN_NOT_READ_PARCEL;
152 }
153 return result;
154 }
155
SetDebugMode(const sptr<ISysEventCallback> & callback,bool mode)156 int32_t SysEventServiceProxy::SetDebugMode(const sptr<ISysEventCallback>& callback, bool mode)
157 {
158 auto remote = Remote();
159 if (remote == nullptr) {
160 HILOG_ERROR(LOG_CORE, "SysEventService Remote is null.");
161 return ERR_REMOTE_SERVICE_IS_NULL;
162 }
163 MessageParcel data;
164 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
165 HILOG_ERROR(LOG_CORE, "write descriptor failed.");
166 return ERR_CAN_NOT_WRITE_DESCRIPTOR;
167 }
168 if (callback == nullptr) {
169 return ERR_PARCEL_DATA_IS_NULL;
170 }
171 bool ret = data.WriteRemoteObject(callback->AsObject());
172 if (!ret) {
173 HILOG_ERROR(LOG_CORE, "parcel write callback failed.");
174 return ERR_CAN_NOT_WRITE_REMOTE_OBJECT;
175 }
176 ret = data.WriteBool(mode);
177 if (!ret) {
178 HILOG_ERROR(LOG_CORE, "parcel write mode failed.");
179 return ERR_CAN_NOT_WRITE_PARCEL;
180 }
181 MessageParcel reply;
182 MessageOption option;
183 int32_t res = remote->SendRequest(SET_DEBUG_MODE, data, reply, option);
184 if (res != ERR_OK) {
185 HILOG_ERROR(LOG_CORE, "send request failed, error is %{public}d.", res);
186 return ERR_CAN_NOT_SEND_REQ;
187 }
188 int32_t result;
189 ret = reply.ReadInt32(result);
190 if (!ret) {
191 HILOG_ERROR(LOG_CORE, "parcel read result failed.");
192 return ERR_CAN_NOT_READ_PARCEL;
193 }
194 return result;
195 }
196
AddSubscriber(const std::vector<SysEventQueryRule> & rules)197 int64_t SysEventServiceProxy::AddSubscriber(const std::vector<SysEventQueryRule> &rules)
198 {
199 auto remote = Remote();
200 if (remote == nullptr) {
201 HILOG_ERROR(LOG_CORE, "SysEventService Remote is NULL.");
202 return ERR_REMOTE_SERVICE_IS_NULL;
203 }
204 MessageParcel data;
205 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
206 HILOG_ERROR(LOG_CORE, "write descriptor failed.");
207 return ERR_CAN_NOT_WRITE_DESCRIPTOR;
208 }
209 bool ret = WriteVectorToParcel(data, rules);
210 if (!ret) {
211 HILOG_ERROR(LOG_CORE, "parcel write rules failed.");
212 return ERR_CAN_NOT_WRITE_PARCEL;
213 }
214 MessageParcel reply;
215 MessageOption option;
216 int32_t res = remote->SendRequest(ADD_SYS_EVENT_SUBSCRIBER, data, reply, option);
217 if (res != ERR_OK) {
218 HILOG_ERROR(LOG_CORE, "send request failed, error is %{public}d.", res);
219 return ERR_CAN_NOT_SEND_REQ;
220 }
221 int64_t result;
222 ret = reply.ReadInt64(result);
223 if (!ret) {
224 HILOG_ERROR(LOG_CORE, "parcel read result failed.");
225 return ERR_CAN_NOT_READ_PARCEL;
226 }
227 return result;
228 }
229
RemoveSubscriber()230 int32_t SysEventServiceProxy::RemoveSubscriber()
231 {
232 auto remote = Remote();
233 if (remote == nullptr) {
234 HILOG_ERROR(LOG_CORE, "SysEventService Remote is NULL.");
235 return ERR_REMOTE_SERVICE_IS_NULL;
236 }
237 MessageParcel data;
238 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
239 HILOG_ERROR(LOG_CORE, "write descriptor failed.");
240 return ERR_CAN_NOT_WRITE_DESCRIPTOR;
241 }
242 MessageParcel reply;
243 MessageOption option;
244 int32_t res = remote->SendRequest(REMOVE_SYS_EVENT_SUBSCRIBER, data, reply, option);
245 if (res != ERR_OK) {
246 HILOG_ERROR(LOG_CORE, "send request failed, error is %{public}d.", res);
247 return ERR_CAN_NOT_SEND_REQ;
248 }
249 int32_t result;
250 auto ret = reply.ReadInt32(result);
251 if (!ret) {
252 HILOG_ERROR(LOG_CORE, "parcel read result failed.");
253 return ERR_CAN_NOT_READ_PARCEL;
254 }
255 return result;
256 }
257
Export(const QueryArgument & queryArgument,const std::vector<SysEventQueryRule> & rules)258 int64_t SysEventServiceProxy::Export(const QueryArgument &queryArgument, const std::vector<SysEventQueryRule> &rules)
259 {
260 auto remote = Remote();
261 if (remote == nullptr) {
262 HILOG_ERROR(LOG_CORE, "SysEventService Remote is NULL.");
263 return ERR_REMOTE_SERVICE_IS_NULL;
264 }
265 MessageParcel data;
266 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
267 HILOG_ERROR(LOG_CORE, "write descriptor failed.");
268 return ERR_CAN_NOT_WRITE_DESCRIPTOR;
269 }
270 if (!data.WriteParcelable(&queryArgument)) {
271 HILOG_ERROR(LOG_CORE, "parcel write export arguments failed.");
272 return ERR_CAN_NOT_WRITE_PARCEL;
273 }
274 bool ret = WriteVectorToParcel(data, rules);
275 if (!ret) {
276 HILOG_ERROR(LOG_CORE, "parcel write export rules failed.");
277 return ERR_CAN_NOT_WRITE_PARCEL;
278 }
279 MessageParcel reply;
280 MessageOption option;
281 int32_t res = remote->SendRequest(EXPORT_SYS_EVENT, data, reply, option);
282 if (res != ERR_OK) {
283 HILOG_ERROR(LOG_CORE, "send request failed, error is %{public}d.", res);
284 return ERR_CAN_NOT_SEND_REQ;
285 }
286 int64_t result;
287 ret = reply.ReadInt64(result);
288 if (!ret) {
289 HILOG_ERROR(LOG_CORE, "parcel read result failed.");
290 return ERR_CAN_NOT_READ_PARCEL;
291 }
292 return result;
293 }
294
295 } // namespace HiviewDFX
296 } // namespace OHOS
297
298