Updated the Arduino APIs and updated sample app
authorlankamadan <lanka.madan@samsung.com>
Thu, 10 Sep 2015 12:44:35 +0000 (21:44 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Thu, 10 Sep 2015 14:27:14 +0000 (14:27 +0000)
- Arduino EasySetup APIs are updated
- ES_RESULT is changed to ESResult
- Updated sample application

Change-Id: I29c3d024ea42c1c4edf6a3586e604bfe9f18b1aa
Signed-off-by: lankamadan <lanka.madan@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2446
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/easy-setup/sampleapp/arduino/thinserver/thinserver.cpp [changed mode: 0644->0755]
service/easy-setup/sdk/common/common.h [changed mode: 0644->0755]
service/easy-setup/sdk/enrollee/arduino/wifi/networkHandler.cpp [changed mode: 0644->0755]
service/easy-setup/sdk/enrollee/arduino/wifi/networkHandler.h [changed mode: 0644->0755]
service/easy-setup/sdk/enrollee/common/inc/easysetup.h [changed mode: 0644->0755]
service/easy-setup/sdk/enrollee/common/inc/resourceHandler.h [changed mode: 0644->0755]
service/easy-setup/sdk/enrollee/common/src/easysetup.cpp [changed mode: 0644->0755]
service/easy-setup/sdk/enrollee/common/src/resourceHandler.cpp [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 8a33941..73ebee2
@@ -22,7 +22,6 @@
 #include "Arduino.h"
 
 #include "logger.h"
-#include "ocstack.h"
 #include <string.h>
 
 #ifdef ARDUINOWIFI
@@ -50,7 +49,7 @@ PROGMEM const char TAG[] = "ThinServer";
 char ssid[] = "hub2.4G";
 char passwd[] = "11112222";
 
-void EventCallbackInApp(ES_RESULT eventFlag)
+void EventCallbackInApp(ESResult eventFlag)
 {
     Serial.println("callback!!! in app");
 }
@@ -81,23 +80,17 @@ void setup()
     OC_LOG_INIT();
     OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
 
-    FindNetworkForOnboarding(ES_WIFI, ssid, passwd, EventCallbackInApp);
-
-    // Initialize the OC Stack in Server mode
-    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
+    if(InitEasySetup(ES_WIFI, ssid, passwd, EventCallbackInApp) != ES_OK)
     {
-        OC_LOG(ERROR, TAG, PCF("OCStack init error"));
+        OC_LOG(ERROR, TAG, "EasySetup Init Failed");
         return;
     }
 
-    InitializeProvisioning(EventCallbackInApp);
-
-   /* if (OCStartPresence(0) != OC_STACK_OK)
+    if(InitProvisioning(EventCallbackInApp)!= ES_OK)
     {
-        OC_LOG(ERROR, TAG, PCF("Start Presence init error"));
+        OC_LOG(ERROR, TAG, "Init Provisioning Failed");
         return;
     }
-       */
 }
 
 // The loop function is called in an endless loop
old mode 100644 (file)
new mode 100755 (executable)
index d1f469b..c9f1417
@@ -70,7 +70,7 @@ typedef enum
     ES_RECVREQOFNETRES,
     ES_RECVUPDATEOFPROVRES,
     ES_RECVTRIGGEROFPROVRES,
-} ES_RESULT;
+} ESResult;
 
 
 /**
old mode 100644 (file)
new mode 100755 (executable)
index 95ad75e..f17c91e
@@ -34,7 +34,7 @@ static const char ARDUINO_WIFI_SHIELD_UDP_FW_VER[] = "1.1.0";
 
 IPAddress myIP;
 
-ES_RESULT ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback cb)
+ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback cb)
 {
     char *fwVersion;
     int status = WL_IDLE_STATUS;
old mode 100644 (file)
new mode 100755 (executable)
index 7509940..5da8cbb
@@ -40,7 +40,7 @@
 #define MAXNUMTYPE 5
 #define MAXADDRLEN 15
 
-typedef void (*NetworkEventCallback)(ES_RESULT);
+typedef void (*NetworkEventCallback)(ESResult);
 
 enum NetworkType
 {
@@ -59,7 +59,7 @@ typedef struct NETWORKINFO
     byte mac[6];
 } NetworkInfo;
 
-ES_RESULT ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback);
+ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback);
 int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info);
 
 #endif
old mode 100644 (file)
new mode 100755 (executable)
index 9066e50..4358c51
 #include "networkHandler.h"
 #include "resourceHandler.h"
 
-typedef void (*EventCallback)(ES_RESULT);
+typedef void (*EventCallback)(ESResult);
 
-OCStackResult Init();
+ESResult InitEasySetup(NetworkType networkType, const char *ssid, const char *passwd,
+              EventCallback cb);
+ESResult InitProvisioning(EventCallback cb);
+ESResult TerminateEasySetup();
 
-ES_RESULT FindNetworkForOnboarding(NetworkType networkType, EventCallback);
-ES_RESULT FindNetworkForOnboarding(NetworkType networkType, const char *name, const char *pass,
-        EventCallback);
-
-//OCStackResult FindNetworkForOnboarding(NetworkType networkType, char *name);
-//OCStackResult FindNetworkForOnboarding(NetworkType networkType, char *name, char *pass);
-
-ES_RESULT InitializeProvisioning(EventCallback);
old mode 100644 (file)
new mode 100755 (executable)
index 0f1567f..13d0ee1
@@ -32,7 +32,7 @@
 #ifndef ES_RESOURCE_HANDLER_H_
 #define ES_RESOURCE_HANDLER_H_
 
-typedef void (*ResourceEventCallback)(ES_RESULT);
+typedef void (*ResourceEventCallback)(ESResult);
 
 /* Structure to represent a Light resource */
 typedef struct PROVRESOURCE
old mode 100644 (file)
new mode 100755 (executable)
index c44fdf0..cfd7bf3
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include "easysetup.h"
-
-/// WiFi network info and credentials
-char defaultSsid[] = "EasyConnect";
-char defaultPass[] = "EasyConnect";
+#include "ocstack.h"
 
 int g_eventflag = 0;
 int g_cnt = 0;
@@ -32,11 +29,11 @@ char *targetPass;
 EventCallback g_cbForProvisioning = NULL;
 EventCallback g_cbForOnboarding = NULL;
 
-void EventCallbackInOnboarding(ES_RESULT event);
-void EventCallbackInProvisioning(ES_RESULT event);
-void EventCallbackAfterProvisioning(ES_RESULT event);
+void EventCallbackInOnboarding(ESResult event);
+void EventCallbackInProvisioning(ESResult event);
+void EventCallbackAfterProvisioning(ESResult event);
 
-void EventCallbackInOnboarding(ES_RESULT event)
+void EventCallbackInOnboarding(ESResult event)
 {
     if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
     {
@@ -47,15 +44,21 @@ void EventCallbackInOnboarding(ES_RESULT event)
     }
 }
 
-void EventCallbackInProvisioning(ES_RESULT event)
+void EventCallbackInProvisioning(ESResult event)
 {
-    ES_RESULT res = ES_OK;
+    ESResult res = ES_OK;
 
     if (event == ES_RECVTRIGGEROFPROVRES)
     {
         targetSsid = (char *) malloc(MAXSSIDLEN);
         targetPass = (char *) malloc(MAXNETCREDLEN);
 
+        if(TerminateEasySetup() != OC_STACK_OK)
+        {
+            OC_LOG(ERROR, TAG, "Terminating stack failed");
+            return;
+        }
+
         GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
 
         res = ConnectToWiFiNetwork(targetSsid, targetPass, EventCallbackAfterProvisioning);
@@ -67,7 +70,7 @@ void EventCallbackInProvisioning(ES_RESULT event)
     }
 }
 
-void EventCallbackAfterProvisioning(ES_RESULT event)
+void EventCallbackAfterProvisioning(ESResult event)
 {
     if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
     {
@@ -78,8 +81,16 @@ void EventCallbackAfterProvisioning(ES_RESULT event)
     }
 }
 
-ES_RESULT FindNetworkForOnboarding(NetworkType networkType, EventCallback cb)
+ESResult FindNetworkForOnboarding(NetworkType networkType,
+                                           const char *ssid,
+                                           const char *passwd,
+                                           EventCallback cb)
 {
+    if (!ssid || !passwd)
+    {
+        return ES_ERROR;
+    }
+
     if (networkType == ES_WIFI)
     {
         if (g_cbForOnboarding == NULL)
@@ -87,30 +98,48 @@ ES_RESULT FindNetworkForOnboarding(NetworkType networkType, EventCallback cb)
             g_cbForOnboarding = cb;
         }
 
-        return ConnectToWiFiNetwork(defaultSsid, defaultPass, EventCallbackInOnboarding);
+        return ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding);
     }
 }
 
-ES_RESULT FindNetworkForOnboarding(NetworkType networkType, const char *ssid, const char *passwd,
-        EventCallback cb)
+
+ESResult InitEasySetup(NetworkType networkType, const char *ssid, const char *passwd,
+              EventCallback cb)
 {
-    if (!ssid || !passwd)
+    if(FindNetworkForOnboarding(networkType, ssid, passwd, cb) != ES_OK)
     {
+        OC_LOG(ERROR, TAG, "OnBoarding Failed");
         return ES_ERROR;
     }
 
-    if (networkType == ES_WIFI)
+    // Initialize the OC Stack in Server mode
+    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
     {
-        if (g_cbForOnboarding == NULL)
-        {
-            g_cbForOnboarding = cb;
-        }
+        OC_LOG(INFO, TAG, PCF("OCStack init error"));
+        return ES_ERROR;
+    }
+    else
+    {
+        OC_LOG(ERROR, TAG, PCF("OCStack init success"));
+        return ES_OK;
+    }
+}
 
-        return ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding);
+ESResult TerminateEasySetup()
+{
+    if(OCStop() != OC_STACK_OK)
+    {
+        OC_LOG(ERROR, TAG, PCF("OCStack stop failed"));
+        return ES_ERROR;
+    }
+    else
+    {
+        OC_LOG(ERROR, TAG, PCF("OCStack stop success"));
+        return ES_OK;
     }
 }
 
-ES_RESULT InitializeProvisioning(EventCallback cb)
+ESResult InitProvisioning(EventCallback cb)
 {
     if (cb == NULL)
     {
old mode 100644 (file)
new mode 100755 (executable)
index 3660137..3f25f06
@@ -26,13 +26,19 @@ PROGMEM const char TAG[] = "resourceHandler";
 ProvResource g_prov;
 NetResource g_net;
 
-OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag, OCEntityHandlerRequest *, void *callback);
+OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
+                                              OCEntityHandlerRequest *ehRequest,
+                                              void *callback);
 const char *getResult(OCStackResult result);
 
-OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,OCRepPayload** payload);
-OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,OCRepPayload** payload);
-OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,OCRepPayload** payload);
+OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
+                                               OCRepPayload** payload);
+OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
+                                               OCRepPayload** payload);
+OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
+                                               OCRepPayload** payload);
 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
+
 int g_flag = 0;
 
 ResourceEventCallback g_cbForResEvent = NULL;
@@ -96,7 +102,8 @@ OCStackResult CreateNetworkResource()
     return res;
 }
 
-OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
+OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
+                                                OCRepPayload **payload)
 {
     OCEntityHandlerResult ehResult = OC_EH_ERROR;
     if(!ehRequest)
@@ -124,7 +131,7 @@ OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRep
 }
 
 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
-        OCRepPayload** payload)
+                                                OCRepPayload** payload)
 {
 
     OCEntityHandlerResult ehResult=OC_EH_ERROR;
@@ -171,7 +178,8 @@ OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
 }
 
 
-OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
+OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
+                                                OCRepPayload** payload)
 {
     OCEntityHandlerResult ehResult = OC_EH_ERROR;
     if(!ehRequest)
@@ -232,7 +240,6 @@ OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
 
 // This is the entity handler for the registered resource.
 // This is invoked by OCStack whenever it recevies a request for this resource.
-
 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
         OCEntityHandlerRequest* entityHandlerRequest,void *callback)
 {
@@ -274,7 +281,8 @@ OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
             response.requestHandle = entityHandlerRequest->requestHandle;
             response.resourceHandle = entityHandlerRequest->resource;
             response.ehResult = ehRet;
-            response.payload = (OCPayload*)(payload);  //response uses OCPaylod while all get,put methodes use OCRepPayload
+            //response uses OCPaylod while all get,put methodes use OCRepPayload
+            response.payload = (OCPayload*)(payload);
             response.numSendVendorSpecificHeaderOptions = 0;
             memset(response.sendVendorSpecificHeaderOptions, 0,
                     sizeof response.sendVendorSpecificHeaderOptions);