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 }
35 static sptr<IThermalCallback> theramalCb_ = nullptr;
36 static std::shared_ptr<HdfThermalCallbackInfo> callbackInfo_ = nullptr;
37 static std::shared_ptr<ThermalHdfTimer> hdfTimer_ = nullptr;
38 static std::shared_ptr<ThermalSimulationNode> simulation_ = nullptr;
39 static std::shared_ptr<ThermalDeviceMitigation> mitigation_ = nullptr;
40 static std::shared_ptr<ThermalZoneManager> thermalZoneMgr_ = nullptr;
41
ThermalInterfaceImpl()42 ThermalInterfaceImpl::ThermalInterfaceImpl()
43 {
44 Init();
45 }
46
Init()47 int32_t ThermalInterfaceImpl::Init()
48 {
49 int32_t ret = ThermalHdfConfig::GetInsance().ThermalHDIConfigInit(HDI_XML_NAME);
50 if (ret != HDF_SUCCESS) {
51 THERMAL_HILOGE(COMP_HDI, "failed to init XML, ret: %{public}d", ret);
52 return HDF_FAILURE;
53 }
54
55 if (simulation_ == nullptr) {
56 simulation_ = std::make_shared<ThermalSimulationNode>();
57 }
58
59 if (thermalZoneMgr_ == nullptr) {
60 thermalZoneMgr_ = std::make_shared<ThermalZoneManager>();
61 }
62
63 if (mitigation_ == nullptr) {
64 mitigation_ = std::make_shared<ThermalDeviceMitigation>();
65 }
66
67 if (hdfTimer_ == nullptr) {
68 hdfTimer_ = std::make_shared<ThermalHdfTimer>(simulation_, thermalZoneMgr_);
69 hdfTimer_->SetSimluationFlag();
70 }
71
72 if (hdfTimer_->GetSimluationFlag()) {
73 ret = simulation_->NodeInit();
74 if (ret != HDF_SUCCESS) {
75 return HDF_FAILURE;
76 }
77 }
78
79 thermalZoneMgr_->CalculateMaxCd();
80 thermalZoneMgr_->SetMultiples();
81 ret = thermalZoneMgr_->ParseThermalZoneInfo();
82 if (ret != HDF_SUCCESS) {
83 return ret;
84 }
85
86 hdfTimer_->DumpSensorConfigInfo();
87 mitigation_->SetFlag(static_cast<bool>(hdfTimer_->GetSimluationFlag()));
88 return HDF_SUCCESS;
89 }
90
SetCpuFreq(int32_t freq)91 int32_t ThermalInterfaceImpl::SetCpuFreq(int32_t freq)
92 {
93 if (mitigation_ != nullptr) {
94 int32_t ret = mitigation_->CpuRequest(freq);
95 if (ret != HDF_SUCCESS) {
96 THERMAL_HILOGE(COMP_HDI, "failed to set freq %{public}d", ret);
97 return ret;
98 }
99 }
100 return HDF_SUCCESS;
101 }
102
SetGpuFreq(int32_t freq)103 int32_t ThermalInterfaceImpl::SetGpuFreq(int32_t freq)
104 {
105 if (mitigation_ != nullptr) {
106 int32_t ret = mitigation_->GpuRequest(freq);
107 if (ret != HDF_SUCCESS) {
108 THERMAL_HILOGE(COMP_HDI, "failed to set freq %{public}d", ret);
109 return ret;
110 }
111 }
112 return HDF_SUCCESS;
113 }
114
SetBatteryCurrent(int32_t current)115 int32_t ThermalInterfaceImpl::SetBatteryCurrent(int32_t current)
116 {
117 if (mitigation_ != nullptr) {
118 int32_t ret = mitigation_->ChargerRequest(current);
119 if (ret != HDF_SUCCESS) {
120 THERMAL_HILOGE(COMP_HDI, "failed to set current %{public}d", ret);
121 return ret;
122 }
123 }
124 return HDF_SUCCESS;
125 }
126
GetThermalZoneInfo(HdfThermalCallbackInfo & event)127 int32_t ThermalInterfaceImpl::GetThermalZoneInfo(HdfThermalCallbackInfo& event)
128 {
129 if (thermalZoneMgr_ != nullptr) {
130 event.info = thermalZoneMgr_->tzInfoAcaualEvent_.info;
131 }
132 return HDF_SUCCESS;
133 }
134
Register(const sptr<IThermalCallback> & callbackObj)135 int32_t ThermalInterfaceImpl::Register(const sptr<IThermalCallback>& callbackObj)
136 {
137 int32_t ret;
138 theramalCb_ = callbackObj;
139 if (hdfTimer_ == nullptr) return HDF_FAILURE;
140 hdfTimer_->SetThermalEventCb(theramalCb_);
141
142 ret = hdfTimer_->Init();
143 if (ret != HDF_SUCCESS) {
144 return ret;
145 }
146 return HDF_SUCCESS;
147 }
148
Unregister()149 int32_t ThermalInterfaceImpl::Unregister()
150 {
151 theramalCb_ = nullptr;
152 return HDF_SUCCESS;
153 }
154 } // V1_0
155 } // Thermal
156 } // HDI
157 } // OHOS