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 length = strlen(iterToken) + 1;
541 *key = (char *) OCMalloc(length);
542 VARIFY_POINTER_NULL(*key, result, exit)
544 strncpy(*key, iterToken + 1, length);
545 ((*key)[((length - 1) - 2)]) = '\0';
547 iterToken = (char *) strtok_r(NULL, "}", &iterTokenPtr);
548 length = strlen(iterToken) + 1;
550 *value = (char *) OCMalloc(length);
551 VARIFY_POINTER_NULL(*value, result, exit)
553 strncpy(*value, iterToken + 1, length);
554 ((*value)[((length - 1) - 2)]) = '\0';
557 if (result != OC_STACK_OK)
566 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
569 char *token, *tokenPtr;
570 OCStackResult result = OC_STACK_OK;
572 token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
573 *setName = (char *) OCMalloc(strlen(token) + 1);
574 VARIFY_POINTER_NULL(*setName, result, exit)
575 VARIFY_PARAM_NULL(token, result, exit)
576 strncpy(*setName, token, strlen(token) + 1);
578 token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
579 VARIFY_POINTER_NULL(pa, result, exit)
580 VARIFY_PARAM_NULL(token, result, exit)
590 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
592 OCStackResult result = OC_STACK_OK;
594 char *iterToken = NULL, *iterTokenPtr = NULL;
595 char *descIterToken = NULL, *descIterTokenPtr = NULL;
596 char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
597 char *desc = NULL, *attr = NULL;
598 char *key = NULL, *value = NULL;
600 OCAction *action = NULL;
601 OCCapability *capa = NULL;
603 OC_LOG(INFO, TAG, PCF("Build ActionSet Instance."));
605 *set = (OCActionSet*) OCMalloc(sizeof(OCActionSet));
606 VARIFY_POINTER_NULL(*set, result, exit)
608 iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
611 memset(*set, 0, sizeof(OCActionSet));
612 (*set)->actionsetName = (char *) OCMalloc(strlen(iterToken) + 1);
613 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
614 VARIFY_PARAM_NULL(iterToken, result, exit)
615 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
617 // Time info. for Scheduled/Recursive Group action.
618 // d is meant Day of the week.
619 // T is meant ActionType.
620 // yyyy-mm-dd hh:mm:ss d
621 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
622 VARIFY_PARAM_NULL(iterToken, result, exit)
624 sscanf(iterToken, "%ld %d", &(*set)->timesteps, &(*set)->type);
627 OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
629 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
632 desc = (char *) OCMalloc(strlen(iterToken) + 1);
633 VARIFY_POINTER_NULL(desc, result, exit)
634 VARIFY_PARAM_NULL(desc, result, exit)
635 strncpy(desc, iterToken, strlen(iterToken) + 1);
636 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
638 while (descIterToken)
640 attr = (char *) OCMalloc(strlen(descIterToken) + 1);
641 VARIFY_POINTER_NULL(attr, result, exit)
642 VARIFY_PARAM_NULL(descIterToken, result, exit)
643 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
645 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
647 key = (char *) OCMalloc(strlen(attrIterToken) + 1);
648 VARIFY_POINTER_NULL(key, result, exit)
649 VARIFY_PARAM_NULL(attrIterToken, result, exit)
650 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
652 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
654 value = (char *) OCMalloc(strlen(attrIterToken) + 1);
655 VARIFY_POINTER_NULL(value, result, exit)
656 VARIFY_PARAM_NULL(attrIterToken, result, exit)
657 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
659 if (strcmp(key, "uri") == 0)
661 OC_LOG(INFO, TAG, PCF("Build OCAction Instance."));
663 action = (OCAction*) OCMalloc(sizeof(OCAction));
664 VARIFY_POINTER_NULL(action, result, exit)
665 memset(action, 0, sizeof(OCAction));
666 action->resourceUri = (char *) OCMalloc(strlen(value) + 1);
667 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
668 VARIFY_PARAM_NULL(value, result, exit)
669 strncpy(action->resourceUri, value, strlen(value) + 1);
673 if ((key != NULL) && (value != NULL))
675 OC_LOG(INFO, TAG, PCF("Build OCCapability Instance."));
677 capa = (OCCapability*) OCMalloc(sizeof(OCCapability));
678 VARIFY_POINTER_NULL(capa, result, exit)
679 memset(capa, 0, sizeof(OCCapability));
681 capa->capability = (char *) OCMalloc(strlen(key) + 1);
682 VARIFY_POINTER_NULL(capa->capability, result, exit)
683 VARIFY_PARAM_NULL(key, result, exit)
684 strncpy(capa->capability, key, strlen(key) + 1);
686 capa->status = (char *) OCMalloc(strlen(value) + 1);
687 VARIFY_POINTER_NULL(capa->status, result, exit)
688 VARIFY_PARAM_NULL(value, result, exit)
689 strncpy(capa->status, value, strlen(value) + 1);
691 VARIFY_POINTER_NULL(action, result, exit)
693 AddCapability(&action->head, capa);
701 descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
705 AddAction(&(*set)->head, action);
706 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
724 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
726 char temp[1024] = { 0 };
727 int remaining = 1023;
729 OCAction *action = actionset->head;
731 if (remaining >= strlen(actionset->actionsetName) + 1)
733 strcat(temp, actionset->actionsetName);
734 remaining -= strlen(actionset->actionsetName);
735 strcat(temp, ACTION_DELIMITER);
740 return OC_STACK_ERROR;
743 while (action != NULL)
745 strcat(temp, "uri=");
746 remaining -= strlen("uri=");
747 strcat(temp, action->resourceUri);
748 remaining -= strlen(action->resourceUri);
752 OCCapability *capas = action->head;
753 while (capas != NULL)
755 strcat(temp, capas->capability);
756 remaining -= strlen(capas->capability);
759 strcat(temp, capas->status);
760 remaining -= strlen(capas->capability);
769 action = action->next;
772 strcat(temp, ACTION_DELIMITER);
777 *desc = (char *) OCMalloc(1024 - remaining);
783 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
784 OCClientResponse* clientResponse)
786 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
788 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
794 unsigned char *responseJson;
795 responseJson = (unsigned char *) OCMalloc(
796 (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
799 // We need the body of response.
800 // Copy the body from the response
801 strcpy((char *) responseJson,
802 ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN));
803 idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN;
804 // And insert NULL at the end of body.
805 (responseJson[idx]) = 0;
807 OCEntityHandlerResponse response = { 0 };
808 response.ehResult = OC_EH_OK;
809 response.payload = (char*)responseJson;
810 response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
811 response.persistentBufferFlag = 0;
812 response.requestHandle = (OCRequestHandle) info->ehRequest;
813 response.resourceHandle = (OCResourceHandle) info->collResource;
815 OCDoResponse(&response);
817 RemoveClientRequestInfo(&clientRequstList, info);
822 return OC_STACK_KEEP_TRANSACTION;
825 void ActionSetCD(void *context)
829 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
832 OCStackResult ret = OC_STACK_ERROR;
839 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
840 json = cJSON_CreateObject();
842 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
844 OCCapability* pointerCapa = action->head;
847 cJSON_AddStringToObject(body, pointerCapa->capability,
848 pointerCapa->status);
849 pointerCapa = pointerCapa->next;
852 jsonStr = cJSON_PrintUnformatted(json);
854 jsonLen = strlen(jsonStr);
855 if (jsonLen < *remaining)
857 strcat((char*) bufferPtr, jsonStr);
858 *remaining -= jsonLen;
859 bufferPtr += jsonLen;
869 unsigned int GetNumOfTargetResource(OCAction *actionset)
871 int numOfResource = 0;
873 OCAction *pointerAction = actionset;
875 while (pointerAction != NULL)
878 pointerAction = pointerAction->next;
881 return numOfResource;
885 #define DEFAULT_CONTEXT_VALUE 0x99
887 OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
888 const unsigned char *action)
890 OCCallbackData cbdata = { 0 };
891 cbdata.cb = &ActionSetCB;
893 cbdata.context = (void*)DEFAULT_CONTEXT_VALUE;
895 return OCDoResource(handle, OC_REST_PUT, targetUri,
896 NULL, (char *) action, OC_ETHERNET, OC_NA_QOS, &cbdata, NULL, 0);
899 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
900 OCServerRequest* requestHandle)
902 OCStackResult result = OC_STACK_ERROR;
903 OCAction *pointerAction = actionset->head;
905 while (pointerAction != NULL)
907 unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
908 unsigned char* actionDescPtr = actionDesc;
909 uint16_t remaining = MAX_RESPONSE_LENGTH;
911 strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX,
912 strlen((const char *) OC_JSON_PREFIX) + 1);
913 BuildActionJSON(pointerAction, actionDescPtr, &remaining);
914 strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX,
915 strlen((const char *) OC_JSON_SUFFIX));
917 ClientRequestInfo *info = (ClientRequestInfo *) OCMalloc(
918 sizeof(ClientRequestInfo));
919 memset(info, 0, sizeof(ClientRequestInfo));
921 info->collResource = resource;
922 info->ehRequest = requestHandle;
924 result = SendAction(&info->required, pointerAction->resourceUri,
926 if (result != OC_STACK_OK)
931 AddClientRequestInfo(&clientRequstList, info);
933 pointerAction = pointerAction->next;
939 void DoScheduledGroupAction()
941 OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
942 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
946 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
949 else if (info->resource == NULL)
951 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
954 else if (info->actionset == NULL)
956 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
959 else if (info->ehRequest == NULL)
961 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
965 pthread_mutex_lock(&lock);
967 DoAction(info->resource, info->actionset, info->ehRequest);
969 pthread_mutex_unlock(&lock);
972 if (info->actionset->type == RECURSIVE)
974 ScheduledResourceInfo *schedule;
975 schedule = (ScheduledResourceInfo *) OCMalloc(
976 sizeof(ScheduledResourceInfo));
980 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
981 memset(schedule, 0, sizeof(ScheduledResourceInfo));
983 if (info->actionset->timesteps > 0)
986 pthread_mutex_lock(&lock);
988 schedule->resource = info->resource;
989 schedule->actionset = info->actionset;
990 schedule->ehRequest = info->ehRequest;
992 schedule->time = registerTimer(info->actionset->timesteps,
994 &DoScheduledGroupAction);
996 OC_LOG(INFO, TAG, PCF("Reregisteration."));
998 pthread_mutex_unlock(&lock);
1000 AddScheduledResource(&scheduleResourceList, schedule);
1005 RemoveScheduledResource(&scheduleResourceList, info);
1012 OCStackResult BuildCollectionGroupActionJSONResponse(
1013 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1014 OCEntityHandlerRequest *ehRequest)
1016 OCStackResult stackRet = OC_STACK_ERROR;
1018 OC_LOG(INFO, TAG, PCF("Group Action is requested."));
1019 // if (stackRet == OC_STACK_OK)
1021 char *doWhat = NULL;
1022 char *details = NULL;
1024 size_t bufferLength = 0;
1025 unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
1026 unsigned char *bufferPtr = NULL;
1030 OCResource * collResource = (OCResource *) ehRequest->resource;
1034 ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload, &doWhat,
1040 if (method == OC_REST_PUT)
1042 json = cJSON_CreateObject();
1043 cJSON_AddStringToObject(json, "href", resource->uri);
1044 cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
1046 OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
1048 if (strcmp(doWhat, ACTIONSET) == 0)
1050 OCActionSet *actionSet = NULL;
1051 stackRet = BuildActionSetFromString(&actionSet, details);
1053 if(stackRet == OC_STACK_OK)
1055 if (actionSet != NULL)
1057 stackRet = AddActionSet(&resource->actionsetHead,
1059 if (stackRet == OC_STACK_ERROR)
1061 if(actionSet != NULL)
1063 DeleteActionSet( &actionSet );
1065 OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
1071 stackRet = OC_STACK_ERROR;
1075 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1077 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1079 stackRet = OC_STACK_OK;
1083 stackRet = OC_STACK_ERROR;
1087 jsonResponse = cJSON_Print(json);
1090 strcat((char *) bufferPtr, jsonResponse);
1092 bufferLength = strlen((const char *) buffer);
1093 if (bufferLength > 0)
1095 OCEntityHandlerResponse response = { 0 };
1096 if(stackRet == OC_STACK_OK)
1097 response.ehResult = OC_EH_OK;
1099 response.ehResult = OC_EH_ERROR;
1100 response.payload = (char*)buffer;
1101 response.payloadSize = bufferLength + 1;
1102 response.persistentBufferFlag = 0;
1103 response.requestHandle =
1104 (OCRequestHandle) ehRequest->requestHandle;
1105 response.resourceHandle = (OCResourceHandle) collResource;
1106 stackRet = OCDoResponse(&response);
1110 if (method == OC_REST_POST)
1112 OCActionSet *actionset = NULL;
1114 json = cJSON_CreateObject();
1115 cJSON_AddStringToObject(json, "href", resource->uri);
1117 if ((strcmp(doWhat, DO_ACTION) == 0)
1118 || (strcmp(doWhat, "DoScheduledAction") == 0))
1120 char *pActionsetName = NULL;
1121 long int delay = -1;
1123 if (strcmp(doWhat, "DoScheduledAction") == 0)
1125 stackRet = ExtractActionSetNameAndDelaytime(details,
1126 &pActionsetName, &delay);
1129 details = pActionsetName;
1133 stackRet = OC_STACK_OK;
1136 if (stackRet == OC_STACK_OK)
1138 if (GetActionSet(details, resource->actionsetHead,
1139 &actionset) != OC_STACK_OK)
1141 OC_LOG(INFO, TAG, PCF("ERROR"));
1142 stackRet = OC_STACK_ERROR;
1145 if (actionset == NULL)
1147 OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
1148 stackRet = OC_STACK_ERROR;
1152 OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
1153 if (actionset->type == NONE)
1155 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1156 actionset->actionsetName);
1157 unsigned int num = GetNumOfTargetResource(
1160 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1161 HandleAggregateResponse;
1162 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1165 DoAction(resource, actionset,
1166 (OCServerRequest*) ehRequest->requestHandle);
1167 stackRet = OC_STACK_OK;
1171 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1172 actionset->actionsetName);
1175 (delay == -1 ? actionset->timesteps : delay);
1177 ScheduledResourceInfo *schedule;
1178 schedule = (ScheduledResourceInfo *) OCMalloc(
1179 sizeof(ScheduledResourceInfo));
1183 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1185 sizeof(ScheduledResourceInfo));
1187 schedule->resource = resource;
1188 schedule->actionset = actionset;
1189 schedule->ehRequest =
1190 (OCServerRequest*) ehRequest->requestHandle;
1194 OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
1195 actionset->timesteps);
1197 schedule->time = registerTimer(delay,
1198 &schedule->timer_id,
1199 &DoScheduledGroupAction);
1201 AddScheduledResource(&scheduleResourceList,
1203 stackRet = OC_STACK_OK;
1207 stackRet = OC_STACK_ERROR;
1214 else if (strcmp(doWhat, "CancelAction") == 0)
1216 ScheduledResourceInfo *info =
1217 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1221 unregisterTimer(info->timer_id);
1223 RemoveScheduledResource(&scheduleResourceList, info);
1224 stackRet = OC_STACK_OK;
1228 stackRet = OC_STACK_ERROR;
1232 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1234 char *plainText = NULL;
1235 OCActionSet *actionset = NULL;
1237 cJSON_AddItemToObject(json, "rep", format =
1238 cJSON_CreateObject());
1239 GetActionSet(details, resource->actionsetHead, &actionset);
1240 if (actionset != NULL)
1242 BuildStringFromActionSet(actionset, &plainText);
1244 if (plainText != NULL)
1246 cJSON_AddStringToObject(format, ACTIONSET, plainText);
1249 stackRet = OC_STACK_OK;
1254 jsonResponse = cJSON_Print(json);
1257 strcat((char *) bufferPtr, jsonResponse);
1259 bufferLength = strlen((const char *) buffer);
1260 if (bufferLength > 0)
1262 OCEntityHandlerResponse response = { 0 };
1263 if(stackRet == OC_STACK_OK)
1264 response.ehResult = OC_EH_OK;
1266 response.ehResult = OC_EH_ERROR;
1267 response.payload = (char *)buffer;
1268 response.payloadSize = bufferLength + 1;
1269 response.persistentBufferFlag = 0;
1270 response.requestHandle =
1271 (OCRequestHandle) ehRequest->requestHandle;
1272 response.resourceHandle = (OCResourceHandle) collResource;
1273 stackRet = OCDoResponse(&response);