15ccb8f90Sopenharmony_ci/*
25ccb8f90Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License.
55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at
65ccb8f90Sopenharmony_ci *
75ccb8f90Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ccb8f90Sopenharmony_ci *
95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and
135ccb8f90Sopenharmony_ci * limitations under the License.
145ccb8f90Sopenharmony_ci */
155ccb8f90Sopenharmony_ci
165ccb8f90Sopenharmony_ci#include "power_mgr_client.h"
175ccb8f90Sopenharmony_ci
185ccb8f90Sopenharmony_ci#include <cinttypes>
195ccb8f90Sopenharmony_ci#include <mutex>
205ccb8f90Sopenharmony_ci#include <memory>
215ccb8f90Sopenharmony_ci#include <unistd.h>
225ccb8f90Sopenharmony_ci#include <vector>
235ccb8f90Sopenharmony_ci#include <datetime_ex.h>
245ccb8f90Sopenharmony_ci#include <thread>
255ccb8f90Sopenharmony_ci#include <chrono>
265ccb8f90Sopenharmony_ci#include <if_system_ability_manager.h>
275ccb8f90Sopenharmony_ci#include <iservice_registry.h>
285ccb8f90Sopenharmony_ci#include <system_ability_definition.h>
295ccb8f90Sopenharmony_ci#include "new"
305ccb8f90Sopenharmony_ci#include "refbase.h"
315ccb8f90Sopenharmony_ci#include "ipower_mgr.h"
325ccb8f90Sopenharmony_ci#include "ipower_mode_callback.h"
335ccb8f90Sopenharmony_ci#include "ipower_state_callback.h"
345ccb8f90Sopenharmony_ci#include "ipower_runninglock_callback.h"
355ccb8f90Sopenharmony_ci#include "iremote_broker.h"
365ccb8f90Sopenharmony_ci#include "iremote_object.h"
375ccb8f90Sopenharmony_ci#include "iscreen_off_pre_callback.h"
385ccb8f90Sopenharmony_ci#include "power_log.h"
395ccb8f90Sopenharmony_ci#include "power_common.h"
405ccb8f90Sopenharmony_ci#include "running_lock_info.h"
415ccb8f90Sopenharmony_ci
425ccb8f90Sopenharmony_cinamespace OHOS {
435ccb8f90Sopenharmony_cinamespace PowerMgr {
445ccb8f90Sopenharmony_cistd::vector<std::weak_ptr<RunningLock>> PowerMgrClient::runningLocks_;
455ccb8f90Sopenharmony_cistd::mutex PowerMgrClient::runningLocksMutex_;
465ccb8f90Sopenharmony_cistd::mutex g_instanceMutex;
475ccb8f90Sopenharmony_ci
485ccb8f90Sopenharmony_ciPowerMgrClient::PowerMgrClient()
495ccb8f90Sopenharmony_ci{
505ccb8f90Sopenharmony_ci    token_ = sptr<IPCObjectStub>::MakeSptr(u"ohos.powermgr.ClientAlivenessToken");
515ccb8f90Sopenharmony_ci}
525ccb8f90Sopenharmony_ci
535ccb8f90Sopenharmony_ciPowerMgrClient::~PowerMgrClient()
545ccb8f90Sopenharmony_ci{
555ccb8f90Sopenharmony_ci    if (proxy_ != nullptr) {
565ccb8f90Sopenharmony_ci        auto remoteObject = proxy_->AsObject();
575ccb8f90Sopenharmony_ci        if (remoteObject != nullptr) {
585ccb8f90Sopenharmony_ci            remoteObject->RemoveDeathRecipient(deathRecipient_);
595ccb8f90Sopenharmony_ci        }
605ccb8f90Sopenharmony_ci    }
615ccb8f90Sopenharmony_ci}
625ccb8f90Sopenharmony_ci
635ccb8f90Sopenharmony_ciPowerMgrClient& PowerMgrClient::GetInstance()
645ccb8f90Sopenharmony_ci{
655ccb8f90Sopenharmony_ci    static PowerMgrClient* instance = nullptr;
665ccb8f90Sopenharmony_ci    if (instance == nullptr) {
675ccb8f90Sopenharmony_ci        std::lock_guard<std::mutex> lock(g_instanceMutex);
685ccb8f90Sopenharmony_ci        if (instance == nullptr) {
695ccb8f90Sopenharmony_ci            instance = new PowerMgrClient();
705ccb8f90Sopenharmony_ci        }
715ccb8f90Sopenharmony_ci    }
725ccb8f90Sopenharmony_ci    return *instance;
735ccb8f90Sopenharmony_ci}
745ccb8f90Sopenharmony_ci
755ccb8f90Sopenharmony_ciErrCode PowerMgrClient::Connect()
765ccb8f90Sopenharmony_ci{
775ccb8f90Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
785ccb8f90Sopenharmony_ci    if (proxy_ != nullptr) {
795ccb8f90Sopenharmony_ci        return ERR_OK;
805ccb8f90Sopenharmony_ci    }
815ccb8f90Sopenharmony_ci
825ccb8f90Sopenharmony_ci    sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
835ccb8f90Sopenharmony_ci    if (sam == nullptr) {
845ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_FWK, "Failed to obtain SystemAbilityMgr");
855ccb8f90Sopenharmony_ci        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
865ccb8f90Sopenharmony_ci    }
875ccb8f90Sopenharmony_ci    sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(POWER_MANAGER_SERVICE_ID);
885ccb8f90Sopenharmony_ci    if (remoteObject_ == nullptr) {
895ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_FWK, "Check SystemAbility failed");
905ccb8f90Sopenharmony_ci        return E_GET_POWER_SERVICE_FAILED;
915ccb8f90Sopenharmony_ci    }
925ccb8f90Sopenharmony_ci
935ccb8f90Sopenharmony_ci    sptr<IRemoteObject::DeathRecipient> drt = new(std::nothrow) PowerMgrDeathRecipient(*this);
945ccb8f90Sopenharmony_ci    if (drt == nullptr) {
955ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_FWK, "Failed to create PowerMgrDeathRecipient");
965ccb8f90Sopenharmony_ci        return ERR_NO_MEMORY;
975ccb8f90Sopenharmony_ci    }
985ccb8f90Sopenharmony_ci    if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(drt))) {
995ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_FWK, "Add death recipient to PowerMgr service failed");
1005ccb8f90Sopenharmony_ci        return E_ADD_DEATH_RECIPIENT_FAILED;
1015ccb8f90Sopenharmony_ci    }
1025ccb8f90Sopenharmony_ci
1035ccb8f90Sopenharmony_ci    proxy_ = iface_cast<IPowerMgr>(remoteObject_);
1045ccb8f90Sopenharmony_ci    deathRecipient_ = drt;
1055ccb8f90Sopenharmony_ci    POWER_HILOGI(COMP_FWK, "Connecting PowerMgrService success, pid=%{public}d", getpid());
1065ccb8f90Sopenharmony_ci    return ERR_OK;
1075ccb8f90Sopenharmony_ci}
1085ccb8f90Sopenharmony_ci
1095ccb8f90Sopenharmony_civoid PowerMgrClient::PowerMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
1105ccb8f90Sopenharmony_ci{
1115ccb8f90Sopenharmony_ci    POWER_HILOGW(COMP_FWK, "Recv death notice, PowerMgr Death");
1125ccb8f90Sopenharmony_ci    client_.ResetProxy(remote);
1135ccb8f90Sopenharmony_ci
1145ccb8f90Sopenharmony_ci    // wait for powermgr service restart
1155ccb8f90Sopenharmony_ci    ErrCode ret = E_GET_POWER_SERVICE_FAILED;
1165ccb8f90Sopenharmony_ci    uint32_t retryCount = 0;
1175ccb8f90Sopenharmony_ci    while (++retryCount <= CONNECT_RETRY_COUNT) {
1185ccb8f90Sopenharmony_ci        usleep(CONNECT_RETRY_MS);
1195ccb8f90Sopenharmony_ci        ret = client_.Connect();
1205ccb8f90Sopenharmony_ci        if (ret == ERR_OK) {
1215ccb8f90Sopenharmony_ci            POWER_HILOGI(COMP_FWK, "retry connect success, count %{public}d", retryCount);
1225ccb8f90Sopenharmony_ci            break;
1235ccb8f90Sopenharmony_ci        }
1245ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_FWK, "retry connect failed, count %{public}d", retryCount);
1255ccb8f90Sopenharmony_ci    }
1265ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
1275ccb8f90Sopenharmony_ci        return;
1285ccb8f90Sopenharmony_ci    }
1295ccb8f90Sopenharmony_ci
1305ccb8f90Sopenharmony_ci    // recover running lock info
1315ccb8f90Sopenharmony_ci    client_.RecoverRunningLocks();
1325ccb8f90Sopenharmony_ci}
1335ccb8f90Sopenharmony_ci
1345ccb8f90Sopenharmony_civoid PowerMgrClient::RecoverRunningLocks()
1355ccb8f90Sopenharmony_ci{
1365ccb8f90Sopenharmony_ci    POWER_HILOGI(COMP_FWK, "start to recover running locks");
1375ccb8f90Sopenharmony_ci    std::lock_guard<std::mutex> lock(runningLocksMutex_);
1385ccb8f90Sopenharmony_ci    for (auto runningLock : runningLocks_) {
1395ccb8f90Sopenharmony_ci        if (runningLock.expired()) {
1405ccb8f90Sopenharmony_ci            continue;
1415ccb8f90Sopenharmony_ci        }
1425ccb8f90Sopenharmony_ci        std::shared_ptr<RunningLock> lock = runningLock.lock();
1435ccb8f90Sopenharmony_ci        if (lock != nullptr) {
1445ccb8f90Sopenharmony_ci            lock->Recover(proxy_);
1455ccb8f90Sopenharmony_ci        }
1465ccb8f90Sopenharmony_ci    }
1475ccb8f90Sopenharmony_ci}
1485ccb8f90Sopenharmony_ci
1495ccb8f90Sopenharmony_civoid PowerMgrClient::ResetProxy(const wptr<IRemoteObject>& remote)
1505ccb8f90Sopenharmony_ci{
1515ccb8f90Sopenharmony_ci    if (remote == nullptr) {
1525ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_FWK, "OnRemoteDied failed, remote is nullptr");
1535ccb8f90Sopenharmony_ci        return;
1545ccb8f90Sopenharmony_ci    }
1555ccb8f90Sopenharmony_ci
1565ccb8f90Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
1575ccb8f90Sopenharmony_ci    RETURN_IF(proxy_ == nullptr);
1585ccb8f90Sopenharmony_ci
1595ccb8f90Sopenharmony_ci    auto serviceRemote = proxy_->AsObject();
1605ccb8f90Sopenharmony_ci    if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
1615ccb8f90Sopenharmony_ci        serviceRemote->RemoveDeathRecipient(deathRecipient_);
1625ccb8f90Sopenharmony_ci        proxy_ = nullptr;
1635ccb8f90Sopenharmony_ci    }
1645ccb8f90Sopenharmony_ci}
1655ccb8f90Sopenharmony_ci
1665ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::RebootDevice(const std::string& reason)
1675ccb8f90Sopenharmony_ci{
1685ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
1695ccb8f90Sopenharmony_ci    return proxy_->RebootDevice(reason);
1705ccb8f90Sopenharmony_ci}
1715ccb8f90Sopenharmony_ci
1725ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::RebootDeviceForDeprecated(const std::string& reason)
1735ccb8f90Sopenharmony_ci{
1745ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
1755ccb8f90Sopenharmony_ci    return proxy_->RebootDeviceForDeprecated(reason);
1765ccb8f90Sopenharmony_ci}
1775ccb8f90Sopenharmony_ci
1785ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::ShutDownDevice(const std::string& reason)
1795ccb8f90Sopenharmony_ci{
1805ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
1815ccb8f90Sopenharmony_ci    return proxy_->ShutDownDevice(reason);
1825ccb8f90Sopenharmony_ci}
1835ccb8f90Sopenharmony_ci
1845ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::SetSuspendTag(const std::string &tag)
1855ccb8f90Sopenharmony_ci{
1865ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
1875ccb8f90Sopenharmony_ci    POWER_HILOGI(FEATURE_SUSPEND, "Set suspend tag: %{public}s", tag.c_str());
1885ccb8f90Sopenharmony_ci    return proxy_->SetSuspendTag(tag);
1895ccb8f90Sopenharmony_ci}
1905ccb8f90Sopenharmony_ci
1915ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::SuspendDevice(SuspendDeviceType reason, bool suspendImmed)
1925ccb8f90Sopenharmony_ci{
1935ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
1945ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_SUSPEND, " Calling SuspendDevice success");
1955ccb8f90Sopenharmony_ci    return proxy_->SuspendDevice(GetTickCount(), reason, suspendImmed);
1965ccb8f90Sopenharmony_ci}
1975ccb8f90Sopenharmony_ci
1985ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::WakeupDevice(WakeupDeviceType reason, const std::string& detail)
1995ccb8f90Sopenharmony_ci{
2005ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
2015ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_WAKEUP, " Calling WakeupDevice success");
2025ccb8f90Sopenharmony_ci    return proxy_->WakeupDevice(GetTickCount(), reason, detail);
2035ccb8f90Sopenharmony_ci}
2045ccb8f90Sopenharmony_ci
2055ccb8f90Sopenharmony_civoid PowerMgrClient::WakeupDeviceAsync(WakeupDeviceType reason, const std::string& detail)
2065ccb8f90Sopenharmony_ci{
2075ccb8f90Sopenharmony_ci    RETURN_IF(Connect() != ERR_OK);
2085ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_WAKEUP, " Calling WakeupDeviceAsync success");
2095ccb8f90Sopenharmony_ci    return proxy_->WakeupDeviceAsync(GetTickCount(), reason, detail);
2105ccb8f90Sopenharmony_ci}
2115ccb8f90Sopenharmony_ci
2125ccb8f90Sopenharmony_cibool PowerMgrClient::RefreshActivity(UserActivityType type)
2135ccb8f90Sopenharmony_ci{
2145ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
2155ccb8f90Sopenharmony_ci    bool ret = proxy_->RefreshActivity(GetTickCount(), type, true);
2165ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_ACTIVITY, "Calling RefreshActivity Success");
2175ccb8f90Sopenharmony_ci    return ret;
2185ccb8f90Sopenharmony_ci}
2195ccb8f90Sopenharmony_ci
2205ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout)
2215ccb8f90Sopenharmony_ci{
2225ccb8f90Sopenharmony_ci    if (timeout <= 0) {
2235ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_FWK, "Invalid timeout, timeout=%{public}" PRId64 "", timeout);
2245ccb8f90Sopenharmony_ci        return PowerErrors::ERR_PARAM_INVALID;
2255ccb8f90Sopenharmony_ci    }
2265ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
2275ccb8f90Sopenharmony_ci    PowerErrors ret = proxy_->OverrideScreenOffTime(timeout);
2285ccb8f90Sopenharmony_ci    POWER_HILOGD(COMP_FWK, "Calling OverrideScreenOffTime Success");
2295ccb8f90Sopenharmony_ci    return ret;
2305ccb8f90Sopenharmony_ci}
2315ccb8f90Sopenharmony_ci
2325ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::RestoreScreenOffTime()
2335ccb8f90Sopenharmony_ci{
2345ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
2355ccb8f90Sopenharmony_ci    PowerErrors ret = proxy_->RestoreScreenOffTime();
2365ccb8f90Sopenharmony_ci    POWER_HILOGD(COMP_FWK, "Calling RestoreScreenOffTime Success");
2375ccb8f90Sopenharmony_ci    return ret;
2385ccb8f90Sopenharmony_ci}
2395ccb8f90Sopenharmony_ci
2405ccb8f90Sopenharmony_cibool PowerMgrClient::IsRunningLockTypeSupported(RunningLockType type)
2415ccb8f90Sopenharmony_ci{
2425ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_RUNNING_LOCK, "RunningLockType=%{public}u", type);
2435ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
2445ccb8f90Sopenharmony_ci    return proxy_->IsRunningLockTypeSupported(type);
2455ccb8f90Sopenharmony_ci}
2465ccb8f90Sopenharmony_ci
2475ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::ForceSuspendDevice()
2485ccb8f90Sopenharmony_ci{
2495ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
2505ccb8f90Sopenharmony_ci    PowerErrors ret = proxy_->ForceSuspendDevice(GetTickCount());
2515ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_SUSPEND, "Calling ForceSuspendDevice Success");
2525ccb8f90Sopenharmony_ci    return ret;
2535ccb8f90Sopenharmony_ci}
2545ccb8f90Sopenharmony_ci
2555ccb8f90Sopenharmony_cibool PowerMgrClient::IsScreenOn(bool needPrintLog)
2565ccb8f90Sopenharmony_ci{
2575ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
2585ccb8f90Sopenharmony_ci    bool ret = false;
2595ccb8f90Sopenharmony_ci    ret = proxy_->IsScreenOn(needPrintLog);
2605ccb8f90Sopenharmony_ci    if (needPrintLog) {
2615ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_FWK, "IsScreenOn=%{public}d, caller pid=%{public}d", ret, getpid());
2625ccb8f90Sopenharmony_ci    } else {
2635ccb8f90Sopenharmony_ci        POWER_HILOGD(COMP_FWK, "IsScreenOn=%{public}d, caller pid=%{public}d", ret, getpid());
2645ccb8f90Sopenharmony_ci    }
2655ccb8f90Sopenharmony_ci    return ret;
2665ccb8f90Sopenharmony_ci}
2675ccb8f90Sopenharmony_ci
2685ccb8f90Sopenharmony_cibool PowerMgrClient::IsFoldScreenOn()
2695ccb8f90Sopenharmony_ci{
2705ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
2715ccb8f90Sopenharmony_ci    bool ret = false;
2725ccb8f90Sopenharmony_ci    ret = proxy_->IsFoldScreenOn();
2735ccb8f90Sopenharmony_ci    POWER_HILOGI(COMP_FWK, "IsFoldScreenOn=%{public}d, caller pid=%{public}d", ret, getpid());
2745ccb8f90Sopenharmony_ci    return ret;
2755ccb8f90Sopenharmony_ci}
2765ccb8f90Sopenharmony_ci
2775ccb8f90Sopenharmony_cibool PowerMgrClient::IsCollaborationScreenOn()
2785ccb8f90Sopenharmony_ci{
2795ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
2805ccb8f90Sopenharmony_ci    bool ret = false;
2815ccb8f90Sopenharmony_ci    ret = proxy_->IsCollaborationScreenOn();
2825ccb8f90Sopenharmony_ci    POWER_HILOGI(COMP_FWK, "IsCollaborationScreenOn=%{public}d, caller pid=%{public}d", ret, getpid());
2835ccb8f90Sopenharmony_ci    return ret;
2845ccb8f90Sopenharmony_ci}
2855ccb8f90Sopenharmony_ci
2865ccb8f90Sopenharmony_ciPowerState PowerMgrClient::GetState()
2875ccb8f90Sopenharmony_ci{
2885ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerState::UNKNOWN);
2895ccb8f90Sopenharmony_ci    return proxy_->GetState();
2905ccb8f90Sopenharmony_ci}
2915ccb8f90Sopenharmony_ci
2925ccb8f90Sopenharmony_cistd::shared_ptr<RunningLock> PowerMgrClient::CreateRunningLock(const std::string& name, RunningLockType type)
2935ccb8f90Sopenharmony_ci{
2945ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, nullptr);
2955ccb8f90Sopenharmony_ci
2965ccb8f90Sopenharmony_ci    uint32_t nameLen = (name.size() > RunningLock::MAX_NAME_LEN) ? RunningLock::MAX_NAME_LEN : name.size();
2975ccb8f90Sopenharmony_ci    std::string nameExt = name.substr(0, nameLen) + "_" + std::to_string(GetTickCount());
2985ccb8f90Sopenharmony_ci    std::shared_ptr<RunningLock> runningLock = std::make_shared<RunningLock>(proxy_, nameExt, type);
2995ccb8f90Sopenharmony_ci    if (runningLock == nullptr) {
3005ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_RUNNING_LOCK, "Failed to create RunningLock record");
3015ccb8f90Sopenharmony_ci        return nullptr;
3025ccb8f90Sopenharmony_ci    }
3035ccb8f90Sopenharmony_ci    POWER_HILOGI(
3045ccb8f90Sopenharmony_ci        FEATURE_RUNNING_LOCK, "Client CreateRunningLock name: %{public}s, type = %{public}d", name.c_str(), type);
3055ccb8f90Sopenharmony_ci    auto error = runningLock->Init();
3065ccb8f90Sopenharmony_ci    if (error != PowerErrors::ERR_OK) {
3075ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_RUNNING_LOCK, "RunningLock init failed");
3085ccb8f90Sopenharmony_ci        error_ = error;
3095ccb8f90Sopenharmony_ci        return nullptr;
3105ccb8f90Sopenharmony_ci    }
3115ccb8f90Sopenharmony_ci
3125ccb8f90Sopenharmony_ci    std::lock_guard<std::mutex> lock(runningLocksMutex_);
3135ccb8f90Sopenharmony_ci    runningLocks_.push_back(std::weak_ptr<RunningLock>(runningLock));
3145ccb8f90Sopenharmony_ci    return runningLock;
3155ccb8f90Sopenharmony_ci}
3165ccb8f90Sopenharmony_ci
3175ccb8f90Sopenharmony_cibool PowerMgrClient::ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)
3185ccb8f90Sopenharmony_ci{
3195ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
3205ccb8f90Sopenharmony_ci    return proxy_->ProxyRunningLock(isProxied, pid, uid);
3215ccb8f90Sopenharmony_ci}
3225ccb8f90Sopenharmony_ci
3235ccb8f90Sopenharmony_cibool PowerMgrClient::ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)
3245ccb8f90Sopenharmony_ci{
3255ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
3265ccb8f90Sopenharmony_ci    return proxy_->ProxyRunningLocks(isProxied, processInfos);
3275ccb8f90Sopenharmony_ci}
3285ccb8f90Sopenharmony_ci
3295ccb8f90Sopenharmony_cibool PowerMgrClient::ResetRunningLocks()
3305ccb8f90Sopenharmony_ci{
3315ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
3325ccb8f90Sopenharmony_ci    return proxy_->ResetRunningLocks();
3335ccb8f90Sopenharmony_ci}
3345ccb8f90Sopenharmony_ci
3355ccb8f90Sopenharmony_cibool PowerMgrClient::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync)
3365ccb8f90Sopenharmony_ci{
3375ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3385ccb8f90Sopenharmony_ci    bool ret = proxy_->RegisterPowerStateCallback(callback, isSync);
3395ccb8f90Sopenharmony_ci    return ret;
3405ccb8f90Sopenharmony_ci}
3415ccb8f90Sopenharmony_ci
3425ccb8f90Sopenharmony_cibool PowerMgrClient::UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
3435ccb8f90Sopenharmony_ci{
3445ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3455ccb8f90Sopenharmony_ci    bool ret = proxy_->UnRegisterPowerStateCallback(callback);
3465ccb8f90Sopenharmony_ci    return ret;
3475ccb8f90Sopenharmony_ci}
3485ccb8f90Sopenharmony_ci
3495ccb8f90Sopenharmony_cibool PowerMgrClient::RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority)
3505ccb8f90Sopenharmony_ci{
3515ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3525ccb8f90Sopenharmony_ci    bool ret = proxy_->RegisterSyncSleepCallback(callback, priority);
3535ccb8f90Sopenharmony_ci    return ret;
3545ccb8f90Sopenharmony_ci}
3555ccb8f90Sopenharmony_ci
3565ccb8f90Sopenharmony_cibool PowerMgrClient::UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback)
3575ccb8f90Sopenharmony_ci{
3585ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3595ccb8f90Sopenharmony_ci    bool ret = proxy_->UnRegisterSyncSleepCallback(callback);
3605ccb8f90Sopenharmony_ci    return ret;
3615ccb8f90Sopenharmony_ci}
3625ccb8f90Sopenharmony_ci
3635ccb8f90Sopenharmony_cibool PowerMgrClient::RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback)
3645ccb8f90Sopenharmony_ci{
3655ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3665ccb8f90Sopenharmony_ci    bool ret = proxy_->RegisterSyncHibernateCallback(callback);
3675ccb8f90Sopenharmony_ci    return ret;
3685ccb8f90Sopenharmony_ci}
3695ccb8f90Sopenharmony_ci
3705ccb8f90Sopenharmony_cibool PowerMgrClient::UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback)
3715ccb8f90Sopenharmony_ci{
3725ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3735ccb8f90Sopenharmony_ci    bool ret = proxy_->UnRegisterSyncHibernateCallback(callback);
3745ccb8f90Sopenharmony_ci    return ret;
3755ccb8f90Sopenharmony_ci}
3765ccb8f90Sopenharmony_ci
3775ccb8f90Sopenharmony_cibool PowerMgrClient::RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
3785ccb8f90Sopenharmony_ci{
3795ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3805ccb8f90Sopenharmony_ci    bool ret = proxy_->RegisterPowerModeCallback(callback);
3815ccb8f90Sopenharmony_ci    return ret;
3825ccb8f90Sopenharmony_ci}
3835ccb8f90Sopenharmony_ci
3845ccb8f90Sopenharmony_cibool PowerMgrClient::UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
3855ccb8f90Sopenharmony_ci{
3865ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
3875ccb8f90Sopenharmony_ci    bool ret = proxy_->UnRegisterPowerModeCallback(callback);
3885ccb8f90Sopenharmony_ci    return ret;
3895ccb8f90Sopenharmony_ci}
3905ccb8f90Sopenharmony_ci
3915ccb8f90Sopenharmony_cibool PowerMgrClient::RegisterScreenStateCallback(int32_t remainTime, const sptr<IScreenOffPreCallback>& callback)
3925ccb8f90Sopenharmony_ci{
3935ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((remainTime <= 0) || (callback == nullptr) || (Connect() != ERR_OK), false);
3945ccb8f90Sopenharmony_ci    POWER_HILOGI(FEATURE_SCREEN_OFF_PRE, "Register screen off pre Callback by client");
3955ccb8f90Sopenharmony_ci    bool ret = proxy_->RegisterScreenStateCallback(remainTime, callback);
3965ccb8f90Sopenharmony_ci    return ret;
3975ccb8f90Sopenharmony_ci}
3985ccb8f90Sopenharmony_ci
3995ccb8f90Sopenharmony_cibool PowerMgrClient::UnRegisterScreenStateCallback(const sptr<IScreenOffPreCallback>& callback)
4005ccb8f90Sopenharmony_ci{
4015ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
4025ccb8f90Sopenharmony_ci    POWER_HILOGI(FEATURE_SCREEN_OFF_PRE, "Unregister screen off pre Callback by client");
4035ccb8f90Sopenharmony_ci    bool ret = proxy_->UnRegisterScreenStateCallback(callback);
4045ccb8f90Sopenharmony_ci    return ret;
4055ccb8f90Sopenharmony_ci}
4065ccb8f90Sopenharmony_ci
4075ccb8f90Sopenharmony_cibool PowerMgrClient::RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
4085ccb8f90Sopenharmony_ci{
4095ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
4105ccb8f90Sopenharmony_ci    POWER_HILOGI(FEATURE_RUNNING_LOCK, "Register running lock Callback by client");
4115ccb8f90Sopenharmony_ci    bool ret = proxy_->RegisterRunningLockCallback(callback);
4125ccb8f90Sopenharmony_ci    return ret;
4135ccb8f90Sopenharmony_ci}
4145ccb8f90Sopenharmony_ci
4155ccb8f90Sopenharmony_cibool PowerMgrClient::UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
4165ccb8f90Sopenharmony_ci{
4175ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
4185ccb8f90Sopenharmony_ci    POWER_HILOGI(FEATURE_RUNNING_LOCK, "Unregister running lock Callback by client");
4195ccb8f90Sopenharmony_ci    bool ret = proxy_->UnRegisterRunningLockCallback(callback);
4205ccb8f90Sopenharmony_ci    return ret;
4215ccb8f90Sopenharmony_ci}
4225ccb8f90Sopenharmony_ci
4235ccb8f90Sopenharmony_cibool PowerMgrClient::SetDisplaySuspend(bool enable)
4245ccb8f90Sopenharmony_ci{
4255ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
4265ccb8f90Sopenharmony_ci    bool ret = proxy_->SetDisplaySuspend(enable);
4275ccb8f90Sopenharmony_ci    return ret;
4285ccb8f90Sopenharmony_ci}
4295ccb8f90Sopenharmony_ci
4305ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::Hibernate(bool clearMemory)
4315ccb8f90Sopenharmony_ci{
4325ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
4335ccb8f90Sopenharmony_ci    return proxy_->Hibernate(clearMemory);
4345ccb8f90Sopenharmony_ci}
4355ccb8f90Sopenharmony_ci
4365ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::SetDeviceMode(const PowerMode mode)
4375ccb8f90Sopenharmony_ci{
4385ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
4395ccb8f90Sopenharmony_ci    return proxy_->SetDeviceMode(mode);
4405ccb8f90Sopenharmony_ci}
4415ccb8f90Sopenharmony_ci
4425ccb8f90Sopenharmony_ciPowerMode PowerMgrClient::GetDeviceMode()
4435ccb8f90Sopenharmony_ci{
4445ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, static_cast<PowerMode>(0));
4455ccb8f90Sopenharmony_ci    return static_cast<PowerMode>(proxy_->GetDeviceMode());
4465ccb8f90Sopenharmony_ci}
4475ccb8f90Sopenharmony_ci
4485ccb8f90Sopenharmony_cistd::string PowerMgrClient::Dump(const std::vector<std::string>& args)
4495ccb8f90Sopenharmony_ci{
4505ccb8f90Sopenharmony_ci    std::string error = "can't connect service";
4515ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, error);
4525ccb8f90Sopenharmony_ci    return proxy_->ShellDump(args, args.size());
4535ccb8f90Sopenharmony_ci}
4545ccb8f90Sopenharmony_ci
4555ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::GetError()
4565ccb8f90Sopenharmony_ci{
4575ccb8f90Sopenharmony_ci    auto temp = error_;
4585ccb8f90Sopenharmony_ci    error_ = PowerErrors::ERR_OK;
4595ccb8f90Sopenharmony_ci    return temp;
4605ccb8f90Sopenharmony_ci}
4615ccb8f90Sopenharmony_ci
4625ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::IsStandby(bool& isStandby)
4635ccb8f90Sopenharmony_ci{
4645ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
4655ccb8f90Sopenharmony_ci    PowerErrors ret = proxy_->IsStandby(isStandby);
4665ccb8f90Sopenharmony_ci    return ret;
4675ccb8f90Sopenharmony_ci}
4685ccb8f90Sopenharmony_ci
4695ccb8f90Sopenharmony_cibool PowerMgrClient::QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists)
4705ccb8f90Sopenharmony_ci{
4715ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
4725ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_RUNNING_LOCK, "Query running lock lists by client");
4735ccb8f90Sopenharmony_ci    return proxy_->QueryRunningLockLists(runningLockLists);
4745ccb8f90Sopenharmony_ci}
4755ccb8f90Sopenharmony_ci
4765ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::SetForceTimingOut(bool enabled)
4775ccb8f90Sopenharmony_ci{
4785ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
4795ccb8f90Sopenharmony_ci    PowerErrors ret = proxy_->SetForceTimingOut(enabled, token_);
4805ccb8f90Sopenharmony_ci    return ret;
4815ccb8f90Sopenharmony_ci}
4825ccb8f90Sopenharmony_ci
4835ccb8f90Sopenharmony_ciPowerErrors PowerMgrClient::LockScreenAfterTimingOut(bool enabledLockScreen, bool checkLock, bool sendScreenOffEvent)
4845ccb8f90Sopenharmony_ci{
4855ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
4865ccb8f90Sopenharmony_ci    PowerErrors ret = proxy_->LockScreenAfterTimingOut(enabledLockScreen, checkLock, sendScreenOffEvent, token_);
4875ccb8f90Sopenharmony_ci    return ret;
4885ccb8f90Sopenharmony_ci}
4895ccb8f90Sopenharmony_ci
4905ccb8f90Sopenharmony_ci} // namespace PowerMgr
4915ccb8f90Sopenharmony_ci} // namespace OHOS
492