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

Commit 309dde82 authored by Zach Johnson's avatar Zach Johnson Committed by Automerger Merge Worker
Browse files

Merge changes I992b781d,Ic2cfde11,I8e8d5a05 am: cbbb8b64

Original change: https://android-review.googlesource.com/c/platform/system/bt/+/1527420

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Id07dfc259eb6efdbfa4bdb817696b0fc2b9ef0df
parents fd1e1cab cbbb8b64
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -55,3 +55,7 @@ void EnumField::GenValidator(std::ostream&) const {
void EnumField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
  s << GetDataType() << "Text(" << accessor << ")";
}

std::string EnumField::GetRustDataType() const {
  return enum_def_.name_;
}
+2 −0
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ class EnumField : public ScalarField {

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

  virtual std::string GetRustDataType() const override;

 private:
  EnumDef enum_def_;
  std::string value_;
+20 −5
Original line number Diff line number Diff line
@@ -138,6 +138,10 @@ std::string ScalarField::GetRustDataType() const {
  return util::GetRustTypeForSize(size_);
}

std::string ScalarField::GetRustParseDataType() const {
  return util::GetRustTypeForSize(size_);
}

int ScalarField::GetRustBitOffset(
    std::ostream&, Size start_offset, Size end_offset, Size size) const {
  int num_leading_bits = 0;
@@ -161,10 +165,10 @@ void ScalarField::GenRustGetter(std::ostream& s, Size start_offset, Size end_off

  s << "let " << GetName() << " = ";
  if (num_leading_bits == 0) {
    s << GetRustDataType() << "::from_le_bytes(bytes[" << start_offset.bytes() << "..";
    s << GetRustParseDataType() << "::from_le_bytes(bytes[" << start_offset.bytes() << "..";
    s << start_offset.bytes() + size.bytes() << "].try_into().unwrap());";
  } else {
    s << GetRustDataType() << "::from_le_bytes(bytes[" << start_offset.bytes() - 1 << "..";
    s << GetRustParseDataType() << "::from_le_bytes(bytes[" << start_offset.bytes() - 1 << "..";
    s << start_offset.bytes() + size.bytes() - 1 << "].try_into().unwrap());";
    s << "let " << GetName() << " = " << GetName() << " >> " << num_leading_bits << ";";
  }
@@ -178,13 +182,24 @@ void ScalarField::GenRustGetter(std::ostream& s, Size start_offset, Size end_off
    s << "let " << GetName() << " = ";
    s << GetName() << " & 0x" << std::hex << mask << std::dec << ";";
  }

  // needs casting from primitive
  if (GetRustParseDataType() != GetRustDataType()) {
    s << "let " << GetName() << " = ";
    s << GetRustDataType() << "::from_" << GetRustParseDataType() << "(" << GetName() << ").unwrap();";
  }
}

void ScalarField::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());

  // needs casting to primitive
  if (GetRustParseDataType() != GetRustDataType()) {
    s << "let " << GetName() << " = self." << GetName() << ".to_" << GetRustParseDataType() << "().unwrap();";
  } else {
    s << "let " << GetName() << " = self." << GetName() << ";";
  }
  if (util::RoundSizeUp(size.bits()) != size.bits()) {
    uint64_t mask = 0;
    for (int i = 0; i < size.bits(); i++) {
@@ -204,8 +219,8 @@ 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 " << GetRustDataType() << ") & 0x" << std::hex << mask << std::dec
      << ");";
      << "(buffer[" << start_offset.bytes() << "] as " << GetRustParseDataType() << ") & 0x" << std::hex << mask
      << std::dec << ");";
  }

  s << "buffer[" << start_offset.bytes() + access_offset << ".."
+2 −0
Original line number Diff line number Diff line
@@ -53,6 +53,8 @@ class ScalarField : public PacketField {

  virtual std::string GetRustDataType() const override;

  virtual std::string GetRustParseDataType() const;

  virtual int GetRustBitOffset(std::ostream& s, Size start_offset,
      Size end_offset, Size size) const override;

+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ void generate_rust_packet_preamble(std::ostream& s) {
      R"(
use bytes::{Bytes, BytesMut};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use num_traits::{FromPrimitive, ToPrimitive};
use std::convert::TryInto;
use thiserror::Error;
use std::rc::Rc;
Loading