Merge branch '1.3-rel' (5fdb8a1)
[iotivity.git] / service / easy-setup / enrollee / src / resourcehandler.c
index 06229d0..54b75f2 100644 (file)
 #include "oic_string.h"
 #include "oic_malloc.h"
 #include "cautilinterface.h"
+#include "experimental/payload_logging.h"
+
+// TODO: Remove this flag and enable the code it guards.
+// This is a temporary workaround to ignore the failure of OCLinksPayloadArrayCreate
+// in some cases. This allows the response to still be made, even though links property will be
+// missing.
+// Bug: IOT-2762
+#define ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
 
 /**
  * @var ES_RH_TAG
@@ -52,11 +60,13 @@ OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandle
 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
-void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
+OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
 void updateEasySetupConnectProperty(OCRepPayload* input);
-void updateWiFiConfResource(OCRepPayload* input);
+OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input);
 void updateCoapCloudConfResource(OCRepPayload* input);
 void updateDevConfResource(OCRepPayload* input);
+bool isAuthTypeSupported(WIFI_AUTHTYPE authType);
+bool isEncTypeSupported(WIFI_ENCTYPE encType);
 const char *getResult(OCStackResult result);
 
 ESConnectRequestCB gConnectRequestEvtCb = NULL;
@@ -96,7 +106,7 @@ void GetInterfaceNameFromQuery(const char *query, char **iface)
 
 bool CompareResourceInterface(const char *from, const char *iface)
 {
-    char *if_ptr;
+    char *if_ptr = NULL;
     GetInterfaceNameFromQuery(from, &if_ptr);
     if (!if_ptr)
     {
@@ -251,6 +261,13 @@ OCStackResult initWiFiConfResource(bool isSecured)
         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
     }
 
+    if (res != OC_STACK_OK)
+    {
+        OIC_LOG_V(ERROR, ES_RH_TAG, "Failed to create WiFiConf resource with result: %s",
+            getResult(res));
+        return res;
+    }
+
     res = OCBindResourceInterfaceToResource(g_ESWiFiConfResource.handle,
     OC_RSRVD_INTERFACE_READ_WRITE);
     if (res != OC_STACK_OK)
@@ -293,6 +310,13 @@ OCStackResult initCoapCloudConfResource(bool isSecured)
                 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
     }
 
+    if (res != OC_STACK_OK)
+    {
+        OIC_LOG_V(ERROR, ES_RH_TAG, "Failed to create CoapCloudConf resource with result: %s",
+            getResult(res));
+        return res;
+    }
+
     res = OCBindResourceInterfaceToResource(g_ESCoapCloudConfResource.handle,
             OC_RSRVD_INTERFACE_READ_WRITE);
     if (res != OC_STACK_OK)
@@ -326,6 +350,13 @@ OCStackResult initDevConfResource(bool isSecured)
         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
     }
 
+    if (res != OC_STACK_OK)
+    {
+        OIC_LOG_V(ERROR, ES_RH_TAG, "Failed to create DevConf resource with result: %s",
+            getResult(res));
+        return res;
+    }
+
     res = OCBindResourceInterfaceToResource(g_ESDevConfResource.handle, OC_RSRVD_INTERFACE_READ);
     if (res != OC_STACK_OK)
     {
@@ -338,66 +369,90 @@ OCStackResult initDevConfResource(bool isSecured)
 
 }
 
-void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
+OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest,
+    OCRepPayload* input)
 {
     OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
 
-    // Below call is to allow cn update without 'rep' property.
-    // Can remove if no longer needed to support this way.
-    updateEasySetupConnectProperty(input);
-
+    OCEntityHandlerResult ehResult = OC_EH_OK;
     if (ehRequest->query)
     {
         if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
         {
+            bool hasError = false;
             // When Provisioning resource has a POST with BatchInterface
             // Parsing POST request on Batch Interface cosidering same format as GET using batch.
             OCRepPayload *children = input;
-
             while(children)
             {
-                char* href = NULL;
-                OCRepPayloadGetPropString(children, OC_RSRVD_HREF, &href);
-                OIC_LOG_V(DEBUG, ES_RH_TAG, "href [%s]", href);
-                ///TODO: Check why href value is null even though available in payload
+                char* uri = children->uri;
+                if (NULL == uri)
+                {
+                    OIC_LOG(DEBUG, ES_RH_TAG, "No URI found in request");
+                }
+                else
+                {
+                    OIC_LOG_V(DEBUG, ES_RH_TAG, "Request URI [%s]", uri);
+                }
 
                 OCRepPayload *repPayload = NULL;
                 OCRepPayloadGetPropObject(children, OC_RSRVD_REPRESENTATION, &repPayload);
-
-                char* uri = children->uri;
-                OIC_LOG_V(DEBUG, ES_RH_TAG, "uri [%s]", uri);
-
-                if(NULL == uri || NULL == repPayload)
+                if (NULL == repPayload)
                 {
+                    OIC_LOG(ERROR, ES_RH_TAG, "repPayload is null!");
                     children = children->next;
-                    OCRepPayloadDestroy(repPayload);
-                    OICFree(href);
                     continue;
                 }
 
-                if (0 == strcmp(uri, OC_RSRVD_ES_URI_EASYSETUP))
+                // If uri is NULL, rep is applied to all resources in collection;
+                // otherwise its applied to specific target resources.
+                if (NULL == uri || 0 == strlen(uri) || 0 == strcmp(uri, OC_RSRVD_ES_URI_EASYSETUP))
                 {
                     updateEasySetupConnectProperty(repPayload);
                 }
-                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
+
+                if (NULL == uri || 0 == strlen(uri)
+                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
                 {
-                    updateWiFiConfResource(repPayload);
+                    if (updateWiFiConfResource(repPayload) != OC_EH_OK)
+                    {
+                        // Possibility of failure exist only when updating WiFiConf resource.
+                        // So error code is checked only for this function.
+                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to update WiFiConf resource.");
+                        hasError = true;
+                    }
                 }
-                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_COAPCLOUDCONF))
+
+                if (NULL == uri ||  0 == strlen(uri)
+                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_COAPCLOUDCONF))
                 {
                     updateCoapCloudConfResource(repPayload);
                 }
-                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_DEVCONF))
+
+                if (NULL == uri ||  0 == strlen(uri)
+                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_DEVCONF))
                 {
                     updateDevConfResource(repPayload);
                 }
 
                 children = children->next;
                 OCRepPayloadDestroy(repPayload);
-                OICFree(href);
-             }
+            }
+
+            if (hasError)
+            {
+               ehResult = OC_EH_BAD_REQ;
+            }
+        }
+        else if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
+        {
+            OIC_LOG(DEBUG, ES_RH_TAG, "Handling POST request on default interface");
+            updateEasySetupConnectProperty(input);
         }
     }
+
+    OIC_LOG(DEBUG, ES_RH_TAG, "updateEasySetupResource exit");
+    return ehResult;
 }
 
 void updateEasySetupConnectProperty(OCRepPayload* input)
@@ -449,70 +504,90 @@ void updateEasySetupConnectProperty(OCRepPayload* input)
     }
 }
 
-void updateWiFiConfResource(OCRepPayload* input)
+OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input)
 {
+    OCEntityHandlerResult ehResult = OC_EH_ERROR;
     ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
-
     if (wiFiData == NULL)
     {
         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
-        return;
+        return ehResult;
     }
 
+    char* ssid = NULL;
+    char* cred = NULL;
+    char *authType = NULL;
+    char *encType = NULL;
     memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
     memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
     wiFiData->authtype = NONE_AUTH;
     wiFiData->enctype = NONE_AUTH;
     wiFiData->userdata = NULL;
 
-    char* ssid = NULL;
-    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
-    {
-        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
-        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
-        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
-                g_ESWiFiConfResource.ssid);
-    }
-
-    char* cred = NULL;
-    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
-    {
-        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
-        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
-        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
-                g_ESWiFiConfResource.cred);
-    }
-
     bool validAuthType = false;
-    char *authType = NULL;
     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
     {
         WIFI_AUTHTYPE tmp;
         validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
-        if (validAuthType == true)
+        if (validAuthType && isAuthTypeSupported(tmp))
         {
-            g_ESWiFiConfResource.authType = tmp;
-            wiFiData->authtype = g_ESWiFiConfResource.authType;
+            wiFiData->authtype = tmp;
             OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
-                    g_ESWiFiConfResource.authType);
+                    wiFiData->authtype);
+        }
+        else
+        {
+            OIC_LOG(ERROR, ES_RH_TAG, "AuthType is not supported.");
+            ehResult = OC_EH_BAD_REQ;
+            goto EXIT;
         }
     }
 
     bool validEncType = false;
-    char *encType = NULL;
     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
     {
         WIFI_ENCTYPE tmp;
         validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
-        if (validEncType == true)
+        if (validEncType && isEncTypeSupported(tmp))
         {
-            g_ESWiFiConfResource.encType = tmp;
-            wiFiData->enctype = g_ESWiFiConfResource.encType;
+            wiFiData->enctype = tmp;
             OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
-                    g_ESWiFiConfResource.encType);
+                    wiFiData->enctype);
+        }
+        else
+        {
+            OIC_LOG(ERROR, ES_RH_TAG, "EncType is not supported.");
+            ehResult = OC_EH_BAD_REQ;
+            goto EXIT;
         }
     }
 
+    if (validAuthType)
+    {
+        g_ESWiFiConfResource.authType = wiFiData->authtype;
+    }
+
+    if (validEncType)
+    {
+        g_ESWiFiConfResource.encType = wiFiData->enctype;
+    }
+
+    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
+    {
+        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
+        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
+        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
+                g_ESWiFiConfResource.ssid);
+    }
+
+    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
+    {
+        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
+        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
+        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
+                g_ESWiFiConfResource.cred);
+    }
+
     if (gReadUserdataCb)
     {
         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
@@ -538,11 +613,15 @@ void updateWiFiConfResource(OCRepPayload* input)
         OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
     }
 
+    ehResult = OC_EH_OK;
+
+EXIT:
     OICFree(encType);
     OICFree(authType);
     OICFree(cred);
     OICFree(ssid);
     OICFree(wiFiData);
+    return ehResult;
 }
 
 void updateCoapCloudConfResource(OCRepPayload* input)
@@ -648,7 +727,7 @@ void updateDevConfResource(OCRepPayload* input)
 
     // If a writable property in oic.r.devconf is added later,
     // a condition for calling a resistered callback should be implemented also.
-    if( devConfData->userdata != NULL )
+    if (devConfData->userdata != NULL)
     {
         OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
 
@@ -970,238 +1049,153 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
         return NULL;
     }
 
-    // Requested interface is Link list interface
-    if (!ehRequest->query ||
-        (ehRequest->query && !strcmp(ehRequest->query, "")) ||
-        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
-        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
-    {
-        const OCRepPayload *arrayPayload[3] = {NULL};
-
-        int childResCnt = 0;
-
-        if (g_ESWiFiConfResource.handle != NULL)
-        {
-            OCRepPayload *add = OCRepPayloadCreate();
-            if (!add)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
-            char **resourceType = NULL;
-            resourceType = (char **)OICMalloc(sizeof(char *) * 1);
-            char **resourceInterface = NULL;
-            resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
-
-            if (!resourceType || !resourceInterface)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
-            resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
-
-            add->base.type = PAYLOAD_TYPE_REPRESENTATION;
-            OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
-            OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
-                                            (const char **)resourceType, dimensions);
-            OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
-                                            (const char **)resourceInterface, dimensions);
-
-            OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
-            OCRepPayload *policy = OCRepPayloadCreate();
-            if (!policy)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
-                                    ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
-            if (p & OC_SECURE)
-            {
-                OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
-                uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
-                                                                    ehRequest->devAddr.flags);
-                OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
-            }
-
-            OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
-
-            arrayPayload[childResCnt++] = add;
-        }
-
-        if (g_ESDevConfResource.handle != NULL)
-        {
-            OCRepPayload *add = OCRepPayloadCreate();
-            if (!add)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
-            char **resourceType = NULL;
-            resourceType = (char **)OICMalloc(sizeof(char *) * 1);
-            char **resourceInterface = NULL;
-            resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
-
-            if (!resourceType || !resourceInterface)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
-            resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
-
-            add->base.type = PAYLOAD_TYPE_REPRESENTATION;
-            OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
-            OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
-                                            (const char **)resourceType, dimensions);
-            OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
-                                            (const char **)resourceInterface, dimensions);
-
-            OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle);
-            OCRepPayload *policy = OCRepPayloadCreate();
-            if (!policy)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
-                                    ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
-            if (p & OC_SECURE)
-            {
-                OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
-                uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
-                                                                    ehRequest->devAddr.flags);
-                OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
-            }
+    OIC_LOG_V(DEBUG, ES_RH_TAG, "constructResponseOfEasySetup: qry = %s",
+            (ehRequest->query) ? (ehRequest->query) : "null");
 
-            OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
+    // Requested interface is Link list or baseline interface
+     if (!ehRequest->query ||
+         (ehRequest->query && !strcmp(ehRequest->query, "")) ||
+         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
+         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
+     {
+        size_t arraySize;
+        OCRepPayload **linkArr = OCLinksPayloadArrayCreate(OC_RSRVD_ES_URI_EASYSETUP, ehRequest,
+            true, &arraySize);
 
-            arrayPayload[childResCnt++] = add;
-        }
+#ifdef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
+        bool linkArrConstructed = true; // TODO: Remove this when
+                                        // ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE is removed.
+#endif
 
-        if (g_ESCoapCloudConfResource.handle != NULL)
+        if (!linkArr || (arraySize == 0))
         {
-            OCRepPayload *add = OCRepPayloadCreate();
-            if (!add)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
+            OIC_LOG(ERROR, ES_RH_TAG, "Failed to create Easy Setup collections ll response.");
 
-            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
-            char **resourceType = NULL;
-            resourceType = (char **)OICMalloc(sizeof(char *) * 1);
-            char **resourceInterface = NULL;
-            resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
-
-            if (!resourceType || !resourceInterface)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
-            resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
-
-            add->base.type = PAYLOAD_TYPE_REPRESENTATION;
-            OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_COAPCLOUDCONF);
-            OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
-                                            (const char **)resourceType, dimensions);
-            OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
-                                            (const char **)resourceInterface, dimensions);
-
-            OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESCoapCloudConfResource.handle);
-            OCRepPayload *policy = OCRepPayloadCreate();
-            if (!policy)
-            {
-                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                return NULL;
-            }
-
-            OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
-                                    ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
-            if (p & OC_SECURE)
-            {
-                OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
-                uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
-                                                                    ehRequest->devAddr.flags);
-                OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
-            }
-
-            OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
-
-            arrayPayload[childResCnt++] = add;
+#ifdef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
+            linkArrConstructed = false;
+#else
+            OICFree(linkArr);
+            OCRepPayloadDestroy(payload);
+            return NULL;
+#endif
         }
-
-        size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
-
-        if (!ehRequest->query ||
-            (ehRequest->query && !strcmp(ehRequest->query, "")) ||
-            (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
+#ifndef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
+        else
         {
-            OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res (Default interface)");
-            OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
-            OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
-            OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
-            OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
-            OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
-            OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
-
-            OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
-            OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
+#endif
+            OIC_LOG(DEBUG, ES_RH_TAG, "Constructed links payload.");
 
-            if (g_ESEasySetupResource.numRequest > 0)
+            if (!ehRequest->query || (ehRequest->query && !strcmp(ehRequest->query, ""))
+                    || (ehRequest->query
+                            && CompareResourceInterface(ehRequest->query,
+                                    OC_RSRVD_INTERFACE_DEFAULT)))
             {
-                size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
-                int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest  * sizeof(int64_t));
-                if (!connectRequest)
+                OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res (Default interface)");
+                OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
+                OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
+                OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
+                OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
+                OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
+                OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
+
+                OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS,
+                        g_ESEasySetupResource.status);
+                OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE,
+                        g_ESEasySetupResource.lastErrCode);
+
+                if (g_ESEasySetupResource.numRequest > 0)
                 {
-                    OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
-                    return NULL;
+                    size_t dimensions[MAX_REP_ARRAY_DEPTH] = { g_ESEasySetupResource.numRequest, 0,
+                            0 };
+                    int64_t *connectRequest = (int64_t *) OICMalloc(
+                            g_ESEasySetupResource.numRequest * sizeof(int64_t));
+                    if (!connectRequest)
+                    {
+                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
+                        return NULL;
+                    }
+
+                    for (int i = 0; i < g_ESEasySetupResource.numRequest; ++i)
+                    {
+                        connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
+                    }
+
+                    bool b = OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT,
+                            (int64_t *) connectRequest, dimensions);
+                    if (!b)
+                    {
+                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to set array value for Connect property");
+                        OICFree(connectRequest);
+                    }
+                }
+                else
+                {
+                    OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
+                    size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0, 0, 0 };
+                    OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
                 }
 
-                for(int i = 0 ; i < g_ESEasySetupResource.numRequest  ; ++i)
+                if (gWriteUserdataCb)
                 {
-                    connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
+                    gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
                 }
 
-                bool b = OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
-                if (!b)
+#ifdef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
+                if (linkArrConstructed)
                 {
-                    OIC_LOG(ERROR, ES_RH_TAG, "Failed to set array value for Connect property");
-                    OICFree(connectRequest);
+#endif
+                    size_t dimensions[MAX_REP_ARRAY_DEPTH] = { arraySize, 0, 0 };
+                    OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_LINKS,
+                            (const OCRepPayload **) linkArr, dimensions);
+                    for (size_t i = 0; i < arraySize; ++i)
+                    {
+                        OCRepPayloadDestroy(linkArr[i]);
+                    }
+#ifdef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
                 }
+#endif
             }
-            else
-            {
-                OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
-                size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
-                OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
-            }
-
-            if (gWriteUserdataCb)
+            else // link list interface
             {
-                gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
+#ifdef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
+                if (linkArrConstructed)
+                {
+#endif
+                    uint16_t acceptVersion = 0;
+                    OCPayloadFormat contentFormat; // not used
+                    OCGetRequestPayloadVersion(ehRequest, &contentFormat, &acceptVersion);
+                    // acceptVersion("OCF-Accept-Content-Format-Version") is present for OCF Request
+                    if (acceptVersion != 0)
+                    {
+                        for (size_t i = 0; i < arraySize - 1; ++i)
+                        {
+                            linkArr[i]->next = linkArr[i + 1];
+                        }
+                        // payload is directly the linkArr array, free earlier allocated memory.
+                        OCRepPayloadDestroy(payload);
+                        payload = linkArr[0];
+                    }
+                    else // for backward compatibility with OIC
+                    {
+                        size_t dimensions[MAX_REP_ARRAY_DEPTH] = { arraySize, 0, 0 };
+                        OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_LINKS,
+                                (const OCRepPayload **) linkArr, dimensions);
+                        for (size_t i = 0; i < arraySize; ++i)
+                        {
+                            OCRepPayloadDestroy(linkArr[i]);
+                        }
+                    }
+#ifdef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
+                }
+#endif
             }
-
-            OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
+#ifndef ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE
         }
-        else    // link list interface
-        {
-            OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
-        }
-    } else if (
-        ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
-
+#endif
+        OICFree(linkArr);
+    }
+    else if (ehRequest->query
+            && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
     {
         OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res (Batch Interface)");
         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
@@ -1215,8 +1209,8 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
             return NULL;
         }
 
-        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {3, 0, 0};
-        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
+        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = { 3, 0, 0 };
+        char **interfaces = (char **) OICMalloc(3 * sizeof(char*));
         if (!interfaces)
         {
             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
@@ -1227,10 +1221,11 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
         interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_LL);
         interfaces[2] = OICStrdup(OC_RSRVD_INTERFACE_BATCH);
 
-        OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
+        OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (const char **) interfaces,
+                interfacesDimensions);
 
-        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
-        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
+        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = { 2, 0, 0 };
+        char **resourceTypes = (char **) OICMalloc(2 * sizeof(char*));
         if (!resourceTypes)
         {
             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
@@ -1240,26 +1235,30 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_EASYSETUP);
         resourceTypes[1] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COL);
 
-        OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
+        OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (const char **) resourceTypes,
+                resourceTypesDimensions);
 
         OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
-        OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
+        OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE,
+                g_ESEasySetupResource.lastErrCode);
         if (g_ESEasySetupResource.numRequest > 0)
         {
-            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
-            int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest  * sizeof(int64_t));
+            size_t dimensions[MAX_REP_ARRAY_DEPTH] = { g_ESEasySetupResource.numRequest, 0, 0 };
+            int64_t *connectRequest = (int64_t *) OICMalloc(
+                    g_ESEasySetupResource.numRequest * sizeof(int64_t));
             if (!connectRequest)
             {
                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
                 return NULL;
             }
 
-            for (int i = 0 ; i < g_ESEasySetupResource.numRequest  ; ++i)
+            for (int i = 0; i < g_ESEasySetupResource.numRequest; ++i)
             {
                 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
             }
 
-            bool b = OCRepPayloadSetIntArrayAsOwner(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
+            bool b = OCRepPayloadSetIntArrayAsOwner(repPayload, OC_RSRVD_ES_CONNECT,
+                    (int64_t *) connectRequest, dimensions);
             if (!b)
             {
                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to set array value for Connect property");
@@ -1269,7 +1268,7 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
         else
         {
             OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
-            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
+            size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0, 0, 0 };
             OCRepPayloadSetIntArrayAsOwner(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
         }
 
@@ -1279,37 +1278,32 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
         }
 
         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
-    }
 
-    if (ehRequest->query)
-    {
-        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
-        {// When Provisioning resource has a GET with BatchInterface
-            OCRepPayload* head = payload;
-            OCRepPayload* nextPayload = NULL;
+        OCRepPayload* head = payload;
+        OCRepPayload* nextPayload = NULL;
 
-            nextPayload = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_BATCH);
-            if (nextPayload != NULL)
-            {
-                payload->next = nextPayload;
-                payload = payload->next;
-            }
-
-            nextPayload = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_BATCH);
-            if (nextPayload != NULL)
-            {
-                payload->next = nextPayload;
-                payload = payload->next;
-            }
+        nextPayload = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_BATCH);
+        if (nextPayload != NULL)
+        {
+            payload->next = nextPayload;
+            payload = payload->next;
+        }
 
-            nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
-            if (nextPayload != NULL)
-            {
-                payload->next = nextPayload;
-            }
+        nextPayload = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_BATCH);
+        if (nextPayload != NULL)
+        {
+            payload->next = nextPayload;
+            payload = payload->next;
+        }
 
-            payload = head;
+        nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
+        if (nextPayload != NULL)
+        {
+            payload->next = nextPayload;
         }
+
+        payload = head;
+
     }
 
     return payload;
@@ -1483,7 +1477,7 @@ OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRep
         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
         return ehResult;
     }
-    if(!isValidESResourceHandle(ehRequest->resource))
+    if (!isValidESResourceHandle(ehRequest->resource))
     {
         OIC_LOG(ERROR, ES_RH_TAG, "Request does not have a valid Easy Setup Resource handle");
         return ehResult;
@@ -1499,7 +1493,7 @@ OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRep
 
     char *iface_name = NULL;
     GetInterfaceNameFromQuery(ehRequest->query, &iface_name);
-    if(!iface_name)
+    if (!iface_name)
     {
         iface_name = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
     }
@@ -1538,6 +1532,7 @@ OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRep
 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
 {
     OIC_LOG(DEBUG, ES_RH_TAG, "ProcessPostRequest enter");
+
     OCEntityHandlerResult ehResult = OC_EH_ERROR;
     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
     {
@@ -1552,6 +1547,8 @@ OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRe
         return ehResult;
     }
 
+    OIC_LOG_PAYLOAD(DEBUG, (OCPayload *)input);
+
     if (ehRequest->resource == g_ESEasySetupResource.handle)
     {
         if (ehRequest->query &&
@@ -1564,7 +1561,11 @@ OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRe
         }
         else
         {
-            updateEasySetupResource(ehRequest, input);
+            if (updateEasySetupResource(ehRequest, input) != OC_EH_OK)
+            {
+                OIC_LOG(ERROR, ES_RH_TAG, "Failed to update EasySetup resource.");
+                return OC_EH_BAD_REQ;
+            }
         }
     }
     else if (ehRequest->resource == g_ESWiFiConfResource.handle)
@@ -1576,7 +1577,11 @@ OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRe
         }
         else
         {
-            updateWiFiConfResource(input);
+            if (updateWiFiConfResource(input) != OC_EH_OK)
+            {
+                OIC_LOG(ERROR, ES_RH_TAG, "Failed to update WifiConf resource.");
+                return OC_EH_BAD_REQ;
+            }
         }
     }
     else if (ehRequest->resource == g_ESCoapCloudConfResource.handle)
@@ -1771,7 +1776,7 @@ OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
     {
         g_ESWiFiConfResource.supportedEncType[i] = (deviceProperty->WiFi).supportedEncType[i];
         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Enc Type : %d",
-                g_ESWiFiConfResource.supportedAuthType[i]);
+                g_ESWiFiConfResource.supportedEncType[i]);
     }
 
     OICStrcpy(g_ESDevConfResource.devName, OIC_STRING_MAX_VALUE,
@@ -1850,6 +1855,34 @@ OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
     return OC_EH_OK;
 }
 
+bool isAuthTypeSupported(WIFI_AUTHTYPE authType)
+{
+    bool supported = false;
+    for (uint8_t i = 0; i < g_ESWiFiConfResource.numSupportedAuthType; ++i)
+    {
+        if (g_ESWiFiConfResource.supportedAuthType[i] == authType)
+        {
+            supported = true;
+            break;
+        }
+    }
+    return supported;
+}
+
+bool isEncTypeSupported(WIFI_ENCTYPE encType)
+{
+    bool supported = false;
+    for (uint8_t i = 0; i < g_ESWiFiConfResource.numSupportedEncType; ++i)
+    {
+        if (g_ESWiFiConfResource.supportedEncType[i] == encType)
+        {
+            supported = true;
+            break;
+        }
+    }
+    return supported;
+}
+
 const char *getResult(OCStackResult result)
 {
     switch (result)