OSDN Git Service

rusty-gd: Changes to make the HCI inquiry test pass
authorQasim Javed <qasimj@google.com>
Fri, 5 Feb 2021 05:49:50 +0000 (21:49 -0800)
committerQasim Javed <qasimj@google.com>
Fri, 5 Feb 2021 22:21:59 +0000 (14:21 -0800)
These include:

Writing count fields
Correct parsing of vectors
Using the correct offset when parsing struct fields

P.S. Also fixes the tokio panic caused by missing handling of the else
branch in the snoop HAL.

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost DirectHciTest:test_inquiry_from_dut

Change-Id: I4bb884369d9d811c906e271c29460a3eb134a727

gd/packet/parser/fields/count_field.cc
gd/packet/parser/fields/count_field.h
gd/packet/parser/fields/struct_field.cc
gd/packet/parser/fields/vector_field.cc
gd/packet/parser/parent_def.cc
gd/packet/parser/struct_def.cc
gd/rust/hal/src/snoop.rs

index 8e1cf09..f4b3d90 100644 (file)
@@ -57,3 +57,9 @@ void CountField::GenValidator(std::ostream&) const {
 std::string CountField::GetSizedFieldName() const {
   return sized_field_name_;
 }
+
+void CountField::GenRustWriter(std::ostream& s, Size start_offset, Size) const {
+  s << "buffer[" << start_offset.bytes() << "..";
+  s << start_offset.bytes() + GetSize().bytes() << "].copy_from_slice(&";
+  s << "(self." << GetSizedFieldName() << ".len() as u8).to_le_bytes());";
+}
index c9cb673..db7a4e7 100644 (file)
@@ -44,6 +44,8 @@ class CountField : public ScalarField {
 
   virtual std::string GetSizedFieldName() const;
 
+  void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const override;
+
  private:
   int size_;
   std::string sized_field_name_;
index e41d74d..47b457a 100644 (file)
@@ -98,6 +98,8 @@ void StructField::GenRustGetter(std::ostream& s, Size start_offset, Size) const
   s << start_offset.bytes() + GetSize().bytes() << "]).unwrap();";
 }
 
-void StructField::GenRustWriter(std::ostream& s, Size, Size) const {
-  s << "self." << GetName() << ".write_to(buffer);";
+void StructField::GenRustWriter(std::ostream& s, Size start_offset, Size) const {
+  s << "let " << GetName() << " = &mut buffer[" << start_offset.bytes();
+  s << ".." << start_offset.bytes() + GetSize().bytes() << "];";
+  s << "self." << GetName() << ".write_to(" << GetName() << ");";
 }
index 768d8ca..9870eae 100644 (file)
@@ -285,8 +285,8 @@ void VectorField::GenRustGetter(std::ostream& s, Size start_offset, Size) const
          s << "bytes[" << start_offset.bytes() << "..].to_vec().chunks_exact(";
        } else {
           s << "bytes[" << start_offset.bytes() << "..(";
-          s << start_offset.bytes() << " + " << size_field_->GetName();
-          s << " as usize)].to_vec().chunks_exact(";
+          s << start_offset.bytes() << " + (" << size_field_->GetName() << " as usize * ";
+          s << GetElementField()->GetSize().bytes() << "))].to_vec().chunks_exact(";
        }
 
         s << element_size << ").into_iter().map(|i| ";
@@ -302,6 +302,15 @@ void VectorField::GenRustGetter(std::ostream& s, Size start_offset, Size) const
   }
 }
 
-void VectorField::GenRustWriter(std::ostream& s, Size, Size) const {
-  s << "unimplemented!();";
+void VectorField::GenRustWriter(std::ostream& s, Size start_offset, Size) const {
+  s << "for (i, e) in self." << GetName() << ".iter().enumerate() {";
+  if (GetElementField()->GetFieldType() == ScalarField::kFieldType) {
+    s << "buffer[" << start_offset.bytes() << "+i..";
+    s << start_offset.bytes() << "+i+" << GetElementField()->GetSize().bytes() << "]";
+    s << ".copy_from_slice(&e.to_le_bytes())";
+  } else {
+    s << "self." << GetName() << "[i].write_to(&mut buffer[" << start_offset.bytes() << "+i..";
+    s << start_offset.bytes() << "+i+" << GetElementField()->GetSize().bytes() << "]);";
+  }
+  s << "}";
 }
index 04bb3d9..682ea4b 100644 (file)
@@ -573,7 +573,6 @@ bool ParentDef::HasChildEnums() const {
 void ParentDef::GenRustWriteToFields(std::ostream& s) const {
   auto fields = fields_.GetFieldsWithoutTypes({
       BodyField::kFieldType,
-      CountField::kFieldType,
       PaddingField::kFieldType,
       ReservedField::kFieldType,
       FixedScalarField::kFieldType,
@@ -634,7 +633,6 @@ void ParentDef::GenSizeRetVal(std::ostream& s) const {
   int size = 0;
   auto fields = fields_.GetFieldsWithoutTypes({
       BodyField::kFieldType,
-      CountField::kFieldType,
   });
   for (int i = 0; i < fields.size(); i++) {
     size += fields[i]->GetSize().bits();
index 53bb816..929f3a6 100644 (file)
@@ -402,7 +402,7 @@ void StructDef::GenRustImpls(std::ostream& s) const {
   s << "})}\n";
 
   // write_to function
-  s << "fn write_to(&self, buffer: &mut BytesMut) {";
+  s << "fn write_to(&self, buffer: &mut [u8]) {";
   GenRustWriteToFields(s);
   s << "}\n";
 
index cd78019..88b2d54 100644 (file)
@@ -145,7 +145,8 @@ async fn provide_snooped_hal(config: SnoopConfig, raw_hal: RawHal, rt: Arc<Runti
                 Some(acl) = consume(&raw_hal.acl_rx) => {
                     acl_up_tx.send(acl.clone()).await.unwrap();
                     logger.log(Type::Acl, Direction::Up, acl.to_bytes()).await;
-                }
+                },
+                else => break,
             }
         }
     });