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()40 static const sp<ISystemSuspend>& getSystemSuspendServiceOnce() {
41     static sp<ISystemSuspend> suspendService = ISystemSuspend::getService();
42     return suspendService;
43 }
44 
acquire_wake_lock(int,const char * id)45 int 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)68 int 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 
92   private:
93     sp<IWakeLock> mWakeLock;
94 };
95 
WakeLock(const std::string & name)96 WakeLock::WakeLock(const std::string& name) : mImpl(std::make_unique<WakeLockImpl>(name)) {}
97 
98 WakeLock::~WakeLock() = default;
99 
WakeLockImpl(const std::string & name)100 WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
101     static sp<ISystemSuspend> suspendService = ISystemSuspend::getService();
102     mWakeLock = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name);
103 }
104 
~WakeLockImpl()105 WakeLock::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
114