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

Commit 2760273c authored by Myles Watson's avatar Myles Watson Committed by Jakub Pawlowski
Browse files

PDL: GetType->GetDataType, GetFieldType->string

Test: bluetooth_packet_parser_test
Change-Id: I5891535131778638becd2ef491644c25560ba0a0
parent ad8210f5
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -5,9 +5,12 @@ cc_binary_host {
        "fields/body_field.cc",
        "fields/checksum_field.cc",
        "fields/checksum_start_field.cc",
        "fields/count_field.cc",
        "fields/custom_field.cc",
        "fields/enum_field.cc",
        "fields/fixed_enum_field.cc",
        "fields/fixed_field.cc",
        "fields/fixed_scalar_field.cc",
        "fields/group_field.cc",
        "fields/packet_field.cc",
        "fields/payload_field.cc",
+12 −12
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <set>
#include <vector>

#include "fields/all_fields.h"
#include "fields/packet_field.h"

using FieldListIterator = std::vector<PacketField*>::const_iterator;
@@ -70,7 +71,7 @@ class FieldList {
    FieldList ret;
    for (auto it = begin(); it != end(); it++) {
      const auto& field = *it;
      if (field->GetFieldType() == PacketField::Type::PAYLOAD || field->GetFieldType() == PacketField::Type::BODY) {
      if (field->GetFieldType() == PayloadField::kFieldType || field->GetFieldType() == BodyField::kFieldType) {
        break;
      }
      ret.AppendField(*it);
@@ -83,7 +84,7 @@ class FieldList {
    FieldListIterator it;
    for (it = begin(); it != end(); it++) {
      const auto& field = *it;
      if (field->GetFieldType() == PacketField::Type::PAYLOAD || field->GetFieldType() == PacketField::Type::BODY) {
      if (field->GetFieldType() == PayloadField::kFieldType || field->GetFieldType() == BodyField::kFieldType) {
        // Increment it once to get first field after payload/body.
        it++;
        break;
@@ -93,7 +94,7 @@ class FieldList {
    return FieldList(it, end());
  }

  FieldList GetFieldsWithTypes(std::set<PacketField::Type> field_types) const {
  FieldList GetFieldsWithTypes(std::set<std::string> field_types) const {
    FieldList ret;

    for (const auto& field : field_list_) {
@@ -105,7 +106,7 @@ class FieldList {
    return ret;
  }

  FieldList GetFieldsWithoutTypes(std::set<PacketField::Type> field_types) const {
  FieldList GetFieldsWithoutTypes(std::set<std::string> field_types) const {
    FieldList ret;

    for (const auto& field : field_list_) {
@@ -182,20 +183,19 @@ class FieldList {
 private:
  void AddField(PacketField* field) {
    if (field_map_.find(field->GetName()) != field_map_.end()) {
      ERROR(field) << "Field with name \"" << field->GetName() << "\" was "
                   << "previously defined.\n";
      ERROR(field) << "Field with name \"" << field->GetName() << "\" was previously defined.\n";
    }

    if (field->GetFieldType() == PacketField::Type::PAYLOAD) {
      if (HasBody()) {
        ERROR(field) << "Can not have payload field in packet that already has a body.";
    if (field->GetFieldType() == PayloadField::kFieldType) {
      if (has_body_) {
        ERROR(field) << "Packet already has a body.";
      }
      has_payload_ = true;
    }

    if (field->GetFieldType() == PacketField::Type::BODY) {
      if (HasPayload()) {
        ERROR(field) << "Can not have body field in packet that already has a payload.";
    if (field->GetFieldType() == BodyField::kFieldType) {
      if (has_payload_) {
        ERROR(field) << "Packet already has a payload.";
      }
      has_body_ = true;
    }
+3 −1
Original line number Diff line number Diff line
@@ -20,9 +20,11 @@
#include "fields/body_field.h"
#include "fields/checksum_field.h"
#include "fields/checksum_start_field.h"
#include "fields/count_field.h"
#include "fields/custom_field.h"
#include "fields/enum_field.h"
#include "fields/fixed_field.h"
#include "fields/fixed_enum_field.h"
#include "fields/fixed_scalar_field.h"
#include "fields/group_field.h"
#include "fields/payload_field.h"
#include "fields/reserved_field.h"
+13 −11
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@
#include "fields/array_field.h"
#include "util.h"

const std::string ArrayField::kFieldType = "ArrayField";

ArrayField::ArrayField(std::string name, int element_size, std::string size_modifier, ParseLocation loc)
    : PacketField(loc, name), element_size_(element_size), size_modifier_(size_modifier) {
    : PacketField(name, loc), element_size_(element_size), size_modifier_(size_modifier) {
  // Make sure the element_size is a multiple of 8.
  if (element_size % 8 != 0) {
    ERROR(this) << "Can only have arrays with elements that are byte aligned (" << element_size << ")";
@@ -26,7 +28,7 @@ ArrayField::ArrayField(std::string name, int element_size, std::string size_modi
}

ArrayField::ArrayField(std::string name, int element_size, int fixed_size, ParseLocation loc)
    : PacketField(loc, name), element_size_(element_size), fixed_size_(fixed_size) {
    : PacketField(name, loc), element_size_(element_size), fixed_size_(fixed_size) {
  // Make sure the element_size is a multiple of 8.
  if (element_size % 8 != 0) {
    ERROR(this) << "Can only have arrays with elements that are byte aligned (" << element_size << ")";
@@ -34,7 +36,7 @@ ArrayField::ArrayField(std::string name, int element_size, int fixed_size, Parse
}

ArrayField::ArrayField(std::string name, TypeDef* type_def, std::string size_modifier, ParseLocation loc)
    : PacketField(loc, name), element_size_(type_def->size_), type_def_(type_def), size_modifier_(size_modifier) {
    : PacketField(name, loc), element_size_(type_def->size_), type_def_(type_def), size_modifier_(size_modifier) {
  // If the element type is not variable sized, make sure that it is byte aligned.
  if (type_def_->size_ != -1 && type_def_->size_ % 8 != 0) {
    ERROR(this) << "Can only have arrays with elements that are byte aligned (" << type_def_->size_ << ")";
@@ -42,15 +44,15 @@ ArrayField::ArrayField(std::string name, TypeDef* type_def, std::string size_mod
}

ArrayField::ArrayField(std::string name, TypeDef* type_def, int fixed_size, ParseLocation loc)
    : PacketField(loc, name), element_size_(type_def->size_), type_def_(type_def), fixed_size_(fixed_size) {
    : PacketField(name, loc), element_size_(type_def->size_), type_def_(type_def), fixed_size_(fixed_size) {
  // If the element type is not variable sized, make sure that it is byte aligned.
  if (type_def_->size_ != -1 && type_def_->size_ % 8 != 0) {
    ERROR(this) << "Can only have arrays with elements that are byte aligned (" << type_def_->size_ << ")";
  }
}

PacketField::Type ArrayField::GetFieldType() const {
  return PacketField::Type::ARRAY;
const std::string& ArrayField::GetFieldType() const {
  return ArrayField::kFieldType;
}

Size ArrayField::GetSize() const {
@@ -64,7 +66,7 @@ Size ArrayField::GetSize() const {
  }

  // size_field_ is of type SIZE
  if (size_field_->GetFieldType() == PacketField::Type::SIZE) {
  if (size_field_->GetFieldType() == SizeField::kFieldType) {
    std::string ret = "Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "()";
    if (!size_modifier_.empty()) ret += size_modifier_;
    return ret;
@@ -101,7 +103,7 @@ Size ArrayField::GetBuilderSize() const {
  }
}

std::string ArrayField::GetType() const {
std::string ArrayField::GetDataType() const {
  if (type_def_ != nullptr) {
    return "std::vector<" + type_def_->name_ + ">";
  }
@@ -121,7 +123,7 @@ void ArrayField::GenGetter(std::ostream& s, Size start_offset, Size end_offset)
    ERROR(this) << "Ambiguous end offset for array with no defined size.";
  }

  s << GetType();
  s << GetDataType();
  s << " Get" << util::UnderscoreToCamelCase(GetName()) << "() {";
  s << "ASSERT(was_validated_);";

@@ -135,7 +137,7 @@ void ArrayField::GenGetter(std::ostream& s, Size start_offset, Size end_offset)
  }

  // Add the element size so that we will extract as many elements as we can.
  s << GetType() << " ret;";
  s << GetDataType() << " ret;";
  if (element_size_ != -1) {
    std::string type = (type_def_ != nullptr) ? type_def_->name_ : util::GetTypeForSize(element_size_);
    s << "while (it + sizeof(" << type << ") <= array_end) {";
@@ -221,7 +223,7 @@ bool ArrayField::IsFixedSize() const {
}

void ArrayField::SetSizeField(const SizeField* size_field) {
  if (size_field->GetFieldType() == PacketField::Type::COUNT && !size_modifier_.empty()) {
  if (size_field->GetFieldType() == CountField::kFieldType && !size_modifier_.empty()) {
    ERROR(this, size_field) << "Can not use count field to describe array with a size modifier."
                            << " Use size instead";
  }
+5 −3
Original line number Diff line number Diff line
@@ -18,10 +18,10 @@

#include "custom_field_def.h"
#include "enum_def.h"
#include "fields/count_field.h"
#include "fields/packet_field.h"
#include "fields/size_field.h"
#include "parse_location.h"
//#include "struct_def.h"

class ArrayField : public PacketField {
 public:
@@ -33,13 +33,15 @@ class ArrayField : public PacketField {

  ArrayField(std::string name, TypeDef* type_def, int fixed_size, ParseLocation loc);

  virtual PacketField::Type GetFieldType() const override;
  static const std::string kFieldType;

  virtual const std::string& GetFieldType() const override;

  virtual Size GetSize() const override;

  virtual Size GetBuilderSize() const override;

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

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

Loading