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_POLICY_H 17 #define THERMAL_POLICY_H 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 23 #include "delayed_sp_singleton.h" 24 #include "thermal_srv_sensor_info.h" 25 #include "thermal_config_sensor_cluster.h" 26 27 namespace OHOS { 28 namespace PowerMgr { 29 struct PolicyAction { 30 std::string actionName; 31 std::string actionValue; 32 std::map<std::string, std::string> actionPropMap; 33 bool isProp; 34 }; 35 36 struct PolicyConfig { 37 uint32_t level; 38 std::vector<PolicyAction> policyActionList; 39 }; 40 41 class ThermalPolicy { 42 public: 43 using PolicyConfigMap = std::map<std::string, std::vector<PolicyConfig>>; 44 ThermalPolicy() = default; 45 ~ThermalPolicy() = default; 46 47 bool Init(); 48 void OnSensorInfoReported(const TypeTempMap& info); 49 void ExecutePolicy(); 50 void SetPolicyMap(PolicyConfigMap& pcm); 51 void SetSensorClusterMap(SensorClusterMap& scm); 52 void FindSubscribeActionValue(); 53 /* Test */ 54 std::map<std::string, uint32_t> GetClusterLevelMap(); 55 void DumpLevel(std::string& result); 56 void DumpPolicy(std::string& result); 57 58 private: 59 void SortLevel(); 60 void RegisterObserver(); 61 void WriteLevel(); 62 void LevelDecision(); 63 void PolicyDecision(); 64 void ActionDecision(const std::vector<PolicyAction>& actionList); 65 bool StateMachineDecision(const std::map<std::string, std::string>& stateMap); 66 bool ActionExecution(); 67 void PrintPolicyState(); 68 void PrintPolicyAction(std::vector<PolicyAction> policyActionList, std::string& result); 69 LevelCompare(const PolicyConfig & r,const PolicyConfig & l)70 static bool LevelCompare(const PolicyConfig& r, const PolicyConfig& l) 71 { 72 return r.level < l.level; 73 } 74 75 TypeTempMap typeTempMap_; 76 SensorClusterMap sensorClusterMap_; 77 std::map<std::string, uint32_t> clusterLevelMap_; 78 PolicyConfigMap clusterPolicyMap_; 79 }; 80 } // namespace PowerMgr 81 } // namespace OHOS 82 #endif // THERMAL_POLICY_H 83