From e0a9c90bf5ab1ba58b7435a602713227fb052e4a Mon Sep 17 00:00:00 2001 From: Tri Vo Date: Tue, 28 Aug 2018 13:58:01 -0700 Subject: [PATCH] libpower: acquire wake locks from SystemSuspend service. This change routes libpower and libhardware_legacy to SystemSuspend service for wake locks instead of /sys/power/wake_[un]lock. Bug: 78888165 Bug: 115946999 Test: /sys/kernel/debug/suspend_stats shows that device suspends if left alone Change-Id: Id4d392e13d06be6d86e3112dedeb6a57bb24c06f --- Android.bp | 20 +++------ power.c | 124 ----------------------------------------------------- power.cpp | 72 +++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 138 deletions(-) delete mode 100644 power.c create mode 100644 power.cpp diff --git a/Android.bp b/Android.bp index 066afcf..31fd49f 100644 --- a/Android.bp +++ b/Android.bp @@ -1,9 +1,5 @@ // Copyright 2006 The Android Open Source Project -subdirs = [ - "audio", -] - cc_library_headers { name: "libhardware_legacy_headers", vendor_available: true, @@ -15,11 +11,10 @@ cc_library_headers { cc_library { name: "libpower", - - srcs: ["power.c"], - cflags: ["-Wall", "-Werror"], + defaults: ["system_suspend_defaults"], + srcs: ["power.cpp"], export_include_dirs: ["include"], - shared_libs: ["libcutils", "liblog"], + shared_libs: ["android.system.suspend@1.0"], vendor_available: true, vndk: { enabled: true, @@ -28,16 +23,15 @@ cc_library { cc_library_shared { name: "libhardware_legacy", + defaults: ["system_suspend_defaults"], vendor_available: true, vndk: { enabled: true, }, shared_libs: [ - "libbase", + "android.system.suspend@1.0", "libdl", - "libcutils", - "liblog", ], header_libs: [ @@ -51,12 +45,10 @@ cc_library_shared { "-DQEMU_HARDWARE", "-Wall", "-Werror", - "-Wno-unused-parameter", - "-Wno-gnu-designator", ], srcs: [ - "power.c", + "power.cpp", "uevent.c", ], } diff --git a/power.c b/power.c deleted file mode 100644 index fb60428..0000000 --- a/power.c +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define LOG_TAG "power" -#include - -enum { - ACQUIRE_PARTIAL_WAKE_LOCK = 0, - RELEASE_WAKE_LOCK, - OUR_FD_COUNT -}; - -const char * const OLD_PATHS[] = { - "/sys/android_power/acquire_partial_wake_lock", - "/sys/android_power/release_wake_lock", -}; - -const char * const NEW_PATHS[] = { - "/sys/power/wake_lock", - "/sys/power/wake_unlock", -}; - -//XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT; -static int g_initialized = 0; -static int g_fds[OUR_FD_COUNT]; -static int g_error = -1; - -static int -open_file_descriptors(const char * const paths[]) -{ - int i; - for (i=0; i +#include +#include +#include + +#include +#include +#include +#include + +using android::sp; +using android::system::suspend::V1_0::ISystemSuspend; +using android::system::suspend::V1_0::IWakeLock; +using android::system::suspend::V1_0::WakeLockType; + +static std::mutex gLock; +static std::unordered_map> gWakeLockMap; + +static sp getSystemSuspendServiceOnce() { + static std::once_flag initFlag; + static sp suspendService = nullptr; + std::call_once(initFlag, []() { + // It's possible for the calling process to not have permissions to + // ISystemSuspend. getService will then return nullptr. + suspendService = ISystemSuspend::getService(); + }); + return suspendService; +} + +int acquire_wake_lock(int, const char* id) { + ATRACE_CALL(); + sp suspendService = getSystemSuspendServiceOnce(); + if (!suspendService) { + return -1; + } + + std::lock_guard l{gLock}; + if (!gWakeLockMap[id]) { + gWakeLockMap[id] = suspendService->acquireWakeLock(WakeLockType::PARTIAL, id); + } + return 0; +} + +int release_wake_lock(const char* id) { + ATRACE_CALL(); + std::lock_guard l{gLock}; + if (gWakeLockMap[id]) { + gWakeLockMap[id]->release(); + gWakeLockMap[id] = nullptr; + return 0; + } + return -1; +}