From 679c11a0d545cd7b80740f49ef877562b8f0eda3 Mon Sep 17 00:00:00 2001 From: Josh Conner Date: Fri, 23 Aug 2024 20:17:58 +0000 Subject: [PATCH] pw_bluetooth_sapphire: Convert advertising return packets to emboss MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace hci_spec packed struct definitions: - LESetExtendedAdvertisingParametersReturnParams - LEReadAdvertisingChannelTxPowerReturnParams with emboss definitions: - LESetExtendedAdvertisingParametersCommandCompleteEvent - LEReadAdvertisingChannelTxPowerCommandCompleteEvent Bug: b/42167863 Test: fx test //src/connectivity/bluetooth/core/bt-host Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1095955 GitOrigin-RevId: 5a73378a878f825a73dfc33061e29595121fdc54 Change-Id: If1b3bae84cb7157bd4e77d039aff1a1b0ef500e1 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/230576 Lint: Lint 🤖 Commit-Queue: Jason Graffius Reviewed-by: Lulu Wang --- .../hci/extended_low_energy_advertiser.cc | 22 +++++++++++-------- .../host/hci/legacy_low_energy_advertiser.cc | 11 +++++----- .../host/testing/fake_controller.cc | 22 ++++++++++++------- .../internal/host/hci-spec/protocol.h | 18 --------------- .../host/hci/extended_low_energy_advertiser.h | 2 +- .../internal/host/hci/low_energy_advertiser.h | 2 +- 6 files changed, 35 insertions(+), 42 deletions(-) diff --git a/pw_bluetooth_sapphire/host/hci/extended_low_energy_advertiser.cc b/pw_bluetooth_sapphire/host/hci/extended_low_energy_advertiser.cc index d423ddf6a7..c045132cf2 100644 --- a/pw_bluetooth_sapphire/host/hci/extended_low_energy_advertiser.cc +++ b/pw_bluetooth_sapphire/host/hci/extended_low_energy_advertiser.cc @@ -447,11 +447,16 @@ EmbossCommandPacket ExtendedLowEnergyAdvertiser::BuildRemoveAdvertisingSet( } void ExtendedLowEnergyAdvertiser::OnSetAdvertisingParamsComplete( - const EventPacket& event) { - BT_ASSERT(event.event_code() == hci_spec::kCommandCompleteEventCode); + const EmbossEventPacket& event) { + auto event_view = event.view(); + BT_ASSERT(event_view.event_code_enum().Read() == + pw::bluetooth::emboss::EventCode::COMMAND_COMPLETE); + + auto cmd_complete_view = + event.view(); BT_ASSERT( - event.params().command_opcode == - hci_spec::kLESetExtendedAdvertisingParameters); + cmd_complete_view.command_opcode_enum().Read() == + pw::bluetooth::emboss::OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS_V1); Result<> result = event.ToResult(); if (bt_is_error(result, @@ -462,13 +467,12 @@ void ExtendedLowEnergyAdvertiser::OnSetAdvertisingParamsComplete( return; // full error handling done in super class, can just return here } - auto params = event.return_params< - hci_spec::LESetExtendedAdvertisingParametersReturnParams>(); - BT_ASSERT(params); - + auto view = event.view< + pw::bluetooth::emboss:: + LESetExtendedAdvertisingParametersCommandCompleteEventView>(); if (staged_advertising_parameters_.include_tx_power_level) { staged_advertising_parameters_.selected_tx_power_level = - params->selected_tx_power; + view.selected_tx_power().Read(); } } diff --git a/pw_bluetooth_sapphire/host/hci/legacy_low_energy_advertiser.cc b/pw_bluetooth_sapphire/host/hci/legacy_low_energy_advertiser.cc index ce6f22ff0a..3349a84f7e 100644 --- a/pw_bluetooth_sapphire/host/hci/legacy_low_energy_advertiser.cc +++ b/pw_bluetooth_sapphire/host/hci/legacy_low_energy_advertiser.cc @@ -244,7 +244,7 @@ void LegacyLowEnergyAdvertiser::StartAdvertising( // If advertising was canceled during the TX power level read (either // |starting_| was reset or the |result_callback| was moved), return early. if (options.include_tx_power_level) { - auto power_cb = [this](auto, const hci::EventPacket& event) mutable { + auto power_cb = [this](auto, const hci::EmbossEventPacket& event) mutable { BT_ASSERT(staged_params_.has_value()); if (!starting_ || !staged_params_.value().result_callback) { bt_log( @@ -264,11 +264,12 @@ void LegacyLowEnergyAdvertiser::StartAdvertising( staged_params_ = {}; // Update the advertising and scan response data with the TX power level. - const auto& params = event.return_params< - hci_spec::LEReadAdvertisingChannelTxPowerReturnParams>(); - staged_params.data.SetTxPower(params->tx_power); + auto view = event.view< + pw::bluetooth::emboss:: + LEReadAdvertisingChannelTxPowerCommandCompleteEventView>(); + staged_params.data.SetTxPower(view.tx_power_level().Read()); if (staged_params.scan_rsp.CalculateBlockSize()) { - staged_params.scan_rsp.SetTxPower(params->tx_power); + staged_params.scan_rsp.SetTxPower(view.tx_power_level().Read()); } StartAdvertisingInternal( diff --git a/pw_bluetooth_sapphire/host/testing/fake_controller.cc b/pw_bluetooth_sapphire/host/testing/fake_controller.cc index 45d7a6db4c..9d5e7ca9cb 100644 --- a/pw_bluetooth_sapphire/host/testing/fake_controller.cc +++ b/pw_bluetooth_sapphire/host/testing/fake_controller.cc @@ -2903,11 +2903,14 @@ void FakeController::OnLESetExtendedAdvertisingParameters( // only want to write if there are no errors) extended_advertising_states_[handle] = state; - hci_spec::LESetExtendedAdvertisingParametersReturnParams return_params; - return_params.status = pwemb::StatusCode::SUCCESS; - return_params.selected_tx_power = hci_spec::kLEAdvertisingTxPowerMax; + auto packet = hci::EmbossEventPacket::New< + pwemb::LESetExtendedAdvertisingParametersCommandCompleteEventWriter>( + hci_spec::kCommandCompleteEventCode); + auto view = packet.view_t(); + view.status().Write(pwemb::StatusCode::SUCCESS); + view.selected_tx_power().Write(hci_spec::kLEAdvertisingTxPowerMax); RespondWithCommandComplete(hci_spec::kLESetExtendedAdvertisingParameters, - BufferView(&return_params, sizeof(return_params))); + &packet); NotifyAdvertisingState(); } @@ -3389,12 +3392,15 @@ void FakeController::OnLEReadAdvertisingChannelTxPower() { return; } - hci_spec::LEReadAdvertisingChannelTxPowerReturnParams params; // Send back arbitrary tx power. - params.status = pwemb::StatusCode::SUCCESS; - params.tx_power = 9; + auto packet = hci::EmbossEventPacket::New< + pwemb::LEReadAdvertisingChannelTxPowerCommandCompleteEventWriter>( + hci_spec::kCommandCompleteEventCode); + auto view = packet.view_t(); + view.status().Write(pwemb::StatusCode::SUCCESS); + view.tx_power_level().Write(9); RespondWithCommandComplete(hci_spec::kLEReadAdvertisingChannelTxPower, - BufferView(¶ms, sizeof(params))); + &packet); } void FakeController::SendLEAdvertisingSetTerminatedEvent( diff --git a/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h b/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h index 6eda3f2a48..aa630ced1c 100644 --- a/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h +++ b/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h @@ -1387,18 +1387,6 @@ constexpr OpCode kLESetAdvertisingParameters = constexpr OpCode kLEReadAdvertisingChannelTxPower = LEControllerCommandOpCode(0x0007); -struct LEReadAdvertisingChannelTxPowerReturnParams { - // See enum StatusCode in hci_constants.h. - StatusCode status; - - // The transmit power level used for LE advertising channel packets. - // - // Range: -20 <= N <= +10 - // Units: dBm - // Accuracy: +/- 4 dB - int8_t tx_power; -} __attribute__((packed)); - // =========================================== // LE Set Advertising Data Command (v4.0) (LE) constexpr OpCode kLESetAdvertisingData = LEControllerCommandOpCode(0x0008); @@ -2080,12 +2068,6 @@ constexpr OpCode kLESetAdvertisingSetRandomAddress = constexpr OpCode kLESetExtendedAdvertisingParameters = LEControllerCommandOpCode(0x0036); -struct LESetExtendedAdvertisingParametersReturnParams { - // See enum StatusCode in hci_constants.h. - StatusCode status; - int8_t selected_tx_power; -} __attribute__((packed)); - // ==================================================== // LE Set Extended Advertising Data Command (v5.0) (LE) constexpr OpCode kLESetExtendedAdvertisingData = diff --git a/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/extended_low_energy_advertiser.h b/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/extended_low_energy_advertiser.h index 5d03522916..e7fba6d8fe 100644 --- a/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/extended_low_energy_advertiser.h +++ b/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/extended_low_energy_advertiser.h @@ -131,7 +131,7 @@ class ExtendedLowEnergyAdvertiser final : public LowEnergyAdvertiser { pw::bluetooth::emboss::LEExtendedAdvFragmentPreference fragment_preference); - void OnSetAdvertisingParamsComplete(const EventPacket& event) override; + void OnSetAdvertisingParamsComplete(const EmbossEventPacket& event) override; void OnCurrentOperationComplete() override; diff --git a/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/low_energy_advertiser.h b/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/low_energy_advertiser.h index 55d088f423..ee61606391 100644 --- a/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/low_energy_advertiser.h +++ b/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/hci/low_energy_advertiser.h @@ -255,7 +255,7 @@ class LowEnergyAdvertiser : public LocalAddressClient { // Called when the command packet created with BuildSetAdvertisingParams // returns with a result - virtual void OnSetAdvertisingParamsComplete(const EventPacket& event) {} + virtual void OnSetAdvertisingParamsComplete(const EmbossEventPacket& event) {} // Called when a sequence of HCI commands that form a single operation (e.g. // start advertising, stop advertising) completes in its entirety. Subclasses