#include "Arduino.h"
#include "logger.h"
-#include "ocstack.h"
#include <string.h>
#ifdef ARDUINOWIFI
char ssid[] = "hub2.4G";
char passwd[] = "11112222";
-void EventCallbackInApp(ES_RESULT eventFlag)
+void EventCallbackInApp(ESResult eventFlag)
{
Serial.println("callback!!! in app");
}
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
ES_RECVREQOFNETRES,
ES_RECVUPDATEOFPROVRES,
ES_RECVTRIGGEROFPROVRES,
-} ES_RESULT;
+} ESResult;
/**
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;
#define MAXNUMTYPE 5
#define MAXADDRLEN 15
-typedef void (*NetworkEventCallback)(ES_RESULT);
+typedef void (*NetworkEventCallback)(ESResult);
enum NetworkType
{
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
#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);
#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
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#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;
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)
{
}
}
-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);
}
}
-void EventCallbackAfterProvisioning(ES_RESULT event)
+void EventCallbackAfterProvisioning(ESResult event)
{
if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
{
}
}
-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)
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)
{
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;
return res;
}
-OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
+OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
+ OCRepPayload **payload)
{
OCEntityHandlerResult ehResult = OC_EH_ERROR;
if(!ehRequest)
}
OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
- OCRepPayload** payload)
+ OCRepPayload** payload)
{
OCEntityHandlerResult ehResult=OC_EH_ERROR;
}
-OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
+OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
+ OCRepPayload** payload)
{
OCEntityHandlerResult ehResult = OC_EH_ERROR;
if(!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)
{
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);