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

Commit ed50811f authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

PacketParser: ToString() method for PacketViews

This is a helper method that should make debugging easier.

Bug: 150669615
Test: added
Change-Id: I15aceaa6a5b7ef628bbc98d9a1ef37950ebc22a2
parent ab062b57
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -14,11 +14,11 @@ Packet Views and Builders

Checksum types
  checksum MyChecksumClass : 16 "path/to/the/class/"
  Checksum fields need to implement the following three static methods:
    static void Initialize(MyChecksumClass&);
    static void AddByte(MyChecksumClass&, uint8_t);
  Checksum fields need to implement the following three methods:
    void Initialize(MyChecksumClass&);
    void AddByte(MyChecksumClass&, uint8_t);
    // Assuming a 16-bit (uint16_t) checksum:
    static uint16_t GetChecksum(MyChecksumClass&);
    uint16_t GetChecksum(MyChecksumClass&);
-------------
 LIMITATIONS
-------------
@@ -57,3 +57,4 @@ Custom fields need the folowing functions:
  static void Serialize(const Type&, MutableView&);
  static std::optional<size_t> Size(Iterator);
  static Type Parse(Iterator);
  std::string ToString();
 No newline at end of file
+5 −2
Original line number Diff line number Diff line
@@ -36,14 +36,17 @@ class CustomTypeChecker {
  template <class C, bool little_endian, std::optional<Iterator<little_endian>> (*)(C* vec, Iterator<little_endian> it)>
  struct ParseChecker {};

  template <class C, std::string (C::*)() const>
  struct ToStringChecker {};

  template <class C, bool little_endian>
  static int Test(SerializeChecker<C, &C::Serialize>*, SizeChecker<C, &C::size>*,
                  ParseChecker<C, little_endian, &C::Parse>*);
                  ParseChecker<C, little_endian, &C::Parse>*, ToStringChecker<C, &C::ToString>*);

  template <class C, bool little_endian>
  static char Test(...);

  static constexpr bool value = (sizeof(Test<T, packet_little_endian>(0, 0, 0)) == sizeof(int));
  static constexpr bool value = (sizeof(Test<T, packet_little_endian>(0, 0, 0, 0)) == sizeof(int));
};
}  // namespace packet
}  // namespace bluetooth
+20 −0
Original line number Diff line number Diff line
@@ -175,3 +175,23 @@ bool ArrayField::IsContainerField() const {
const PacketField* ArrayField::GetElementField() const {
  return element_field_;
}

void ArrayField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
  s << "\"ARRAY[\";";
  s << "/* " << element_field_->GetDataType() << "   " << element_field_->GetFieldType() << " */";

  std::string arr_idx = "arridx_" + accessor;
  std::string arr_size = std::to_string(array_size_);
  s << "for (size_t index = 0; index < " << arr_size << "; index++) {";
  std::string element_accessor = "(" + accessor + "[index])";
  s << "ss << ((index == 0) ? \"\" : \", \") << ";

  if (element_field_->GetFieldType() == CustomField::kFieldType) {
    s << element_accessor << ".ToString()";
  } else {
    element_field_->GenStringRepresentation(s, element_accessor);
  }

  s << ";}";
  s << "ss << \"]\"";
}
+2 −0
Original line number Diff line number Diff line
@@ -62,6 +62,8 @@ class ArrayField : public PacketField {

  virtual const PacketField* GetElementField() const override;

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

  const std::string name_;

  const PacketField* element_field_{nullptr};
+4 −0
Original line number Diff line number Diff line
@@ -71,3 +71,7 @@ void BodyField::GenInserter(std::ostream&) const {
void BodyField::GenValidator(std::ostream&) const {
  // Do nothing
}

void BodyField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
  s << "\"BODY REPRENTATION_UNIMPLEMENTED " << accessor << " \"";
}
Loading