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