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 "SystemSuspendAidl.h"
18
19 #include <binder/IPCThreadState.h>
20
21 namespace aidl {
22 namespace android {
23 namespace system {
24 namespace suspend {
25
getCallingPid()26 static inline int getCallingPid() {
27 return ::android::IPCThreadState::self()->getCallingPid();
28 }
29
WakeLock(SystemSuspend * systemSuspend,const std::string & name,int pid)30 WakeLock::WakeLock(SystemSuspend* systemSuspend, const std::string& name, int pid)
31 : mReleased(), mSystemSuspend(systemSuspend), mName(name), mPid(pid) {
32 mSystemSuspend->incSuspendCounter(mName);
33 }
34
~WakeLock()35 WakeLock::~WakeLock() {
36 releaseOnce();
37 }
38
release()39 ndk::ScopedAStatus WakeLock::release() {
40 releaseOnce();
41 return ndk::ScopedAStatus::ok();
42 }
43
releaseOnce()44 void WakeLock::releaseOnce() {
45 std::call_once(mReleased, [this]() {
46 mSystemSuspend->decSuspendCounter(mName);
47 mSystemSuspend->updateWakeLockStatOnRelease(mName, mPid);
48 });
49 }
50
SystemSuspendAidl(SystemSuspend * systemSuspend)51 SystemSuspendAidl::SystemSuspendAidl(SystemSuspend* systemSuspend)
52 : mSystemSuspend(systemSuspend) {}
53
acquireWakeLock(WakeLockType,const std::string & name,std::shared_ptr<IWakeLock> * _aidl_return)54 ndk::ScopedAStatus SystemSuspendAidl::acquireWakeLock(WakeLockType /* type */,
55 const std::string& name,
56 std::shared_ptr<IWakeLock>* _aidl_return) {
57 auto pid = getCallingPid();
58 if (_aidl_return == nullptr) {
59 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
60 }
61 *_aidl_return = ndk::SharedRefBase::make<WakeLock>(mSystemSuspend, name, pid);
62 mSystemSuspend->updateWakeLockStatOnAcquire(name, pid);
63 return ndk::ScopedAStatus::ok();
64 }
65
66 } // namespace suspend
67 } // namespace system
68 } // namespace android
69 } // namespace aidl
70