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

Commit 06951b95 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

PDL: fixed size type arrays

Currently attempt to add fixed size array is causing segfault. This
patch fixes that. It also adds a test to verify those arrays are
properly encoded and decoded.

Test: bluetooth_packet_parser_test
Change-Id: I09c7e5595a7d1819a43e9203846bb487fb23aca3
parent 98fae21a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -113,7 +113,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;";
  std::string type = type_def_->name_;
  std::string type = (type_def_ != nullptr) ? type_def_->name_ : util::GetTypeForSize(element_size_);
  s << "while (it + sizeof(" << type << ") <= array_end) {";
  s << "ret.push_back(it.extract<" << type << ">());";
  s << "}";
+38 −0
Original line number Diff line number Diff line
@@ -511,6 +511,44 @@ TEST(GeneratedPacketTest, testCountArrayEnum) {
    ASSERT_EQ(array[i], count_array[i]);
  }
}

TEST(GeneratedPacketTest, testFixedSizeByteArray) {
  constexpr int byte_array_size = 32;
  std::vector<uint8_t> byte_array(byte_array_size);
  for (uint8_t i = 0; i < byte_array_size; i++) byte_array[i] = i;

  constexpr int word_array_size = 8;
  std::vector<uint32_t> word_array(word_array_size);
  for (uint32_t i = 0; i < word_array_size; i++) word_array[i] = i;

  auto packet = PacketWithFixedArraysOFBytesBuilder::Create(byte_array, word_array);

  std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
  BitInserter it(*packet_bytes);
  packet->Serialize(it);

  ASSERT_EQ(byte_array_size + word_array_size * sizeof(uint32_t), packet_bytes->size());

  for (size_t i = 0; i < byte_array_size; i++) {
    ASSERT_EQ(byte_array[i], packet_bytes->at(i));
  }

  PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
  auto view = PacketWithFixedArraysOFBytesView::Create(packet_bytes_view);
  ASSERT_TRUE(view.IsValid());
  auto array = view.GetFixed256bitInBytes();
  ASSERT_EQ(byte_array.size(), array.size());
  for (size_t i = 0; i < array.size(); i++) {
    ASSERT_EQ(array[i], byte_array[i]);
  }

  auto decoded_word_array = view.GetFixed256bitInWords();
  ASSERT_EQ(word_array.size(), decoded_word_array.size());
  for (size_t i = 0; i < decoded_word_array.size(); i++) {
    ASSERT_EQ(word_array[i], decoded_word_array[i]);
  }
}

}  // namespace parser
}  // namespace packet
}  // namespace bluetooth
+5 −0
Original line number Diff line number Diff line
@@ -124,3 +124,8 @@ packet CountArrayCustom {
  _count_(six_bytes_array) : 8,
  six_bytes_array : SixBytes[],
}

packet PacketWithFixedArraysOFBytes {
   fixed_256bit_in_bytes : 8[32],
   fixed_256bit_in_words : 32[8],
}