• 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 "thermal_hdf_timer.h"
17 #include <cerrno>
18 #include <thread>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <hdf_base.h>
22 #include <sys/socket.h>
23 #include <sys/epoll.h>
24 #include <sys/timerfd.h>
25 #include <linux/netlink.h>
26 #include "thermal_log.h"
27 #include "thermal_dfx.h"
28 
29 namespace OHOS {
30 namespace HDI {
31 namespace Thermal {
32 namespace V1_0 {
33 namespace {
34 const int32_t MS_PER_SECOND = 1000;
35 const std::string THERMAL_SIMULATION_TAG = "sim_tz";
36 }
ThermalHdfTimer(const std::shared_ptr<ThermalSimulationNode> & node,const std::shared_ptr<ThermalZoneManager> & thermalZoneMgr)37 ThermalHdfTimer::ThermalHdfTimer(const std::shared_ptr<ThermalSimulationNode> &node,
38     const std::shared_ptr<ThermalZoneManager> &thermalZoneMgr)
39 {
40     node_ = node;
41     thermalZoneMgr_ = thermalZoneMgr;
42     reportTime_ = 0;
43 }
44 
~ThermalHdfTimer()45 ThermalHdfTimer::~ThermalHdfTimer()
46 {
47     isRunning_ = false;
48     if (callbackThread_->joinable()) {
49         callbackThread_->join();
50     }
51 }
52 
SetThermalEventCb(const sptr<IThermalCallback> & thermalCb)53 void ThermalHdfTimer::SetThermalEventCb(const sptr<IThermalCallback> &thermalCb)
54 {
55     thermalCb_ = thermalCb;
56 }
57 
SetSimluationFlag()58 void ThermalHdfTimer::SetSimluationFlag()
59 {
60     auto baseConfigList = ThermalHdfConfig::GetInsance().GetBaseConfig()->GetBaseItem();
61     if (baseConfigList.empty()) {
62         THERMAL_HILOGE(COMP_HDI, "baseConfigList is empty");
63         return;
64     }
65     auto baseIter = std::find(baseConfigList.begin(), baseConfigList.end(), THERMAL_SIMULATION_TAG);
66     if (baseIter != baseConfigList.end()) {
67         isSim_ = atoi(baseIter->value.c_str());
68         THERMAL_HILOGI(COMP_HDI, "isSim value:%{public}d", isSim_);
69     } else {
70         THERMAL_HILOGI(COMP_HDI, "not found");
71     }
72 }
73 
SetSimFlag(int32_t flag)74 void ThermalHdfTimer::SetSimFlag(int32_t flag)
75 {
76     isSim_ = flag;
77 }
78 
GetSimluationFlag()79 int32_t ThermalHdfTimer::GetSimluationFlag()
80 {
81     return isSim_;
82 }
83 
TimerProviderCallback()84 void ThermalHdfTimer::TimerProviderCallback()
85 {
86     reportTime_ = reportTime_ + 1;
87     ReportThermalData();
88     ResetCount();
89     return;
90 }
91 
LoopingThreadEntry()92 void ThermalHdfTimer::LoopingThreadEntry()
93 {
94     while (isRunning_) {
95         std::this_thread::sleep_for(std::chrono::seconds(thermalZoneMgr_->maxCd_ / MS_PER_SECOND));
96         TimerProviderCallback();
97     }
98 }
99 
Run()100 void ThermalHdfTimer::Run()
101 {
102     callbackThread_ = std::make_unique<std::thread>(&ThermalHdfTimer::LoopingThreadEntry, this);
103 }
104 
StartThread()105 void ThermalHdfTimer::StartThread()
106 {
107     Run();
108 }
109 
Init()110 int32_t ThermalHdfTimer::Init()
111 {
112     std::unique_ptr<ThermalDfx> thermalDfx = std::make_unique<ThermalDfx>();
113     if (thermalDfx != nullptr) {
114         thermalDfx->Init();
115     }
116     StartThread();
117     return HDF_SUCCESS;
118 }
119 
ReportThermalData()120 void ThermalHdfTimer::ReportThermalData()
121 {
122     if (thermalCb_ == nullptr) {
123         THERMAL_HILOGE(COMP_HDI, "check thermalCb_ failed");
124         return;
125     }
126 
127     thermalZoneMgr_->ReportThermalZoneData(reportTime_, multipleList_);
128     tzInfoEvent_ = thermalZoneMgr_->GetCallbackInfo();
129     // callback thermal event
130     thermalCb_->OnThermalDataEvent(tzInfoEvent_);
131 }
132 
ResetCount()133 void ThermalHdfTimer::ResetCount()
134 {
135     THERMAL_HILOGI(COMP_HDI, "multipleList_:%{public}zu", multipleList_.size());
136     if (multipleList_.empty()) return;
137 
138     int32_t maxValue = *(std::max_element(multipleList_.begin(), multipleList_.end()));
139     if (reportTime_ == maxValue) {
140         THERMAL_HILOGI(COMP_HDI, "reportTime:%{public}d", reportTime_);
141         reportTime_ = 0;
142     }
143     tzInfoEvent_.info.clear();
144 }
145 
DumpSensorConfigInfo()146 void ThermalHdfTimer::DumpSensorConfigInfo()
147 {
148     auto sensorTypeMap = ThermalHdfConfig::GetInsance().GetSensorTypeMap();
149     for (auto sensorIter : sensorTypeMap) {
150         THERMAL_HILOGI(COMP_HDI, "groupName %{public}s, interval %{public}d, multiple %{public}d",
151             sensorIter.first.c_str(), sensorIter.second->GetInterval(), sensorIter.second->multiple_);
152         for (auto tzIter : sensorIter.second->GetXMLThermalZoneInfo()) {
153             THERMAL_HILOGI(COMP_HDI, "type %{public}s, replace %{public}s", tzIter.type.c_str(),
154                 tzIter.replace.c_str());
155         }
156         for (auto tnIter : sensorIter.second->GetXMLThermalNodeInfo()) {
157             THERMAL_HILOGI(COMP_HDI, "type %{public}s", tnIter.type.c_str());
158         }
159         for (auto dataIter : sensorIter.second->thermalDataList_) {
160             THERMAL_HILOGI(COMP_HDI, "data type %{public}s", dataIter.type.c_str());
161         }
162     }
163 }
164 } // V1_0
165 } // Thermal
166 } // HDI
167 } // OHOS
168