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

Commit 6c1449d4 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: remaining fixes to get DirectHciTest running consistently

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost DirectHciTest
Change-Id: I05c6fca13d9af2705b4ae597f6a458963c0fd53f
parent 08834531
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -517,6 +517,7 @@ rust_library {
        "libnum_traits",
        "libthiserror",
        "libbt_hci_custom_types",
        "liblog_rust",
    ],
}

+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ from google.protobuf import text_format

def custom_message_formatter(m, ident, as_one_line):
    if m.DESCRIPTOR == common.Data.DESCRIPTOR:
        return 'payload: (hex) "{}"'.format(m.payload.hex())
        return 'payload: (hex) "{}"'.format(m.payload.hex(" "))
    return None


+9 −0
Original line number Diff line number Diff line
@@ -37,3 +37,12 @@ void FixedScalarField::GenValue(std::ostream& s) const {
void FixedScalarField::GenStringRepresentation(std::ostream& s, std::string) const {
  s << "+" << value_;
}

void FixedScalarField::GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const {
  s << "let " << GetName() << ": " << GetRustDataType() << " = " << value_ << ";";
  FixedField::GenRustWriter(s, start_offset, end_offset);
}

void FixedScalarField::GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  FixedField::GenRustGetter(s, start_offset, end_offset);
}
+4 −0
Original line number Diff line number Diff line
@@ -39,6 +39,10 @@ class FixedScalarField : public FixedField {

  static const std::string field_type;

  void GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const override;

  void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const override;

 private:

  const int64_t value_;
+30 −10
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "fields/scalar_field.h"

#include "fields/fixed_scalar_field.h"
#include "fields/size_field.h"
#include "util.h"

@@ -165,12 +166,31 @@ void ScalarField::GenRustGetter(std::ostream& s, Size start_offset, Size end_off
  int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());

  s << "let " << GetName() << " = ";
  if (num_leading_bits == 0) {
    s << GetRustParseDataType() << "::from_le_bytes(bytes[" << start_offset.bytes() << "..";
    s << start_offset.bytes() + size.bytes() << "].try_into().unwrap());";
  auto offset = num_leading_bits == 0 ? 0 : -1;
  s << GetRustParseDataType() << "::from_le_bytes([";
  int total_bytes;
  if (size_ <= 8) {
    total_bytes = 1;
  } else if (size_ <= 16) {
    total_bytes = 2;
  } else if (size_ <= 32) {
    total_bytes = 4;
  } else {
    s << GetRustParseDataType() << "::from_le_bytes(bytes[" << start_offset.bytes() - 1 << "..";
    s << start_offset.bytes() + size.bytes() - 1 << "].try_into().unwrap());";
    total_bytes = 8;
  }
  for (int i = 0; i < total_bytes; i++) {
    if (i > 0) {
      s << ",";
    }
    if (i < size.bytes()) {
      s << "bytes[" << start_offset.bytes() + i + offset << "]";
    } else {
      s << 0;
    }
  }
  s << "]);";

  if (num_leading_bits != 0) {
    s << "let " << GetName() << " = " << GetName() << " >> " << num_leading_bits << ";";
  }

@@ -195,8 +215,8 @@ void ScalarField::GenRustWriter(std::ostream& s, Size start_offset, Size end_off
  Size size = GetSize();
  int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());

  if (GetFieldType() == SizeField::kFieldType) {
    // Do nothing, the field access has already happened in packet_def
  if (GetFieldType() == SizeField::kFieldType || GetFieldType() == FixedScalarField::kFieldType) {
    // Do nothing, the field access has already happened
  } else if (GetRustParseDataType() != GetRustDataType()) {
    // needs casting to primitive
    s << "let " << GetName() << " = self." << GetName() << ".to_" << GetRustParseDataType() << "().unwrap();";
@@ -222,11 +242,11 @@ void ScalarField::GenRustWriter(std::ostream& s, Size start_offset, Size end_off
      mask |= 1;
    }
    s << "let " << GetName() << " = (" << GetName() << " << " << num_leading_bits << ") | ("
      << "(buffer[" << start_offset.bytes() << "] as " << GetRustParseDataType() << ") & 0x" << std::hex << mask
      << std::dec << ");";
      << "(buffer[" << start_offset.bytes() + access_offset << "] as " << GetRustParseDataType() << ") & 0x" << std::hex
      << mask << std::dec << ");";
  }

  s << "buffer[" << start_offset.bytes() + access_offset << ".."
    << start_offset.bytes() + GetSize().bytes() + access_offset << "].copy_from_slice(&" << GetName()
    << ".to_le_bytes());";
    << ".to_le_bytes()[0.." << size.bytes() << "]);";
}
Loading