1 /*
2  * Copyright (c) 2021 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 "protector_thermal_zone_info.h"
17 
18 #include "thermal_log.h"
19 
20 namespace OHOS {
21 namespace PowerMgr {
UpdateThermalLevel(int32_t curTemp)22 void ProtectorThermalZoneInfo::UpdateThermalLevel(int32_t curTemp)
23 {
24     THERMAL_HILOGD(FEATURE_PROTECTOR, "Enter");
25     uint32_t level = 0;
26     if (desc_) {
27         DescJudgment(curTemp, level);
28     } else {
29         AscJudgment(curTemp, level);
30     }
31     latestLevel_ = level;
32 }
33 
AscJudgment(int32_t curTemp,uint32_t & level)34 void ProtectorThermalZoneInfo::AscJudgment(int32_t curTemp, uint32_t &level)
35 {
36     THERMAL_HILOGD(FEATURE_PROTECTOR, "curTemp:%{public}d", curTemp);
37     level = latestLevel_;
38     if (level > 0 && level < tzItemList_.size()) {
39         int32_t curDownTemp = tzItemList_.at(level - 1).thresholdClr;
40         int32_t nextUptemp = tzItemList_.at(level).threshold;
41         if (curTemp >= nextUptemp) {
42             HandleAscNextUpTemp(level, curTemp);
43         } else if (curTemp < curDownTemp) {
44             HandleAscCurDownTemp(level, curTemp);
45         } else {
46             level = tzItemList_.at(level - 1).level;
47             THERMAL_HILOGI(FEATURE_PROTECTOR, "third level = %{public}d", level);
48         }
49     } else if (level == tzItemList_.size()) {
50         HandleAscMaxSizeTemp(level, curTemp);
51     } else if (level == 0)  {
52         HandleAscMinSizeTemp(level, curTemp);
53     }
54 }
55 
DescJudgment(int32_t curTemp,uint32_t & level)56 void ProtectorThermalZoneInfo::DescJudgment(int32_t curTemp, uint32_t &level)
57 {
58     THERMAL_HILOGD(FEATURE_PROTECTOR, "curTemp:%{public}d", curTemp);
59     level = latestLevel_;
60     if (level > 0 && level < tzItemList_.size()) {
61         int32_t curDownTemp = tzItemList_.at(level - 1).thresholdClr;
62         int32_t nextUptemp = tzItemList_.at(level).threshold;
63         if (curTemp <= nextUptemp) {
64             HandleDescNextUpTemp(level, curTemp);
65         } else if (curTemp > curDownTemp) {
66             HandleDescCurDownTemp(level, curTemp);
67         } else {
68             level = tzItemList_.at(level - 1).level;
69             THERMAL_HILOGI(FEATURE_PROTECTOR, "third level = %{public}d", level);
70         }
71     } else if (level == tzItemList_.size()) {
72         HandleDescMaxSizeTemp(level, curTemp);
73     } else if (level == 0) {
74         HandleDescMinSizeTemp(level, curTemp);
75     }
76     THERMAL_HILOGD(FEATURE_PROTECTOR, "Exit");
77 }
78 
HandleAscNextUpTemp(uint32_t & level,int32_t curTemp)79 void ProtectorThermalZoneInfo::HandleAscNextUpTemp(uint32_t &level, int32_t curTemp)
80 {
81     for (uint32_t i = level; i < tzItemList_.size(); i++) {
82         if (curTemp >= tzItemList_.at(i).threshold) {
83             level = tzItemList_.at(i).level;
84         } else {
85             break;
86         }
87     }
88     THERMAL_HILOGI(FEATURE_PROTECTOR, "first level = %{public}d", level);
89 }
90 
HandleAscCurDownTemp(uint32_t & level,int32_t curTemp)91 void ProtectorThermalZoneInfo::HandleAscCurDownTemp(uint32_t &level, int32_t curTemp)
92 {
93     for (uint32_t i = level; i >= 1; i--) {
94         if (curTemp < tzItemList_.at(i - 1).thresholdClr) {
95             level = (tzItemList_.at(i - 1).level > 0) ? (tzItemList_.at(i - 1).level - 1) : 0;
96         } else {
97             break;
98         }
99     }
100     THERMAL_HILOGI(FEATURE_PROTECTOR, "second level = %{public}d", level);
101 }
102 
HandleAscMaxSizeTemp(uint32_t & level,int32_t curTemp)103 void ProtectorThermalZoneInfo::HandleAscMaxSizeTemp(uint32_t &level, int32_t curTemp)
104 {
105     int32_t curDownTemp = tzItemList_.at(level - 1).thresholdClr;
106     if (curTemp < curDownTemp) {
107         for (uint32_t i = level; i >= 1; i--) {
108             if (curTemp < tzItemList_.at(i - 1).thresholdClr) {
109                 level = (tzItemList_.at(i - 1).level > 0) ? (tzItemList_.at(i - 1).level - 1) : 0;
110             } else {
111                 break;
112             }
113         }
114     }
115     THERMAL_HILOGI(FEATURE_PROTECTOR, "fourth level = %{public}d", level);
116 }
117 
HandleAscMinSizeTemp(uint32_t & level,int32_t curTemp)118 void ProtectorThermalZoneInfo::HandleAscMinSizeTemp(uint32_t &level, int32_t curTemp)
119 {
120     int32_t nextUptemp = tzItemList_.at(level).threshold;
121     if (curTemp >= nextUptemp) {
122         for (uint32_t i = level; i < tzItemList_.size(); i++) {
123             if (curTemp >= tzItemList_.at(i).threshold) {
124                 level = tzItemList_.at(i).level;
125             } else {
126                 break;
127             }
128         }
129     } else {
130         level = 0;
131     }
132     THERMAL_HILOGI(FEATURE_PROTECTOR, "fifth level = %{public}d", level);
133 }
134 
HandleDescNextUpTemp(uint32_t & level,int32_t curTemp)135 void ProtectorThermalZoneInfo::HandleDescNextUpTemp(uint32_t &level, int32_t curTemp)
136 {
137     for (uint32_t i = level; i < tzItemList_.size(); i++) {
138         if (curTemp <= tzItemList_.at(i).threshold) {
139             level = tzItemList_.at(i).level;
140         } else {
141             break;
142         }
143     }
144     THERMAL_HILOGI(FEATURE_PROTECTOR, "first level = %{public}d", level);
145 }
146 
HandleDescCurDownTemp(uint32_t & level,int32_t curTemp)147 void ProtectorThermalZoneInfo::HandleDescCurDownTemp(uint32_t &level, int32_t curTemp)
148 {
149     for (uint32_t i = level; i >= 1; i--) {
150         if (curTemp > tzItemList_.at(i - 1).thresholdClr) {
151             level = (tzItemList_.at(i - 1).level > 0) ? (tzItemList_.at(i - 1).level - 1) : 0;
152         } else {
153             break;
154         }
155     }
156     THERMAL_HILOGI(FEATURE_PROTECTOR, "second level = %{public}d", level);
157 }
158 
HandleDescMaxSizeTemp(uint32_t & level,int32_t curTemp)159 void ProtectorThermalZoneInfo::HandleDescMaxSizeTemp(uint32_t &level, int32_t curTemp)
160 {
161     int32_t curDownTemp = tzItemList_.at(level - 1).thresholdClr;
162     if (curTemp > curDownTemp) {
163         for (uint32_t i = level; i >= 1; i--) {
164             if (curTemp > tzItemList_.at(i - 1).thresholdClr) {
165                 level = (tzItemList_.at(i - 1).level > 0) ? (tzItemList_.at(i - 1).level - 1) : 0;
166             } else {
167                 break;
168             }
169         }
170     }
171     THERMAL_HILOGI(FEATURE_PROTECTOR, "fourth level = %{public}d", level);
172 }
173 
HandleDescMinSizeTemp(uint32_t & level,int32_t curTemp)174 void ProtectorThermalZoneInfo::HandleDescMinSizeTemp(uint32_t &level, int32_t curTemp)
175 {
176     int32_t nextUptemp = tzItemList_.at(level).threshold;
177     if (curTemp <= nextUptemp) {
178         for (uint32_t i = level; i < tzItemList_.size(); i++) {
179             if (curTemp <= tzItemList_.at(i).threshold) {
180                 level = tzItemList_.at(i).level;
181             } else {
182                 break;
183             }
184         }
185     } else {
186         level = 0;
187     }
188     THERMAL_HILOGI(FEATURE_PROTECTOR, "fifth level = %{public}d", level);
189 }
190 
SetThermalZoneItem(std::vector<ThermalZoneInfoItem> & tzItemList)191 void ProtectorThermalZoneInfo::SetThermalZoneItem(std::vector<ThermalZoneInfoItem> &tzItemList)
192 {
193     tzItemList_ = tzItemList;
194 }
195 
SetDesc(bool desc)196 void ProtectorThermalZoneInfo::SetDesc(bool desc)
197 {
198     desc_ = desc;
199 }
200 
SetInterval(int32_t interval)201 void ProtectorThermalZoneInfo::SetInterval(int32_t interval)
202 {
203     interval_ = interval;
204 }
205 
GetInterval() const206 int32_t ProtectorThermalZoneInfo::GetInterval() const
207 {
208     return interval_;
209 }
210 
SetMultiple(int32_t multiple)211 void ProtectorThermalZoneInfo::SetMultiple(int32_t multiple)
212 {
213     multiple_ = multiple;
214 }
215 
GetMultiple() const216 int32_t ProtectorThermalZoneInfo::GetMultiple() const
217 {
218     return multiple_;
219 }
220 
GetPath() const221 std::string ProtectorThermalZoneInfo::GetPath() const
222 {
223     return path_;
224 }
225 
SetPath(const std::string & path)226 void ProtectorThermalZoneInfo::SetPath(const std::string &path)
227 {
228     path_ = path;
229 }
230 
Dump()231 void ProtectorThermalZoneInfo::Dump()
232 {
233     THERMAL_HILOGD(FEATURE_PROTECTOR, "interval:%{public}d, desc:%{public}d, multiple:%{public}d",
234         interval_, desc_, multiple_);
235     for (auto infoIter : tzItemList_) {
236         THERMAL_HILOGI(FEATURE_PROTECTOR,
237             "level: %{public}d, threshold:%{public}d, thresholdClr:%{public}d",
238             infoIter.level, infoIter.threshold, infoIter.thresholdClr);
239     }
240 }
241 } // namespace PowerMgr
242 } // namespace OHOS
243