#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
!= 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)
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)
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);
// 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;
}
/**
- * 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