• 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 "active_key_event.h"
17 
18 #include <vector>
19 
20 #include "event_log_task.h"
21 #include "file_util.h"
22 #include "logger.h"
23 #include "sys_event.h"
24 #include "time_util.h"
25 #include "trace_collector.h"
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 constexpr int32_t HOLD_TIME_500_MS = 500;
30 constexpr int32_t HOLD_TIME_3000_MS = 3000;
31 }
32 DEFINE_LOG_LABEL(0xD002D01, "EventLogger-ActiveKeyEvent");
ActiveKeyEvent()33 ActiveKeyEvent::ActiveKeyEvent()
34 {
35     triggeringTime_ = 0;
36     eventPool_ = nullptr;
37     logStore_ = nullptr;
38 }
39 
~ActiveKeyEvent()40 ActiveKeyEvent::~ActiveKeyEvent()
41 {
42     for (auto it = subscribeIds_.begin(); it != subscribeIds_.end(); it = subscribeIds_.erase(it)) {
43         if (*it >= 0) {
44             MMI::InputManager::GetInstance()->UnsubscribeKeyEvent(*it);
45             HIVIEW_LOGI("~ActiveKeyEvent subscribeId_: %{public}d", *it);
46         }
47     }
48 }
49 
SystemTimeMillisecond()50 int64_t ActiveKeyEvent::SystemTimeMillisecond()
51 {
52     struct timespec t;
53     t.tv_sec = 0;
54     t.tv_nsec = 0;
55     clock_gettime(CLOCK_MONOTONIC, &t);
56     return (int64_t)((t.tv_sec) * TimeUtil::SEC_TO_NANOSEC + t.tv_nsec) / TimeUtil::SEC_TO_MICROSEC;
57 }
58 
InitSubscribe(std::set<int32_t> preKeys,int32_t finalKey,int32_t count,int32_t holdTime)59 void ActiveKeyEvent::InitSubscribe(std::set<int32_t> preKeys, int32_t finalKey, int32_t count, int32_t holdTime)
60 {
61     const int32_t maxCount = 5;
62     if (++count > maxCount) {
63         return;
64     }
65     std::shared_ptr<MMI::KeyOption> keyOption = std::make_shared<MMI::KeyOption>();
66     if (keyOption == nullptr) {
67         HIVIEW_LOGE("Invalid key option");
68         return;
69     }
70 
71     keyOption->SetPreKeys(preKeys);
72     keyOption->SetFinalKey(finalKey);
73     keyOption->SetFinalKeyDown(true);
74     keyOption->SetFinalKeyDownDuration(holdTime);
75     auto keyEventCallBack = std::bind(&ActiveKeyEvent::CombinationKeyCallback, this, std::placeholders::_1);
76     int32_t subscribeId = MMI::InputManager::GetInstance()->SubscribeKeyEvent(keyOption, keyEventCallBack);
77     if (subscribeId < 0) {
78         HIVIEW_LOGE("SubscribeKeyEvent failed, finalKey: %{public}d,"
79             "subscribeId: %{public}d option failed.", finalKey, subscribeId);
80         if (eventPool_ == nullptr) {
81             return;
82         }
83         auto initSubscribe = std::bind(&ActiveKeyEvent::InitSubscribe, this, preKeys, finalKey, count, holdTime);
84         eventPool_->AddTask(initSubscribe, "initSubscribe" + std::to_string(finalKey) +
85             "_" + std::to_string(count), initDelay_ * count);
86     }
87     subscribeIds_.emplace_back(subscribeId);
88     HIVIEW_LOGI("CombinationKeyInit finalKey: %{public}d subscribeId_: %{public}d",
89         finalKey, subscribeId);
90 }
91 
Init(std::shared_ptr<EventThreadPool> eventPool,std::shared_ptr<LogStoreEx> logStore)92 void ActiveKeyEvent::Init(std::shared_ptr<EventThreadPool> eventPool, std::shared_ptr<LogStoreEx> logStore)
93 {
94     HIVIEW_LOGI("CombinationKeyInit");
95     eventPool_ = eventPool;
96     logStore_ = logStore;
97 
98     std::set<int32_t> preDownKeys;
99     preDownKeys.insert(MMI::KeyEvent::KEYCODE_VOLUME_UP);
100     std::set<int32_t> prePowerKeys;
101     prePowerKeys.insert(MMI::KeyEvent::KEYCODE_VOLUME_DOWN);
102     std::set<int32_t> preOnlyPowerKeys;
103 
104     if (eventPool_ == nullptr) {
105         InitSubscribe(preDownKeys, MMI::KeyEvent::KEYCODE_VOLUME_DOWN, 0, HOLD_TIME_500_MS);
106         InitSubscribe(prePowerKeys, MMI::KeyEvent::KEYCODE_POWER, 0, HOLD_TIME_500_MS);
107         InitSubscribe(preOnlyPowerKeys, MMI::KeyEvent::KEYCODE_POWER, 0, HOLD_TIME_3000_MS);
108         return;
109     }
110     auto initSubscribeDown = std::bind(&ActiveKeyEvent::InitSubscribe, this,
111         preDownKeys, MMI::KeyEvent::KEYCODE_VOLUME_DOWN, 0, HOLD_TIME_500_MS);
112     auto initSubscribePower = std::bind(&ActiveKeyEvent::InitSubscribe, this,
113         prePowerKeys, MMI::KeyEvent::KEYCODE_POWER, 0, HOLD_TIME_500_MS);
114     auto initSubscribeOnlyPower = std::bind(&ActiveKeyEvent::InitSubscribe, this,
115         preOnlyPowerKeys, MMI::KeyEvent::KEYCODE_POWER, 0, HOLD_TIME_3000_MS);
116     eventPool_->AddTask(initSubscribeDown, "initSubscribeDown", initDelay_);
117     eventPool_->AddTask(initSubscribePower, "initSubscribePower", initDelay_);
118     eventPool_->AddTask(initSubscribeOnlyPower, "initSubscribeOnlyPower", initDelay_);
119 }
120 
HitraceCapture()121 void ActiveKeyEvent::HitraceCapture()
122 {
123     std::shared_ptr<UCollectUtil::TraceCollector> collector = UCollectUtil::TraceCollector::Create();
124     UCollectUtil::TraceCollector::Caller caller = UCollectUtil::TraceCollector::Caller::BETACLUB;
125     auto result = collector->DumpTrace(caller);
126     if (result.retCode != 0) {
127         HIVIEW_LOGE("get hitrace fail! error code: %{public}d", result.retCode);
128         return;
129     }
130 }
131 
SysMemCapture(int fd)132 void ActiveKeyEvent::SysMemCapture(int fd)
133 {
134     FileUtil::SaveStringToFd(fd, "\n\ncatcher cmd : /proc/meminfo\n");
135     std::string content;
136     FileUtil::LoadStringFromFile("/proc/meminfo", content);
137     FileUtil::SaveStringToFd(fd, content);
138 }
139 
DumpCapture(int fd)140 void ActiveKeyEvent::DumpCapture(int fd)
141 {
142     SysEventCreator sysEventCreator("HIVIEWDFX", "ACTIVE_KEY", SysEventCreator::FAULT);
143     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("ActiveKeyEvent", nullptr, sysEventCreator);
144     int noNeedJsonFd = -1;
145     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, noNeedJsonFd, sysEvent);
146     for (const std::string& cmd : CMD_LIST) {
147         logTask->AddLog(cmd);
148     }
149 
150     auto ret = logTask->StartCompose();
151     if (ret != EventLogTask::TASK_SUCCESS) {
152         HIVIEW_LOGE("capture fail %{public}d", ret);
153     }
154     SysMemCapture(fd);
155 }
156 
CombinationKeyHandle(std::shared_ptr<MMI::KeyEvent> keyEvent)157 void ActiveKeyEvent::CombinationKeyHandle(std::shared_ptr<MMI::KeyEvent> keyEvent)
158 {
159     HIVIEW_LOGI("Receive CombinationKeyHandle.");
160     if (logStore_ == nullptr) {
161         return;
162     }
163 
164     std::string logFile = "ACTIVE_KEY_EVENT-0-" +
165         TimeUtil::TimestampFormatToDate(TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC,
166         "%Y%m%d%H%M%S") + ".log";
167     if (FileUtil::FileExists("/data/log/eventlog/" + logFile)) {
168         HIVIEW_LOGW("filename: %{public}s is existed, direct use.", logFile.c_str());
169         return;
170     }
171     int fd = logStore_->CreateLogFile(logFile);
172 
173     auto sysStart = ActiveKeyEvent::SystemTimeMillisecond();
174     const uint32_t placeholder = 3;
175     auto start = TimeUtil::GetMilliseconds();
176     uint64_t startTime = start / TimeUtil::SEC_TO_MILLISEC;
177     std::ostringstream startTimeStr;
178     startTimeStr << "start time: " << TimeUtil::TimestampFormatToDate(startTime, "%Y/%m/%d-%H:%M:%S");
179     startTimeStr << ":" << std::setw(placeholder) << std::setfill('0') <<
180         std::to_string(start % TimeUtil::SEC_TO_MILLISEC) << std::endl;
181     std::vector<int32_t> keys = keyEvent->GetPressedKeys();
182     for (auto& i : keys) {
183         startTimeStr << "CombinationKeyCallback key : ";
184         startTimeStr << MMI::KeyEvent::KeyCodeToString(i) << std::endl;
185     }
186     FileUtil::SaveStringToFd(fd, startTimeStr.str());
187 
188     if (eventPool_ == nullptr) {
189         HitraceCapture();
190     } else {
191         auto hitraceCapture = std::bind(&ActiveKeyEvent::HitraceCapture, this);
192         eventPool_->AddTask(hitraceCapture, "HitraceCapture", 0, EventThreadPool::Priority::HIGH_PRIORITY);
193     }
194 
195     DumpCapture(fd);
196     auto end = ActiveKeyEvent::SystemTimeMillisecond();
197     std::string totalTime = "\n\nCatcher log total time is " + std::to_string(end - sysStart) + "ms\n";
198     FileUtil::SaveStringToFd(fd, totalTime);
199     close(fd);
200 }
201 
CombinationKeyCallback(std::shared_ptr<MMI::KeyEvent> keyEvent)202 void ActiveKeyEvent::CombinationKeyCallback(std::shared_ptr<MMI::KeyEvent> keyEvent)
203 {
204     HIVIEW_LOGI("Receive CombinationKeyCallback key id: %{public}d.", keyEvent->GetId());
205     uint64_t now = (uint64_t)ActiveKeyEvent::SystemTimeMillisecond();
206     const uint64_t interval = 10000;
207     if (now - triggeringTime_ < interval) {
208         return;
209     }
210     triggeringTime_ = now;
211     auto combinationKeyHandle = std::bind(&ActiveKeyEvent::CombinationKeyHandle, this, keyEvent);
212     if (eventPool_ != nullptr) {
213         eventPool_->AddTask(combinationKeyHandle, "ActiveKeyEvent", 0, EventThreadPool::Priority::HIGH_PRIORITY);
214     } else {
215         CombinationKeyHandle(keyEvent);
216     }
217 }
218 } // namesapce HiviewDFX
219 } // namespace OHOS