Add packet capturing to the simple connector.

This shows the dynamic suite can configure APs and then
grab packet captures while it runs the same as the static
tests.

TEST=Manual
BUG=None

Change-Id: I42ea475a3921422e88408212c623dbe1e1478e2b
Reviewed-on: https://gerrit.chromium.org/gerrit/45256
Reviewed-by: Deepak Gopal <deepakg@chromium.org>
Tested-by: Kris Rambish <krisr@chromium.org>
diff --git a/server/cros/wlan/connector.py b/server/cros/wlan/connector.py
index ed65605..2360a8c 100644
--- a/server/cros/wlan/connector.py
+++ b/server/cros/wlan/connector.py
@@ -2,7 +2,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import logging, re, os
+import logging
 
 import common
 
@@ -114,6 +114,9 @@
     def set_filename(self, filename):
        """ Set the filename.
 
+       The following strings are appended to the filename based on the
+       connection status: _success.trc or _fail.trc
+
        @param filename: The file with which to store the capture.
        """
        self.trace_filename = filename
@@ -130,11 +133,20 @@
 
         self.set_frequency(frequency)
         self.set_bandwidth(bandwidth)
+        success = True
         try:
             self.capturer.start_capture(self.trace_frequency,
                                         self.trace_bandwidth)
             super(TracingConnector, self).connect(ssid, security, psk)
         except ConnectException, e:
+            success = False
+            logging.info('Connection failed to connect')
+        finally:
             self.capturer.stop_capture()
-            self.capture.get_capture_file(self.filename)
-            raise e
+            if success:
+                filename = self.trace_filename + '_success.trc'
+            else:
+                filename = self.trace_filename + '_fail.trc'
+            self.capturer.get_capture_file(filename)
+            if not success:
+                raise e
diff --git a/server/site_tests/network_WiFiSimpleConnectionServer/network_WiFiSimpleConnectionServer.py b/server/site_tests/network_WiFiSimpleConnectionServer/network_WiFiSimpleConnectionServer.py
index 413d809..cf47e33 100644
--- a/server/site_tests/network_WiFiSimpleConnectionServer/network_WiFiSimpleConnectionServer.py
+++ b/server/site_tests/network_WiFiSimpleConnectionServer/network_WiFiSimpleConnectionServer.py
@@ -24,17 +24,15 @@
         self.client_at = autotest.Autotest(self.host)
         self.c = connector.TracingConnector(self.host, capturer)
         self.d = disconnector.Disconnector(self.host)
-
         self.error_list = []
 
 
-    def run_connect_disconnect_test(self, ap):
+    def run_connect_disconnect_test(self, ap, iteration):
         """ Connects to the AP and Navigates to URL.
 
         Args:
-            ssid: The ssid of the AP to connect.
-            security: The security type of the AP to connect.
-            passphrase: The passphrase of the AP to connect.
+            ap: the ap object
+            iteration: the current iteration
 
         Returns:
             None if there are no errors
@@ -46,19 +44,22 @@
         frequency = ap['frequency']
         bss = ap['bss']
 
+        self.d.disconnect(ssid)
         self.c.set_frequency(frequency)
-        self.c.set_filename(os.path.join(self.outputdir,
-                            'connect_fail_%s.trc' % bss))
-
+        log_folder = os.path.join(self.outputdir, '%s' % bss)
+        if not os.path.exists(log_folder):
+            os.mkdir(log_folder)
+        self.c.set_filename(os.path.join(log_folder, 'connect_try_%d'
+                                         % (iteration+1)))
+        error = None
         try:
-            self.c.connect(ssid, frequency=frequency, bandwidth='HT40+')
+            self.c.connect(ssid, frequency=frequency)
         except (connector.ConnectException,
                 connector.ConnectFailed,
                 connector.ConnectTimeout) as e:
             error = 'Failed to connect'
         finally:
             self.d.disconnect(ssid)
-
         return error
 
 
@@ -130,7 +131,7 @@
                 failure = False
                 for iteration in range(tries):
                     logging.info('Connection try %d' % (iteration + 1))
-                    resp = self.run_connect_disconnect_test(ap)
+                    resp = self.run_connect_disconnect_test(ap, iteration)
                     if resp:
                         failure = True
                         ap_info['failed_connections'].append({'error': resp,
@@ -145,7 +146,6 @@
                                   'run: (outside-chroot) <path to chroot tmp '
                                   'directory>/ %s./ chromedriver'
                                   % download_chromium_prebuilt.DOWNLOAD_PATH)
-
         # Install all of the autotest libriaries on the client
         self.client_at.install()
         factory = ap_configurator_factory.APConfiguratorFactory()