• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         ISystemSuspend::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(
44             (ISystemSuspend::descriptor + std::string("/default")).c_str())));
45     return suspendService;
46 }
47 
acquire_wake_lock(int,const char * id)48 int acquire_wake_lock(int, const char* id) {
49     ATRACE_CALL();
50     const auto suspendService = getSystemSuspendServiceOnce();
51     if (!suspendService) {
52         LOG(ERROR) << "Failed to get SystemSuspend service";
53         return -1;
54     }
55 
56     std::lock_guard<std::mutex> l{gLock};
57     if (!gWakeLockMap[id]) {
58         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();
65             return -1;
66         } else {
67             gWakeLockMap[id] = wl;
68         }
69     }
70     return 0;
71 }
72 
release_wake_lock(const char * id)73 int release_wake_lock(const char* id) {
74     ATRACE_CALL();
75     std::lock_guard<std::mutex> l{gLock};
76     if (gWakeLockMap[id]) {
77         // Ignore errors on release() call since hwbinder driver will clean up the underlying object
78         // 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();
82         }
83         gWakeLockMap[id] = nullptr;
84         return 0;
85     }
86     return -1;
87 }
88 
89 namespace android {
90 namespace wakelock {
91 
92 class WakeLock::WakeLockImpl {
93   public:
94     WakeLockImpl(const std::string& name);
95     ~WakeLockImpl();
96     bool acquireOk();
97 
98   private:
99     std::shared_ptr<IWakeLock> mWakeLock;
100 };
101 
tryGet(const std::string & name)102 std::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 
WakeLock(std::unique_ptr<WakeLockImpl> wlImpl)112 WakeLock::WakeLock(std::unique_ptr<WakeLockImpl> wlImpl) : mImpl(std::move(wlImpl)) {}
113 
114 WakeLock::~WakeLock() = default;
115 
WakeLockImpl(const std::string & name)116 WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
117     const auto suspendService = getSystemSuspendServiceOnce();
118     if (!suspendService) {
119         LOG(ERROR) << "Failed to get SystemSuspend service";
120         return;
121     }
122 
123     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();
129     } else {
130         mWakeLock = wl;
131     }
132 }
133 
~WakeLockImpl()134 WakeLock::WakeLockImpl::~WakeLockImpl() {
135     if (!acquireOk()) {
136         return;
137     }
138     auto status = mWakeLock->release();
139     if (!status.isOk()) {
140         LOG(ERROR) << "IWakeLock::release() call failed: " << status.getDescription();
141     }
142 }
143 
acquireOk()144 bool WakeLock::WakeLockImpl::acquireOk() {
145     return mWakeLock != nullptr;
146 }
147 
148 }  // namespace wakelock
149 }  // namespace android
150