• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "power_common.h"
19 #include "running_lock_token_stub.h"
20 
21 using std::lock_guard;
22 
23 namespace OHOS {
24 namespace PowerMgr {
RunningLock(const wptr<IPowerMgr> & proxy,const std::string & name,RunningLockType type)25 RunningLock::RunningLock(const wptr<IPowerMgr>& proxy, const std::string& name, RunningLockType type)
26     : proxy_(proxy)
27 {
28     runningLockInfo_.name = name;
29     runningLockInfo_.type = type;
30 }
31 
~RunningLock()32 RunningLock::~RunningLock()
33 {
34     if (token_ != nullptr) {
35         Release();
36     }
37 }
38 
Init()39 bool RunningLock::Init()
40 {
41     token_ = new (std::nothrow)RunningLockTokenStub();
42     if (token_ == nullptr) {
43         POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :creating RunningLockTokenStub error.", __func__);
44         return false;
45     }
46     Create();
47     return true;
48 }
49 
Lock(uint32_t timeOutMs)50 ErrCode RunningLock::Lock(uint32_t timeOutMs)
51 {
52     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :timeOutMs = %u.", __func__, timeOutMs);
53 
54     sptr<IPowerMgr> proxy = proxy_.promote();
55     if (proxy == nullptr) {
56         POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__);
57         return E_GET_POWER_SERVICE_FAILED;
58     }
59     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :service lock is called", __func__);
60     proxy->Lock(token_, runningLockInfo_, timeOutMs);
61 
62     return ERR_OK;
63 }
64 
UnLock()65 ErrCode RunningLock::UnLock()
66 {
67     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s.", __func__);
68     if (!CheckUsedNoLock()) {
69         return ERR_OK;
70     }
71     sptr<IPowerMgr> proxy = proxy_.promote();
72     if (proxy == nullptr) {
73         POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__);
74         return E_GET_POWER_SERVICE_FAILED;
75     }
76     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :really called service UnLock.", __func__);
77     proxy->UnLock(token_);
78     return ERR_OK;
79 }
80 
CheckUsedNoLock()81 bool RunningLock::CheckUsedNoLock()
82 {
83     sptr<IPowerMgr> proxy = proxy_.promote();
84     if (proxy == nullptr) {
85         POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__);
86         return false;
87     }
88     bool ret = proxy->IsUsed(token_);
89 
90     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s, ret = %d.", __func__, ret);
91     return ret;
92 }
93 
IsUsed()94 bool RunningLock::IsUsed()
95 {
96     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s.", __func__);
97     return CheckUsedNoLock();
98 }
99 
SetWorkTriggerList(const WorkTriggerList & workTriggerList)100 ErrCode RunningLock::SetWorkTriggerList(const WorkTriggerList& workTriggerList)
101 {
102     auto& list = runningLockInfo_.workTriggerlist;
103     list.clear();
104     for (auto& w : workTriggerList) {
105         if (w != nullptr) {
106             list.push_back(w);
107         }
108     }
109     if (!CheckUsedNoLock()) {
110         // no need to notify service when the lock is not used.
111         return ERR_OK;
112     }
113 
114     sptr<IPowerMgr> proxy = proxy_.promote();
115     if (proxy == nullptr) {
116         POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__);
117         return E_GET_POWER_SERVICE_FAILED;
118     }
119     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :service SetWorkTriggerList is called.", __func__);
120     proxy->SetWorkTriggerList(token_, runningLockInfo_.workTriggerlist);
121     return ERR_OK;
122 }
123 
GetWorkTriggerList() const124 const WorkTriggerList& RunningLock::GetWorkTriggerList() const
125 {
126     return runningLockInfo_.workTriggerlist;
127 }
128 
Create()129 void RunningLock::Create()
130 {
131     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s", __func__);
132 
133     sptr<IPowerMgr> proxy = proxy_.promote();
134     if (proxy == nullptr) {
135         POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__);
136         return;
137     }
138     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :service lock is called", __func__);
139     proxy->CreateRunningLock(token_, runningLockInfo_);
140 }
141 
Release()142 void RunningLock::Release()
143 {
144     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s ", __func__);
145 
146     sptr<IPowerMgr> proxy = proxy_.promote();
147     if (proxy == nullptr) {
148         POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__);
149         return;
150     }
151     POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :service lock is called", __func__);
152     proxy->ReleaseRunningLock(token_);
153 }
154 } // namespace PowerMgr
155 } // namespace OHOS
156