[WiFi][Non-ACR][Update precondition codes] 68/218568/2
authorSeonah Moon <seonah1.moon@samsung.com>
Mon, 25 Nov 2019 13:05:29 +0000 (22:05 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Tue, 26 Nov 2019 07:42:47 +0000 (16:42 +0900)
- Add exception handling: Already exist, In progress
- Check bg scan state before scanning or connecting AP

Change-Id: I98d0906db5a554a095d6af0e73b1dde773f49f2c

tct-suite-vs/Tizen.WiFi.Tests/testcase/TSWiFiAP.cs
tct-suite-vs/Tizen.WiFi.Tests/testcase/TSWiFiManager.cs
tct-suite-vs/Tizen.WiFi.Tests/testcase/support/networkSetup.cs

index ae8f011..450ee4f 100755 (executable)
@@ -357,7 +357,7 @@ namespace Tizen.Network.WiFi.Tests
                 /**
                  * POST CONDITION
                  * */
-                await _wifiAP.ConnectAsync();
+                await setUp();
             }
             catch (NotSupportedException)
             {
index 140841e..c997da4 100755 (executable)
@@ -968,6 +968,7 @@ namespace Tizen.Network.WiFi.Tests
             try
             {
                 await setUp();
+                await networkSetup.waitBgScan();
 
                 var count = 0;
                 await WiFiManager.BssidScanAsync();
index 374099a..143b0d7 100755 (executable)
@@ -16,10 +16,73 @@ namespace networkUtils
     public class networkSetup
     {
         private static int MAX_SCAN_TRY = 3;
+        private static bool scanning = false;
+        private static WiFiConnectionState connectionState = WiFiConnectionState.Disconnected;
 
         private networkSetup()
         {
         }
+        private static void EventHandlerScanState(object sender, ScanStateChangedEventArgs State)
+        {
+            if (State.State == WiFiScanState.Scanning)
+            {
+                Tizen.Log.Info(Globals.LogTag, "ScanState is changed: Scanning");
+                scanning = true;
+            }
+            else
+            {
+                Tizen.Log.Info(Globals.LogTag, "ScanState is changed: Done");
+                scanning = false;
+            }
+        }
+
+        private static void EventHandlerConnectionStateChangedCBState(object sender, ConnectionStateChangedEventArgs e)
+        {
+            connectionState = e.State;
+            Tizen.Log.Info(Globals.LogTag, "ConnectionState is changed: " + connectionState.ToString());
+        }
+
+        private static async Task waitScanning()
+        {
+            int count = 0;
+            while (true)
+            {
+                await Task.Delay(1000);
+                count++;
+                if (!scanning)
+                {
+                    Tizen.Log.Info(Globals.LogTag, "Scan Done");
+                    break;
+                }
+
+                if (count == 30)
+                {
+                    Tizen.Log.Info(Globals.LogTag, "Scanning timeout is reached");
+                    break;
+                }
+            }
+        }
+
+        private static async Task waitConnecting()
+        {
+            int count = 0;
+            while (true)
+            {
+                await Task.Delay(1000);
+                count++;
+                if (connectionState == WiFiConnectionState.Connected)
+                {
+                    Tizen.Log.Info(Globals.LogTag, "Connected");
+                    break;
+                }
+
+                if (count == 30)
+                {
+                    Tizen.Log.Info(Globals.LogTag, "connecting timeout is reached");
+                    break;
+                }
+            }
+        }
 
         public static async Task ForgetCurrentAP()
         {
@@ -55,7 +118,6 @@ namespace networkUtils
             }
         }
 
-
         private static WiFiAP FindAP(string WiFiName)
         {
             Tizen.Log.Info(Globals.LogTag, "Find AP " + WiFiName + " from scan results");
@@ -73,6 +135,14 @@ namespace networkUtils
             return null;
         }
 
+        public static async Task waitBgScan()
+        {
+            WiFiManager.ScanStateChanged += EventHandlerScanState;
+            await waitScanning();
+            WiFiManager.ScanStateChanged -= EventHandlerScanState;
+            Tizen.Log.Info(Globals.LogTag, "bg scan is done");
+        }
+
         /* Connect to a WANTED AP */
         public static async Task ConnectWiFi(string WiFiName, networkParameter.wType type, string WiFiPass)
         {
@@ -105,6 +175,8 @@ namespace networkUtils
             }
             Tizen.Log.Info(Globals.LogTag, "No connected AP");
 
+            WiFiManager.ScanStateChanged += EventHandlerScanState;
+
             /**
              * Find AP from scan results
              * If it fails, scan Wi-Fi APs maximum three times
@@ -120,33 +192,45 @@ namespace networkUtils
             if (ap == null)
             {
                 Tizen.Log.Error(Globals.LogTag, "AP " + WiFiName + " is not found");
+                WiFiManager.ScanStateChanged -= EventHandlerScanState;
                 return;
             }
 
             /**
-             * Auto-connection might occurs after scan.
-             * Check connection state before requesting connect to AP.
+             * Sometimes, bg scan can occurs.
+             * To avoid it, wait until finishing scan.
              */
-            if (WiFiManager.ConnectionState == WiFiConnectionState.Connected)
-            {
-                Tizen.Log.Info(Globals.LogTag, "Already connected");
-                return;
-            }
+            await waitScanning();
 
             // Connect to AP
             Tizen.Log.Info(Globals.LogTag, "Set passphrase " + WiFiPass);
             ap.SecurityInformation.SetPassphrase(WiFiPass);
 
+            WiFiManager.ConnectionStateChanged += EventHandlerConnectionStateChangedCBState;
             try
             {
                 await ap.ConnectAsync();
                 Tizen.Log.Info(Globals.LogTag, "Connected");
             }
+            catch(NowInProgressException)
+            {
+                Tizen.Log.Info(Globals.LogTag, "Connecting..");
+                await waitConnecting();
+            }
             catch (System.Exception e)
             {
-                Tizen.Log.Info(Globals.LogTag, "Exception during Wi-Fi Association: " + e);
-                await ap.ConnectAsync();
+                if (WiFiManager.ConnectionState == WiFiConnectionState.Connected)
+                {
+                    Tizen.Log.Info(Globals.LogTag, "Already connected");
+                }
+                else
+                {
+                    Tizen.Log.Info(Globals.LogTag, "Exception during Wi-Fi Association: " + e);
+                    await ap.ConnectAsync();
+                }
             }
+            WiFiManager.ScanStateChanged -= EventHandlerScanState;
+            WiFiManager.ConnectionStateChanged -= EventHandlerConnectionStateChangedCBState;
         } // ConnectWiFi()
     }
 }