[easy-setup] Added limit for Arduino Enrollee network reconnection attempts
authorlankamadan <lanka.madan@samsung.com>
Tue, 6 Oct 2015 11:34:04 +0000 (20:34 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Tue, 6 Oct 2015 13:44:44 +0000 (13:44 +0000)
- Added max 5 retry count for connecting to SoftAP nework
- Removed unused code in WiFiSoftAPManager

Change-Id: I9de157e9c69abb8301adf43a2404ac307681f286
Signed-off-by: lankamadan <lanka.madan@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3621
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/easy-setup/sdk/enrollee/arduino/wifi/networkHandler.cpp
service/easy-setup/sdk/mediator/android/EasySetupCore/src/main/java/org/iotivity/service/easysetup/mediator/ip/WiFiSoftAPManager.java [changed mode: 0644->0755]

index b9504fb..92d7360 100755 (executable)
 #define ES_NH_TAG "ES_NH"
 
 //-----------------------------------------------------------------------------
+// Defines
+//-----------------------------------------------------------------------------
+/**
+ *  ES_MAX_NETWORK_RETRY sets the default number of retry count for network connection.
+ */
+#define ES_MAX_NETWORK_RETRY (5)
+
+//-----------------------------------------------------------------------------
 // Private variables
 //-----------------------------------------------------------------------------
-static IPAddress myIP;
+static IPAddress enrolleeIP;
+
+/**
+ * @var g_retryCounter
+ * @brief Retry counter for cancelling network retry. Currently network retry is limited to 5 attempts
+ */
+static uint16_t g_retryCounter = 0;
+
+
 
 //-----------------------------------------------------------------------------
 // Private internal function prototypes
@@ -72,14 +88,24 @@ ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCa
             != 0)
     {
         OC_LOG(DEBUG, ES_NH_TAG, "!!!!! Upgrade WiFi Shield Firmware version !!!!!!");
-        //return ES_ERROR;
+        return ES_ERROR;
     }
 
+    //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
+    g_retryCounter = 0;
+
     OC_LOG_V(INFO, ES_NH_TAG, "Finding SSID: %s", ssid);
 
-    while (findNetwork(ssid) == 0) // found
+    while ((findNetwork(ssid) == 0) && g_retryCounter < ES_MAX_NETWORK_RETRY) // found
     {
         delay(1000);
+        g_retryCounter++;
+    }
+
+    if(g_retryCounter == ES_MAX_NETWORK_RETRY){
+        OC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
+                                                                    g_retryCounter);
+        return ES_ERROR;
     }
 
     if (cb != NULL)
@@ -90,6 +116,9 @@ ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCa
     if (WiFi.status() == WL_CONNECTED)
         WiFi.disconnect();
 
+    //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
+    g_retryCounter = 0;
+
     res = ConnectToNetwork(ssid, pass);
 
     if (res == 0)
@@ -142,7 +171,7 @@ int ConnectToNetwork(const char *ssid, const char *pass)
     int status = WL_IDLE_STATUS;
 
     // attempt to connect to Wifi network:
-    while (status != WL_CONNECTED)
+    while (status != WL_CONNECTED && g_retryCounter < ES_MAX_NETWORK_RETRY)
     {
         OC_LOG_V(INFO, ES_NH_TAG, "Attempting to connect to SSID: %s", ssid);
 
@@ -150,14 +179,25 @@ int ConnectToNetwork(const char *ssid, const char *pass)
 
         // wait 10 seconds for connection:
         delay(10000);
+
+        g_retryCounter++;
     }
+
+    if(g_retryCounter == ES_MAX_NETWORK_RETRY){
+        OC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
+                                                                    g_retryCounter);
+        return ES_ERROR;
+    }
+
     OC_LOG(DEBUG, ES_NH_TAG, "Connected to wifi");
 
-    myIP = WiFi.localIP();
-    OC_LOG_V(INFO, ES_NH_TAG, "IP Address:  %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
+    enrolleeIP = WiFi.localIP();
+    OC_LOG_V(INFO, ES_NH_TAG, "IP Address:  %d.%d.%d.%d", enrolleeIP[0], enrolleeIP[1],
+                                                          enrolleeIP[2], enrolleeIP[3]);
 
     char buf[50];
-    sprintf(buf, "IP Address:  %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
+    sprintf(buf, "IP Address:  %d.%d.%d.%d", enrolleeIP[0], enrolleeIP[1],
+                                             enrolleeIP[2], enrolleeIP[3]);
     Serial.println(buf);
 
     return 0;
old mode 100644 (file)
new mode 100755 (executable)
index 1212932..7e08f2b
@@ -201,58 +201,6 @@ public class WiFiSoftAPManager {
     }
 
     /**
-     * Fetch the current state of the Wi-Fi Soft AP
-     *
-     * @return {@link WIFI_AP_STATE}
-     */
-    public WIFI_AP_STATE getWifiApState() {
-        try {
-            Method method = mWifiManager.getClass().getMethod("getWifiApState");
-
-            int currentWiFiState = ((Integer) method.invoke(mWifiManager));
-            WIFI_AP_STATE wifi_ap_state_enum[] = WIFI_AP_STATE.class.getEnumConstants();
-            if (wifi_ap_state_enum != null)
-                return wifi_ap_state_enum[currentWiFiState];
-            else return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
-        } catch (Exception e) {
-            Log.e(this.getClass().toString(), "", e);
-            return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
-        }
-    }
-
-    /**
-     * Fetch the current Wi-Fi AP Configuration.
-     *
-     * @return AP details in {@link WifiConfiguration}
-     */
-    public WifiConfiguration getWifiApConfiguration() {
-        try {
-            Method method = mWifiManager.getClass().getMethod(
-                    "getWifiApConfiguration");
-            return (WifiConfiguration) method.invoke(mWifiManager);
-        } catch (Exception e) {
-            Log.e(this.getClass().toString(), "", e);
-            return null;
-        }
-    }
-
-    /**
-     * Set/Update the Wi-Fi AP Configuration.
-     *
-     * @return {@code true} if the operation succeeds, {@code false} otherwise
-     */
-    public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
-        try {
-            Method method = mWifiManager.getClass().getMethod(
-                    "setWifiApConfiguration", WifiConfiguration.class);
-            return (Boolean) method.invoke(mWifiManager, wifiConfig);
-        } catch (Exception e) {
-            Log.e(this.getClass().toString(), "", e);
-            return false;
-        }
-    }
-
-    /**
      * Gets a list of the Soft AP clients connected to the Wi-Fi Soft Access point
      *
      * @param finishListener   Interface called when the scan method finishes