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)
802 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
804 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
810 unsigned char *responseJson = NULL;
811 // TODO: Figure out what this does, change implementation
812 //responseJson = (unsigned char *) OICMalloc(
813 // (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
816 if( responseJson == NULL )
817 return OC_STACK_DELETE_TRANSACTION;
819 // We need the body of response.
820 // Copy the body from the response
822 //strcpy((char *) responseJson,
823 // ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN));
824 //idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN;
825 // And insert NULL at the end of body.
826 (responseJson[idx]) = 0;
828 OCEntityHandlerResponse response = { 0 };
829 response.ehResult = OC_EH_OK;
830 // TODO: Removing payload size, waht goes here?
831 // response.payload = (char*)responseJson;
832 //response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
833 response.persistentBufferFlag = 0;
834 response.requestHandle = (OCRequestHandle) info->ehRequest;
835 response.resourceHandle = (OCResourceHandle) info->collResource;
837 OCDoResponse(&response);
839 RemoveClientRequestInfo(&clientRequstList, info);
844 return OC_STACK_KEEP_TRANSACTION;
847 void ActionSetCD(void *context)
851 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
854 OCStackResult ret = OC_STACK_ERROR;
861 OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
862 json = cJSON_CreateObject();
864 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
866 OCCapability* pointerCapa = action->head;
869 cJSON_AddStringToObject(body, pointerCapa->capability,
870 pointerCapa->status);
871 pointerCapa = pointerCapa->next;
874 jsonStr = cJSON_PrintUnformatted(json);
876 jsonLen = strlen(jsonStr);
877 if (jsonLen < *remaining)
879 strcat((char*) bufferPtr, jsonStr);
880 *remaining -= jsonLen;
881 bufferPtr += jsonLen;
891 unsigned int GetNumOfTargetResource(OCAction *actionset)
893 int numOfResource = 0;
895 OCAction *pointerAction = actionset;
897 while (pointerAction != NULL)
900 pointerAction = pointerAction->next;
903 return numOfResource;
907 #define DEFAULT_CONTEXT_VALUE 0x99
909 OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
910 const unsigned char *action)
912 // TODO: disabled since this is no longer compatible
913 return OC_STACK_NOTIMPL;
916 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
917 OCServerRequest* requestHandle)
919 OCStackResult result = OC_STACK_ERROR;
920 OCAction *pointerAction = actionset->head;
922 while (pointerAction != NULL)
924 unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
925 unsigned char* actionDescPtr = actionDesc;
926 uint16_t remaining = MAX_RESPONSE_LENGTH;
928 strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX,
929 strlen((const char *) OC_JSON_PREFIX) + 1);
930 BuildActionJSON(pointerAction, actionDescPtr, &remaining);
931 strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX,
932 strlen((const char *) OC_JSON_SUFFIX));
934 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
935 sizeof(ClientRequestInfo));
938 return OC_STACK_NO_MEMORY;
940 memset(info, 0, sizeof(ClientRequestInfo));
942 info->collResource = resource;
943 info->ehRequest = requestHandle;
945 result = SendAction(&info->required, pointerAction->resourceUri,
947 if (result != OC_STACK_OK)
953 AddClientRequestInfo(&clientRequstList, info);
955 pointerAction = pointerAction->next;
961 void DoScheduledGroupAction()
963 OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
964 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
968 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
971 else if (info->resource == NULL)
973 OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
976 else if (info->actionset == NULL)
978 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
981 else if (info->ehRequest == NULL)
983 OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
987 pthread_mutex_lock(&lock);
989 DoAction(info->resource, info->actionset, info->ehRequest);
991 pthread_mutex_unlock(&lock);
994 if (info->actionset->type == RECURSIVE)
996 ScheduledResourceInfo *schedule;
997 schedule = (ScheduledResourceInfo *) OICMalloc(
998 sizeof(ScheduledResourceInfo));
1002 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1003 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1005 if (info->actionset->timesteps > 0)
1007 #ifndef WITH_ARDUINO
1008 pthread_mutex_lock(&lock);
1010 schedule->resource = info->resource;
1011 schedule->actionset = info->actionset;
1012 schedule->ehRequest = info->ehRequest;
1014 schedule->time = registerTimer(info->actionset->timesteps,
1015 &schedule->timer_id,
1016 &DoScheduledGroupAction);
1018 OC_LOG(INFO, TAG, PCF("Reregisteration."));
1019 #ifndef WITH_ARDUINO
1020 pthread_mutex_unlock(&lock);
1022 AddScheduledResource(&scheduleResourceList, schedule);
1031 RemoveScheduledResource(&scheduleResourceList, info);
1038 OCStackResult BuildCollectionGroupActionJSONResponse(
1039 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1040 OCEntityHandlerRequest *ehRequest)
1042 OCStackResult stackRet = OC_STACK_ERROR;
1044 OC_LOG(INFO, TAG, PCF("Group Action is requested."));
1045 // if (stackRet == OC_STACK_OK)
1047 char *doWhat = NULL;
1048 char *details = NULL;
1050 size_t bufferLength = 0;
1051 unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
1053 OCResource * collResource = (OCResource *) ehRequest->resource;
1057 stackRet = OC_STACK_NOTIMPL;
1059 //stackRet = ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload,
1060 // &doWhat, &details);
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 OICStrcat((char*)buffer, sizeof(buffer), 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;
1134 //response.payload = (char*)buffer;
1135 //response.payloadSize = bufferLength + 1;
1136 response.persistentBufferFlag = 0;
1137 response.requestHandle =
1138 (OCRequestHandle) ehRequest->requestHandle;
1139 response.resourceHandle = (OCResourceHandle) collResource;
1140 stackRet = OCDoResponse(&response);
1144 if (method == OC_REST_POST)
1146 OCActionSet *actionset = NULL;
1148 json = cJSON_CreateObject();
1149 cJSON_AddStringToObject(json, "href", resource->uri);
1151 if ((strcmp(doWhat, DO_ACTION) == 0)
1152 || (strcmp(doWhat, "DoScheduledAction") == 0))
1154 char *pActionsetName = NULL;
1155 long int delay = -1;
1157 if (strcmp(doWhat, "DoScheduledAction") == 0)
1159 stackRet = ExtractActionSetNameAndDelaytime(details,
1160 &pActionsetName, &delay);
1163 details = pActionsetName;
1167 stackRet = OC_STACK_OK;
1170 if (stackRet == OC_STACK_OK)
1172 if (GetActionSet(details, resource->actionsetHead,
1173 &actionset) != OC_STACK_OK)
1175 OC_LOG(INFO, TAG, PCF("ERROR"));
1176 stackRet = OC_STACK_ERROR;
1179 if (actionset == NULL)
1181 OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
1182 stackRet = OC_STACK_ERROR;
1186 OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
1187 if (actionset->type == NONE)
1189 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1190 actionset->actionsetName);
1191 unsigned int num = GetNumOfTargetResource(
1194 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1195 HandleAggregateResponse;
1196 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1199 DoAction(resource, actionset,
1200 (OCServerRequest*) ehRequest->requestHandle);
1201 stackRet = OC_STACK_OK;
1205 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1206 actionset->actionsetName);
1209 (delay == -1 ? actionset->timesteps : delay);
1211 ScheduledResourceInfo *schedule;
1212 schedule = (ScheduledResourceInfo *) OICMalloc(
1213 sizeof(ScheduledResourceInfo));
1217 OC_LOG(INFO, TAG, PCF("Building New Call Info."));
1219 sizeof(ScheduledResourceInfo));
1221 schedule->resource = resource;
1222 schedule->actionset = actionset;
1223 schedule->ehRequest =
1224 (OCServerRequest*) ehRequest->requestHandle;
1228 OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
1229 actionset->timesteps);
1231 schedule->time = registerTimer(delay,
1232 &schedule->timer_id,
1233 &DoScheduledGroupAction);
1235 AddScheduledResource(&scheduleResourceList,
1237 stackRet = OC_STACK_OK;
1241 stackRet = OC_STACK_ERROR;
1248 else if (strcmp(doWhat, "CancelAction") == 0)
1250 ScheduledResourceInfo *info =
1251 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1255 unregisterTimer(info->timer_id);
1257 RemoveScheduledResource(&scheduleResourceList, info);
1258 stackRet = OC_STACK_OK;
1262 stackRet = OC_STACK_ERROR;
1266 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1268 char *plainText = NULL;
1269 OCActionSet *actionset = NULL;
1271 cJSON_AddItemToObject(json, "rep", format =
1272 cJSON_CreateObject());
1273 GetActionSet(details, resource->actionsetHead, &actionset);
1274 if (actionset != NULL)
1276 BuildStringFromActionSet(actionset, &plainText);
1278 if (plainText != NULL)
1280 cJSON_AddStringToObject(format, ACTIONSET, plainText);
1283 stackRet = OC_STACK_OK;
1288 jsonResponse = cJSON_Print(json);
1291 OICStrcat((char*)buffer, sizeof(buffer), jsonResponse);
1293 bufferLength = strlen((const char *) buffer);
1294 if (bufferLength > 0)
1296 OCEntityHandlerResponse response = { 0 };
1297 if(stackRet == OC_STACK_OK)
1298 response.ehResult = OC_EH_OK;
1300 response.ehResult = OC_EH_ERROR;
1302 //response.payload = (char *)buffer;
1303 //response.payloadSize = bufferLength + 1;
1304 response.persistentBufferFlag = 0;
1305 response.requestHandle =
1306 (OCRequestHandle) ehRequest->requestHandle;
1307 response.resourceHandle = (OCResourceHandle) collResource;
1308 stackRet = OCDoResponse(&response);