blob: 16ed961af8395aac3982caaace1a1c3a0f2dfcbd [file] [log] [blame]
Tri Voe0a9c902018-08-28 13:58:01 -07001/*
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 Vo934f1752019-06-26 10:34:28 -070020#include <hardware_legacy/power.h>
21#include <wakelock/wakelock.h>
22
Tri Voe0a9c902018-08-28 13:58:01 -070023#include <android-base/logging.h>
24#include <android/system/suspend/1.0/ISystemSuspend.h>
Tri Voe0a9c902018-08-28 13:58:01 -070025#include <utils/Trace.h>
26
27#include <mutex>
28#include <string>
29#include <thread>
30#include <unordered_map>
31
32using android::sp;
33using android::system::suspend::V1_0::ISystemSuspend;
34using android::system::suspend::V1_0::IWakeLock;
35using android::system::suspend::V1_0::WakeLockType;
36
37static std::mutex gLock;
38static std::unordered_map<std::string, sp<IWakeLock>> gWakeLockMap;
39
Tri Voe1a9a632018-10-22 13:29:24 -070040static const sp<ISystemSuspend>& getSystemSuspendServiceOnce() {
Tri Voca0b45a2018-11-27 17:56:56 -080041 static sp<ISystemSuspend> suspendService = ISystemSuspend::getService();
Tri Voe0a9c902018-08-28 13:58:01 -070042 return suspendService;
43}
44
45int acquire_wake_lock(int, const char* id) {
46 ATRACE_CALL();
Tri Voe1a9a632018-10-22 13:29:24 -070047 const auto& suspendService = getSystemSuspendServiceOnce();
Tri Voe0a9c902018-08-28 13:58:01 -070048 if (!suspendService) {
Tri Vo3714b982019-05-31 11:48:13 -070049 LOG(ERROR) << "ISystemSuspend::getService() failed.";
Tri Voe0a9c902018-08-28 13:58:01 -070050 return -1;
51 }
52
53 std::lock_guard<std::mutex> l{gLock};
54 if (!gWakeLockMap[id]) {
Tri Vo631215c2018-12-04 16:21:12 -080055 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 Voe0a9c902018-08-28 13:58:01 -070064 }
65 return 0;
66}
67
68int release_wake_lock(const char* id) {
69 ATRACE_CALL();
70 std::lock_guard<std::mutex> l{gLock};
71 if (gWakeLockMap[id]) {
Tri Vo8eb59a02018-10-14 16:09:51 -070072 // 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 Voe0a9c902018-08-28 13:58:01 -070079 return 0;
80 }
81 return -1;
82}
Tri Vo934f1752019-06-26 10:34:28 -070083
84namespace android {
85namespace wakelock {
86
87class WakeLock::WakeLockImpl {
88 public:
89 WakeLockImpl(const std::string& name);
90 ~WakeLockImpl();
91
92 private:
93 sp<IWakeLock> mWakeLock;
94};
95
96WakeLock::WakeLock(const std::string& name) : mImpl(std::make_unique<WakeLockImpl>(name)) {}
97
98WakeLock::~WakeLock() = default;
99
100WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
101 static sp<ISystemSuspend> suspendService = ISystemSuspend::getService();
102 mWakeLock = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name);
103}
104
105WakeLock::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