Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cf61d9aa authored by Chris Manton's avatar Chris Manton
Browse files

Properly remove callbacks when scanning stopped

Hide the concrete handler variable behind a
virtual method to maintain a pure virtual class interface.

Bug: 143578947
Test: Inspection
Change-Id: Icbc74169979abc3ec44d12a165cfa4bc7235ec62
parent ba32e125
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ struct LeScanningManager::impl {
        break;
      case hci::SubeventCode::SCAN_TIMEOUT:
        if (registered_callback_ != nullptr) {
          registered_callback_->handler->Post(
          registered_callback_->Handler()->Post(
              common::BindOnce(&LeScanningManagerCallbacks::on_timeout, common::Unretained(registered_callback_)));
          registered_callback_ = nullptr;
        }
@@ -102,7 +102,7 @@ struct LeScanningManager::impl {
    for (const ReportStructType& report : report_vector) {
      param.push_back(std::shared_ptr<LeReport>(static_cast<LeReport*>(new ReportType(report))));
    }
    registered_callback_->handler->Post(common::BindOnce(&LeScanningManagerCallbacks::on_advertisements,
    registered_callback_->Handler()->Post(common::BindOnce(&LeScanningManagerCallbacks::on_advertisements,
                                                           common::Unretained(registered_callback_), param));
  }

@@ -159,21 +159,21 @@ struct LeScanningManager::impl {
    if (registered_callback_ == nullptr) {
      return;
    }
    registered_callback_->handler->Post(std::move(on_stopped));
    registered_callback_->Handler()->Post(std::move(on_stopped));
    switch (api_type_) {
      case ScanApiType::LE_5_0:
        le_scanning_interface_->EnqueueCommand(
            hci::LeSetExtendedScanEnableBuilder::Create(Enable::DISABLED,
                                                        FilterDuplicates::DISABLED /* filter duplicates */, 0, 0),
            common::BindOnce(impl::check_status), module_handler_);
        registered_callback_->handler = nullptr;
        registered_callback_ = nullptr;
        break;
      case ScanApiType::ANDROID_HCI:
      case ScanApiType::LE_4_0:
        le_scanning_interface_->EnqueueCommand(
            hci::LeSetScanEnableBuilder::Create(Enable::DISABLED, Enable::DISABLED /* filter duplicates */),
            common::BindOnce(impl::check_status), module_handler_);
        registered_callback_->handler = nullptr;
        registered_callback_ = nullptr;
        break;
    }
  }
+2 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ class LeScanningManagerCallbacks {
  virtual ~LeScanningManagerCallbacks() = default;
  virtual void on_advertisements(std::vector<std::shared_ptr<LeReport>>) = 0;
  virtual void on_timeout() = 0;
  os::Handler* handler;
  virtual os::Handler* Handler() = 0;
};

class LeScanningManager : public bluetooth::Module {
+8 −5
Original line number Diff line number Diff line
@@ -14,20 +14,19 @@
 * limitations under the License.
 */

#include "hci/le_scanning_manager.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <algorithm>
#include <chrono>
#include <future>
#include <map>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "common/bind.h"
#include "hci/address.h"
#include "hci/controller.h"
#include "hci/hci_layer.h"
#include "hci/le_scanning_manager.h"
#include "os/thread.h"
#include "packet/raw_builder.h"

@@ -187,7 +186,7 @@ class LeScanningManagerTest : public ::testing::Test {
    fake_registry_.InjectTestModule(&Controller::Factory, test_controller_);
    client_handler_ = fake_registry_.GetTestModuleHandler(&HciLayer::Factory);
    ASSERT_NE(client_handler_, nullptr);
    mock_callbacks_.handler = client_handler_;
    mock_callbacks_.handler_ = client_handler_;
    std::future<void> config_future = test_hci_layer_->GetCommandFuture();
    fake_registry_.Start<LeScanningManager>(&thread_);
    le_scanning_manager =
@@ -217,6 +216,10 @@ class LeScanningManagerTest : public ::testing::Test {
   public:
    MOCK_METHOD(void, on_advertisements, (std::vector<std::shared_ptr<LeReport>>), (override));
    MOCK_METHOD(void, on_timeout, (), (override));
    os::Handler* Handler() {
      return handler_;
    }
    os::Handler* handler_{nullptr};
  } mock_callbacks_;

  OpCode param_opcode_{OpCode::LE_SET_ADVERTISING_PARAMETERS};