1 /*
2 * Copyright (c) 2021 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
22 namespace OHOS {
23 namespace HiviewDFX {
24 static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HiView-SysEventServiceProxy" };
AddListener(const std::vector<SysEventRule> & rules,const sptr<ISysEventCallback> & callback)25 bool SysEventServiceProxy::AddListener(const std::vector<SysEventRule>& rules, const sptr<ISysEventCallback>& callback)
26 {
27 auto remote = Remote();
28 bool result = false;
29 if (remote == nullptr) {
30 HiLog::Error(LABEL, "SysEventService Remote is NULL.");
31 return result;
32 }
33 MessageParcel data;
34 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
35 HiLog::Error(LABEL, "write descriptor failed.");
36 return result;
37 }
38 bool ret = WriteVectorToParcel(data, rules);
39 if (!ret) {
40 HiLog::Error(LABEL, "parcel write rules failed.");
41 return result;
42 }
43 ret = data.WriteRemoteObject(callback->AsObject());
44 if (!ret) {
45 HiLog::Error(LABEL, "parcel write callback failed.");
46 return result;
47 }
48 MessageParcel reply;
49 MessageOption option;
50 int32_t res = remote->SendRequest(ADD_SYS_EVENT_LISTENER, data, reply, option);
51 if (res != ERR_OK) {
52 HiLog::Error(LABEL, "send request failed, error is %{public}d.", res);
53 return false;
54 }
55 ret = reply.ReadBool(result);
56 if (!ret) {
57 HiLog::Error(LABEL, "parcel read result failed.");
58 }
59 return result;
60 }
61
RemoveListener(const sptr<ISysEventCallback> & callback)62 bool SysEventServiceProxy::RemoveListener(const sptr<ISysEventCallback> &callback)
63 {
64 auto remote = Remote();
65 bool result = false;
66 if (remote == nullptr) {
67 HiLog::Error(LABEL, "SysEventService Remote is null.");
68 return result;
69 }
70 MessageParcel data;
71 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
72 HiLog::Error(LABEL, "write descriptor failed.");
73 return result;
74 }
75 bool ret = data.WriteRemoteObject(callback->AsObject());
76 if (!ret) {
77 HiLog::Error(LABEL, "parcel write object in callback failed.");
78 return result;
79 }
80 MessageParcel reply;
81 MessageOption option;
82 int32_t res = remote->SendRequest(REMOVE_SYS_EVENT_LISTENER, data, reply, option);
83 if (res != ERR_OK) {
84 HiLog::Error(LABEL, "send request failed, error is %{public}d.", res);
85 }
86 ret = reply.ReadBool(result);
87 if (!ret) {
88 HiLog::Error(LABEL, "parcel read result failed.");
89 }
90 return result;
91 }
92
QuerySysEvent(int64_t beginTime,int64_t endTime,int32_t maxEvents,const std::vector<SysEventQueryRule> & rules,const sptr<IQuerySysEventCallback> & callback)93 bool SysEventServiceProxy::QuerySysEvent(int64_t beginTime, int64_t endTime, int32_t maxEvents,
94 const std::vector<SysEventQueryRule>& rules, const sptr<IQuerySysEventCallback>& callback)
95 {
96 bool result = false;
97 auto remote = Remote();
98 if (remote == nullptr) {
99 HiLog::Error(LABEL, "SysEventService Remote is null.");
100 return result;
101 }
102 MessageParcel data;
103 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
104 HiLog::Error(LABEL, "write descriptor failed.");
105 return result;
106 }
107
108 bool ret = data.WriteInt64(beginTime);
109 if (!ret) {
110 HiLog::Error(LABEL, "parcel write begin time failed.");
111 return result;
112 }
113 ret = data.WriteInt64(endTime);
114 if (!ret) {
115 HiLog::Error(LABEL, "parcel write end time failed.");
116 return result;
117 }
118 ret = data.WriteInt32(maxEvents);
119 if (!ret) {
120 HiLog::Error(LABEL, "parcel write max events failed.");
121 return result;
122 }
123 ret = WriteVectorToParcel(data, rules);
124 if (!ret) {
125 HiLog::Error(LABEL, "parcel write query rules failed.");
126 return result;
127 }
128 ret = data.WriteRemoteObject(callback->AsObject());
129 if (!ret) {
130 HiLog::Error(LABEL, "parcel write callback failed.");
131 return result;
132 }
133
134 MessageParcel reply;
135 MessageOption option;
136 int32_t res = remote->SendRequest(QUERY_SYS_EVENT, data, reply, option);
137 if (res != ERR_OK) {
138 HiLog::Error(LABEL, "send request failed, error is %{public}d.", res);
139 return result;
140 }
141 ret = reply.ReadBool(result);
142 if (!ret) {
143 HiLog::Error(LABEL, "parcel read result failed.");
144 return result;
145 }
146 return result;
147 }
148
SetDebugMode(const sptr<ISysEventCallback> & callback,bool mode)149 bool SysEventServiceProxy::SetDebugMode(const sptr<ISysEventCallback>& callback, bool mode)
150 {
151 bool result = false;
152 auto remote = Remote();
153 if (remote == nullptr) {
154 HiLog::Error(LABEL, "SysEventService Remote is null.");
155 return result;
156 }
157 MessageParcel data;
158 if (!data.WriteInterfaceToken(SysEventServiceProxy::GetDescriptor())) {
159 HiLog::Error(LABEL, "write descriptor failed.");
160 return result;
161 }
162
163 bool ret = data.WriteRemoteObject(callback->AsObject());
164 if (!ret) {
165 HiLog::Error(LABEL, "parcel write callback failed.");
166 return result;
167 }
168 ret = data.WriteBool(mode);
169 if (!ret) {
170 HiLog::Error(LABEL, "parcel write mode failed.");
171 return result;
172 }
173 MessageParcel reply;
174 MessageOption option;
175 int32_t res = remote->SendRequest(SET_DEBUG_MODE, data, reply, option);
176 if (res != ERR_OK) {
177 HiLog::Error(LABEL, "send request failed, error is %{public}d.", res);
178 return result;
179 }
180 ret = reply.ReadBool(result);
181 if (!ret) {
182 HiLog::Error(LABEL, "parcel read result failed.");
183 return result;
184 }
185 return result;
186 }
187 } // namespace HiviewDFX
188 } // namespace OHOS
189