Skip to content

Commit

Permalink
pw_bluetooth_sapphire: LE Read Buffer Size [v2]
Browse files Browse the repository at this point in the history
Add support for HCI_LE_Read_Buffer_Size [v2]:
* Add to FakeController
* Add explicit v1 suffix to legacy definitions, as needed

Bug: b/311639040
Test: pw presubmit --step gn_chre_googletest_nanopb_sapphire_build
Change-Id: Ib83ceb14c789493908232b063f2e05eb537dfd21
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/183772
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ben Lawson <benlawson@google.com>
Commit-Queue: Josh Conner <joshconner@google.com>
  • Loading branch information
josh-conner authored and CQ Bot Account committed Dec 15, 2023
1 parent 19d61d6 commit e45a2d6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pw_bluetooth_sapphire/host/gap/adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ void AdapterImpl::InitializeStep2() {
}
auto params =
cmd_complete
.return_params<hci_spec::LEReadBufferSizeReturnParams>();
.return_params<hci_spec::LEReadBufferSizeV1ReturnParams>();
uint16_t mtu = le16toh(params->hc_le_acl_data_packet_length);
uint8_t max_count = params->hc_total_num_le_acl_data_packets;
if (mtu && max_count) {
Expand Down
52 changes: 51 additions & 1 deletion pw_bluetooth_sapphire/host/testing/fake_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void FakeController::Settings::ApplyDualModeDefaults() {
le_total_num_acl_data_packets = 1;
synchronous_data_packet_length = 0;
total_num_synchronous_data_packets = 0;
iso_data_packet_length = 512;
total_num_iso_data_packets = 1;
android_extension_settings.SetToZeros();
}

Expand Down Expand Up @@ -162,6 +164,8 @@ void FakeController::Settings::AddLESupportedCommands() {
hci_spec::SupportedCommand::kLEReadRemoteFeatures);
SetBit(supported_commands + 28,
hci_spec::SupportedCommand::kLEStartEncryption);
SetBit(supported_commands + 41,
hci_spec::SupportedCommand::kLEReadBufferSizeV2);
}

void FakeController::Settings::ApplyLegacyLEConfig() {
Expand Down Expand Up @@ -307,6 +311,18 @@ void FakeController::RespondWithCommandComplete(hci_spec::OpCode opcode,
SendEvent(hci_spec::kCommandCompleteEventCode, buffer);
}

void FakeController::RespondWithCommandComplete(
hci_spec::OpCode opcode, hci::EmbossEventPacket* packet) {
auto header =
packet
->template view<pw::bluetooth::emboss::CommandCompleteEventWriter>();

header.num_hci_command_packets().Write(settings_.num_hci_command_packets);
header.command_opcode().BackingStorage().WriteUInt(opcode);

SendEvent(hci_spec::kCommandCompleteEventCode, packet);
}

void FakeController::RespondWithCommandStatus(
hci_spec::OpCode opcode, pw::bluetooth::emboss::StatusCode status) {
StaticByteBuffer<sizeof(hci_spec::CommandStatusEventParams)> buffer;
Expand All @@ -332,6 +348,20 @@ void FakeController::SendEvent(hci_spec::EventCode event_code,
SendCommandChannelPacket(buffer);
}

void FakeController::SendEvent(hci_spec::EventCode event_code,
hci::EmbossEventPacket* packet) {
auto header =
packet->template view<pw::bluetooth::emboss::EventHeaderWriter>();
uint8_t parameter_total_size =
packet->size() -
pw::bluetooth::emboss::EventHeader::IntrinsicSizeInBytes();

header.event_code().Write(event_code);
header.parameter_total_size().Write(parameter_total_size);

SendCommandChannelPacket(packet->data());
}

void FakeController::SendLEMetaEvent(hci_spec::EventCode subevent_code,
const ByteBuffer& payload) {
DynamicByteBuffer buffer(sizeof(hci_spec::LEMetaEventParams) +
Expand Down Expand Up @@ -1217,7 +1247,7 @@ void FakeController::OnLESetEventMask(
}

void FakeController::OnLEReadBufferSizeV1() {
hci_spec::LEReadBufferSizeReturnParams params;
hci_spec::LEReadBufferSizeV1ReturnParams params;
params.status = pw::bluetooth::emboss::StatusCode::SUCCESS;
params.hc_le_acl_data_packet_length =
htole16(settings_.le_acl_data_packet_length);
Expand All @@ -1227,6 +1257,22 @@ void FakeController::OnLEReadBufferSizeV1() {
BufferView(&params, sizeof(params)));
}

void FakeController::OnLEReadBufferSizeV2() {
auto packet = hci::EmbossEventPacket::New<
pw::bluetooth::emboss::LEReadBufferSizeV2CommandCompleteEventWriter>(
hci_spec::kCommandCompleteEventCode);
auto view = packet.view_t();

view.status().Write(pw::bluetooth::emboss::StatusCode::SUCCESS);
view.le_acl_data_packet_length().Write(settings_.le_acl_data_packet_length);
view.total_num_le_acl_data_packets().Write(
settings_.le_total_num_acl_data_packets);
view.iso_data_packet_length().Write(settings_.iso_data_packet_length);
view.total_num_iso_data_packets().Write(settings_.total_num_iso_data_packets);

RespondWithCommandComplete(hci_spec::kLEReadBufferSizeV2, &packet);
}

void FakeController::OnLEReadSupportedStates() {
hci_spec::LEReadSupportedStatesReturnParams params;
params.status = pw::bluetooth::emboss::StatusCode::SUCCESS;
Expand Down Expand Up @@ -3584,6 +3630,10 @@ void FakeController::HandleReceivedCommandPacket(
OnLEReadBufferSizeV1();
break;
}
case hci_spec::kLEReadBufferSizeV2: {
OnLEReadBufferSizeV2();
break;
}
case hci_spec::kReset: {
OnReset();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1417,10 +1417,10 @@ inline bool IsLECommand(OpCode opcode) {
constexpr OpCode kLESetEventMask = LEControllerCommandOpCode(0x0001);

// =======================================
// LE Read Buffer Size Command (v4.0) (LE)
// LE Read Buffer Size [v1] Command (v4.0) (LE)
constexpr OpCode kLEReadBufferSizeV1 = LEControllerCommandOpCode(0x0002);

struct LEReadBufferSizeReturnParams {
struct LEReadBufferSizeV1ReturnParams {
// See enum StatusCode in hci_constants.h.
StatusCode status;

Expand Down Expand Up @@ -2370,6 +2370,10 @@ constexpr OpCode kLEWriteRFPathCompensation = LEControllerCommandOpCode(0x004D);
// LE Set Privacy Mode Command (v5.0) (LE)
constexpr OpCode kLESetPrivacyMode = LEControllerCommandOpCode(0x004E);

// =======================================
// LE Read Buffer Size [v2] Command (v5.2) (LE)
constexpr OpCode kLEReadBufferSizeV2 = LEControllerCommandOpCode(0x0060);

// =======================================
// LE Set Host Feature Command (v5.2) (LE)
constexpr OpCode kLESetHostFeature = LEControllerCommandOpCode(0x0074);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class FakeController final : public ControllerTestDoubleBase,
uint8_t le_total_num_acl_data_packets = 0;
uint16_t synchronous_data_packet_length = 0;
uint8_t total_num_synchronous_data_packets = 0;
uint16_t iso_data_packet_length = 0;
uint8_t total_num_iso_data_packets = 0;

// Vendor extensions
StaticPacket<pw::bluetooth::vendor::android_hci::
Expand Down Expand Up @@ -291,6 +293,10 @@ class FakeController final : public ControllerTestDoubleBase,
// Sends a HCI event with the given parameters.
void SendEvent(hci_spec::EventCode event_code, const ByteBuffer& payload);

// Sends an HCI event, filling in the parameters in a provided event packet.
void SendEvent(hci_spec::EventCode event_code,
hci::EmbossEventPacket* packet);

// Sends a LE Meta event with the given parameters.
void SendLEMetaEvent(hci_spec::EventCode subevent_code,
const ByteBuffer& payload);
Expand Down Expand Up @@ -485,6 +491,12 @@ class FakeController final : public ControllerTestDoubleBase,
void RespondWithCommandComplete(hci_spec::OpCode opcode,
const ByteBuffer& params);

// Sends an HCI_Command_Complete event in response to the command with
// |opcode| and using the provided event packet, filling in the event header
// fields.
void RespondWithCommandComplete(hci_spec::OpCode opcode,
hci::EmbossEventPacket* packet);

// Sends a HCI_Command_Status event in response to the command with |opcode|
// and using the given data as the parameter payload.
void RespondWithCommandStatus(hci_spec::OpCode opcode,
Expand Down Expand Up @@ -588,6 +600,9 @@ class FakeController final : public ControllerTestDoubleBase,
// Called when a HCI_LE_Read_Buffer_Size command is received.
void OnLEReadBufferSizeV1();

// Called when a HCI_LE_Read_Buffer_Size command is received.
void OnLEReadBufferSizeV2();

// Called when a HCI_LE_Read_Supported_States command is received.
void OnLEReadSupportedStates();

Expand Down

0 comments on commit e45a2d6

Please sign in to comment.