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