1 /******************************************************************************
2 *
3 * Copyright 2021 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #define LOG_TAG "BtGdWakelockNative"
20
21 #include "os/internal/wakelock_native.h"
22
23 #include <aidl/android/system/suspend/ISystemSuspend.h>
24 #include <aidl/android/system/suspend/IWakeLock.h>
25 #include <android/binder_ibinder.h>
26 #include <android/binder_manager.h>
27 #include <bluetooth/log.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include <cerrno>
32 #include <functional>
33 #include <string>
34
35 // We want the os/log.h definitions
36 #undef LOG_DEBUG
37 #undef LOG_INFO
38
39 #include "os/log.h"
40
41 // Save the os/log.h definitions
42 #pragma push_macro("LOG_DEBUG")
43 #pragma push_macro("LOG_INFO")
44
45 // Undef these to avoid conflicting with later imports
46 #undef LOG_DEBUG
47 #undef LOG_INFO
48
49 using ::aidl::android::system::suspend::ISystemSuspend;
50 using ::aidl::android::system::suspend::IWakeLock;
51 using ::aidl::android::system::suspend::WakeLockType;
52
53 namespace bluetooth {
54 namespace os {
55 namespace internal {
56
57 // Restore the os/log.h definitions after all imported headers
58 #pragma pop_macro("LOG_DEBUG")
59 #pragma pop_macro("LOG_INFO")
60
onSuspendDeath(void * cookie)61 static void onSuspendDeath(void* cookie) {
62 auto onDeath = static_cast<std::function<void(void)>*>(cookie);
63 (*onDeath)();
64 }
65
66 struct WakelockNative::Impl {
Implbluetooth::os::internal::WakelockNative::Impl67 Impl() : suspend_death_recipient(AIBinder_DeathRecipient_new(onSuspendDeath)) {}
68
__anon4c5308e80102bluetooth::os::internal::WakelockNative::Impl69 std::function<void(void)> onDeath = [this] {
70 log::error("ISystemSuspend HAL service died!");
71 this->suspend_service = nullptr;
72 };
73
74 std::shared_ptr<ISystemSuspend> suspend_service = nullptr;
75 std::shared_ptr<IWakeLock> current_wakelock = nullptr;
76 ::ndk::ScopedAIBinder_DeathRecipient suspend_death_recipient;
77 };
78
Initialize()79 void WakelockNative::Initialize() {
80 log::info("Initializing native wake locks");
81 const std::string suspendInstance = std::string() + ISystemSuspend::descriptor + "/default";
82 pimpl_->suspend_service = ISystemSuspend::fromBinder(
83 ndk::SpAIBinder(AServiceManager_waitForService(suspendInstance.c_str())));
84 log::assert_that(pimpl_->suspend_service != nullptr, "Cannot get ISystemSuspend service");
85 AIBinder_linkToDeath(
86 pimpl_->suspend_service->asBinder().get(),
87 pimpl_->suspend_death_recipient.get(),
88 static_cast<void*>(&pimpl_->onDeath));
89 }
90
Acquire(const std::string & lock_name)91 WakelockNative::StatusCode WakelockNative::Acquire(const std::string& lock_name) {
92 if (!pimpl_->suspend_service) {
93 log::error("lock not acquired, ISystemService is not available");
94 return StatusCode::NATIVE_SERVICE_NOT_AVAILABLE;
95 }
96
97 if (pimpl_->current_wakelock) {
98 log::info("wakelock is already acquired");
99 return StatusCode::SUCCESS;
100 }
101
102 auto status = pimpl_->suspend_service->acquireWakeLock(
103 WakeLockType::PARTIAL, lock_name, &pimpl_->current_wakelock);
104 if (!pimpl_->current_wakelock) {
105 log::error("wake lock not acquired: {}", status.getDescription());
106 return StatusCode::NATIVE_API_ERROR;
107 }
108
109 return StatusCode::SUCCESS;
110 }
111
Release(const std::string &)112 WakelockNative::StatusCode WakelockNative::Release(const std::string& /* lock_name */) {
113 if (!pimpl_->current_wakelock) {
114 log::warn("no lock is currently acquired");
115 return StatusCode::SUCCESS;
116 }
117 pimpl_->current_wakelock->release();
118 pimpl_->current_wakelock = nullptr;
119 return StatusCode::SUCCESS;
120 }
121
CleanUp()122 void WakelockNative::CleanUp() {
123 log::info("Cleaning up native wake locks");
124 if (pimpl_->current_wakelock) {
125 log::info("releasing current wakelock during clean up");
126 pimpl_->current_wakelock->release();
127 pimpl_->current_wakelock = nullptr;
128 }
129 if (pimpl_->suspend_service) {
130 log::info("Unlink death recipient");
131 AIBinder_unlinkToDeath(
132 pimpl_->suspend_service->asBinder().get(),
133 pimpl_->suspend_death_recipient.get(),
134 static_cast<void*>(&pimpl_->onDeath));
135 pimpl_->suspend_service = nullptr;
136 }
137 }
138
WakelockNative()139 WakelockNative::WakelockNative() : pimpl_(std::make_unique<Impl>()) {}
140
141 WakelockNative::~WakelockNative() = default;
142
143 } // namespace internal
144 } // namespace os
145 } // namespace bluetooth
146