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 | |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | #include <android/system/suspend/1.0/ISystemSuspend.h> |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 25 | #include <utils/Trace.h> |
| 26 | |
| 27 | #include <mutex> |
| 28 | #include <string> |
| 29 | #include <thread> |
| 30 | #include <unordered_map> |
| 31 | |
| 32 | using android::sp; |
| 33 | using android::system::suspend::V1_0::ISystemSuspend; |
| 34 | using android::system::suspend::V1_0::IWakeLock; |
| 35 | using android::system::suspend::V1_0::WakeLockType; |
| 36 | |
| 37 | static std::mutex gLock; |
| 38 | static std::unordered_map<std::string, sp<IWakeLock>> gWakeLockMap; |
| 39 | |
Tri Vo | e1a9a63 | 2018-10-22 13:29:24 -0700 | [diff] [blame] | 40 | static const sp<ISystemSuspend>& getSystemSuspendServiceOnce() { |
Tri Vo | ca0b45a | 2018-11-27 17:56:56 -0800 | [diff] [blame] | 41 | static sp<ISystemSuspend> suspendService = ISystemSuspend::getService(); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 42 | return suspendService; |
| 43 | } |
| 44 | |
| 45 | int acquire_wake_lock(int, const char* id) { |
| 46 | ATRACE_CALL(); |
Tri Vo | e1a9a63 | 2018-10-22 13:29:24 -0700 | [diff] [blame] | 47 | const auto& suspendService = getSystemSuspendServiceOnce(); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 48 | if (!suspendService) { |
Tri Vo | 3714b98 | 2019-05-31 11:48:13 -0700 | [diff] [blame] | 49 | LOG(ERROR) << "ISystemSuspend::getService() failed."; |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | std::lock_guard<std::mutex> l{gLock}; |
| 54 | if (!gWakeLockMap[id]) { |
Tri Vo | 631215c | 2018-12-04 16:21:12 -0800 | [diff] [blame] | 55 | auto ret = suspendService->acquireWakeLock(WakeLockType::PARTIAL, id); |
| 56 | // It's possible that during device shutdown SystemSuspend service has already exited. In |
| 57 | // these situations HIDL calls to it will result in a DEAD_OBJECT transaction error. We |
| 58 | // check for DEAD_OBJECT so that libpower clients can shutdown cleanly. |
| 59 | if (ret.isDeadObject()) { |
| 60 | return -1; |
| 61 | } else { |
| 62 | gWakeLockMap[id] = ret; |
| 63 | } |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int release_wake_lock(const char* id) { |
| 69 | ATRACE_CALL(); |
| 70 | std::lock_guard<std::mutex> l{gLock}; |
| 71 | if (gWakeLockMap[id]) { |
Tri Vo | 8eb59a0 | 2018-10-14 16:09:51 -0700 | [diff] [blame] | 72 | // Ignore errors on release() call since hwbinder driver will clean up the underlying object |
| 73 | // once we clear the corresponding strong pointer. |
| 74 | auto ret = gWakeLockMap[id]->release(); |
| 75 | if (!ret.isOk()) { |
| 76 | LOG(ERROR) << "IWakeLock::release() call failed: " << ret.description(); |
| 77 | } |
| 78 | gWakeLockMap[id].clear(); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | return -1; |
| 82 | } |
Tri Vo | 934f175 | 2019-06-26 10:34:28 -0700 | [diff] [blame] | 83 | |
| 84 | namespace android { |
| 85 | namespace wakelock { |
| 86 | |
| 87 | class WakeLock::WakeLockImpl { |
| 88 | public: |
| 89 | WakeLockImpl(const std::string& name); |
| 90 | ~WakeLockImpl(); |
| 91 | |
| 92 | private: |
| 93 | sp<IWakeLock> mWakeLock; |
| 94 | }; |
| 95 | |
| 96 | WakeLock::WakeLock(const std::string& name) : mImpl(std::make_unique<WakeLockImpl>(name)) {} |
| 97 | |
| 98 | WakeLock::~WakeLock() = default; |
| 99 | |
| 100 | WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) { |
| 101 | static sp<ISystemSuspend> suspendService = ISystemSuspend::getService(); |
| 102 | mWakeLock = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name); |
| 103 | } |
| 104 | |
| 105 | WakeLock::WakeLockImpl::~WakeLockImpl() { |
| 106 | auto ret = mWakeLock->release(); |
| 107 | if (!ret.isOk()) { |
| 108 | LOG(ERROR) << "IWakeLock::release() call failed: " << ret.description(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } // namespace wakelock |
| 113 | } // namespace android |