blob: 98ce861a9c1eebb4678144e987780c52be474867 [file] [log] [blame]
Tri Voe0a9c902018-08-28 13:58:01 -07001/*
2 * Copyright (C) 2018 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#define LOG_TAG "power"
18#define ATRACE_TAG ATRACE_TAG_POWER
19
Tri Vo934f1752019-06-26 10:34:28 -070020#include <hardware_legacy/power.h>
21#include <wakelock/wakelock.h>
22
Kalesh Singhb64a38e2021-07-20 23:20:03 +000023#include <aidl/android/system/suspend/ISystemSuspend.h>
24#include <aidl/android/system/suspend/IWakeLock.h>
25#include <android/binder_manager.h>
Tri Voe0a9c902018-08-28 13:58:01 -070026#include <android-base/logging.h>
Tri Voe0a9c902018-08-28 13:58:01 -070027#include <utils/Trace.h>
28
29#include <mutex>
30#include <string>
31#include <thread>
32#include <unordered_map>
33
Kalesh Singhb64a38e2021-07-20 23:20:03 +000034using aidl::android::system::suspend::ISystemSuspend;
35using aidl::android::system::suspend::IWakeLock;
36using aidl::android::system::suspend::WakeLockType;
Tri Voe0a9c902018-08-28 13:58:01 -070037
38static std::mutex gLock;
Kalesh Singhb64a38e2021-07-20 23:20:03 +000039static std::unordered_map<std::string, std::shared_ptr<IWakeLock>> gWakeLockMap;
Tri Voe0a9c902018-08-28 13:58:01 -070040
Kalesh Singhb64a38e2021-07-20 23:20:03 +000041static const std::shared_ptr<ISystemSuspend> getSystemSuspendServiceOnce() {
42 static std::shared_ptr<ISystemSuspend> suspendService =
Kalesh Singh06c0d042021-08-04 15:14:34 +000043 ISystemSuspend::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(
Kalesh Singhb64a38e2021-07-20 23:20:03 +000044 (ISystemSuspend::descriptor + std::string("/default")).c_str())));
Tri Voe0a9c902018-08-28 13:58:01 -070045 return suspendService;
46}
47
48int acquire_wake_lock(int, const char* id) {
49 ATRACE_CALL();
Kalesh Singhb64a38e2021-07-20 23:20:03 +000050 const auto suspendService = getSystemSuspendServiceOnce();
Tri Voe0a9c902018-08-28 13:58:01 -070051 if (!suspendService) {
Kalesh Singh06c0d042021-08-04 15:14:34 +000052 LOG(ERROR) << "Failed to get SystemSuspend service";
Tri Voe0a9c902018-08-28 13:58:01 -070053 return -1;
54 }
55
56 std::lock_guard<std::mutex> l{gLock};
57 if (!gWakeLockMap[id]) {
Kalesh Singhb64a38e2021-07-20 23:20:03 +000058 std::shared_ptr<IWakeLock> wl = nullptr;
59 auto status = suspendService->acquireWakeLock(WakeLockType::PARTIAL, id, &wl);
60 // It's possible that during device shutdown SystemSuspend service has already exited.
61 // Check that the wakelock object is not null.
62 if (!wl) {
63 LOG(ERROR) << "ISuspendService::acquireWakeLock() call failed: "
64 << status.getDescription();
Tri Vo631215c2018-12-04 16:21:12 -080065 return -1;
66 } else {
Kalesh Singhb64a38e2021-07-20 23:20:03 +000067 gWakeLockMap[id] = wl;
Tri Vo631215c2018-12-04 16:21:12 -080068 }
Tri Voe0a9c902018-08-28 13:58:01 -070069 }
70 return 0;
71}
72
73int release_wake_lock(const char* id) {
74 ATRACE_CALL();
75 std::lock_guard<std::mutex> l{gLock};
76 if (gWakeLockMap[id]) {
Tri Vo8eb59a02018-10-14 16:09:51 -070077 // Ignore errors on release() call since hwbinder driver will clean up the underlying object
Kalesh Singhb64a38e2021-07-20 23:20:03 +000078 // once we clear the corresponding shared_ptr.
79 auto status = gWakeLockMap[id]->release();
80 if (!status.isOk()) {
81 LOG(ERROR) << "IWakeLock::release() call failed: " << status.getDescription();
Tri Vo8eb59a02018-10-14 16:09:51 -070082 }
Kalesh Singhb64a38e2021-07-20 23:20:03 +000083 gWakeLockMap[id] = nullptr;
Tri Voe0a9c902018-08-28 13:58:01 -070084 return 0;
85 }
86 return -1;
87}
Tri Vo934f1752019-06-26 10:34:28 -070088
89namespace android {
90namespace wakelock {
91
92class WakeLock::WakeLockImpl {
93 public:
94 WakeLockImpl(const std::string& name);
95 ~WakeLockImpl();
Kalesh Singhd8da2af2021-02-22 12:59:17 -050096 bool acquireOk();
Tri Vo934f1752019-06-26 10:34:28 -070097
98 private:
Kalesh Singhb64a38e2021-07-20 23:20:03 +000099 std::shared_ptr<IWakeLock> mWakeLock;
Tri Vo934f1752019-06-26 10:34:28 -0700100};
101
Kalesh Singhd8da2af2021-02-22 12:59:17 -0500102std::optional<WakeLock> WakeLock::tryGet(const std::string& name) {
103 std::unique_ptr<WakeLockImpl> wlImpl = std::make_unique<WakeLockImpl>(name);
104 if (wlImpl->acquireOk()) {
105 return { std::move(wlImpl) };
106 } else {
107 LOG(ERROR) << "Failed to acquire wakelock: " << name;
108 return {};
109 }
110}
111
112WakeLock::WakeLock(std::unique_ptr<WakeLockImpl> wlImpl) : mImpl(std::move(wlImpl)) {}
Tri Vo934f1752019-06-26 10:34:28 -0700113
114WakeLock::~WakeLock() = default;
115
116WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
Kalesh Singhb64a38e2021-07-20 23:20:03 +0000117 const auto suspendService = getSystemSuspendServiceOnce();
Kalesh Singh06c0d042021-08-04 15:14:34 +0000118 if (!suspendService) {
119 LOG(ERROR) << "Failed to get SystemSuspend service";
120 return;
121 }
122
Kalesh Singhb64a38e2021-07-20 23:20:03 +0000123 std::shared_ptr<IWakeLock> wl = nullptr;
124 auto status = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name, &wl);
125 // It's possible that during device shutdown SystemSuspend service has already exited.
126 // Check that the wakelock object is not null.
127 if (!wl) {
128 LOG(ERROR) << "ISuspendService::acquireWakeLock() call failed: " << status.getDescription();
Kalesh Singhd8da2af2021-02-22 12:59:17 -0500129 } else {
Kalesh Singhb64a38e2021-07-20 23:20:03 +0000130 mWakeLock = wl;
Kalesh Singhd8da2af2021-02-22 12:59:17 -0500131 }
Tri Vo934f1752019-06-26 10:34:28 -0700132}
133
134WakeLock::WakeLockImpl::~WakeLockImpl() {
Kalesh Singhd8da2af2021-02-22 12:59:17 -0500135 if (!acquireOk()) {
136 return;
137 }
Kalesh Singhb64a38e2021-07-20 23:20:03 +0000138 auto status = mWakeLock->release();
139 if (!status.isOk()) {
140 LOG(ERROR) << "IWakeLock::release() call failed: " << status.getDescription();
Tri Vo934f1752019-06-26 10:34:28 -0700141 }
142}
143
Kalesh Singhd8da2af2021-02-22 12:59:17 -0500144bool WakeLock::WakeLockImpl::acquireOk() {
145 return mWakeLock != nullptr;
146}
147
Tri Vo934f1752019-06-26 10:34:28 -0700148} // namespace wakelock
149} // namespace android