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 <timer.h>
23 #include "errors.h"
24 #include "new"
25 #include "refbase.h"
26 #include "power_log.h"
27 #include "power_mgr_errors.h"
28 #include "running_lock_token_stub.h"
29
30 namespace OHOS {
31 namespace PowerMgr {
32 constexpr int32_t DEFAULT_TIMEOUT = 3000;
33 constexpr int32_t NOT_USE_TIMEOUT = -1;
RunningLock(const wptr<IPowerMgr> & proxy,const std::string & name,RunningLockType type)34 RunningLock::RunningLock(const wptr<IPowerMgr>& proxy, const std::string& name, RunningLockType type)
35 : proxy_(proxy)
36 {
37 runningLockInfo_.name = name;
38 runningLockInfo_.type = type;
39 }
40
~RunningLock()41 RunningLock::~RunningLock()
42 {
43 if (token_ != nullptr) {
44 Release();
45 }
46 }
47
Init()48 PowerErrors RunningLock::Init()
49 {
50 token_ = new (std::nothrow)RunningLockTokenStub();
51 if (token_ == nullptr) {
52 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Failed to create the RunningLockTokenStub");
53 return PowerErrors::ERR_CONNECTION_FAIL;
54 }
55 return Create();
56 }
57
Create()58 PowerErrors RunningLock::Create()
59 {
60 sptr<IPowerMgr> proxy = proxy_.promote();
61 if (proxy == nullptr) {
62 POWER_HILOGE(FEATURE_RUNNING_LOCK, "CProxy=null");
63 return PowerErrors::ERR_CONNECTION_FAIL;
64 }
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
UpdateWorkSource(const std::vector<int32_t> & workSources)76 ErrCode RunningLock::UpdateWorkSource(const std::vector<int32_t>& workSources)
77 {
78 sptr<IPowerMgr> proxy = proxy_.promote();
79 if (proxy == nullptr) {
80 POWER_HILOGE(FEATURE_RUNNING_LOCK, "UpProxy=null");
81 return E_GET_POWER_SERVICE_FAILED;
82 }
83 if (!proxy->UpdateWorkSource(token_, workSources)) {
84 return E_INNER_ERR;
85 }
86 return ERR_OK;
87 }
88
Lock(int32_t timeOutMs)89 ErrCode RunningLock::Lock(int32_t timeOutMs)
90 {
91 sptr<IPowerMgr> proxy = proxy_.promote();
92 if (proxy == nullptr) {
93 POWER_HILOGE(FEATURE_RUNNING_LOCK, "LProxy=null");
94 return E_GET_POWER_SERVICE_FAILED;
95 }
96 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side Lock call, timeOutMs=%{public}d", timeOutMs);
97 if (runningLockInfo_.type == RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) {
98 timeOutMs = NOT_USE_TIMEOUT;
99 POWER_HILOGW(FEATURE_RUNNING_LOCK, "PROXIMITY not use timeout");
100 }
101 if (timeOutMs == 0) {
102 timeOutMs = DEFAULT_TIMEOUT;
103 POWER_HILOGW(FEATURE_RUNNING_LOCK, "use default timeout");
104 }
105 PowerErrors error = proxy->Lock(token_, timeOutMs);
106 if (error != PowerErrors::ERR_OK) {
107 return error == PowerErrors::ERR_PERMISSION_DENIED ? E_PERMISSION_DENIED : E_INNER_ERR;
108 }
109 return ERR_OK;
110 }
111
UnLock()112 ErrCode RunningLock::UnLock()
113 {
114 sptr<IPowerMgr> proxy = proxy_.promote();
115 if (proxy == nullptr) {
116 POWER_HILOGE(FEATURE_RUNNING_LOCK, "UnProxy=null");
117 return E_GET_POWER_SERVICE_FAILED;
118 }
119 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side UnLock call");
120 PowerErrors error = proxy->UnLock(token_, runningLockInfo_.name);
121 if (error != PowerErrors::ERR_OK) {
122 return error == PowerErrors::ERR_PERMISSION_DENIED ? E_PERMISSION_DENIED : E_INNER_ERR;
123 }
124 return ERR_OK;
125 }
126
IsUsed()127 bool RunningLock::IsUsed()
128 {
129 sptr<IPowerMgr> proxy = proxy_.promote();
130 if (proxy == nullptr) {
131 POWER_HILOGE(FEATURE_RUNNING_LOCK, "IProxy=null");
132 return false;
133 }
134 bool ret = proxy->IsUsed(token_);
135 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Is Used: %{public}d", ret);
136 return ret;
137 }
138
Release()139 void RunningLock::Release()
140 {
141 sptr<IPowerMgr> proxy = proxy_.promote();
142 if (proxy == nullptr) {
143 POWER_HILOGE(FEATURE_RUNNING_LOCK, "RProxy=null");
144 return;
145 }
146 // ReleaseRunningLock
147 POWER_HILOGI(FEATURE_RUNNING_LOCK, "RlsN=%{public}s", runningLockInfo_.name.c_str());
148 proxy->ReleaseRunningLock(token_, runningLockInfo_.name);
149 }
150 } // namespace PowerMgr
151 } // namespace OHOS
152