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 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android/system/suspend/1.0/ISystemSuspend.h> |
| 22 | #include <hardware_legacy/power.h> |
| 23 | #include <utils/Trace.h> |
| 24 | |
| 25 | #include <mutex> |
| 26 | #include <string> |
| 27 | #include <thread> |
| 28 | #include <unordered_map> |
| 29 | |
| 30 | using android::sp; |
| 31 | using android::system::suspend::V1_0::ISystemSuspend; |
| 32 | using android::system::suspend::V1_0::IWakeLock; |
| 33 | using android::system::suspend::V1_0::WakeLockType; |
| 34 | |
| 35 | static std::mutex gLock; |
| 36 | static std::unordered_map<std::string, sp<IWakeLock>> gWakeLockMap; |
| 37 | |
Tri Vo | e1a9a63 | 2018-10-22 13:29:24 -0700 | [diff] [blame] | 38 | static const sp<ISystemSuspend>& getSystemSuspendServiceOnce() { |
Tri Vo | ca0b45a | 2018-11-27 17:56:56 -0800 | [diff] [blame] | 39 | static sp<ISystemSuspend> suspendService = ISystemSuspend::getService(); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 40 | return suspendService; |
| 41 | } |
| 42 | |
| 43 | int acquire_wake_lock(int, const char* id) { |
| 44 | ATRACE_CALL(); |
Tri Vo | e1a9a63 | 2018-10-22 13:29:24 -0700 | [diff] [blame] | 45 | const auto& suspendService = getSystemSuspendServiceOnce(); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 46 | if (!suspendService) { |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | std::lock_guard<std::mutex> l{gLock}; |
| 51 | if (!gWakeLockMap[id]) { |
Tri Vo | 631215c | 2018-12-04 16:21:12 -0800 | [diff] [blame] | 52 | auto ret = suspendService->acquireWakeLock(WakeLockType::PARTIAL, id); |
| 53 | // It's possible that during device shutdown SystemSuspend service has already exited. In |
| 54 | // these situations HIDL calls to it will result in a DEAD_OBJECT transaction error. We |
| 55 | // check for DEAD_OBJECT so that libpower clients can shutdown cleanly. |
| 56 | if (ret.isDeadObject()) { |
| 57 | return -1; |
| 58 | } else { |
| 59 | gWakeLockMap[id] = ret; |
| 60 | } |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int release_wake_lock(const char* id) { |
| 66 | ATRACE_CALL(); |
| 67 | std::lock_guard<std::mutex> l{gLock}; |
| 68 | if (gWakeLockMap[id]) { |
Tri Vo | 8eb59a0 | 2018-10-14 16:09:51 -0700 | [diff] [blame] | 69 | // Ignore errors on release() call since hwbinder driver will clean up the underlying object |
| 70 | // once we clear the corresponding strong pointer. |
| 71 | auto ret = gWakeLockMap[id]->release(); |
| 72 | if (!ret.isOk()) { |
| 73 | LOG(ERROR) << "IWakeLock::release() call failed: " << ret.description(); |
| 74 | } |
| 75 | gWakeLockMap[id].clear(); |
Tri Vo | e0a9c90 | 2018-08-28 13:58:01 -0700 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | return -1; |
| 79 | } |