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

Commit 9d388bbd authored by Myles Watson's avatar Myles Watson
Browse files

PDL: Support Type Length Value in Structs

Bug: 138260498
Test: bluetooth_packet_parser_test
Change-Id: If2a83908c507f37915e9a8520a1797cf5fc704a5
parent 7e04a062
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ Size VectorField::GetStructSize() const {
  // 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_;
    if (!size_modifier_.empty()) ret += "-" + size_modifier_;
    return ret;
  }

+8 −3
Original line number Diff line number Diff line
@@ -349,9 +349,14 @@ void ParentDef::GenSerialize(std::ostream& s) const {
          s << vector_name + "bytes = ";
          s << vector_name << ".size() * ((" << vector->element_size_ << ") / 8);";
        }
        std::string modifier = vector->GetSizeModifier();
        if (modifier != "") {
          s << "static_assert((" << modifier << ")%8 == 0, \"Modifiers must be byte-aligned\");";
          s << vector_name << "bytes = ";
          s << vector_name << "bytes + (" << modifier << ") / 8;";
        }
        s << "ASSERT(" << vector_name + "bytes < (1 << " << field->GetSize().bits() << "));";
        s << "insert(" << vector_name << "bytes";
        s << vector->GetSizeModifier() << ", i, ";
        s << "insert(" << vector_name << "bytes, i, ";
        s << field->GetSize().bits() << ");";
      }
    } else if (field->GetFieldType() == ChecksumStartField::kFieldType) {
+37 −0
Original line number Diff line number Diff line
@@ -1722,6 +1722,43 @@ vector<uint8_t> one_struct_array_after_fixed{

DEFINE_AND_INSTANTIATE_OneGenericStructArrayAfterFixedReflectionTest(one_struct_array_after_fixed);

vector<uint8_t> one_length_type_value_struct{
    // _size_(value):16 type value
    0x04, 0x00, 0x01, 'o', 'n', 'e',            // ONE
    0x04, 0x00, 0x02, 't', 'w', 'o',            // TWO
    0x06, 0x00, 0x03, 't', 'h', 'r', 'e', 'e',  // THREE
};

DEFINE_AND_INSTANTIATE_OneLengthTypeValueStructReflectionTest(one_length_type_value_struct);

TEST(GeneratedPacketTest, testOneLengthTypeValueStruct) {
  std::shared_ptr<std::vector<uint8_t>> packet_bytes =
      std::make_shared<std::vector<uint8_t>>(one_length_type_value_struct);

  PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
  auto view = OneLengthTypeValueStructView::Create(packet_bytes_view);
  ASSERT_TRUE(view.IsValid());
  auto one = view.GetOneArray();
  size_t entry_id = 0;
  for (const auto& entry : one) {
    switch (entry_id++) {
      case 0:
        ASSERT_EQ(entry.type_, DataType::ONE);
        ASSERT_EQ(entry.value_, std::vector<uint8_t>({'o', 'n', 'e'}));
        break;
      case 1:
        ASSERT_EQ(entry.type_, DataType::TWO);
        ASSERT_EQ(entry.value_, std::vector<uint8_t>({'t', 'w', 'o'}));
        break;
      case 2:
        ASSERT_EQ(entry.type_, DataType::THREE);
        ASSERT_EQ(entry.value_, std::vector<uint8_t>({'t', 'h', 'r', 'e', 'e'}));
        break;
      default:
        ASSERT_EQ(entry.type_, DataType::UNUSED);
    }
  }
}
}  // namespace parser
}  // namespace packet
}  // namespace bluetooth
+19 −0
Original line number Diff line number Diff line
@@ -365,3 +365,22 @@ packet ParentWithOnlyFixed {
packet OneGenericStructArrayAfterFixed : ParentWithOnlyFixed {
  an_array : UnusedParentStruct[],
}

enum DataType : 8 {
  ONE = 0x01,
  TWO = 0x02,
  THREE = 0x03,
  FOUR = 0x04,
  FIVE = 0x05,
  UNUSED = 0x06,
}

struct LengthTypeValueStruct {
  _size_(value) : 16,
  type : DataType,
  value : 8[+1*8],
}

packet OneLengthTypeValueStruct {
  one_array : LengthTypeValueStruct[],
}