• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "hisysevent_c_wrapper.h"
17 
18 #include <string>
19 #include <map>
20 #include <mutex>
21 
22 #include "hisysevent_base_manager.h"
23 #include "hisysevent_rust_listener.h"
24 #include "hisysevent_rust_querier.h"
25 #include "ret_code.h"
26 
27 namespace {
28 using OHOS::HiviewDFX::HiSysEventBaseManager;
29 using OHOS::HiviewDFX::HiSysEventBaseQueryCallback;
30 using QueryArgCls = OHOS::HiviewDFX::QueryArg;
31 using QueryRuleCls = OHOS::HiviewDFX::QueryRule;
32 using OHOS::HiviewDFX::RuleType::WHOLE_WORD;
33 using OHOS::HiviewDFX::ERR_QUERY_RULE_INVALID;
34 using ListenerRuleCls = OHOS::HiviewDFX::ListenerRule;
35 using OHOS::HiviewDFX::HiSysEventBaseListener;
36 using OHOS::HiviewDFX::IPC_CALL_SUCCEED;
37 using OHOS::HiviewDFX::ERR_LISTENER_NOT_EXIST;
38 using OHOS::HiviewDFX::RuleType;
39 
40 static std::map<std::pair<OnRustCb, OnRustCb>, std::shared_ptr<HiSysEventBaseListener>> g_baseWatchers;
41 static std::mutex g_baseWatchersMutex;
42 static std::map<std::pair<OnRustCb, OnRustCb>, std::shared_ptr<HiSysEventRustListener>> g_watchers;
43 static std::mutex g_watchersMutex;
44 static std::map<std::pair<OnRustCb, OnRustCb>, std::shared_ptr<HiSysEventRustQuerier>> g_queriers;
45 static std::mutex g_queriersMutex;
46 
HiSysEventQuery(HiSysEventQueryArg * arg,const HiSysEventQueryRule rules[],const size_t ruleSize,HiSysEventRustQuerierC * querier)47 int HiSysEventQuery(HiSysEventQueryArg* arg, const HiSysEventQueryRule rules[], const size_t ruleSize,
48     HiSysEventRustQuerierC* querier)
49 {
50     if (querier == nullptr || querier->status != STATUS_NORMAL) {
51         return ERR_LISTENER_NOT_EXIST;
52     }
53     std::vector<QueryRuleCls> queryRules;
54     for (size_t i = 0; i < ruleSize; ++i) {
55         if (strlen(rules[i].domain) == 0 || rules[i].eventListSize == 0) {
56             return ERR_QUERY_RULE_INVALID;
57         }
58         std::vector<std::string> eventList;
59         for (size_t j = 0; j < rules[i].eventListSize; ++j) {
60             eventList.emplace_back(rules[i].eventList[j]);
61         }
62         std::string cond = rules[i].condition == nullptr ? "" : rules[i].condition;
63         queryRules.emplace_back(rules[i].domain, eventList, WHOLE_WORD, 0, cond);
64     }
65     int64_t beginTime = 0;
66     int64_t endTime = 0;
67     int32_t maxEvents = 0;
68     if (arg != nullptr) {
69         beginTime = arg->beginTime;
70         endTime = arg->endTime;
71         maxEvents = arg->maxEvents;
72     }
73     QueryArgCls argCls(beginTime, endTime, maxEvents);
74     auto querierRust = std::make_shared<HiSysEventRustQuerier>(querier);
75     auto baseQuerierRust = std::make_shared<HiSysEventBaseQueryCallback>(querierRust);
76     auto ret = HiSysEventBaseManager::Query(argCls, queryRules, baseQuerierRust);
77     if (ret == IPC_CALL_SUCCEED) {
78         std::lock_guard<std::mutex> lock(g_queriersMutex);
79         g_queriers[std::make_pair(querier->onQueryRustCb, querier->onCompleteRustCb)] = querierRust;
80     }
81     return ret;
82 }
83 
HiSysEventAddWatcher(HiSysEventRustWatcherC * watcher,const HiSysEventWatchRule rules[],const size_t ruleSize)84 int HiSysEventAddWatcher(HiSysEventRustWatcherC* watcher, const HiSysEventWatchRule rules[],
85     const size_t ruleSize)
86 {
87     if (watcher == nullptr || watcher->status != STATUS_NORMAL) {
88         return ERR_LISTENER_NOT_EXIST;
89     }
90     std::vector<ListenerRuleCls> watchRules;
91     for (size_t i = 0; i < ruleSize; ++i) {
92         watchRules.emplace_back(rules[i].domain, rules[i].name, rules[i].tag,
93             RuleType(rules[i].ruleType), static_cast<uint32_t>(rules[i].eventType));
94     }
95     auto listenerRust = std::make_shared<HiSysEventRustListener>(watcher);
96     auto baseListenerRust = std::make_shared<HiSysEventBaseListener>(listenerRust);
97     auto ret = HiSysEventBaseManager::AddListener(baseListenerRust, watchRules);
98     if (ret != IPC_CALL_SUCCEED) {
99         return ret;
100     }
101     {
102         std::lock_guard<std::mutex> lock(g_baseWatchersMutex);
103         g_baseWatchers[std::make_pair(watcher->onEventRustCb, watcher->onServiceDiedRustCb)] = baseListenerRust;
104     }
105     {
106         std::lock_guard<std::mutex> lock(g_watchersMutex);
107         g_watchers[std::make_pair(watcher->onEventRustCb, watcher->onServiceDiedRustCb)] = listenerRust;
108     }
109     return ret;
110 }
111 
HiSysEventRemoveWatcher(HiSysEventRustWatcherC * watcher)112 int HiSysEventRemoveWatcher(HiSysEventRustWatcherC* watcher)
113 {
114     if (watcher == nullptr || watcher->status != STATUS_NORMAL) {
115         return ERR_LISTENER_NOT_EXIST;
116     }
117     auto watcherKey = std::make_pair(watcher->onEventRustCb, watcher->onServiceDiedRustCb);
118     auto watcherIter = g_baseWatchers.find(watcherKey);
119     if (watcherIter == g_baseWatchers.end()) {
120         return ERR_LISTENER_NOT_EXIST;
121     }
122     auto ret = HiSysEventBaseManager::RemoveListener(watcherIter->second);
123     if (ret != IPC_CALL_SUCCEED) {
124         return ret;
125     }
126     {
127         std::lock_guard<std::mutex> lock(g_baseWatchersMutex);
128         g_baseWatchers.erase(watcherIter->first);
129     }
130     return ret;
131 }
132 
HiSysEventRecycleWatcher(HiSysEventRustWatcherC * watcher)133 void HiSysEventRecycleWatcher(HiSysEventRustWatcherC* watcher)
134 {
135     if (watcher == nullptr) {
136         return;
137     }
138     auto watcherKey = std::make_pair(watcher->onEventRustCb, watcher->onServiceDiedRustCb);
139     auto watcherIter = g_watchers.find(watcherKey);
140     if (watcherIter == g_watchers.end()) {
141         return;
142     }
143     auto listener = watcherIter->second;
144     if (listener != nullptr) {
145         listener->RecycleWatcher(watcher);
146     }
147     {
148         std::lock_guard<std::mutex> lock(g_watchersMutex);
149         g_watchers.erase(watcherIter->first);
150     }
151 }
152 
HiSysEventRecycleQuerier(HiSysEventRustQuerierC * querier)153 void HiSysEventRecycleQuerier(HiSysEventRustQuerierC* querier)
154 {
155     if (querier == nullptr) {
156         return;
157     }
158     auto querierKey = std::make_pair(querier->onQueryRustCb, querier->onCompleteRustCb);
159     auto querierIter = g_queriers.find(querierKey);
160     if (querierIter == g_queriers.end()) {
161         return;
162     }
163     auto callback = querierIter->second;
164     if (callback != nullptr) {
165         callback->RecycleQuerier(querier);
166     }
167     {
168         std::lock_guard<std::mutex> lock(g_queriersMutex);
169         g_queriers.erase(querierIter->first);
170     }
171 }
172 }
173 
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177 
OhHiSysEventAddRustWatcher(HiSysEventRustWatcherC * watcher,const HiSysEventWatchRule watchRules[],const size_t ruleSize)178 int OhHiSysEventAddRustWatcher(HiSysEventRustWatcherC* watcher, const HiSysEventWatchRule watchRules[],
179     const size_t ruleSize)
180 {
181     return HiSysEventAddWatcher(watcher, watchRules, ruleSize);
182 }
183 
OhHiSysEventRemoveRustWatcher(HiSysEventRustWatcherC * watcher)184 int OhHiSysEventRemoveRustWatcher(HiSysEventRustWatcherC* watcher)
185 {
186     return HiSysEventRemoveWatcher(watcher);
187 }
188 
OhHiSysEventRustQuery(HiSysEventQueryArg * queryArg,const HiSysEventQueryRule queryRules[],const size_t ruleSize,HiSysEventRustQuerierC * querier)189 int OhHiSysEventRustQuery(HiSysEventQueryArg* queryArg, const HiSysEventQueryRule queryRules[],
190     const size_t ruleSize, HiSysEventRustQuerierC* querier)
191 {
192     return HiSysEventQuery(queryArg, queryRules, ruleSize, querier);
193 }
194 
OhHiSysEventRecycleRustWatcher(HiSysEventRustWatcherC * watcher)195 void OhHiSysEventRecycleRustWatcher(HiSysEventRustWatcherC* watcher)
196 {
197     HiSysEventRecycleWatcher(watcher);
198 }
199 
OhHiSysEventRecycleRustQuerier(HiSysEventRustQuerierC * querier)200 void OhHiSysEventRecycleRustQuerier(HiSysEventRustQuerierC* querier)
201 {
202     HiSysEventRecycleQuerier(querier);
203 }
204 
205 #ifdef __cplusplus
206 }
207 #endif