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

Commit 743e4e91 authored by Myles Watson's avatar Myles Watson
Browse files

PDL: Extract CustomFieldFixedSize

Test: bluetooth_packet_parser_test
Change-Id: Ia93442ca1cc173d0b6c813a45c722f14d2f34fa0
parent f069e92e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ cc_binary_host {
        "fields/checksum_start_field.cc",
        "fields/count_field.cc",
        "fields/custom_field.cc",
        "fields/custom_field_fixed_size.cc",
        "fields/enum_field.cc",
        "fields/fixed_enum_field.cc",
        "fields/fixed_field.cc",
+5 −1
Original line number Diff line number Diff line
@@ -28,7 +28,11 @@ CustomFieldDef::CustomFieldDef(std::string name, std::string include, int size)
}

PacketField* CustomFieldDef::GetNewField(const std::string& name, ParseLocation loc) const {
  return new CustomField(name, name_, size_, loc);
  if (size_ == -1) {
    return new CustomField(name, name_, loc);
  } else {
    return new CustomFieldFixedSize(name, name_, size_, loc);
  }
}

TypeDef::Type CustomFieldDef::GetDefinitionType() const {
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <iostream>

#include "fields/custom_field.h"
#include "fields/custom_field_fixed_size.h"
#include "parse_location.h"
#include "type_def.h"

+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include "fields/checksum_start_field.h"
#include "fields/count_field.h"
#include "fields/custom_field.h"
#include "fields/custom_field_fixed_size.h"
#include "fields/enum_field.h"
#include "fields/fixed_enum_field.h"
#include "fields/fixed_scalar_field.h"
+9 −48
Original line number Diff line number Diff line
@@ -19,25 +19,21 @@

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

CustomField::CustomField(std::string name, std::string type_name, int size, ParseLocation loc)
    : PacketField(name, loc), type_name_(type_name), size_(size) {}
CustomField::CustomField(std::string name, std::string type_name, ParseLocation loc)
    : PacketField(name, loc), type_name_(type_name) {}

const std::string& CustomField::GetFieldType() const {
  return CustomField::kFieldType;
}

Size CustomField::GetSize() const {
  return size_;
  return Size();
}

Size CustomField::GetBuilderSize() const {
  if (size_ != -1) {
    return size_;
  } else {
  std::string ret = "(" + GetName() + "_.size() * 8) ";
  return ret;
}
}

std::string CustomField::GetDataType() const {
  return type_name_;
@@ -52,42 +48,11 @@ void CustomField::GenExtractor(std::ostream& s, Size start_offset, Size end_offs
}

void CustomField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  if (size_ != -1) {
    s << GetDataType();
  } else {
  s << "std::vector<" << GetDataType() << ">";
  }
  s << " Get" << util::UnderscoreToCamelCase(GetName()) << "() const {";

  s << "auto it = ";
  if (!start_offset.empty()) {
    // Default to start if available.
    if (start_offset.bits() % 8 != 0) {
      ERROR(this) << "Custom field must be byte aligned. start_offset.bits = " << start_offset.bits();
    }
    s << "begin() + (" << start_offset << ") / 8;";
  } else if (size_ != -1) {
    // If the size of the custom field is already known, we can determine it's offset based on end().
    if (!end_offset.empty()) {
      if (end_offset.bits() % 8) {
        ERROR(this) << "Custom field must be byte aligned. end_offset.bits = " << end_offset.bits();
      }

      s << "end() - (" << size_ << " + " << end_offset << ") / 8;";
    } else {
      ERROR(this) << "Ambiguous offset for fixed size custom field.";
    }
  } else {
    ERROR(this) << "Custom Field offset can not be determined from begin().";
  }

  if (size_ != -1) {
    s << "return it.extract<" << GetDataType() << ">();";
  } else {
    s << "std::vector<" << GetDataType() << "> to_return;";
    s << GetDataType() << "::Parse(to_return, it);";
    s << "return to_return;";
  }
  GenExtractor(s, start_offset, end_offset);
  s << "return vec;";
  s << "}\n";
}

@@ -105,12 +70,8 @@ void CustomField::GenParameterValidator(std::ostream&) const {
}

void CustomField::GenInserter(std::ostream& s) const {
  if (size_ != -1) {
    s << "insert(" << GetName() << "_, i);";
  } else {
  s << GetName() << "_.Serialize(i);";
}
}

void CustomField::GenValidator(std::ostream&) const {
  // Do nothing.
Loading