1 /*
2 * Copyright (C) 2021-2022 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
Lock(uint32_t timeOutMs)57 ErrCode RunningLock::Lock(uint32_t timeOutMs)
58 {
59 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Lock timeOutMs: %{public}u", timeOutMs);
60
61 sptr<IPowerMgr> proxy = proxy_.promote();
62 if (proxy == nullptr) {
63 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
64 return E_GET_POWER_SERVICE_FAILED;
65 }
66 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side Lock call");
67 proxy->Lock(token_, runningLockInfo_, timeOutMs);
68
69 return ERR_OK;
70 }
71
UnLock()72 ErrCode RunningLock::UnLock()
73 {
74 if (!CheckUsedNoLock()) {
75 return ERR_OK;
76 }
77 sptr<IPowerMgr> proxy = proxy_.promote();
78 if (proxy == nullptr) {
79 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
80 return E_GET_POWER_SERVICE_FAILED;
81 }
82 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side UnLock call");
83 proxy->UnLock(token_);
84 return ERR_OK;
85 }
86
CheckUsedNoLock()87 bool RunningLock::CheckUsedNoLock()
88 {
89 sptr<IPowerMgr> proxy = proxy_.promote();
90 if (proxy == nullptr) {
91 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
92 return false;
93 }
94 bool ret = proxy->IsUsed(token_);
95
96 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Is Used: %{public}d", ret);
97 return ret;
98 }
99
IsUsed()100 bool RunningLock::IsUsed()
101 {
102 return CheckUsedNoLock();
103 }
104
SetWorkTriggerList(const WorkTriggerList & workTriggerList)105 ErrCode RunningLock::SetWorkTriggerList(const WorkTriggerList& workTriggerList)
106 {
107 auto& list = runningLockInfo_.workTriggerlist;
108 list.clear();
109 std::copy_if(workTriggerList.begin(), workTriggerList.end(), std::back_inserter(list), [](auto& work) {
110 return work != nullptr;
111 });
112
113 if (!CheckUsedNoLock()) {
114 // no need to notify service when the lock is not used.
115 return ERR_OK;
116 }
117
118 sptr<IPowerMgr> proxy = proxy_.promote();
119 if (proxy == nullptr) {
120 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
121 return E_GET_POWER_SERVICE_FAILED;
122 }
123 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side SetWorkTriggerList call");
124 proxy->SetWorkTriggerList(token_, runningLockInfo_.workTriggerlist);
125 return ERR_OK;
126 }
127
GetWorkTriggerList() const128 const WorkTriggerList& RunningLock::GetWorkTriggerList() const
129 {
130 return runningLockInfo_.workTriggerlist;
131 }
132
Create()133 PowerErrors RunningLock::Create()
134 {
135 sptr<IPowerMgr> proxy = proxy_.promote();
136 if (proxy == nullptr) {
137 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
138 return PowerErrors::ERR_CONNECTION_FAIL;
139 }
140 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side CreateRunningLock call");
141 return proxy->CreateRunningLock(token_, runningLockInfo_);
142 }
143
Release()144 void RunningLock::Release()
145 {
146 sptr<IPowerMgr> proxy = proxy_.promote();
147 if (proxy == nullptr) {
148 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
149 return;
150 }
151 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side ReleaseRunningLock call");
152 proxy->ReleaseRunningLock(token_);
153 }
154 } // namespace PowerMgr
155 } // namespace OHOS
156