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 "hilog/log.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HiView-QuerySysEventCallbackProxy" };
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)27 void QuerySysEventCallbackProxy::OnQuery(const std::vector<std::u16string>& sysEvent, const std::vector<int64_t>& seq)
28 {
29 auto remote = Remote();
30 if (remote == nullptr) {
31 HiLog::Error(LABEL, "SysEventService Remote is NULL.");
32 return;
33 }
34 MessageParcel data;
35 if (!data.WriteInterfaceToken(QuerySysEventCallbackProxy::GetDescriptor())) {
36 HiLog::Error(LABEL, "write descriptor failed.");
37 return;
38 }
39 auto ashMemory = AshMemUtils::WriteBulkData(data, sysEvent);
40 if (ashMemory == nullptr) {
41 HiLog::Error(LABEL, "write sys event failed.");
42 return;
43 }
44 allAshMemories.emplace_back(ashMemory);
45 auto ret = data.WriteInt64Vector(seq);
46 if (!ret) {
47 HiLog::Error(LABEL, "write sys seq failed.");
48 return;
49 }
50 MessageParcel reply;
51 MessageOption option = {MessageOption::TF_ASYNC};
52 int32_t res = remote->SendRequest(ON_QUERY, data, reply, option);
53 if (res != ERR_OK) {
54 HiLog::Error(LABEL, "send request failed, error is %{public}d.", res);
55 }
56 }
57
OnComplete(int32_t reason,int32_t total,int64_t seq)58 void QuerySysEventCallbackProxy::OnComplete(int32_t reason, int32_t total, int64_t seq)
59 {
60 auto remote = Remote();
61 if (remote == nullptr) {
62 HiLog::Error(LABEL, "SysEventService Remote is NULL.");
63 return;
64 }
65 MessageParcel data;
66 if (!data.WriteInterfaceToken(QuerySysEventCallbackProxy::GetDescriptor())) {
67 HiLog::Error(LABEL, "write descriptor failed.");
68 return;
69 }
70 bool ret = data.WriteInt32(reason) && data.WriteInt32(total);
71 if (!ret) {
72 HiLog::Error(LABEL, "write params failed.");
73 return;
74 }
75 ret = data.WriteInt64(seq);
76 if (!ret) {
77 HiLog::Error(LABEL, "write seq failed.");
78 return;
79 }
80 MessageParcel reply;
81 MessageOption option = {MessageOption::TF_ASYNC};
82 int32_t res = remote->SendRequest(ON_COMPLETE, data, reply, option);
83 if (res != ERR_OK) {
84 HiLog::Error(LABEL, "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
102