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

Commit ba7a3a1b authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: implement custom types & read address in controller

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: Ic8ddcac8fa2738e1282ca853a27450a56f1b54c7
parent a1d3bb59
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -507,6 +507,7 @@ rust_library {
        "libbytes",
        "libnum_traits",
        "libthiserror",
        "libbt_hci_custom_types",
    ],
}

+37 −0
Original line number Diff line number Diff line
@@ -31,6 +31,10 @@ std::string CustomFieldFixedSize::GetDataType() const {
  return type_name_;
}

std::string CustomFieldFixedSize::GetRustDataType() const {
  return type_name_;
}

int CustomFieldFixedSize::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
  if (!start_offset.empty()) {
    // Default to start if available.
@@ -68,3 +72,36 @@ void CustomFieldFixedSize::GenStringRepresentation(std::ostream& s, std::string
  // We assume that custom fields will have a ToString() method
  s << accessor << ".ToString()";
}

std::string CustomFieldFixedSize::GetRustParseDataType() const {
  return "[u8; " + std::to_string(GetSize().bytes()) + "]";
}

void CustomFieldFixedSize::GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  Size size = GetSize();
  int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());
  if (num_leading_bits != 0) {
    ERROR(this) << "must be byte aligned";
  }
  if (size.bits() % 8 != 0) {
    ERROR(this) << "size must be in full bytes";
  }

  s << "let " << GetName() << " = bytes[" << start_offset.bytes() << "..";
  s << start_offset.bytes() + size.bytes() << "].try_into().unwrap();";
}

void CustomFieldFixedSize::GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const {
  Size size = GetSize();
  int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());
  if (num_leading_bits != 0) {
    ERROR(this) << "must be byte aligned";
  }
  if (size.bits() % 8 != 0) {
    ERROR(this) << "size must be in full bytes";
  }

  s << "let " << GetName() << ": " << GetRustParseDataType() << " = self." << GetName() << ".into();";
  s << "buffer[" << start_offset.bytes() << ".." << start_offset.bytes() + GetSize().bytes() << "].copy_from_slice(&"
    << GetName() << ");";
}
+8 −0
Original line number Diff line number Diff line
@@ -43,5 +43,13 @@ class CustomFieldFixedSize : public ScalarField {

  virtual void GenStringRepresentation(std::ostream& s, std::string accessor) const override;

  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;

  virtual std::string GetRustDataType() const override;

  virtual std::string GetRustParseDataType() const override;

  std::string type_name_;
};
+0 −7
Original line number Diff line number Diff line
@@ -37,13 +37,6 @@ pub enum Error {
  InvalidPacketError
}

pub struct Address {
  pub addr: [u8; 6],
}

pub struct ClassOfDevice {
  pub cod: [u8; 3],
}
)";
}

+9 −0
Original line number Diff line number Diff line
@@ -18,8 +18,17 @@ rust_library {
        "libgddi",
        "liblog_rust",
        "libbt_common",
        "libbt_hci_custom_types",
    ],
    proc_macros: [
        "libnum_derive",
    ],
}

rust_library {
    name: "libbt_hci_custom_types",
    defaults: ["gd_rust_defaults"],
    crate_name: "bt_hci_custom_types",
    srcs: ["custom_types/lib.rs"],
    edition: "2018",
}
Loading