1 /*
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
20 #include <hardware_legacy/power.h>
21 #include <wakelock/wakelock.h>
22
23 #include <aidl/android/system/suspend/ISystemSuspend.h>
24 #include <aidl/android/system/suspend/IWakeLock.h>
25 #include <android/binder_manager.h>
26 #include <android-base/logging.h>
27 #include <utils/Trace.h>
28
29 #include <mutex>
30 #include <string>
31 #include <thread>
32 #include <unordered_map>
33
34 using aidl::android::system::suspend::ISystemSuspend;
35 using aidl::android::system::suspend::IWakeLock;
36 using aidl::android::system::suspend::WakeLockType;
37
38 static std::mutex gLock;
39 static std::unordered_map<std::string, std::shared_ptr<IWakeLock>> gWakeLockMap;
40
getSystemSuspendServiceOnce()41 static const std::shared_ptr<ISystemSuspend> getSystemSuspendServiceOnce() {
42 static std::shared_ptr<ISystemSuspend> suspendService =
43 []() -> std::shared_ptr<ISystemSuspend> {
44 std::string suspendServiceName =
45 ISystemSuspend::descriptor + std::string("/default");
46 if (!AServiceManager_isDeclared(suspendServiceName.c_str())) {
47 return nullptr;
48 }
49 return ISystemSuspend::fromBinder(ndk::SpAIBinder(
50 AServiceManager_waitForService(suspendServiceName.c_str())));
51 }();
52 return suspendService;
53 }
54
acquire_wake_lock(int,const char * id)55 int acquire_wake_lock(int, const char* id) {
56 ATRACE_CALL();
57 const auto suspendService = getSystemSuspendServiceOnce();
58 if (!suspendService) {
59 LOG(ERROR) << "Failed to get SystemSuspend service";
60 return -1;
61 }
62
63 #ifdef ENABLE_NO_LOCK_BINDER_TXN
64 bool hasLock;
65 {
66 std::lock_guard<std::mutex> l{gLock};
67 hasLock = !!gWakeLockMap[id];
68 }
69 if (hasLock)
70 return 0;
71
72 std::shared_ptr<IWakeLock> wl = nullptr;
73 auto status =
74 suspendService->acquireWakeLock(WakeLockType::PARTIAL, id, &wl);
75 // It's possible that during device shutdown SystemSuspend service has
76 // already exited. Check that the wakelock object is not null.
77 if (!wl) {
78 LOG(ERROR) << "ISuspendService::acquireWakeLock() call failed: "
79 << status.getDescription();
80 return -1;
81 }
82 {
83 std::lock_guard<std::mutex> l{gLock};
84 // Check if another thread has already set it.
85 if (!gWakeLockMap[id]) gWakeLockMap[id] = wl;
86 }
87 #else
88 std::lock_guard<std::mutex> l{gLock};
89 if (!gWakeLockMap[id]) {
90 std::shared_ptr<IWakeLock> wl = nullptr;
91 auto status = suspendService->acquireWakeLock(WakeLockType::PARTIAL, id, &wl);
92 // It's possible that during device shutdown SystemSuspend service has already exited.
93 // Check that the wakelock object is not null.
94 if (!wl) {
95 LOG(ERROR) << "ISuspendService::acquireWakeLock() call failed: "
96 << status.getDescription();
97 return -1;
98 }
99 gWakeLockMap[id] = wl;
100 }
101 #endif
102 return 0;
103 }
104
release_wake_lock(const char * id)105 int release_wake_lock(const char* id) {
106 ATRACE_CALL();
107 std::lock_guard<std::mutex> l{gLock};
108 if (gWakeLockMap[id]) {
109 // Ignore errors on release() call since hwbinder driver will clean up the underlying object
110 // once we clear the corresponding shared_ptr.
111 auto status = gWakeLockMap[id]->release();
112 if (!status.isOk()) {
113 LOG(ERROR) << "IWakeLock::release() call failed: " << status.getDescription();
114 }
115 gWakeLockMap[id] = nullptr;
116 return 0;
117 }
118 return -1;
119 }
120
121 namespace android {
122 namespace wakelock {
123
124 class WakeLock::WakeLockImpl {
125 public:
126 WakeLockImpl(const std::string& name);
127 ~WakeLockImpl();
128 bool acquireOk();
129
130 private:
131 std::shared_ptr<IWakeLock> mWakeLock;
132 };
133
tryGet(const std::string & name)134 std::optional<WakeLock> WakeLock::tryGet(const std::string& name) {
135 std::unique_ptr<WakeLockImpl> wlImpl = std::make_unique<WakeLockImpl>(name);
136 if (wlImpl->acquireOk()) {
137 return { std::move(wlImpl) };
138 } else {
139 LOG(ERROR) << "Failed to acquire wakelock: " << name;
140 return {};
141 }
142 }
143
WakeLock(std::unique_ptr<WakeLockImpl> wlImpl)144 WakeLock::WakeLock(std::unique_ptr<WakeLockImpl> wlImpl) : mImpl(std::move(wlImpl)) {}
145
146 WakeLock::~WakeLock() = default;
147
WakeLockImpl(const std::string & name)148 WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
149 const auto suspendService = getSystemSuspendServiceOnce();
150 if (!suspendService) {
151 LOG(ERROR) << "Failed to get SystemSuspend service";
152 return;
153 }
154
155 std::shared_ptr<IWakeLock> wl = nullptr;
156 auto status = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name, &wl);
157 // It's possible that during device shutdown SystemSuspend service has already exited.
158 // Check that the wakelock object is not null.
159 if (!wl) {
160 LOG(ERROR) << "ISuspendService::acquireWakeLock() call failed: " << status.getDescription();
161 } else {
162 mWakeLock = wl;
163 }
164 }
165
~WakeLockImpl()166 WakeLock::WakeLockImpl::~WakeLockImpl() {
167 if (!acquireOk()) {
168 return;
169 }
170 auto status = mWakeLock->release();
171 if (!status.isOk()) {
172 LOG(ERROR) << "IWakeLock::release() call failed: " << status.getDescription();
173 }
174 }
175
acquireOk()176 bool WakeLock::WakeLockImpl::acquireOk() {
177 return mWakeLock != nullptr;
178 }
179
180 } // namespace wakelock
181 } // namespace android
182