replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / src / oicgroup.c
old mode 100644 (file)
new mode 100755 (executable)
index 741ebf5..5114f41
 
 #define _POSIX_C_SOURCE 200112L
 
+#include "iotivity_config.h"
+
 #include <string.h>
 
 #include "oicgroup.h"
+#if defined (__TIZENRT__)
+#include <apps/netutils/cJSON.h>
+#else
 #include "cJSON.h"
-#include "ocmalloc.h"
+#endif
+#include "cbor.h"
+#include "ocpayload.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
 #include "occollection.h"
 #include "logger.h"
 #include "timer.h"
 
-#ifndef WITH_ARDUINO
-#include <pthread.h>
-#endif
-
-#define TAG PCF("OICGROUP")
+#define TAG "OIC_RI_GROUP"
 
 #define DESC_DELIMITER          "\""
 #define ACTION_DELIMITER        "*"
 #define DO_ACTION               "DoAction"
 #define GET_ACTIONSET           "GetActionSet"
 #define ACTIONSET               "ActionSet"
+#define CANCEL_ACTIONSET        "CancelAction"
 #define DELETE_ACTIONSET        "DelActionSet"
 
-#define OIC_ACTION_PREFIX               "{\"oc\":[{\"rep\":{"
+#define DEFAULT_CONTEXT_VALUE 0x99
+
 #define VARIFY_POINTER_NULL(pointer, result, toExit) \
     if(pointer == NULL) \
     {\
 
 #define OCFREE(pointer) \
     { \
-        OCFree(pointer); \
+        OICFree(pointer); \
         pointer = NULL; \
     }
 
-#ifndef WITH_ARDUINO
-pthread_mutex_t lock;
+// Mutex implementation macros
+#if defined(HAVE_PTHREAD_H)
+
+ #include <pthread.h>
+ pthread_mutex_t g_scheduledResourceLock;
+ #define MUTEX_LOCK(ARG_NAME)       { pthread_mutex_lock(ARG_NAME); }
+ #define MUTEX_UNLOCK(ARG_NAME)     { pthread_mutex_unlock(ARG_NAME); }
+ #define MUTEX_INITIALIZE(ARG_NAME) { }
+ #define MUTEX_TERMINATE(ARG_NAME)  { }
+
+#elif defined(HAVE_WINDOWS_H)
+
+ #include <windows.h>
+ CRITICAL_SECTION g_scheduledResourceLock;
+ bool g_initializedScheduledResourceLock = false;
+ #define MUTEX_LOCK(ARG_NAME)       { EnterCriticalSection(ARG_NAME); }
+ #define MUTEX_UNLOCK(ARG_NAME)     { LeaveCriticalSection(ARG_NAME); }
+ #define MUTEX_INITIALIZE(ARG_NAME) { assert(!g_initializedScheduledResourceLock); \
+                                      InitializeCriticalSection(ARG_NAME); \
+                                      g_initializedScheduledResourceLock = true; \
+                                    }
+ #define MUTEX_TERMINATE(ARG_NAME)  { if (g_initializedScheduledResourceLock) \
+                                      { \
+                                          DeleteCriticalSection(ARG_NAME); \
+                                          g_initializedScheduledResourceLock = false; \
+                                      } \
+                                    }
+
+#elif defined(WITH_ARDUINO)
+
+ #define MUTEX_LOCK(ARG_NAME)       { }
+ #define MUTEX_UNLOCK(ARG_NAME)     { }
+ #define MUTEX_INITIALIZE(ARG_NAME) { }
+ #define MUTEX_TERMINATE(ARG_NAME)  { }
+
+#else
+
+ ERROR Need mutex implementation on this platform
+
 #endif
 
 enum ACTION_TYPE
@@ -88,16 +132,14 @@ typedef struct scheduledresourceinfo
     struct scheduledresourceinfo* next;
 } ScheduledResourceInfo;
 
-ScheduledResourceInfo *scheduleResourceList = NULL;
+ScheduledResourceInfo *g_scheduleResourceList = NULL;
 
 void AddScheduledResource(ScheduledResourceInfo **head,
         ScheduledResourceInfo* add)
 {
-    OC_LOG(INFO, TAG, PCF("AddScheduledResource Entering..."));
+    OIC_LOG(INFO, TAG, "AddScheduledResource Entering...");
 
-#ifndef WITH_ARDUINO
-    pthread_mutex_lock(&lock);
-#endif
+    MUTEX_LOCK(&g_scheduledResourceLock);
     ScheduledResourceInfo *tmp = NULL;
 
     if (*head != NULL)
@@ -114,25 +156,21 @@ void AddScheduledResource(ScheduledResourceInfo **head,
     {
         *head = add;
     }
-#ifndef WITH_ARDUINO
-    pthread_mutex_unlock(&lock);
-#endif
+    MUTEX_UNLOCK(&g_scheduledResourceLock);
 }
 
 ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
 {
-    OC_LOG(INFO, TAG, PCF("GetScheduledResource Entering..."));
+    OIC_LOG(INFO, TAG, "GetScheduledResource Entering...");
 
-#ifndef WITH_ARDUINO
-    pthread_mutex_lock(&lock);
-#endif
+    MUTEX_LOCK(&g_scheduledResourceLock);
 
     time_t t_now;
 
     ScheduledResourceInfo *tmp = NULL;
     tmp = head;
 
-#ifndef WITH_ARDUINO
+#if !defined(WITH_ARDUINO)
     time(&t_now);
 #else
     t_now = now();
@@ -143,15 +181,11 @@ ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
         while (tmp)
         {
             time_t diffTm = 0;
-#ifndef WITH_ARDUINO
-            diffTm = timespec_diff(tmp->time, t_now);
-#else
             diffTm = timespec_diff(tmp->time, t_now);
-#endif
 
             if (diffTm <= (time_t) 0)
             {
-                OC_LOG(INFO, TAG, PCF("return Call INFO."));
+                OIC_LOG(INFO, TAG, "return Call INFO.");
                 goto exit;
             }
 
@@ -160,23 +194,22 @@ ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
     }
 
     exit:
-#ifndef WITH_ARDUINO
-    pthread_mutex_unlock(&lock);
-#endif
+
+    MUTEX_UNLOCK(&g_scheduledResourceLock);
+
     if (tmp == NULL)
     {
-        OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
+        OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
     }
     return tmp;
 }
 
 ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
 {
-    OC_LOG(INFO, TAG, PCF("GetScheduledResourceByActionSetName Entering..."));
+    OIC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering...");
+
+    MUTEX_LOCK(&g_scheduledResourceLock);
 
-#ifndef WITH_ARDUINO
-    pthread_mutex_lock(&lock);
-#endif
     ScheduledResourceInfo *tmp = NULL;
     tmp = head;
 
@@ -186,7 +219,7 @@ ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo
         {
             if (strcmp(tmp->actionset->actionsetName, setName) == 0)
             {
-                OC_LOG(INFO, TAG, PCF("return Call INFO."));
+                OIC_LOG(INFO, TAG, "return Call INFO.");
                 goto exit;
             }
             tmp = tmp->next;
@@ -194,12 +227,12 @@ ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo
     }
 
 exit:
-#ifndef WITH_ARDUINO
-    pthread_mutex_unlock(&lock);
-#endif
+
+    MUTEX_UNLOCK(&g_scheduledResourceLock);
+
     if (tmp == NULL)
     {
-        OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
+        OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
     }
     return tmp;
 }
@@ -207,14 +240,17 @@ exit:
 void RemoveScheduledResource(ScheduledResourceInfo **head,
         ScheduledResourceInfo* del)
 {
-#ifndef WITH_ARDUINO
-    pthread_mutex_lock(&lock);
-#endif
-    OC_LOG(INFO, TAG, PCF("RemoveScheduledResource Entering..."));
+
+    MUTEX_LOCK(&g_scheduledResourceLock);
+
+    OIC_LOG(INFO, TAG, "RemoveScheduledResource Entering...");
     ScheduledResourceInfo *tmp = NULL;
 
     if (del == NULL)
     {
+
+        MUTEX_UNLOCK(&g_scheduledResourceLock);
+
         return;
     }
 
@@ -236,9 +272,8 @@ void RemoveScheduledResource(ScheduledResourceInfo **head,
     }
 
     OCFREE(del)
-#ifndef WITH_ARDUINO
-    pthread_mutex_unlock(&lock);
-#endif
+
+    MUTEX_UNLOCK(&g_scheduledResourceLock);
 }
 
 typedef struct aggregatehandleinfo
@@ -419,11 +454,13 @@ void DeleteAction(OCAction** action)
 
 void DeleteActionSet(OCActionSet** actionset)
 {
+    OCAction* pointer = NULL;
+    OCAction* pDel = NULL;
+
     if(*actionset == NULL)
         return;
 
-    OCAction* pointer = (*actionset)->head;
-    OCAction* pDel = NULL;
+    pointer = (*actionset)->head;
 
     while (pointer)
     {
@@ -488,24 +525,6 @@ OCStackResult FindAndDeleteActionSet(OCResource **resource,
     return OC_STACK_ERROR;
 }
 
-OCStackResult DeleteActionSets(OCResource** resource)
-{
-    OCActionSet *pointer = (*resource)->actionsetHead;
-    OCActionSet *pDel = pointer;
-
-    while (pointer)
-    {
-        pDel = pointer;
-        pointer = pointer->next;
-
-        DeleteActionSet(&pDel);
-        pDel->next = NULL;
-    }
-
-    (*resource)->actionsetHead = NULL;
-    return OC_STACK_OK;
-}
-
 OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
         OCActionSet** actionset)
 {
@@ -526,34 +545,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 = NULL;
 
-    iterToken = (char *) strtok_r(pRequest, ":", &iterTokenPtr);
-    VARIFY_POINTER_NULL(iterToken, result, exit);
-    length = strlen(iterToken) + 1;
+    if( NULL == ehRequest->payload )
+    {
+        result = OC_STACK_ERROR;
+        goto exit;
+    }
 
-    *key = (char *) OCMalloc(length);
-    VARIFY_POINTER_NULL(*key, result, exit)
+    OCRepPayload* input;
 
-    strncpy(*key, iterToken + 1, length);
-    ((*key)[((length - 1) - 2)]) = '\0';
+    input = (OCRepPayload*)(ehRequest->payload);
 
-    iterToken = (char *) strtok_r(NULL, "}", &iterTokenPtr);
-    VARIFY_POINTER_NULL(iterToken, result, exit);
-    length = strlen(iterToken) + 1;
+    if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
+    {
+        *key = OICStrdup(ACTIONSET);
+        VARIFY_POINTER_NULL(*key, result, exit);
+
+        *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);
 
-    *value = (char *) OCMalloc(length);
-    VARIFY_POINTER_NULL(*value, result, exit)
+        *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 = 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);
+
+        *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);
 
-    strncpy(*value, iterToken + 1, length);
-    ((*value)[((length - 1) - 2)]) = '\0';
+        *value = OICStrdup(actionSetStr);
+        VARIFY_POINTER_NULL(*value, result, exit);
+    }
+    else
+    {
+        result = OC_STACK_ERROR;
+    }
 
 exit:
     if (result != OC_STACK_OK)
@@ -562,17 +614,21 @@ exit:
         OCFREE(*value)
     }
 
+    OCFREE(actionSetStr);
+
     return result;
 }
 
 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
         long int *pa)
 {
-    char *token, *tokenPtr;
+    char *token = NULL, *tokenPtr = NULL;
     OCStackResult result = OC_STACK_OK;
 
     token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
-    *setName = (char *) OCMalloc(strlen(token) + 1);
+    VARIFY_POINTER_NULL(token, result, exit)
+
+    *setName = (char *) OICMalloc(strlen(token) + 1);
     VARIFY_POINTER_NULL(*setName, result, exit)
     VARIFY_PARAM_NULL(token, result, exit)
     strncpy(*setName, token, strlen(token) + 1);
@@ -602,16 +658,17 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
     OCAction *action = NULL;
     OCCapability *capa = NULL;
 
-    OC_LOG(INFO, TAG, PCF("Build ActionSet Instance."));
+    OIC_LOG(INFO, TAG, "Build ActionSet Instance.");
 
-    *set = (OCActionSet*) OCMalloc(sizeof(OCActionSet));
+    *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
     VARIFY_POINTER_NULL(*set, result, exit)
 
     iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
+    VARIFY_POINTER_NULL(iterToken, result, exit);
 
     // ActionSet Name
     memset(*set, 0, sizeof(OCActionSet));
-    (*set)->actionsetName = (char *) OCMalloc(strlen(iterToken) + 1);
+    (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
     VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
     VARIFY_PARAM_NULL(iterToken, result, exit)
     strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
@@ -622,16 +679,20 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
     // yyyy-mm-dd hh:mm:ss d
     iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
     VARIFY_PARAM_NULL(iterToken, result, exit)
-#ifndef WITH_ARDUINO
-    sscanf(iterToken, "%ld %d", &(*set)->timesteps, &(*set)->type);
+#if !defined(WITH_ARDUINO)
+    if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
+    {
+        // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
+        goto exit;
+    }
 #endif
 
-    OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
+    OIC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
 
     iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
     while (iterToken)
     {
-        desc = (char *) OCMalloc(strlen(iterToken) + 1);
+        desc = (char *) OICMalloc(strlen(iterToken) + 1);
         VARIFY_POINTER_NULL(desc, result, exit)
         VARIFY_PARAM_NULL(desc, result, exit)
         strncpy(desc, iterToken, strlen(iterToken) + 1);
@@ -639,33 +700,36 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
                 &descIterTokenPtr);
         while (descIterToken)
         {
-            attr = (char *) OCMalloc(strlen(descIterToken) + 1);
+            attr = (char *) OICMalloc(strlen(descIterToken) + 1);
             VARIFY_POINTER_NULL(attr, result, exit)
             VARIFY_PARAM_NULL(descIterToken, result, exit)
             strncpy(attr, descIterToken, strlen(descIterToken) + 1);
 
             attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
                     &attrIterTokenPtr);
-            key = (char *) OCMalloc(strlen(attrIterToken) + 1);
+            VARIFY_POINTER_NULL(attrIterToken, result, exit);
+
+            key = (char *) OICMalloc(strlen(attrIterToken) + 1);
             VARIFY_POINTER_NULL(key, result, exit)
             VARIFY_PARAM_NULL(attrIterToken, result, exit)
             strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
 
             attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
                     &attrIterTokenPtr);
-            value = (char *) OCMalloc(strlen(attrIterToken) + 1);
+            VARIFY_POINTER_NULL(attrIterToken, result, exit);
+            value = (char *) OICMalloc(strlen(attrIterToken) + 1);
             VARIFY_POINTER_NULL(value, result, exit)
             VARIFY_PARAM_NULL(attrIterToken, result, exit)
             strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
 
             if (strcmp(key, "uri") == 0)
             {
-                OC_LOG(INFO, TAG, PCF("Build OCAction Instance."));
+                OIC_LOG(INFO, TAG, "Build OCAction Instance.");
 
-                action = (OCAction*) OCMalloc(sizeof(OCAction));
+                action = (OCAction*) OICMalloc(sizeof(OCAction));
                 VARIFY_POINTER_NULL(action, result, exit)
                 memset(action, 0, sizeof(OCAction));
-                action->resourceUri = (char *) OCMalloc(strlen(value) + 1);
+                action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
                 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
                 VARIFY_PARAM_NULL(value, result, exit)
                 strncpy(action->resourceUri, value, strlen(value) + 1);
@@ -674,18 +738,18 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
             {
                 if ((key != NULL) && (value != NULL))
                 {
-                    OC_LOG(INFO, TAG, PCF("Build OCCapability Instance."));
+                    OIC_LOG(INFO, TAG, "Build OCCapability Instance.");
 
-                    capa = (OCCapability*) OCMalloc(sizeof(OCCapability));
+                    capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
                     VARIFY_POINTER_NULL(capa, result, exit)
                     memset(capa, 0, sizeof(OCCapability));
 
-                    capa->capability = (char *) OCMalloc(strlen(key) + 1);
+                    capa->capability = (char *) OICMalloc(strlen(key) + 1);
                     VARIFY_POINTER_NULL(capa->capability, result, exit)
                     VARIFY_PARAM_NULL(key, result, exit)
                     strncpy(capa->capability, key, strlen(key) + 1);
 
-                    capa->status = (char *) OCMalloc(strlen(value) + 1);
+                    capa->status = (char *) OICMalloc(strlen(value) + 1);
                     VARIFY_POINTER_NULL(capa->status, result, exit)
                     VARIFY_PARAM_NULL(value, result, exit)
                     strncpy(capa->status, value, strlen(value) + 1);
@@ -715,6 +779,7 @@ exit:
     OCFREE(desc)
     OCFREE(capa)
     OCFREE(action)
+    OCFREE((*set)->actionsetName)
     OCFREE(*set)
     OCFREE(key)
     OCFREE(value)
@@ -725,17 +790,24 @@ exit:
 
 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
 {
+    // Can't use the macros here as they are hardcoded to 'exit' and will
+    // result in dereferencing a null pointer.
+    if (!actionset || !desc)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
     char temp[1024] = { 0 };
-    int remaining = 1023;
+    size_t remaining = sizeof(temp) - 1;
     OCStackResult res = OC_STACK_ERROR;
+    char* actionTypeStr = NULL;
 
     OCAction *action = actionset->head;
 
     if (remaining >= strlen(actionset->actionsetName) + 1)
     {
-        strcat(temp, actionset->actionsetName);
+        strncat(temp, actionset->actionsetName, strlen(actionset->actionsetName));
         remaining -= strlen(actionset->actionsetName);
-        strcat(temp, ACTION_DELIMITER);
+        strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
         remaining--;
     }
     else
@@ -744,8 +816,39 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
         goto exit;
     }
 
+    actionTypeStr = (char *)OICMalloc(1024);
+    if(actionTypeStr != NULL)
+    {
+        sprintf(actionTypeStr, "%ld %u", actionset->timesteps, actionset->type);
+        if(remaining >= strlen(actionTypeStr) + strlen(ACTION_DELIMITER) + 1)
+        {
+            strncat(temp, actionTypeStr, strlen(actionTypeStr));
+            remaining -= strlen(actionTypeStr);
+            strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
+            remaining -= strlen(ACTION_DELIMITER);
+            OICFree(actionTypeStr);
+        }
+        else
+        {
+            OICFree(actionTypeStr);
+            res = OC_STACK_ERROR;
+            goto exit;
+        }
+    }
+    else
+    {
+        res = OC_STACK_ERROR;
+        goto exit;
+    }
+
     while (action != NULL)
     {
+        if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
+        {
+            res = OC_STACK_ERROR;
+            goto exit;
+        }
+
         strcat(temp, "uri=");
         remaining -= strlen("uri=");
         strcat(temp, action->resourceUri);
@@ -756,80 +859,97 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
         OCCapability *capas = action->head;
         while (capas != NULL)
         {
+            if (remaining < (strlen(capas->capability)
+                             + 1 + strlen(capas->status)))
+            {
+                res = OC_STACK_ERROR;
+                goto exit;
+            }
+
             strcat(temp, capas->capability);
             remaining -= strlen(capas->capability);
             strcat(temp, "=");
             remaining--;
             strcat(temp, capas->status);
-            remaining -= strlen(capas->capability);
+            remaining -= strlen(capas->status);
 
             capas = capas->next;
             if (capas != NULL)
             {
+                if (remaining < 1)
+                {
+                    res = OC_STACK_ERROR;
+                    goto exit;
+                }
                 strcat(temp, "|");
+                remaining --;
             }
         }
 
         action = action->next;
         if (action != NULL)
         {
+            if (remaining < strlen(ACTION_DELIMITER))
+            {
+                res = OC_STACK_ERROR;
+                goto exit;
+            }
             strcat(temp, ACTION_DELIMITER);
             remaining--;
         }
     }
 
-    *desc = (char *) OCMalloc(1024 - remaining);
+    *desc = OICStrdup(temp);
     VARIFY_POINTER_NULL(*desc, res, exit);
-    strcpy(*desc, temp);
 
     return OC_STACK_OK;
 
 exit:
     OCFREE(*desc);
     return res;
-
 }
 
 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
         OCClientResponse* clientResponse)
 {
-    OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
+    (void)context;
+    (void)clientResponse;
+    OIC_LOG(INFO, TAG, "Entering ActionSetCB");
 
     ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
 
     if (info)
     {
-        int idx;
+        OCEntityHandlerResponse response = { 0 };
 
-        unsigned char *responseJson;
-        responseJson = (unsigned char *) OCMalloc(
-                (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
-                        + 1));
+        response.ehResult = OC_EH_OK;
 
-        if( responseJson == NULL )
+        if(NULL == clientResponse->payload)
+        {
+            OIC_LOG(ERROR, TAG, "Error sending response");
             return OC_STACK_DELETE_TRANSACTION;
+        }
 
-        // We need the body of response.
-        // Copy the body from the response
-        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;
-        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->requestId;
+        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)
+        {
+            OIC_LOG(ERROR, TAG, "Error sending response");
+            return OC_STACK_DELETE_TRANSACTION;
+        }
 
         RemoveClientRequestInfo(&clientRequstList, info);
         OCFREE(info)
-        OCFREE(responseJson)
     }
 
     return OC_STACK_KEEP_TRANSACTION;
@@ -837,6 +957,7 @@ OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
 
 void ActionSetCD(void *context)
 {
+    (void)context;
 }
 
 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
@@ -849,7 +970,7 @@ OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
     char *jsonStr;
     uint16_t jsonLen;
 
-    OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
+    OIC_LOG(INFO, TAG, "Entering BuildActionJSON");
     json = cJSON_CreateObject();
 
     cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
@@ -867,7 +988,7 @@ OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
     jsonLen = strlen(jsonStr);
     if (jsonLen < *remaining)
     {
-        strcat((char*) bufferPtr, jsonStr);
+        strncat((char*) bufferPtr, jsonStr, jsonLen);
         *remaining -= jsonLen;
         bufferPtr += jsonLen;
         ret = OC_STACK_OK;
@@ -879,6 +1000,26 @@ OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
     return ret;
 }
 
+OCPayload* BuildActionCBOR(OCAction* action)
+{
+    OCRepPayload* payload = OCRepPayloadCreate();
+
+    if (!payload)
+    {
+        OIC_LOG(INFO, TAG, "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;
@@ -894,58 +1035,61 @@ unsigned int GetNumOfTargetResource(OCAction *actionset)
     return numOfResource;
 }
 
-
-#define DEFAULT_CONTEXT_VALUE 0x99
-
-OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
-        const unsigned char *action)
+OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
+        OCPayload *payload)
 {
-    OCCallbackData cbdata;
-    cbdata.cb = &ActionSetCB;
-    cbdata.cd = NULL;
-    cbdata.context = (void*)DEFAULT_CONTEXT_VALUE;
 
-// TODO: Selecting OC_IPV4.
-// It is temporary change as OC_ALL is not working currently. Remove this code and use OC_ALL
-// once it is functioning.
+    OCCallbackData cbData;
+    cbData.cb = &ActionSetCB;
+    cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
+    cbData.cd = NULL;
 
-    return OCDoResource(handle, OC_REST_PUT, targetUri,
-    NULL, (char *) action, OC_IPV4, OC_NA_QOS, &cbdata, NULL, 0);
+    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 *) OCMalloc(
+        ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
                 sizeof(ClientRequestInfo));
 
         if( info == NULL )
+        {
+            OCFREE(payload);
             return OC_STACK_NO_MEMORY;
+        }
 
         memset(info, 0, sizeof(ClientRequestInfo));
 
         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);
             return result;
         }
 
@@ -959,53 +1103,51 @@ OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
 
 void DoScheduledGroupAction()
 {
-    OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
-    ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
+    OIC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
+    ScheduledResourceInfo* info = GetScheduledResource(g_scheduleResourceList);
 
     if (info == NULL)
     {
-        OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
+        OIC_LOG(INFO, TAG, "Target resource is NULL");
         goto exit;
     }
     else if (info->resource == NULL)
     {
-        OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
+        OIC_LOG(INFO, TAG, "Target resource is NULL");
         goto exit;
     }
     else if (info->actionset == NULL)
     {
-        OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
+        OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
         goto exit;
     }
     else if (info->ehRequest == NULL)
     {
-        OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
+        OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
         goto exit;
     }
-#ifndef WITH_ARDUINO
-    pthread_mutex_lock(&lock);
-#endif
+
+    MUTEX_LOCK(&g_scheduledResourceLock);
+
     DoAction(info->resource, info->actionset, info->ehRequest);
-#ifndef WITH_ARDUINO
-    pthread_mutex_unlock(&lock);
-#endif
+
+    MUTEX_UNLOCK(&g_scheduledResourceLock);
+
 
     if (info->actionset->type == RECURSIVE)
     {
         ScheduledResourceInfo *schedule;
-        schedule = (ScheduledResourceInfo *) OCMalloc(
+        schedule = (ScheduledResourceInfo *) OICMalloc(
                 sizeof(ScheduledResourceInfo));
 
         if (schedule)
         {
-            OC_LOG(INFO, TAG, PCF("Building New Call Info."));
+            OIC_LOG(INFO, TAG, "Building New Call Info.");
             memset(schedule, 0, sizeof(ScheduledResourceInfo));
 
             if (info->actionset->timesteps > 0)
             {
-#ifndef WITH_ARDUINO
-                pthread_mutex_lock(&lock);
-#endif
+                MUTEX_LOCK(&g_scheduledResourceLock);
                 schedule->resource = info->resource;
                 schedule->actionset = info->actionset;
                 schedule->ehRequest = info->ehRequest;
@@ -1014,298 +1156,333 @@ void DoScheduledGroupAction()
                         &schedule->timer_id,
                         &DoScheduledGroupAction);
 
-                OC_LOG(INFO, TAG, PCF("Reregisteration."));
-#ifndef WITH_ARDUINO
-                pthread_mutex_unlock(&lock);
-#endif
-                AddScheduledResource(&scheduleResourceList, schedule);
+                OIC_LOG(INFO, TAG, "Reregistration.");
+                MUTEX_UNLOCK(&g_scheduledResourceLock);
+                AddScheduledResource(&g_scheduleResourceList, schedule);
+            }
+            else
+            {
+                OICFree(schedule);
             }
         }
     }
 
-    RemoveScheduledResource(&scheduleResourceList, info);
+    RemoveScheduledResource(&g_scheduleResourceList, info);
 
     exit:
 
     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 };
-        unsigned char *bufferPtr = NULL;
-
-        bufferPtr = buffer;
+    OIC_LOG(INFO, TAG, "Group Action is requested.");
 
-        OCResource * collResource = (OCResource *) ehRequest->resource;
+    char *doWhat = NULL;
+    char *details = NULL;
 
-        char *jsonResponse;
+    stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
 
-        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)
+    {
+        OIC_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)
+    {
+        OIC_LOG(INFO, TAG, "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());
+            OCActionSet *actionSet = NULL;
+            stackRet = BuildActionSetFromString(&actionSet, details);
 
-            OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
-
-            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 );
                         }
+                        OIC_LOG(INFO, TAG, "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();
 
-            strcat((char *) bufferPtr, jsonResponse);
+        if(!payload)
+        {
+            OIC_LOG(ERROR, TAG, "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;
-                response.payload = (char*)buffer;
-                response.payloadSize = bufferLength + 1;
-                response.persistentBufferFlag = 0;
-                response.requestHandle =
-                        (OCRequestHandle) ehRequest->requestHandle;
-                response.resourceHandle = (OCResourceHandle) collResource;
-                stackRet = OCDoResponse(&response);
+                OIC_LOG(ERROR, TAG, "Error sending response");
+                stackRet = OC_STACK_ERROR;
             }
         }
+        OCRepPayloadDestroy(payload);
+    }
+    else if (method == OC_REST_POST)
+    {
+        OCActionSet *actionset = NULL;
+
+        OCRepPayload* payload = OCRepPayloadCreate();
+        OCRepPayloadSetUri(payload, resource->uri);
 
-        if (method == OC_REST_POST)
+        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;
+                    OIC_LOG(INFO, TAG, "ERROR");
+                    stackRet = OC_STACK_ERROR;
                 }
-                else
+
+                if (actionset == NULL)
                 {
-                    stackRet = OC_STACK_OK;
+                    OIC_LOG(INFO, TAG, "Cannot Find ActionSet");
+                    stackRet = OC_STACK_ERROR;
                 }
-
-                if (stackRet == OC_STACK_OK)
+                else
                 {
-                    if (GetActionSet(details, resource->actionsetHead,
-                            &actionset) != OC_STACK_OK)
+                    OIC_LOG(INFO, TAG, "Group Action[POST].");
+                    OCServerRequest *request =
+                            GetServerRequestUsingHandle(ehRequest->requestHandle);
+                    if (NULL == request)
                     {
-                        OC_LOG(INFO, TAG, PCF("ERROR"));
                         stackRet = OC_STACK_ERROR;
+                        goto exit;
                     }
 
-                    if (actionset == NULL)
+                    if (actionset->type == NONE)
                     {
-                        OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
-                        stackRet = OC_STACK_ERROR;
+                        OIC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
+                                actionset->actionsetName);
+                        unsigned int num = GetNumOfTargetResource(
+                                actionset->head);
+
+                        request->ehResponseHandler = HandleAggregateResponse;
+                        request->numResponses = num + 1;
+
+                        DoAction(resource, actionset, request);
+
+                        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);
+                        OIC_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 *) OCMalloc(
+                        ScheduledResourceInfo *schedule;
+                        schedule = (ScheduledResourceInfo *) OICMalloc(
+                                sizeof(ScheduledResourceInfo));
+
+                        if (schedule)
+                        {
+                            OIC_LOG(INFO, TAG, "Building New Call Info.");
+                            memset(schedule, 0,
                                     sizeof(ScheduledResourceInfo));
+                            MUTEX_LOCK(&g_scheduledResourceLock);
+                            schedule->resource = resource;
+                            schedule->actionset = actionset;
+                            schedule->ehRequest = request;
 
-                            if (schedule)
+                            MUTEX_UNLOCK(&g_scheduledResourceLock);
+                            if (delay > 0)
+                            {
+                                OIC_LOG_V(INFO, TAG, "delay_time is %ld seconds.",
+                                        actionset->timesteps);
+                                MUTEX_LOCK(&g_scheduledResourceLock);
+                                schedule->time = registerTimer(delay,
+                                        &schedule->timer_id,
+                                        &DoScheduledGroupAction);
+                                MUTEX_UNLOCK(&g_scheduledResourceLock);
+                                AddScheduledResource(&g_scheduleResourceList,
+                                        schedule);
+                                stackRet = OC_STACK_OK;
+                            }
+                            else
                             {
-                                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;
-                                }
+                                stackRet = OC_STACK_ERROR;
                             }
                         }
                     }
                 }
             }
-            else if (strcmp(doWhat, "CancelAction") == 0)
-            {
-                ScheduledResourceInfo *info =
-                        GetScheduledResourceByActionSetName(scheduleResourceList, details);
+        }
+        else if (strcmp(doWhat, "CancelAction") == 0)
+        {
+            ScheduledResourceInfo *info =
+                    GetScheduledResourceByActionSetName(g_scheduleResourceList, details);
 
-                if(info != NULL)
-                {
-                    unregisterTimer(info->timer_id);
+            if(info != NULL)
+            {
+                MUTEX_LOCK(&g_scheduledResourceLock);
+                unregisterTimer(info->timer_id);
+                MUTEX_UNLOCK(&g_scheduledResourceLock);
 
-                    RemoveScheduledResource(&scheduleResourceList, info);
-                    stackRet = OC_STACK_OK;
-                }
-                else
-                {
-                    stackRet = OC_STACK_ERROR;
-                }
+                RemoveScheduledResource(&g_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);
-                    }
-                    OCFree(plainText);
-                    stackRet = OC_STACK_OK;
+                    OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
                 }
+                OICFree(plainText);
+                stackRet = OC_STACK_OK;
             }
+        }
 
-
-            jsonResponse = cJSON_Print(json);
-            cJSON_Delete(json);
-
-            strcat((char *) bufferPtr, jsonResponse);
-
-            bufferLength = strlen((const char *) buffer);
-            if (bufferLength > 0)
+        if(!payload)
+        {
+            OIC_LOG(ERROR, TAG, "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;
-                response.payload = (char *)buffer;
-                response.payloadSize = bufferLength + 1;
-                response.persistentBufferFlag = 0;
-                response.requestHandle =
-                        (OCRequestHandle) ehRequest->requestHandle;
-                response.resourceHandle = (OCResourceHandle) collResource;
-                stackRet = OCDoResponse(&response);
+                OIC_LOG(ERROR, TAG, "Error sending response");
+                stackRet = OC_STACK_ERROR;
             }
         }
-
-        OCFREE(doWhat)
-        OCFREE(details)
+        OCRepPayloadDestroy(payload);
     }
 
+exit:
+
+    OCFREE(doWhat)
+    OCFREE(details)
+
     return stackRet;
 }
+
+OCStackResult InitializeScheduleResourceList()
+{
+    MUTEX_INITIALIZE(&g_scheduledResourceLock);
+
+    g_scheduleResourceList = NULL;
+    return OC_STACK_OK;
+}
+
+void TerminateScheduleResourceList()
+{
+    assert(g_scheduleResourceList == NULL);
+
+    MUTEX_TERMINATE(&g_scheduledResourceLock);
+}