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

Commit 7e20b8c2 authored by Henri Chataing's avatar Henri Chataing
Browse files

RootCanal: Reuse device addresses

The address is directly derived from the device identifier.
This change modifies TestModel to reuse identifiers from
disconnected devices.

The option is enabled by default and disabled by passing the
parameter --disable_address_reuse

Test: m root-canal && manual check
Bug: 253682053
Change-Id: Ie97763c1eae46df75d548a54b273cf8d363c7bb9
parent a7e4d670
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -44,6 +44,8 @@ DEFINE_string(default_commands_file, "",
DEFINE_bool(enable_hci_sniffer, false, "enable hci sniffer");
DEFINE_bool(enable_baseband_sniffer, false, "enable baseband sniffer");
DEFINE_bool(enable_pcap_filter, false, "enable PCAP filter");
DEFINE_bool(disable_address_reuse, false,
            "prevent rootcanal from reusing device addresses");
DEFINE_uint32(test_port, 6401, "test tcp port");
DEFINE_uint32(hci_port, 6402, "hci server tcp port");
DEFINE_uint32(link_port, 6403, "link server tcp port");
@@ -126,7 +128,7 @@ int main(int argc, char** argv) {
      std::make_shared<PosixAsyncSocketConnector>(&am),
      FLAGS_controller_properties_file, FLAGS_default_commands_file,
      FLAGS_enable_hci_sniffer, FLAGS_enable_baseband_sniffer,
      FLAGS_enable_pcap_filter);
      FLAGS_enable_pcap_filter, FLAGS_disable_address_reuse);
  std::promise<void> barrier;
  std::future<void> barrier_future = barrier.get_future();
  root_canal.initialize(std::move(barrier));
+5 −2
Original line number Diff line number Diff line
@@ -57,7 +57,8 @@ class TestEnvironment {
                  const std::string& default_commands_file = "",
                  bool enable_hci_sniffer = false,
                  bool enable_baseband_sniffer = false,
                  bool enable_pcap_filter = false)
                  bool enable_pcap_filter = false,
                  bool disable_address_reuse = false)
      : test_socket_server_(test_port),
        hci_socket_server_(hci_server_port),
        link_socket_server_(link_server_port),
@@ -69,7 +70,9 @@ class TestEnvironment {
        enable_baseband_sniffer_(enable_baseband_sniffer),
        enable_pcap_filter_(enable_pcap_filter),
        controller_(std::make_shared<rootcanal::DualModeController>(
            controller_properties_file)) {}
            controller_properties_file)) {
    test_model_.SetReuseDeviceIds(!disable_address_reuse);
  }

  void initialize(std::promise<void> barrier);

+21 −2
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <iomanip>      // for operator<<, setfill
#include <iostream>     // for basic_ostream
#include <memory>       // for shared_ptr, make...
#include <optional>
#include <type_traits>  // for remove_extent_t
#include <utility>      // for move

@@ -97,10 +98,27 @@ std::shared_ptr<PhyDevice> TestModel::CreatePhyDevice(

// Add a device to the test model.
PhyDevice::Identifier TestModel::AddDevice(std::shared_ptr<Device> device) {
  static PhyDevice::Identifier next_id = 0;
  std::optional<PhyDevice::Identifier> device_id{};
  if (reuse_device_ids_) {
    // Find the first unused identifier.
    // The identifier is used to generate the bluetooth address,
    // and reusing the first unused identifier lets a re-connecting
    // get the same identifier and address.
    for (PhyDevice::Identifier id = 0; id < next_device_id_; id++) {
      if (phy_devices_.count(id) == 0) {
        device_id = id;
        break;
      }
    }
  }

  if (!device_id.has_value()) {
    device_id = next_device_id_++;
  }

  std::string device_type = device->GetTypeString();
  std::shared_ptr<PhyDevice> phy_device =
      CreatePhyDevice(next_id++, device_type, std::move(device));
      CreatePhyDevice(device_id.value(), device_type, std::move(device));
  phy_devices_[phy_device->id] = phy_device;
  return phy_device->id;
}
@@ -260,6 +278,7 @@ void TestModel::Reset() {
      phy_layer->UnregisterAll();
    }
    phy_devices_.clear();
    next_device_id_ = 0;
  });
}

+8 −0
Original line number Diff line number Diff line
@@ -56,6 +56,10 @@ class TestModel {
  TestModel(TestModel& model) = delete;
  TestModel& operator=(const TestModel& model) = delete;

  void SetReuseDeviceIds(bool reuse_device_ids) {
    reuse_device_ids_ = reuse_device_ids;
  }

  // Allow derived classes to use custom phy layer.
  virtual std::unique_ptr<PhyLayer> CreatePhyLayer(PhyLayer::Identifier id,
                                                   Phy::Type type);
@@ -110,6 +114,10 @@ class TestModel {
  std::map<PhyDevice::Identifier, std::shared_ptr<PhyDevice>> phy_devices_;
  std::string list_string_;

  // Generator for device identifiers.
  PhyDevice::Identifier next_device_id_{0};
  bool reuse_device_ids_{true};

  // Prefix used to generate public device addresses for hosts
  // connecting over TCP.
  std::array<uint8_t, 5> bluetooth_address_prefix_;