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

Commit 82caf38d authored by Myles Watson's avatar Myles Watson
Browse files

PDL: Add GetStructOffset and GetStructSize

Structs are parsed all at once, so they don't need as much
information as packets.

Bug: 138260498
Test: bluetooth_packet_parser_test
Change-Id: I49d433332874514d2cc1c5779865437fab5533b4
parent a630ba0f
Loading
Loading
Loading
Loading
+14 −0
Original line number Original line Diff line number Diff line
@@ -65,6 +65,20 @@ Size ArrayField::GetBuilderSize() const {
  }
  }
}
}


Size ArrayField::GetStructSize() const {
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    return GetSize();
  } else if (element_field_->BuilderParameterMustBeMoved()) {
    std::string ret = "[this](){ size_t length = 0; for (const auto& elem : to_fill->" + GetName() +
                      "_) { length += elem->size() * 8; } return length; }()";
    return ret;
  } else {
    std::string ret = "[this](){ size_t length = 0; for (const auto& elem : to_fill->" + GetName() +
                      "_) { length += elem.size() * 8; } return length; }()";
    return ret;
  }
}

std::string ArrayField::GetDataType() const {
std::string ArrayField::GetDataType() const {
  return "std::array<" + element_field_->GetDataType() + "," + std::to_string(array_size_) + ">";
  return "std::array<" + element_field_->GetDataType() + "," + std::to_string(array_size_) + ">";
}
}
+2 −0
Original line number Original line Diff line number Diff line
@@ -34,6 +34,8 @@ class ArrayField : public PacketField {


  virtual Size GetBuilderSize() const override;
  virtual Size GetBuilderSize() const override;


  virtual Size GetStructSize() const override;

  virtual std::string GetDataType() const override;
  virtual std::string GetDataType() const override;


  virtual void GenExtractor(std::ostream& s, int num_leading_bits, bool for_struct) const override;
  virtual void GenExtractor(std::ostream& s, int num_leading_bits, bool for_struct) const override;
+4 −0
Original line number Original line Diff line number Diff line
@@ -36,6 +36,10 @@ Size PacketField::GetBuilderSize() const {
  return GetSize();
  return GetSize();
}
}


Size PacketField::GetStructSize() const {
  return GetSize();
}

int PacketField::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
int PacketField::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
  // In order to find field_begin and field_end, we must have two of the three Sizes.
  // In order to find field_begin and field_end, we must have two of the three Sizes.
  if ((start_offset.empty() && size.empty()) || (start_offset.empty() && end_offset.empty()) ||
  if ((start_offset.empty() && size.empty()) || (start_offset.empty() && end_offset.empty()) ||
+4 −0
Original line number Original line Diff line number Diff line
@@ -40,6 +40,10 @@ class PacketField : public Loggable {
  // For most field types, this will be the same as GetSize();
  // For most field types, this will be the same as GetSize();
  virtual Size GetBuilderSize() const;
  virtual Size GetBuilderSize() const;


  // Returns the size of the field in bits given the information in the parsed struct.
  // For most field types, this will be the same as GetSize();
  virtual Size GetStructSize() const;

  // Get the type of the field to be used in the builders constructor and
  // Get the type of the field to be used in the builders constructor and
  // variables.
  // variables.
  virtual std::string GetDataType() const = 0;
  virtual std::string GetDataType() const = 0;
+25 −3
Original line number Original line Diff line number Diff line
@@ -52,14 +52,14 @@ Size VectorField::GetSize() const {


  // size_field_ is of type SIZE
  // size_field_ is of type SIZE
  if (size_field_->GetFieldType() == SizeField::kFieldType) {
  if (size_field_->GetFieldType() == SizeField::kFieldType) {
    std::string ret = "(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "() * 8)";
    std::string ret = "(static_cast<size_t>(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "()) * 8)";
    if (!size_modifier_.empty()) ret += size_modifier_;
    if (!size_modifier_.empty()) ret += size_modifier_;
    return ret;
    return ret;
  }
  }


  // size_field_ is of type COUNT and elements have a fixed size
  // size_field_ is of type COUNT and elements have a fixed size
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    return "(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "() * " +
    return "(static_cast<size_t>(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "()) * " +
           std::to_string(element_size_.bits()) + ")";
           std::to_string(element_size_.bits()) + ")";
  }
  }


@@ -68,7 +68,7 @@ Size VectorField::GetSize() const {


Size VectorField::GetBuilderSize() const {
Size VectorField::GetBuilderSize() const {
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    std::string ret = "(" + GetName() + "_.size() * " + std::to_string(element_size_.bits()) + ")";
    std::string ret = "(static_cast<size_t>(" + GetName() + "_.size()) * " + std::to_string(element_size_.bits()) + ")";
    return ret;
    return ret;
  } else if (element_field_->BuilderParameterMustBeMoved()) {
  } else if (element_field_->BuilderParameterMustBeMoved()) {
    std::string ret = "[this](){ size_t length = 0; for (const auto& elem : " + GetName() +
    std::string ret = "[this](){ size_t length = 0; for (const auto& elem : " + GetName() +
@@ -81,6 +81,28 @@ Size VectorField::GetBuilderSize() const {
  }
  }
}
}


Size VectorField::GetStructSize() const {
  // If there is no size field, then it is of unknown size.
  if (size_field_ == nullptr) {
    return Size();
  }

  // size_field_ is of type SIZE
  if (size_field_->GetFieldType() == SizeField::kFieldType) {
    std::string ret = "(static_cast<size_t>(" + size_field_->GetName() + "_extracted) * 8)";
    if (!size_modifier_.empty()) ret += size_modifier_;
    return ret;
  }

  // size_field_ is of type COUNT and elements have a fixed size
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    return "(static_cast<size_t>(" + size_field_->GetName() + "_extracted) * " + std::to_string(element_size_.bits()) +
           ")";
  }

  return Size();
}

std::string VectorField::GetDataType() const {
std::string VectorField::GetDataType() const {
  return "std::vector<" + element_field_->GetDataType() + ">";
  return "std::vector<" + element_field_->GetDataType() + ">";
}
}
Loading