• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "monitor_task_timer.h"
17 
18 #include "anonymous_string.h"
19 #include "capability_info_manager.h"
20 #include "distributed_hardware_errno.h"
21 #include "distributed_hardware_log.h"
22 #include "task_board.h"
23 #include "task_executor.h"
24 #include "task_factory.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 IMPLEMENT_SINGLE_INSTANCE(MonitorTaskTimer);
29 namespace {
30     const std::string MONITOR_TASK_TIMER_ID = "monitor_task_timer_id";
31     constexpr int32_t DELAY_TIME_MS = 5000;
32 }
33 #undef DH_LOG_TAG
34 #define DH_LOG_TAG "MonitorTaskTimer"
35 
MonitorTaskTimer()36 MonitorTaskTimer::MonitorTaskTimer()
37 {
38     DHLOGI("MonitorTaskTimer construction");
39 }
40 
~MonitorTaskTimer()41 MonitorTaskTimer::~MonitorTaskTimer()
42 {
43     DHLOGI("MonitorTaskTimer destruction");
44     ReleaseTimer();
45 }
46 
InitTimer()47 void MonitorTaskTimer::InitTimer()
48 {
49     DHLOGI("start");
50     std::unique_lock<std::mutex> lock(monitorTaskTimerMutex_);
51     if (eventHandler_ == nullptr) {
52         eventHandlerThread_ = std::thread(&MonitorTaskTimer::StartEventRunner, this);
53         monitorTaskTimerCond_.wait(lock, [this] {
54             return eventHandler_ != nullptr;
55         });
56     }
57     DHLOGI("end");
58 }
59 
ReleaseTimer()60 void MonitorTaskTimer::ReleaseTimer()
61 {
62     DHLOGI("start");
63     StopTimer();
64     DHLOGI("end");
65 }
66 
StartEventRunner()67 void MonitorTaskTimer::StartEventRunner()
68 {
69     DHLOGI("start");
70     auto busRunner = AppExecFwk::EventRunner::Create(false);
71     if (busRunner == nullptr) {
72         DHLOGE("busRunner is nullptr!");
73         return;
74     }
75 
76     {
77         std::lock_guard<std::mutex> lock(monitorTaskTimerMutex_);
78         eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(busRunner);
79     }
80     monitorTaskTimerCond_.notify_all();
81     busRunner->Run();
82     DHLOGI("end");
83 }
84 
StartTimer()85 void MonitorTaskTimer::StartTimer()
86 {
87     DHLOGI("start");
88     InitTimer();
89     std::lock_guard<std::mutex> lock(monitorTaskTimerMutex_);
90     if (eventHandler_ == nullptr) {
91         DHLOGE("eventHandler is nullptr!");
92         return;
93     }
94     auto monitorTaskTimer = [this] {Execute(eventHandler_);};
95     eventHandler_->PostTask(monitorTaskTimer, MONITOR_TASK_TIMER_ID, DELAY_TIME_MS);
96 }
97 
StopTimer()98 void MonitorTaskTimer::StopTimer()
99 {
100     DHLOGI("start");
101     std::lock_guard<std::mutex> lock(monitorTaskTimerMutex_);
102     if (eventHandler_ != nullptr) {
103         eventHandler_->RemoveTask(MONITOR_TASK_TIMER_ID);
104         if (eventHandler_->GetEventRunner() != nullptr) {
105             eventHandler_->GetEventRunner()->Stop();
106         }
107     }
108     if (eventHandlerThread_.joinable()) {
109         eventHandlerThread_.join();
110     }
111     eventHandler_ = nullptr;
112     DHLOGI("end");
113 }
114 
Execute(const std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler)115 void MonitorTaskTimer::Execute(const std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler)
116 {
117     DHLOGI("start");
118     if (eventHandler == nullptr) {
119         DHLOGE("eventHandler is nullptr!");
120         return;
121     }
122     auto enabledDevices = TaskBoard::GetInstance().GetEnabledDevice();
123     std::shared_ptr<CapabilityInfo> capInfoPtr = nullptr;
124     TaskParam taskParam;
125     std::string capabilityKey;
126     for (auto item : enabledDevices) {
127         capabilityKey = item.first;
128         taskParam = item.second;
129         if (taskParam.dhType != DHType::INPUT) {
130             continue;
131         }
132         if (CapabilityInfoManager::GetInstance()->GetDataByKey(capabilityKey, capInfoPtr) != DH_FWK_SUCCESS) {
133             DHLOGI("CapabilityInfoManager can not find this key in DB, key: %s, networkId: %s, uuid: %s, dhId: %s",
134                 GetAnonyString(capabilityKey).c_str(), GetAnonyString(taskParam.networkId).c_str(),
135                 GetAnonyString(taskParam.uuid).c_str(), GetAnonyString(taskParam.dhId).c_str());
136             auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr);
137             TaskExecutor::GetInstance().PushTask(task);
138         }
139     }
140     auto monitorTaskTimer = [this, eventHandler] {Execute(eventHandler);};
141     eventHandler->PostTask(monitorTaskTimer, MONITOR_TASK_TIMER_ID, DELAY_TIME_MS);
142 }
143 } // namespace DistributedHardware
144 } // namespace OHOS
145