• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef THERMAL_CONFIG_SENSOR_CLUSTER_H
17 #define THERMAL_CONFIG_SENSOR_CLUSTER_H
18 
19 #include <deque>
20 #include <string>
21 #include <map>
22 #include <vector>
23 #include <memory>
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 enum ThermalStatus {
28     STATUS_IDEAL = 0,
29     STATUS_STABLE,
30     STATUS_CRITICAL,
31     MAX_STATUS,
32 };
33 
34 struct LevelItem {
35     int32_t threshold;
36     int32_t thresholdClr;
37     double tempRiseRate {-1000000};
38     uint32_t level;
39 };
40 
41 struct AuxLevelItem {
42     int32_t lowerTemp;
43     int32_t upperTemp;
44     int32_t level;
45 };
46 
47 struct TempDiffItem {
48     std::string sensor1;
49     std::string sensor2;
50     int32_t tempDiff;
51     int32_t level;
52 };
53 
54 using TypeTempMap = std::map<std::string, int32_t>;
55 using SensorInfoMap = std::map<std::string, std::vector<LevelItem>>;
56 using AuxSensorInfoMap = std::map<std::string, std::vector<AuxLevelItem>>;
57 using TempDiffInfoList = std::vector<TempDiffItem>;
58 
59 class ThermalConfigSensorCluster {
60 public:
61     bool CheckStandard();
62     void UpdateThermalLevel(const TypeTempMap& typeTempInfo);
63 
64     uint32_t GetCurrentLevel();
65     void SetSensorLevelInfo(SensorInfoMap& sensorInfolist);
66     void SetAuxSensorLevelInfo(AuxSensorInfoMap& auxSensorInfolist);
67     void SetTempDiffInfo(TempDiffInfoList& tempDiffInfoList);
68     void SetDescFlag(bool descflag);
69     void SetAuxFlag(bool auxflag);
70     void SetRateFlag(bool rateFlag);
71     void SetTempDiffFlag(bool tempDiffFlag);
AddState(std::string & state,std::string & val)72     void AddState(std::string& state, std::string& val)
73     {
74         stateMap_.emplace(state, val);
75     }
76 
77 private:
78     bool CheckState();
79     void CalculateSensorLevel(const TypeTempMap& typeTempInfo, std::vector<uint32_t>& levelList);
80     void AscendLevelToThreshold(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
81         const TypeTempMap& typeTempInfo, const std::string& type);
82     void DescendLevelToThresholdClr(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
83         const TypeTempMap& typeTempInfo, const std::string& type);
84     void DescendLevelToThreshold(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
85         const TypeTempMap& typeTempInfo, const std::string& type);
86     void AscendLevelToThresholdClr(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
87         const TypeTempMap& typeTempInfo, const std::string& type);
88     void LevelUpwardsSearch(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
89         const TypeTempMap& typeTempInfo, const std::string& type);
90     void LevelDownwardsSearch(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
91         const TypeTempMap& typeTempInfo, const std::string& type);
92     void LevelDownwardsSearchWithThreshold(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
93         const TypeTempMap& typeTempInfo, const std::string& type);
94     void LevelUpwardsSearchWithThreshold(std::vector<LevelItem>& levItems, uint32_t& level, const int32_t& curTemp,
95         const TypeTempMap& typeTempInfo, const std::string& type);
96     void AscJudgment(std::vector<LevelItem>& levItems, const int32_t& curTemp, uint32_t& level,
97         const TypeTempMap& typeTempInfo, const std::string& type);
98     void DescJudgment(std::vector<LevelItem>& levItems, const int32_t& curTemp, uint32_t& level,
99         const TypeTempMap& typeTempInfo, const std::string& type);
100     bool CheckExtraCondition(const TypeTempMap& typeTempInfo, uint32_t& level, const std::string& type,
101         std::vector<LevelItem>& levItems, bool isCritical);
102     bool IsTempRateTrigger(uint32_t& level, const std::string& type, std::vector<LevelItem>& levItems,
103         bool& isCritical);
104     bool IsAuxSensorTrigger(const TypeTempMap& typeTempInfo, uint32_t& level);
105     bool IsTempDiffTrigger(const TypeTempMap& typeTempInfo, uint32_t& level);
106     ThermalStatus GetRateTrend(const double targetRate, const std::deque<double>& rateList,
107         const uint32_t& maxRateCount);
108 
109     bool descFlag_ {false};
110     bool auxFlag_ {false};
111     bool rateFlag_ {false};
112     bool tempDiffFlag_ {false};
113     uint32_t latestLevel_ {0};
114     SensorInfoMap sensorInfolist_;
115     AuxSensorInfoMap auxSensorInfolist_;
116     TempDiffInfoList tempDiffInfoList_;
117     std::map<std::string, std::string> stateMap_;
118 };
119 
120 using SensorClusterPtr = std::shared_ptr<ThermalConfigSensorCluster>;
121 using SensorClusterMap = std::map<std::string, SensorClusterPtr>;
122 } // namespace PowerMgr
123 } // namesapce OHOS
124 #endif // THERMAL_CONFIG_SENSOR_CLUSTER_H
125