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 <android-base/logging.h> 24 #include <android/system/suspend/1.0/ISystemSuspend.h> 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 getSystemSuspendServiceOnce()40static const sp<ISystemSuspend>& getSystemSuspendServiceOnce() { 41 static sp<ISystemSuspend> suspendService = ISystemSuspend::getService(); 42 return suspendService; 43 } 44 acquire_wake_lock(int,const char * id)45int acquire_wake_lock(int, const char* id) { 46 ATRACE_CALL(); 47 const auto& suspendService = getSystemSuspendServiceOnce(); 48 if (!suspendService) { 49 LOG(ERROR) << "ISystemSuspend::getService() failed."; 50 return -1; 51 } 52 53 std::lock_guard<std::mutex> l{gLock}; 54 if (!gWakeLockMap[id]) { 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 } 64 } 65 return 0; 66 } 67 release_wake_lock(const char * id)68int release_wake_lock(const char* id) { 69 ATRACE_CALL(); 70 std::lock_guard<std::mutex> l{gLock}; 71 if (gWakeLockMap[id]) { 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(); 79 return 0; 80 } 81 return -1; 82 } 83 84 namespace android { 85 namespace wakelock { 86 87 class WakeLock::WakeLockImpl { 88 public: 89 WakeLockImpl(const std::string& name); 90 ~WakeLockImpl(); 91 bool acquireOk(); 92 93 private: 94 sp<IWakeLock> mWakeLock; 95 }; 96 tryGet(const std::string & name)97std::optional<WakeLock> WakeLock::tryGet(const std::string& name) { 98 std::unique_ptr<WakeLockImpl> wlImpl = std::make_unique<WakeLockImpl>(name); 99 if (wlImpl->acquireOk()) { 100 return { std::move(wlImpl) }; 101 } else { 102 LOG(ERROR) << "Failed to acquire wakelock: " << name; 103 return {}; 104 } 105 } 106 WakeLock(std::unique_ptr<WakeLockImpl> wlImpl)107WakeLock::WakeLock(std::unique_ptr<WakeLockImpl> wlImpl) : mImpl(std::move(wlImpl)) {} 108 109 WakeLock::~WakeLock() = default; 110 WakeLockImpl(const std::string & name)111WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) { 112 static sp<ISystemSuspend> suspendService = ISystemSuspend::getService(); 113 auto ret = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name); 114 // It's possible that during device SystemSuspend service is not avaiable. In these 115 // situations HIDL calls to it will result in a DEAD_OBJECT transaction error. 116 if (ret.isDeadObject()) { 117 LOG(ERROR) << "ISuspendService::acquireWakeLock() call failed: " << ret.description(); 118 } else { 119 mWakeLock = ret; 120 } 121 } 122 ~WakeLockImpl()123WakeLock::WakeLockImpl::~WakeLockImpl() { 124 if (!acquireOk()) { 125 return; 126 } 127 auto ret = mWakeLock->release(); 128 if (!ret.isOk()) { 129 LOG(ERROR) << "IWakeLock::release() call failed: " << ret.description(); 130 } 131 } 132 acquireOk()133bool WakeLock::WakeLockImpl::acquireOk() { 134 return mWakeLock != nullptr; 135 } 136 137 } // namespace wakelock 138 } // namespace android 139