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