blob: e9b6ccd959a7e9a950de32481fe72248d711bc97 [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
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
30using android::sp;
31using android::system::suspend::V1_0::ISystemSuspend;
32using android::system::suspend::V1_0::IWakeLock;
33using android::system::suspend::V1_0::WakeLockType;
34
35static std::mutex gLock;
36static std::unordered_map<std::string, sp<IWakeLock>> gWakeLockMap;
37
Tri Voe1a9a632018-10-22 13:29:24 -070038static const sp<ISystemSuspend>& getSystemSuspendServiceOnce() {
Tri Voca0b45a2018-11-27 17:56:56 -080039 static sp<ISystemSuspend> suspendService = ISystemSuspend::getService();
Tri Voe0a9c902018-08-28 13:58:01 -070040 return suspendService;
41}
42
43int acquire_wake_lock(int, const char* id) {
44 ATRACE_CALL();
Tri Voe1a9a632018-10-22 13:29:24 -070045 const auto& suspendService = getSystemSuspendServiceOnce();
Tri Voe0a9c902018-08-28 13:58:01 -070046 if (!suspendService) {
47 return -1;
48 }
49
50 std::lock_guard<std::mutex> l{gLock};
51 if (!gWakeLockMap[id]) {
Tri Vo631215c2018-12-04 16:21:12 -080052 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 Voe0a9c902018-08-28 13:58:01 -070061 }
62 return 0;
63}
64
65int release_wake_lock(const char* id) {
66 ATRACE_CALL();
67 std::lock_guard<std::mutex> l{gLock};
68 if (gWakeLockMap[id]) {
Tri Vo8eb59a02018-10-14 16:09:51 -070069 // 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 Voe0a9c902018-08-28 13:58:01 -070076 return 0;
77 }
78 return -1;
79}