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