From 9a82aa077f442651b7051ec330e8e7dc5c35ba2e Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Tue, 26 Oct 2021 13:54:01 +0800 Subject: [PATCH 1/2] Use init interface to shutdown/reboot Change-Id: I776a3210946b78acfc65a10e08c31a1a77184438 Signed-off-by: Du Jiang --- services/native/src/actions/default/BUILD.gn | 2 ++ .../src/actions/default/device_power_action.cpp | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/services/native/src/actions/default/BUILD.gn b/services/native/src/actions/default/BUILD.gn index df2144ca..c55ae191 100644 --- a/services/native/src/actions/default/BUILD.gn +++ b/services/native/src/actions/default/BUILD.gn @@ -18,6 +18,7 @@ config("powermgr_actions_impl_public_config") { include_dirs = [ ".", "${powermgr_hdi_path}/api/include", + "//base/startup/init_lite/interfaces/innerkits/include", "//base/startup/syspara_lite/adapter/native/syspara/include", ] } @@ -37,6 +38,7 @@ ohos_source_set("${powermgr_actions_default_target}") { deps = [ "${powermgr_hdi_path}/client:power_hdf_client", "../:powermgr_actions_common", + "//base/startup/init_lite/interfaces/innerkits/reboot:libreboot", "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//utils/native/base:utils", ] diff --git a/services/native/src/actions/default/device_power_action.cpp b/services/native/src/actions/default/device_power_action.cpp index d451612c..2cb298ce 100644 --- a/services/native/src/actions/default/device_power_action.cpp +++ b/services/native/src/actions/default/device_power_action.cpp @@ -22,6 +22,14 @@ #include "hilog_wrapper.h" #include "device_power_action.h" +#ifdef __cplusplus +extern "C" +{ +#endif +#include "init_reboot.h" +#ifdef __cplusplus +} +#endif namespace OHOS { namespace PowerMgr { @@ -33,7 +41,7 @@ void DevicePowerAction::Reboot(const std::string& reason) return; } POWER_HILOGI(MODULE_SERVICE, "Reboot executing."); - OHOS::system::SetParameter("sys.powerctl", updateCmd); + DoReboot(updateCmd); } void DevicePowerAction::Shutdown(const std::string& reason) @@ -44,7 +52,7 @@ void DevicePowerAction::Shutdown(const std::string& reason) return; } POWER_HILOGI(MODULE_SERVICE, "Shutdown executing."); - OHOS::system::SetParameter("sys.powerctl", updateCmd); + DoReboot(updateCmd); } } // namespace PowerMgr } // namespace OHOS -- Gitee From a1d539bf7600c9c144602d363529756b0cac2793 Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Thu, 28 Oct 2021 14:45:02 +0800 Subject: [PATCH 2/2] Redefine power hdi to adapt different OS Change-Id: Ib7669c760cddcabd8604a4af2539ee9dfc66e207 Signed-off-by: Du Jiang --- hdi/api/include/power_hdf_callback.h | 34 ++++ hdi/api/include/power_hdf_client.h | 13 +- hdi/api/include/power_hdf_info.h | 16 +- hdi/client/BUILD.gn | 5 +- hdi/client/src/power_hdf_callback.cpp | 42 +++++ hdi/client/src/power_hdf_client.cpp | 50 +++-- hdi/service/BUILD.gn | 2 +- hdi/service/include/power_hdf_service.h | 26 ++- hdi/service/src/power_hdf_service.cpp | 172 ++++++++++++++---- .../default/suspend/suspend_controller.cpp | 19 -- .../default/suspend/suspend_controller.h | 6 - .../default/system_suspend_controller.cpp | 49 ++++- .../default/system_suspend_controller.h | 12 ++ 13 files changed, 334 insertions(+), 112 deletions(-) create mode 100644 hdi/api/include/power_hdf_callback.h create mode 100644 hdi/client/src/power_hdf_callback.cpp diff --git a/hdi/api/include/power_hdf_callback.h b/hdi/api/include/power_hdf_callback.h new file mode 100644 index 00000000..2458fa5d --- /dev/null +++ b/hdi/api/include/power_hdf_callback.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef POWERMGR_IPOWER_HDF_SUSPEND_CALLBACK_H +#define POWERMGR_IPOWER_HDF_SUSPEND_CALLBACK_H + +#include "ipc_object_stub.h" +#include "power_hdf_info.h" + +namespace OHOS { +namespace PowerMgr { +class IPowerHdfCallback : public IPCObjectStub { +public: + explicit IPowerHdfCallback() : IPCObjectStub(u"ohos.powermgr.IPowerHdfCallback") {}; + virtual ~IPowerHdfCallback() = default; + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + virtual void OnSuspend() = 0; + virtual void OnWakeup() = 0; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_IPOWER_HDF_SUSPEND_CALLBACK_H diff --git a/hdi/api/include/power_hdf_client.h b/hdi/api/include/power_hdf_client.h index d853ee9c..a6665bc3 100644 --- a/hdi/api/include/power_hdf_client.h +++ b/hdi/api/include/power_hdf_client.h @@ -17,8 +17,8 @@ #define POWER_HDF_CLIENT_H #include -#include "ipc_object_proxy.h" #include "power_hdf_info.h" +#include "power_hdf_callback.h" namespace OHOS { namespace PowerMgr { @@ -26,11 +26,12 @@ class PowerHdfClient { public: PowerHdfClient() = default; ~PowerHdfClient() = default; - ErrCode Suspend(); - ErrCode ReadWakeCount(std::string& count); - ErrCode WriteWakeCount(const std::string& count); - ErrCode WakeLock(const std::string& name); - ErrCode WakeUnlock(const std::string& name); + ErrCode RegisterCallback(const sptr& callback); + ErrCode StartSuspend(); + ErrCode StopSuspend(); + ErrCode ForceSuspend(); + ErrCode SuspendBlock(const std::string& name); + ErrCode SuspendUnblock(const std::string& name); ErrCode Dump(std::string& info); private: ErrCode Dispatch(uint32_t cmd, MessageParcel &data, MessageParcel &reply); diff --git a/hdi/api/include/power_hdf_info.h b/hdi/api/include/power_hdf_info.h index 8c40d7b7..5f829bcf 100644 --- a/hdi/api/include/power_hdf_info.h +++ b/hdi/api/include/power_hdf_info.h @@ -17,14 +17,20 @@ #define POWER_HDF_INFO_H enum PowerHdfCmd { - CMD_SUSPEND = 0, - CMD_READ_WAKE_COUNT, - CMD_WRITE_WAKE_COUNT, - CMD_WAKE_LOCK, - CMD_WAKE_UNLOCK, + CMD_REGISTER_CALLBCK = 0, + CMD_START_SUSPEND, + CMD_STOP_SUSPEND, + CMD_FORCE_SUSPEND, + CMD_SUSPEND_BLOCK, + CMD_SUSPEND_UNBLOCK, CMD_DUMP, }; +enum PowerHdfCallbackCmd { + CMD_ON_SUSPEND = 0, + CMD_ON_WAKEUP, +}; + enum PowerHdfState { AWAKE = 0, INACTIVE, diff --git a/hdi/client/BUILD.gn b/hdi/client/BUILD.gn index 0872c3f1..562b8e6b 100644 --- a/hdi/client/BUILD.gn +++ b/hdi/client/BUILD.gn @@ -23,7 +23,10 @@ config("power_client_public_config") { } ohos_shared_library("power_hdf_client") { - sources = [ "src/power_hdf_client.cpp" ] + sources = [ + "src/power_hdf_client.cpp", + "src/power_hdf_callback.cpp", + ] configs = [ "${powermgr_utils_path}:utils_config" ] diff --git a/hdi/client/src/power_hdf_callback.cpp b/hdi/client/src/power_hdf_callback.cpp new file mode 100644 index 00000000..119cf4ba --- /dev/null +++ b/hdi/client/src/power_hdf_callback.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "power_hdf_callback.h" + +namespace OHOS { +namespace PowerMgr { + +int IPowerHdfCallback::OnRemoteRequest(uint32_t code, + MessageParcel &data, + MessageParcel &reply, + MessageOption &option) +{ + switch (code) { + case CMD_ON_SUSPEND: { + OnSuspend(); + return ERR_OK; + } + case CMD_ON_WAKEUP: { + OnWakeup(); + return ERR_OK; + } + default: { + return ERR_INVALID_OPERATION; + } + } +} + +} // namespace PowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/hdi/client/src/power_hdf_client.cpp b/hdi/client/src/power_hdf_client.cpp index 5af7ee6e..a48f8cfe 100644 --- a/hdi/client/src/power_hdf_client.cpp +++ b/hdi/client/src/power_hdf_client.cpp @@ -26,57 +26,55 @@ const std::string POWER_HDF_SERVICE = "power_hdf"; using OHOS::HDI::ServiceManager::V1_0::IServiceManager; -ErrCode PowerHdfClient::Suspend() +ErrCode PowerHdfClient::RegisterCallback(const sptr& callback) { - POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::Suspend: fun is start"); + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::RegisterCallback: fun is start"); MessageParcel data; MessageParcel reply; - return Dispatch(CMD_SUSPEND, data, reply); + data.WriteRemoteObject(callback); + return Dispatch(CMD_REGISTER_CALLBCK, data, reply); } -ErrCode PowerHdfClient::ReadWakeCount(std::string& count) +ErrCode PowerHdfClient::StartSuspend() { - POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::ReadWakeCount: fun is start"); + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::StartSuspend: fun is start"); MessageParcel data; MessageParcel reply; - ErrCode ret = Dispatch(CMD_READ_WAKE_COUNT, data, reply); - if (ret != ERR_OK) { - return ret; - } - count = reply.ReadString(); - POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::ReadWakeCount: %{public}s", count.c_str()); - return ERR_OK; + return Dispatch(CMD_START_SUSPEND, data, reply); } -ErrCode PowerHdfClient::WriteWakeCount(const std::string& count) +ErrCode PowerHdfClient::StopSuspend() { - POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::WriteWakeCount: fun is start"); + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::StopSuspend: fun is start"); MessageParcel data; MessageParcel reply; - data.WriteString(count); - ErrCode ret = Dispatch(CMD_WRITE_WAKE_COUNT, data, reply); - if (ret != ERR_OK) { - return ret; - } - return ERR_OK; + return Dispatch(CMD_STOP_SUSPEND, data, reply); +} + +ErrCode PowerHdfClient::ForceSuspend() +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::ForceSuspend: fun is start"); + MessageParcel data; + MessageParcel reply; + return Dispatch(CMD_FORCE_SUSPEND, data, reply); } -ErrCode PowerHdfClient::WakeLock(const std::string& name) +ErrCode PowerHdfClient::SuspendBlock(const std::string& name) { - POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::WakeLock: fun is start"); + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::SuspendBlock: fun is start"); MessageParcel data; MessageParcel reply; data.WriteString(name); - return Dispatch(CMD_WAKE_LOCK, data, reply); + return Dispatch(CMD_SUSPEND_BLOCK, data, reply); } -ErrCode PowerHdfClient::WakeUnlock(const std::string& name) +ErrCode PowerHdfClient::SuspendUnblock(const std::string& name) { - POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::WakeUnlock: fun is start"); + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::SuspendUnblock: fun is start"); MessageParcel data; MessageParcel reply; data.WriteString(name); - return Dispatch(CMD_WAKE_UNLOCK, data, reply); + return Dispatch(CMD_SUSPEND_UNBLOCK, data, reply); } sptr PowerHdfClient::GetService() diff --git a/hdi/service/BUILD.gn b/hdi/service/BUILD.gn index dafa0869..6392122d 100644 --- a/hdi/service/BUILD.gn +++ b/hdi/service/BUILD.gn @@ -47,7 +47,7 @@ ohos_shared_library("power_hdf_service") { "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core" ] part_name = "power_manager_native" } diff --git a/hdi/service/include/power_hdf_service.h b/hdi/service/include/power_hdf_service.h index b46d8c9d..8da60eee 100644 --- a/hdi/service/include/power_hdf_service.h +++ b/hdi/service/include/power_hdf_service.h @@ -16,10 +16,15 @@ #ifndef POWER_HDF_SERVICE_H #define POWER_HDF_SERVICE_H +#include #include #include +#include #include "errors.h" #include "hdf_device_desc.h" +#include "hdf_remote_service.h" +#include "power_hdf_callback.h" +#include "unique_fd.h" namespace OHOS { namespace PowerMgr { @@ -33,11 +38,12 @@ public: static int32_t Dispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply); - static int32_t Suspend(); - static int32_t ReadWakeCount(struct HdfSBuf *reply); - static int32_t WriteWakeCount(struct HdfSBuf *data); - static int32_t WakeLock(struct HdfSBuf *data); - static int32_t WakeUnlock(struct HdfSBuf *data); + static int32_t RegisterCallback(struct HdfSBuf *data); + static int32_t StartSuspend(struct HdfSBuf *data); + static int32_t StopSuspend(struct HdfSBuf *data); + static int32_t ForceSuspend(struct HdfSBuf *data); + static int32_t SuspendBlock(struct HdfSBuf *data); + static int32_t SuspendUnblock(struct HdfSBuf *data); static int32_t Dump(struct HdfSBuf *data); struct IDeviceIoService ioService; @@ -48,7 +54,17 @@ private: static constexpr const char * const LOCK_PATH = "/sys/power/wake_lock"; static constexpr const char * const UNLOCK_PATH = "/sys/power/wake_unlock"; static constexpr const char * const WAKEUP_COUNT_PATH = "/sys/power/wakeup_count"; + static std::chrono::milliseconds waitTime_; //{100ms}; static std::mutex mutex_; + static std::unique_ptr daemon_; + static bool suspending_; + static struct HdfRemoteService *callback_; + static UniqueFd wakeupCountFd; + static void AutoSuspendLoop(); + static int32_t DoSuspend(); + static std::string ReadWakeCount(); + static bool WriteWakeCount(const std::string& count); + static void NotifyCallback(int code); }; } // namespace PowerMgr } // namespace OHOS diff --git a/hdi/service/src/power_hdf_service.cpp b/hdi/service/src/power_hdf_service.cpp index 8307f19b..71a75116 100644 --- a/hdi/service/src/power_hdf_service.cpp +++ b/hdi/service/src/power_hdf_service.cpp @@ -21,12 +21,16 @@ #include "hdf_sbuf.h" #include "power_hdf_info.h" #include "pubdef.h" -#include "unique_fd.h" #include "utils/hdf_log.h" namespace OHOS { namespace PowerMgr { std::mutex PowerHdfService::mutex_; +std::unique_ptr PowerHdfService::daemon_; +std::chrono::milliseconds PowerHdfService::waitTime_ = std::chrono::milliseconds(100); +bool PowerHdfService::suspending_ = false; +struct HdfRemoteService* PowerHdfService::callback_ = nullptr; +UniqueFd PowerHdfService::wakeupCountFd = UniqueFd(-1); int32_t PowerHdfService::Bind(struct HdfDeviceObject *device) { @@ -86,24 +90,24 @@ int32_t PowerHdfService::Dispatch(struct HdfDeviceIoClient *client, } int ret = HDF_ERR_NOT_SUPPORT; switch (cmdId) { - case CMD_SUSPEND: { - ret = PowerHdfService::Suspend(); + case CMD_REGISTER_CALLBCK: { + ret = PowerHdfService::RegisterCallback(data); break; } - case CMD_READ_WAKE_COUNT: { - ret = PowerHdfService::ReadWakeCount(reply); + case CMD_START_SUSPEND: { + ret = PowerHdfService::StartSuspend(data); break; } - case CMD_WRITE_WAKE_COUNT: { - ret = PowerHdfService::WriteWakeCount(data); + case CMD_STOP_SUSPEND: { + ret = PowerHdfService::StopSuspend(data); break; } - case CMD_WAKE_LOCK: { - ret = PowerHdfService::WakeLock(data); + case CMD_SUSPEND_BLOCK: { + ret = PowerHdfService::SuspendBlock(data); break; } - case CMD_WAKE_UNLOCK: { - ret = PowerHdfService::WakeUnlock(data); + case CMD_SUSPEND_UNBLOCK: { + ret = PowerHdfService::SuspendUnblock(data); break; } case CMD_DUMP: { @@ -121,7 +125,55 @@ int32_t PowerHdfService::Dispatch(struct HdfDeviceIoClient *client, return ret; } -int32_t PowerHdfService::Suspend() +int32_t PowerHdfService::RegisterCallback(struct HdfSBuf *data) +{ + HDF_LOGD("%{public}s enter", __func__); + std::lock_guard lock(mutex_); + struct HdfRemoteService *remoteService = HdfSBufReadRemoteService(data); + callback_ = remoteService; + return HDF_SUCCESS; +} + +int32_t PowerHdfService::StartSuspend(struct HdfSBuf *data) +{ + HDF_LOGD("%{public}s enter", __func__); + std::lock_guard lock(mutex_); + if (suspending_) { + return HDF_SUCCESS; + } + suspending_ = true; + daemon_ = std::make_unique(&AutoSuspendLoop); + daemon_->detach(); + return HDF_SUCCESS; +} + +void PowerHdfService::AutoSuspendLoop() +{ + HDF_LOGD("%{public}s enter", __func__); + while (suspending_) { + std::this_thread::sleep_for(waitTime_); + + const std::string wakeupCount = ReadWakeCount(); + if (wakeupCount.empty()) { + HDF_LOGD("Can't read wake count, continue"); + continue; + } + if (!WriteWakeCount(wakeupCount)) { + HDF_LOGD("Can't write wake count, continue"); + continue; + } + NotifyCallback(CMD_ON_SUSPEND); + bool success = DoSuspend(); + if (!success) { + HDF_LOGE("Do suspend failed!"); + } + NotifyCallback(CMD_ON_WAKEUP); + break; + } + suspending_ = false; +} + +int32_t PowerHdfService::DoSuspend() { HDF_LOGD("%{public}s enter", __func__); std::lock_guard lock(mutex_); @@ -138,46 +190,59 @@ int32_t PowerHdfService::Suspend() return HDF_SUCCESS; } -int32_t PowerHdfService::ReadWakeCount(struct HdfSBuf *reply) +void PowerHdfService::NotifyCallback(int code) { - HDF_LOGD("%{public}s enter", __func__); - std::lock_guard lock(mutex_); - UniqueFd fd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); - if (fd < 0) { - HDF_LOGD("Failed to open the suspending state fd!"); - return HDF_FAILURE; + HDF_LOGI("%{public}s enter", __func__); + if (callback_ == nullptr) { + HDF_LOGD("%{public}s: subscriber is nullptr", __func__); + return; } - std::string count; - bool ret = LoadStringFromFd(fd, count); - if (!ret) { - HDF_LOGE("Failed to read wake count from kernel!"); - return HDF_FAILURE; + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + if (data == nullptr || reply == nullptr) { + HDF_LOGE("%{public}s: failed to obtain hdf sbuf", __func__); + return; } - HdfSbufWriteString(reply, count.c_str()); + + int ret = callback_->dispatcher->Dispatch(callback_, code, data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: failed to notify subscriber, ret: %{public}d", __func__, ret); + } else { + HDF_LOGD("%{public}s: succeed to notify subscriber", __func__); + } + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} + +int32_t PowerHdfService::StopSuspend(struct HdfSBuf *data) +{ + HDF_LOGD("%{public}s enter", __func__); + if (suspending_) { + suspending_ = false; + daemon_->join(); + } + return HDF_SUCCESS; } -int32_t PowerHdfService::WriteWakeCount(struct HdfSBuf *data) +int32_t PowerHdfService::ForceSuspend(struct HdfSBuf *data) { HDF_LOGD("%{public}s enter", __func__); - std::lock_guard lock(mutex_); - if (data == nullptr) { - return HDF_ERR_INVALID_PARAM; + if (suspending_) { + suspending_ = false; + daemon_->join(); } - const char* count = HdfSbufReadString(data); - if (count == nullptr) { - return HDF_ERR_INVALID_PARAM; - } - UniqueFd fd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); - bool ret = SaveStringToFd(fd, count); - if (!ret) { - HDF_LOGE("Failed to write the lock to kernel!"); - return HDF_FAILURE; + NotifyCallback(CMD_ON_SUSPEND); + bool success = DoSuspend(); + if (!success) { + HDF_LOGE("Do suspend failed!"); } + NotifyCallback(CMD_ON_WAKEUP); return HDF_SUCCESS; } -int32_t PowerHdfService::WakeLock(struct HdfSBuf *data) +int32_t PowerHdfService::SuspendBlock(struct HdfSBuf *data) { HDF_LOGD("%{public}s enter", __func__); std::lock_guard lock(mutex_); @@ -197,7 +262,7 @@ int32_t PowerHdfService::WakeLock(struct HdfSBuf *data) return HDF_SUCCESS; } -int32_t PowerHdfService::WakeUnlock(struct HdfSBuf *data) +int32_t PowerHdfService::SuspendUnblock(struct HdfSBuf *data) { HDF_LOGD("%{public}s enter", __func__); std::lock_guard lock(mutex_); @@ -217,6 +282,33 @@ int32_t PowerHdfService::WakeUnlock(struct HdfSBuf *data) return HDF_SUCCESS; } +std::string PowerHdfService::ReadWakeCount() +{ + if (wakeupCountFd < 0) { + wakeupCountFd = UniqueFd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); + } + std::string wakeupCount; + bool ret = LoadStringFromFd(wakeupCountFd, wakeupCount); + if (!ret) { + HDF_LOGE("Read wakeup count failed!"); + return std::string(); + } + return wakeupCount; +} + +bool PowerHdfService::WriteWakeCount(const std::string& count) +{ + if (wakeupCountFd < 0) { + wakeupCountFd = UniqueFd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); + } + bool ret = SaveStringToFd(wakeupCountFd, count.c_str()); + if (!ret) { + HDF_LOGE("Failed to write the wakeup count!"); + } + return ret; +} + + static void loadSystemInfo(const char* const path, std::string& info) { UniqueFd fd(TEMP_FAILURE_RETRY(open(path, O_RDWR | O_CLOEXEC))); diff --git a/services/native/src/actions/default/suspend/suspend_controller.cpp b/services/native/src/actions/default/suspend/suspend_controller.cpp index a0d3d64b..ef4ed906 100644 --- a/services/native/src/actions/default/suspend/suspend_controller.cpp +++ b/services/native/src/actions/default/suspend/suspend_controller.cpp @@ -80,7 +80,6 @@ void SuspendController::AutoSuspend::Start(SuspendCallback onSuspend, SuspendCal POWER_HILOGW(MODULE_SERVICE, "AutoSuspend is already started"); return; } - client_ = std::make_unique(); daemon_ = std::make_unique(&AutoSuspend::AutoSuspendLoop, this); daemon_->detach(); POWER_HILOGD(MODULE_SERVICE, "AutoSuspend Start detach"); @@ -101,12 +100,6 @@ void SuspendController::AutoSuspend::Stop() bool SuspendController::AutoSuspend::SuspendEnter() { POWER_HILOGE(MODULE_SERVICE, "SuspendController::AutoSuspend::SuspendEnter: fun is start!"); -#ifndef POWER_SUSPEND_NO_HDI - POWER_HILOGE(MODULE_SERVICE, "Before suspend!"); - ErrCode ret = client_->Suspend(); - POWER_HILOGE(MODULE_SERVICE, "After suspend!"); - return ret == ERR_OK ? true : false; -#else static bool inited = false; static UniqueFd suspendStateFd(TEMP_FAILURE_RETRY(open(SUSPEND_STATE_PATH, O_RDWR | O_CLOEXEC))); if (!inited) { @@ -123,17 +116,11 @@ bool SuspendController::AutoSuspend::SuspendEnter() POWER_HILOGE(MODULE_SERVICE, "Failed to write the suspending state!"); } return ret; -#endif } std::string SuspendController::AutoSuspend::WaitWakeupCount() { POWER_HILOGI(MODULE_SERVICE, "SuspendController::AutoSuspend::WaitWakeupCount: fun is start"); -#ifndef POWER_SUSPEND_NO_HDI - std::string count; - client_->ReadWakeCount(count); - return count; -#else if (wakeupCountFd < 0) { wakeupCountFd = UniqueFd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); } @@ -144,16 +131,11 @@ std::string SuspendController::AutoSuspend::WaitWakeupCount() return std::string(); } return wakeupCount; -#endif } bool SuspendController::AutoSuspend::WriteWakeupCount(std::string wakeupCount) { POWER_HILOGI(MODULE_SERVICE, "SuspendController::AutoSuspend::WriteWakeupCount: fun is start"); -#ifndef POWER_SUSPEND_NO_HDI - ErrCode ret = client_->WriteWakeCount(wakeupCount); - return ret == ERR_OK ? true : false; -#else if (wakeupCountFd < 0) { return false; } @@ -162,7 +144,6 @@ bool SuspendController::AutoSuspend::WriteWakeupCount(std::string wakeupCount) POWER_HILOGE(MODULE_SERVICE, "Failed to write the wakeup count!"); } return ret; -#endif } void SuspendController::Suspend(SuspendCallback onSuspend, SuspendCallback onWakeup, bool force) diff --git a/services/native/src/actions/default/suspend/suspend_controller.h b/services/native/src/actions/default/suspend/suspend_controller.h index a6631166..e7029092 100644 --- a/services/native/src/actions/default/suspend/suspend_controller.h +++ b/services/native/src/actions/default/suspend/suspend_controller.h @@ -22,12 +22,9 @@ #include #include -#include "power_hdf_client.h" #include "suspend/isuspend_controller.h" -#ifdef POWER_SUSPEND_NO_HDI #include "unique_fd.h" -#endif namespace OHOS { namespace PowerMgr { @@ -61,19 +58,16 @@ private: std::string WaitWakeupCount(); bool WriteWakeupCount(std::string wakeupCount); -#ifdef POWER_SUSPEND_NO_HDI static constexpr const char * const SUSPEND_STATE = "mem"; static constexpr const char * const SUSPEND_STATE_PATH = "/sys/power/state"; static constexpr const char * const WAKEUP_COUNT_PATH = "/sys/power/wakeup_count"; UniqueFd wakeupCountFd {-1}; -#endif std::chrono::milliseconds waitTime_ {100ms}; std::unique_ptr daemon_; WaitingSuspendConditionFunc waitingFunc_; SuspendCallback onSuspend_; SuspendCallback onWakeup_; - std::unique_ptr client_; }; bool SuspendConditionSatisfied(); diff --git a/services/native/src/actions/default/system_suspend_controller.cpp b/services/native/src/actions/default/system_suspend_controller.cpp index a948a951..04acaad5 100644 --- a/services/native/src/actions/default/system_suspend_controller.cpp +++ b/services/native/src/actions/default/system_suspend_controller.cpp @@ -22,8 +22,13 @@ namespace OHOS { namespace PowerMgr { SystemSuspendController::SystemSuspendController() { - sc_ = std::make_shared(); +#ifndef POWER_SUSPEND_NO_HDI + callback_ = new PowerHdfCallback(); client_ = std::make_unique(); + client_->RegisterCallback(callback_); +#else + sc_ = std::make_shared(); +#endif } SystemSuspendController::~SystemSuspendController() = default; @@ -31,27 +36,65 @@ SystemSuspendController::~SystemSuspendController() = default; void SystemSuspendController::Suspend(std::function onSuspend, std::function onWakeup, bool force) { +#ifndef POWER_SUSPEND_NO_HDI + if (force) { + client_->ForceSuspend(); + } else { + client_->StartSuspend(); + } +#else sc_->Suspend(onSuspend, onWakeup, force); +#endif } void SystemSuspendController::Wakeup() { +#ifndef POWER_SUSPEND_NO_HDI + client_->StopSuspend(); +#else sc_->Wakeup(); +#endif } void SystemSuspendController::AcquireRunningLock(const std::string& name) { - client_->WakeLock(name); +#ifndef POWER_SUSPEND_NO_HDI + client_->SuspendBlock(name); +#endif } void SystemSuspendController::ReleaseRunningLock(const std::string& name) { - client_->WakeUnlock(name); +#ifndef POWER_SUSPEND_NO_HDI + client_->SuspendUnblock(name); +#endif } void SystemSuspendController::Dump(std::string& info) { +#ifndef POWER_SUSPEND_NO_HDI client_->Dump(info); +#endif +} + +void SystemSuspendController::PowerHdfCallback::OnSuspend() { + if (onSuspend_ != nullptr) { + onSuspend_(); + } +} + +void SystemSuspendController::PowerHdfCallback::OnWakeup() { + if (onWakeup_ != nullptr) { + onWakeup_(); + } +} + +void SystemSuspendController::PowerHdfCallback::SetListener( + std::function suspend, + std::function wakeup) +{ + onSuspend_ = suspend; + onWakeup_ = wakeup; } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/actions/default/system_suspend_controller.h b/services/native/src/actions/default/system_suspend_controller.h index a5a2b8de..6a5bcf9e 100644 --- a/services/native/src/actions/default/system_suspend_controller.h +++ b/services/native/src/actions/default/system_suspend_controller.h @@ -39,9 +39,21 @@ private: DECLARE_DELAYED_REF_SINGLETON(SystemSuspendController); inline static const std::string WAKEUP_HOLDER = "OHOSPowerMgr.WakeupHolder"; + class PowerHdfCallback : public IPowerHdfCallback { + public: + PowerHdfCallback() = default; + ~PowerHdfCallback() = default; + void OnSuspend() override; + void OnWakeup() override; + void SetListener(std::function suspend, std::function wakeup); + private: + std::function onSuspend_; + std::function onWakeup_; + }; std::mutex mutex_; std::shared_ptr sc_; std::unique_ptr client_; + sptr callback_; }; } // namespace PowerMgr } // namespace OHOS -- Gitee