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 getSystemSuspendServiceOnce()38static const sp<ISystemSuspend>& getSystemSuspendServiceOnce() { 39 static sp<ISystemSuspend> suspendService = ISystemSuspend::getService(); 40 return suspendService; 41 } 42 acquire_wake_lock(int,const char * id)43int acquire_wake_lock(int, const char* id) { 44 ATRACE_CALL(); 45 const auto& suspendService = getSystemSuspendServiceOnce(); 46 if (!suspendService) { 47 return -1; 48 } 49 50 std::lock_guard<std::mutex> l{gLock}; 51 if (!gWakeLockMap[id]) { 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 } 61 } 62 return 0; 63 } 64 release_wake_lock(const char * id)65int release_wake_lock(const char* id) { 66 ATRACE_CALL(); 67 std::lock_guard<std::mutex> l{gLock}; 68 if (gWakeLockMap[id]) { 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(); 76 return 0; 77 } 78 return -1; 79 } 80