Add packet capture logger for wifi tests

This commit has below changes:
- Add mechanism to capture packet logs when test failed
- Enable this mechanism for wifi test classes that use fixed wifi
  channels and have available packet capturer

Bug: 165446308
Test: Local
Change-Id: I62ca08aa86464fd30436e90e0da6e8423bc95102
Merged-In: I62ca08aa86464fd30436e90e0da6e8423bc95102
diff --git a/acts/framework/acts/test_utils/wifi/WifiBaseTest.py b/acts/framework/acts/test_utils/wifi/WifiBaseTest.py
index a6f642a..2fb7d37 100644
--- a/acts/framework/acts/test_utils/wifi/WifiBaseTest.py
+++ b/acts/framework/acts/test_utils/wifi/WifiBaseTest.py
@@ -45,6 +45,13 @@
 
 
 class WifiBaseTest(BaseTestClass):
+
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = False
+        self.packet_log_2g = hostapd_constants.AP_DEFAULT_CHANNEL_2G
+        self.packet_log_5g = hostapd_constants.AP_DEFAULT_CHANNEL_5G
+
     def setup_class(self):
         if hasattr(self, 'attenuators') and self.attenuators:
             for attenuator in self.attenuators:
@@ -58,6 +65,10 @@
                 self.cnss_diag_file = os.path.join(
                     self.user_params[Config.key_config_path.value],
                     self.cnss_diag_file)
+        if self.enable_packet_log and hasattr(self, "packet_capture"):
+            self.packet_logger = self.packet_capture[0]
+            self.packet_logger.configure_monitor_mode("2G", self.packet_log_2g)
+            self.packet_logger.configure_monitor_mode("5G", self.packet_log_5g)
 
     def setup_test(self):
         if (hasattr(self, "android_devices") and
@@ -70,6 +81,9 @@
             for ad in self.android_devices:
                 proc = nutils.start_tcpdump(ad, self.test_name)
                 self.tcpdump_proc.append((ad, proc))
+        if hasattr(self, "packet_logger"):
+            self.packet_log_pid = wutils.start_pcap(
+                    self.packet_logger, 'dual', self.test_name)
 
     def teardown_test(self):
         if (hasattr(self, "android_devices") and
@@ -80,6 +94,10 @@
             nutils.stop_tcpdump(
                     proc[0], proc[1], self.test_name, pull_dump=False)
         self.tcpdump_proc = []
+        if hasattr(self, "packet_logger") and self.packet_log_pid:
+            wutils.stop_pcap(
+                    self.packet_logger, self.packet_log_pid, test_status=True)
+            self.packet_log_pid = {}
 
     def on_fail(self, test_name, begin_time):
         if hasattr(self, "android_devices"):
@@ -95,6 +113,10 @@
         for proc in self.tcpdump_proc:
             nutils.stop_tcpdump(proc[0], proc[1], self.test_name)
         self.tcpdump_proc = []
+        if hasattr(self, "packet_logger") and self.packet_log_pid:
+            wutils.stop_pcap(
+                    self.packet_logger, self.packet_log_pid, test_status=False)
+            self.packet_log_pid = {}
 
     def get_psk_network(
             self,
diff --git a/acts/tests/google/wifi/WifiCrashStressTest.py b/acts/tests/google/wifi/WifiCrashStressTest.py
index b643bd3..02584ef 100644
--- a/acts/tests/google/wifi/WifiCrashStressTest.py
+++ b/acts/tests/google/wifi/WifiCrashStressTest.py
@@ -33,6 +33,10 @@
     * One Wi-Fi network visible to the device.
     """
 
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
+
     def setup_class(self):
         super().setup_class()
 
diff --git a/acts/tests/google/wifi/WifiCrashTest.py b/acts/tests/google/wifi/WifiCrashTest.py
index e031988..7f2ad68 100644
--- a/acts/tests/google/wifi/WifiCrashTest.py
+++ b/acts/tests/google/wifi/WifiCrashTest.py
@@ -42,6 +42,9 @@
     * One Android device
     * One Wi-Fi network visible to the device.
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()
diff --git a/acts/tests/google/wifi/WifiEnterpriseRoamingTest.py b/acts/tests/google/wifi/WifiEnterpriseRoamingTest.py
index ca1c015..05b3549 100644
--- a/acts/tests/google/wifi/WifiEnterpriseRoamingTest.py
+++ b/acts/tests/google/wifi/WifiEnterpriseRoamingTest.py
@@ -34,6 +34,11 @@
 
 
 class WifiEnterpriseRoamingTest(WifiBaseTest):
+
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
+
     def setup_class(self):
         super().setup_class()
 
diff --git a/acts/tests/google/wifi/WifiEnterpriseTest.py b/acts/tests/google/wifi/WifiEnterpriseTest.py
index 20c1b7c..aee773c 100644
--- a/acts/tests/google/wifi/WifiEnterpriseTest.py
+++ b/acts/tests/google/wifi/WifiEnterpriseTest.py
@@ -34,6 +34,11 @@
 
 
 class WifiEnterpriseTest(WifiBaseTest):
+
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
+
     def setup_class(self):
         super().setup_class()
 
diff --git a/acts/tests/google/wifi/WifiHiddenSSIDTest.py b/acts/tests/google/wifi/WifiHiddenSSIDTest.py
index df18a04..da2c066 100644
--- a/acts/tests/google/wifi/WifiHiddenSSIDTest.py
+++ b/acts/tests/google/wifi/WifiHiddenSSIDTest.py
@@ -29,6 +29,9 @@
     * Several Wi-Fi networks visible to the device, including an open Wi-Fi
       network.
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()
diff --git a/acts/tests/google/wifi/WifiManagerTest.py b/acts/tests/google/wifi/WifiManagerTest.py
index f5702fc..8b4c197 100644
--- a/acts/tests/google/wifi/WifiManagerTest.py
+++ b/acts/tests/google/wifi/WifiManagerTest.py
@@ -47,6 +47,10 @@
       network.
     """
 
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
+
     def setup_class(self):
         super().setup_class()
 
diff --git a/acts/tests/google/wifi/WifiNetworkRequestTest.py b/acts/tests/google/wifi/WifiNetworkRequestTest.py
index 4190de4..c2671fd 100644
--- a/acts/tests/google/wifi/WifiNetworkRequestTest.py
+++ b/acts/tests/google/wifi/WifiNetworkRequestTest.py
@@ -45,6 +45,9 @@
     * Several Wi-Fi networks visible to the device, including an open Wi-Fi
       network.
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()
diff --git a/acts/tests/google/wifi/WifiNetworkSelectorTest.py b/acts/tests/google/wifi/WifiNetworkSelectorTest.py
index 0e38da8..349df17 100644
--- a/acts/tests/google/wifi/WifiNetworkSelectorTest.py
+++ b/acts/tests/google/wifi/WifiNetworkSelectorTest.py
@@ -48,6 +48,9 @@
     """These tests verify the behavior of the Android Wi-Fi Network Selector
     feature.
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()
@@ -62,8 +65,6 @@
         self.dut.droid.wakeLockAcquireBright()
         self.dut.droid.wakeUpNow()
         self.dut.ed.clear_all_events()
-        self.pcap_procs = wutils.start_pcap(
-            self.packet_capture, 'dual', self.test_name)
         for a in self.attenuators:
             a.set_atten(MAX_ATTN)
         time.sleep(ATTN_SLEEP)
@@ -76,13 +77,6 @@
         self.dut.droid.wakeLockRelease()
         self.dut.droid.goToSleepNow()
 
-    def on_pass(self, test_name, begin_time):
-        wutils.stop_pcap(self.packet_capture, self.pcap_procs, True)
-
-    def on_fail(self, test_name, begin_time):
-        wutils.stop_pcap(self.packet_capture, self.pcap_procs, False)
-        super().on_fail(test_name, begin_time)
-
     def teardown_class(self):
         if "AccessPoint" in self.user_params:
             del self.user_params["reference_networks"]
diff --git a/acts/tests/google/wifi/WifiNetworkSuggestionTest.py b/acts/tests/google/wifi/WifiNetworkSuggestionTest.py
index dc3eee8..d8c97b1 100644
--- a/acts/tests/google/wifi/WifiNetworkSuggestionTest.py
+++ b/acts/tests/google/wifi/WifiNetworkSuggestionTest.py
@@ -59,6 +59,9 @@
     * Several Wi-Fi networks visible to the device, including an open Wi-Fi
       network.
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()
diff --git a/acts/tests/google/wifi/WifiNewSetupAutoJoinTest.py b/acts/tests/google/wifi/WifiNewSetupAutoJoinTest.py
index e419a96..59d65e7 100644
--- a/acts/tests/google/wifi/WifiNewSetupAutoJoinTest.py
+++ b/acts/tests/google/wifi/WifiNewSetupAutoJoinTest.py
@@ -29,6 +29,11 @@
 
 
 class WifiNewSetupAutoJoinTest(WifiBaseTest):
+
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
+
     def add_network_and_enable(self, network):
         """Add a network and enable it.
 
diff --git a/acts/tests/google/wifi/WifiPnoTest.py b/acts/tests/google/wifi/WifiPnoTest.py
index 55655f8..16b131a 100644
--- a/acts/tests/google/wifi/WifiPnoTest.py
+++ b/acts/tests/google/wifi/WifiPnoTest.py
@@ -26,6 +26,10 @@
 
 class WifiPnoTest(WifiBaseTest):
 
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
+
     def setup_class(self):
         super().setup_class()
 
diff --git a/acts/tests/google/wifi/WifiScannerMultiScanTest.py b/acts/tests/google/wifi/WifiScannerMultiScanTest.py
index 19e19be..b75cbb2 100755
--- a/acts/tests/google/wifi/WifiScannerMultiScanTest.py
+++ b/acts/tests/google/wifi/WifiScannerMultiScanTest.py
@@ -228,8 +228,9 @@
         wifi_chs: WiFi channels according to the device model.
     """
 
-    def __init__(self, controllers):
-        WifiBaseTest.__init__(self, controllers)
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
         self.tests = (
             'test_wifi_two_scans_at_same_interval',
             'test_wifi_two_scans_at_different_interval',
diff --git a/acts/tests/google/wifi/WifiScannerScanTest.py b/acts/tests/google/wifi/WifiScannerScanTest.py
index 23717ea..c59190a 100755
--- a/acts/tests/google/wifi/WifiScannerScanTest.py
+++ b/acts/tests/google/wifi/WifiScannerScanTest.py
@@ -44,8 +44,9 @@
 
 
 class WifiScannerScanTest(WifiBaseTest):
-    def __init__(self, controllers):
-        WifiBaseTest.__init__(self, controllers)
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
         # TODO(angli): Remove this list.
         # There are order dependencies among these tests so we'll have to leave
         # it here for now. :(
diff --git a/acts/tests/google/wifi/WifiStressTest.py b/acts/tests/google/wifi/WifiStressTest.py
index 317c6c1..10ba578 100644
--- a/acts/tests/google/wifi/WifiStressTest.py
+++ b/acts/tests/google/wifi/WifiStressTest.py
@@ -46,6 +46,9 @@
     * Several Wi-Fi networks visible to the device, including an open Wi-Fi
       network.
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()
diff --git a/acts/tests/google/wifi/WifiWakeTest.py b/acts/tests/google/wifi/WifiWakeTest.py
index 6c3dd36..52ab2fd 100644
--- a/acts/tests/google/wifi/WifiWakeTest.py
+++ b/acts/tests/google/wifi/WifiWakeTest.py
@@ -43,6 +43,9 @@
     * One Android Device
     * Two APs that can be turned on and off
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()
diff --git a/acts/tests/google/wifi/WifiWpa3OweTest.py b/acts/tests/google/wifi/WifiWpa3OweTest.py
index d4b76dd..a3c70f3 100644
--- a/acts/tests/google/wifi/WifiWpa3OweTest.py
+++ b/acts/tests/google/wifi/WifiWpa3OweTest.py
@@ -27,6 +27,9 @@
     * At least one Android device and atleast two Access Points.
     * Several Wi-Fi networks visible to the device.
     """
+    def __init__(self, configs):
+        super().__init__(configs)
+        self.enable_packet_log = True
 
     def setup_class(self):
         super().setup_class()