• 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_interface_impl.h"
17 
18 #include <thread>
19 #include <memory>
20 #include <hdf_base.h>
21 #include "thermal_hdf_config.h"
22 #include "thermal_hdf_timer.h"
23 #include "thermal_simulation_node.h"
24 #include "thermal_device_mitigation.h"
25 #include "thermal_zone_manager.h"
26 #include "thermal_log.h"
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace Thermal {
31 namespace V1_0 {
32 namespace {
33 const std::string HDI_XML_NAME = HDF_ETC_DIR "/thermal_config/hdf/thermal_hdi_config.xml";
34 bool g_isHdiStart = false;
35 }
36 static sptr<IThermalCallback> theramalCb_ = nullptr;
37 static std::shared_ptr<HdfThermalCallbackInfo> callbackInfo_ = nullptr;
38 static std::shared_ptr<ThermalHdfTimer> hdfTimer_ = nullptr;
39 static std::shared_ptr<ThermalSimulationNode> simulation_ = nullptr;
40 static std::shared_ptr<ThermalDeviceMitigation> mitigation_ = nullptr;
41 static std::shared_ptr<ThermalZoneManager> thermalZoneMgr_ = nullptr;
42 
ThermalInterfaceImplGetInstance(void)43 extern "C" IThermalInterface *ThermalInterfaceImplGetInstance(void)
44 {
45     return new (std::nothrow) ThermalInterfaceImpl();
46 }
47 
ThermalInterfaceImpl()48 ThermalInterfaceImpl::ThermalInterfaceImpl()
49 {
50     Init();
51 }
52 
Init()53 int32_t ThermalInterfaceImpl::Init()
54 {
55     int32_t ret = ThermalHdfConfig::GetInsance().ThermalHDIConfigInit(HDI_XML_NAME);
56     if (ret != HDF_SUCCESS) {
57         THERMAL_HILOGE(COMP_HDI, "failed to init XML, ret: %{public}d", ret);
58         return HDF_FAILURE;
59     }
60 
61     if (simulation_ == nullptr) {
62         simulation_ = std::make_shared<ThermalSimulationNode>();
63     }
64 
65     if (thermalZoneMgr_ == nullptr) {
66         thermalZoneMgr_ = std::make_shared<ThermalZoneManager>();
67     }
68 
69     if (mitigation_ == nullptr) {
70         mitigation_ = std::make_shared<ThermalDeviceMitigation>();
71     }
72 
73     if (hdfTimer_ == nullptr) {
74         hdfTimer_ = std::make_shared<ThermalHdfTimer>(simulation_, thermalZoneMgr_);
75         hdfTimer_->SetSimluationFlag();
76     }
77 
78     if (hdfTimer_->GetSimluationFlag()) {
79         ret = simulation_->NodeInit();
80         if (ret != HDF_SUCCESS) {
81             return HDF_FAILURE;
82         }
83     }
84 
85     thermalZoneMgr_->CalculateMaxCd();
86     thermalZoneMgr_->SetMultiples();
87     ret = thermalZoneMgr_->ParseThermalZoneInfo();
88     if (ret != HDF_SUCCESS) {
89         return ret;
90     }
91 
92     hdfTimer_->DumpSensorConfigInfo();
93     mitigation_->SetFlag(static_cast<bool>(hdfTimer_->GetSimluationFlag()));
94     return HDF_SUCCESS;
95 }
96 
SetCpuFreq(int32_t freq)97 int32_t ThermalInterfaceImpl::SetCpuFreq(int32_t freq)
98 {
99     if (mitigation_ != nullptr) {
100         int32_t ret = mitigation_->CpuRequest(freq);
101         if (ret != HDF_SUCCESS) {
102             THERMAL_HILOGE(COMP_HDI, "failed to set freq %{public}d", ret);
103             return ret;
104         }
105     }
106     return HDF_SUCCESS;
107 }
108 
SetGpuFreq(int32_t freq)109 int32_t ThermalInterfaceImpl::SetGpuFreq(int32_t freq)
110 {
111     if (mitigation_ != nullptr) {
112         int32_t ret = mitigation_->GpuRequest(freq);
113         if (ret != HDF_SUCCESS) {
114             THERMAL_HILOGE(COMP_HDI, "failed to set freq %{public}d", ret);
115             return ret;
116         }
117     }
118     return HDF_SUCCESS;
119 }
120 
SetBatteryCurrent(int32_t current)121 int32_t ThermalInterfaceImpl::SetBatteryCurrent(int32_t current)
122 {
123     if (mitigation_ != nullptr) {
124         int32_t ret = mitigation_->ChargerRequest(current);
125         if (ret != HDF_SUCCESS) {
126             THERMAL_HILOGE(COMP_HDI, "failed to set current %{public}d", ret);
127             return ret;
128         }
129     }
130     return HDF_SUCCESS;
131 }
132 
GetThermalZoneInfo(HdfThermalCallbackInfo & event)133 int32_t ThermalInterfaceImpl::GetThermalZoneInfo(HdfThermalCallbackInfo& event)
134 {
135     if (thermalZoneMgr_ != nullptr) {
136         thermalZoneMgr_->ParseThermalZoneInfo();
137         event.info = thermalZoneMgr_->GetCallbackInfo().info;
138     }
139     return HDF_SUCCESS;
140 }
141 
Register(const sptr<IThermalCallback> & callbackObj)142 int32_t ThermalInterfaceImpl::Register(const sptr<IThermalCallback>& callbackObj)
143 {
144     int32_t ret;
145     theramalCb_ = callbackObj;
146     if (hdfTimer_ == nullptr) return HDF_FAILURE;
147     hdfTimer_->SetThermalEventCb(theramalCb_);
148 
149     if (!g_isHdiStart) {
150         ret = hdfTimer_->Init();
151         if (ret != HDF_SUCCESS) {
152             return ret;
153         }
154         g_isHdiStart = true;
155     }
156     return HDF_SUCCESS;
157 }
158 
Unregister()159 int32_t ThermalInterfaceImpl::Unregister()
160 {
161     theramalCb_ = nullptr;
162     return HDF_SUCCESS;
163 }
164 } // V1_0
165 } // Thermal
166 } // HDI
167 } // OHOS
168