Fix merge issues, Group action feature omit. 11/711/3
authorHyunJun Kim <hyunjun2.kim@samsung.com>
Tue, 14 Apr 2015 05:00:30 +0000 (14:00 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 14 Apr 2015 10:28:56 +0000 (10:28 +0000)
Scheduled / Recursive Group Action feature was omitted
when CA and Master were Merged.

Change-Id: I0a850eef946fdc3612455fb57552e84991868bc8
Signed-off-by: HyunJun Kim <hyunjun2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/711
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
resource/csdk/stack/include/internal/oicgroup.h
resource/csdk/stack/src/oicgroup.c
service/things-manager/sampleapp/linux/groupaction/groupserver.cpp
service/things-manager/sdk/src/GroupManager.cpp
service/things-manager/sdk/src/GroupSynchronization.cpp

index eb7b551..3b0c784 100644 (file)
@@ -21,8 +21,9 @@
 #ifndef OIC_GROUP_H
 #define OIC_GROUP_H
 
-#include "ocstack.h"
 #include "ocstackinternal.h"
+#include "ocstack.h"
+#include "ocresource.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -32,7 +33,7 @@ void AddCapability(OCCapability** head, OCCapability* node);
 
 void AddAction(OCAction** head, OCAction* node);
 
-void AddActionSet(OCActionSet **head, OCActionSet* node);
+OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node);
 
 void DeleteCapability(OCCapability *del);
 
index 946b094..45522ed 100755 (executable)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-// Defining _POSIX_C_SOURCE macro with 200112L (or greater) as value
-// causes header files to expose definitions
-// corresponding to the POSIX.1-2001 base
-// specification (excluding the XSI extension).
-// For POSIX.1-2001 base specification,
-// Refer http://pubs.opengroup.org/onlinepubs/009695399/
 #define _POSIX_C_SOURCE 200112L
+
 #include <string.h>
 
+#include "oicgroup.h"
 #include "cJSON.h"
 #include "ocmalloc.h"
-#include "oicgroup.h"
-#include "ocresource.h"
 #include "occollection.h"
 #include "logger.h"
+#include "timer.h"
+
+#ifndef WITH_ARDUINO
+#include <pthread.h>
+#endif
 
 #define TAG PCF("OICGROUP")
 
 #define ACTIONSET               "ActionSet"
 #define DELETE_ACTIONSET        "DelActionSet"
 
+#define OIC_ACTION_PREFIX               "{\"oc\":[{\"rep\":{"
+#define VARIFY_POINTER_NULL(pointer, result, toExit) \
+    if(pointer == NULL) \
+    {\
+        result = OC_STACK_NO_MEMORY;\
+        goto toExit;\
+    }
+#define VARIFY_PARAM_NULL(pointer, result, toExit) \
+    if(pointer == NULL)\
+    {\
+        result = OC_STACK_INVALID_PARAM;\
+        goto exit;\
+    }
+
+#define OCFREE(pointer) \
+    { \
+        OCFree(pointer); \
+        pointer = NULL; \
+    }
+
+#ifndef WITH_ARDUINO
+pthread_mutex_t lock;
+#endif
+
+enum ACTION_TYPE
+{
+    NONE = 0, SCHEDULED, RECURSIVE
+};
+
+typedef struct scheduledresourceinfo
+{
+    OCResource *resource;
+    OCActionSet *actionset;
+
+    int timer_id;
+
+    OCServerRequest *ehRequest;
+
+    time_t time;
+    struct scheduledresourceinfo* next;
+} ScheduledResourceInfo;
+
+ScheduledResourceInfo *scheduleResourceList = NULL;
+
+void AddScheduledResource(ScheduledResourceInfo **head,
+        ScheduledResourceInfo* add)
+{
+    OC_LOG(INFO, TAG, PCF("AddScheduledResource Entering..."));
+
+#ifndef WITH_ARDUINO
+    pthread_mutex_lock(&lock);
+#endif
+    ScheduledResourceInfo *tmp = NULL;
+
+    if (*head != NULL)
+    {
+        tmp = *head;
+
+        while (tmp->next)
+        {
+            tmp = tmp->next;
+        }
+        tmp->next = add;
+    }
+    else
+    {
+        *head = add;
+    }
+#ifndef WITH_ARDUINO
+    pthread_mutex_unlock(&lock);
+#endif
+}
+
+ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
+{
+    OC_LOG(INFO, TAG, PCF("GetScheduledResource Entering..."));
+
+#ifndef WITH_ARDUINO
+    pthread_mutex_lock(&lock);
+#endif
+
+    time_t t_now;
+
+    ScheduledResourceInfo *tmp = NULL;
+    tmp = head;
+
+#ifndef WITH_ARDUINO
+    time(&t_now);
+#else
+    t_now = now();
+#endif
+
+    if (tmp)
+    {
+        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."));
+                goto exit;
+            }
+
+            tmp = tmp->next;
+        }
+    }
+
+    exit:
+#ifndef WITH_ARDUINO
+    pthread_mutex_unlock(&lock);
+#endif
+    if (tmp == NULL)
+    {
+        OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
+    }
+    return tmp;
+}
+
+ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
+{
+    OC_LOG(INFO, TAG, PCF("GetScheduledResourceByActionSetName Entering..."));
+
+#ifndef WITH_ARDUINO
+    pthread_mutex_lock(&lock);
+#endif
+    ScheduledResourceInfo *tmp = NULL;
+    tmp = head;
+
+    if (tmp)
+    {
+        while (tmp)
+        {
+            if (strcmp(tmp->actionset->actionsetName, setName) == 0)
+            {
+                OC_LOG(INFO, TAG, PCF("return Call INFO."));
+                goto exit;
+            }
+            tmp = tmp->next;
+        }
+    }
+
+exit:
+#ifndef WITH_ARDUINO
+    pthread_mutex_unlock(&lock);
+#endif
+    if (tmp == NULL)
+    {
+        OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
+    }
+    return tmp;
+}
+
+void RemoveScheduledResource(ScheduledResourceInfo **head,
+        ScheduledResourceInfo* del)
+{
+#ifndef WITH_ARDUINO
+    pthread_mutex_lock(&lock);
+#endif
+    OC_LOG(INFO, TAG, PCF("RemoveScheduledResource Entering..."));
+    ScheduledResourceInfo *tmp = NULL;
+
+    if (del == NULL)
+    {
+        return;
+    }
+
+    if (*head == del)
+    {
+        *head = (*head)->next;
+    }
+    else
+    {
+        tmp = *head;
+        while (tmp->next && (tmp->next != del))
+        {
+            tmp = tmp->next;
+        }
+        if (tmp->next)
+        {
+            tmp->next = del->next;
+        }
+    }
+
+    OCFREE(del)
+#ifndef WITH_ARDUINO
+    pthread_mutex_unlock(&lock);
+#endif
+}
+
 typedef struct aggregatehandleinfo
 {
     OCServerRequest *ehRequest;
@@ -56,7 +250,7 @@ typedef struct aggregatehandleinfo
     struct aggregatehandleinfo *next;
 } ClientRequestInfo;
 
-static ClientRequestInfo *clientRequestList = NULL;
+ClientRequestInfo *clientRequstList = NULL;
 
 void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
 {
@@ -78,7 +272,8 @@ void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
     }
 }
 
-ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head, OCDoHandle handle)
+ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head,
+        OCDoHandle handle)
 {
     ClientRequestInfo *tmp = NULL;
 
@@ -105,6 +300,9 @@ void RemoveClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* del)
 {
     ClientRequestInfo *tmp = NULL;
 
+    if (del == NULL)
+        return;
+
     if (*head == del)
     {
         *head = (*head)->next;
@@ -160,31 +358,46 @@ void AddAction(OCAction** head, OCAction* node)
     }
 }
 
-void AddActionSet(OCActionSet **head, OCActionSet* node)
+OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node)
 {
     OCActionSet *pointer = *head;
+    OCActionSet *prev = NULL;
+    if(node == NULL)
+    {
+        return OC_STACK_ERROR;
+    }
     if (NULL == pointer)
     {
         *head = node;
     }
     else
     {
-
-        while (pointer->next != NULL)
+        prev = pointer;
+        while (pointer != NULL)
         {
+            // check the uniqeness of actionsetname.
+            if (strcmp(pointer->actionsetName, node->actionsetName) == 0)
+            {
+                return OC_STACK_ERROR;
+            }
+
+            prev = pointer;
             pointer = pointer->next;
         }
 
-        pointer->next = node;
+        prev->next = node;
     }
+
+    return OC_STACK_OK;
 }
 
 void DeleteCapability(OCCapability *del)
 {
-    OCFree(del->capability);
+    OCFREE(del->capability)
     del->capability = NULL;
-    OCFree(del->status);
+    OCFREE(del->status)
     del->status = NULL;
+    OCFREE(del)
 }
 
 void DeleteAction(OCAction** action)
@@ -198,15 +411,17 @@ void DeleteAction(OCAction** action)
         pointer = pointer->next;
 
         DeleteCapability(pDel);
-        pDel->next = NULL;
     }
-    OCFree((*action)->resourceUri);
-    (*action)->resourceUri = NULL;
+    OCFREE((*action)->resourceUri)
     (*action)->next = NULL;
+    OCFREE(*action)
 }
 
 void DeleteActionSet(OCActionSet** actionset)
 {
+    if(*actionset == NULL)
+        return;
+
     OCAction* pointer = (*actionset)->head;
     OCAction* pDel = NULL;
 
@@ -216,14 +431,14 @@ void DeleteActionSet(OCActionSet** actionset)
         pointer = pointer->next;
 
         DeleteAction(&pDel);
-        pDel->next = NULL;
     }
-
-    OCFree((*actionset)->actionsetName);
-    (*actionset)->head = NULL;
+    //    (*actionset)->head = NULL;
+    OCFREE((*actionset)->actionsetName)
+    OCFREE(*actionset)
 }
 
-OCStackResult FindAndDeleteActionSet(OCResource **resource, const char * actionsetName)
+OCStackResult FindAndDeleteActionSet(OCResource **resource,
+        const char * actionsetName)
 {
     if (*resource != NULL)
     {
@@ -244,6 +459,7 @@ OCStackResult FindAndDeleteActionSet(OCResource **resource, const char * actions
                     (*resource)->actionsetHead = pointer->next;
                 else
                     (*resource)->actionsetHead = NULL;
+
                 DeleteActionSet(&pointer);
             }
             else if (pointer->next != NULL)
@@ -252,7 +468,8 @@ OCStackResult FindAndDeleteActionSet(OCResource **resource, const char * actions
                 {
                     if (pointer->next != NULL)
                     {
-                        if (strcmp(pointer->next->actionsetName, actionsetName) == 0)
+                        if (strcmp(pointer->next->actionsetName, actionsetName)
+                                == 0)
                         {
                             pDel = pointer->next;
                             pointer->next = pointer->next->next;
@@ -289,7 +506,8 @@ OCStackResult DeleteActionSets(OCResource** resource)
     return OC_STACK_OK;
 }
 
-OCStackResult GetActionSet(const char *actionName, OCActionSet *head, OCActionSet** actionset)
+OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
+        OCActionSet** actionset)
 {
     OCActionSet *pointer = head;
 
@@ -308,77 +526,76 @@ OCStackResult GetActionSet(const char *actionName, OCActionSet *head, OCActionSe
 
 }
 
-
-#define OIC_ACTION_PREFIX               "{\"oc\":[{\"rep\":{"
-#define VARIFY_POINTER_NULL(pointer, result, toExit) \
-    if(pointer == NULL) \
-    {\
-        result = OC_STACK_NO_MEMORY;\
-        goto toExit;\
-    }
-#define VARIFY_PARAM_NULL(pointer, result, toExit) \
-    if(pointer == NULL)\
-    {\
-        result = OC_STACK_INVALID_PARAM;\
-        goto exit;\
-    }
-
-OCStackResult ExtractKeyValueFromRequest(char *request, char **key, char **value)
+OCStackResult ExtractKeyValueFromRequest(char *request, char **key,
+        char **value)
 {
     OCStackResult result = OC_STACK_OK;
     size_t length = 0;
 
-    if(strlen(request) <= strlen(OIC_ACTION_PREFIX))
-    {
-        return OC_STACK_INVALID_PARAM;
-    }
-
-    char* pRequest = (char *)request + strlen(OIC_ACTION_PREFIX);
-    char* iterToken = NULL;
-    char* iterTokenPtr = NULL;
+    char* pRequest = (char *) request + strlen(OIC_ACTION_PREFIX);
+    char* iterToken, *iterTokenPtr;
 
     iterToken = (char *) strtok_r(pRequest, ":", &iterTokenPtr);
     length = strlen(iterToken) + 1;
 
-    *key = (char *)OCMalloc(length);
-    VARIFY_POINTER_NULL(*key, result, exit);
+    *key = (char *) OCMalloc(length);
+    VARIFY_POINTER_NULL(*key, result, exit)
 
     strncpy(*key, iterToken + 1, length);
-    ((*key)[ (( length - 1 ) - 2) ]) = '\0';
+    ((*key)[((length - 1) - 2)]) = '\0';
 
     iterToken = (char *) strtok_r(NULL, "}", &iterTokenPtr);
     length = strlen(iterToken) + 1;
 
-    *value = (char *)OCMalloc(length);
-    VARIFY_POINTER_NULL(*key, result, exit);
+    *value = (char *) OCMalloc(length);
+    VARIFY_POINTER_NULL(*value, result, exit)
 
     strncpy(*value, iterToken + 1, length);
-    ((*value)[ (( length - 1 ) - 2) ]) = '\0';
+    ((*value)[((length - 1) - 2)]) = '\0';
+
 exit:
-    if(result != OC_STACK_OK)
+    if (result != OC_STACK_OK)
     {
-        OCFree(*key);
-        OCFree(*value);
-        *key = NULL;
-        *value = NULL;
+        OCFREE(*key)
+        OCFREE(*value)
     }
 
     return result;
 }
 
+OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
+        long int *pa)
+{
+    char *token, *tokenPtr;
+    OCStackResult result = OC_STACK_OK;
+
+    token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
+    *setName = (char *) OCMalloc(strlen(token) + 1);
+    VARIFY_POINTER_NULL(*setName, result, exit)
+    VARIFY_PARAM_NULL(token, result, exit)
+    strncpy(*setName, token, strlen(token) + 1);
+
+    token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
+    VARIFY_POINTER_NULL(pa, result, exit)
+    VARIFY_PARAM_NULL(token, result, exit)
+    *pa = atoi(token);
+
+    return OC_STACK_OK;
+
+exit:
+    OCFREE(*setName);
+    return result;
+}
+
 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
 {
     OCStackResult result = OC_STACK_OK;
 
     char *iterToken = NULL, *iterTokenPtr = NULL;
-    char *descIterToken = NULL;
-    char *descIterTokenPtr = NULL;
-    char *attrIterToken = NULL;
-    char *attrIterTokenPtr = NULL;
-    char *desc = NULL;
-    char *attr = NULL;
-    char *key = NULL;
-    char *value = NULL;
+    char *descIterToken = NULL, *descIterTokenPtr = NULL;
+    char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
+    char *desc = NULL, *attr = NULL;
+    char *key = NULL, *value = NULL;
 
     OCAction *action = NULL;
     OCCapability *capa = NULL;
@@ -390,62 +607,85 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
 
     iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
 
+    // ActionSet Name
     memset(*set, 0, sizeof(OCActionSet));
-    (*set)->actionsetName = (char *)OCMalloc(sizeof(OCActionSet));
+    (*set)->actionsetName = (char *) OCMalloc(strlen(iterToken) + 1);
     VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
     VARIFY_PARAM_NULL(iterToken, result, exit)
     strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
 
+    // Time info. for Scheduled/Recursive Group action.
+    // d is meant Day of the week.
+    // T is meant ActionType.
+    // 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);
+#endif
+
     OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
 
     iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
-    while(iterToken)
+    while (iterToken)
     {
-        desc = (char *)OCMalloc(strlen(iterToken) + 1);
+        desc = (char *) OCMalloc(strlen(iterToken) + 1);
+        VARIFY_POINTER_NULL(desc, result, exit)
+        VARIFY_PARAM_NULL(desc, result, exit)
         strncpy(desc, iterToken, strlen(iterToken) + 1);
-        descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER, &descIterTokenPtr);
-        while(descIterToken)
+        descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
+                &descIterTokenPtr);
+        while (descIterToken)
         {
-            attr = (char *)OCMalloc(strlen(descIterToken) + 1);
+            attr = (char *) OCMalloc(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);
+            attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
+                    &attrIterTokenPtr);
+            key = (char *) OCMalloc(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);
+            attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
+                    &attrIterTokenPtr);
+            value = (char *) OCMalloc(strlen(attrIterToken) + 1);
             VARIFY_POINTER_NULL(value, result, exit)
             VARIFY_PARAM_NULL(attrIterToken, result, exit)
             strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
 
-            if(strncmp(key, "uri", sizeof("uri") - 1) == 0)
+            if (strcmp(key, "uri") == 0)
             {
                 OC_LOG(INFO, TAG, PCF("Build OCAction Instance."));
 
-                action = (OCAction*)OCMalloc(sizeof(OCAction));
+                action = (OCAction*) OCMalloc(sizeof(OCAction));
                 VARIFY_POINTER_NULL(action, result, exit)
                 memset(action, 0, sizeof(OCAction));
-                action->resourceUri = (char *)OCMalloc(strlen(value) + 1);
+                action->resourceUri = (char *) OCMalloc(strlen(value) + 1);
                 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
                 VARIFY_PARAM_NULL(value, result, exit)
                 strncpy(action->resourceUri, value, strlen(value) + 1);
             }
             else
             {
-                if(key != NULL) && (value != NULL))
+                if ((key != NULL) && (value != NULL))
                 {
                     OC_LOG(INFO, TAG, PCF("Build OCCapability Instance."));
-                    capa = (OCCapability*)OCMalloc(sizeof(OCCapability));
+
+                    capa = (OCCapability*) OCMalloc(sizeof(OCCapability));
                     VARIFY_POINTER_NULL(capa, result, exit)
                     memset(capa, 0, sizeof(OCCapability));
-                    capa->capability = (char *)OCMalloc(strlen(key) + 1);
-                    capa->status = (char *)OCMalloc(strlen(value) + 1);
 
+                    capa->capability = (char *) OCMalloc(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);
+                    VARIFY_POINTER_NULL(capa->status, result, exit)
+                    VARIFY_PARAM_NULL(value, result, exit)
                     strncpy(capa->status, value, strlen(value) + 1);
 
                     VARIFY_POINTER_NULL(action, result, exit)
@@ -454,42 +694,46 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
                 }
             }
 
-            OCFree(key);
-            OCFree(value);
-            OCFree(attr);
+            OCFREE(key)
+            OCFREE(value)
+            OCFREE(attr)
 
-            descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER, &descIterTokenPtr);
+            descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
+                    &descIterTokenPtr);
         }
 
         AddAction(&(*set)->head, action);
         iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
-        OCFree(desc);
+        OCFREE(desc);
     }
 
     return OC_STACK_OK;
 exit:
-    OCFree(attr);
-    OCFree(desc);
-    OCFree(capa);
-    OCFree(action);
-    OCFree(*set);
-    *set = NULL;
+    OCFREE(attr)
+    OCFREE(desc)
+    OCFREE(capa)
+    OCFREE(action)
+    OCFREE(*set)
+    OCFREE(key)
+    OCFREE(value)
+    OCFREE(attr)
+
     return result;
 }
 
 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
 {
     char temp[1024] = { 0 };
-    uint16_t remaining = sizeof(temp) - 1;
+    int remaining = 1023;
 
     OCAction *action = actionset->head;
 
-    if (remaining >= strlen(actionset->actionsetName) + (sizeof(ACTION_DELIMITER)-1) )
+    if (remaining >= strlen(actionset->actionsetName) + 1)
     {
-        strncat(temp, actionset->actionsetName, remaining);
+        strcat(temp, actionset->actionsetName);
         remaining -= strlen(actionset->actionsetName);
         strcat(temp, ACTION_DELIMITER);
-        remaining -= (sizeof(ACTION_DELIMITER)-1);
+        remaining--;
     }
     else
     {
@@ -498,21 +742,19 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
 
     while (action != NULL)
     {
-        OC_LOG_V(INFO, TAG, "\tURI :: %s\n", action->resourceUri);
         strcat(temp, "uri=");
         remaining -= strlen("uri=");
         strcat(temp, action->resourceUri);
         remaining -= strlen(action->resourceUri);
-        strcat(temp, ATTR_DELIMITER);
+        strcat(temp, "|");
         remaining--;
 
         OCCapability *capas = action->head;
         while (capas != NULL)
         {
-            OC_LOG_V(INFO, TAG, "\t\t%s = %s\n", capas->capability, capas->status);
             strcat(temp, capas->capability);
             remaining -= strlen(capas->capability);
-            strcat(temp, ATTR_ASSIGN);
+            strcat(temp, "=");
             remaining--;
             strcat(temp, capas->status);
             remaining -= strlen(capas->capability);
@@ -520,7 +762,7 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
             capas = capas->next;
             if (capas != NULL)
             {
-                strcat(temp, ATTR_DELIMITER);
+                strcat(temp, "|");
             }
         }
 
@@ -533,11 +775,6 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
     }
 
     *desc = (char *) OCMalloc(1024 - remaining);
-    if(!*desc)
-    {
-        return OC_STACK_NO_MEMORY;
-    }
-
     strcpy(*desc, temp);
 
     return OC_STACK_OK;
@@ -546,28 +783,30 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
         OCClientResponse* clientResponse)
 {
-    OC_LOG(INFO, TAG, PCF("\n\n\tcallback is called\n\n"));
+    OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
 
-    ClientRequestInfo *info = GetClientRequestInfo(clientRequestList, handle);
+    ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
 
     if (info)
     {
-        uint32_t idx = 0;
+        int idx;
 
-        char *responseJson = (char *) OCMalloc(
-                (unsigned int) (strlen( clientResponse->resJSONPayload) + 1));
+        unsigned char *responseJson;
+        responseJson = (unsigned char *) OCMalloc(
+                (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
+                        + 1));
 
         // We need the body of response.
         // Copy the body from the response
-        strncpy((char *) responseJson, (clientResponse->resJSONPayload
-                + OC_JSON_PREFIX_LEN), strlen(clientResponse->resJSONPayload) + 1);
+        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 = { };
+        OCEntityHandlerResponse response = { };
         response.ehResult = OC_EH_OK;
-        response.payload = responseJson;
+        response.payload = (char*)responseJson;
         response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
         response.persistentBufferFlag = 0;
         response.requestHandle = (OCRequestHandle) info->ehRequest;
@@ -575,8 +814,9 @@ OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
 
         OCDoResponse(&response);
 
-        RemoveClientRequestInfo(&clientRequestList, info);
-        OCFree(responseJson);
+        RemoveClientRequestInfo(&clientRequstList, info);
+        OCFREE(info)
+        OCFREE(responseJson)
     }
 
     return OC_STACK_KEEP_TRANSACTION;
@@ -586,24 +826,26 @@ void ActionSetCD(void *context)
 {
 }
 
-OCStackResult BuildActionJSON(OCAction* action, char* bufferPtr, uint16_t *remaining)
+OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
+        uint16_t *remaining)
 {
     OCStackResult ret = OC_STACK_ERROR;
-    cJSON *json = NULL;
-    cJSON *body = NULL;
+    cJSON *json;
+    cJSON *body;
 
-    char *jsonStr = NULL;
-    uint16_t jsonLen = 0;
+    char *jsonStr;
+    uint16_t jsonLen;
 
     OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
     json = cJSON_CreateObject();
 
-    cJSON_AddItemToObject(json, OC_RSRVD_REPRESENTATION, body = cJSON_CreateObject());
+    cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
 
     OCCapability* pointerCapa = action->head;
     while (pointerCapa)
     {
-        cJSON_AddStringToObject(body, pointerCapa->capability, pointerCapa->status);
+        cJSON_AddStringToObject(body, pointerCapa->capability,
+                pointerCapa->status);
         pointerCapa = pointerCapa->next;
     }
 
@@ -619,14 +861,14 @@ OCStackResult BuildActionJSON(OCAction* action, char* bufferPtr, uint16_t *remai
     }
 
     cJSON_Delete(json);
-    OCFree(jsonStr);
+    free(jsonStr);
 
     return ret;
 }
 
-uint32_t GetNumOfTargetResource(OCAction *actionset)
+unsigned int GetNumOfTargetResource(OCAction *actionset)
 {
-    uint32_t numOfResource = 0;
+    int numOfResource = 0;
 
     OCAction *pointerAction = actionset;
 
@@ -639,210 +881,402 @@ uint32_t GetNumOfTargetResource(OCAction *actionset)
     return numOfResource;
 }
 
-OCStackResult SendAction(OCDoHandle *handle, const char *targetUri, const char *action)
+
+#define DEFAULT_CONTEXT_VALUE 0x99
+
+OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
+        const unsigned char *action)
 {
-    OCCallbackData cbdata = {  };
+    OCCallbackData cbdata = { 0 };
     cbdata.cb = &ActionSetCB;
-    cbdata.cd = &ActionSetCD;
-    cbdata.context = (void *) 0x99;
+    cbdata.cd = NULL;
+    cbdata.context = (void*)DEFAULT_CONTEXT_VALUE;
 
     return OCDoResource(handle, OC_REST_PUT, targetUri,
-    //temp->rsrcType->resourcetypename,
-            NULL, (char *) action, OC_WIFI, OC_NA_QOS, &cbdata, NULL, 0);
+    NULL, (char *) action, OC_ETHERNET, OC_NA_QOS, &cbdata, NULL, 0);
 }
 
-OCStackResult BuildCollectionGroupActionJSONResponse(OCMethod method/*OCEntityHandlerFlag flag*/,
-        OCResource *resource, OCEntityHandlerRequest *ehRequest)
+OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
+        OCServerRequest* requestHandle)
 {
-    OCStackResult stackRet = OC_STACK_ERROR;
-
-    OC_LOG(INFO, TAG, PCF("Group Action is requested."));
-    char *doWhat = NULL;
-    char *details = NULL;
+    OCStackResult result = OC_STACK_ERROR;
+    OCAction *pointerAction = actionset->head;
 
-    size_t bufferLength = 0;
-    char buffer[MAX_RESPONSE_LENGTH] = { 0 };
-    char *bufferPtr = NULL;
+    while (pointerAction != NULL)
+    {
+        unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
+        unsigned char* actionDescPtr = actionDesc;
+        uint16_t remaining = MAX_RESPONSE_LENGTH;
+
+        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));
+
+        ClientRequestInfo *info = (ClientRequestInfo *) OCMalloc(
+                sizeof(ClientRequestInfo));
+        memset(info, 0, sizeof(ClientRequestInfo));
+
+        info->collResource = resource;
+        info->ehRequest = requestHandle;
+
+        result = SendAction(&info->required, pointerAction->resourceUri,
+                actionDescPtr);
+        if (result != OC_STACK_OK)
+        {
+            return result;
+        }
 
-    bufferPtr = buffer;
+        AddClientRequestInfo(&clientRequstList, info);
 
-    OCResource * collResource = (OCResource *) ehRequest->resource;
+        pointerAction = pointerAction->next;
+    }
 
-    char *jsonResponse;
+    return result;
+}
 
-    stackRet = ExtractKeyValueFromRequest((char *)ehRequest->reqJSONPayload, &doWhat, &details);
+void DoScheduledGroupAction()
+{
+    OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
+    ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
 
-    if (stackRet != OC_STACK_OK)
+    if (info == NULL)
     {
-        return stackRet;
+        OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
+        goto exit;
     }
-
-    cJSON *json;
-    cJSON *format;
-
-
-    if (method == OC_REST_PUT)
+    else if (info->resource == NULL)
     {
-        json = cJSON_CreateObject();
-        cJSON_AddStringToObject(json, OC_RSRVD_HREF, resource->uri);
-        cJSON_AddItemToObject(json, OC_RSRVD_REPRESENTATION, format = cJSON_CreateObject());
-
-        OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
+        OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
+        goto exit;
+    }
+    else if (info->actionset == NULL)
+    {
+        OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
+        goto exit;
+    }
+    else if (info->ehRequest == NULL)
+    {
+        OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
+        goto exit;
+    }
+#ifndef WITH_ARDUINO
+    pthread_mutex_lock(&lock);
+#endif
+    DoAction(info->resource, info->actionset, info->ehRequest);
+#ifndef WITH_ARDUINO
+    pthread_mutex_unlock(&lock);
+#endif
+
+    if (info->actionset->type == RECURSIVE)
+    {
+        ScheduledResourceInfo *schedule;
+        schedule = (ScheduledResourceInfo *) OCMalloc(
+                sizeof(ScheduledResourceInfo));
 
-        if(strcmp(doWhat, ACTIONSET) == 0)
+        if (schedule)
         {
-            OCActionSet *actionSet;
-            BuildActionSetFromString(&actionSet, details);
-
-            if(actionSet != NULL)
-            {
-                AddActionSet(&resource->actionsetHead, actionSet);
-                stackRet = OC_STACK_OK;
-            }
-            else
-            {
-                stackRet = OC_STACK_ERROR;
-            }
+            OC_LOG(INFO, TAG, PCF("Building New Call Info."));
+            memset(schedule, 0, sizeof(ScheduledResourceInfo));
 
-        }
-        else if (strncmp(doWhat, DELETE_ACTIONSET, sizeof(DELETE_ACTIONSET)) == 0)
-        {
-            if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
-            {
-                stackRet = OC_STACK_OK;
-            }
-            else
+            if (info->actionset->timesteps > 0)
             {
-                stackRet = OC_STACK_ERROR;
+#ifndef WITH_ARDUINO
+                pthread_mutex_lock(&lock);
+#endif
+                schedule->resource = info->resource;
+                schedule->actionset = info->actionset;
+                schedule->ehRequest = info->ehRequest;
+
+                schedule->time = registerTimer(info->actionset->timesteps,
+                        &schedule->timer_id,
+                        &DoScheduledGroupAction);
+
+                OC_LOG(INFO, TAG, PCF("Reregisteration."));
+#ifndef WITH_ARDUINO
+                pthread_mutex_unlock(&lock);
+#endif
+                AddScheduledResource(&scheduleResourceList, schedule);
             }
         }
+    }
 
-        jsonResponse = cJSON_Print(json);
-        cJSON_Delete(json);
+    RemoveScheduledResource(&scheduleResourceList, info);
 
-        strcat((char *) bufferPtr, jsonResponse);
+    exit:
 
-        bufferLength = strlen((const char *) buffer);
-        if (bufferLength > 0)
-        {
-            OCEntityHandlerResponse response = { 0 };
-            response.ehResult = OC_EH_OK;
-            response.payload = buffer;
-            response.payloadSize = bufferLength + 1;
-            response.persistentBufferFlag = 0;
-            response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
-            response.resourceHandle = (OCResourceHandle) collResource;
-            stackRet = OCDoResponse(&response);
-        }
+    return;
+}
 
-        stackRet = OC_STACK_OK;
-    }
+OCStackResult BuildCollectionGroupActionJSONResponse(
+        OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
+        OCEntityHandlerRequest *ehRequest)
+{
+    OCStackResult stackRet = OC_STACK_ERROR;
 
-    if (method == OC_REST_POST)
+    OC_LOG(INFO, TAG, PCF("Group Action is requested."));
+    // if (stackRet == OC_STACK_OK)
     {
-        OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
+        char *doWhat = NULL;
+        char *details = NULL;
+
+        size_t bufferLength = 0;
+        unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
+        unsigned char *bufferPtr = NULL;
 
-        OCActionSet *actionset = NULL;
+        bufferPtr = buffer;
 
-        json = cJSON_CreateObject();
-        cJSON_AddStringToObject(json, OC_RSRVD_HREF, resource->uri);
+        OCResource * collResource = (OCResource *) ehRequest->resource;
 
-        if (strcmp(doWhat, DO_ACTION) == 0)
+        char *jsonResponse;
+
+        ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload, &doWhat,
+                &details);
+
+        cJSON *json;
+        cJSON *format;
+
+        if (method == OC_REST_PUT)
         {
-            if (GetActionSet(details, resource->actionsetHead, &actionset) != OC_STACK_OK)
-            {
-                OC_LOG(ERROR, TAG, PCF("ERROR: GetActionSet failed"));
-                stackRet = OC_STACK_ERROR;
-            }
+            json = cJSON_CreateObject();
+            cJSON_AddStringToObject(json, "href", resource->uri);
+            cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
 
-            if (actionset == NULL)
+            OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
+
+            if (strcmp(doWhat, ACTIONSET) == 0)
             {
-                OC_LOG(ERROR, TAG, PCF("ERROR: Actionset is NULL"));
-                stackRet = OC_STACK_ERROR;
+                OCActionSet *actionSet = NULL;
+                stackRet = BuildActionSetFromString(&actionSet, details);
+
+                if(stackRet == OC_STACK_OK)
+                {
+                    if (actionSet != NULL)
+                    {
+                        stackRet = AddActionSet(&resource->actionsetHead,
+                                actionSet);
+                        if (stackRet == OC_STACK_ERROR)
+                        {
+                            if(actionSet != NULL)
+                            {
+                                DeleteActionSet( &actionSet );
+                            }
+                            OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
+                        }
+                    }
+                }
+                else
+                {
+                    stackRet = OC_STACK_ERROR;
+                }
+
             }
-            else
+            else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
             {
+                if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
+                {
+                    stackRet = OC_STACK_OK;
+                }
+                else
+                {
+                    stackRet = OC_STACK_ERROR;
+                }
+            }
 
-                OCAction *pointerAction = actionset->head;
+            jsonResponse = cJSON_Print(json);
+            cJSON_Delete(json);
 
-                uint32_t num = GetNumOfTargetResource(pointerAction);
+            strcat((char *) bufferPtr, jsonResponse);
 
-                ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
-                        HandleAggregateResponse;
-                ((OCServerRequest *) ehRequest->requestHandle)->numResponses = num + 1;
+            bufferLength = strlen((const char *) buffer);
+            if (bufferLength > 0)
+            {
+                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);
+            }
+        }
 
-                while (pointerAction != NULL)
-                {
-                    char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
-                    char* actionDescPtr = actionDesc;
-                    uint16_t remaining = MAX_RESPONSE_LENGTH;
+        if (method == OC_REST_POST)
+        {
+            OCActionSet *actionset = NULL;
 
-                    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));
+            json = cJSON_CreateObject();
+            cJSON_AddStringToObject(json, "href", resource->uri);
 
-                    ClientRequestInfo *info = (ClientRequestInfo *) OCMalloc(
-                            sizeof(ClientRequestInfo));
-                    memset(info, 0, sizeof(ClientRequestInfo));
+            if ((strcmp(doWhat, DO_ACTION) == 0)
+                    || (strcmp(doWhat, "DoScheduledAction") == 0))
+            {
+                char *pActionsetName = NULL;
+                long int delay = -1;
 
-                    info->collResource = resource;
-                    info->ehRequest = (OCServerRequest *) ehRequest->requestHandle;
+                if (strcmp(doWhat, "DoScheduledAction") == 0)
+                {
+                    stackRet = ExtractActionSetNameAndDelaytime(details,
+                            &pActionsetName, &delay);
 
-                    SendAction(&info->required, pointerAction->resourceUri, actionDescPtr);
+                    OCFREE(details)
+                    details = pActionsetName;
+                }
+                else
+                {
+                    stackRet = OC_STACK_OK;
+                }
 
-                    AddClientRequestInfo(&clientRequestList, info);
+                if (stackRet == OC_STACK_OK)
+                {
+                    if (GetActionSet(details, resource->actionsetHead,
+                            &actionset) != OC_STACK_OK)
+                    {
+                        OC_LOG(INFO, TAG, PCF("ERROR"));
+                        stackRet = OC_STACK_ERROR;
+                    }
 
-                    pointerAction = pointerAction->next;
+                    if (actionset == NULL)
+                    {
+                        OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
+                        stackRet = OC_STACK_ERROR;
+                    }
+                    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);
+
+                            delay =
+                                    (delay == -1 ? actionset->timesteps : delay);
+
+                            ScheduledResourceInfo *schedule;
+                            schedule = (ScheduledResourceInfo *) OCMalloc(
+                                    sizeof(ScheduledResourceInfo));
+
+                            if (schedule)
+                            {
+                                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;
+                                }
+                            }
+                        }
+                    }
                 }
+            }
+            else if (strcmp(doWhat, "CancelAction") == 0)
+            {
+                ScheduledResourceInfo *info =
+                        GetScheduledResourceByActionSetName(scheduleResourceList, details);
 
+                if(info != NULL)
+                {
+                    unregisterTimer(info->timer_id);
 
-                stackRet = OC_STACK_OK;
+                    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;
 
-            cJSON_AddItemToObject(json, OC_RSRVD_REPRESENTATION, format = cJSON_CreateObject());
-            GetActionSet(details, resource->actionsetHead, &actionset);
-            if (actionset != NULL)
+            else if (strcmp(doWhat, GET_ACTIONSET) == 0)
             {
-                BuildStringFromActionSet(actionset, &plainText);
+                char *plainText = NULL;
+                OCActionSet *actionset = NULL;
 
-                if (plainText != NULL)
+                cJSON_AddItemToObject(json, "rep", format =
+                        cJSON_CreateObject());
+                GetActionSet(details, resource->actionsetHead, &actionset);
+                if (actionset != NULL)
                 {
-                    cJSON_AddStringToObject(format, ACTIONSET, plainText);
-                }
+                    BuildStringFromActionSet(actionset, &plainText);
 
-                stackRet = OC_STACK_OK;
+                    if (plainText != NULL)
+                    {
+                        cJSON_AddStringToObject(format, ACTIONSET, plainText);
+                    }
+
+                    stackRet = OC_STACK_OK;
+                }
             }
-        }
 
-        jsonResponse = cJSON_Print(json);
-        cJSON_Delete(json);
 
-        strcat((char *) bufferPtr, jsonResponse);
+            jsonResponse = cJSON_Print(json);
+            cJSON_Delete(json);
 
-        bufferLength = strlen((const char *) buffer);
-        if (bufferLength > 0)
-        {
-            OCEntityHandlerResponse response = {};
-            response.ehResult = OC_EH_OK;
-            response.payload = buffer;
-            response.payloadSize = bufferLength + 1;
-            response.persistentBufferFlag = 0;
-            response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
-            response.resourceHandle = (OCResourceHandle) collResource;
-            stackRet = OCDoResponse(&response);
+            strcat((char *) bufferPtr, jsonResponse);
+
+            bufferLength = strlen((const char *) buffer);
+            if (bufferLength > 0)
+            {
+                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);
+            }
         }
-    }
 
-    OCFree(doWhat);
-    OCFree(details);
+        OCFREE(doWhat)
+        OCFREE(details)
+    }
 
     return stackRet;
 }
-
index d7d7ca0..8d0c5ab 100755 (executable)
@@ -59,6 +59,8 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
 void allBulbOn();
 void allBulbOff();
 
+shared_ptr<OCResource> g_light;
+
 void foundResources(
         std::vector<std::shared_ptr<OC::OCResource> > listOfResource)
 {
@@ -89,6 +91,8 @@ void foundResources(
             }
 
             lights.push_back((hostAddress + resourceURI));
+
+            g_light = (*rsrc);
         }
     }
 
@@ -100,10 +104,10 @@ void foundResource(std::shared_ptr<OCResource> resource)
     std::string resourceURI;
     std::string hostAddress;
 
+    cout << "FOUND RESOURCE" << endl;
+
     try
     {
-        cout << "FOUND RESOURCE" << endl;
-
         if (resource)
         {
             resourceURI = resource->uri();
@@ -114,16 +118,14 @@ void foundResource(std::shared_ptr<OCResource> resource)
 
                 // g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
 
-                printf("\tHOST :: %s\n", resource->host().c_str());
+                cout << "FOUND " << resourceURI << endl;
+                // printf("\tHOST :: %s\n", resource->host().c_str());
             }
             else if (resourceURI == "/core/bookmark")
             {
                 resource->observe(ObserveType::Observe, QueryParamsMap(),
                         &onObserve);
             }
-
-            // p_platform.bindResource(resourceHandle, foundResourceHandle);
-
         }
     }
     catch (std::exception& e)
@@ -222,8 +224,11 @@ void allBulbOn()
 
     if (g_resource)
     {
-        g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
-                &onPost);
+        OCStackResult res = g_resource->post("a.collection", GROUP_INTERFACE,
+            rep, QueryParamsMap(), &onPost);
+
+        if( res != OC_STACK_OK )
+            cout << "failed" << endl;
     }
 }
 
@@ -431,9 +436,15 @@ int main()
         types.push_back("core.light");
         thingsMgr->findCandidateResources(types, &foundResources, 5);
 
-        OCPlatform::registerResource(resourceHandle, resourceURI,
+        OCStackResult res = OCPlatform::registerResource(resourceHandle, resourceURI,
                 resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE);
 
+        if( res != OC_STACK_OK )
+        {
+            cout << "Resource registeration failed." << endl;
+            return 0;
+        }
+
         cout << "registerResource is called." << endl;
 
         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
@@ -462,30 +473,39 @@ int main()
                 cout << "0 :: FIND BOOKMARK TO OBSERVE"
                         << endl;
 
-                fflush(stdin);
+                //fflush(stdin);
                 cin >> n;
 
                 if (n == 9)
                 {
+                    std::string query = OC_WELL_KNOWN_QUERY;
+                    query.append("?rt=");
+                    query.append(resourceTypeName);
+
                     OCPlatform::findResource("",
-                            "coap://224.0.1.187/oc/core?rt=a.collection",
+                            query,
                             OC_ETHERNET,
                             &foundResource);
-                    OCPlatform::findResource("",
-                            "coap://224.0.1.187/oc/core?rt=a.collection",
-                            OC_WIFI,
-                            &foundResource);
+
+                    // OCPlatform::findResource("",
+                    //         query,
+                    //         OC_WIFI,
+                    //         &foundResource);
                 }
                 else if (n == 0)
                 {
+                    std::string query = OC_WELL_KNOWN_QUERY;
+                    query.append("?rt=");
+                    query.append("core.bookmark");
+
                     OCPlatform::findResource("",
-                            "coap://224.0.1.187/oc/core?rt=core.bookmark",
+                            query,
                             OC_ETHERNET,
                             &foundResource);
-                    OCPlatform::findResource("",
-                            "coap://224.0.1.187/oc/core?rt=core.bookmark",
-                            OC_WIFI,
-                            &foundResource);
+                    // OCPlatform::findResource("",
+                    //         query,
+                    //         OC_WIFI,
+                    //         &foundResource);
                 }
                 else if (n == 1)
                 {
@@ -546,8 +566,19 @@ int main()
                     isRun = false;
                     break;
                 }
+                else if(n == 100)
+                {
+
+                    OCRepresentation rep;
+
+                    rep.setValue("power", std::string("on"));
+
+                    g_light->put(rep, QueryParamsMap(), &onPut);
+
+                }
             }
         }
+        usleep(500*1000);
     }
     catch (OCException& e)
     {
index c40c11e..091621a 100755 (executable)
@@ -222,6 +222,7 @@ OCStackResult GroupManager::findCandidateResources(
     for (unsigned int i = 0; i < resourceTypes.size(); ++i)
     {
         // std::cout << "resourceTypes : " << resourceTypes.at(i) << std::endl;
+
         std::string query = OC_WELL_KNOWN_QUERY;
         query.append("?rt=");
         query.append(resourceTypes.at(i));
@@ -233,12 +234,12 @@ OCStackResult GroupManager::findCandidateResources(
                         > (std::bind(&GroupManager::onFoundResource, this, std::placeholders::_1,
                                 waitsec)));
 
-        OCPlatform::findResource("", 
-                query,
-                OC_WIFI,
-                std::function < void(std::shared_ptr < OCResource > resource)
-                        > (std::bind(&GroupManager::onFoundResource, this, std::placeholders::_1,
-                                waitsec)));
+        // OCPlatform::findResource("",
+        //         query,
+        //         OC_WIFI,
+        //         std::function < void(std::shared_ptr < OCResource > resource)
+        //                 > (std::bind(&GroupManager::onFoundResource, this, std::placeholders::_1,
+        //                         waitsec)));
     }
 
     if (waitsec >= 0)
@@ -365,7 +366,7 @@ void GroupManager::checkCollectionRepresentation(const OCRepresentation& rep,
         result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
                 // resourceType,
                 resourceTypes.front(),
-                OC_WIFI,
+                OC_ETHERNET,
                 std::function<
                         void(OCStackResult result, const unsigned int nonce,
                                 const std::string& hostAddress) >(
index 87458cb..09327f9 100644 (file)
@@ -74,6 +74,8 @@ namespace OIC
             std::string query = OC_WELL_KNOWN_QUERY;
             query.append("?rt=");
             query.append(collectionResourceTypes.at(i));
+
+            cout << query << endl;
             cout << "GroupSynchronization::findGroup - " << query << endl;
 
             OCPlatform::findResource("", query,
@@ -704,10 +706,10 @@ OCStackResult GroupSynchronization::leaveGroup(
                                 std::bind(&GroupSynchronization::onFindResource, this,
                                         std::placeholders::_1));
                         
-                        OCPlatform::findResource("", resourceName,
-                                OC_WIFI,
-                                std::bind(&GroupSynchronization::onFindResource, this,
-                                        std::placeholders::_1));
+                        // OCPlatform::findResource("", resourceName,
+                        //         OC_WIFI,
+                        //         std::bind(&GroupSynchronization::onFindResource, this,
+                        //                 std::placeholders::_1));
                     }
                     else if (methodType == "leaveGroup")
                     {