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_KERNEL_POLICY_H
17 #define THERMAL_KERNEL_POLICY_H
18 
19 #include <map>
20 #include "thermal_kernel_config_file.h"
21 #include "protector_thermal_zone_info.h"
22 #include "ithermal_action.h"
23 
24 namespace OHOS {
25 namespace PowerMgr {
26 struct ActionItem {
27     std::string name;
28     uint32_t value;
29 };
30 
31 struct LevelAction {
32     std::string name;
33     uint32_t level;
34     std::vector<ActionItem> vAction;
35 };
36 
37 class ThermalKernelPolicy {
38 public:
39     using ThermalZoneMap = std::map<std::string, std::shared_ptr<ProtectorThermalZoneInfo>>;
40     using LevelMap = std::map<std::string, uint32_t>;
41     using TypeLevelsMap = std::map<std::string, std::vector<uint32_t>>;
ThermalKernelPolicy()42     ThermalKernelPolicy() {};
~ThermalKernelPolicy()43     ~ThermalKernelPolicy() {};
44 
45     bool Init();
46     void SetThermalZoneMap(ThermalZoneMap &tzInfoMap);
47     ThermalZoneMap GetThermalZoneMap();
48     void SetLevelAction(std::vector<LevelAction> &levelAction);
49     std::vector<LevelAction> GetLevelAction();
50     int32_t GetMaxCd();
51 private:
52     void LevelDecision();
53     void PolicyDecision();
54     void ActionDecision(std::vector<ActionItem> &vAction);
55     void ActionExecution();
56     void ActionFallbackDecision();
57     void SetCallback();
58     void OnReceivedSensorsInfo(SensorsMap typeTempMap);
59     void Dump();
60     /* Gets the maximum common divisor of the time interval */
61     int32_t GetMaxCommonDivisor(int32_t a, int32_t b);
62     int32_t GetIntervalCommonDivisor(std::vector<int32_t> intervalList);
63     void SetMultiples();
64     void CalculateMaxCd();
65     TypeLevelsMap typeLevelsMap_;
66     SensorsMap typeTempMap_;
67     ThermalZoneMap tzInfoMap_;
68     LevelMap levelMap_;
69     std::vector<LevelAction> levelAction_;
70     std::vector<std::string> executeActionList_;
71     int32_t maxCd_ {0};
72 };
73 } // namespace PowerMgr
74 } // namespace OHOS
75 #endif