diff options
author | Mingguang Xu <mingguangxu@google.com> | 2021-11-04 21:20:06 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-11-04 21:20:06 +0000 |
commit | 6bba2371c34c663b9368e68766f84a5e084739f0 (patch) | |
tree | 8959e32ec52e2c6f6ed897eb35db17b84e999169 | |
parent | 126699071c6669371065fe566bda1b133a162222 (diff) | |
parent | 4bc778789ca6e25d2ec9f94cf55ad7eb398e0f12 (diff) | |
download | bt-6bba2371c34c663b9368e68766f84a5e084739f0.tar.gz |
Merge "BTAA: Do not register callback repeatedly in the same process"
-rw-r--r-- | gd/btaa/android/activity_attribution.cc | 73 |
1 files changed, 53 insertions, 20 deletions
diff --git a/gd/btaa/android/activity_attribution.cc b/gd/btaa/android/activity_attribution.cc index 34d4a3691..19c0df11f 100644 --- a/gd/btaa/android/activity_attribution.cc +++ b/gd/btaa/android/activity_attribution.cc @@ -46,41 +46,62 @@ static const std::string kBtWakelockName("hal_bluetooth_lock"); static const std::string kBtWakeupReason("hs_uart_wakeup"); static const size_t kHciAclHeaderSize = 4; +static std::mutex g_module_mutex; +static ActivityAttribution* g_module = nullptr; +static bool is_wakeup_callback_registered = false; +static bool is_wakelock_callback_registered = false; + struct wakelock_callback : public BnWakelockCallback { - wakelock_callback(ActivityAttribution* module) : module_(module) {} + wakelock_callback() {} Status notifyAcquired() override { - module_->OnWakelockAcquired(); + std::lock_guard<std::mutex> guard(g_module_mutex); + if (g_module != nullptr) { + g_module->OnWakelockAcquired(); + } return Status::ok(); } Status notifyReleased() override { - module_->OnWakelockReleased(); + std::lock_guard<std::mutex> guard(g_module_mutex); + if (g_module != nullptr) { + g_module->OnWakelockReleased(); + } return Status::ok(); } - - ActivityAttribution* module_; }; +static std::shared_ptr<wakelock_callback> g_wakelock_callback = nullptr; + struct wakeup_callback : public BnSuspendCallback { - wakeup_callback(ActivityAttribution* module) : module_(module) {} + wakeup_callback() {} Status notifyWakeup(bool success, const std::vector<std::string>& wakeup_reasons) override { for (auto& wakeup_reason : wakeup_reasons) { if (wakeup_reason.find(kBtWakeupReason) != std::string::npos) { - module_->OnWakeup(); + std::lock_guard<std::mutex> guard(g_module_mutex); + if (g_module != nullptr) { + g_module->OnWakeup(); + } break; } } return Status::ok(); } - - ActivityAttribution* module_; }; +static std::shared_ptr<wakeup_callback> g_wakeup_callback = nullptr; + struct ActivityAttribution::impl { impl(ActivityAttribution* module) { - bool is_registered = false; + std::lock_guard<std::mutex> guard(g_module_mutex); + g_module = module; + if (is_wakeup_callback_registered && is_wakelock_callback_registered) { + LOG_ERROR("Wakeup and wakelock callbacks are already registered"); + return; + } + Status register_callback_status; + bool is_register_successful = false; auto control_service = ISuspendControlService::fromBinder(SpAIBinder(AServiceManager_getService("suspend_control"))); if (!control_service) { @@ -88,21 +109,33 @@ struct ActivityAttribution::impl { return; } - Status register_callback_status = - control_service->registerCallback(SharedRefBase::make<wakeup_callback>(module), &is_registered); - if (!is_registered || !register_callback_status.isOk()) { - LOG_ERROR("Fail to register wakeup callback"); - return; + if (!is_wakeup_callback_registered) { + g_wakeup_callback = SharedRefBase::make<wakeup_callback>(); + register_callback_status = control_service->registerCallback(g_wakeup_callback, &is_register_successful); + if (!is_register_successful || !register_callback_status.isOk()) { + LOG_ERROR("Fail to register wakeup callback"); + return; + } + is_wakeup_callback_registered = true; } - register_callback_status = control_service->registerWakelockCallback( - SharedRefBase::make<wakelock_callback>(module), kBtWakelockName, &is_registered); - if (!is_registered || !register_callback_status.isOk()) { - LOG_ERROR("Fail to register wakelock callback"); - return; + if (!is_wakelock_callback_registered) { + g_wakelock_callback = SharedRefBase::make<wakelock_callback>(); + register_callback_status = + control_service->registerWakelockCallback(g_wakelock_callback, kBtWakelockName, &is_register_successful); + if (!is_register_successful || !register_callback_status.isOk()) { + LOG_ERROR("Fail to register wakelock callback"); + return; + } + is_wakelock_callback_registered = true; } } + ~impl() { + std::lock_guard<std::mutex> guard(g_module_mutex); + g_module = nullptr; + } + void on_hci_packet(hal::HciPacket packet, hal::SnoopLogger::PacketType type, uint16_t length) { attribution_processor_.OnBtaaPackets(std::move(hci_processor_.OnHciPacket(std::move(packet), type, length))); } |