OSDN Git Service

PDL: fixed size type arrays
authorJakub Pawlowski <jpawlowski@google.com>
Fri, 31 May 2019 12:07:44 +0000 (14:07 +0200)
committerJakub Pawlowski <jpawlowski@google.com>
Fri, 31 May 2019 12:12:49 +0000 (14:12 +0200)
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

gd/packet/parser/fields/array_field.cc
gd/packet/parser/test/generated_packet_test.cc
gd/packet/parser/test/test_packets.pdl

index f41877a..bce768f 100644 (file)
@@ -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 << "}";
index 07240b4..e1963ae 100644 (file)
@@ -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
index d31bdcf..4c97c9d 100644 (file)
@@ -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],
+}