• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
45 #include "common_event_data.h"
46 #include "common_event_manager.h"
47 #include "common_event_subscribe_info.h"
48 #include "common_event_support.h"
49 #endif
50 
51 namespace OHOS {
52 namespace PowerMgr {
53 using TypeTempMap = std::map<std::string, int32_t>;
54 using namespace OHOS::HDI::Thermal::V1_1;
55 using namespace OHOS::HDI::ServiceManager::V1_0;
56 class ThermalService final : public SystemAbility, public ThermalSrvStub {
57     DECLARE_SYSTEM_ABILITY(ThermalService);
58     DISALLOW_COPY_AND_MOVE(ThermalService);
59 
60 public:
61     ThermalService();
62     virtual ~ThermalService();
63     virtual void OnStart() override;
64     virtual void OnStop() override;
65     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
66     virtual int32_t Dump(int fd, const std::vector<std::u16string>& args) override;
67     int32_t SubscribeThermalTempCallback(
68         const std::vector<std::string>& typeList, const sptr<IThermalTempCallback>& callback) override;
69     int32_t UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback) override;
70     int32_t SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback) override;
71     int32_t UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback) override;
72     int32_t SubscribeThermalActionCallback(const std::vector<std::string>& actionList, const std::string& desc,
73         const sptr<IThermalActionCallback>& callback) override;
74     int32_t UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback) override;
75     int32_t GetThermalSrvSensorInfo(int32_t type, ThermalSrvSensorInfo& sensorInfo, bool& sensorInfoRet) override;
76     int32_t GetThermalLevel(int32_t& level) override;
77     int32_t GetThermalInfo() override;
78     int32_t SetScene(const std::string& scene) override;
79     int32_t UpdateThermalState(const std::string& tag, const std::string& val, bool isImmed = false) override;
80     bool CreateConfigModule();
81     int32_t ShellDump(const std::vector<std::string>& args, uint32_t argc, std::string& dumpShell) override;
82 
83     int32_t HandleThermalCallbackEvent(const HdfThermalCallbackInfo& event);
84     int32_t HandleFanCallbackEvent(const HdfThermalCallbackInfo& event);
85     bool HandleTempEmulation(const TypeTempMap& typeTempMap);
86     static sptr<ThermalService> GetInstance();
87     static void DestroyInstance();
88 
SetTempReportSwitch(bool enable)89     void SetTempReportSwitch(bool enable)
90     {
91         isTempReport_ = enable;
92     }
93 
IsServiceReady()94     bool IsServiceReady() const
95     {
96         return ready_;
97     }
98 
GetBaseinfoObj()99     std::shared_ptr<ThermalConfigBaseInfo> GetBaseinfoObj() const
100     {
101         return baseInfo_;
102     }
GetStateMachineObj()103     std::shared_ptr<StateMachine> GetStateMachineObj() const
104     {
105         return state_;
106     }
GetActionManagerObj()107     std::shared_ptr<ThermalActionManager> GetActionManagerObj() const
108     {
109         return actionMgr_;
110     }
GetPolicy()111     std::shared_ptr<ThermalPolicy> GetPolicy() const
112     {
113         return policy_;
114     }
GetObserver()115     std::shared_ptr<ThermalObserver> GetObserver() const
116     {
117         return observer_;
118     }
GetSensorInfo()119     std::shared_ptr<ThermalSensorInfo> GetSensorInfo() const
120     {
121         return info_;
122     }
GetSubscriber()123     std::shared_ptr<ThermalServiceSubscriber> GetSubscriber() const
124     {
125         return serviceSubscriber_;
126     }
127 
GetActionPopup()128     std::shared_ptr<ActionPopup> GetActionPopup() const
129     {
130         return popup_;
131     }
132 
GetThermalInterface()133     sptr<IThermalInterface> GetThermalInterface() const
134     {
135         return thermalInterface_;
136     }
137 
GetSimulationXml()138     bool GetSimulationXml()
139     {
140         return isSimulation_;
141     }
142 
SetSimulationXml(bool isSimulation)143     void SetSimulationXml(bool isSimulation)
144     {
145         isSimulation_ = isSimulation;
146     }
147 
GetFanFaultDetect()148     std::shared_ptr<FanFaultDetect> GetFanFaultDetect() const
149     {
150         return fanFaultDetect_;
151     }
152 
GetConfigParser()153     ThermalSrvConfigParser& GetConfigParser()
154     {
155         return configParser_;
156     }
157 
InitSystemTestModules()158     void InitSystemTestModules()
159     {
160         InitConfigModule();
161     }
162 
GetScene()163     static const std::string& GetScene()
164     {
165         return scene_;
166     }
167 #ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
168     static bool userAirplaneState_;
169     static bool isThermalAirplane_;
170 #endif
171 
172 private:
173     bool Init();
174     bool InitThermalDriver();
175     bool InitThermalObserver();
176     bool InitThermalSubscriber();
177     bool InitThermalAction();
178     bool InitThermalPolicy();
179     bool InitBaseInfo();
180     bool InitSensorCluser();
181     bool InitActionManager();
182     bool InitStateMachine();
183     bool InitModules();
184     bool InitConfigFile();
185     bool InitConfigModule();
186     void RegisterHdiStatusListener();
187     void RegisterThermalHdiCallback();
188     void UnRegisterThermalHdiCallback();
189     void RegisterFanHdiCallback();
190     void RegisterBootCompletedCallback();
191     void EnableMock(const std::string& actionName, void* mockObject);
192 #ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
193     bool SubscribeCommonEvent();
194 #endif
195     bool ready_ {false};
196     static std::atomic_bool isBootCompleted_;
197     bool isSimulation_ {false};
198     bool isTempReport_ {true};
199     std::mutex mutex_;
200     std::shared_ptr<ThermalServiceSubscriber> serviceSubscriber_ {nullptr};
201     std::shared_ptr<ThermalObserver> observer_ {nullptr};
202     std::shared_ptr<ThermalSensorInfo> info_ {nullptr};
203     std::shared_ptr<ThermalPolicy> policy_ {nullptr};
204     std::shared_ptr<ThermalConfigBaseInfo> baseInfo_ {nullptr};
205     std::shared_ptr<StateMachine> state_ {nullptr};
206     std::shared_ptr<ThermalActionManager> actionMgr_ {nullptr};
207     std::shared_ptr<FanFaultDetect> fanFaultDetect_ {nullptr};
208     ThermalSrvConfigParser configParser_;
209     sptr<IThermalInterface> thermalInterface_ {nullptr};
210     sptr<IServiceManager> hdiServiceMgr_ {nullptr};
211     sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ {nullptr};
212     std::shared_ptr<ActionPopup> popup_;
213     std::atomic<bool> serviceConfigParsed {false};
214     static std::string scene_;
215     static sptr<ThermalService> instance_;
216     static std::mutex singletonMutex_;
217 #ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
218     std::shared_ptr<EventFwk::CommonEventSubscriber> subscriberPtr_;
219 #endif
220 };
221 #ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
222 class AirplaneCommonEventSubscriber : public EventFwk::CommonEventSubscriber {
223 public:
AirplaneCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)224     explicit AirplaneCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo)
225         : EventFwk::CommonEventSubscriber(subscribeInfo) {}
~AirplaneCommonEventSubscriber()226     virtual ~AirplaneCommonEventSubscriber() {}
227     void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
228 };
229 #endif
230 } // namespace PowerMgr
231 } // namespace OHOS
232 #endif // THERMAL_SERVICE_H
233