1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #define _POSIX_C_SOURCE 200112L
27 #include "oic_malloc.h"
28 #include "oic_string.h"
29 #include "occollection.h"
37 #define TAG PCF("OICGROUP")
39 #define DESC_DELIMITER "\""
40 #define ACTION_DELIMITER "*"
41 #define ATTR_DELIMITER "|"
42 #define ATTR_ASSIGN "="
44 // Definitions for operations related to actions
45 #define DO_ACTION "DoAction"
46 #define GET_ACTIONSET "GetActionSet"
47 #define ACTIONSET "ActionSet"
48 #define DELETE_ACTIONSET "DelActionSet"
50 #define OIC_ACTION_PREFIX "{\"oic\":[{\"rep\":{"
51 #define VARIFY_POINTER_NULL(pointer, result, toExit) \
54 result = OC_STACK_NO_MEMORY;\
57 #define VARIFY_PARAM_NULL(pointer, result, toExit) \
60 result = OC_STACK_INVALID_PARAM;\
64 #define OCFREE(pointer) \
76 NONE = 0, SCHEDULED, RECURSIVE
79 typedef struct scheduledresourceinfo
82 OCActionSet *actionset;
86 OCServerRequest *ehRequest;
89 struct scheduledresourceinfo* next;
90 } ScheduledResourceInfo;
92 ScheduledResourceInfo *scheduleResourceList = NULL;
94 void AddScheduledResource(ScheduledResourceInfo **head,
95 ScheduledResourceInfo* add)
97 OC_LOG(INFO, TAG, PCF("AddScheduledResource Entering..."));
100 pthread_mutex_lock(&lock);
102 ScheduledResourceInfo *tmp = NULL;
119 pthread_mutex_unlock(&lock);
123 ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
125 OC_LOG(INFO, TAG, PCF("GetScheduledResource Entering..."));
128 pthread_mutex_lock(&lock);
133 ScheduledResourceInfo *tmp = NULL;
148 diffTm = timespec_diff(tmp->time, t_now);
150 diffTm = timespec_diff(tmp->time, t_now);
153 if (diffTm <= (time_t) 0)
155 OC_LOG(INFO, TAG, PCF("return Call INFO."));
165 pthread_mutex_unlock(&lock);
169 OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
174 ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
176 OC_LOG(INFO, TAG, PCF("GetScheduledResourceByActionSetName Entering..."));
179 pthread_mutex_lock(&lock);
181 ScheduledResourceInfo *tmp = NULL;
188 if (strcmp(tmp->actionset->actionsetName, setName) == 0)
190 OC_LOG(INFO, TAG, PCF("return Call INFO."));
199 pthread_mutex_unlock(&lock);
203 OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
208 void RemoveScheduledResource(ScheduledResourceInfo **head,
209 ScheduledResourceInfo* del)
212 pthread_mutex_lock(&lock);
214 OC_LOG(INFO, TAG, PCF("RemoveScheduledResource Entering..."));
215 ScheduledResourceInfo *tmp = NULL;
224 *head = (*head)->next;
229 while (tmp->next && (tmp->next != del))
235 tmp->next = del->next;
241 pthread_mutex_unlock(&lock);
245 typedef struct aggregatehandleinfo
247 OCServerRequest *ehRequest;
249 OCResource *collResource;
251 struct aggregatehandleinfo *next;
254 ClientRequestInfo *clientRequstList = NULL;
256 void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
258 ClientRequestInfo *tmp = NULL;
276 ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head,
279 ClientRequestInfo *tmp = NULL;
287 if (tmp->required == handle)
300 void RemoveClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* del)
302 ClientRequestInfo *tmp = NULL;
309 *head = (*head)->next;
314 while (tmp->next && (tmp->next != del))
320 tmp->next = del->next;
325 void AddCapability(OCCapability** head, OCCapability* node)
327 OCCapability *pointer = *head;
334 while (pointer->next != NULL)
336 pointer = pointer->next;
339 pointer->next = node;
343 void AddAction(OCAction** head, OCAction* node)
345 OCAction *pointer = *head;
353 while (pointer->next != NULL)
355 pointer = pointer->next;
358 pointer->next = node;
362 OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node)
364 OCActionSet *pointer = *head;
365 OCActionSet *prev = NULL;
368 return OC_STACK_ERROR;
377 while (pointer != NULL)
379 // check the uniqeness of actionsetname.
380 if (strcmp(pointer->actionsetName, node->actionsetName) == 0)
382 return OC_STACK_ERROR;
386 pointer = pointer->next;
395 void DeleteCapability(OCCapability *del)
397 OCFREE(del->capability)
398 del->capability = NULL;
404 void DeleteAction(OCAction** action)
406 OCCapability* pointer = (*action)->head;
407 OCCapability* pDel = NULL;
412 pointer = pointer->next;
414 DeleteCapability(pDel);
416 OCFREE((*action)->resourceUri)
417 (*action)->next = NULL;
421 void DeleteActionSet(OCActionSet** actionset)
423 if(*actionset == NULL)
426 OCAction* pointer = (*actionset)->head;
427 OCAction* pDel = NULL;
432 pointer = pointer->next;
436 // (*actionset)->head = NULL;
437 OCFREE((*actionset)->actionsetName)
441 OCStackResult FindAndDeleteActionSet(OCResource **resource,
442 const char * actionsetName)
444 if (*resource != NULL)
446 OCActionSet *pointer = NULL;
447 OCActionSet *pDel = NULL;
449 pointer = (*resource)->actionsetHead;
453 return OC_STACK_ERROR;
457 if (strcmp(pointer->actionsetName, actionsetName) == 0)
459 if (pointer->next != NULL)
460 (*resource)->actionsetHead = pointer->next;
462 (*resource)->actionsetHead = NULL;
464 DeleteActionSet(&pointer);
466 else if (pointer->next != NULL)
470 if (pointer->next != NULL)
472 if (strcmp(pointer->next->actionsetName, actionsetName)
475 pDel = pointer->next;
476 pointer->next = pointer->next->next;
478 DeleteActionSet(&pDel);
481 pointer = pointer->next;
489 return OC_STACK_ERROR;
492 OCStackResult DeleteActionSets(OCResource** resource)
494 OCActionSet *pointer = (*resource)->actionsetHead;
495 OCActionSet *pDel = pointer;
500 pointer = pointer->next;
502 DeleteActionSet(&pDel);
506 (*resource)->actionsetHead = NULL;
510 OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
511 OCActionSet** actionset)
513 OCActionSet *pointer = head;
517 if (strcmp(pointer->actionsetName, actionName) == 0)
519 *actionset = pointer;
523 pointer = pointer->next;
526 return OC_STACK_ERROR;
530 OCStackResult ExtractKeyValueFromRequest(char *request, char **key,
533 OCStackResult result = OC_STACK_OK;
536 char* pRequest = (char *) request + strlen(OIC_ACTION_PREFIX);
537 char* iterToken, *iterTokenPtr;
539 iterToken = (char *) strtok_r(pRequest, ":", &iterTokenPtr);
540 VARIFY_POINTER_NULL(iterToken, result, exit);
541 length = strlen(iterToken) + 1;
543 *key = (char *) OICMalloc(length);
544 VARIFY_POINTER_NULL(*key, result, exit)
546 strncpy(*key, iterToken + 1, length);
547 ((*key)[((length - 1) - 2)]) = '\0';
549 iterToken = (char *) strtok_r(NULL, "}", &iterTokenPtr);
550 VARIFY_POINTER_NULL(iterToken, result, exit);
551 length = strlen(iterToken) + 1;
553 *value = (char *) OICMalloc(length);
554 VARIFY_POINTER_NULL(*value, result, exit)
556 strncpy(*value, iterToken + 1, length);
557 ((*value)[((length - 1) - 2)]) = '\0';
560 if (result != OC_STACK_OK)
569 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
572 char *token, *tokenPtr;
573 OCStackResult result = OC_STACK_OK;
575 token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
576 *setName = (char *) OICMalloc(strlen(token) + 1);
577 VARIFY_POINTER_NULL(*setName, result, exit)
578 VARIFY_PARAM_NULL(token, result, exit)
579 strncpy(*setName, token, strlen(token) + 1);
581 token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
582 VARIFY_POINTER_NULL(pa, result, exit)
583 VARIFY_PARAM_NULL(token, result, exit)
593 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
595 OCStackResult result = OC_STACK_OK;
597 char *iterToken = NULL, *iterTokenPtr = NULL;
598 char *descIterToken = NULL, *descIterTokenPtr = NULL;
599 char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
600 char *desc = NULL, *attr = NULL;
601 char *key = NULL, *value = NULL;
603 OCAction *action = NULL;
604 OCCapability *capa = NULL;
606 OC_LOG(INFO, TAG, PCF("Build ActionSet Instance."));
608 *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
609 VARIFY_POINTER_NULL(*set, result, exit)
611 iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
614 memset(*set, 0, sizeof(OCActionSet));
615 (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
616 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
617 VARIFY_PARAM_NULL(iterToken, result, exit)
618 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
620 // Time info. for Scheduled/Recursive Group action.
621 // d is meant Day of the week.
622 // T is meant ActionType.
623 // yyyy-mm-dd hh:mm:ss d
624 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
625 VARIFY_PARAM_NULL(iterToken, result, exit)
627 sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type);
630 OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
632 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
635 desc = (char *) OICMalloc(strlen(iterToken) + 1);
636 VARIFY_POINTER_NULL(desc, result, exit)
637 VARIFY_PARAM_NULL(desc, result, exit)
638 strncpy(desc, iterToken, strlen(iterToken) + 1);
639 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
641 while (descIterToken)
643 attr = (char *) OICMalloc(strlen(descIterToken) + 1);
644 VARIFY_POINTER_NULL(attr, result, exit)
645 VARIFY_PARAM_NULL(descIterToken, result, exit)
646 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
648 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
650 key = (char *) OICMalloc(strlen(attrIterToken) + 1);
651 VARIFY_POINTER_NULL(key, result, exit)
652 VARIFY_PARAM_NULL(attrIterToken, result, exit)
653 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
655 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
657 value = (char *) OICMalloc(strlen(attrIterToken) + 1);
658 VARIFY_POINTER_NULL(value, result, exit)
659 VARIFY_PARAM_NULL(attrIterToken, result, exit)
660 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
662 if (strcmp(key, "uri") == 0)
664 OC_LOG(INFO, TAG, PCF("Build OCAction Instance."));
668 OICFree(action->resourceUri);
671 action = (OCAction*) OICMalloc(sizeof(OCAction));
672 VARIFY_POINTER_NULL(action, result, exit)
673 memset(action, 0, sizeof(OCAction));
674 action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
675 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
676 VARIFY_PARAM_NULL(value, result, exit)
677 strncpy(action->resourceUri, value, strlen(value) + 1);
681 if ((key != NULL) && (value != NULL))
683 OC_LOG(INFO, TAG, PCF("Build OCCapability Instance."));
685 capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
686 VARIFY_POINTER_NULL(capa, result, exit)
687 memset(capa, 0, sizeof(OCCapability));
689 capa->capability = (char *) OICMalloc(strlen(key) + 1);
690 VARIFY_POINTER_NULL(capa->capability, result, exit)
691 VARIFY_PARAM_NULL(key, result, exit)
692 strncpy(capa->capability, key, strlen(key) + 1);
694 capa->status = (char *) OICMalloc(strlen(value) + 1);
695 VARIFY_POINTER_NULL(capa->status, result, exit)
696 VARIFY_PARAM_NULL(value, result, exit)
697 strncpy(capa->status, value, strlen(value) + 1);
699 VARIFY_POINTER_NULL(action, result, exit)
701 AddCapability(&action->head, capa);
709 descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
713 AddAction(&(*set)->head, action);
714 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
732 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
734 // Can't use the macros here as they are hardcoded to 'exit' and will
735 // result in dereferencing a null pointer.
736 if (!actionset || !desc)
738 return OC_STACK_INVALID_PARAM;
740 char temp[1024] = { 0 };
741 size_t remaining = sizeof(temp) - 1;
742 OCStackResult res = OC_STACK_ERROR;
744 OCAction *action = actionset->head;
746 if (remaining >= strlen(actionset->actionsetName) + 1)
748 strcat(temp, actionset->actionsetName);
749 remaining -= strlen(actionset->actionsetName);
750 strcat(temp, ACTION_DELIMITER);
755 res = OC_STACK_ERROR;
759 while (action != NULL)
761 if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
763 res = OC_STACK_ERROR;
766 strcat(temp, "uri=");
767 remaining -= strlen("uri=");
768 strcat(temp, action->resourceUri);
769 remaining -= strlen(action->resourceUri);
773 OCCapability *capas = action->head;
774 while (capas != NULL)
776 if (remaining < (strlen(capas->capability)
777 + 1 + strlen(capas->status)))
779 res = OC_STACK_ERROR;
783 strcat(temp, capas->capability);
784 remaining -= strlen(capas->capability);
787 strcat(temp, capas->status);
788 remaining -= strlen(capas->status);
795 res = OC_STACK_ERROR;
802 action = action->next;
805 if (remaining < strlen(ACTION_DELIMITER))
807 res = OC_STACK_ERROR;
810 strcat(temp, ACTION_DELIMITER);
815 *desc = OICStrdup(temp);
816 VARIFY_POINTER_NULL(*desc, res, exit);
825 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
826 OCClientResponse* clientResponse)
829 (void)clientResponse;
830 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
832 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
838 unsigned char *responseJson = NULL;
839 // TODO: Figure out what this does, change implementation
840 //responseJson = (unsigned char *) OICMalloc(
841 // (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
844 if( responseJson == NULL )
845 return OC_STACK_DELETE_TRANSACTION;
847 // We need the body of response.
848 // Copy the body from the response
850 //strcpy((char *) responseJson,
851 // ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN));
852 //idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN;
853 // And insert NULL at the end of body.
854 (responseJson[idx]) = 0;
856 OCEntityHandlerResponse response = { 0 };
857 response.ehResult = OC_EH_OK;
858 // TODO: Removing payload size, waht goes here?
859 // response.payload = (char*)responseJson;
860 //response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
861 response.persistentBufferFlag = 0;
862 response.requestHandle = (OCRequestHandle) info->ehRequest;
863 response.resourceHandle = (OCResourceHandle) info->collResource;
865 OCDoResponse(&response);
867 RemoveClientRequestInfo(&clientRequstList, info);
872 return OC_STACK_KEEP_TRANSACTION;
875 void ActionSetCD(void *context)
880 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
883 OCStackResult ret = OC_STACK_ERROR;
890 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
891 json = cJSON_CreateObject();
893 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
895 OCCapability* pointerCapa = action->head;
898 cJSON_AddStringToObject(body, pointerCapa->capability,
899 pointerCapa->status);
900 pointerCapa = pointerCapa->next;
903 jsonStr = cJSON_PrintUnformatted(json);
905 jsonLen = strlen(jsonStr);
906 if (jsonLen < *remaining)
908 strcat((char*) bufferPtr, jsonStr);
909 *remaining -= jsonLen;
910 bufferPtr += jsonLen;
920 unsigned int GetNumOfTargetResource(OCAction *actionset)
922 int numOfResource = 0;
924 OCAction *pointerAction = actionset;
926 while (pointerAction != NULL)
929 pointerAction = pointerAction->next;
932 return numOfResource;
936 #define DEFAULT_CONTEXT_VALUE 0x99
938 OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
939 const unsigned char *action)
944 // TODO: disabled since this is no longer compatible
945 return OC_STACK_NOTIMPL;
948 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
949 OCServerRequest* requestHandle)
951 OCStackResult result = OC_STACK_ERROR;
952 OCAction *pointerAction = actionset->head;
954 while (pointerAction != NULL)
956 unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
957 unsigned char* actionDescPtr = actionDesc;
958 uint16_t remaining = MAX_RESPONSE_LENGTH;
960 strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX,
961 strlen((const char *) OC_JSON_PREFIX) + 1);
962 BuildActionJSON(pointerAction, actionDescPtr, &remaining);
963 strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX,
964 strlen((const char *) OC_JSON_SUFFIX));
966 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
967 sizeof(ClientRequestInfo));
970 return OC_STACK_NO_MEMORY;
972 memset(info, 0, sizeof(ClientRequestInfo));
974 info->collResource = resource;
975 info->ehRequest = requestHandle;
977 result = SendAction(&info->required, pointerAction->resourceUri,
979 if (result != OC_STACK_OK)
985 AddClientRequestInfo(&clientRequstList, info);
987 pointerAction = pointerAction->next;
993 void DoScheduledGroupAction()
995 OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
996 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
1000 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
1003 else if (info->resource == NULL)
1005 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
1008 else if (info->actionset == NULL)
1010 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
1013 else if (info->ehRequest == NULL)
1015 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
1018 #ifndef WITH_ARDUINO
1019 pthread_mutex_lock(&lock);
1021 DoAction(info->resource, info->actionset, info->ehRequest);
1022 #ifndef WITH_ARDUINO
1023 pthread_mutex_unlock(&lock);
1026 if (info->actionset->type == RECURSIVE)
1028 ScheduledResourceInfo *schedule;
1029 schedule = (ScheduledResourceInfo *) OICMalloc(
1030 sizeof(ScheduledResourceInfo));
1034 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1035 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1037 if (info->actionset->timesteps > 0)
1039 #ifndef WITH_ARDUINO
1040 pthread_mutex_lock(&lock);
1042 schedule->resource = info->resource;
1043 schedule->actionset = info->actionset;
1044 schedule->ehRequest = info->ehRequest;
1046 schedule->time = registerTimer(info->actionset->timesteps,
1047 &schedule->timer_id,
1048 &DoScheduledGroupAction);
1050 OC_LOG(INFO, TAG, PCF("Reregisteration."));
1051 #ifndef WITH_ARDUINO
1052 pthread_mutex_unlock(&lock);
1054 AddScheduledResource(&scheduleResourceList, schedule);
1063 RemoveScheduledResource(&scheduleResourceList, info);
1070 OCStackResult BuildCollectionGroupActionJSONResponse(
1071 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1072 OCEntityHandlerRequest *ehRequest)
1074 OCStackResult stackRet = OC_STACK_ERROR;
1076 OC_LOG(INFO, TAG, PCF("Group Action is requested."));
1077 // if (stackRet == OC_STACK_OK)
1079 char *doWhat = NULL;
1080 char *details = NULL;
1082 size_t bufferLength = 0;
1083 unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
1085 OCResource * collResource = (OCResource *) ehRequest->resource;
1089 stackRet = OC_STACK_NOTIMPL;
1091 //stackRet = ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload,
1092 // &doWhat, &details);
1094 if(stackRet != OC_STACK_OK)
1096 OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1100 stackRet = OC_STACK_ERROR;
1105 if (method == OC_REST_PUT)
1107 json = cJSON_CreateObject();
1108 cJSON_AddStringToObject(json, "href", resource->uri);
1109 cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
1111 OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
1113 if (strcmp(doWhat, ACTIONSET) == 0)
1115 OCActionSet *actionSet = NULL;
1116 stackRet = BuildActionSetFromString(&actionSet, details);
1118 if(stackRet == OC_STACK_OK)
1120 if (actionSet != NULL)
1122 stackRet = AddActionSet(&resource->actionsetHead,
1124 if (stackRet == OC_STACK_ERROR)
1126 if(actionSet != NULL)
1128 DeleteActionSet( &actionSet );
1130 OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
1136 stackRet = OC_STACK_ERROR;
1140 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1142 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1144 stackRet = OC_STACK_OK;
1148 stackRet = OC_STACK_ERROR;
1152 jsonResponse = cJSON_Print(json);
1155 OICStrcat((char*)buffer, sizeof(buffer), jsonResponse);
1157 bufferLength = strlen((const char *) buffer);
1158 if (bufferLength > 0)
1160 OCEntityHandlerResponse response = { 0 };
1161 if(stackRet == OC_STACK_OK)
1162 response.ehResult = OC_EH_OK;
1164 response.ehResult = OC_EH_ERROR;
1166 //response.payload = (char*)buffer;
1167 //response.payloadSize = bufferLength + 1;
1168 response.persistentBufferFlag = 0;
1169 response.requestHandle =
1170 (OCRequestHandle) ehRequest->requestHandle;
1171 response.resourceHandle = (OCResourceHandle) collResource;
1172 stackRet = OCDoResponse(&response);
1176 if (method == OC_REST_POST)
1178 OCActionSet *actionset = NULL;
1180 json = cJSON_CreateObject();
1181 cJSON_AddStringToObject(json, "href", resource->uri);
1183 if ((strcmp(doWhat, DO_ACTION) == 0)
1184 || (strcmp(doWhat, "DoScheduledAction") == 0))
1186 char *pActionsetName = NULL;
1187 long int delay = -1;
1189 if (strcmp(doWhat, "DoScheduledAction") == 0)
1191 stackRet = ExtractActionSetNameAndDelaytime(details,
1192 &pActionsetName, &delay);
1195 details = pActionsetName;
1199 stackRet = OC_STACK_OK;
1202 if (stackRet == OC_STACK_OK)
1204 if (GetActionSet(details, resource->actionsetHead,
1205 &actionset) != OC_STACK_OK)
1207 OC_LOG(INFO, TAG, PCF("ERROR"));
1208 stackRet = OC_STACK_ERROR;
1211 if (actionset == NULL)
1213 OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
1214 stackRet = OC_STACK_ERROR;
1218 OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
1219 if (actionset->type == NONE)
1221 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1222 actionset->actionsetName);
1223 unsigned int num = GetNumOfTargetResource(
1226 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1227 HandleAggregateResponse;
1228 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1231 DoAction(resource, actionset,
1232 (OCServerRequest*) ehRequest->requestHandle);
1233 stackRet = OC_STACK_OK;
1237 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1238 actionset->actionsetName);
1241 (delay == -1 ? actionset->timesteps : delay);
1243 ScheduledResourceInfo *schedule;
1244 schedule = (ScheduledResourceInfo *) OICMalloc(
1245 sizeof(ScheduledResourceInfo));
1249 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1251 sizeof(ScheduledResourceInfo));
1253 schedule->resource = resource;
1254 schedule->actionset = actionset;
1255 schedule->ehRequest =
1256 (OCServerRequest*) ehRequest->requestHandle;
1260 OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
1261 actionset->timesteps);
1263 schedule->time = registerTimer(delay,
1264 &schedule->timer_id,
1265 &DoScheduledGroupAction);
1267 AddScheduledResource(&scheduleResourceList,
1269 stackRet = OC_STACK_OK;
1273 stackRet = OC_STACK_ERROR;
1280 else if (strcmp(doWhat, "CancelAction") == 0)
1282 ScheduledResourceInfo *info =
1283 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1287 unregisterTimer(info->timer_id);
1289 RemoveScheduledResource(&scheduleResourceList, info);
1290 stackRet = OC_STACK_OK;
1294 stackRet = OC_STACK_ERROR;
1298 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1300 char *plainText = NULL;
1301 OCActionSet *actionset = NULL;
1303 cJSON_AddItemToObject(json, "rep", format =
1304 cJSON_CreateObject());
1305 GetActionSet(details, resource->actionsetHead, &actionset);
1306 if (actionset != NULL)
1308 BuildStringFromActionSet(actionset, &plainText);
1310 if (plainText != NULL)
1312 cJSON_AddStringToObject(format, ACTIONSET, plainText);
1315 stackRet = OC_STACK_OK;
1320 jsonResponse = cJSON_Print(json);
1323 OICStrcat((char*)buffer, sizeof(buffer), jsonResponse);
1325 bufferLength = strlen((const char *) buffer);
1326 if (bufferLength > 0)
1328 OCEntityHandlerResponse response = { 0 };
1329 if(stackRet == OC_STACK_OK)
1330 response.ehResult = OC_EH_OK;
1332 response.ehResult = OC_EH_ERROR;
1334 //response.payload = (char *)buffer;
1335 //response.payloadSize = bufferLength + 1;
1336 response.persistentBufferFlag = 0;
1337 response.requestHandle =
1338 (OCRequestHandle) ehRequest->requestHandle;
1339 response.resourceHandle = (OCResourceHandle) collResource;
1340 stackRet = OCDoResponse(&response);