Modify a group management implementation with CBOR conversion in c stack
authorJihun Ha <jihun.ha@samsung.com>
Thu, 27 Aug 2015 06:33:08 +0000 (15:33 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Thu, 27 Aug 2015 10:39:26 +0000 (10:39 +0000)
Previously, oicgroup.c file dealt with processing group action requests and
constructing the corresponding responses and it was strongly related with
json encoding/decoding. With this commit, all json-based codes in the file
have been updated to use CBOR instead of json.

Change-Id: I2f0953fab9c77024435d389538a4f43b600d7864
Signed-off-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2297
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
resource/csdk/stack/include/internal/oicgroup.h [changed mode: 0644->0755]
resource/csdk/stack/include/ocpayload.h [changed mode: 0644->0755]
resource/csdk/stack/src/occollection.c [changed mode: 0644->0755]
resource/csdk/stack/src/ocpayload.c [changed mode: 0644->0755]
resource/csdk/stack/src/oicgroup.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 3b0c784..6ba6be2
@@ -45,7 +45,7 @@ OCStackResult DeleteActionSets(OCResource** resource);
 
 OCStackResult FindAndDeleteActionSet(OCResource **resource, const char * actionsetName);
 
-OCStackResult ExtractKeyValueFromRequest(char *request, char **key, char **value);
+OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest, char **key, char **value);
 
 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc);
 
@@ -58,7 +58,7 @@ void ActionSetCD(void *context);
 
 
 OCStackResult
-BuildCollectionGroupActionJSONResponse(OCMethod method/*OCEntityHandlerFlag flag*/,
+BuildCollectionGroupActionCBORResponse(OCMethod method/*OCEntityHandlerFlag flag*/,
         OCResource *resource, OCEntityHandlerRequest *ehRequest);
 
 
old mode 100644 (file)
new mode 100755 (executable)
index 4850d92..cb950f9
@@ -63,7 +63,7 @@ bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, do
 
 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value);
 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value);
-bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, const char** value);
+bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value);
 
 bool OCRepPayloadSetPropBool(OCRepPayload* payload, const char* name, bool value);
 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value);
old mode 100644 (file)
new mode 100755 (executable)
index 87664e2..5fed1e6
@@ -420,7 +420,7 @@ OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag,
                 return HandleBatchInterface(ehRequest);
 
             case STACK_IF_GROUP:
-                return BuildCollectionGroupActionJSONResponse(OC_REST_GET/*flag*/,
+                return BuildCollectionGroupActionCBORResponse(OC_REST_GET/*flag*/,
                         (OCResource *) ehRequest->resource, ehRequest);
             default:
                 return OC_STACK_ERROR;
@@ -449,7 +449,7 @@ OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag,
             {
                 OC_LOG(INFO, TAG, PCF("IF_COLLECTION PUT with request ::\n"));
                 OC_LOG_PAYLOAD(INFO, TAG, ehRequest->payload);
-                return BuildCollectionGroupActionJSONResponse(OC_REST_PUT/*flag*/,
+                return BuildCollectionGroupActionCBORResponse(OC_REST_PUT/*flag*/,
                         (OCResource *) ehRequest->resource, ehRequest);
             }
             default:
@@ -465,7 +465,7 @@ OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag,
             {
                 OC_LOG(INFO, TAG, PCF("IF_COLLECTION POST with request ::\n"));
                 OC_LOG_PAYLOAD(INFO, TAG, ehRequest->payload);
-                return BuildCollectionGroupActionJSONResponse(OC_REST_POST/*flag*/,
+                return BuildCollectionGroupActionCBORResponse(OC_REST_POST/*flag*/,
                         (OCResource *) ehRequest->resource, ehRequest);
             }
             default:
@@ -479,7 +479,7 @@ OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag,
         {
             OC_LOG(INFO, TAG, PCF("IF_COLLECTION POST with request ::\n"));
             OC_LOG_PAYLOAD(INFO, TAG, ehRequest->payload);
-            return BuildCollectionGroupActionJSONResponse(OC_REST_POST/*flag*/,
+            return BuildCollectionGroupActionCBORResponse(OC_REST_POST/*flag*/,
                     (OCResource *) ehRequest->resource, ehRequest);
         }
         else
old mode 100644 (file)
new mode 100755 (executable)
index cc2ca22..3c7bcc5
@@ -540,7 +540,7 @@ bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, c
     return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
 }
 
-bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, const char** value)
+bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value)
 {
     OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
 
old mode 100644 (file)
new mode 100755 (executable)
index 3dd175d..00d429b
@@ -24,6 +24,8 @@
 
 #include "oicgroup.h"
 #include "cJSON.h"
+#include "cbor.h"
+#include "ocpayload.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
 #include "occollection.h"
 #define DO_ACTION               "DoAction"
 #define GET_ACTIONSET           "GetActionSet"
 #define ACTIONSET               "ActionSet"
+#define CANCEL_ACTIONSET        "CancelAction"
 #define DELETE_ACTIONSET        "DelActionSet"
 
-#define OIC_ACTION_PREFIX               "{\"oic\":[{\"rep\":{"
+#define DEFAULT_CONTEXT_VALUE 0x99
+
 #define VARIFY_POINTER_NULL(pointer, result, toExit) \
     if(pointer == NULL) \
     {\
@@ -527,34 +531,67 @@ OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
 
 }
 
-OCStackResult ExtractKeyValueFromRequest(char *request, char **key,
-        char **value)
+OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest,
+                                        char **key, char **value)
 {
     OCStackResult result = OC_STACK_OK;
-    size_t length = 0;
 
-    char* pRequest = (char *) request + strlen(OIC_ACTION_PREFIX);
-    char* iterToken, *iterTokenPtr;
+    char *actionSetStr;
+
+    if( NULL == ehRequest->payload )
+    {
+        result = OC_STACK_ERROR;
+        goto exit;
+    }
+
+    OCRepPayload* input;
 
-    iterToken = (char *) strtok_r(pRequest, ":", &iterTokenPtr);
-    VARIFY_POINTER_NULL(iterToken, result, exit);
-    length = strlen(iterToken) + 1;
+    input = (OCRepPayload*)(ehRequest->payload);
 
-    *key = (char *) OICMalloc(length);
-    VARIFY_POINTER_NULL(*key, result, exit)
+    if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
+    {
+        *key = OICStrdup(ACTIONSET);
+        VARIFY_POINTER_NULL(*key, result, exit);
 
-    strncpy(*key, iterToken + 1, length);
-    ((*key)[((length - 1) - 2)]) = '\0';
+        *value = OICStrdup(actionSetStr);
+        VARIFY_POINTER_NULL(*value, result, exit);
+    }
+    else if(OCRepPayloadGetPropString(input, DO_ACTION, &actionSetStr))
+    {
+        *key = OICStrdup(DO_ACTION);
+        VARIFY_POINTER_NULL(*key, result, exit);
 
-    iterToken = (char *) strtok_r(NULL, "}", &iterTokenPtr);
-    VARIFY_POINTER_NULL(iterToken, result, exit);
-    length = strlen(iterToken) + 1;
+        *value = OICStrdup(actionSetStr);
+        VARIFY_POINTER_NULL(*value, result, exit);
+    }
+    else if(OCRepPayloadGetPropString(input, GET_ACTIONSET, &actionSetStr))
+    {
+        *key = OICStrdup(GET_ACTIONSET);
+        VARIFY_POINTER_NULL(*key, result, exit);
 
-    *value = (char *) OICMalloc(length);
-    VARIFY_POINTER_NULL(*value, result, exit)
+        *value = OICStrdup(actionSetStr);
+        VARIFY_POINTER_NULL(*value, result, exit);
+    }
+    else if(OCRepPayloadGetPropString(input, DELETE_ACTIONSET, &actionSetStr))
+    {
+        *key = OICStrdup(DELETE_ACTIONSET);
+        VARIFY_POINTER_NULL(*key, result, exit);
 
-    strncpy(*value, iterToken + 1, length);
-    ((*value)[((length - 1) - 2)]) = '\0';
+        *value = OICStrdup(actionSetStr);
+        VARIFY_POINTER_NULL(*value, result, exit);
+    }
+    else if(OCRepPayloadGetPropString(input, CANCEL_ACTIONSET, &actionSetStr))
+    {
+        *key = OICStrdup(CANCEL_ACTIONSET);
+        VARIFY_POINTER_NULL(*key, result, exit);
+
+        *value = OICStrdup(actionSetStr);
+        VARIFY_POINTER_NULL(*value, result, exit);
+    }
+    else
+    {
+        result = OC_STACK_ERROR;
+    }
 
 exit:
     if (result != OC_STACK_OK)
@@ -827,46 +864,42 @@ OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
 {
     (void)context;
     (void)clientResponse;
-    OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
+    OC_LOG(INFO, TAG, PCF("Entering ActionSetCB"));
 
     ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
 
     if (info)
     {
-        int idx;
+        OCEntityHandlerResponse response = { 0 };
 
-        unsigned char *responseJson = NULL;
-        // TODO: Figure out what this does, change implementation
-        //responseJson = (unsigned char *) OICMalloc(
-        //        (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
-        //                + 1));
+        response.ehResult = OC_EH_OK;
 
-        if( responseJson == NULL )
+        if(NULL == clientResponse->payload)
+        {
+            OC_LOG(ERROR, TAG, "Error sending response");
             return OC_STACK_DELETE_TRANSACTION;
+        }
 
-        // We need the body of response.
-        // Copy the body from the response
-        // TODO: Taken out
-        //strcpy((char *) responseJson,
-        //        ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN));
-        //idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN;
-        // And insert NULL at the end of body.
-        (responseJson[idx]) = 0;
-
-        OCEntityHandlerResponse response = { 0 };
-        response.ehResult = OC_EH_OK;
-        // TODO: Removing payload size, waht goes here?
-        // response.payload = (char*)responseJson;
-        //response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
+        // Format the response.  Note this requires some info about the request
+        response.requestHandle = info->ehRequest;
+        response.resourceHandle = info->collResource;
+        response.payload = clientResponse->payload;
+        response.numSendVendorSpecificHeaderOptions = 0;
+        memset(response.sendVendorSpecificHeaderOptions, 0,
+                sizeof response.sendVendorSpecificHeaderOptions);
+        memset(response.resourceUri, 0, sizeof response.resourceUri);
+        // Indicate that response is NOT in a persistent buffer
         response.persistentBufferFlag = 0;
-        response.requestHandle = (OCRequestHandle) info->ehRequest;
-        response.resourceHandle = (OCResourceHandle) info->collResource;
 
-        OCDoResponse(&response);
+        // Send the response
+        if (OCDoResponse(&response) != OC_STACK_OK)
+        {
+            OC_LOG(ERROR, TAG, "Error sending response");
+            return OC_STACK_DELETE_TRANSACTION;
+        }
 
         RemoveClientRequestInfo(&clientRequstList, info);
         OCFREE(info)
-        OCFREE(responseJson)
     }
 
     return OC_STACK_KEEP_TRANSACTION;
@@ -917,6 +950,26 @@ OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
     return ret;
 }
 
+OCPayload* BuildActionCBOR(OCAction* action)
+{
+    OCRepPayload* payload = OCRepPayloadCreate();
+
+    if (!payload)
+    {
+        OC_LOG(INFO, TAG, PCF("Failed to create put payload object"));
+        return NULL;
+    }
+
+    OCCapability* pointerCapa = action->head;
+    while (pointerCapa)
+    {
+        OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
+        pointerCapa = pointerCapa->next;
+    }
+
+    return (OCPayload*) payload;
+}
+
 unsigned int GetNumOfTargetResource(OCAction *actionset)
 {
     int numOfResource = 0;
@@ -932,36 +985,40 @@ unsigned int GetNumOfTargetResource(OCAction *actionset)
     return numOfResource;
 }
 
+OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
+        OCPayload *payload)
+{
 
-#define DEFAULT_CONTEXT_VALUE 0x99
+    OCCallbackData cbData;
+    cbData.cb = &ActionSetCB;
+    cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
+    cbData.cd = NULL;
 
-OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
-        const unsigned char *action)
-{
-    (void)handle;
-    (void)targetUri;
-    (void)action;
-    // TODO: disabled since this is no longer compatible
-    return OC_STACK_NOTIMPL;
+    return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
+                       payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
 }
 
 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
         OCServerRequest* requestHandle)
 {
     OCStackResult result = OC_STACK_ERROR;
+
+    if( NULL == actionset->head)
+    {
+        return result;
+    }
+
     OCAction *pointerAction = actionset->head;
 
     while (pointerAction != NULL)
     {
-        unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
-        unsigned char* actionDescPtr = actionDesc;
-        uint16_t remaining = MAX_RESPONSE_LENGTH;
+        OCPayload* payload;
+        payload = BuildActionCBOR(pointerAction);
 
-        strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX,
-                strlen((const char *) OC_JSON_PREFIX) + 1);
-        BuildActionJSON(pointerAction, actionDescPtr, &remaining);
-        strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX,
-                strlen((const char *) OC_JSON_SUFFIX));
+        if(payload == NULL)
+        {
+            return result;
+        }
 
         ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
                 sizeof(ClientRequestInfo));
@@ -974,8 +1031,9 @@ OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
         info->collResource = resource;
         info->ehRequest = requestHandle;
 
-        result = SendAction(&info->required, pointerAction->resourceUri,
-                actionDescPtr);
+        result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
+                payload);
+
         if (result != OC_STACK_OK)
         {
             OICFree(info);
@@ -1067,283 +1125,291 @@ void DoScheduledGroupAction()
     return;
 }
 
-OCStackResult BuildCollectionGroupActionJSONResponse(
+OCStackResult BuildCollectionGroupActionCBORResponse(
         OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
         OCEntityHandlerRequest *ehRequest)
 {
     OCStackResult stackRet = OC_STACK_ERROR;
 
     OC_LOG(INFO, TAG, PCF("Group Action is requested."));
-    // if (stackRet == OC_STACK_OK)
-    {
-        char *doWhat = NULL;
-        char *details = NULL;
-
-        size_t bufferLength = 0;
-        unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
 
-        OCResource * collResource = (OCResource *) ehRequest->resource;
+    char *doWhat = NULL;
+    char *details = NULL;
 
-        char *jsonResponse;
+    stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
 
-        stackRet = OC_STACK_NOTIMPL;
-        // TODO: Fix?
-        //stackRet = ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload,
-        //        &doWhat, &details);
-
-        if(stackRet != OC_STACK_OK)
-        {
-            OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
-            return stackRet;
-        }
+    if(stackRet != OC_STACK_OK)
+    {
+        OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
+        return stackRet;
+    }
 
-        stackRet = OC_STACK_ERROR;
+    stackRet = OC_STACK_ERROR;
 
-        cJSON *json;
-        cJSON *format;
+    if (method == OC_REST_PUT)
+    {
+        OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
 
-        if (method == OC_REST_PUT)
+        if (strcmp(doWhat, ACTIONSET) == 0)
         {
-            json = cJSON_CreateObject();
-            cJSON_AddStringToObject(json, "href", resource->uri);
-            cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
-
-            OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
+            OCActionSet *actionSet = NULL;
+            stackRet = BuildActionSetFromString(&actionSet, details);
 
-            if (strcmp(doWhat, ACTIONSET) == 0)
+            if(stackRet == OC_STACK_OK)
             {
-                OCActionSet *actionSet = NULL;
-                stackRet = BuildActionSetFromString(&actionSet, details);
-
-                if(stackRet == OC_STACK_OK)
+                if (actionSet != NULL)
                 {
-                    if (actionSet != NULL)
+                    stackRet = AddActionSet(&resource->actionsetHead,
+                            actionSet);
+                    if (stackRet == OC_STACK_ERROR)
                     {
-                        stackRet = AddActionSet(&resource->actionsetHead,
-                                actionSet);
-                        if (stackRet == OC_STACK_ERROR)
+                        if(actionSet != NULL)
                         {
-                            if(actionSet != NULL)
-                            {
-                                DeleteActionSet( &actionSet );
-                            }
-                            OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
+                            DeleteActionSet( &actionSet );
                         }
+                        OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
                     }
                 }
                 else
                 {
                     stackRet = OC_STACK_ERROR;
+                    goto exit;
                 }
+            }
+            else
+            {
+                stackRet = OC_STACK_ERROR;
+            }
 
+        }
+        else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
+        {
+            if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
+            {
+                stackRet = OC_STACK_OK;
             }
-            else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
+            else
             {
-                if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
-                {
-                    stackRet = OC_STACK_OK;
-                }
-                else
-                {
-                    stackRet = OC_STACK_ERROR;
-                }
+                stackRet = OC_STACK_ERROR;
             }
+        }
 
-            jsonResponse = cJSON_Print(json);
-            cJSON_Delete(json);
+        OCRepPayload* payload = OCRepPayloadCreate();
 
-            OICStrcat((char*)buffer, sizeof(buffer), jsonResponse);
+        if(!payload)
+        {
+            OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
+            stackRet = OC_STACK_ERROR;
+        }
+        else
+        {
+            OCEntityHandlerResponse response = { 0 };
 
-            bufferLength = strlen((const char *) buffer);
-            if (bufferLength > 0)
+            if(stackRet == OC_STACK_OK)
+                response.ehResult = OC_EH_OK;
+            else
+                response.ehResult = OC_EH_ERROR;
+
+            // Format the response.  Note this requires some info about the request
+            response.requestHandle = ehRequest->requestHandle;
+            response.resourceHandle = ehRequest->resource;
+            response.payload = (OCPayload*) payload;
+            response.numSendVendorSpecificHeaderOptions = 0;
+            memset(response.sendVendorSpecificHeaderOptions, 0,
+                    sizeof response.sendVendorSpecificHeaderOptions);
+            memset(response.resourceUri, 0, sizeof response. resourceUri);
+            // Indicate that response is NOT in a persistent buffer
+            response.persistentBufferFlag = 0;
+            response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
+
+            // Send the response
+            if (OCDoResponse(&response) != OC_STACK_OK)
             {
-                OCEntityHandlerResponse response = { 0 };
-                if(stackRet == OC_STACK_OK)
-                    response.ehResult = OC_EH_OK;
-                else
-                    response.ehResult = OC_EH_ERROR;
-                // TODO: Fix
-                //response.payload = (char*)buffer;
-                //response.payloadSize = bufferLength + 1;
-                response.persistentBufferFlag = 0;
-                response.requestHandle =
-                        (OCRequestHandle) ehRequest->requestHandle;
-                response.resourceHandle = (OCResourceHandle) collResource;
-                stackRet = OCDoResponse(&response);
+                OC_LOG(ERROR, TAG, "Error sending response");
+                stackRet = OC_STACK_ERROR;
             }
         }
+    }
+    else if (method == OC_REST_POST)
+    {
+        OCActionSet *actionset = NULL;
 
-        if (method == OC_REST_POST)
+        OCRepPayload* payload = OCRepPayloadCreate();
+        OCRepPayloadSetUri(payload, resource->uri);
+
+        if ((strcmp(doWhat, DO_ACTION) == 0)
+                || (strcmp(doWhat, "DoScheduledAction") == 0))
         {
-            OCActionSet *actionset = NULL;
+            char *pActionsetName = NULL;
+            long int delay = -1;
 
-            json = cJSON_CreateObject();
-            cJSON_AddStringToObject(json, "href", resource->uri);
+            if (strcmp(doWhat, "DoScheduledAction") == 0)
+            {
+                stackRet = ExtractActionSetNameAndDelaytime(details,
+                        &pActionsetName, &delay);
 
-            if ((strcmp(doWhat, DO_ACTION) == 0)
-                    || (strcmp(doWhat, "DoScheduledAction") == 0))
+                OCFREE(details)
+                details = pActionsetName;
+            }
+            else
             {
-                char *pActionsetName = NULL;
-                long int delay = -1;
+                stackRet = OC_STACK_OK;
+            }
 
-                if (strcmp(doWhat, "DoScheduledAction") == 0)
+            if (stackRet == OC_STACK_OK)
+            {
+                if (GetActionSet(details, resource->actionsetHead,
+                        &actionset) != OC_STACK_OK)
                 {
-                    stackRet = ExtractActionSetNameAndDelaytime(details,
-                            &pActionsetName, &delay);
-
-                    OCFREE(details)
-                    details = pActionsetName;
+                    OC_LOG(INFO, TAG, PCF("ERROR"));
+                    stackRet = OC_STACK_ERROR;
                 }
-                else
+
+                if (actionset == NULL)
                 {
-                    stackRet = OC_STACK_OK;
+                    OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
+                    stackRet = OC_STACK_ERROR;
                 }
-
-                if (stackRet == OC_STACK_OK)
+                else
                 {
-                    if (GetActionSet(details, resource->actionsetHead,
-                            &actionset) != OC_STACK_OK)
+                    OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
+                    if (actionset->type == NONE)
                     {
-                        OC_LOG(INFO, TAG, PCF("ERROR"));
-                        stackRet = OC_STACK_ERROR;
-                    }
-
-                    if (actionset == NULL)
-                    {
-                        OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
-                        stackRet = OC_STACK_ERROR;
+                        OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
+                                actionset->actionsetName);
+                        unsigned int num = GetNumOfTargetResource(
+                                actionset->head);
+
+                        ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
+                                HandleAggregateResponse;
+                        ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
+                                num + 1;
+
+                        DoAction(resource, actionset,
+                                (OCServerRequest*) ehRequest->requestHandle);
+                        stackRet = OC_STACK_OK;
                     }
                     else
                     {
-                        OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
-                        if (actionset->type == NONE)
-                        {
-                            OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
-                                    actionset->actionsetName);
-                            unsigned int num = GetNumOfTargetResource(
-                                    actionset->head);
-
-                            ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
-                                    HandleAggregateResponse;
-                            ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
-                                    num + 1;
-
-                            DoAction(resource, actionset,
-                                    (OCServerRequest*) ehRequest->requestHandle);
-                            stackRet = OC_STACK_OK;
-                        }
-                        else
-                        {
-                            OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
-                                    actionset->actionsetName);
+                        OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
+                                actionset->actionsetName);
 
-                            delay =
-                                    (delay == -1 ? actionset->timesteps : delay);
+                        delay =
+                                (delay == -1 ? actionset->timesteps : delay);
 
-                            ScheduledResourceInfo *schedule;
-                            schedule = (ScheduledResourceInfo *) OICMalloc(
+                        ScheduledResourceInfo *schedule;
+                        schedule = (ScheduledResourceInfo *) OICMalloc(
+                                sizeof(ScheduledResourceInfo));
+
+                        if (schedule)
+                        {
+                            OC_LOG(INFO, TAG, PCF("Building New Call Info."));
+                            memset(schedule, 0,
                                     sizeof(ScheduledResourceInfo));
 
-                            if (schedule)
+                            schedule->resource = resource;
+                            schedule->actionset = actionset;
+                            schedule->ehRequest =
+                                    (OCServerRequest*) ehRequest->requestHandle;
+
+                            if (delay > 0)
                             {
-                                OC_LOG(INFO, TAG, PCF("Building New Call Info."));
-                                memset(schedule, 0,
-                                        sizeof(ScheduledResourceInfo));
-
-                                schedule->resource = resource;
-                                schedule->actionset = actionset;
-                                schedule->ehRequest =
-                                        (OCServerRequest*) ehRequest->requestHandle;
-
-                                if (delay > 0)
-                                {
-                                    OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
-                                            actionset->timesteps);
-
-                                    schedule->time = registerTimer(delay,
-                                            &schedule->timer_id,
-                                            &DoScheduledGroupAction);
-
-                                    AddScheduledResource(&scheduleResourceList,
-                                            schedule);
-                                    stackRet = OC_STACK_OK;
-                                }
-                                else
-                                {
-                                    stackRet = OC_STACK_ERROR;
-                                }
+                                OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
+                                        actionset->timesteps);
+
+                                schedule->time = registerTimer(delay,
+                                        &schedule->timer_id,
+                                        &DoScheduledGroupAction);
+
+                                AddScheduledResource(&scheduleResourceList,
+                                        schedule);
+                                stackRet = OC_STACK_OK;
+                            }
+                            else
+                            {
+                                stackRet = OC_STACK_ERROR;
                             }
                         }
                     }
                 }
             }
-            else if (strcmp(doWhat, "CancelAction") == 0)
-            {
-                ScheduledResourceInfo *info =
-                        GetScheduledResourceByActionSetName(scheduleResourceList, details);
+        }
+        else if (strcmp(doWhat, "CancelAction") == 0)
+        {
+            ScheduledResourceInfo *info =
+                    GetScheduledResourceByActionSetName(scheduleResourceList, details);
 
-                if(info != NULL)
-                {
-                    unregisterTimer(info->timer_id);
+            if(info != NULL)
+            {
+                unregisterTimer(info->timer_id);
 
-                    RemoveScheduledResource(&scheduleResourceList, info);
-                    stackRet = OC_STACK_OK;
-                }
-                else
-                {
-                    stackRet = OC_STACK_ERROR;
-                }
+                RemoveScheduledResource(&scheduleResourceList, info);
+                stackRet = OC_STACK_OK;
             }
+            else
+            {
+                stackRet = OC_STACK_ERROR;
+            }
+        }
+
+        else if (strcmp(doWhat, GET_ACTIONSET) == 0)
+        {
+            char *plainText = NULL;
+            OCActionSet *actionset = NULL;
 
-            else if (strcmp(doWhat, GET_ACTIONSET) == 0)
+            GetActionSet(details, resource->actionsetHead, &actionset);
+            if (actionset != NULL)
             {
-                char *plainText = NULL;
-                OCActionSet *actionset = NULL;
+                BuildStringFromActionSet(actionset, &plainText);
 
-                cJSON_AddItemToObject(json, "rep", format =
-                        cJSON_CreateObject());
-                GetActionSet(details, resource->actionsetHead, &actionset);
-                if (actionset != NULL)
+                if (plainText != NULL)
                 {
-                    BuildStringFromActionSet(actionset, &plainText);
-
-                    if (plainText != NULL)
-                    {
-                        cJSON_AddStringToObject(format, ACTIONSET, plainText);
-                    }
-                    OICFree(plainText);
-                    stackRet = OC_STACK_OK;
+                    OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
                 }
+                OICFree(plainText);
+                stackRet = OC_STACK_OK;
             }
+        }
 
-
-            jsonResponse = cJSON_Print(json);
-            cJSON_Delete(json);
-
-            OICStrcat((char*)buffer, sizeof(buffer), jsonResponse);
-
-            bufferLength = strlen((const char *) buffer);
-            if (bufferLength > 0)
+        if(!payload)
+        {
+            OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
+            stackRet = OC_STACK_ERROR;
+        }
+        else
+        {
+            OCEntityHandlerResponse response = { 0 };
+            if(stackRet == OC_STACK_OK)
+                response.ehResult = OC_EH_OK;
+            else
+                response.ehResult = OC_EH_ERROR;
+
+            // Format the response.  Note this requires some info about the request
+            response.requestHandle = ehRequest->requestHandle;
+            response.resourceHandle = ehRequest->resource;
+            response.payload = (OCPayload*) payload;
+            response.numSendVendorSpecificHeaderOptions = 0;
+            memset(response.sendVendorSpecificHeaderOptions, 0,
+                    sizeof response.sendVendorSpecificHeaderOptions);
+            memset(response.resourceUri, 0, sizeof response.resourceUri);
+            // Indicate that response is NOT in a persistent buffer
+            response.persistentBufferFlag = 0;
+            response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
+
+            // Send the response
+            if (OCDoResponse(&response) != OC_STACK_OK)
             {
-                OCEntityHandlerResponse response = { 0 };
-                if(stackRet == OC_STACK_OK)
-                    response.ehResult = OC_EH_OK;
-                else
-                    response.ehResult = OC_EH_ERROR;
-                // TODO: Implement
-                //response.payload = (char *)buffer;
-                //response.payloadSize = bufferLength + 1;
-                response.persistentBufferFlag = 0;
-                response.requestHandle =
-                        (OCRequestHandle) ehRequest->requestHandle;
-                response.resourceHandle = (OCResourceHandle) collResource;
-                stackRet = OCDoResponse(&response);
+                OC_LOG(ERROR, TAG, "Error sending response");
+                stackRet = OC_STACK_ERROR;
             }
         }
-
-        OCFREE(doWhat)
-        OCFREE(details)
     }
 
+exit:
+
+    OCFREE(doWhat)
+    OCFREE(details)
+
     return stackRet;
 }
+