Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 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 | |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 20 | #include <hardware_legacy/power.h> |
| 21 | #include <wakelock/wakelock.h> |
| 22 | |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 23 | #include <aidl/android/system/suspend/ISystemSuspend.h> |
| 24 | #include <aidl/android/system/suspend/IWakeLock.h> |
| 25 | #include <android/binder_manager.h> |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 27 | #include <utils/Trace.h> |
| 28 | |
| 29 | #include <mutex> |
| 30 | #include <string> |
| 31 | #include <thread> |
| 32 | #include <unordered_map> |
| 33 | |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 34 | using aidl::android::system::suspend::ISystemSuspend; |
| 35 | using aidl::android::system::suspend::IWakeLock; |
| 36 | using aidl::android::system::suspend::WakeLockType; |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 37 | |
| 38 | static std::mutex gLock; |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 39 | static std::unordered_map<std::string, std::shared_ptr<IWakeLock>> gWakeLockMap; |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 40 | |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 41 | static const std::shared_ptr<ISystemSuspend> getSystemSuspendServiceOnce() { |
| 42 | static std::shared_ptr<ISystemSuspend> suspendService = |
Kalesh Singh | 06c0d04 | 2021-08-04 15:14:34 +0000 | [diff] [blame] | 43 | ISystemSuspend::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService( |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 44 | (ISystemSuspend::descriptor + std::string("/default")).c_str()))); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 45 | return suspendService; |
| 46 | } |
| 47 | |
| 48 | int acquire_wake_lock(int, const char* id) { |
| 49 | ATRACE_CALL(); |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 50 | const auto suspendService = getSystemSuspendServiceOnce(); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 51 | if (!suspendService) { |
Kalesh Singh | 06c0d04 | 2021-08-04 15:14:34 +0000 | [diff] [blame] | 52 | LOG(ERROR) << "Failed to get SystemSuspend service"; |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | std::lock_guard<std::mutex> l{gLock}; |
| 57 | if (!gWakeLockMap[id]) { |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 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(); |
Tri Vo | 631215c | 2018-12-04 16:21:12 -0800 | [diff] [blame] | 65 | return -1; |
| 66 | } else { |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 67 | gWakeLockMap[id] = wl; |
Tri Vo | 631215c | 2018-12-04 16:21:12 -0800 | [diff] [blame] | 68 | } |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int release_wake_lock(const char* id) { |
| 74 | ATRACE_CALL(); |
| 75 | std::lock_guard<std::mutex> l{gLock}; |
| 76 | if (gWakeLockMap[id]) { |
Tri Vo | 8eb59a0 | 2018-10-14 16:09:51 -0700 | [diff] [blame] | 77 | // Ignore errors on release() call since hwbinder driver will clean up the underlying object |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 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(); |
Tri Vo | 8eb59a0 | 2018-10-14 16:09:51 -0700 | [diff] [blame] | 82 | } |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 83 | gWakeLockMap[id] = nullptr; |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | return -1; |
| 87 | } |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 88 | |
| 89 | namespace android { |
| 90 | namespace wakelock { |
| 91 | |
| 92 | class WakeLock::WakeLockImpl { |
| 93 | public: |
| 94 | WakeLockImpl(const std::string& name); |
| 95 | ~WakeLockImpl(); |
Kalesh Singh | d8da2af | 2021-02-22 12:59:17 -0500 | [diff] [blame] | 96 | bool acquireOk(); |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 97 | |
| 98 | private: |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 99 | std::shared_ptr<IWakeLock> mWakeLock; |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Kalesh Singh | d8da2af | 2021-02-22 12:59:17 -0500 | [diff] [blame] | 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 | |
| 112 | WakeLock::WakeLock(std::unique_ptr<WakeLockImpl> wlImpl) : mImpl(std::move(wlImpl)) {} |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 113 | |
| 114 | WakeLock::~WakeLock() = default; |
| 115 | |
| 116 | WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) { |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 117 | const auto suspendService = getSystemSuspendServiceOnce(); |
Kalesh Singh | 06c0d04 | 2021-08-04 15:14:34 +0000 | [diff] [blame] | 118 | if (!suspendService) { |
| 119 | LOG(ERROR) << "Failed to get SystemSuspend service"; |
| 120 | return; |
| 121 | } |
| 122 | |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 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(); |
Kalesh Singh | d8da2af | 2021-02-22 12:59:17 -0500 | [diff] [blame] | 129 | } else { |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 130 | mWakeLock = wl; |
Kalesh Singh | d8da2af | 2021-02-22 12:59:17 -0500 | [diff] [blame] | 131 | } |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | WakeLock::WakeLockImpl::~WakeLockImpl() { |
Kalesh Singh | d8da2af | 2021-02-22 12:59:17 -0500 | [diff] [blame] | 135 | if (!acquireOk()) { |
| 136 | return; |
| 137 | } |
Kalesh Singh | b64a38e | 2021-07-20 23:20:03 +0000 | [diff] [blame] | 138 | auto status = mWakeLock->release(); |
| 139 | if (!status.isOk()) { |
| 140 | LOG(ERROR) << "IWakeLock::release() call failed: " << status.getDescription(); |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Kalesh Singh | d8da2af | 2021-02-22 12:59:17 -0500 | [diff] [blame] | 144 | bool WakeLock::WakeLockImpl::acquireOk() { |
| 145 | return mWakeLock != nullptr; |
| 146 | } |
| 147 | |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 148 | } // namespace wakelock |
| 149 | } // namespace android |