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 "query_sys_event_callback_proxy.h"
17
18 #include "ash_mem_utils.h"
19 #include "errors.h"
20 #include "hiview_logger.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 DEFINE_LOG_TAG("HiView-QuerySysEventCallbackProxy");
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)25 void QuerySysEventCallbackProxy::OnQuery(const std::vector<std::u16string>& sysEvent, const std::vector<int64_t>& seq)
26 {
27 auto remote = Remote();
28 if (remote == nullptr) {
29 HIVIEW_LOGE("SysEventService Remote is NULL.");
30 return;
31 }
32 MessageParcel data;
33 if (!data.WriteInterfaceToken(QuerySysEventCallbackProxy::GetDescriptor())) {
34 HIVIEW_LOGE("write descriptor failed.");
35 return;
36 }
37 auto ashMemory = AshMemUtils::WriteBulkData(data, sysEvent);
38 if (ashMemory == nullptr) {
39 HIVIEW_LOGE("write sys event failed.");
40 return;
41 }
42 allAshMemories.emplace_back(ashMemory);
43 auto ret = data.WriteInt64Vector(seq);
44 if (!ret) {
45 HIVIEW_LOGE("write sys seq failed.");
46 return;
47 }
48 MessageParcel reply;
49 MessageOption option = {MessageOption::TF_ASYNC};
50 int32_t res = remote->SendRequest(
51 static_cast<uint32_t>(QuerySysEventCallbackInterfaceCode::ON_QUERY), data, reply, option);
52 if (res != ERR_OK) {
53 HIVIEW_LOGE("send request failed, error is %{public}d.", res);
54 }
55 }
56
OnComplete(int32_t reason,int32_t total,int64_t seq)57 void QuerySysEventCallbackProxy::OnComplete(int32_t reason, int32_t total, int64_t seq)
58 {
59 auto remote = Remote();
60 if (remote == nullptr) {
61 HIVIEW_LOGE("SysEventService Remote is NULL.");
62 return;
63 }
64 MessageParcel data;
65 if (!data.WriteInterfaceToken(QuerySysEventCallbackProxy::GetDescriptor())) {
66 HIVIEW_LOGE("write descriptor failed.");
67 return;
68 }
69 bool ret = data.WriteInt32(reason) && data.WriteInt32(total);
70 if (!ret) {
71 HIVIEW_LOGE("write params failed.");
72 return;
73 }
74 ret = data.WriteInt64(seq);
75 if (!ret) {
76 HIVIEW_LOGE("write seq failed.");
77 return;
78 }
79 MessageParcel reply;
80 MessageOption option = {MessageOption::TF_ASYNC};
81 int32_t res = remote->SendRequest(
82 static_cast<uint32_t>(QuerySysEventCallbackInterfaceCode::ON_COMPLETE), data, reply, option);
83 if (res != ERR_OK) {
84 HIVIEW_LOGE("send request failed, error is %{public}d.", res);
85 }
86 }
87
~QuerySysEventCallbackProxy()88 QuerySysEventCallbackProxy::~QuerySysEventCallbackProxy()
89 {
90 ClearAllAshMemories();
91 }
92
ClearAllAshMemories()93 void QuerySysEventCallbackProxy::ClearAllAshMemories()
94 {
95 for_each(allAshMemories.begin(), allAshMemories.end(), [] (auto& ashMem) {
96 AshMemUtils::CloseAshmem(ashMem);
97 });
98 allAshMemories.clear();
99 }
100 } // namespace HiviewDFX
101 } // namespace OHOS