Skip to content

Commit

Permalink
pw_bluetooth_sapphire: Convert advertising return packets to emboss
Browse files Browse the repository at this point in the history
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 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Commit-Queue: Jason Graffius <jgraff@google.com>
Reviewed-by: Lulu Wang <luluwang@google.com>
  • Loading branch information
josh-conner authored and CQ Bot Account committed Aug 23, 2024
1 parent 63ff91d commit 679c11a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 42 deletions.
22 changes: 13 additions & 9 deletions pw_bluetooth_sapphire/host/hci/extended_low_energy_advertiser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<pw::bluetooth::emboss::EventHeaderView>();
BT_ASSERT(event_view.event_code_enum().Read() ==
pw::bluetooth::emboss::EventCode::COMMAND_COMPLETE);

auto cmd_complete_view =
event.view<pw::bluetooth::emboss::CommandCompleteEventView>();
BT_ASSERT(
event.params<hci_spec::CommandCompleteEventParams>().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,
Expand All @@ -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();
}
}

Expand Down
11 changes: 6 additions & 5 deletions pw_bluetooth_sapphire/host/hci/legacy_low_energy_advertiser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
22 changes: 14 additions & 8 deletions pw_bluetooth_sapphire/host/testing/fake_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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(&params, sizeof(params)));
&packet);
}

void FakeController::SendLEAdvertisingSetTerminatedEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 679c11a

Please sign in to comment.