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 <thread> 20 #include <condition_variable> 21 #include <mutex> 22 #include <list> 23 #include <vector> 24 #include <map> 25 #include <memory> 26 27 #include "delayed_sp_singleton.h" 28 #include "thermal_srv_sensor_info.h" 29 #include "thermal_config_sensor_cluster.h" 30 #include "thermalsrv_event_handler.h" 31 32 namespace OHOS { 33 namespace PowerMgr { 34 struct PolicyAction { 35 std::string actionName; 36 std::string actionValue; 37 std::map<std::string, std::string> mActionProp; 38 bool isProp; 39 }; 40 41 struct PolicyConfig { 42 uint32_t level; 43 std::vector<PolicyAction> vPolicyAction; 44 }; 45 46 47 class ThermalPolicy { 48 public: 49 using PolicyConfigMap = std::map<std::string, std::vector<PolicyConfig>>; 50 ThermalPolicy(); 51 ~ThermalPolicy() = default; 52 bool Init(); 53 /* receive sensor temp related */ 54 void RegisterObserver(); 55 void GetSensorInfomation(TypeTempMap info); 56 void SetPolicyMap(PolicyConfigMap &policyConfigMap); 57 void SetSensorClusterMap(std::map<std::string, std::shared_ptr<ThermalConfigSensorCluster>> &msc); 58 void DumpConfigInfo(); 59 void LevelDecision(); 60 void PolicyDecision(std::map<std::string, uint32_t> &clusterLevelMap); 61 void ActionDecision(std::vector<PolicyAction> &vAction); 62 bool StateMachineDecision(std::map<std::string, std::string> &stateMap); 63 bool ActionFallbackDecision(); 64 bool ActionExecution(); 65 void SortLevel(); 66 void WriteLevel(); 67 void FindSubscribeActionValue(); 68 public: 69 /* Test */ 70 std::map<std::string, uint32_t> GetClusterLevelMap(); 71 private: LevelCompare(const PolicyConfig & r,const PolicyConfig & l)72 static bool LevelCompare(const PolicyConfig& r, const PolicyConfig& l) 73 { 74 return r.level < l.level; 75 } 76 struct classcomp { operatorclasscomp77 bool operator()(const std::shared_ptr<IThermalAction> &l, const std::shared_ptr<IThermalAction> &r) const 78 { 79 return l < r; 80 } 81 }; 82 private: 83 std::set<const std::shared_ptr<IThermalAction>, classcomp> actionFallbackSet_; 84 std::set<const std::shared_ptr<IThermalAction>, classcomp> preExecuteList_; 85 std::shared_ptr<ThermalsrvEventHandler> handler_; 86 std::map<std::string, std::shared_ptr<ThermalConfigSensorCluster>> msc_; 87 std::map<std::string, uint32_t> clusterLevelMap_; 88 PolicyConfigMap clusterPolicyMap_; 89 }; 90 } // namespace PowerMgr 91 } // namespace OHOS 92 #endif // THERMAL_POLICY_H 93