• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "thermal_hdf_timer.h"
17 
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <linux/netlink.h>
21 #include <sys/epoll.h>
22 #include <sys/socket.h>
23 #include <sys/timerfd.h>
24 #include <thread>
25 #include <unistd.h>
26 
27 #include "hdf_base.h"
28 #include "string_ex.h"
29 #include "thermal_dfx.h"
30 #include "thermal_log.h"
31 
32 namespace OHOS {
33 namespace HDI {
34 namespace Thermal {
35 namespace V1_1 {
36 namespace {
37 const int32_t MS_PER_SECOND = 1000;
38 const std::string THERMAL_SIMULATION_TAG = "sim_tz";
39 }
ThermalHdfTimer(const std::shared_ptr<ThermalSimulationNode> & node,const std::shared_ptr<ThermalZoneManager> & thermalZoneMgr)40 ThermalHdfTimer::ThermalHdfTimer(const std::shared_ptr<ThermalSimulationNode> &node,
41     const std::shared_ptr<ThermalZoneManager> &thermalZoneMgr)
42 {
43     node_ = node;
44     thermalZoneMgr_ = thermalZoneMgr;
45     reportTime_ = 0;
46 }
47 
~ThermalHdfTimer()48 ThermalHdfTimer::~ThermalHdfTimer()
49 {
50     isRunning_ = false;
51     if (callbackThread_ != nullptr && callbackThread_->joinable()) {
52         callbackThread_->join();
53     }
54     ThermalDfx::DestroyInstance();
55 }
56 
SetSimluationFlag()57 void ThermalHdfTimer::SetSimluationFlag()
58 {
59     auto baseConfigList = ThermalHdfConfig::GetInstance().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         StrToInt(TrimStr(baseIter->value), isSim_);
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_->GetMaxCd() / 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::GetInstance().Init();
112     StartThread();
113     return HDF_SUCCESS;
114 }
115 
ReportThermalData()116 void ThermalHdfTimer::ReportThermalData()
117 {
118     thermalZoneMgr_->ReportThermalZoneData(reportTime_);
119 }
120 
ResetCount()121 void ThermalHdfTimer::ResetCount()
122 {
123     if (reportTime_ == thermalZoneMgr_->GetMaxReportTime()) {
124         THERMAL_HILOGD(COMP_HDI, "reportTime:%{public}d", reportTime_);
125         reportTime_ = 0;
126     }
127 }
128 } // V1_1
129 } // Thermal
130 } // HDI
131 } // OHOS
132