• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "running_lock.h"
17 
18 #include <cstdint>
19 #include <list>
20 #include <iosfwd>
21 #include <string>
22 #include "errors.h"
23 #include "new"
24 #include "refbase.h"
25 #include "power_log.h"
26 #include "power_mgr_errors.h"
27 #include "running_lock_token_stub.h"
28 
29 using std::lock_guard;
30 
31 namespace OHOS {
32 namespace PowerMgr {
RunningLock(const wptr<IPowerMgr> & proxy,const std::string & name,RunningLockType type)33 RunningLock::RunningLock(const wptr<IPowerMgr>& proxy, const std::string& name, RunningLockType type)
34     : proxy_(proxy)
35 {
36     runningLockInfo_.name = name;
37     runningLockInfo_.type = type;
38 }
39 
~RunningLock()40 RunningLock::~RunningLock()
41 {
42     if (token_ != nullptr) {
43         Release();
44     }
45 }
46 
Init()47 PowerErrors RunningLock::Init()
48 {
49     token_ = new (std::nothrow)RunningLockTokenStub();
50     if (token_ == nullptr) {
51         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Failed to create the RunningLockTokenStub");
52         return PowerErrors::ERR_CONNECTION_FAIL;
53     }
54     return Create();
55 }
56 
Create()57 PowerErrors RunningLock::Create()
58 {
59     sptr<IPowerMgr> proxy = proxy_.promote();
60     if (proxy == nullptr) {
61         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
62         return PowerErrors::ERR_CONNECTION_FAIL;
63     }
64     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side CreateRunningLock call");
65     return proxy->CreateRunningLock(token_, runningLockInfo_);
66 }
67 
Recover(const wptr<IPowerMgr> & proxy)68 PowerErrors RunningLock::Recover(const wptr<IPowerMgr>& proxy)
69 {
70     POWER_HILOGI(FEATURE_RUNNING_LOCK, "recover running lock name %{public}s type %{public}d",
71         runningLockInfo_.name.c_str(), runningLockInfo_.type);
72     proxy_ = proxy;
73     return Create();
74 }
75 
Lock(int32_t timeOutMs)76 ErrCode RunningLock::Lock(int32_t timeOutMs)
77 {
78     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Lock timeOutMs: %{public}u", timeOutMs);
79 
80     sptr<IPowerMgr> proxy = proxy_.promote();
81     if (proxy == nullptr) {
82         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
83         return E_GET_POWER_SERVICE_FAILED;
84     }
85     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side Lock call");
86     proxy->Lock(token_, timeOutMs);
87     return ERR_OK;
88 }
89 
UnLock()90 ErrCode RunningLock::UnLock()
91 {
92     sptr<IPowerMgr> proxy = proxy_.promote();
93     if (proxy == nullptr) {
94         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
95         return E_GET_POWER_SERVICE_FAILED;
96     }
97     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side UnLock call");
98     proxy->UnLock(token_);
99     return ERR_OK;
100 }
101 
CheckUsedNoLock()102 bool RunningLock::CheckUsedNoLock()
103 {
104     sptr<IPowerMgr> proxy = proxy_.promote();
105     if (proxy == nullptr) {
106         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
107         return false;
108     }
109     bool ret = proxy->IsUsed(token_);
110 
111     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Is Used: %{public}d", ret);
112     return ret;
113 }
114 
IsUsed()115 bool RunningLock::IsUsed()
116 {
117     return CheckUsedNoLock();
118 }
119 
Release()120 void RunningLock::Release()
121 {
122     sptr<IPowerMgr> proxy = proxy_.promote();
123     if (proxy == nullptr) {
124         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
125         return;
126     }
127     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side ReleaseRunningLock call");
128     proxy->ReleaseRunningLock(token_);
129 }
130 } // namespace PowerMgr
131 } // namespace OHOS
132