merge from upstream/master into main at 2023-06-13T19:32:30.008575Z

Change-Id: Idf4fdccd2282270df477bb3a2dd6b5dd1f1f89a5
diff --git a/src/Android.bp b/src/Android.bp
index 2838002..4c59eab 100755
--- a/src/Android.bp
+++ b/src/Android.bp
@@ -356,7 +356,7 @@
 genrule {
     name: "uwb_core_artifacts",
     tools: [
-        "pdl",
+        "pdlc",
         "soong_zip",
     ],
     cmd:
@@ -368,7 +368,7 @@
         "      $(genDir)/artifacts;" +
 
         // Generate uci_packets.rs at $(genDir)/artifacts/uwb_uci_packets/.
-        "$(location pdl) --output-format rust " +
+        "$(location pdlc) --output-format rust " +
         "      external/uwb/src/rust/uwb_uci_packets/uci_packets.pdl " +
         "      > $(genDir)/artifacts/uwb_uci_packets/uci_packets.rs;" +
 
diff --git a/src/rust/uwb_core/README.md b/src/rust/uwb_core/README.md
index 0601af1..f59c22b 100644
--- a/src/rust/uwb_core/README.md
+++ b/src/rust/uwb_core/README.md
@@ -5,11 +5,11 @@
 
 ## Building `uwb_uci_packets` package
 
-The `uwb_uci_packets` package depends on `pdl` and thus simply using `cargo
+The `uwb_uci_packets` package depends on `pdlc and thus simply using `cargo
 build` will fail. Follow the steps below before using cargo.
 
 1. Enter Android environment by `source build/make/rbesetup.sh; lunch <target>`
-2. Run `m pdl` to compile the `pdl` Rust binary.
+2. Run `m pdlc` to compile the `pdlc` Rust binary.
 
 After that, we could build or test the package by `cargo test --features proto`.
 
diff --git a/src/rust/uwb_core/protos/uwb_service.proto b/src/rust/uwb_core/protos/uwb_service.proto
index 944f82e..72cd879 100644
--- a/src/rust/uwb_core/protos/uwb_service.proto
+++ b/src/rust/uwb_core/protos/uwb_service.proto
@@ -186,6 +186,7 @@
   FIRA_RANGING_SESSION = 0;
   FIRA_DATA_TRANSFER = 1;
   CCC = 2;
+  DEVICE_TEST_MODE = 3;
 }
 
 // Represent uwb_uci_packets::UpdateMulticastListAction.
diff --git a/src/rust/uwb_core/src/proto/mappings.rs b/src/rust/uwb_core/src/proto/mappings.rs
index cb0e078..1535197 100644
--- a/src/rust/uwb_core/src/proto/mappings.rs
+++ b/src/rust/uwb_core/src/proto/mappings.rs
@@ -528,6 +528,7 @@
     FIRA_RANGING_SESSION => FiraRangingSession,
     FIRA_DATA_TRANSFER => FiraDataTransfer,
     CCC => Ccc,
+    DEVICE_TEST_MODE => DeviceTestMode,
 }
 
 enum_mapping! {
diff --git a/src/rust/uwb_core/src/uci/notification.rs b/src/rust/uwb_core/src/uci/notification.rs
index 2158850..2e955f6 100644
--- a/src/rust/uwb_core/src/uci/notification.rs
+++ b/src/rust/uwb_core/src/uci/notification.rs
@@ -201,6 +201,7 @@
             UciNotificationChild::UciVendor_B_Notification(evt) => vendor_notification(evt.into()),
             UciNotificationChild::UciVendor_E_Notification(evt) => vendor_notification(evt.into()),
             UciNotificationChild::UciVendor_F_Notification(evt) => vendor_notification(evt.into()),
+            UciNotificationChild::TestNotification(evt) => vendor_notification(evt.into()),
             _ => {
                 error!("Unknown UciNotification: {:?}", evt);
                 Err(Error::Unknown)
@@ -450,13 +451,16 @@
                 uwb_uci_packets::UciVendor_F_NotificationChild::None => Ok(Vec::new()),
             }
         }
+        uwb_uci_packets::UciNotificationChild::TestNotification(evt) => match evt.specialize() {
+            uwb_uci_packets::TestNotificationChild::Payload(payload) => Ok(payload.to_vec()),
+            uwb_uci_packets::TestNotificationChild::None => Ok(Vec::new()),
+        },
         _ => {
             error!("Unknown UciVendor packet: {:?}", evt);
             Err(Error::Unknown)
         }
     }
 }
-
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -483,8 +487,7 @@
                 rssi: u8::MAX,
             },
         ]);
-        let extended_ranging_measurements_copy = extended_ranging_measurements.clone();
-        assert_eq!(extended_ranging_measurements, extended_ranging_measurements_copy);
+        assert_eq!(extended_ranging_measurements, extended_ranging_measurements.clone());
         let empty_extended_ranging_measurements =
             RangingMeasurements::ExtendedAddressTwoWay(vec![]);
         assert_eq!(empty_short_ranging_measurements, empty_short_ranging_measurements);
@@ -815,4 +818,19 @@
             })
         );
     }
+
+    #[test]
+    fn test_test_to_vendor_notification_casting() {
+        let test_notification: uwb_uci_packets::UciNotification =
+            uwb_uci_packets::TestNotificationBuilder { opcode: 0x22, payload: None }.build().into();
+        let test_uci_notification = UciNotification::try_from(test_notification).unwrap();
+        assert_eq!(
+            test_uci_notification,
+            UciNotification::Vendor(RawUciMessage {
+                gid: 0x0d, // per enum Test GroupId in uci_packets.pdl
+                oid: 0x22,
+                payload: vec![],
+            })
+        );
+    }
 }
diff --git a/src/rust/uwb_core/src/uci/pcapng_uci_logger_factory.rs b/src/rust/uwb_core/src/uci/pcapng_uci_logger_factory.rs
index 7c191b7..e99bb8b 100644
--- a/src/rust/uwb_core/src/uci/pcapng_uci_logger_factory.rs
+++ b/src/rust/uwb_core/src/uci/pcapng_uci_logger_factory.rs
@@ -502,6 +502,7 @@
     fn test_no_preexisting_dir_created() {
         let dir_root = Path::new("./uwb_test_dir_123");
         let dir = dir_root.join("this/path/doesnt/exist");
+        let log_path = dir.join("log.pcapng");
         {
             let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
             let mut file_manager = PcapngUciLoggerFactoryBuilder::new()
@@ -515,12 +516,19 @@
             let packet_0 = UciVendor_A_NotificationBuilder { opcode: 0, payload: None }.build();
             logger_0.log_uci_control_packet(packet_0.into());
             // Sleep needed to guarantee handling pending logs before runtime goes out of scope.
-            thread::sleep(time::Duration::from_millis(10));
+            let mut timeout = 100;
+            let timeout_slice = 10;
+            loop {
+                if log_path.exists() || timeout == 0 {
+                    break;
+                }
+                thread::sleep(time::Duration::from_millis(timeout_slice));
+                timeout -= timeout_slice;
+            }
         }
         // Expect the dir was created.
         assert!(dir.is_dir());
         // Expect the log file exists.
-        let log_path = dir.join("log.pcapng");
         assert!(log_path.is_file());
         // Clear test dir
         let _ = fs::remove_dir_all(dir_root);
@@ -529,6 +537,7 @@
     #[test]
     fn test_single_file_write() {
         let dir = tempdir().unwrap();
+        let last_file_expected = dir.as_ref().to_owned().join("log.pcapng");
         {
             let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
             let mut file_manager = PcapngUciLoggerFactoryBuilder::new()
@@ -547,7 +556,15 @@
             let packet_2 = UciVendor_A_NotificationBuilder { opcode: 2, payload: None }.build();
             logger_0.log_uci_control_packet(packet_2.into());
             // Sleep needed to guarantee handling pending logs before runtime goes out of scope.
-            thread::sleep(time::Duration::from_millis(10));
+            let mut timeout = 100;
+            let timeout_slice = 10;
+            loop {
+                if last_file_expected.exists() || timeout == 0 {
+                    break;
+                }
+                thread::sleep(time::Duration::from_millis(timeout_slice));
+                timeout -= timeout_slice;
+            }
         }
         // Expect file log.pcapng consist of SHB->IDB(logger 0)->EPB(packet 0)->IDB(logger 1)
         // ->EPB(packet 1)->EPB(packet 2)
@@ -629,6 +646,7 @@
     #[test]
     fn test_file_switch_idb_unfit_case() {
         let dir = tempdir().unwrap();
+        let last_file_expected = dir.as_ref().to_owned().join("log_1.pcapng");
         {
             let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
             let mut file_manager_144 = PcapngUciLoggerFactoryBuilder::new()
@@ -648,7 +666,15 @@
             let packet_1 = UciVendor_A_NotificationBuilder { opcode: 1, payload: None }.build();
             logger_1.log_uci_control_packet(packet_1.into());
             // Sleep needed to guarantee handling pending logs before runtime goes out of scope.
-            thread::sleep(time::Duration::from_millis(10));
+            let mut timeout = 100;
+            let timeout_slice = 10;
+            loop {
+                if last_file_expected.exists() || timeout == 0 {
+                    break;
+                }
+                thread::sleep(time::Duration::from_millis(timeout_slice));
+                timeout -= timeout_slice;
+            }
         }
         // Expect (Old to new):
         // File 1: SHB->IDB->EPB->EPB (cannot fit next)
diff --git a/src/rust/uwb_uci_packets/build.rs b/src/rust/uwb_uci_packets/build.rs
index f001de5..240c3f2 100644
--- a/src/rust/uwb_uci_packets/build.rs
+++ b/src/rust/uwb_uci_packets/build.rs
@@ -27,9 +27,9 @@
         return;
     }
 
-    // The binary should be compiled by `m pdl` before calling cargo.
+    // The binary should be compiled by `m pdlc` before calling cargo.
     let output = Command::new("env")
-        .arg("pdl")
+        .arg("pdlc")
         .arg("--output-format")
         .arg("rust")
         .arg("uci_packets.pdl")
diff --git a/src/rust/uwb_uci_packets/uci_packets.pdl b/src/rust/uwb_uci_packets/uci_packets.pdl
index 54de313..7deb831 100644
--- a/src/rust/uwb_uci_packets/uci_packets.pdl
+++ b/src/rust/uwb_uci_packets/uci_packets.pdl
@@ -237,6 +237,8 @@
     UL_TDOA_TX_INTERVAL = 0x33,
     UL_TDOA_RANDOM_WINDOW = 0x34,
     STS_LENGTH = 0x35,
+    SUSPEND_RANGING_ROUNDS = 0x36,
+    UL_TDOA_NTF_REPORT_CONFIG = 0x37,
     UL_TDOA_DEVICE_ID = 0x38,
     UL_TDOA_TX_TIMESTAMP = 0x39,
     MIN_FRAMES_PER_RR = 0x3A,
@@ -426,6 +428,7 @@
     FIRA_RANGING_SESSION = 0x00,
     FIRA_DATA_TRANSFER = 0x01,
     CCC = 0xA0,
+    DEVICE_TEST_MODE = 0xD0,
 }
 
 enum MessageType: 3 {
@@ -1339,3 +1342,7 @@
 packet UciVendor_F_Notification : UciNotification (group_id = VENDOR_RESERVED_F) {
     _payload_,
 }
+
+packet TestNotification : UciNotification (group_id = TEST) {
+    _payload_,
+}