Collect tcpdump when failed for wifi related tests

This CL has below changes:
- Modify tcpdump related utility functions in net_test_utils.
  Including clean up unused variables, split dump file to
  prevent a unreadable large file, add necessary parameters.
- Add codes of tcpdump collection for wifi base test classes
  (WifiBaseTest, WifiP2pBaseTest, AwareBaseTest, RttBaseTest)

Bug: 165446308
Test: Run AU drop tests
Change-Id: I3c80161108747a4f97b4ced060206046ba043c8c
Merged-In: I3c80161108747a4f97b4ced060206046ba043c8c
diff --git a/acts/framework/acts/test_utils/net/net_test_utils.py b/acts/framework/acts/test_utils/net/net_test_utils.py
index b21c974..93e62a7 100644
--- a/acts/framework/acts/test_utils/net/net_test_utils.py
+++ b/acts/framework/acts/test_utils/net/net_test_utils.py
@@ -21,9 +21,6 @@
 from acts import signals
 from acts import utils
 from acts.controllers.adb import AdbError
-from acts.logger import epoch_to_log_line_timestamp
-from acts.utils import get_current_epoch_time
-from acts.logger import normalize_log_line_timestamp
 from acts.utils import start_standing_subprocess
 from acts.utils import stop_standing_subprocess
 from acts.test_utils.net import connectivity_const as cconst
@@ -287,23 +284,14 @@
         test_name: tcpdump file name will have this
     """
     ad.log.info("Starting tcpdump on all interfaces")
-    try:
-        ad.adb.shell("killall -9 tcpdump")
-    except AdbError:
-        ad.log.warn("Killing existing tcpdump processes failed")
-    out = ad.adb.shell("ls -l %s" % TCPDUMP_PATH)
-    if "No such file" in out or not out:
-        ad.adb.shell("mkdir %s" % TCPDUMP_PATH)
-    else:
-        ad.adb.shell("rm -rf %s/*" % TCPDUMP_PATH, ignore_status=True)
-
-    begin_time = epoch_to_log_line_timestamp(get_current_epoch_time())
-    begin_time = normalize_log_line_timestamp(begin_time)
+    ad.adb.shell("killall -9 tcpdump", ignore_status=True)
+    ad.adb.shell("mkdir %s" % TCPDUMP_PATH, ignore_status=True)
+    ad.adb.shell("rm -rf %s/*" % TCPDUMP_PATH, ignore_status=True)
 
     file_name = "%s/tcpdump_%s_%s.pcap" % (TCPDUMP_PATH, ad.serial, test_name)
     ad.log.info("tcpdump file is %s", file_name)
-    cmd = "adb -s {} shell tcpdump -i any -s0 -w {}".format(ad.serial,
-                                                            file_name)
+    cmd = "adb -s {} shell tcpdump -i any -W 100 -C 50 -s0 -w {}".format(
+            ad.serial, file_name)
     try:
         return start_standing_subprocess(cmd, 5)
     except Exception:
@@ -314,14 +302,16 @@
 def stop_tcpdump(ad,
                  proc,
                  test_name,
+                 pull_dump=True,
                  adb_pull_timeout=adb.DEFAULT_ADB_PULL_TIMEOUT):
     """Stops tcpdump on any iface
-       Pulls the tcpdump file in the tcpdump dir
+       Pulls the tcpdump file in the tcpdump dir if necessary
 
     Args:
         ad: android device object.
         proc: need to know which pid to stop
         test_name: test name to save the tcpdump file
+        pull_dump: pull tcpdump file or not
         adb_pull_timeout: timeout for adb_pull
 
     Returns:
@@ -334,12 +324,14 @@
         stop_standing_subprocess(proc)
     except Exception as e:
         ad.log.warning(e)
-    log_path = os.path.join(ad.log_path, test_name)
-    os.makedirs(log_path, exist_ok=True)
-    ad.adb.pull("%s/. %s" % (TCPDUMP_PATH, log_path), timeout=adb_pull_timeout)
-    ad.adb.shell("rm -rf %s/*" % TCPDUMP_PATH, ignore_status=True)
-    file_name = "tcpdump_%s_%s.pcap" % (ad.serial, test_name)
-    return "%s/%s" % (log_path, file_name)
+    if pull_dump:
+        log_path = os.path.join(ad.device_log_path, "TCPDUMP_%s" % ad.serial)
+        os.makedirs(log_path, exist_ok=True)
+        ad.adb.pull("%s/. %s" % (TCPDUMP_PATH, log_path),
+                timeout=adb_pull_timeout)
+        ad.adb.shell("rm -rf %s/*" % TCPDUMP_PATH, ignore_status=True)
+        return log_path
+    return None
 
 def start_tcpdump_gce_server(ad, test_name, dest_port, gce):
     """ Start tcpdump on gce server
diff --git a/acts/framework/acts/test_utils/wifi/WifiBaseTest.py b/acts/framework/acts/test_utils/wifi/WifiBaseTest.py
index 50ec821..a6f642a 100644
--- a/acts/framework/acts/test_utils/wifi/WifiBaseTest.py
+++ b/acts/framework/acts/test_utils/wifi/WifiBaseTest.py
@@ -36,6 +36,7 @@
 from acts.controllers.ap_lib import hostapd_constants
 from acts.controllers.ap_lib import hostapd_security
 from acts.keys import Config
+from acts.test_utils.net import net_test_utils as nutils
 from acts.test_utils.wifi import wifi_test_utils as wutils
 
 AP_1 = 0
@@ -64,12 +65,21 @@
                 hasattr(self, "pixel_models")):
             wutils.start_cnss_diags(
                 self.android_devices, self.cnss_diag_file, self.pixel_models)
+        self.tcpdump_proc = []
+        if hasattr(self, "android_devices"):
+            for ad in self.android_devices:
+                proc = nutils.start_tcpdump(ad, self.test_name)
+                self.tcpdump_proc.append((ad, proc))
 
     def teardown_test(self):
         if (hasattr(self, "android_devices") and
                 hasattr(self, "cnss_diag_file") and
                 hasattr(self, "pixel_models")):
             wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(
+                    proc[0], proc[1], self.test_name, pull_dump=False)
+        self.tcpdump_proc = []
 
     def on_fail(self, test_name, begin_time):
         if hasattr(self, "android_devices"):
@@ -82,6 +92,9 @@
                 wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
                 for ad in self.android_devices:
                     wutils.get_cnss_diag_log(ad)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(proc[0], proc[1], self.test_name)
+        self.tcpdump_proc = []
 
     def get_psk_network(
             self,
diff --git a/acts/framework/acts/test_utils/wifi/aware/AwareBaseTest.py b/acts/framework/acts/test_utils/wifi/aware/AwareBaseTest.py
index 901b31f..42287aa 100644
--- a/acts/framework/acts/test_utils/wifi/aware/AwareBaseTest.py
+++ b/acts/framework/acts/test_utils/wifi/aware/AwareBaseTest.py
@@ -20,6 +20,7 @@
 from acts import utils
 from acts.base_test import BaseTestClass
 from acts.keys import Config
+from acts.test_utils.net import net_test_utils as nutils
 from acts.test_utils.wifi import wifi_test_utils as wutils
 from acts.test_utils.wifi.aware import aware_const as aconsts
 from acts.test_utils.wifi.aware import aware_test_utils as autils
@@ -53,6 +54,12 @@
         if hasattr(self, "cnss_diag_file") and hasattr(self, "pixel_models"):
             wutils.start_cnss_diags(
                 self.android_devices, self.cnss_diag_file, self.pixel_models)
+        self.tcpdump_proc = []
+        if hasattr(self, "android_devices"):
+            for ad in self.android_devices:
+                proc = nutils.start_tcpdump(ad, self.test_name)
+                self.tcpdump_proc.append((ad, proc))
+
         for ad in self.android_devices:
             ad.droid.wifiEnableVerboseLogging(1)
             asserts.skip_if(
@@ -81,6 +88,10 @@
     def teardown_test(self):
         if hasattr(self, "cnss_diag_file") and hasattr(self, "pixel_models"):
             wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(
+                    proc[0], proc[1], self.test_name, pull_dump=False)
+        self.tcpdump_proc = []
         for ad in self.android_devices:
             if not ad.droid.doesDeviceSupportWifiAwareFeature():
                 return
@@ -137,3 +148,6 @@
             wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
             for ad in self.android_devices:
                 wutils.get_cnss_diag_log(ad)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(proc[0], proc[1], self.test_name)
+        self.tcpdump_proc = []
diff --git a/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py b/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py
index 3839e40..5c41aae 100644
--- a/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py
+++ b/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py
@@ -23,6 +23,7 @@
 from acts import utils
 from acts.base_test import BaseTestClass
 from acts.keys import Config
+from acts.test_utils.net import net_test_utils as nutils
 from acts.test_utils.wifi import wifi_test_utils as wutils
 from acts.test_utils.wifi.p2p import wifi_p2p_const as p2pconsts
 
@@ -112,12 +113,22 @@
         if hasattr(self, "cnss_diag_file") and hasattr(self, "pixel_models"):
             wutils.start_cnss_diags(
                 self.android_devices, self.cnss_diag_file, self.pixel_models)
+        self.tcpdump_proc = []
+        if hasattr(self, "android_devices"):
+            for ad in self.android_devices:
+                proc = nutils.start_tcpdump(ad, self.test_name)
+                self.tcpdump_proc.append((ad, proc))
+
         for ad in self.android_devices:
             ad.ed.clear_all_events()
 
     def teardown_test(self):
         if hasattr(self, "cnss_diag_file") and hasattr(self, "pixel_models"):
             wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(
+                    proc[0], proc[1], self.test_name, pull_dump=False)
+        self.tcpdump_proc = []
         for ad in self.android_devices:
             # Clear p2p group info
             ad.droid.wifiP2pRequestPersistentGroupInfo()
@@ -137,6 +148,9 @@
             wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
             for ad in self.android_devices:
                 wutils.get_cnss_diag_log(ad)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(proc[0], proc[1], self.test_name)
+        self.tcpdump_proc = []
 
     def get_p2p_mac_address(self, dut):
         """Gets the current MAC address being used for Wi-Fi Direct."""
diff --git a/acts/framework/acts/test_utils/wifi/rtt/RttBaseTest.py b/acts/framework/acts/test_utils/wifi/rtt/RttBaseTest.py
index 00118e2..90c87bd 100644
--- a/acts/framework/acts/test_utils/wifi/rtt/RttBaseTest.py
+++ b/acts/framework/acts/test_utils/wifi/rtt/RttBaseTest.py
@@ -20,6 +20,7 @@
 from acts import utils
 from acts.base_test import BaseTestClass
 from acts.keys import Config
+from acts.test_utils.net import net_test_utils as nutils
 from acts.test_utils.wifi import wifi_test_utils as wutils
 from acts.test_utils.wifi.rtt import rtt_const as rconsts
 from acts.test_utils.wifi.rtt import rtt_test_utils as rutils
@@ -56,6 +57,12 @@
         if hasattr(self, "cnss_diag_file") and hasattr(self, "pixel_models"):
             wutils.start_cnss_diags(
                 self.android_devices, self.cnss_diag_file, self.pixel_models)
+        self.tcpdump_proc = []
+        if hasattr(self, "android_devices"):
+            for ad in self.android_devices:
+                proc = nutils.start_tcpdump(ad, self.test_name)
+                self.tcpdump_proc.append((ad, proc))
+
         for ad in self.android_devices:
             utils.set_location_service(ad, True)
             ad.droid.wifiEnableVerboseLogging(1)
@@ -75,6 +82,10 @@
     def teardown_test(self):
         if hasattr(self, "cnss_diag_file") and hasattr(self, "pixel_models"):
             wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(
+                    proc[0], proc[1], self.test_name, pull_dump=False)
+        self.tcpdump_proc = []
         for ad in self.android_devices:
             if not ad.droid.doesDeviceSupportWifiRttFeature():
                 return
@@ -91,3 +102,6 @@
             wutils.stop_cnss_diags(self.android_devices, self.pixel_models)
             for ad in self.android_devices:
                 wutils.get_cnss_diag_log(ad)
+        for proc in self.tcpdump_proc:
+            nutils.stop_tcpdump(proc[0], proc[1], self.test_name)
+        self.tcpdump_proc = []
diff --git a/acts/tests/google/wifi/WifiEnterpriseTest.py b/acts/tests/google/wifi/WifiEnterpriseTest.py
index 700716d..20c1b7c 100644
--- a/acts/tests/google/wifi/WifiEnterpriseTest.py
+++ b/acts/tests/google/wifi/WifiEnterpriseTest.py
@@ -21,8 +21,6 @@
 from acts import asserts
 from acts import signals
 from acts.test_decorators import test_tracker_info
-from acts.test_utils.net.net_test_utils import start_tcpdump
-from acts.test_utils.net.net_test_utils import stop_tcpdump
 from acts.test_utils.wifi import wifi_test_utils as wutils
 from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest
 
@@ -151,7 +149,6 @@
         del self.config_passpoint_ttls[WifiEnums.SSID_KEY]
         # Set screen lock password so ConfigStore is unlocked.
         self.dut.droid.setDevicePassword(self.device_password)
-        self.tcpdump_pid = None
 
     def teardown_class(self):
         wutils.reset_wifi(self.dut)
@@ -165,11 +162,9 @@
         self.dut.droid.wakeUpNow()
         wutils.reset_wifi(self.dut)
         self.dut.ed.clear_all_events()
-        self.tcpdump_pid = start_tcpdump(self.dut, self.test_name)
 
     def teardown_test(self):
         super().teardown_test()
-        stop_tcpdump(self.dut, self.tcpdump_pid, self.test_name)
         self.dut.droid.wakeLockRelease()
         self.dut.droid.goToSleepNow()
         self.dut.droid.wifiStopTrackingStateChange()
diff --git a/acts/tests/google/wifi/WifiTetheringTest.py b/acts/tests/google/wifi/WifiTetheringTest.py
index af61d8c..56c6427 100644
--- a/acts/tests/google/wifi/WifiTetheringTest.py
+++ b/acts/tests/google/wifi/WifiTetheringTest.py
@@ -50,7 +50,6 @@
         self.network = {"SSID": "hotspot_%s" % utils.rand_ascii_str(6),
                         "password": "pass_%s" % utils.rand_ascii_str(6)}
         self.new_ssid = "hs_%s" % utils.rand_ascii_str(6)
-        self.tcpdump_pid=[]
 
         nutils.verify_lte_data_and_tethering_supported(self.hotspot_device)
         for ad in self.tethered_devices:
@@ -58,8 +57,6 @@
 
     def setup_test(self):
         super().setup_test()
-        for ad in self.android_devices:
-            self.tcpdump_pid.append(nutils.start_tcpdump(ad, self.test_name))
         self.tethered_devices[0].droid.telephonyToggleDataConnection(False)
 
     def teardown_test(self):
@@ -67,10 +64,6 @@
         if self.hotspot_device.droid.wifiIsApEnabled():
             wutils.stop_wifi_tethering(self.hotspot_device)
         self.tethered_devices[0].droid.telephonyToggleDataConnection(True)
-        for ad, pid in zip(self.android_devices, self.tcpdump_pid):
-            nutils.stop_tcpdump(ad, pid, self.test_name)
-        self.tcpdump_pid = []
-
 
     def teardown_class(self):
         """ Reset devices """