summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2023-08-05 02:42:51 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2023-08-05 02:42:51 +0000
commitd5f4e5ecdc05b12e7094a7a56ffe29de232b530b (patch)
tree224868c3b846d19508285033a2a51a92bc25a4d8
parent4ad14a62506b917357daa31602328180f9f86b45 (diff)
parentdbfb5e0dcb02df1909888ddbbf96daa039e79f28 (diff)
Merge "RootCanal: Use unique device identifiers" into main
-rw-r--r--tools/rootcanal/desktop/test_environment.cc2
-rw-r--r--tools/rootcanal/model/controller/dual_mode_controller.cc8
-rw-r--r--tools/rootcanal/model/controller/dual_mode_controller.h3
-rw-r--r--tools/rootcanal/model/controller/link_layer_controller.cc2
-rw-r--r--tools/rootcanal/model/controller/link_layer_controller.h4
-rw-r--r--tools/rootcanal/model/devices/device.cc9
-rw-r--r--tools/rootcanal/model/devices/device.h3
-rw-r--r--tools/rootcanal/model/setup/phy_device.cc9
-rw-r--r--tools/rootcanal/model/setup/phy_device.h3
-rw-r--r--tools/rootcanal/model/setup/test_model.cc67
-rw-r--r--tools/rootcanal/model/setup/test_model.h12
11 files changed, 63 insertions, 59 deletions
diff --git a/tools/rootcanal/desktop/test_environment.cc b/tools/rootcanal/desktop/test_environment.cc
index ef32cba446..10a16616d0 100644
--- a/tools/rootcanal/desktop/test_environment.cc
+++ b/tools/rootcanal/desktop/test_environment.cc
@@ -57,7 +57,7 @@ TestEnvironment::TestEnvironment(
link_socket_server_ = open_server(&async_manager_, link_port);
link_ble_socket_server_ = open_server(&async_manager_, link_ble_port);
connector_ = open_connector(&async_manager_);
- test_model_.SetReuseDeviceIds(!disable_address_reuse);
+ test_model_.SetReuseDeviceAddresses(!disable_address_reuse);
// Get a user ID for tasks scheduled within the test environment.
socket_user_id_ = async_manager_.GetNextUserId();
diff --git a/tools/rootcanal/model/controller/dual_mode_controller.cc b/tools/rootcanal/model/controller/dual_mode_controller.cc
index 175f08aa03..d2b8780c05 100644
--- a/tools/rootcanal/model/controller/dual_mode_controller.cc
+++ b/tools/rootcanal/model/controller/dual_mode_controller.cc
@@ -33,11 +33,6 @@ constexpr uint16_t kLeMaximumDataLength = 64;
constexpr uint16_t kLeMaximumDataTime = 0x148;
constexpr uint8_t kTransmitPowerLevel = -20;
-static int next_instance_id() {
- static int instance_counter = 0;
- return instance_counter++;
-}
-
// Device methods.
std::string DualModeController::GetTypeString() const {
return "Simulated Bluetooth Controller";
@@ -65,8 +60,7 @@ void DualModeController::SendCommandCompleteUnknownOpCodeEvent(
}
DualModeController::DualModeController(ControllerProperties properties)
- : id_(next_instance_id()), properties_(std::move(properties)),
- random_generator_(id_) {
+ : properties_(std::move(properties)), random_generator_(id_) {
Address public_address{};
ASSERT(Address::FromString("3C:5A:B4:04:05:06", public_address));
SetAddress(public_address);
diff --git a/tools/rootcanal/model/controller/dual_mode_controller.h b/tools/rootcanal/model/controller/dual_mode_controller.h
index d8a71eb484..c3a160dfad 100644
--- a/tools/rootcanal/model/controller/dual_mode_controller.h
+++ b/tools/rootcanal/model/controller/dual_mode_controller.h
@@ -50,9 +50,6 @@ using ::bluetooth::hci::CommandView;
// "Hci" to distinguish it as a controller command.
class DualModeController : public Device {
public:
- // Unique instance identifier.
- const int id_;
-
DualModeController(ControllerProperties properties = ControllerProperties());
DualModeController(DualModeController&&) = delete;
DualModeController(const DualModeController&) = delete;
diff --git a/tools/rootcanal/model/controller/link_layer_controller.cc b/tools/rootcanal/model/controller/link_layer_controller.cc
index adafc72b99..ed92bc1f71 100644
--- a/tools/rootcanal/model/controller/link_layer_controller.cc
+++ b/tools/rootcanal/model/controller/link_layer_controller.cc
@@ -1980,7 +1980,7 @@ void LinkLayerController::SetExtendedInquiryResponse(
LinkLayerController::LinkLayerController(const Address& address,
const ControllerProperties& properties,
- int id)
+ uint32_t id)
: id_(id),
address_(address),
properties_(properties),
diff --git a/tools/rootcanal/model/controller/link_layer_controller.h b/tools/rootcanal/model/controller/link_layer_controller.h
index 3e036faff6..2e426a86fb 100644
--- a/tools/rootcanal/model/controller/link_layer_controller.h
+++ b/tools/rootcanal/model/controller/link_layer_controller.h
@@ -57,7 +57,7 @@ class LinkLayerController {
static constexpr size_t kExtendedInquiryResponseSize = 240;
// Unique instance identifier.
- const int id_;
+ const uint32_t id_;
// Generate a resolvable private address using the specified IRK.
static Address generate_rpa(
@@ -67,7 +67,7 @@ class LinkLayerController {
static bool irk_is_zero(std::array<uint8_t, LinkLayerController::kIrkSize> irk);
LinkLayerController(const Address& address,
- const ControllerProperties& properties, int id = 0);
+ const ControllerProperties& properties, uint32_t id = 0);
~LinkLayerController();
ErrorCode SendCommandToRemoteByAddress(OpCode opcode, pdl::packet::slice args,
diff --git a/tools/rootcanal/model/devices/device.cc b/tools/rootcanal/model/devices/device.cc
index e7aeabbd9f..bf10ae63de 100644
--- a/tools/rootcanal/model/devices/device.cc
+++ b/tools/rootcanal/model/devices/device.cc
@@ -22,7 +22,14 @@
namespace rootcanal {
-Device::Device() { ASSERT(Address::FromString("BB:BB:BB:BB:BB:AD", address_)); }
+static uint32_t next_instance_id() {
+ static uint32_t instance_counter = 0;
+ return instance_counter++;
+}
+
+Device::Device() : id_(next_instance_id()) {
+ ASSERT(Address::FromString("BB:BB:BB:BB:BB:AD", address_));
+}
std::string Device::ToString() const {
return GetTypeString() + "@" + address_.ToString();
diff --git a/tools/rootcanal/model/devices/device.h b/tools/rootcanal/model/devices/device.h
index c9deec67bc..15ced21944 100644
--- a/tools/rootcanal/model/devices/device.h
+++ b/tools/rootcanal/model/devices/device.h
@@ -35,6 +35,9 @@ using ::bluetooth::hci::Address;
// - Provide Get*() and Set*() functions for device attributes.
class Device {
public:
+ // Unique device identifier.
+ const uint32_t id_;
+
Device();
virtual ~Device() = default;
diff --git a/tools/rootcanal/model/setup/phy_device.cc b/tools/rootcanal/model/setup/phy_device.cc
index bd2cd78250..7704f36004 100644
--- a/tools/rootcanal/model/setup/phy_device.cc
+++ b/tools/rootcanal/model/setup/phy_device.cc
@@ -22,9 +22,8 @@
namespace rootcanal {
-PhyDevice::PhyDevice(Identifier id, std::string type,
- std::shared_ptr<Device> device)
- : id(id), type(std::move(type)), device_(std::move(device)) {
+PhyDevice::PhyDevice(std::string type, std::shared_ptr<Device> device)
+ : id(device->id_), type(std::move(type)), device_(std::move(device)) {
using namespace std::placeholders;
ASSERT(device_ != nullptr);
device_->RegisterLinkLayerChannel(
@@ -37,6 +36,10 @@ void PhyDevice::Unregister(PhyLayer* phy) { phy_layers_.erase(phy); }
void PhyDevice::Tick() { device_->Tick(); }
+bluetooth::hci::Address PhyDevice::GetAddress() const {
+ return device_->GetAddress();
+}
+
void PhyDevice::SetAddress(bluetooth::hci::Address address) {
device_->SetAddress(std::move(address));
}
diff --git a/tools/rootcanal/model/setup/phy_device.h b/tools/rootcanal/model/setup/phy_device.h
index 60a5edb98b..b23ba87c1b 100644
--- a/tools/rootcanal/model/setup/phy_device.h
+++ b/tools/rootcanal/model/setup/phy_device.h
@@ -31,7 +31,7 @@ class PhyDevice {
public:
using Identifier = uint32_t;
- PhyDevice(Identifier id, std::string type, std::shared_ptr<Device> device);
+ PhyDevice(std::string type, std::shared_ptr<Device> device);
PhyDevice(PhyDevice &&) = delete;
~PhyDevice() = default;
@@ -43,6 +43,7 @@ class PhyDevice {
void Send(std::vector<uint8_t> const& packet, Phy::Type type,
int8_t tx_power);
+ bluetooth::hci::Address GetAddress() const;
void SetAddress(bluetooth::hci::Address address);
std::string ToString();
diff --git a/tools/rootcanal/model/setup/test_model.cc b/tools/rootcanal/model/setup/test_model.cc
index 56ac1694fc..1ca7424134 100644
--- a/tools/rootcanal/model/setup/test_model.cc
+++ b/tools/rootcanal/model/setup/test_model.cc
@@ -91,34 +91,42 @@ std::unique_ptr<PhyLayer> TestModel::CreatePhyLayer(PhyLayer::Identifier id,
}
std::shared_ptr<PhyDevice> TestModel::CreatePhyDevice(
- PhyDevice::Identifier id, std::string type,
- std::shared_ptr<Device> device) {
- return std::make_shared<PhyDevice>(id, std::move(type), std::move(device));
+ std::string type, std::shared_ptr<Device> device) {
+ return std::make_shared<PhyDevice>(std::move(type), std::move(device));
}
-// Add a device to the test model.
-PhyDevice::Identifier TestModel::AddDevice(std::shared_ptr<Device> device) {
- 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;
+Address TestModel::GenerateBluetoothAddress() const {
+ Address address({
+ 0xff,
+ bluetooth_address_prefix_[4],
+ bluetooth_address_prefix_[3],
+ bluetooth_address_prefix_[2],
+ bluetooth_address_prefix_[1],
+ bluetooth_address_prefix_[0],
+ });
+
+ if (reuse_device_addresses_) {
+ // Find the first unused address.
+ for (uint16_t b0 = 0; b0 <= 0xff; b0++) {
+ address.address[0] = b0;
+ bool used = std::any_of(phy_devices_.begin(), phy_devices_.end(),
+ [address](auto& device) {
+ return device.second->GetAddress() == address;
+ });
+ if (!used) {
break;
}
}
}
- if (!device_id.has_value()) {
- device_id = next_device_id_++;
- }
+ return address;
+}
+// Add a device to the test model.
+PhyDevice::Identifier TestModel::AddDevice(std::shared_ptr<Device> device) {
std::string device_type = device->GetTypeString();
std::shared_ptr<PhyDevice> phy_device =
- CreatePhyDevice(device_id.value(), device_type, std::move(device));
+ CreatePhyDevice(device_type, std::move(device));
phy_devices_[phy_device->id] = phy_device;
return phy_device->id;
}
@@ -166,7 +174,7 @@ void TestModel::RemoveDeviceFromPhy(PhyDevice::Identifier device_id,
void TestModel::AddLinkLayerConnection(std::shared_ptr<Device> device,
Phy::Type type) {
- INFO("Adding a new link layer connection of type: {}",
+ INFO(device->id_, "Adding a new link layer connection of type: {}",
type == Phy::Type::BR_EDR ? "BR_EDR" : "LOW_ENERGY");
PhyDevice::Identifier device_id = AddDevice(device);
@@ -196,24 +204,16 @@ void TestModel::AddRemote(const std::string& server, int port, Phy::Type type) {
PhyDevice::Identifier TestModel::AddHciConnection(
std::shared_ptr<HciDevice> device) {
- PhyDevice::Identifier device_id =
- AddDevice(std::static_pointer_cast<Device>(device));
- auto bluetooth_address = Address{{
- uint8_t(device_id),
- bluetooth_address_prefix_[4],
- bluetooth_address_prefix_[3],
- bluetooth_address_prefix_[2],
- bluetooth_address_prefix_[1],
- bluetooth_address_prefix_[0],
- }};
- device->SetAddress(bluetooth_address);
+ device->SetAddress(GenerateBluetoothAddress());
+ AddDevice(std::static_pointer_cast<Device>(device));
- INFO(device->id_, "Initialized device with address {}", bluetooth_address.ToString());
+ INFO(device->id_, "Initialized device with address {}", device->GetAddress());
for (auto& [_, phy_layer] : phy_layers_) {
- phy_layer->Register(phy_devices_[device_id]);
+ phy_layer->Register(phy_devices_[device->id_]);
}
+ PhyDevice::Identifier device_id = device->id_;
AsyncUserId user_id = get_user_id_();
device->RegisterCloseCallback([this, device_id, user_id] {
schedule_task_(user_id, std::chrono::milliseconds(0),
@@ -221,7 +221,7 @@ PhyDevice::Identifier TestModel::AddHciConnection(
OnConnectionClosed(device_id, user_id);
});
});
- return device_id;
+ return device->id_;
}
void TestModel::OnConnectionClosed(PhyDevice::Identifier device_id,
@@ -272,7 +272,6 @@ void TestModel::Reset() {
phy_layer->UnregisterAll();
}
phy_devices_.clear();
- next_device_id_ = 0;
});
}
diff --git a/tools/rootcanal/model/setup/test_model.h b/tools/rootcanal/model/setup/test_model.h
index 7ef94c5819..4fb15a5103 100644
--- a/tools/rootcanal/model/setup/test_model.h
+++ b/tools/rootcanal/model/setup/test_model.h
@@ -56,8 +56,8 @@ class TestModel {
TestModel(TestModel& model) = delete;
TestModel& operator=(const TestModel& model) = delete;
- void SetReuseDeviceIds(bool reuse_device_ids) {
- reuse_device_ids_ = reuse_device_ids;
+ void SetReuseDeviceAddresses(bool reuse_device_addresses) {
+ reuse_device_addresses_ = reuse_device_addresses;
}
// Allow derived classes to use custom phy layer.
@@ -66,8 +66,7 @@ class TestModel {
// Allow derived classes to use custom phy devices.
virtual std::shared_ptr<PhyDevice> CreatePhyDevice(
- PhyDevice::Identifier id, std::string type,
- std::shared_ptr<Device> device);
+ std::string type, std::shared_ptr<Device> device);
// Test model commands
@@ -110,13 +109,14 @@ class TestModel {
void Reset();
private:
+ Address GenerateBluetoothAddress() const;
+
std::map<PhyLayer::Identifier, std::shared_ptr<PhyLayer>> phy_layers_;
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};
+ bool reuse_device_addresses_{true};
// Prefix used to generate public device addresses for hosts
// connecting over TCP.