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 NotifyActionChanged(const sptr<IThermalActionCallback>& listener, 52 IThermalActionCallback::ActionCallbackMap& newActionCbMap); 53 void NotifySensorTempChanged(IThermalTempCallback::TempCallbackMap& tempCbMap); 54 bool CompareActionCallbackMap(const IThermalActionCallback::ActionCallbackMap& map1, 55 const IThermalActionCallback::ActionCallbackMap& map2); 56 void DecisionActionValue(const std::vector<std::string>& actionList, 57 IThermalActionCallback::ActionCallbackMap& newActionCbMap); 58 bool GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo); 59 void SetRegisterCallback(Callback& callback); 60 void SetSensorTemp(const std::string& type, const int32_t& temp); 61 void SetDecisionValue(const std::string& actionName, const std::string& actionValue); 62 /** 63 * Get related function 64 */ 65 int32_t GetTemp(const SensorType& type); GetSensorType()66 std::map<SensorType, std::string> GetSensorType() 67 { 68 return typeMap_; 69 } 70 class SensorTempCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 71 public: 72 SensorTempCallbackDeathRecipient() = default; 73 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 74 virtual ~SensorTempCallbackDeathRecipient() = default; 75 }; 76 77 class ActionCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 78 public: 79 ActionCallbackDeathRecipient() = default; 80 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 81 virtual ~ActionCallbackDeathRecipient() = default; 82 }; 83 private: 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 const wptr<ThermalService> tms_; 100 std::mutex mutexActionCallback_; 101 std::mutex mutexTempCallback_; 102 std::mutex mutexActionMap_; 103 std::mutex mutexCallbackInfo_; 104 TypeTempMap callbackinfo_; 105 sptr<IRemoteObject::DeathRecipient> sensorTempCBDeathRecipient_; 106 sptr<IRemoteObject::DeathRecipient> actionCBDeathRecipient_; 107 std::set<const sptr<IThermalTempCallback>, classcomp> sensorTempListeners_; 108 std::map<const sptr<IThermalTempCallback>, std::vector<std::string>> callbackTypeMap_; 109 std::set<const sptr<IThermalActionCallback>, ClassActionComp> actionListeners_; 110 std::map<const sptr<IThermalActionCallback>, std::vector<std::string>> callbackActionMap_; 111 Callback callback_; 112 std::map<SensorType, std::string> typeMap_; 113 }; 114 } // namespace PowerMgr 115 } // namespace OHOS 116 117 #endif // THERMAL_OBSERVER_H 118