EasySetup:Enrollee implementation optimization
authorVinil Jain <vinil.gj@samsung.com>
Tue, 6 Oct 2015 06:03:16 +0000 (11:33 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Tue, 6 Oct 2015 12:50:17 +0000 (12:50 +0000)
- for handling error cases like SoftAP disconnect

Change-Id: I64abbb147c9d223d92e007f11a80c98ef36dea35
Signed-off-by: Vinil Jain <vinil.gj@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3605
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/easy-setup/sampleapp/enrollee/arduino/enrollee_wifi.cpp
service/easy-setup/sdk/mediator/android/EasySetupCore/src/main/java/org/iotivity/service/easysetup/impl/EnrolleeDeviceWiFiOnboarding.java

index 7e7d6f7..dd3c58c 100755 (executable)
@@ -18,7 +18,6 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-// Do not remove the include below
 #include "Arduino.h"
 
 #include "logger.h"
 
 const char *getResult(OCStackResult result);
 
-char ssid[] = "EasySetup123";
-char passwd[] = "EasySetup123";
+/**
+ * @var ssid
+ * @brief Target SSID of the Soft Access point to which the device has to connect
+ */
+static char ssid[] = "EasySetup123";
 
-void EventCallbackInApp(ESResult eventFlag)
+/**
+ * @var passwd
+ * @brief Password of the Soft Access point to which the device has to connect
+ */
+static char passwd[] = "EasySetup123";
+
+/**
+ * @var g_OnBoardingSucceeded
+ * @brief This variable will be set if OnBoarding is successful
+ */
+static bool g_OnBoardingSucceeded = false;
+
+/**
+ * @var g_ProvisioningSucceeded
+ * @brief This variable will be set if Provisioning is successful
+ */
+static bool g_ProvisioningSucceeded = false;
+
+static bool g_isInitialized = false;
+
+bool is_connected=false;
+
+void GetData(char *readInput, size_t bufferLength, size_t *dataLength)
+{
+    if (!readInput || bufferLength == 0 || !dataLength)
+    {
+        Serial.println("Invalid buffer");
+        return;
+    }
+
+    while (!Serial.available())
+    {
+        delay(500);
+    }
+    int len = 0;
+    while (Serial.available())
+    {
+        delay(100);
+        char c = Serial.read();
+        if ('\n' != c && '\r' != c && len < bufferLength - 1)
+        {
+            readInput[len++] = c;
+        }
+        else
+        {
+            break;
+        }
+    }
+
+    readInput[len] = '\0';
+    Serial.flush();
+    Serial.print("PD: ");
+    Serial.println(readInput);
+    (*dataLength) = len;
+}
+
+void PrintMenu()
+{
+    Serial.println("============");
+    Serial.println("s: start easy setup");
+    Serial.println("p: start provisioning resources");
+    Serial.println("t: terminate");
+    Serial.println("q: quit");
+    Serial.println("============");
+}
+
+void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
 {
     Serial.println("callback!!! in app");
+
+    if(esResult == ES_OK)
+    {
+        if(!g_OnBoardingSucceeded){
+            Serial.println("Device is successfully OnBoarded");
+            g_OnBoardingSucceeded = true;
+        }
+        else if(g_OnBoardingSucceeded & enrolleeState == ES_ON_BOARDED_STATE){
+            Serial.println("Device is successfully OnBoared with SoftAP");
+            g_ProvisioningSucceeded = true;
+        }
+    }
+    else if (esResult == ES_ERROR)
+    {
+        if(g_OnBoardingSucceeded)
+        {
+            OC_LOG_V(ERROR, TAG, "Failure in Provisioning. \
+                                        Current Enrollee State: %d",enrolleeState);
+            g_OnBoardingSucceeded = false;
+        }
+        else if(g_ProvisioningSucceeded)
+        {
+            OC_LOG_V(ERROR, TAG, "Failure in connect to target network. \
+                                        Current Enrollee State: %d",enrolleeState);
+            g_ProvisioningSucceeded = false;
+        }
+    }
+    PrintMenu();
 }
 
 // On Arduino Atmel boards with Harvard memory architecture, the stack grows
@@ -72,20 +168,26 @@ void PrintArduinoMemoryStats()
             ((unsigned int)&tmp - (unsigned int)__brkval));
 #endif
 }
-//The setup function is called once at startup of the sketch
-void setup()
+
+void StartEasySetup()
 {
-    // Add your initialization code here
-    // Note : This will initialize Serial port on Arduino at 115200 bauds
-    OC_LOG_INIT();
     OC_LOG(DEBUG, TAG, "OCServer is starting...");
 
     if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
     {
-        OC_LOG(ERROR, TAG, "EasySetup Init Failed");
+        OC_LOG(ERROR, TAG, "OnBoarding Failed");
         return;
     }
 
+    g_isInitialized = true;
+
+    OC_LOG_V(ERROR, TAG, "OnBoarding succeeded. Successfully connected to ssid : %s",ssid);
+}
+
+void StartProvisioning()
+{
+    OC_LOG(DEBUG, TAG, "StartProvisioning is invoked...");
+
     if(InitProvisioning()== ES_ERROR)
     {
         OC_LOG(ERROR, TAG, "Init Provisioning Failed");
@@ -93,20 +195,93 @@ void setup()
     }
 }
 
+void StopEasySetup()
+{
+    OC_LOG(DEBUG, TAG, "Stopping EasySetup is invoked...");
+
+    g_isInitialized = false;
+
+    if(TerminateEasySetup()== ES_ERROR)
+    {
+        OC_LOG(ERROR, TAG, "TerminateEasySetup Failed");
+        return;
+    }
+}
+
+
+//The setup function is called once at startup of the sketch
+void setup()
+{
+    // Add your initialization code here
+    // Note : This will initialize Serial port on Arduino at 115200 bauds
+    OC_LOG_INIT();
+
+    Serial.println("#########################");
+    Serial.println("EasySetup Enrollee SAMPLE");
+    Serial.println("#########################");
+    PrintMenu();
+}
+
 // The loop function is called in an endless loop
 void loop()
 {
-    // This artificial delay is kept here to avoid endless spinning
-    // of Arduino microcontroller. Modify it as per specific application needs.
-    delay(2000);
+    char buffer[5] = {0};
+    size_t len;
+    if (Serial.available() > 0)
+    {
+        GetData(buffer, sizeof(buffer), &len);
+        if (0 >= len)
+        {
+            Serial.println("Input Error err");
+            return;
+        }
+        switch (toupper(buffer[0]))
+        {
+            case 'H': // help
+                PrintMenu();
+                break;
+
+            case 'Q': // quit
+                Serial.println("quit");
+                return;
+
+            case 'S': // start easy setup
+                StartEasySetup();
+                break;
+
+            case 'P': // start provisioning
+                StartProvisioning();
+                break;
 
-    // This call displays the amount of free SRAM available on Arduino
-    PrintArduinoMemoryStats();
+            case 'T': // stop easy setup
+                StopEasySetup();
+                break;
 
-    // Give CPU cycles to OCStack to perform send/recv and other OCStack stuff
-    if (OCProcess() != OC_STACK_OK)
+            default:
+                Serial.println("wrong option");
+                break;
+        }
+    }
+
+    //check g_isInitialized to see if stack init is success
+    if (g_isInitialized)
     {
-        OC_LOG(ERROR, TAG, "OCStack process error");
-        return;
+        // This call displays the amount of free SRAM available on Arduino
+        PrintArduinoMemoryStats();
+        if (WiFi.status() == WL_CONNECTED)
+            is_connected = true;
+        else if (is_connected)
+            TerminateEasySetup();
+
+        // Give CPU cycles to OCStack to perform send/recv and other OCStack stuff
+        if (OCProcess() != OC_STACK_OK)
+        {
+            OC_LOG(ERROR, TAG, "OCStack process error");
+            return;
+        }
     }
+
+    // This artificial delay is kept here to avoid endless spinning
+    // of Arduino microcontroller. Modify it as per specific application needs.
+    delay(2000);
 }
index 2b022c7..e525e62 100644 (file)
@@ -136,8 +136,8 @@ public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
     @Override\r
     protected void startProvisioningProcess(OnBoardingConnection conn) {\r
         try {\r
-            Log.i(TAG, "waiting for 15 seconds to start provisioning");\r
-            Thread.sleep(15000);//Sleep for allowing thin device to start the services\r
+            Log.i(TAG, "waiting for 20 seconds to start provisioning");\r
+            Thread.sleep(20000);//Sleep for allowing thin device to start the services\r
         } catch (InterruptedException e) {\r
             e.printStackTrace();\r
         }\r