1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "SystemSuspendHidl.h"
18
19 #include <hwbinder/IPCThreadState.h>
20
21 using ::android::hardware::Void;
22
23 namespace android {
24 namespace system {
25 namespace suspend {
26 namespace V1_0 {
27
getCallingPid()28 static inline int getCallingPid() {
29 return ::android::hardware::IPCThreadState::self()->getCallingPid();
30 }
31
WakeLock(SystemSuspend * systemSuspend,const std::string & name,int pid)32 WakeLock::WakeLock(SystemSuspend* systemSuspend, const std::string& name, int pid)
33 : mReleased(), mSystemSuspend(systemSuspend), mName(name), mPid(pid) {
34 mSystemSuspend->incSuspendCounter(mName);
35 mSystemSuspend->updateWakeLockStatOnAcquire(mName, mPid);
36 }
37
~WakeLock()38 WakeLock::~WakeLock() {
39 releaseOnce();
40 }
41
release()42 Return<void> WakeLock::release() {
43 releaseOnce();
44 return Void();
45 }
46
releaseOnce()47 void WakeLock::releaseOnce() {
48 std::call_once(mReleased, [this]() {
49 mSystemSuspend->decSuspendCounter(mName);
50 mSystemSuspend->updateWakeLockStatOnRelease(mName, mPid);
51 });
52 }
53
SystemSuspendHidl(SystemSuspend * systemSuspend)54 SystemSuspendHidl::SystemSuspendHidl(SystemSuspend* systemSuspend)
55 : mSystemSuspend(systemSuspend) {}
56
acquireWakeLock(WakeLockType,const hidl_string & name)57 Return<sp<IWakeLock>> SystemSuspendHidl::acquireWakeLock(WakeLockType /* type */,
58 const hidl_string& name) {
59 auto pid = getCallingPid();
60 IWakeLock* wl = new WakeLock{mSystemSuspend, name, pid};
61 return wl;
62 }
63
64 } // namespace V1_0
65 } // namespace suspend
66 } // namespace system
67 } // namespace android
68