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_3/ipower_interface.h" 30 #include "ffrt_utils.h" 31 32 namespace OHOS { 33 namespace PowerMgr { 34 using namespace OHOS::HDI::Power; 35 class SystemSuspendController : public DelayedRefSingleton<SystemSuspendController> { 36 public: 37 void Suspend(const std::function<void()>& onSuspend, const std::function<void()>& onWakeup, bool force); 38 void Wakeup(); 39 bool Hibernate(); 40 int32_t AcquireRunningLock(const RunningLockParam& param); 41 int32_t ReleaseRunningLock(const RunningLockParam& param); 42 void Dump(std::string& info); 43 void GetWakeupReason(std::string& reason); 44 void RegisterHdiStatusListener(); 45 void RegisterPowerHdiCallback(); 46 void UnRegisterPowerHdiCallback(); 47 void AllowAutoSleep(); 48 void DisallowAutoSleep(); 49 void SetSuspendTag(const std::string& tag); 50 int32_t SetPowerConfig(const std::string& sceneName, const std::string& value); 51 int32_t GetPowerConfig(const std::string& sceneName, std::string& value); 52 53 private: 54 DECLARE_DELAYED_REF_SINGLETON(SystemSuspendController); 55 56 inline static constexpr const char* WAKEUP_HOLDER = "OHOSPowerMgr.WakeupHolder"; 57 class PowerHdfCallback : public OHOS::HDI::Power::V1_2::IPowerHdiCallback { 58 public: 59 PowerHdfCallback() = default; 60 ~PowerHdfCallback() = default; 61 int32_t OnSuspend() override; 62 int32_t OnWakeup() override; 63 void SetListener(std::function<void()>& suspend, std::function<void()>& wakeup); 64 private: 65 std::function<void()> onSuspend_; 66 std::function<void()> onWakeup_; 67 }; 68 OHOS::HDI::Power::V1_2::RunningLockInfo FillRunningLockInfo(const RunningLockParam& param); 69 sptr<V1_3::IPowerInterface> GetPowerInterface(); 70 std::mutex mutex_; 71 std::mutex interfaceMutex_; 72 std::shared_ptr<Suspend::ISuspendController> sc_; 73 sptr<V1_3::IPowerInterface> powerInterface_ { nullptr }; 74 sptr<V1_3::IPowerHdiCallbackExt> powerCallbackExt_ { nullptr }; 75 sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> hdiServiceMgr_ { nullptr }; 76 sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ { nullptr }; 77 std::atomic<bool> allowSleepTask_ {false}; 78 FFRTQueue queue_ {"power_system_suspend_controller"}; 79 }; 80 } // namespace PowerMgr 81 } // namespace OHOS 82 #endif // POWERMGR_SYSTEM_SUSPEND_CONTROLLER_H 83