1 /*
2  * Copyright (c) 2021-2022 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_SERVICE_H
17 #define THERMAL_SERVICE_H
18 
19 #include <atomic>
20 #include <memory>
21 #include "iremote_object.h"
22 #include "system_ability.h"
23 
24 #include "action_popup.h"
25 #include "fan_callback.h"
26 #include "fan_fault_detect.h"
27 #include "hdi_service_status_listener.h"
28 #include "ithermal_level_callback.h"
29 #include "ithermal_temp_callback.h"
30 #include "state_machine.h"
31 #include "thermal_action_manager.h"
32 #include "thermal_callback.h"
33 #include "thermal_config_base_info.h"
34 #include "thermal_config_sensor_cluster.h"
35 #include "thermal_observer.h"
36 #include "thermal_policy.h"
37 #include "thermal_sensor_info.h"
38 #include "thermal_service_subscriber.h"
39 #include "thermal_srv_config_parser.h"
40 #include "thermal_srv_sensor_info.h"
41 #include "thermal_srv_stub.h"
42 #include "v1_1/ithermal_interface.h"
43 #include "v1_1/thermal_types.h"
44 
45 namespace OHOS {
46 namespace PowerMgr {
47 using TypeTempMap = std::map<std::string, int32_t>;
48 using namespace OHOS::HDI::Thermal::V1_1;
49 using namespace OHOS::HDI::ServiceManager::V1_0;
50 class ThermalService final : public SystemAbility, public ThermalSrvStub {
51     DECLARE_SYSTEM_ABILITY(ThermalService);
52     DISALLOW_COPY_AND_MOVE(ThermalService);
53 
54 public:
55     ThermalService();
56     virtual ~ThermalService();
57     virtual void OnStart() override;
58     virtual void OnStop() override;
59     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
60     virtual int32_t Dump(int fd, const std::vector<std::u16string>& args) override;
61     bool SubscribeThermalTempCallback(
62         const std::vector<std::string>& typeList, const sptr<IThermalTempCallback>& callback) override;
63     bool UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback) override;
64     bool SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback) override;
65     bool UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback) override;
66     bool SubscribeThermalActionCallback(const std::vector<std::string>& actionList, const std::string& desc,
67         const sptr<IThermalActionCallback>& callback) override;
68     bool UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback) override;
69     bool GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo) override;
70     bool GetThermalLevel(ThermalLevel& level) override;
71     bool GetThermalInfo() override;
72     bool SetScene(const std::string& scene) override;
73     bool UpdateThermalState(const std::string& tag, const std::string& val, bool isImmed = false) override;
74     bool CreateConfigModule();
75     virtual std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override;
76 
77     int32_t HandleThermalCallbackEvent(const HdfThermalCallbackInfo& event);
78     int32_t HandleFanCallbackEvent(const HdfThermalCallbackInfo& event);
79     bool HandleTempEmulation(const TypeTempMap& typeTempMap);
80     static sptr<ThermalService> GetInstance();
81     static void DestroyInstance();
82 
SetTempReportSwitch(bool enable)83     void SetTempReportSwitch(bool enable)
84     {
85         isTempReport_ = enable;
86     }
87 
IsServiceReady()88     bool IsServiceReady() const
89     {
90         return ready_;
91     }
92 
GetBaseinfoObj()93     std::shared_ptr<ThermalConfigBaseInfo> GetBaseinfoObj() const
94     {
95         return baseInfo_;
96     }
GetStateMachineObj()97     std::shared_ptr<StateMachine> GetStateMachineObj() const
98     {
99         return state_;
100     }
GetActionManagerObj()101     std::shared_ptr<ThermalActionManager> GetActionManagerObj() const
102     {
103         return actionMgr_;
104     }
GetPolicy()105     std::shared_ptr<ThermalPolicy> GetPolicy() const
106     {
107         return policy_;
108     }
GetObserver()109     std::shared_ptr<ThermalObserver> GetObserver() const
110     {
111         return observer_;
112     }
GetSensorInfo()113     std::shared_ptr<ThermalSensorInfo> GetSensorInfo() const
114     {
115         return info_;
116     }
GetSubscriber()117     std::shared_ptr<ThermalServiceSubscriber> GetSubscriber() const
118     {
119         return serviceSubscriber_;
120     }
121 
GetActionPopup()122     std::shared_ptr<ActionPopup> GetActionPopup() const
123     {
124         return popup_;
125     }
126 
GetThermalInterface()127     sptr<IThermalInterface> GetThermalInterface() const
128     {
129         return thermalInterface_;
130     }
131 
GetSimulationXml()132     bool GetSimulationXml()
133     {
134         return isSimulation_;
135     }
136 
SetSimulationXml(bool isSimulation)137     void SetSimulationXml(bool isSimulation)
138     {
139         isSimulation_ = isSimulation;
140     }
141 
GetFanFaultDetect()142     std::shared_ptr<FanFaultDetect> GetFanFaultDetect() const
143     {
144         return fanFaultDetect_;
145     }
146 
GetConfigParser()147     ThermalSrvConfigParser& GetConfigParser()
148     {
149         return configParser_;
150     }
151 
InitSystemTestModules()152     void InitSystemTestModules()
153     {
154         InitConfigModule();
155     }
156 
GetScene()157     static const std::string& GetScene()
158     {
159         return scene_;
160     }
161 
162 private:
163     bool Init();
164     bool InitThermalDriver();
165     bool InitThermalObserver();
166     bool InitThermalSubscriber();
167     bool InitThermalAction();
168     bool InitThermalPolicy();
169     bool InitBaseInfo();
170     bool InitSensorCluser();
171     bool InitActionManager();
172     bool InitStateMachine();
173     bool InitModules();
174     bool InitConfigFile();
175     bool InitConfigModule();
176     void RegisterHdiStatusListener();
177     void RegisterThermalHdiCallback();
178     void UnRegisterThermalHdiCallback();
179     void RegisterFanHdiCallback();
180     void RegisterBootCompletedCallback();
181     void EnableMock(const std::string& actionName, void* mockObject);
182     bool ready_ {false};
183     static std::atomic_bool isBootCompleted_;
184     bool isSimulation_ {false};
185     bool isTempReport_ {true};
186     std::mutex mutex_;
187     std::shared_ptr<ThermalServiceSubscriber> serviceSubscriber_ {nullptr};
188     std::shared_ptr<ThermalObserver> observer_ {nullptr};
189     std::shared_ptr<ThermalSensorInfo> info_ {nullptr};
190     std::shared_ptr<ThermalPolicy> policy_ {nullptr};
191     std::shared_ptr<ThermalConfigBaseInfo> baseInfo_ {nullptr};
192     std::shared_ptr<StateMachine> state_ {nullptr};
193     std::shared_ptr<ThermalActionManager> actionMgr_ {nullptr};
194     std::shared_ptr<FanFaultDetect> fanFaultDetect_ {nullptr};
195     ThermalSrvConfigParser configParser_;
196     sptr<IThermalInterface> thermalInterface_ {nullptr};
197     sptr<IServiceManager> hdiServiceMgr_ {nullptr};
198     sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ {nullptr};
199     std::shared_ptr<ActionPopup> popup_;
200     std::atomic<bool> serviceConfigParsed {false};
201     static std::string scene_;
202     static sptr<ThermalService> instance_;
203     static std::mutex singletonMutex_;
204 };
205 } // namespace PowerMgr
206 } // namespace OHOS
207 #endif // THERMAL_SERVICE_H
208