/* * Copyright (C) 2021-2023 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 "running_lock.h" #include #include #include #include #include "errors.h" #include "new" #include "refbase.h" #include "power_log.h" #include "power_mgr_errors.h" #include "running_lock_token_stub.h" using std::lock_guard; namespace OHOS { namespace PowerMgr { RunningLock::RunningLock(const wptr& proxy, const std::string& name, RunningLockType type) : proxy_(proxy) { runningLockInfo_.name = name; runningLockInfo_.type = type; } RunningLock::~RunningLock() { if (token_ != nullptr) { Release(); } } PowerErrors RunningLock::Init() { token_ = new (std::nothrow)RunningLockTokenStub(); if (token_ == nullptr) { POWER_HILOGE(FEATURE_RUNNING_LOCK, "Failed to create the RunningLockTokenStub"); return PowerErrors::ERR_CONNECTION_FAIL; } return Create(); } PowerErrors RunningLock::Create() { sptr proxy = proxy_.promote(); if (proxy == nullptr) { POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer"); return PowerErrors::ERR_CONNECTION_FAIL; } POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side CreateRunningLock call"); return proxy->CreateRunningLock(token_, runningLockInfo_); } PowerErrors RunningLock::Recover(const wptr& proxy) { POWER_HILOGI(FEATURE_RUNNING_LOCK, "recover running lock name %{public}s type %{public}d", runningLockInfo_.name.c_str(), runningLockInfo_.type); proxy_ = proxy; return Create(); } ErrCode RunningLock::Lock(int32_t timeOutMs) { POWER_HILOGD(FEATURE_RUNNING_LOCK, "Lock timeOutMs: %{public}u", timeOutMs); sptr proxy = proxy_.promote(); if (proxy == nullptr) { POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer"); return E_GET_POWER_SERVICE_FAILED; } POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side Lock call"); proxy->Lock(token_, timeOutMs); return ERR_OK; } ErrCode RunningLock::UnLock() { sptr proxy = proxy_.promote(); if (proxy == nullptr) { POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer"); return E_GET_POWER_SERVICE_FAILED; } POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side UnLock call"); proxy->UnLock(token_); return ERR_OK; } bool RunningLock::CheckUsedNoLock() { sptr proxy = proxy_.promote(); if (proxy == nullptr) { POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer"); return false; } bool ret = proxy->IsUsed(token_); POWER_HILOGD(FEATURE_RUNNING_LOCK, "Is Used: %{public}d", ret); return ret; } bool RunningLock::IsUsed() { return CheckUsedNoLock(); } void RunningLock::Release() { sptr proxy = proxy_.promote(); if (proxy == nullptr) { POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer"); return; } POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side ReleaseRunningLock call"); proxy->ReleaseRunningLock(token_); } } // namespace PowerMgr } // namespace OHOS