• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "test_plugin.h"
16 
17 #include "cjson_util.h"
18 #include "common_event_manager.h"
19 #include "hiview_logger.h"
20 #include "hiview_global.h"
21 #include "plugin_factory.h"
22 #include "test_content.h"
23 #include "trace_collector.h"
24 
25 namespace OHOS::HiviewDFX {
26 namespace {
27 REGISTER(TraceTestPlugin);
28 DEFINE_LOG_TAG("TraceTestPlugin");
29 
30 const std::map<std::string, std::function<void(const CommonEventData &)>> TEST_CONTENT = {
31     {"xperf_dump", TestXperfDump},
32     {"reliability_dump", TestReliabilityDump},
33     {"xpower_dump", TestXpowerDump},
34     {"other_dump", TestOtherDump},
35     {"screen_dump", TestScreenDump},
36     {"telemetryStart", TestTelemetryStart},
37     {"telemetryEnd", TestTelemetryEnd},
38     {"traceOnXpower", TestTraceOnXpower},
39     {"traceOffXpower", TestTraceOffXpower},
40     {"traceOnXPerf", TestTraceOnXPerf},
41     {"traceOffXPerf", TestTraceOffXPerf},
42     {"telemetryDumpXpower", TelemetryDumpXpower},
43     {"telemetryDumpXPerf", TelemetryDumpXPerf}
44 };
45 }
46 using namespace UCollectUtil;
47 using namespace UCollect;
48 
OnLoad()49 void TraceTestPlugin::OnLoad()
50 {
51     HIVIEW_LOGI("Register matchingSkills");
52     MatchingSkills matchingSkills;
53     matchingSkills.AddEvent("telemetryStart");
54     matchingSkills.AddEvent("telemetryEnd");
55     matchingSkills.AddEvent("telemetryDumpXpower");
56     matchingSkills.AddEvent("telemetryDumpXPerf");
57     matchingSkills.AddEvent("traceOnXpower");
58     matchingSkills.AddEvent("traceOffXpower");
59     matchingSkills.AddEvent("traceOnXPerf");
60     matchingSkills.AddEvent("traceOffXPerf");
61     matchingSkills.AddEvent("xperf_dump");
62     matchingSkills.AddEvent("reliability_dump");
63     matchingSkills.AddEvent("xpower_dump");
64     matchingSkills.AddEvent("other_dump");
65     matchingSkills.AddEvent("screen_dump");
66 
67     CommonEventSubscribeInfo info(matchingSkills);
68     telemetrySubscriber_ = std::make_shared<TestSubscriber>(info);
69     if (!CommonEventManager::SubscribeCommonEvent(telemetrySubscriber_)) {
70         HIVIEW_LOGE("register telemetrySubscriber_ fail");
71     }
72 }
73 
OnEvent(std::shared_ptr<Event> & event)74 bool TraceTestPlugin::OnEvent(std::shared_ptr<Event> &event)
75 {
76     HIVIEW_LOGI("event:%{public}s", event->eventName_.c_str());
77     return true;
78 }
79 
OnUnload()80 void TraceTestPlugin::OnUnload()
81 {
82     HIVIEW_LOGI("UnRegister matchingSkills");
83     if (telemetrySubscriber_ != nullptr) {
84         CommonEventManager::UnSubscribeCommonEvent(telemetrySubscriber_);
85     }
86 }
87 
OnReceiveEvent(const CommonEventData & data)88 void TestSubscriber::OnReceiveEvent(const CommonEventData &data)
89 {
90     std::string action = data.GetWant().GetAction();
91     auto it = TEST_CONTENT.find(action);
92     if (it == TEST_CONTENT.end()) {
93         return;
94     }
95     it->second(data);
96 }
97 
TestSubscriber(const CommonEventSubscribeInfo & subscribeInfo)98 TestSubscriber::TestSubscriber(const CommonEventSubscribeInfo &subscribeInfo)
99     : CommonEventSubscriber(subscribeInfo) {}
100 
101 }
102