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 char temp[1024] = { 0 };
735 int remaining = 1023;
736 OCStackResult res = OC_STACK_ERROR;
738 OCAction *action = actionset->head;
740 if (remaining >= strlen(actionset->actionsetName) + 1)
742 strcat(temp, actionset->actionsetName);
743 remaining -= strlen(actionset->actionsetName);
744 strcat(temp, ACTION_DELIMITER);
749 res = OC_STACK_ERROR;
753 while (action != NULL)
755 strcat(temp, "uri=");
756 remaining -= strlen("uri=");
757 strcat(temp, action->resourceUri);
758 remaining -= strlen(action->resourceUri);
762 OCCapability *capas = action->head;
763 while (capas != NULL)
765 strcat(temp, capas->capability);
766 remaining -= strlen(capas->capability);
769 strcat(temp, capas->status);
770 remaining -= strlen(capas->capability);
779 action = action->next;
782 strcat(temp, ACTION_DELIMITER);
787 *desc = (char *) OICMalloc(1024 - remaining);
788 VARIFY_POINTER_NULL(*desc, res, exit);
799 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
800 OCClientResponse* clientResponse)
803 (void)clientResponse;
804 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
806 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
812 unsigned char *responseJson = NULL;
813 // TODO: Figure out what this does, change implementation
814 //responseJson = (unsigned char *) OICMalloc(
815 // (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
818 if( responseJson == NULL )
819 return OC_STACK_DELETE_TRANSACTION;
821 // We need the body of response.
822 // Copy the body from the response
824 //strcpy((char *) responseJson,
825 // ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN));
826 //idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN;
827 // And insert NULL at the end of body.
828 (responseJson[idx]) = 0;
830 OCEntityHandlerResponse response = { 0 };
831 response.ehResult = OC_EH_OK;
832 // TODO: Removing payload size, waht goes here?
833 // response.payload = (char*)responseJson;
834 //response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
835 response.persistentBufferFlag = 0;
836 response.requestHandle = (OCRequestHandle) info->ehRequest;
837 response.resourceHandle = (OCResourceHandle) info->collResource;
839 OCDoResponse(&response);
841 RemoveClientRequestInfo(&clientRequstList, info);
846 return OC_STACK_KEEP_TRANSACTION;
849 void ActionSetCD(void *context)
854 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
857 OCStackResult ret = OC_STACK_ERROR;
864 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
865 json = cJSON_CreateObject();
867 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
869 OCCapability* pointerCapa = action->head;
872 cJSON_AddStringToObject(body, pointerCapa->capability,
873 pointerCapa->status);
874 pointerCapa = pointerCapa->next;
877 jsonStr = cJSON_PrintUnformatted(json);
879 jsonLen = strlen(jsonStr);
880 if (jsonLen < *remaining)
882 strcat((char*) bufferPtr, jsonStr);
883 *remaining -= jsonLen;
884 bufferPtr += jsonLen;
894 unsigned int GetNumOfTargetResource(OCAction *actionset)
896 int numOfResource = 0;
898 OCAction *pointerAction = actionset;
900 while (pointerAction != NULL)
903 pointerAction = pointerAction->next;
906 return numOfResource;
910 #define DEFAULT_CONTEXT_VALUE 0x99
912 OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
913 const unsigned char *action)
918 // TODO: disabled since this is no longer compatible
919 return OC_STACK_NOTIMPL;
922 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
923 OCServerRequest* requestHandle)
925 OCStackResult result = OC_STACK_ERROR;
926 OCAction *pointerAction = actionset->head;
928 while (pointerAction != NULL)
930 unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
931 unsigned char* actionDescPtr = actionDesc;
932 uint16_t remaining = MAX_RESPONSE_LENGTH;
934 strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX,
935 strlen((const char *) OC_JSON_PREFIX) + 1);
936 BuildActionJSON(pointerAction, actionDescPtr, &remaining);
937 strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX,
938 strlen((const char *) OC_JSON_SUFFIX));
940 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
941 sizeof(ClientRequestInfo));
944 return OC_STACK_NO_MEMORY;
946 memset(info, 0, sizeof(ClientRequestInfo));
948 info->collResource = resource;
949 info->ehRequest = requestHandle;
951 result = SendAction(&info->required, pointerAction->resourceUri,
953 if (result != OC_STACK_OK)
959 AddClientRequestInfo(&clientRequstList, info);
961 pointerAction = pointerAction->next;
967 void DoScheduledGroupAction()
969 OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
970 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
974 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
977 else if (info->resource == NULL)
979 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
982 else if (info->actionset == NULL)
984 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
987 else if (info->ehRequest == NULL)
989 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
993 pthread_mutex_lock(&lock);
995 DoAction(info->resource, info->actionset, info->ehRequest);
997 pthread_mutex_unlock(&lock);
1000 if (info->actionset->type == RECURSIVE)
1002 ScheduledResourceInfo *schedule;
1003 schedule = (ScheduledResourceInfo *) OICMalloc(
1004 sizeof(ScheduledResourceInfo));
1008 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1009 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1011 if (info->actionset->timesteps > 0)
1013 #ifndef WITH_ARDUINO
1014 pthread_mutex_lock(&lock);
1016 schedule->resource = info->resource;
1017 schedule->actionset = info->actionset;
1018 schedule->ehRequest = info->ehRequest;
1020 schedule->time = registerTimer(info->actionset->timesteps,
1021 &schedule->timer_id,
1022 &DoScheduledGroupAction);
1024 OC_LOG(INFO, TAG, PCF("Reregisteration."));
1025 #ifndef WITH_ARDUINO
1026 pthread_mutex_unlock(&lock);
1028 AddScheduledResource(&scheduleResourceList, schedule);
1037 RemoveScheduledResource(&scheduleResourceList, info);
1044 OCStackResult BuildCollectionGroupActionJSONResponse(
1045 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1046 OCEntityHandlerRequest *ehRequest)
1048 OCStackResult stackRet = OC_STACK_ERROR;
1050 OC_LOG(INFO, TAG, PCF("Group Action is requested."));
1051 // if (stackRet == OC_STACK_OK)
1053 char *doWhat = NULL;
1054 char *details = NULL;
1056 size_t bufferLength = 0;
1057 unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
1059 OCResource * collResource = (OCResource *) ehRequest->resource;
1063 stackRet = OC_STACK_NOTIMPL;
1065 //stackRet = ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload,
1066 // &doWhat, &details);
1068 if(stackRet != OC_STACK_OK)
1070 OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1074 stackRet = OC_STACK_ERROR;
1079 if (method == OC_REST_PUT)
1081 json = cJSON_CreateObject();
1082 cJSON_AddStringToObject(json, "href", resource->uri);
1083 cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
1085 OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
1087 if (strcmp(doWhat, ACTIONSET) == 0)
1089 OCActionSet *actionSet = NULL;
1090 stackRet = BuildActionSetFromString(&actionSet, details);
1092 if(stackRet == OC_STACK_OK)
1094 if (actionSet != NULL)
1096 stackRet = AddActionSet(&resource->actionsetHead,
1098 if (stackRet == OC_STACK_ERROR)
1100 if(actionSet != NULL)
1102 DeleteActionSet( &actionSet );
1104 OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
1110 stackRet = OC_STACK_ERROR;
1114 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1116 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1118 stackRet = OC_STACK_OK;
1122 stackRet = OC_STACK_ERROR;
1126 jsonResponse = cJSON_Print(json);
1129 OICStrcat((char*)buffer, sizeof(buffer), jsonResponse);
1131 bufferLength = strlen((const char *) buffer);
1132 if (bufferLength > 0)
1134 OCEntityHandlerResponse response = { 0 };
1135 if(stackRet == OC_STACK_OK)
1136 response.ehResult = OC_EH_OK;
1138 response.ehResult = OC_EH_ERROR;
1140 //response.payload = (char*)buffer;
1141 //response.payloadSize = bufferLength + 1;
1142 response.persistentBufferFlag = 0;
1143 response.requestHandle =
1144 (OCRequestHandle) ehRequest->requestHandle;
1145 response.resourceHandle = (OCResourceHandle) collResource;
1146 stackRet = OCDoResponse(&response);
1150 if (method == OC_REST_POST)
1152 OCActionSet *actionset = NULL;
1154 json = cJSON_CreateObject();
1155 cJSON_AddStringToObject(json, "href", resource->uri);
1157 if ((strcmp(doWhat, DO_ACTION) == 0)
1158 || (strcmp(doWhat, "DoScheduledAction") == 0))
1160 char *pActionsetName = NULL;
1161 long int delay = -1;
1163 if (strcmp(doWhat, "DoScheduledAction") == 0)
1165 stackRet = ExtractActionSetNameAndDelaytime(details,
1166 &pActionsetName, &delay);
1169 details = pActionsetName;
1173 stackRet = OC_STACK_OK;
1176 if (stackRet == OC_STACK_OK)
1178 if (GetActionSet(details, resource->actionsetHead,
1179 &actionset) != OC_STACK_OK)
1181 OC_LOG(INFO, TAG, PCF("ERROR"));
1182 stackRet = OC_STACK_ERROR;
1185 if (actionset == NULL)
1187 OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
1188 stackRet = OC_STACK_ERROR;
1192 OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
1193 if (actionset->type == NONE)
1195 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1196 actionset->actionsetName);
1197 unsigned int num = GetNumOfTargetResource(
1200 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1201 HandleAggregateResponse;
1202 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1205 DoAction(resource, actionset,
1206 (OCServerRequest*) ehRequest->requestHandle);
1207 stackRet = OC_STACK_OK;
1211 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1212 actionset->actionsetName);
1215 (delay == -1 ? actionset->timesteps : delay);
1217 ScheduledResourceInfo *schedule;
1218 schedule = (ScheduledResourceInfo *) OICMalloc(
1219 sizeof(ScheduledResourceInfo));
1223 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1225 sizeof(ScheduledResourceInfo));
1227 schedule->resource = resource;
1228 schedule->actionset = actionset;
1229 schedule->ehRequest =
1230 (OCServerRequest*) ehRequest->requestHandle;
1234 OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
1235 actionset->timesteps);
1237 schedule->time = registerTimer(delay,
1238 &schedule->timer_id,
1239 &DoScheduledGroupAction);
1241 AddScheduledResource(&scheduleResourceList,
1243 stackRet = OC_STACK_OK;
1247 stackRet = OC_STACK_ERROR;
1254 else if (strcmp(doWhat, "CancelAction") == 0)
1256 ScheduledResourceInfo *info =
1257 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1261 unregisterTimer(info->timer_id);
1263 RemoveScheduledResource(&scheduleResourceList, info);
1264 stackRet = OC_STACK_OK;
1268 stackRet = OC_STACK_ERROR;
1272 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1274 char *plainText = NULL;
1275 OCActionSet *actionset = NULL;
1277 cJSON_AddItemToObject(json, "rep", format =
1278 cJSON_CreateObject());
1279 GetActionSet(details, resource->actionsetHead, &actionset);
1280 if (actionset != NULL)
1282 BuildStringFromActionSet(actionset, &plainText);
1284 if (plainText != NULL)
1286 cJSON_AddStringToObject(format, ACTIONSET, plainText);
1289 stackRet = OC_STACK_OK;
1294 jsonResponse = cJSON_Print(json);
1297 OICStrcat((char*)buffer, sizeof(buffer), jsonResponse);
1299 bufferLength = strlen((const char *) buffer);
1300 if (bufferLength > 0)
1302 OCEntityHandlerResponse response = { 0 };
1303 if(stackRet == OC_STACK_OK)
1304 response.ehResult = OC_EH_OK;
1306 response.ehResult = OC_EH_ERROR;
1308 //response.payload = (char *)buffer;
1309 //response.payloadSize = bufferLength + 1;
1310 response.persistentBufferFlag = 0;
1311 response.requestHandle =
1312 (OCRequestHandle) ehRequest->requestHandle;
1313 response.resourceHandle = (OCResourceHandle) collResource;
1314 stackRet = OCDoResponse(&response);