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

Commit 8f8ce7e1 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: more controller

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I1d3569dadac6411be16c1cf94f3ce6c031846920
parent 0ac08468
Loading
Loading
Loading
Loading
+7 −18
Original line number Diff line number Diff line
@@ -353,7 +353,7 @@ void StructDef::GenRustSizeField(std::ostream& s) const {
    size += field->GetSize().bytes();
  }
  if (fields.size() > 0) {
    s << " size: " << size;
    s << size;
  }
}

@@ -374,23 +374,12 @@ void StructDef::GenRustDeclarations(std::ostream& s) const {
    field->GenRustNameAndType(s);
    s << ", ";
  }

  // Generate size field
  fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
      CountField::kFieldType,
      PaddingField::kFieldType,
      SizeField::kFieldType,
  });
  if (fields.size() > 0) {
    s << " size: usize";
  }
  s << "}\n";
}

void StructDef::GenRustImpls(std::ostream& s) const {
  s << "impl " << name_ << "{";
  s << "pub fn new(";
  /*s << "pub fn new(";
  GenRustFieldNameAndType(s, false);
  s << ") -> Self { Self {";
  auto fields = fields_.GetFieldsWithoutTypes({
@@ -409,11 +398,10 @@ void StructDef::GenRustImpls(std::ostream& s) const {
    }
    s << ", ";
  }
  GenRustSizeField(s);
  s << "}}";
  s << "}}";*/

  s << "pub fn parse(bytes: &[u8]) -> Result<Self> {";
  fields = fields_.GetFieldsWithoutTypes({
  auto fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
  });

@@ -447,7 +435,6 @@ void StructDef::GenRustImpls(std::ostream& s) const {
    }
    s << ", ";
  }
  GenRustSizeField(s);
  s << "})}\n";

  // write_to function
@@ -475,7 +462,9 @@ void StructDef::GenRustImpls(std::ostream& s) const {
  s << "}\n";

  if (fields.size() > 0) {
    s << "pub fn get_size(&self) -> usize { self.size }";
    s << "pub fn get_size(&self) -> usize {";
    GenRustSizeField(s);
    s << "}";
  }
  s << "}\n";
}
+104 −23
Original line number Diff line number Diff line
@@ -2,11 +2,15 @@

use crate::HciExports;
use bt_packets::hci::{
    Enable, ErrorCode, LeReadBufferSizeV1Builder, LeReadBufferSizeV2Builder, LeSetEventMaskBuilder,
    LocalVersionInformation, OpCode, OpCodeIndex, ReadBufferSizeBuilder,
    ReadLocalExtendedFeaturesBuilder, ReadLocalNameBuilder, ReadLocalSupportedCommandsBuilder,
    ReadLocalVersionInformationBuilder, SetEventMaskBuilder, WriteLeHostSupportBuilder,
    WriteSimplePairingModeBuilder,
    Enable, ErrorCode, LeMaximumDataLength, LeReadBufferSizeV1Builder, LeReadBufferSizeV2Builder,
    LeReadConnectListSizeBuilder, LeReadLocalSupportedFeaturesBuilder,
    LeReadMaximumAdvertisingDataLengthBuilder, LeReadMaximumDataLengthBuilder,
    LeReadNumberOfSupportedAdvertisingSetsBuilder, LeReadPeriodicAdvertiserListSizeBuilder,
    LeReadResolvingListSizeBuilder, LeReadSuggestedDefaultDataLengthBuilder,
    LeReadSupportedStatesBuilder, LeSetEventMaskBuilder, LocalVersionInformation, OpCode,
    OpCodeIndex, ReadBufferSizeBuilder, ReadLocalExtendedFeaturesBuilder, ReadLocalNameBuilder,
    ReadLocalSupportedCommandsBuilder, ReadLocalVersionInformationBuilder, SetEventMaskBuilder,
    WriteLeHostSupportBuilder, WriteSimplePairingModeBuilder,
};
use gddi::{module, provides, Stoppable};
use num_traits::ToPrimitive;
@@ -43,9 +47,9 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
        le_supported_host: Enable::Enabled
    }));

    let response = assert_success!(hci.send(ReadLocalNameBuilder {}));
    let name = std::str::from_utf8(response.get_local_name()).unwrap();
    let name = name[0..name.find('\0').unwrap()].to_string();
    let name = null_terminated_to_string(
        assert_success!(hci.send(ReadLocalNameBuilder {})).get_local_name(),
    );

    let version_info = assert_success!(hci.send(ReadLocalVersionInformationBuilder {}))
        .get_local_version_information()
@@ -62,21 +66,24 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
    let acl_buffer_length = buffer_size.get_acl_data_packet_length();
    let mut acl_buffers = buffer_size.get_total_num_acl_data_packets();

    let mut le_buffer_length;
    let mut le_buffers;
    let mut iso_buffer_length = 0;
    let mut iso_buffers = 0;
    let (mut le_buffer_length, mut le_buffers, iso_buffer_length, iso_buffers) =
        if commands.is_supported(OpCode::LeReadBufferSizeV2) {
            let response = assert_success!(hci.send(LeReadBufferSizeV2Builder {}));
        le_buffer_length = response.get_le_buffer_size().le_data_packet_length;
        le_buffers = response.get_le_buffer_size().total_num_le_packets;
        iso_buffer_length = response.get_iso_buffer_size().le_data_packet_length;
        iso_buffers = response.get_iso_buffer_size().total_num_le_packets;
            (
                response.get_le_buffer_size().le_data_packet_length,
                response.get_le_buffer_size().total_num_le_packets,
                response.get_iso_buffer_size().le_data_packet_length,
                response.get_iso_buffer_size().total_num_le_packets,
            )
        } else {
            let response = assert_success!(hci.send(LeReadBufferSizeV1Builder {}));
        le_buffer_length = response.get_le_buffer_size().le_data_packet_length;
        le_buffers = response.get_le_buffer_size().total_num_le_packets;
    }
            (
                response.get_le_buffer_size().le_data_packet_length,
                response.get_le_buffer_size().total_num_le_packets,
                0,
                0,
            )
        };

    // If the controller reports zero LE buffers, the ACL buffers are shared between classic & LE
    if le_buffers == 0 {
@@ -85,6 +92,57 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
        le_buffer_length = acl_buffer_length;
    }

    let le_features =
        assert_success!(hci.send(LeReadLocalSupportedFeaturesBuilder {})).get_le_features();
    let le_supported_states =
        assert_success!(hci.send(LeReadSupportedStatesBuilder {})).get_le_states();
    let le_connect_list_size =
        assert_success!(hci.send(LeReadConnectListSizeBuilder {})).get_connect_list_size();
    let le_resolving_list_size =
        assert_success!(hci.send(LeReadResolvingListSizeBuilder {})).get_resolving_list_size();

    let le_max_data_length = if commands.is_supported(OpCode::LeReadMaximumDataLength) {
        assert_success!(hci.send(LeReadMaximumDataLengthBuilder {}))
            .get_le_maximum_data_length()
            .clone()
    } else {
        LeMaximumDataLength {
            supported_max_rx_octets: 0,
            supported_max_rx_time: 0,
            supported_max_tx_octets: 0,
            supported_max_tx_time: 0,
        }
    };

    let le_suggested_default_data_length =
        if commands.is_supported(OpCode::LeReadSuggestedDefaultDataLength) {
            assert_success!(hci.send(LeReadSuggestedDefaultDataLengthBuilder {})).get_tx_octets()
        } else {
            0
        };

    let le_max_advertising_data_length =
        if commands.is_supported(OpCode::LeReadMaximumAdvertisingDataLength) {
            assert_success!(hci.send(LeReadMaximumAdvertisingDataLengthBuilder {}))
                .get_maximum_advertising_data_length()
        } else {
            31
        };
    let le_supported_advertising_sets =
        if commands.is_supported(OpCode::LeReadNumberOfSupportedAdvertisingSets) {
            assert_success!(hci.send(LeReadNumberOfSupportedAdvertisingSetsBuilder {}))
                .get_number_supported_advertising_sets()
        } else {
            1
        };
    let le_periodic_advertiser_list_size =
        if commands.is_supported(OpCode::LeReadPeriodicAdvertisingListSize) {
            assert_success!(hci.send(LeReadPeriodicAdvertiserListSizeBuilder {}))
                .get_periodic_advertiser_list_size()
        } else {
            0
        };

    ControllerExports {
        name,
        version_info,
@@ -98,6 +156,15 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
        le_buffers,
        iso_buffer_length,
        iso_buffers,
        le_features,
        le_supported_states,
        le_connect_list_size,
        le_resolving_list_size,
        le_max_data_length,
        le_suggested_default_data_length,
        le_max_advertising_data_length,
        le_supported_advertising_sets,
        le_periodic_advertiser_list_size,
    }
}

@@ -130,6 +197,15 @@ pub struct ControllerExports {
    le_buffers: u8,
    iso_buffer_length: u16,
    iso_buffers: u8,
    le_features: u64,
    le_supported_states: u64,
    le_connect_list_size: u8,
    le_resolving_list_size: u8,
    le_max_data_length: LeMaximumDataLength,
    le_suggested_default_data_length: u16,
    le_max_advertising_data_length: u16,
    le_supported_advertising_sets: u8,
    le_periodic_advertiser_list_size: u8,
}

/// Convenience struct for checking what commands are supported
@@ -158,3 +234,8 @@ impl SupportedCommands {
        }
    }
}

fn null_terminated_to_string(slice: &[u8]) -> String {
    let temp = std::str::from_utf8(slice).unwrap();
    temp[0..temp.find('\0').unwrap()].to_string()
}