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_OBSERVER_H 17 #define THERMAL_OBSERVER_H 18 19 #include <string> 20 #include <vector> 21 #include <map> 22 #include <functional> 23 #include <set> 24 #include "delayed_sp_singleton.h" 25 26 #include "ithermal_action_callback.h" 27 #include "ithermal_temp_callback.h" 28 #include "thermal_srv_sensor_info.h" 29 30 namespace OHOS { 31 namespace PowerMgr { 32 using TypeTempMap = std::map<std::string, int32_t>; 33 34 class ThermalService; 35 class ThermalObserver { 36 public: 37 using Callback = std::function<void(const TypeTempMap&)>; ThermalObserver()38 ThermalObserver() {}; 39 explicit ThermalObserver(const wptr<ThermalService>& tms); 40 ~ThermalObserver(); 41 42 bool Init(); 43 void OnReceivedSensorInfo(const TypeTempMap& info); 44 void SubscribeThermalTempCallback(const std::vector<std::string>& typeList, 45 const sptr<IThermalTempCallback>& callback); 46 void UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback); 47 void SubscribeThermalActionCallback(const std::vector<std::string>& actionList, 48 const std::string& desc, const sptr<IThermalActionCallback>& callback); 49 void UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback); 50 void FindSubscribeActionValue(); 51 void NotifySensorTempChanged(IThermalTempCallback::TempCallbackMap& tempCbMap); 52 void DecisionActionValue(const std::vector<std::string>& actionList, 53 IThermalActionCallback::ActionCallbackMap& newActionCbMap); 54 bool GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo); 55 void SetRegisterCallback(Callback& callback); 56 void SetSensorTemp(const std::string& type, const int32_t& temp); 57 void SetDecisionValue(const std::string& actionName, const std::string& actionValue); 58 UpdatePolicyState(std::string & state)59 void UpdatePolicyState(std::string& state) 60 { 61 policyState_ = state; 62 } 63 int32_t GetTemp(const SensorType& type); GetSensorType()64 std::map<SensorType, std::string> GetSensorType() 65 { 66 return typeMap_; 67 } 68 class SensorTempCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 69 public: 70 SensorTempCallbackDeathRecipient() = default; 71 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 72 virtual ~SensorTempCallbackDeathRecipient() = default; 73 }; 74 75 class ActionCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 76 public: 77 ActionCallbackDeathRecipient() = default; 78 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 79 virtual ~ActionCallbackDeathRecipient() = default; 80 }; 81 82 private: 83 void PrintAction(); 84 void InitSensorTypeMap(); 85 struct classcomp { operatorclasscomp86 bool operator() (const sptr<IThermalTempCallback>& l, const sptr<IThermalTempCallback>& r) const 87 { 88 return l->AsObject() < r->AsObject(); 89 } 90 }; 91 92 struct ClassActionComp { operatorClassActionComp93 bool operator() (const sptr<IThermalActionCallback>& l, const sptr<IThermalActionCallback>& r) const 94 { 95 return l->AsObject() < r->AsObject(); 96 } 97 }; 98 99 std::string policyState_; 100 const wptr<ThermalService> tms_; 101 std::mutex mutexActionCallback_; 102 std::mutex mutexTempCallback_; 103 std::mutex mutexActionMap_; 104 std::mutex mutexCallbackInfo_; 105 TypeTempMap callbackinfo_; 106 sptr<IRemoteObject::DeathRecipient> sensorTempCBDeathRecipient_; 107 sptr<IRemoteObject::DeathRecipient> actionCBDeathRecipient_; 108 std::set<const sptr<IThermalTempCallback>, classcomp> sensorTempListeners_; 109 std::map<const sptr<IThermalTempCallback>, std::vector<std::string>, classcomp> callbackTypeMap_; 110 std::set<const sptr<IThermalActionCallback>, ClassActionComp> actionListeners_; 111 std::map<const sptr<IThermalActionCallback>, std::vector<std::string>, ClassActionComp> callbackActionMap_; 112 Callback callback_; 113 std::map<SensorType, std::string> typeMap_; 114 }; 115 } // namespace PowerMgr 116 } // namespace OHOS 117 118 #endif // THERMAL_OBSERVER_H 119