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

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

Merge "rusty-gd: Serialize size fields." am: 4a2da16a

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

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Iee493eb11ea4e74667eb12cc1eb525e57d0429dc
parents 6154a238 4a2da16a
Loading
Loading
Loading
Loading
+35 −0
Original line number Original line Diff line number Diff line
@@ -65,3 +65,38 @@ std::string SizeField::GetSizedFieldName() const {
void SizeField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
void SizeField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
  s << accessor;
  s << accessor;
}
}

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

  s << "let mut " << GetName() << ": " << GetRustDataType() << " = self.get_total_size() as ";
  s << GetRustDataType() << ";";
  s << GetName() << " -= self.get_size() as " << GetRustDataType() << ";";
  if (util::RoundSizeUp(size.bits()) != size.bits()) {
    uint64_t mask = 0;
    for (int i = 0; i < size.bits(); i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "let " << GetName() << " = ";
    s << GetName() << " & 0x" << std::hex << mask << std::dec << ";";
  }

  int access_offset = 0;
  if (num_leading_bits != 0) {
    access_offset = -1;
    uint64_t mask = 0;
    for (int i = 0; i < num_leading_bits; i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "let " << GetName() << " = (" << GetName() << " << " << num_leading_bits << ") | ("
      << "(buffer[" << start_offset.bytes() << "] 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());";
}
+2 −0
Original line number Original line Diff line number Diff line
@@ -48,6 +48,8 @@ class SizeField : public ScalarField {


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


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

 private:
 private:
  int size_;
  int size_;
  std::string sized_field_name_;
  std::string sized_field_name_;
+1 −3
Original line number Original line Diff line number Diff line
@@ -871,7 +871,6 @@ void PacketDef::GenRustStructSizeField(std::ostream& s) const {
  auto fields = fields_.GetFieldsWithoutTypes({
  auto fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
      BodyField::kFieldType,
      CountField::kFieldType,
      CountField::kFieldType,
      SizeField::kFieldType,
  });
  });
  for (int i = 0; i < fields.size(); i++) {
  for (int i = 0; i < fields.size(); i++) {
    size += fields[i]->GetSize().bytes();
    size += fields[i]->GetSize().bytes();
@@ -998,7 +997,7 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
  // write_to function
  // write_to function
  s << "fn write_to(&self, buffer: &mut BytesMut) {";
  s << "fn write_to(&self, buffer: &mut BytesMut) {";
  if (fields.size() > 0) {
  if (fields.size() > 0) {
    s << " buffer.resize(buffer.len() + self.get_size(), 0);";
    s << " buffer.resize(buffer.len() + self.get_total_size(), 0);";
  }
  }


  fields = fields_.GetFieldsWithoutTypes({
  fields = fields_.GetFieldsWithoutTypes({
@@ -1006,7 +1005,6 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
      CountField::kFieldType,
      CountField::kFieldType,
      PaddingField::kFieldType,
      PaddingField::kFieldType,
      ReservedField::kFieldType,
      ReservedField::kFieldType,
      SizeField::kFieldType,
      FixedScalarField::kFieldType,
      FixedScalarField::kFieldType,
  });
  });