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
28 #include "occollection.h"
36 #define TAG PCF("OICGROUP")
38 #define DESC_DELIMITER "\""
39 #define ACTION_DELIMITER "*"
40 #define ATTR_DELIMITER "|"
41 #define ATTR_ASSIGN "="
43 // Definitions for operations related to actions
44 #define DO_ACTION "DoAction"
45 #define GET_ACTIONSET "GetActionSet"
46 #define ACTIONSET "ActionSet"
47 #define DELETE_ACTIONSET "DelActionSet"
49 #define OIC_ACTION_PREFIX "{\"oc\":[{\"rep\":{"
50 #define VARIFY_POINTER_NULL(pointer, result, toExit) \
53 result = OC_STACK_NO_MEMORY;\
56 #define VARIFY_PARAM_NULL(pointer, result, toExit) \
59 result = OC_STACK_INVALID_PARAM;\
63 #define OCFREE(pointer) \
75 NONE = 0, SCHEDULED, RECURSIVE
78 typedef struct scheduledresourceinfo
81 OCActionSet *actionset;
85 OCServerRequest *ehRequest;
88 struct scheduledresourceinfo* next;
89 } ScheduledResourceInfo;
91 ScheduledResourceInfo *scheduleResourceList = NULL;
93 void AddScheduledResource(ScheduledResourceInfo **head,
94 ScheduledResourceInfo* add)
96 OC_LOG(INFO, TAG, PCF("AddScheduledResource Entering..."));
99 pthread_mutex_lock(&lock);
101 ScheduledResourceInfo *tmp = NULL;
118 pthread_mutex_unlock(&lock);
122 ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
124 OC_LOG(INFO, TAG, PCF("GetScheduledResource Entering..."));
127 pthread_mutex_lock(&lock);
132 ScheduledResourceInfo *tmp = NULL;
147 diffTm = timespec_diff(tmp->time, t_now);
149 diffTm = timespec_diff(tmp->time, t_now);
152 if (diffTm <= (time_t) 0)
154 OC_LOG(INFO, TAG, PCF("return Call INFO."));
164 pthread_mutex_unlock(&lock);
168 OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
173 ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
175 OC_LOG(INFO, TAG, PCF("GetScheduledResourceByActionSetName Entering..."));
178 pthread_mutex_lock(&lock);
180 ScheduledResourceInfo *tmp = NULL;
187 if (strcmp(tmp->actionset->actionsetName, setName) == 0)
189 OC_LOG(INFO, TAG, PCF("return Call INFO."));
198 pthread_mutex_unlock(&lock);
202 OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
207 void RemoveScheduledResource(ScheduledResourceInfo **head,
208 ScheduledResourceInfo* del)
211 pthread_mutex_lock(&lock);
213 OC_LOG(INFO, TAG, PCF("RemoveScheduledResource Entering..."));
214 ScheduledResourceInfo *tmp = NULL;
223 *head = (*head)->next;
228 while (tmp->next && (tmp->next != del))
234 tmp->next = del->next;
240 pthread_mutex_unlock(&lock);
244 typedef struct aggregatehandleinfo
246 OCServerRequest *ehRequest;
248 OCResource *collResource;
250 struct aggregatehandleinfo *next;
253 ClientRequestInfo *clientRequstList = NULL;
255 void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
257 ClientRequestInfo *tmp = NULL;
275 ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head,
278 ClientRequestInfo *tmp = NULL;
286 if (tmp->required == handle)
299 void RemoveClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* del)
301 ClientRequestInfo *tmp = NULL;
308 *head = (*head)->next;
313 while (tmp->next && (tmp->next != del))
319 tmp->next = del->next;
324 void AddCapability(OCCapability** head, OCCapability* node)
326 OCCapability *pointer = *head;
333 while (pointer->next != NULL)
335 pointer = pointer->next;
338 pointer->next = node;
342 void AddAction(OCAction** head, OCAction* node)
344 OCAction *pointer = *head;
352 while (pointer->next != NULL)
354 pointer = pointer->next;
357 pointer->next = node;
361 OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node)
363 OCActionSet *pointer = *head;
364 OCActionSet *prev = NULL;
367 return OC_STACK_ERROR;
376 while (pointer != NULL)
378 // check the uniqeness of actionsetname.
379 if (strcmp(pointer->actionsetName, node->actionsetName) == 0)
381 return OC_STACK_ERROR;
385 pointer = pointer->next;
394 void DeleteCapability(OCCapability *del)
396 OCFREE(del->capability)
397 del->capability = NULL;
403 void DeleteAction(OCAction** action)
405 OCCapability* pointer = (*action)->head;
406 OCCapability* pDel = NULL;
411 pointer = pointer->next;
413 DeleteCapability(pDel);
415 OCFREE((*action)->resourceUri)
416 (*action)->next = NULL;
420 void DeleteActionSet(OCActionSet** actionset)
422 if(*actionset == NULL)
425 OCAction* pointer = (*actionset)->head;
426 OCAction* pDel = NULL;
431 pointer = pointer->next;
435 // (*actionset)->head = NULL;
436 OCFREE((*actionset)->actionsetName)
440 OCStackResult FindAndDeleteActionSet(OCResource **resource,
441 const char * actionsetName)
443 if (*resource != NULL)
445 OCActionSet *pointer = NULL;
446 OCActionSet *pDel = NULL;
448 pointer = (*resource)->actionsetHead;
452 return OC_STACK_ERROR;
456 if (strcmp(pointer->actionsetName, actionsetName) == 0)
458 if (pointer->next != NULL)
459 (*resource)->actionsetHead = pointer->next;
461 (*resource)->actionsetHead = NULL;
463 DeleteActionSet(&pointer);
465 else if (pointer->next != NULL)
469 if (pointer->next != NULL)
471 if (strcmp(pointer->next->actionsetName, actionsetName)
474 pDel = pointer->next;
475 pointer->next = pointer->next->next;
477 DeleteActionSet(&pDel);
480 pointer = pointer->next;
488 return OC_STACK_ERROR;
491 OCStackResult DeleteActionSets(OCResource** resource)
493 OCActionSet *pointer = (*resource)->actionsetHead;
494 OCActionSet *pDel = pointer;
499 pointer = pointer->next;
501 DeleteActionSet(&pDel);
505 (*resource)->actionsetHead = NULL;
509 OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
510 OCActionSet** actionset)
512 OCActionSet *pointer = head;
516 if (strcmp(pointer->actionsetName, actionName) == 0)
518 *actionset = pointer;
522 pointer = pointer->next;
525 return OC_STACK_ERROR;
529 OCStackResult ExtractKeyValueFromRequest(char *request, char **key,
532 OCStackResult result = OC_STACK_OK;
535 char* pRequest = (char *) request + strlen(OIC_ACTION_PREFIX);
536 char* iterToken, *iterTokenPtr;
538 iterToken = (char *) strtok_r(pRequest, ":", &iterTokenPtr);
539 VARIFY_POINTER_NULL(iterToken, result, exit);
540 length = strlen(iterToken) + 1;
542 *key = (char *) OCMalloc(length);
543 VARIFY_POINTER_NULL(*key, result, exit)
545 strncpy(*key, iterToken + 1, length);
546 ((*key)[((length - 1) - 2)]) = '\0';
548 iterToken = (char *) strtok_r(NULL, "}", &iterTokenPtr);
549 VARIFY_POINTER_NULL(iterToken, result, exit);
550 length = strlen(iterToken) + 1;
552 *value = (char *) OCMalloc(length);
553 VARIFY_POINTER_NULL(*value, result, exit)
555 strncpy(*value, iterToken + 1, length);
556 ((*value)[((length - 1) - 2)]) = '\0';
559 if (result != OC_STACK_OK)
568 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
571 char *token, *tokenPtr;
572 OCStackResult result = OC_STACK_OK;
574 token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
575 *setName = (char *) OCMalloc(strlen(token) + 1);
576 VARIFY_POINTER_NULL(*setName, result, exit)
577 VARIFY_PARAM_NULL(token, result, exit)
578 strncpy(*setName, token, strlen(token) + 1);
580 token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
581 VARIFY_POINTER_NULL(pa, result, exit)
582 VARIFY_PARAM_NULL(token, result, exit)
592 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
594 OCStackResult result = OC_STACK_OK;
596 char *iterToken = NULL, *iterTokenPtr = NULL;
597 char *descIterToken = NULL, *descIterTokenPtr = NULL;
598 char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
599 char *desc = NULL, *attr = NULL;
600 char *key = NULL, *value = NULL;
602 OCAction *action = NULL;
603 OCCapability *capa = NULL;
605 OC_LOG(INFO, TAG, PCF("Build ActionSet Instance."));
607 *set = (OCActionSet*) OCMalloc(sizeof(OCActionSet));
608 VARIFY_POINTER_NULL(*set, result, exit)
610 iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
613 memset(*set, 0, sizeof(OCActionSet));
614 (*set)->actionsetName = (char *) OCMalloc(strlen(iterToken) + 1);
615 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
616 VARIFY_PARAM_NULL(iterToken, result, exit)
617 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
619 // Time info. for Scheduled/Recursive Group action.
620 // d is meant Day of the week.
621 // T is meant ActionType.
622 // yyyy-mm-dd hh:mm:ss d
623 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
624 VARIFY_PARAM_NULL(iterToken, result, exit)
626 sscanf(iterToken, "%ld %d", &(*set)->timesteps, &(*set)->type);
629 OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
631 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
634 desc = (char *) OCMalloc(strlen(iterToken) + 1);
635 VARIFY_POINTER_NULL(desc, result, exit)
636 VARIFY_PARAM_NULL(desc, result, exit)
637 strncpy(desc, iterToken, strlen(iterToken) + 1);
638 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
640 while (descIterToken)
642 attr = (char *) OCMalloc(strlen(descIterToken) + 1);
643 VARIFY_POINTER_NULL(attr, result, exit)
644 VARIFY_PARAM_NULL(descIterToken, result, exit)
645 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
647 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
649 key = (char *) OCMalloc(strlen(attrIterToken) + 1);
650 VARIFY_POINTER_NULL(key, result, exit)
651 VARIFY_PARAM_NULL(attrIterToken, result, exit)
652 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
654 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
656 value = (char *) OCMalloc(strlen(attrIterToken) + 1);
657 VARIFY_POINTER_NULL(value, result, exit)
658 VARIFY_PARAM_NULL(attrIterToken, result, exit)
659 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
661 if (strcmp(key, "uri") == 0)
663 OC_LOG(INFO, TAG, PCF("Build OCAction Instance."));
665 action = (OCAction*) OCMalloc(sizeof(OCAction));
666 VARIFY_POINTER_NULL(action, result, exit)
667 memset(action, 0, sizeof(OCAction));
668 action->resourceUri = (char *) OCMalloc(strlen(value) + 1);
669 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
670 VARIFY_PARAM_NULL(value, result, exit)
671 strncpy(action->resourceUri, value, strlen(value) + 1);
675 if ((key != NULL) && (value != NULL))
677 OC_LOG(INFO, TAG, PCF("Build OCCapability Instance."));
679 capa = (OCCapability*) OCMalloc(sizeof(OCCapability));
680 VARIFY_POINTER_NULL(capa, result, exit)
681 memset(capa, 0, sizeof(OCCapability));
683 capa->capability = (char *) OCMalloc(strlen(key) + 1);
684 VARIFY_POINTER_NULL(capa->capability, result, exit)
685 VARIFY_PARAM_NULL(key, result, exit)
686 strncpy(capa->capability, key, strlen(key) + 1);
688 capa->status = (char *) OCMalloc(strlen(value) + 1);
689 VARIFY_POINTER_NULL(capa->status, result, exit)
690 VARIFY_PARAM_NULL(value, result, exit)
691 strncpy(capa->status, value, strlen(value) + 1);
693 VARIFY_POINTER_NULL(action, result, exit)
695 AddCapability(&action->head, capa);
703 descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
707 AddAction(&(*set)->head, action);
708 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
726 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
728 char temp[1024] = { 0 };
729 int remaining = 1023;
730 OCStackResult res = OC_STACK_ERROR;
732 OCAction *action = actionset->head;
734 if (remaining >= strlen(actionset->actionsetName) + 1)
736 strcat(temp, actionset->actionsetName);
737 remaining -= strlen(actionset->actionsetName);
738 strcat(temp, ACTION_DELIMITER);
743 res = OC_STACK_ERROR;
747 while (action != NULL)
749 strcat(temp, "uri=");
750 remaining -= strlen("uri=");
751 strcat(temp, action->resourceUri);
752 remaining -= strlen(action->resourceUri);
756 OCCapability *capas = action->head;
757 while (capas != NULL)
759 strcat(temp, capas->capability);
760 remaining -= strlen(capas->capability);
763 strcat(temp, capas->status);
764 remaining -= strlen(capas->capability);
773 action = action->next;
776 strcat(temp, ACTION_DELIMITER);
781 *desc = (char *) OCMalloc(1024 - remaining);
782 VARIFY_POINTER_NULL(*desc, res, exit);
793 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
794 OCClientResponse* clientResponse)
796 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
798 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
804 unsigned char *responseJson;
805 responseJson = (unsigned char *) OCMalloc(
806 (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
809 if( responseJson == NULL )
810 return OC_STACK_DELETE_TRANSACTION;
812 // We need the body of response.
813 // Copy the body from the response
814 strcpy((char *) responseJson,
815 ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN));
816 idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN;
817 // And insert NULL at the end of body.
818 (responseJson[idx]) = 0;
820 OCEntityHandlerResponse response = { 0 };
821 response.ehResult = OC_EH_OK;
822 response.payload = (char*)responseJson;
823 response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
824 response.persistentBufferFlag = 0;
825 response.requestHandle = (OCRequestHandle) info->ehRequest;
826 response.resourceHandle = (OCResourceHandle) info->collResource;
828 OCDoResponse(&response);
830 RemoveClientRequestInfo(&clientRequstList, info);
835 return OC_STACK_KEEP_TRANSACTION;
838 void ActionSetCD(void *context)
842 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
845 OCStackResult ret = OC_STACK_ERROR;
852 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
853 json = cJSON_CreateObject();
855 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
857 OCCapability* pointerCapa = action->head;
860 cJSON_AddStringToObject(body, pointerCapa->capability,
861 pointerCapa->status);
862 pointerCapa = pointerCapa->next;
865 jsonStr = cJSON_PrintUnformatted(json);
867 jsonLen = strlen(jsonStr);
868 if (jsonLen < *remaining)
870 strcat((char*) bufferPtr, jsonStr);
871 *remaining -= jsonLen;
872 bufferPtr += jsonLen;
882 unsigned int GetNumOfTargetResource(OCAction *actionset)
884 int numOfResource = 0;
886 OCAction *pointerAction = actionset;
888 while (pointerAction != NULL)
891 pointerAction = pointerAction->next;
894 return numOfResource;
898 #define DEFAULT_CONTEXT_VALUE 0x99
900 OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
901 const unsigned char *action)
903 OCCallbackData cbdata = { 0 };
904 cbdata.cb = &ActionSetCB;
906 cbdata.context = (void*)DEFAULT_CONTEXT_VALUE;
908 // TODO: Selecting OC_WIFI for android, tizen and OC_ETHERNET for linux platform.
909 // It is temporary change as OC_ALL is not working currently. Remove this code and use OC_ALL
910 // once it is functioning.
911 #if defined(__ANDROID__) || defined(__TIZEN__)
912 return OCDoResource(handle, OC_REST_PUT, targetUri,
913 NULL, (char *) action, OC_WIFI, OC_NA_QOS, &cbdata, NULL, 0);
915 return OCDoResource(handle, OC_REST_PUT, targetUri,
916 NULL, (char *) action, OC_ETHERNET, OC_NA_QOS, &cbdata, NULL, 0);
920 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
921 OCServerRequest* requestHandle)
923 OCStackResult result = OC_STACK_ERROR;
924 OCAction *pointerAction = actionset->head;
926 while (pointerAction != NULL)
928 unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
929 unsigned char* actionDescPtr = actionDesc;
930 uint16_t remaining = MAX_RESPONSE_LENGTH;
932 strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX,
933 strlen((const char *) OC_JSON_PREFIX) + 1);
934 BuildActionJSON(pointerAction, actionDescPtr, &remaining);
935 strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX,
936 strlen((const char *) OC_JSON_SUFFIX));
938 ClientRequestInfo *info = (ClientRequestInfo *) OCMalloc(
939 sizeof(ClientRequestInfo));
942 return OC_STACK_NO_MEMORY;
944 memset(info, 0, sizeof(ClientRequestInfo));
946 info->collResource = resource;
947 info->ehRequest = requestHandle;
949 result = SendAction(&info->required, pointerAction->resourceUri,
951 if (result != OC_STACK_OK)
956 AddClientRequestInfo(&clientRequstList, info);
958 pointerAction = pointerAction->next;
964 void DoScheduledGroupAction()
966 OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
967 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
971 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
974 else if (info->resource == NULL)
976 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
979 else if (info->actionset == NULL)
981 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
984 else if (info->ehRequest == NULL)
986 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
990 pthread_mutex_lock(&lock);
992 DoAction(info->resource, info->actionset, info->ehRequest);
994 pthread_mutex_unlock(&lock);
997 if (info->actionset->type == RECURSIVE)
999 ScheduledResourceInfo *schedule;
1000 schedule = (ScheduledResourceInfo *) OCMalloc(
1001 sizeof(ScheduledResourceInfo));
1005 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1006 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1008 if (info->actionset->timesteps > 0)
1010 #ifndef WITH_ARDUINO
1011 pthread_mutex_lock(&lock);
1013 schedule->resource = info->resource;
1014 schedule->actionset = info->actionset;
1015 schedule->ehRequest = info->ehRequest;
1017 schedule->time = registerTimer(info->actionset->timesteps,
1018 &schedule->timer_id,
1019 &DoScheduledGroupAction);
1021 OC_LOG(INFO, TAG, PCF("Reregisteration."));
1022 #ifndef WITH_ARDUINO
1023 pthread_mutex_unlock(&lock);
1025 AddScheduledResource(&scheduleResourceList, schedule);
1030 RemoveScheduledResource(&scheduleResourceList, info);
1037 OCStackResult BuildCollectionGroupActionJSONResponse(
1038 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1039 OCEntityHandlerRequest *ehRequest)
1041 OCStackResult stackRet = OC_STACK_ERROR;
1043 OC_LOG(INFO, TAG, PCF("Group Action is requested."));
1044 // if (stackRet == OC_STACK_OK)
1046 char *doWhat = NULL;
1047 char *details = NULL;
1049 size_t bufferLength = 0;
1050 unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
1051 unsigned char *bufferPtr = NULL;
1055 OCResource * collResource = (OCResource *) ehRequest->resource;
1059 stackRet = ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload,
1062 if(stackRet != OC_STACK_OK)
1064 OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1068 stackRet = OC_STACK_ERROR;
1073 if (method == OC_REST_PUT)
1075 json = cJSON_CreateObject();
1076 cJSON_AddStringToObject(json, "href", resource->uri);
1077 cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
1079 OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
1081 if (strcmp(doWhat, ACTIONSET) == 0)
1083 OCActionSet *actionSet = NULL;
1084 stackRet = BuildActionSetFromString(&actionSet, details);
1086 if(stackRet == OC_STACK_OK)
1088 if (actionSet != NULL)
1090 stackRet = AddActionSet(&resource->actionsetHead,
1092 if (stackRet == OC_STACK_ERROR)
1094 if(actionSet != NULL)
1096 DeleteActionSet( &actionSet );
1098 OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
1104 stackRet = OC_STACK_ERROR;
1108 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1110 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1112 stackRet = OC_STACK_OK;
1116 stackRet = OC_STACK_ERROR;
1120 jsonResponse = cJSON_Print(json);
1123 strcat((char *) bufferPtr, jsonResponse);
1125 bufferLength = strlen((const char *) buffer);
1126 if (bufferLength > 0)
1128 OCEntityHandlerResponse response = { 0 };
1129 if(stackRet == OC_STACK_OK)
1130 response.ehResult = OC_EH_OK;
1132 response.ehResult = OC_EH_ERROR;
1133 response.payload = (char*)buffer;
1134 response.payloadSize = bufferLength + 1;
1135 response.persistentBufferFlag = 0;
1136 response.requestHandle =
1137 (OCRequestHandle) ehRequest->requestHandle;
1138 response.resourceHandle = (OCResourceHandle) collResource;
1139 stackRet = OCDoResponse(&response);
1143 if (method == OC_REST_POST)
1145 OCActionSet *actionset = NULL;
1147 json = cJSON_CreateObject();
1148 cJSON_AddStringToObject(json, "href", resource->uri);
1150 if ((strcmp(doWhat, DO_ACTION) == 0)
1151 || (strcmp(doWhat, "DoScheduledAction") == 0))
1153 char *pActionsetName = NULL;
1154 long int delay = -1;
1156 if (strcmp(doWhat, "DoScheduledAction") == 0)
1158 stackRet = ExtractActionSetNameAndDelaytime(details,
1159 &pActionsetName, &delay);
1162 details = pActionsetName;
1166 stackRet = OC_STACK_OK;
1169 if (stackRet == OC_STACK_OK)
1171 if (GetActionSet(details, resource->actionsetHead,
1172 &actionset) != OC_STACK_OK)
1174 OC_LOG(INFO, TAG, PCF("ERROR"));
1175 stackRet = OC_STACK_ERROR;
1178 if (actionset == NULL)
1180 OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
1181 stackRet = OC_STACK_ERROR;
1185 OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
1186 if (actionset->type == NONE)
1188 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1189 actionset->actionsetName);
1190 unsigned int num = GetNumOfTargetResource(
1193 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1194 HandleAggregateResponse;
1195 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1198 DoAction(resource, actionset,
1199 (OCServerRequest*) ehRequest->requestHandle);
1200 stackRet = OC_STACK_OK;
1204 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1205 actionset->actionsetName);
1208 (delay == -1 ? actionset->timesteps : delay);
1210 ScheduledResourceInfo *schedule;
1211 schedule = (ScheduledResourceInfo *) OCMalloc(
1212 sizeof(ScheduledResourceInfo));
1216 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1218 sizeof(ScheduledResourceInfo));
1220 schedule->resource = resource;
1221 schedule->actionset = actionset;
1222 schedule->ehRequest =
1223 (OCServerRequest*) ehRequest->requestHandle;
1227 OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
1228 actionset->timesteps);
1230 schedule->time = registerTimer(delay,
1231 &schedule->timer_id,
1232 &DoScheduledGroupAction);
1234 AddScheduledResource(&scheduleResourceList,
1236 stackRet = OC_STACK_OK;
1240 stackRet = OC_STACK_ERROR;
1247 else if (strcmp(doWhat, "CancelAction") == 0)
1249 ScheduledResourceInfo *info =
1250 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1254 unregisterTimer(info->timer_id);
1256 RemoveScheduledResource(&scheduleResourceList, info);
1257 stackRet = OC_STACK_OK;
1261 stackRet = OC_STACK_ERROR;
1265 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1267 char *plainText = NULL;
1268 OCActionSet *actionset = NULL;
1270 cJSON_AddItemToObject(json, "rep", format =
1271 cJSON_CreateObject());
1272 GetActionSet(details, resource->actionsetHead, &actionset);
1273 if (actionset != NULL)
1275 BuildStringFromActionSet(actionset, &plainText);
1277 if (plainText != NULL)
1279 cJSON_AddStringToObject(format, ACTIONSET, plainText);
1282 stackRet = OC_STACK_OK;
1287 jsonResponse = cJSON_Print(json);
1290 strcat((char *) bufferPtr, jsonResponse);
1292 bufferLength = strlen((const char *) buffer);
1293 if (bufferLength > 0)
1295 OCEntityHandlerResponse response = { 0 };
1296 if(stackRet == OC_STACK_OK)
1297 response.ehResult = OC_EH_OK;
1299 response.ehResult = OC_EH_ERROR;
1300 response.payload = (char *)buffer;
1301 response.payloadSize = bufferLength + 1;
1302 response.persistentBufferFlag = 0;
1303 response.requestHandle =
1304 (OCRequestHandle) ehRequest->requestHandle;
1305 response.resourceHandle = (OCResourceHandle) collResource;
1306 stackRet = OCDoResponse(&response);