We provide function of Scheduled Group Action.
It will send some action message
when preset time is expired.
And we need some function about the time.
These function will use for checking time.
Change-Id: Ib42941db257cd481be4ed6b7f1a8c1b28ff3809e
Signed-off-by: HyunJun Kim <hyunjun2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/436
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
--- /dev/null
+//******************************************************************
+//
+// Copyright 2014 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+
+
+#define _BSD_SOURCE
+
+#ifndef WITH_ARDUINO
+#include <pthread.h>
+#include <unistd.h>
+#include <memory.h>
+#include <stdlib.h>
+#endif
+
+#include <string.h>
+#include <stdio.h>
+
+#include "timer.h"
+
+#define SECOND (1)
+
+#define TIMEOUTS 10
+
+#define TIMEOUT_USED 1
+#define TIMEOUT_UNUSED 2
+
+#ifndef WITH_ARDUINO
+pthread_t thread_id = 0; // 0: initial thread id (meaningless)
+#endif
+
+struct timelist_t
+{
+ int timeout_state;
+ time_t timeout_seconds;
+ time_t timeout_time;
+ void (*cb)();
+} timeout_list[TIMEOUTS];
+
+/*
+ * Return the number of seconds between before and after, (after - before).
+ * This must be async-signal safe, so it cannot use difftime().
+ */
+time_t timespec_diff(const time_t after, const time_t before)
+{
+ return after - before;
+}
+
+/*
+ * Add positive seconds to a timespec, nothing if seconds is negative.
+ */
+void timespec_add(time_t * to, const time_t seconds)
+{
+ if (to && seconds > 0)
+ {
+ (*to) += seconds;
+ }
+}
+
+#ifndef WITH_ARDUINO
+
+long int getSeconds(struct tm* tp)
+{
+ long int nInterval = 0;
+
+ nInterval = (tp->tm_hour * SECS_PER_HOUR);
+ nInterval += (tp->tm_min * SECS_PER_MIN);
+ nInterval += (tp->tm_sec * SECOND);
+
+ printf("%ld", nInterval);
+
+ return nInterval;
+}
+
+long int getRelativeSecondsOfDayofweek(int ia, int ib)
+{
+ if( ia > ib )
+ return (((long int)(7 - (ib - ia))) * SECS_PER_DAY);
+
+ return (((long int)((ib - ia))) * SECS_PER_DAY);
+}
+
+long int getRelativeIntervalOfWeek(struct tm* tp)
+{
+ time_t current_time;
+ struct tm* current, *midnight;
+ time_t delayed_time = 0;
+
+ time(¤t_time);
+ current = localtime(¤t_time);
+ midnight = (struct tm* )malloc(sizeof(struct tm));
+ memcpy(midnight, current, sizeof(struct tm));
+
+ midnight->tm_hour = 0;
+ midnight->tm_min = 0;
+ midnight->tm_sec = 0;
+
+ // Seconds.
+ // Seconds from midnight.
+ delayed_time = current_time - mktime(midnight);
+ delayed_time = getRelativeSecondsOfDayofweek(current->tm_wday, tp->tm_wday) - delayed_time;
+ delayed_time = delayed_time + getSeconds(tp);
+
+ return delayed_time;
+}
+
+long int getSecondsFromAbsTime(struct tm* tp)
+{
+ time_t current_time;
+ time_t delayed_time = 0;
+
+ time(¤t_time);
+ localtime(¤t_time);
+
+ delayed_time = mktime(tp) - current_time;
+
+ return delayed_time;
+}
+
+time_t registerTimer(const time_t seconds, int *id, void *cb)
+{
+ time_t now, then;
+ time_t next;
+ int i, idx;
+
+ if (0 == thread_id)
+ {
+ initThread();
+ }
+
+ if (seconds <= 0)
+ return -1 ;
+
+ // get the current time
+ time(&now);
+
+ for (idx = 0; idx < TIMEOUTS; ++idx)
+ if (!((timeout_list[idx].timeout_state & TIMEOUT_USED) & TIMEOUT_USED))
+ break;
+
+ if (TIMEOUTS == idx) // reach to end of timer list
+ return -1;
+
+ // idx th timeout will be used.
+ // Reset and set state of the timer
+ timeout_list[idx].timeout_state = 0;
+ timeout_list[idx].timeout_state |= TIMEOUT_USED;
+
+ // calculate when the timeout should fire
+ then = now;
+ timespec_add(&then, seconds);
+
+ timeout_list[idx].timeout_time = then;
+ timeout_list[idx].timeout_seconds = seconds;
+
+ // printf( "\nbefore timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
+ timeout_list[idx].cb = cb;
+ // printf( " after timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
+
+ // How long till the next timeout?
+ next = seconds;
+ for (i = 0; i < TIMEOUTS; i++)
+ {
+ if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED)) == TIMEOUT_USED)
+ {
+ const time_t secs = timespec_diff(timeout_list[i].timeout_time, now);
+
+ if (secs >= 0 && secs < next)
+ next = secs;
+ }
+ }
+
+ *id = idx;
+ /* Return the timeout number. */
+ return timeout_list[idx].timeout_time;
+}
+
+void unregisterTimer(int idx)
+{
+ if( 0 <= idx && idx <= TIMEOUTS)
+ timeout_list[idx].timeout_state = TIMEOUT_UNUSED;
+}
+
+void checkTimeout()
+{
+ time_t now;
+ int i;
+
+ time(&now);
+
+ /* Check all timeouts that are used and armed, but not passed yet. */
+ for (i = 0; i < TIMEOUTS; i++)
+ {
+ if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED)) == TIMEOUT_USED)
+ {
+ const time_t seconds = timespec_diff(timeout_list[i].timeout_time, now);
+
+ if (seconds <= 0)
+ {
+ /* timeout [i] fires! */
+ timeout_list[i].timeout_state = TIMEOUT_UNUSED;
+ if (timeout_list[i].cb)
+ {
+ timeout_list[i].cb();
+ }
+ }
+ }
+ }
+}
+
+void *loop(void *threadid)
+{
+ while (1)
+ {
+ sleep(SECOND);
+ checkTimeout();
+ }
+
+ return NULL ;
+}
+
+int initThread()
+{
+ int res;
+ long t = 0;
+
+ res = pthread_create(&thread_id, NULL, loop, (void *) t);
+
+ if (res)
+ {
+ printf("ERROR; return code from pthread_create() is %d\n", res);
+ return -1;
+ }
+
+ return 0;
+}
+#else // WITH_ARDUINO
+time_t timeToSecondsFromNow(tmElements_t *t_then)
+{
+ time_t t, then;
+
+ t = now();
+ then = makeTime((*t_then));
+
+ return (time_t) (then - t);
+}
+
+time_t registerTimer(const time_t seconds, int *id, void (*cb)())
+{
+ time_t t, then;
+ time_t next;
+ int i, idx;
+
+ if (seconds <= 0)
+ return -1;
+
+ // get the current time
+ t = now();
+
+ for (idx = 0; idx < TIMEOUTS; ++idx)
+ if (!((timeout_list[idx].timeout_state & TIMEOUT_USED) & TIMEOUT_USED))
+ break;
+
+ if (TIMEOUTS == idx)// reach to end of timer list
+ return -1;
+
+ // idx th timeout will be used.
+ // Reset and set state of the timer
+ timeout_list[idx].timeout_state = 0;
+ timeout_list[idx].timeout_state |= TIMEOUT_USED;
+
+ // calculate when the timeout should fire
+ then = t;
+ timespec_add(&then, seconds);
+
+ timeout_list[idx].timeout_time = then;
+ timeout_list[idx].timeout_seconds = seconds;
+
+ // printf( "\nbefore timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
+ timeout_list[idx].cb = cb;
+ // printf( " after timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
+
+ // How long till the next timeout?
+ next = seconds;
+ for (i = 0; i < TIMEOUTS; i++)
+ {
+ if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED))
+ == TIMEOUT_USED)
+ {
+ const time_t secs = timespec_diff(timeout_list[i].timeout_time,
+ t);
+
+ if (secs >= 0 && secs < next)
+ next = secs;
+ }
+ }
+
+ *id = idx;
+ /* Return the timeout number. */
+ return timeout_list[idx].timeout_time;
+}
+
+void unregisterTimer(int idx)
+{
+ if( 0 <= idx && idx <= TIMEOUTS)
+ timeout_list[idx].timeout_state = TIMEOUT_UNUSED;
+}
+
+void checkTimeout()
+{
+ time_t t;
+ int i;
+
+ t = now();
+
+ /* Check all timeouts that are used and armed, but not passed yet. */
+ for (i = 0; i < TIMEOUTS; i++)
+ {
+ if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED))
+ == TIMEOUT_USED)
+ {
+ const time_t seconds = timespec_diff(timeout_list[i].timeout_time,
+ t);
+
+ if (seconds <= 0)
+ {
+ /* timeout [i] fires! */
+ timeout_list[i].timeout_state = TIMEOUT_UNUSED;
+ if (timeout_list[i].cb)
+ {
+ timeout_list[i].cb();
+ }
+ }
+ }
+ }
+}
+
+#endif
--- /dev/null
+//******************************************************************
+//
+// Copyright 2014 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef TIMER_H_
+#define TIMER_H_
+
+#ifndef WITH_ARDUINO
+#include <sys/time.h>
+#else
+#include <Time.h>
+#endif
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <math.h>
+
+#ifndef WITH_ARDUINO
+#define SECS_PER_MIN (60L)
+#define SECS_PER_HOUR (SECS_PER_MIN * 60L)
+#define SECS_PER_DAY (SECS_PER_HOUR * 24L)
+#define DAYS_PER_WEEK (7L)
+#define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK)
+#define SECS_PER_YEAR (SECS_PER_WEEK * 52L)
+#define SECS_YR_2000 (946684800L)
+#endif
+
+time_t timespec_diff(const time_t after, const time_t before);
+void timespec_add(time_t * to, const time_t seconds);
+void checkTimeout();
+
+#ifndef WITH_ARDUINO
+long int getSeconds(struct tm* tp);
+long int getRelativeIntervalOfWeek(struct tm* tp);
+long int getSecondsFromAbsTime(struct tm* tp);
+
+int initThread();
+void *loop(void *threadid);
+time_t registerTimer(const time_t seconds, int *id, void *cb);
+void unregisterTimer(int id);
+
+#else
+
+time_t timeToSecondsFromNow(tmElements_t *t);
+time_t registerTimer(const time_t seconds, int *id, void (*cb)());
+void unregisterTimer(int id);
+
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* TIMER_H_ */
+
######################################################################
liboctbstack_env.PrependUnique(CPPPATH = [
'../../extlibs/cjson/',
+ '../../extlibs/timer/',
'ocsocket/include',
'logger/include',
'ocrandom/include',
OCTBSTACK_SRC = 'stack/src/'
liboctbstack_src = [
'../../extlibs/cjson/cJSON.c',
+ '../../extlibs/timer/timer.c',
'occoap/src/occoap.c',
'occoap/src/occoaphelper.c',
OCTBSTACK_SRC + 'ocstack.c',
OCMALLOC_DIR = ocmalloc
EXTLIBS_DIR = ../../extlibs
CJSON_DIR = $(EXTLIBS_DIR)/cjson
+TIMER_DIR = $(EXTLIBS_DIR)/timer
TINYDTLS_DIR = $(EXTLIBS_DIR)/tinydtls
OCCOAP_SRC = $(OCCOAP_DIR)/src
OCTBSTACK_SRC = $(OCTBSTACK_DIR)/src
OCMALLOC_SRC = $(OCMALLOC_DIR)/src
CJSON_SRC = $(CJSON_DIR)
+TIMER_SRC = $(TIMER_DIR)
OCLOGGER_INC = $(OCLOGGER_DIR)/include
OC_LOG_INC = $(OC_LOG_DIR)/include
OCTBSTACK_INC = $(OCTBSTACK_DIR)/include
OCMALLOC_INC = $(OCMALLOC_DIR)/include
CJSON_INC = $(CJSON_DIR)
+TIMER_INC = $(TIMER_DIR)
INC_DIRS := -I$(OCLOGGER_INC)
INC_DIRS += -I$(OC_LOG_INC)
INC_DIRS += -I$(OCTBSTACK_INC)
INC_DIRS += -I$(OCTBSTACK_INC)/internal
INC_DIRS += -I$(CJSON_INC)
+INC_DIRS += -I$(TIMER_INC)
CC_FLAGS.debug := -O0 -g3 -Wall -c -fmessage-length=0 -pedantic -fpic -DTB_LOG
CC_FLAGS.release := -Os -Wall -c -fmessage-length=0 -fpic
LDLIBS += -lcoap
CJSON_SOURCES := $(CJSON_SRC)/cJSON.c
+TIMER_SOURCES := $(TIMER_SRC)/timer.c
OCCOAP_SOURCES := $(OCCOAP_SRC)/occoap.c
OCCOAP_SOURCES += $(OCCOAP_SRC)/occoaphelper.c
OCTBSTACK_SOURCES += $(OCTBSTACK_SRC)/ocsecurity.c
SOURCES := $(CJSON_SOURCES)
+SOURCES += $(TIMER_SOURCES)
SOURCES += $(OCCOAP_SOURCES)
SOURCES += $(OCTBSTACK_SOURCES)
} OCAction;
// following structure will be created in occollection.
-typedef struct ocactionset {
-
+typedef struct ocactionset
+{
struct ocactionset *next;
char *actionsetName;
+ long int timesteps;
+ unsigned int type;
OCAction* head;
} OCActionSet;
-
-
typedef struct resourcetype_t {
struct resourcetype_t *next; // linked list; for multiple types on resource
#ifndef OIC_GROUP_H
#define OIC_GROUP_H
-#include "ocstack.h"
#include "ocstackinternal.h"
+#include "ocstack.h"
+#include "ocresource.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
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);
BuildCollectionGroupActionJSONResponse(OCMethod method/*OCEntityHandlerFlag flag*/,
OCResource *resource, OCEntityHandlerRequest *ehRequest);
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
#endif // OIC_GROUP_H
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#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 ATTR_DELIMITER "|"
#define ATTR_ASSIGN "="
+#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;
struct aggregatehandleinfo *next;
} ClientRequstInfo;
-// unsigned int nHandleIdx = 0;
-// ClientRequstInfo g_AggregateResponseHandle[10];
-
ClientRequstInfo *clientRequstList = NULL;
void AddClientRequestInfo(ClientRequstInfo **head, ClientRequstInfo* add)
}
}
-ClientRequstInfo* GetClientRequestInfo(ClientRequstInfo *head, OCDoHandle handle)
+ClientRequstInfo* GetClientRequestInfo(ClientRequstInfo *head,
+ OCDoHandle handle)
{
ClientRequstInfo *tmp = NULL;
{
while (tmp)
{
-// printf("%p :: %p\n", tmp->required, handle);
if (tmp->required == handle)
{
break;
tmp = tmp->next;
}
- return tmp;
}
return NULL;
}
{
ClientRequstInfo *tmp = NULL;
+ if (del == NULL)
+ return;
+
if (*head == del)
{
*head = (*head)->next;
}
}
-
-
void AddCapability(OCCapability** head, OCCapability* node)
{
OCCapability *pointer = *head;
}
}
-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)
{
- free(del->capability);
+ OCFREE(del->capability)
del->capability = NULL;
- free(del->status);
+ OCFREE(del->status)
del->status = NULL;
+ OCFREE(del)
}
void DeleteAction(OCAction** action)
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;
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)
{
(*resource)->actionsetHead = pointer->next;
else
(*resource)->actionsetHead = NULL;
+
DeleteActionSet(&pointer);
}
else if (pointer->next != NULL)
{
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;
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;
}
-
-#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;
- char* pRequest = (char *)request + strlen(OIC_ACTION_PREFIX);
+ 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;
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(strcmp(key, "uri") == 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)
}
}
- 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;
}
char temp[1024] = { 0 };
int remaining = 1023;
- // OCActionSet *as = resource->actionsetHead;
- // while(as != NULL)
- // {
- // printf("\n\n\nAction Set Name :: %s\n", actionset->actionsetName);
OCAction *action = actionset->head;
if (remaining >= strlen(actionset->actionsetName) + 1)
{
strcat(temp, actionset->actionsetName);
remaining -= strlen(actionset->actionsetName);
- strcat(temp, "*");
+ strcat(temp, ACTION_DELIMITER);
remaining--;
}
else
while (action != NULL)
{
- printf("\tURI :: %s\n", action->resourceUri);
strcat(temp, "uri=");
remaining -= strlen("uri=");
strcat(temp, action->resourceUri);
OCCapability *capas = action->head;
while (capas != NULL)
{
- printf("\t\t%s = %s\n", capas->capability, capas->status);
strcat(temp, capas->capability);
remaining -= strlen(capas->capability);
strcat(temp, "=");
action = action->next;
if (action != NULL)
{
- strcat(temp, "*");
+ strcat(temp, ACTION_DELIMITER);
remaining--;
}
}
- // as = as->next;
- // }
*desc = (char *) OCMalloc(1024 - remaining);
strcpy(*desc, temp);
- // printf("\t\tPlain Text = %s(%i)\n", *desc, 1024 - remaining);
return OC_STACK_OK;
}
OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
OCClientResponse* clientResponse)
{
- printf("\n\n\tcallback is called\n\n");
+ OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
ClientRequstInfo *info = GetClientRequestInfo(clientRequstList, handle);
unsigned char *responseJson;
responseJson = (unsigned char *) OCMalloc(
- (unsigned int) (strlen((char *) clientResponse->resJSONPayload) + 1));
+ (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
+ + 1));
// We need the body of response.
// Copy the body from the response
- strcpy((char *) responseJson, ((char *) clientResponse->resJSONPayload
- + OC_JSON_PREFIX_LEN));
+ 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;
OCDoResponse(&response);
RemoveClientRequestInfo(&clientRequstList, info);
- OCFree(responseJson);
+ OCFREE(info)
+ OCFREE(responseJson)
}
- // g_AggregateResponseHandle
-
return OC_STACK_KEEP_TRANSACTION;
}
void ActionSetCD(void *context)
{
- // printf("\n\t\tCD is called\n");
-
- // free( context );
}
-OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr, uint16_t *remaining)
+OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
+ uint16_t *remaining)
{
OCStackResult ret = OC_STACK_ERROR;
cJSON *json;
OCCapability* pointerCapa = action->head;
while (pointerCapa)
{
- cJSON_AddStringToObject(body, pointerCapa->capability, pointerCapa->status);
+ cJSON_AddStringToObject(body, pointerCapa->capability,
+ pointerCapa->status);
pointerCapa = pointerCapa->next;
}
unsigned int GetNumOfTargetResource(OCAction *actionset)
{
- int numOfREsource = 0;
+ int numOfResource = 0;
OCAction *pointerAction = actionset;
while (pointerAction != NULL)
{
- numOfREsource++;
+ numOfResource++;
pointerAction = pointerAction->next;
}
- return numOfREsource;
+ return numOfResource;
}
-OCStackResult SendAction(OCDoHandle *handle, const char *targetUri, const unsigned char *action)
+OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
+ const unsigned char *action)
{
OCCallbackData cbdata = { 0 };
cbdata.cb = &ActionSetCB;
cbdata.context = (void *) 0x99;
return OCDoResource(handle, OC_REST_PUT, targetUri,
- //temp->rsrcType->resourcetypename,
- NULL, (char *) action, OC_NA_QOS, &cbdata, NULL, 0);
+ NULL, (char *) action, OC_NA_QOS, &cbdata, NULL, 0);
+}
+
+OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
+ OCServerRequest* requestHandle)
+{
+ OCStackResult result = OC_STACK_ERROR;
+ OCAction *pointerAction = actionset->head;
+
+ 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));
+
+ ClientRequstInfo *info = (ClientRequstInfo *) OCMalloc(
+ sizeof(ClientRequstInfo));
+ memset(info, 0, sizeof(ClientRequstInfo));
+
+ info->collResource = resource;
+ info->ehRequest = requestHandle;
+
+ result = SendAction(&info->required, pointerAction->resourceUri,
+ actionDescPtr);
+ if (result != OC_STACK_OK)
+ {
+ return result;
+ }
+
+ AddClientRequestInfo(&clientRequstList, info);
+
+ pointerAction = pointerAction->next;
+ }
+
+ return result;
+}
+
+void DoScheduledGroupAction()
+{
+ OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
+ ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
+
+ if (info == NULL)
+ {
+ OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
+ goto exit;
+ }
+ else if (info->resource == NULL)
+ {
+ 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 (schedule)
+ {
+ OC_LOG(INFO, TAG, PCF("Building New Call Info."));
+ memset(schedule, 0, sizeof(ScheduledResourceInfo));
+
+ if (info->actionset->timesteps > 0)
+ {
+#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);
+ }
+ }
+ }
+
+ RemoveScheduledResource(&scheduleResourceList, info);
+
+ exit:
+
+ return;
}
-OCStackResult BuildCollectionGroupActionJSONResponse(OCMethod method/*OCEntityHandlerFlag flag*/,
- OCResource *resource, OCEntityHandlerRequest *ehRequest)
+OCStackResult BuildCollectionGroupActionJSONResponse(
+ OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
+ OCEntityHandlerRequest *ehRequest)
{
OCStackResult stackRet = OC_STACK_ERROR;
char *jsonResponse;
- ExtractKeyValueFromRequest((char *)ehRequest->reqJSONPayload, &doWhat, &details);
+ ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload, &doWhat,
+ &details);
cJSON *json;
cJSON *format;
-
if (method == OC_REST_PUT)
{
json = cJSON_CreateObject();
OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
- if(strcmp(doWhat, "ActionSet") == 0)
+ if (strcmp(doWhat, "ActionSet") == 0)
{
- OCActionSet *actionSet;
- BuildActionSetFromString(&actionSet, details);
+ OCActionSet *actionSet = NULL;
+ stackRet = BuildActionSetFromString(&actionSet, details);
- if(actionSet != NULL)
+ if(stackRet == OC_STACK_OK)
{
- AddActionSet(&resource->actionsetHead, actionSet);
- 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
{
if (bufferLength > 0)
{
OCEntityHandlerResponse response = { 0 };
- response.ehResult = OC_EH_OK;
+ if(stackRet == OC_STACK_OK)
+ response.ehResult = OC_EH_OK;
+ else
+ response.ehResult = OC_EH_ERROR;
response.payload = buffer;
response.payloadSize = bufferLength + 1;
response.persistentBufferFlag = 0;
- response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
+ response.requestHandle =
+ (OCRequestHandle) ehRequest->requestHandle;
response.resourceHandle = (OCResourceHandle) collResource;
stackRet = OCDoResponse(&response);
}
-
- stackRet = OC_STACK_OK;
}
if (method == OC_REST_POST)
{
- OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
-
OCActionSet *actionset = NULL;
json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "href", resource->uri);
- if (strcmp(doWhat, "DoAction") == 0)
+ if ((strcmp(doWhat, "DoAction") == 0)
+ || (strcmp(doWhat, "DoScheduledAction") == 0))
{
- if (GetActionSet(details, resource->actionsetHead, &actionset) != OC_STACK_OK)
- {
- OC_LOG(INFO, TAG, PCF("ERROR"));
- stackRet = OC_STACK_ERROR;
- }
+ char *pActionsetName = NULL;
+ long int delay = -1;
- if (actionset == NULL)
+ if (strcmp(doWhat, "DoScheduledAction") == 0)
{
- OC_LOG(INFO, TAG, PCF("ERROR"));
- stackRet = OC_STACK_ERROR;
+ stackRet = ExtractActionSetNameAndDelaytime(details,
+ &pActionsetName, &delay);
+
+ OCFREE(details)
+ details = pActionsetName;
}
else
{
+ stackRet = OC_STACK_OK;
+ }
- OCAction *pointerAction = actionset->head;
-
- unsigned int num = GetNumOfTargetResource(pointerAction);
-
- ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
- HandleAggregateResponse;
- ((OCServerRequest *) ehRequest->requestHandle)->numResponses = num + 1;
-
- while (pointerAction != NULL)
+ if (stackRet == OC_STACK_OK)
+ {
+ if (GetActionSet(details, resource->actionsetHead,
+ &actionset) != OC_STACK_OK)
{
- 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));
-
- ClientRequstInfo *info = (ClientRequstInfo *) OCMalloc(
- sizeof(ClientRequstInfo));
- memset(info, 0, sizeof(ClientRequstInfo));
-
- info->collResource = resource;
- info->ehRequest = (OCServerRequest *) ehRequest->requestHandle;
-
- SendAction(&info->required, pointerAction->resourceUri, actionDescPtr);
-
- AddClientRequestInfo(&clientRequstList, info);
+ 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);
+ RemoveScheduledResource(&scheduleResourceList, info);
stackRet = OC_STACK_OK;
}
+ else
+ {
+ stackRet = OC_STACK_ERROR;
+ }
}
+
else if (strcmp(doWhat, "GetActionSet") == 0)
{
char *plainText = NULL;
OCActionSet *actionset = NULL;
- cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
+ cJSON_AddItemToObject(json, "rep", format =
+ cJSON_CreateObject());
GetActionSet(details, resource->actionsetHead, &actionset);
if (actionset != NULL)
{
}
}
+
jsonResponse = cJSON_Print(json);
cJSON_Delete(json);
bufferLength = strlen((const char *) buffer);
if (bufferLength > 0)
{
- OCEntityHandlerResponse response =
- { 0 };
- response.ehResult = OC_EH_OK;
+ OCEntityHandlerResponse response = { 0 };
+ if(stackRet == OC_STACK_OK)
+ response.ehResult = OC_EH_OK;
+ else
+ response.ehResult = OC_EH_ERROR;
response.payload = buffer;
response.payloadSize = bufferLength + 1;
response.persistentBufferFlag = 0;
- response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
+ response.requestHandle =
+ (OCRequestHandle) ehRequest->requestHandle;
response.resourceHandle = (OCResourceHandle) collResource;
stackRet = OCDoResponse(&response);
}
}
- OCFree(doWhat);
- OCFree(details);
+ OCFREE(doWhat)
+ OCFREE(details)
}
return stackRet;
######################################################################
# Build flags
######################################################################
-things_manager_env.AppendUnique(CPPPATH = ['sdk/inc', 'sdk/src'])
+things_manager_env.AppendUnique(CPPPATH = ['../../extlibs/timer', 'sdk/inc', 'sdk/src'])
if target_os not in ['windows', 'winrt']:
things_manager_env.AppendUnique(CXXFLAGS = ['-Wall'])
######################################################################
# Source files and Targets
######################################################################
-tgm_src = env.Glob('sdk/src/*.cpp')
+tgm_src = env.Glob('sdk/src/*.cpp', '../../extlibs/timer/timer.c')
tgmsdk = things_manager_env.StaticLibrary('TGMSDKLibrary', tgm_src)
things_manager_env.InstallTarget(tgmsdk, 'libTGMSDK')
# Build flags
######################################################################
linux_sample_env.AppendUnique(CPPPATH = ['include'])
+linux_sample_env.AppendUnique(CPPPATH = ['../../../../../extlibs/timer'])
linux_sample_env.AppendUnique(CPPPATH = ['../../../sdk/inc'])
linux_sample_env.AppendUnique(CPPPATH = ['../../../sdk/src'])
linux_sample_env.AppendUnique(CXXFLAGS = ['-Wall', '-pthread'])
TGMROOT=../../../
+OIC_ROOT=${TGMROOT}../..
IOT_BASE=${TGMROOT}../../resource
RST_NAME=.
TARGET1=con-server
CXX_FLAGS=-std=c++0x -Wall -pthread -DLINUX -ldl
CXX_INC := -I../../ -I../../inc/ -I../../../sdk/inc/ -I../../../sdk/src/
+CXX_INC += -I${OIC_ROOT}/extlibs/timer
CXX_INC += -I${IOT_BASE}/include/
-CXX_INC += -I${IOT_BASE}/oc_logger/include
+CXX_INC += -I${IOT_BASE}/oc_logger/include
CXX_INC += -I${IOT_BASE}/csdk/stack/include
CXX_INC += -I${IOT_BASE}/csdk/ocsocket/include
CXX_INC += -I${IOT_BASE}/csdk/ocrandom/include
# Build flags
######################################################################
linux_sample_env.AppendUnique(CPPPATH = ['include'])
+linux_sample_env.AppendUnique(CPPPATH = ['../../../../../extlibs/timer'])
linux_sample_env.AppendUnique(CPPPATH = ['../../../sdk/inc'])
linux_sample_env.AppendUnique(CPPPATH = ['../../../sdk/src'])
linux_sample_env.AppendUnique(CXXFLAGS = ['-Wall', '-pthread'])
#include <pthread.h>
#include <iostream>
+#include "timer.h"
+
#include <ThingsManager.h>
using namespace std;
bool isReady = false;
OCResourceHandle resourceHandle;
-std::vector< OCResourceHandle > resourceHandleVector;
+std::vector<OCResourceHandle> resourceHandleVector;
-shared_ptr< OCResource > g_resource;
-vector< string > lights;
+shared_ptr<OCResource> g_resource;
+vector<string> lights;
ThingsManager *thingsMgr = new ThingsManager();
-void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
+void onGet(const HeaderOptions& opt, const OCRepresentation &rep,
+ const int eCode);
-void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
+void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep,
+ const int eCode);
-void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
+void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep,
+ const int eCode);
-void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, const int& eCode,
- const int& sequenceNumber);
+void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
+ const int& eCode, const int& sequenceNumber);
void allBulbOn();
void allBulbOff();
-void foundResources(std::vector< std::shared_ptr< OC::OCResource > > listOfResource)
+void foundResources(
+ std::vector<std::shared_ptr<OC::OCResource> > listOfResource)
{
- for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end(); ++rsrc)
+ for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end();
+ ++rsrc)
{
std::string resourceURI = (*rsrc)->uri();
std::string hostAddress = (*rsrc)->host();
if (resourceURI == "/a/light")
{
-
cout << "\tResource URI : " << resourceURI << endl;
cout << "\tResource Host : " << hostAddress << endl;
OCResourceHandle foundResourceHandle;
- OCStackResult result = OCPlatform::registerResource(foundResourceHandle, (*rsrc));
+ OCStackResult result = OCPlatform::registerResource(
+ foundResourceHandle, (*rsrc));
cout << "\tresource registed!" << endl;
if (result == OC_STACK_OK)
{
isReady = true;
}
-void foundResource(std::shared_ptr< OCResource > resource)
+void foundResource(std::shared_ptr<OCResource> resource)
{
std::string resourceURI;
std::string hostAddress;
}
else if (resourceURI == "/core/bookmark")
{
- resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
+ resource->observe(ObserveType::Observe, QueryParamsMap(),
+ &onObserve);
}
// p_platform.bindResource(resourceHandle, foundResourceHandle);
}
}
-void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
+void onGet(const HeaderOptions& opt, const OCRepresentation &rep,
+ const int eCode)
{
- // std::vector<OCRepresentation> children = rep.getChildren();
-
- // cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
- // for( auto iter = children.begin(); iter != children.end(); ++iter )
- // {
- // lights.push_back((*iter).getUri());
- // cout << "\tURI :: " << (*iter).getUri() << endl;
- // }
+ cout << "\nonGet" << endl;
+ if (eCode == OC_STACK_OK)
+ cout << "\tResult is OK." << endl;
+ else
+ cout << "\tInvalid parameter." << endl;
}
-void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep,
+ const int eCode)
{
- printf("\nonPut\n");
+ cout << "\nonPut" << endl;
+ if (eCode == OC_STACK_OK)
+ cout << "\tResult is OK." << endl;
+ else
+ cout << "\tInvalid parameter." << endl;
}
-void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep,
+ const int eCode)
{
printf("\nonPost\n");
ActionSet *actionset = thingsMgr->getActionSetfromString(plainText);
if (actionset != NULL)
{
- cout << endl << "\tACTIONSET NAME :: " << actionset->actionsetName << endl;
+ cout << endl << "\tACTIONSET NAME :: "
+ << actionset->actionsetName << endl;
for (auto actIter = actionset->listOfAction.begin();
actIter != actionset->listOfAction.end(); ++actIter)
{
cout << "\t\tTARGET :: " << (*actIter)->target << endl;
for (auto capaIter = (*actIter)->listOfCapability.begin();
- capaIter != (*actIter)->listOfCapability.end(); ++capaIter)
+ capaIter != (*actIter)->listOfCapability.end();
+ ++capaIter)
{
- cout << "\t\t\t" << (*capaIter)->capability << " :: " << (*capaIter)->status
- << endl;
+ cout << "\t\t\t" << (*capaIter)->capability << " :: "
+ << (*capaIter)->status << endl;
}
}
}
if (g_resource)
{
- g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), &onPost);
+ g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
+ &onPost);
}
}
if (g_resource)
{
- g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), &onPost);
+ g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
+ &onPost);
}
}
-void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, const int& eCode,
- const int& sequenceNumber)
+void Scheduled_AllbulbOff()
+{
+ thingsMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
+}
+void Scheduled_AllbulbOffEx()
+{
+ thingsMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", 10, &onPost);
+}
+void CancelScheduled_AllBulbOff()
+{
+ thingsMgr->cancelActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
+}
+void Recursive_allBulbOn()
+{
+ thingsMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
+}
+void Recursive_allBulbOnEx()
+{
+ thingsMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", 10, &onPost);
+}
+
+void CancelRecursive_allBulbOn()
+{
+
+ thingsMgr->cancelActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
+}
+
+void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
+ const int& eCode, const int& sequenceNumber)
{
if (eCode == OC_STACK_OK)
{
action->listOfCapability.push_back(capa);
allBulbOff->listOfAction.push_back(action);
}
- // actionsetDesc = thingsMgr->getStringFromActionSet(allBulbOff);
-
- // cout << "ActionSet :: " << actionsetDesc << endl;
-
- // OCRepresentation rep;
- // rep.setValue("ActionSet", actionsetDesc);
-
if (g_resource)
{
thingsMgr->addActionSet(g_resource, allBulbOff, onPut);
- // g_resource->put("a.collection", GROUP_INTERFACE, rep,
- // QueryParamsMap(), &onPut);
}
delete allBulbOff;
action->listOfCapability.push_back(capa);
allBulbOff->listOfAction.push_back(action);
}
- // actionsetDesc = thingsMgr->getStringFromActionSet(allBulbOff);
+ if (g_resource)
+ {
+ thingsMgr->addActionSet(g_resource, allBulbOff, onPut);
+ }
- // cout << "ActionSet :: " << actionsetDesc << endl;
+ delete allBulbOff;
+}
- // OCRepresentation rep;
- // rep.setValue("ActionSet", actionsetDesc);
+void createScheduledActionSet_AllBulbOff()
+{
+ string actionsetDesc;
+ ActionSet *allBulbOff = new ActionSet();
+ allBulbOff->type = OIC::ACTIONSET_TYPE::SCHEDULED;
+ allBulbOff->actionsetName = "AllBulbOffScheduledCall";
+
+ printf("ENTER(YYYY-MM-DD hh:mm:ss) :: ");
+ int res = scanf("%d-%d-%d %d:%d:%d", &allBulbOff->mTime.tm_year,
+ &allBulbOff->mTime.tm_mon, &allBulbOff->mTime.tm_mday,
+ &allBulbOff->mTime.tm_hour, &allBulbOff->mTime.tm_min,
+ &allBulbOff->mTime.tm_sec);
+ if( res < 0 )
+ {
+ printf("Invalid Input. try again.");
+ return;
+ }
+
+ allBulbOff->setDelay(allBulbOff->getSecondsFromAbsoluteTime());
+ printf("DELAY :: %ld\n", allBulbOff->getSecondsFromAbsoluteTime());
+ for (auto iter = lights.begin(); iter != lights.end(); ++iter)
+ {
+ Action *action = new Action();
+ action->target = (*iter);
+
+ Capability *capa = new Capability();
+ capa->capability = "power";
+ capa->status = "off";
+
+ action->listOfCapability.push_back(capa);
+ allBulbOff->listOfAction.push_back(action);
+ }
if (g_resource)
{
thingsMgr->addActionSet(g_resource, allBulbOff, onPut);
- // g_resource->put("a.collection", GROUP_INTERFACE, rep,
- // QueryParamsMap(), &onPut);
}
delete allBulbOff;
}
+void createRecursiveActionSet_AllBulbOn()
+{
+ string actionsetDesc;
+ ActionSet *allBulbOn = new ActionSet();
+ allBulbOn->type = OIC::ACTIONSET_TYPE::RECURSIVE;
+
+ allBulbOn->actionsetName = "AllBulbOnRecursiveCall";
+ allBulbOn->mTime.tm_year = 0;
+ allBulbOn->mTime.tm_mon = 0;
+ allBulbOn->mTime.tm_mday = 0;
+ allBulbOn->mTime.tm_hour = 0;
+ allBulbOn->mTime.tm_min = 0;
+ allBulbOn->mTime.tm_sec = 5;
+
+ allBulbOn->setDelay(allBulbOn->getSecAbsTime());
+
+ for (auto iter = lights.begin(); iter != lights.end(); ++iter)
+ {
+ Action *action = new Action();
+ action->target = (*iter);
+
+ Capability *capa = new Capability();
+ capa->capability = "power";
+ capa->status = "on";
+
+ action->listOfCapability.push_back(capa);
+ allBulbOn->listOfAction.push_back(action);
+ }
+ if (g_resource)
+ {
+ thingsMgr->addActionSet(g_resource, allBulbOn, onPut);
+ }
+
+ delete allBulbOn;
+}
+
int main()
{
PlatformConfig config
- { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
+ { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0,
+ OC::QualityOfService::LowQos };
try
{
OCPlatform::Configure(config);
// Find lights for group creation.
- vector< string > types;
+ vector<string> types;
types.push_back("core.light");
thingsMgr->findCandidateResources(types, &foundResources, 5);
- OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
- resourceInterface, NULL,
- //&entityHandler, // entityHandler
- OC_DISCOVERABLE);
+ OCPlatform::registerResource(resourceHandle, resourceURI,
+ resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE);
cout << "registerResource is called." << endl;
int n;
cout << endl;
- cout << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTIONSET(ALLBULBON)"
- << "3 :: EXECUTE ACTIONSET(ALLBULBOFF)" << endl;
- cout << "4 :: GET ACTIONSET 5 :: DELETE ACTIONSET 6 :: QUIT" << endl;
- cout << "9 :: FIND GROUP 0 :: FIND BOOKMARK TO OBSERVE" << endl;
+ cout << "1 :: CREATE ACTIONSET" << endl;
+ cout << "2 :: EXECUTE ACTIONSET(ALLBULBON)" << endl;
+ cout << "3 :: EXECUTE ACTIONSET(ALLBULBOFF)" << endl;
+ cout << "4 :: CREATE ACTIONSET(R_ALLBULBON)" << endl;
+ cout << "\t41 :: EXECUTE ACTIONSET 42 :: CANCEL ACTIONSET" << endl;
+ cout << "5 :: CREATE ACTIONSET(S_ALLBULBON)" << endl;
+ cout << "\t51 :: EXECUTE ACTIONSET 52 :: CANCEL ACTIONSET" << endl;
+ cout << "6 :: GET ACTIONSET" << endl;
+ cout << "7 :: DELETE ACTIONSET" << endl;
+ cout << "8 :: QUIT" << endl;
+ cout << "9 :: FIND GROUP" << endl;
+ cout << "0 :: FIND BOOKMARK TO OBSERVE"
+ << endl;
fflush(stdin);
cin >> n;
if (n == 9)
{
- OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection",
+ OCPlatform::findResource("",
+ "coap://224.0.1.187/oc/core?rt=a.collection",
&foundResource);
}
else if (n == 0)
{
- OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.bookmark",
+ OCPlatform::findResource("",
+ "coap://224.0.1.187/oc/core?rt=core.bookmark",
&foundResource);
}
else if (n == 1)
{
-
- // Craete Action Set
- // "AllBulbOff"
- //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10";
createActionSet_AllBulbOff();
createActionSet_AllBulbOn();
-
}
else if (n == 2)
{
-
allBulbOn();
- // thingsMgr->executeActionSet(g_resource, "AllBulbOn", onPost);
-
}
else if (n == 3)
{
-
allBulbOff();
- // thingsMgr->executeActionSet(g_resource, "AllBulbOff", onPost);
-
}
else if (n == 4)
{
- // OCRepresentation rep;
-
- // rep.setValue("GetActionSet", std::string("AllBulbOff"));
-
- // if(g_resource)
- // {
- // g_resource->post("a.collection", GROUP_INTERFACE, rep,
- // QueryParamsMap(), &onPost);
- // }
-
- thingsMgr->getActionSet(g_resource, "AllBulbOff", onPost);
+ createRecursiveActionSet_AllBulbOn();
+ }
+ else if (n == 41)
+ {
+ Recursive_allBulbOn();
+ }
+ else if (n == 42)
+ {
+ CancelRecursive_allBulbOn();
+ }
+ // Exampel of
+ else if (n == 43)
+ {
+ Recursive_allBulbOnEx();
}
else if (n == 5)
{
- // OCRepresentation rep;
-
- // rep.setValue("DelActionSet", std::string("AllBulbOff"));
-
- // if(g_resource)
- // {
- // g_resource->put("a.collection", GROUP_INTERFACE, rep,
- // QueryParamsMap(), &onPut);
- // }
- thingsMgr->deleteActionSet(g_resource, "AllBulbOff", onPut);
+ createScheduledActionSet_AllBulbOff();
+ }
+ else if (n == 51)
+ {
+ Scheduled_AllbulbOff();
+ }
+ else if (n == 52)
+ {
+ CancelScheduled_AllBulbOff();
+ }
+ else if (n == 53)
+ {
+ Scheduled_AllbulbOffEx();
}
else if (n == 6)
{
+ thingsMgr->getActionSet(g_resource, "AllBulbOff", onPost);
+ }
+ else if (n == 7)
+ {
+ thingsMgr->deleteActionSet(g_resource, "AllBulbOff", onPut);
+ }
+ else if (n == 8)
+ {
isRun = false;
break;
}
}
catch (OCException& e)
{
-
+ cout << "ERROR :: " << e.reason() << endl;
}
return 0;
TGMROOT=../../../
+OIC_ROOT=${TGMROOT}../..
IOT_BASE=${TGMROOT}../../resource
RST_NAME=.
CEREAL_DIR:= $(DEPEND_DIR)/cereal
CXX_INC := -I../../ -I../../inc/
+CXX_INC += -I${OIC_ROOT}/extlibs/timer
CXX_INC += -I${IOT_BASE}/include/
CXX_INC += -I${IOT_BASE}/oc_logger/include
CXX_INC += -I${IOT_BASE}/csdk/stack/include
# Build flags
######################################################################
linux_sample_env.AppendUnique(CPPPATH = ['include'])
+linux_sample_env.AppendUnique(CPPPATH = ['../../../../../extlibs/timer'])
linux_sample_env.AppendUnique(CPPPATH = ['../../../sdk/inc'])
linux_sample_env.AppendUnique(CPPPATH = ['../../../sdk/src'])
linux_sample_env.AppendUnique(CXXFLAGS = ['-Wall', '-pthread'])
-
TGMROOT=../../../
+OIC_ROOT=${TGMROOT}../..
IOT_BASE=${TGMROOT}../../resource
RST_NAME=.
TARGET1=group
CXX_FLAGS=-std=c++0x -Wall -pthread -DLINUX -ldl
CXX_INC := -I../../ -I../../inc/ -I../../../sdk/inc/ -I../../../sdk/src/
+CXX_INC += -I${OIC_ROOT}/extlibs/timer
CXX_INC += -I${IOT_BASE}/include/
CXX_INC += -I${IOT_BASE}/oc_logger/include
CXX_INC += -I${IOT_BASE}/csdk/stack/include
-#OIC_ROOT=../../../resource/
-IOT_BASE=../../../../../resource
+OIC_ROOT=../../../../..
+IOT_BASE=${OIC_ROOT}/resource
#BOOST=${BOOST_DIR}
RST_NAME=.
CXX_INC := -I../../ -I../../inc/ -I../../src/
+CXX_INC += -I${OIC_ROOT}/extlibs/timer
CXX_INC += -I${IOT_BASE}/include/
CXX_INC += -I${IOT_BASE}/oc_logger/include
CXX_INC += -I${IOT_BASE}/csdk/stack/include
#include <string>
#include <vector>
+#include <cstdio>
+#include <iostream>
+
+#include <ctime>
+
+#include <timer.h>
using namespace std;
-namespace OIC{
- class Capability
- {
- public:
- std::string capability;
- std::string status;
- };
+namespace OIC
+{
+enum ACTIONSET_TYPE
+{
+ NONE = 0, SCHEDULED, RECURSIVE
+};
+
+typedef tm OCTime;
+
+class Time
+{
+public:
+ long int mDelay;
+ OCTime mTime;
+ ACTIONSET_TYPE type;
+
+ Time();
+ ~Time();
+
+ void setTime(OCTime t);
+ void setTime(unsigned int yy, unsigned int mm, unsigned int dd,
+ unsigned int h, unsigned int m, unsigned int s,
+ int dayoftheweek);
+ void setDayOfWeekForRecursive(int day);
+ unsigned int getYear();
+ unsigned int getMonth();
+ unsigned int getDay();
+ unsigned int getHour();
+ unsigned int getMin();
+ unsigned int getSec();
+ long int getSecondsFromAbsoluteTime();
+ long int getSecAbsTime();
+ long int getSecondsForWeeklySchedule();
+ void setDelay(long int seconds);
+ std::string toString() const;
+};
+
+class Capability
+{
+public:
+ std::string capability;
+ std::string status;
+};
- class Action
- {
- public:
- Action() :
- target("")
- {
- }
- ~Action()
- {
- listOfCapability.clear();
- }
- std::string target;
+class Action
+{
+public:
+ Action();
+ ~Action();
- std::vector< Capability* > listOfCapability;
- };
+ std::string target;
+ std::vector<Capability*> listOfCapability;
+};
- class ActionSet
- {
- public:
- ActionSet() :
- actionsetName("")
- {
- }
- ~ActionSet()
- {
- listOfAction.clear();
- }
- std::string actionsetName;
+class ActionSet: public Time
+{
+public:
+ /**
+ * Constructor for ActionSet
+ */
+ ActionSet();
+ /**
+ * Virtual destructor for ActionSet
+ */
+ ~ActionSet();
- std::vector< Action* > listOfAction;
- };
+ std::string actionsetName;
+ std::vector<Action*> listOfAction;
+};
}
-#endif
+#endif
std::string actionsetName, PostCallback cb);
/**
+ * API for executing the Action Set.
+ * Callback is called when the response of POST operation arrives.
+ *
+ * @param resource - resource pointer of the group resource
+ * @param actionsetName - Action Set name for executing the Action set
+ * @param delay - waiting time for until action set run.
+ * @param callback - callback for POST operation.
+ *
+ * @return OCStackResult - return value of this API.
+ * It returns OC_STACK_OK if success.
+ *
+ * NOTE: OCStackResult is defined in ocstack.h.
+ */
+ OCStackResult executeActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, long int delay, PostCallback cb);
+
+ /**
+ * API for canceling the Action Set.
+ * Callback is called when the response of POST operation arrives.
+ *
+ * @param resource - resource pointer of the group resource
+ * @param actionsetName - Action Set name for executing the Action set
+ * @param callback - callback for POST operation.
+ *
+ * @return OCStackResult - return value of this API.
+ * It returns OC_STACK_OK if success.
+ *
+ * NOTE: OCStackResult is defined in ocstack.h.
+ */
+ OCStackResult cancelActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, PostCallback cb);
+ /**
* API for reading the Action Set.
* Callback is called when the response of GET operation arrives.
*
--- /dev/null
+//******************************************************************
+//
+// Copyright 2014 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "ActionSet.h"
+using namespace std;
+
+namespace OIC
+{
+Time::Time()
+{
+ setTime(0, 0, 0, 0, 0, 0, 0);
+}
+
+Time::~Time()
+{
+}
+
+void Time::setTime(OCTime t)
+{
+ mTime = t;
+}
+void Time::setTime(unsigned int yy, unsigned int mm, unsigned int dd,
+ unsigned int h, unsigned int m, unsigned int s,
+ int dayoftheweek = 0)
+{
+ yy -= 1900;
+ mm -= 1;
+
+ mDelay = 0;
+ mTime.tm_year = yy;
+ mTime.tm_mon = mm;
+ mTime.tm_mday = dd;
+
+ mTime.tm_hour = h;
+ mTime.tm_min = m;
+ mTime.tm_sec = s;
+
+ mTime.tm_wday = (unsigned int) dayoftheweek;
+ type = NONE;
+}
+void Time::setDayOfWeekForRecursive(int day)
+{
+ if (day != -1)
+ type = RECURSIVE;
+ else
+ return;
+
+ setTime(0, 0, 0, 0, 0, 0, day);
+}
+unsigned int Time::getYear()
+{
+ return mTime.tm_year;
+}
+unsigned int Time::getMonth()
+{
+ return mTime.tm_mon;
+}
+unsigned int Time::getDay()
+{
+ return mTime.tm_mday;
+}
+unsigned int Time::getHour()
+{
+ return mTime.tm_hour;
+}
+unsigned int Time::getMin()
+{
+ return mTime.tm_min;
+}
+unsigned int Time::getSec()
+{
+ return mTime.tm_sec;
+}
+long int Time::getSecondsFromAbsoluteTime()
+{
+ if(mTime.tm_year > 1900)
+ mTime.tm_year -= 1900;
+
+ mTime.tm_mon -= 1;
+
+ return getSecondsFromAbsTime(&mTime);
+}
+long int Time::getSecAbsTime()
+{
+ return getSeconds(&mTime);
+}
+long int Time::getSecondsForWeeklySchedule()
+{
+ if(mTime.tm_year > 1900)
+ mTime.tm_year -= 1900;
+
+ mTime.tm_mon -= 1;
+ return getRelativeIntervalOfWeek(&mTime);
+}
+
+void Time::setDelay(long int seconds)
+{
+ if(type != NONE)
+ {
+ mDelay = seconds;
+ }
+}
+
+std::string Time::toString() const
+{
+ char temp[25] = { 0 };
+ // It is shown format which required of scheduled/recursive group action time.
+ // " [delay] [type of actionset] "
+ snprintf(temp, sizeof(temp) / sizeof(char),
+ "%ld %d", mDelay, (unsigned int) type);
+ return std::string(temp);
+}
+
+
+
+
+
+
+
+
+
+
+Action::Action() :
+ target("")
+{
+}
+Action::~Action()
+{
+ listOfCapability.clear();
+}
+
+
+
+
+
+
+
+
+
+ActionSet::ActionSet() :
+ actionsetName("")
+{
+}
+ActionSet::~ActionSet()
+{
+ listOfAction.clear();
+}
+}
message = newActionSet->actionsetName;
message.append("*");
+ message.append(newActionSet->toString());
+ message.append("*");
for (auto iterAction = newActionSet->listOfAction.begin();
iterAction != newActionSet->listOfAction.end(); iterAction++)
{
goto exit;
}
+ if (token != NULL)
+ {
+ sscanf(token, "%ld %d", &actionset->mDelay, (int*)&actionset->type);
+
+ token = strtok_r(NULL, ACTION_DELIMITER, &plainPtr);
+ }
+ else
+ {
+ goto exit;
+ }
+
while (token)
{
char *descPtr = NULL;
strcpy(desc, token);
token = strtok_r(desc, DESC_DELIMITER, &descPtr);
- // cout << "desc :: " << token << endl;
while (token != NULL)
{
char *attrPtr = NULL;
strcpy(attr, token);
- // cout << "attr :: " << attr << endl;
-
token = strtok_r(attr, ATTR_DELIMITER, &attrPtr);
while (token != NULL)
{
if ((resource != NULL) && (newActionSet != NULL))
{
std::string message = getStringFromActionSet(newActionSet);
- OCRepresentation rep;
+ OCRepresentation rep;
rep.setValue("ActionSet", message);
-
return resource->put(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
QueryParamsMap(), cb);
}
}
}
+OCStackResult GroupManager::executeActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, long int delay, PostCallback cb)
+{
+ if(delay == 0 )
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (resource != NULL)
+ {
+ std::string value = actionsetName;
+ value.append("*");
+ value.append(std::to_string(delay));
+
+ OCRepresentation rep;
+ rep.setValue("DoScheduledAction", value);
+ return resource->post(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
+ QueryParamsMap(), cb);
+ }
+ else
+ {
+ return OC_STACK_ERROR;
+ }
+}
+
+OCStackResult GroupManager::cancelActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, PostCallback cb)
+{
+ if (resource != NULL)
+ {
+ OCRepresentation rep;
+
+ rep.setValue("CancelAction", actionsetName);
+ return resource->post(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
+ QueryParamsMap(), cb);
+ }
+ else
+ {
+ return OC_STACK_ERROR;
+ }
+}
+
OCStackResult GroupManager::getActionSet(std::shared_ptr< OCResource > resource,
std::string actionsetName, PostCallback cb)
{
OCStackResult subscribeCollectionPresence(std::shared_ptr< OCResource > resource,
CollectionPresenceCallback);
- OCStackResult bindResourceToGroup(OCResourceHandle& childHandle, std::shared_ptr< OCResource > resource, OCResourceHandle& collectionHandle);
+ OCStackResult bindResourceToGroup(OCResourceHandle& childHandle,
+ std::shared_ptr< OCResource > resource, OCResourceHandle& collectionHandle);
private:
const ActionSet* newActionSet, PutCallback cb);
OCStackResult executeActionSet(std::shared_ptr< OCResource > resource,
std::string actionsetName, PostCallback cb);
+ OCStackResult executeActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, long int delay, PostCallback cb);
+ OCStackResult cancelActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, PostCallback cb);
OCStackResult getActionSet(std::shared_ptr< OCResource > resource, std::string actionsetName,
PostCallback cb);
OCStackResult deleteActionSet(std::shared_ptr< OCResource > resource, std::string actionsetName,
{
return g_groupManager->executeActionSet(resource, actionsetName, cb);
}
+ OCStackResult ThingsManager::executeActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, long int delay, PostCallback cb)
+ {
+ return g_groupManager->executeActionSet(resource, actionsetName, delay, cb);
+ }
+ OCStackResult ThingsManager::cancelActionSet(std::shared_ptr< OCResource > resource,
+ std::string actionsetName, PostCallback cb)
+ {
+ return g_groupManager->cancelActionSet(resource, actionsetName, cb);
+ }
OCStackResult ThingsManager::getActionSet(std::shared_ptr< OCResource > resource,
std::string actionsetName, GetCallback cb)
{