1 /*
2  * Copyright (c) 2021-2023 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 POWERMGR_SYSTEM_SUSPEND_CONTROLLER_H
17 #define POWERMGR_SYSTEM_SUSPEND_CONTROLLER_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include <singleton.h>
23 
24 #include "actions/running_lock_action_info.h"
25 #include "hdi_service_status_listener.h"
26 #include "suspend/irunning_lock_hub.h"
27 #include "suspend/isuspend_controller.h"
28 #include "power_hdi_callback.h"
29 #include "v1_2/ipower_interface.h"
30 #include "ffrt_utils.h"
31 
32 namespace OHOS {
33 namespace PowerMgr {
34 class SystemSuspendController : public DelayedRefSingleton<SystemSuspendController> {
35 public:
36     void Suspend(const std::function<void()>& onSuspend, const std::function<void()>& onWakeup, bool force);
37     void Wakeup();
38     bool Hibernate();
39     int32_t AcquireRunningLock(const RunningLockParam& param);
40     int32_t ReleaseRunningLock(const RunningLockParam& param);
41     void Dump(std::string& info);
42     void GetWakeupReason(std::string& reason);
43     void RegisterHdiStatusListener();
44     void RegisterPowerHdiCallback();
45     void UnRegisterPowerHdiCallback();
46     void AllowAutoSleep();
47     void DisallowAutoSleep();
48     void SetSuspendTag(const std::string& tag);
49 
50 private:
51     DECLARE_DELAYED_REF_SINGLETON(SystemSuspendController);
52 
53     inline static constexpr const char* WAKEUP_HOLDER = "OHOSPowerMgr.WakeupHolder";
54     class PowerHdfCallback : public OHOS::HDI::Power::V1_2::IPowerHdiCallback {
55     public:
56         PowerHdfCallback() = default;
57         ~PowerHdfCallback() = default;
58         int32_t OnSuspend() override;
59         int32_t OnWakeup() override;
60         void SetListener(std::function<void()>& suspend, std::function<void()>& wakeup);
61     private:
62         std::function<void()> onSuspend_;
63         std::function<void()> onWakeup_;
64     };
65     OHOS::HDI::Power::V1_2::RunningLockInfo FillRunningLockInfo(const RunningLockParam& param);
66     std::mutex mutex_;
67     std::shared_ptr<Suspend::ISuspendController> sc_;
68     sptr<OHOS::HDI::Power::V1_2::IPowerInterface> powerInterface_ { nullptr };
69     sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> hdiServiceMgr_ { nullptr };
70     sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ { nullptr };
71     std::atomic<bool> allowSleepTask_ {false};
72     FFRTQueue queue_ {"power_system_suspend_controller"};
73 };
74 } // namespace PowerMgr
75 } // namespace OHOS
76 #endif // POWERMGR_SYSTEM_SUSPEND_CONTROLLER_H
77