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 "ocpayload.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31 #include "occollection.h"
39 #define TAG "OICGROUP"
41 #define DESC_DELIMITER "\""
42 #define ACTION_DELIMITER "*"
43 #define ATTR_DELIMITER "|"
44 #define ATTR_ASSIGN "="
46 // Definitions for operations related to actions
47 #define DO_ACTION "DoAction"
48 #define GET_ACTIONSET "GetActionSet"
49 #define ACTIONSET "ActionSet"
50 #define CANCEL_ACTIONSET "CancelAction"
51 #define DELETE_ACTIONSET "DelActionSet"
53 #define DEFAULT_CONTEXT_VALUE 0x99
55 #define VARIFY_POINTER_NULL(pointer, result, toExit) \
58 result = OC_STACK_NO_MEMORY;\
61 #define VARIFY_PARAM_NULL(pointer, result, toExit) \
64 result = OC_STACK_INVALID_PARAM;\
68 #define OCFREE(pointer) \
80 NONE = 0, SCHEDULED, RECURSIVE
83 typedef struct scheduledresourceinfo
86 OCActionSet *actionset;
90 OCServerRequest *ehRequest;
93 struct scheduledresourceinfo* next;
94 } ScheduledResourceInfo;
96 ScheduledResourceInfo *scheduleResourceList = NULL;
98 void AddScheduledResource(ScheduledResourceInfo **head,
99 ScheduledResourceInfo* add)
101 OC_LOG(INFO, TAG, "AddScheduledResource Entering...");
104 pthread_mutex_lock(&lock);
106 ScheduledResourceInfo *tmp = NULL;
123 pthread_mutex_unlock(&lock);
127 ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
129 OC_LOG(INFO, TAG, "GetScheduledResource Entering...");
132 pthread_mutex_lock(&lock);
137 ScheduledResourceInfo *tmp = NULL;
152 diffTm = timespec_diff(tmp->time, t_now);
154 diffTm = timespec_diff(tmp->time, t_now);
157 if (diffTm <= (time_t) 0)
159 OC_LOG(INFO, TAG, "return Call INFO.");
169 pthread_mutex_unlock(&lock);
173 OC_LOG(INFO, TAG, "Cannot Find Call Info.");
178 ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
180 OC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering...");
183 pthread_mutex_lock(&lock);
185 ScheduledResourceInfo *tmp = NULL;
192 if (strcmp(tmp->actionset->actionsetName, setName) == 0)
194 OC_LOG(INFO, TAG, "return Call INFO.");
203 pthread_mutex_unlock(&lock);
207 OC_LOG(INFO, TAG, "Cannot Find Call Info.");
212 void RemoveScheduledResource(ScheduledResourceInfo **head,
213 ScheduledResourceInfo* del)
216 pthread_mutex_lock(&lock);
218 OC_LOG(INFO, TAG, "RemoveScheduledResource Entering...");
219 ScheduledResourceInfo *tmp = NULL;
224 pthread_mutex_unlock(&lock);
231 *head = (*head)->next;
236 while (tmp->next && (tmp->next != del))
242 tmp->next = del->next;
248 pthread_mutex_unlock(&lock);
252 typedef struct aggregatehandleinfo
254 OCServerRequest *ehRequest;
256 OCResource *collResource;
258 struct aggregatehandleinfo *next;
261 ClientRequestInfo *clientRequstList = NULL;
263 void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
265 ClientRequestInfo *tmp = NULL;
283 ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head,
286 ClientRequestInfo *tmp = NULL;
294 if (tmp->required == handle)
307 void RemoveClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* del)
309 ClientRequestInfo *tmp = NULL;
316 *head = (*head)->next;
321 while (tmp->next && (tmp->next != del))
327 tmp->next = del->next;
332 void AddCapability(OCCapability** head, OCCapability* node)
334 OCCapability *pointer = *head;
341 while (pointer->next != NULL)
343 pointer = pointer->next;
346 pointer->next = node;
350 void AddAction(OCAction** head, OCAction* node)
352 OCAction *pointer = *head;
360 while (pointer->next != NULL)
362 pointer = pointer->next;
365 pointer->next = node;
369 OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node)
371 OCActionSet *pointer = *head;
372 OCActionSet *prev = NULL;
375 return OC_STACK_ERROR;
384 while (pointer != NULL)
386 // check the uniqeness of actionsetname.
387 if (strcmp(pointer->actionsetName, node->actionsetName) == 0)
389 return OC_STACK_ERROR;
393 pointer = pointer->next;
402 void DeleteCapability(OCCapability *del)
404 OCFREE(del->capability)
405 del->capability = NULL;
411 void DeleteAction(OCAction** action)
413 OCCapability* pointer = (*action)->head;
414 OCCapability* pDel = NULL;
419 pointer = pointer->next;
421 DeleteCapability(pDel);
423 OCFREE((*action)->resourceUri)
424 (*action)->next = NULL;
428 void DeleteActionSet(OCActionSet** actionset)
430 if(*actionset == NULL)
433 OCAction* pointer = (*actionset)->head;
434 OCAction* pDel = NULL;
439 pointer = pointer->next;
443 // (*actionset)->head = NULL;
444 OCFREE((*actionset)->actionsetName)
448 OCStackResult FindAndDeleteActionSet(OCResource **resource,
449 const char * actionsetName)
451 if (*resource != NULL)
453 OCActionSet *pointer = NULL;
454 OCActionSet *pDel = NULL;
456 pointer = (*resource)->actionsetHead;
460 return OC_STACK_ERROR;
464 if (strcmp(pointer->actionsetName, actionsetName) == 0)
466 if (pointer->next != NULL)
467 (*resource)->actionsetHead = pointer->next;
469 (*resource)->actionsetHead = NULL;
471 DeleteActionSet(&pointer);
473 else if (pointer->next != NULL)
477 if (pointer->next != NULL)
479 if (strcmp(pointer->next->actionsetName, actionsetName)
482 pDel = pointer->next;
483 pointer->next = pointer->next->next;
485 DeleteActionSet(&pDel);
488 pointer = pointer->next;
496 return OC_STACK_ERROR;
499 OCStackResult DeleteActionSets(OCResource** resource)
501 OCActionSet *pointer = (*resource)->actionsetHead;
502 OCActionSet *pDel = pointer;
507 pointer = pointer->next;
509 DeleteActionSet(&pDel);
513 (*resource)->actionsetHead = NULL;
517 OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
518 OCActionSet** actionset)
520 OCActionSet *pointer = head;
524 if (strcmp(pointer->actionsetName, actionName) == 0)
526 *actionset = pointer;
530 pointer = pointer->next;
533 return OC_STACK_ERROR;
537 OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest,
538 char **key, char **value)
540 OCStackResult result = OC_STACK_OK;
542 char *actionSetStr = NULL;
544 if( NULL == ehRequest->payload )
546 result = OC_STACK_ERROR;
552 input = (OCRepPayload*)(ehRequest->payload);
554 if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
556 *key = OICStrdup(ACTIONSET);
557 VARIFY_POINTER_NULL(*key, result, exit);
559 *value = OICStrdup(actionSetStr);
560 VARIFY_POINTER_NULL(*value, result, exit);
562 else if(OCRepPayloadGetPropString(input, DO_ACTION, &actionSetStr))
564 *key = OICStrdup(DO_ACTION);
565 VARIFY_POINTER_NULL(*key, result, exit);
567 *value = OICStrdup(actionSetStr);
568 VARIFY_POINTER_NULL(*value, result, exit);
570 else if(OCRepPayloadGetPropString(input, GET_ACTIONSET, &actionSetStr))
572 *key = OICStrdup(GET_ACTIONSET);
573 VARIFY_POINTER_NULL(*key, result, exit);
575 *value = OICStrdup(actionSetStr);
576 VARIFY_POINTER_NULL(*value, result, exit);
578 else if(OCRepPayloadGetPropString(input, DELETE_ACTIONSET, &actionSetStr))
580 *key = OICStrdup(DELETE_ACTIONSET);
581 VARIFY_POINTER_NULL(*key, result, exit);
583 *value = OICStrdup(actionSetStr);
584 VARIFY_POINTER_NULL(*value, result, exit);
586 else if(OCRepPayloadGetPropString(input, CANCEL_ACTIONSET, &actionSetStr))
588 *key = OICStrdup(CANCEL_ACTIONSET);
589 VARIFY_POINTER_NULL(*key, result, exit);
591 *value = OICStrdup(actionSetStr);
592 VARIFY_POINTER_NULL(*value, result, exit);
596 result = OC_STACK_ERROR;
600 if (result != OC_STACK_OK)
606 OCFREE(actionSetStr);
611 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
614 char *token = NULL, *tokenPtr = NULL;
615 OCStackResult result = OC_STACK_OK;
617 token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
618 VARIFY_POINTER_NULL(token, result, exit)
620 *setName = (char *) OICMalloc(strlen(token) + 1);
621 VARIFY_POINTER_NULL(*setName, result, exit)
622 VARIFY_PARAM_NULL(token, result, exit)
623 strncpy(*setName, token, strlen(token) + 1);
625 token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
626 VARIFY_POINTER_NULL(pa, result, exit)
627 VARIFY_PARAM_NULL(token, result, exit)
637 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
639 OCStackResult result = OC_STACK_OK;
641 char *iterToken = NULL, *iterTokenPtr = NULL;
642 char *descIterToken = NULL, *descIterTokenPtr = NULL;
643 char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
644 char *desc = NULL, *attr = NULL;
645 char *key = NULL, *value = NULL;
647 OCAction *action = NULL;
648 OCCapability *capa = NULL;
650 OC_LOG(INFO, TAG, "Build ActionSet Instance.");
652 *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
653 VARIFY_POINTER_NULL(*set, result, exit)
655 iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
656 VARIFY_POINTER_NULL(iterToken, result, exit);
659 memset(*set, 0, sizeof(OCActionSet));
660 (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
661 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
662 VARIFY_PARAM_NULL(iterToken, result, exit)
663 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
665 // Time info. for Scheduled/Recursive Group action.
666 // d is meant Day of the week.
667 // T is meant ActionType.
668 // yyyy-mm-dd hh:mm:ss d
669 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
670 VARIFY_PARAM_NULL(iterToken, result, exit)
672 if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
674 // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
679 OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
681 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
684 desc = (char *) OICMalloc(strlen(iterToken) + 1);
685 VARIFY_POINTER_NULL(desc, result, exit)
686 VARIFY_PARAM_NULL(desc, result, exit)
687 strncpy(desc, iterToken, strlen(iterToken) + 1);
688 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
690 while (descIterToken)
692 attr = (char *) OICMalloc(strlen(descIterToken) + 1);
693 VARIFY_POINTER_NULL(attr, result, exit)
694 VARIFY_PARAM_NULL(descIterToken, result, exit)
695 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
697 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
699 VARIFY_POINTER_NULL(attrIterToken, result, exit);
701 key = (char *) OICMalloc(strlen(attrIterToken) + 1);
702 VARIFY_POINTER_NULL(key, result, exit)
703 VARIFY_PARAM_NULL(attrIterToken, result, exit)
704 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
706 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
708 VARIFY_POINTER_NULL(attrIterToken, result, exit);
709 value = (char *) OICMalloc(strlen(attrIterToken) + 1);
710 VARIFY_POINTER_NULL(value, result, exit)
711 VARIFY_PARAM_NULL(attrIterToken, result, exit)
712 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
714 if (strcmp(key, "uri") == 0)
716 OC_LOG(INFO, TAG, "Build OCAction Instance.");
718 action = (OCAction*) OICMalloc(sizeof(OCAction));
719 VARIFY_POINTER_NULL(action, result, exit)
720 memset(action, 0, sizeof(OCAction));
721 action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
722 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
723 VARIFY_PARAM_NULL(value, result, exit)
724 strncpy(action->resourceUri, value, strlen(value) + 1);
728 if ((key != NULL) && (value != NULL))
730 OC_LOG(INFO, TAG, "Build OCCapability Instance.");
732 capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
733 VARIFY_POINTER_NULL(capa, result, exit)
734 memset(capa, 0, sizeof(OCCapability));
736 capa->capability = (char *) OICMalloc(strlen(key) + 1);
737 VARIFY_POINTER_NULL(capa->capability, result, exit)
738 VARIFY_PARAM_NULL(key, result, exit)
739 strncpy(capa->capability, key, strlen(key) + 1);
741 capa->status = (char *) OICMalloc(strlen(value) + 1);
742 VARIFY_POINTER_NULL(capa->status, result, exit)
743 VARIFY_PARAM_NULL(value, result, exit)
744 strncpy(capa->status, value, strlen(value) + 1);
746 VARIFY_POINTER_NULL(action, result, exit)
748 AddCapability(&action->head, capa);
756 descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
760 AddAction(&(*set)->head, action);
761 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
779 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
781 // Can't use the macros here as they are hardcoded to 'exit' and will
782 // result in dereferencing a null pointer.
783 if (!actionset || !desc)
785 return OC_STACK_INVALID_PARAM;
787 char temp[1024] = { 0 };
788 size_t remaining = sizeof(temp) - 1;
789 OCStackResult res = OC_STACK_ERROR;
790 char* actionTypeStr = NULL;
792 OCAction *action = actionset->head;
794 if (remaining >= strlen(actionset->actionsetName) + 1)
796 strncat(temp, actionset->actionsetName, strlen(actionset->actionsetName));
797 remaining -= strlen(actionset->actionsetName);
798 strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
803 res = OC_STACK_ERROR;
807 actionTypeStr = (char *)malloc(1024);
808 if(actionTypeStr != NULL)
810 sprintf(actionTypeStr, "%ld %u", actionset->timesteps, actionset->type);
811 strncat(temp, actionTypeStr, strlen(actionTypeStr));
812 remaining -= strlen(actionTypeStr);
814 strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
819 res = OC_STACK_ERROR;
823 while (action != NULL)
825 if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
827 res = OC_STACK_ERROR;
830 strcat(temp, "uri=");
831 remaining -= strlen("uri=");
832 strcat(temp, action->resourceUri);
833 remaining -= strlen(action->resourceUri);
837 OCCapability *capas = action->head;
838 while (capas != NULL)
840 if (remaining < (strlen(capas->capability)
841 + 1 + strlen(capas->status)))
843 res = OC_STACK_ERROR;
847 strcat(temp, capas->capability);
848 remaining -= strlen(capas->capability);
851 strcat(temp, capas->status);
852 remaining -= strlen(capas->status);
859 res = OC_STACK_ERROR;
866 action = action->next;
869 if (remaining < strlen(ACTION_DELIMITER))
871 res = OC_STACK_ERROR;
874 strcat(temp, ACTION_DELIMITER);
879 *desc = OICStrdup(temp);
880 VARIFY_POINTER_NULL(*desc, res, exit);
889 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
890 OCClientResponse* clientResponse)
893 (void)clientResponse;
894 OC_LOG(INFO, TAG, "Entering ActionSetCB");
896 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
900 OCEntityHandlerResponse response = { 0 };
902 response.ehResult = OC_EH_OK;
904 if(NULL == clientResponse->payload)
906 OC_LOG(ERROR, TAG, "Error sending response");
907 return OC_STACK_DELETE_TRANSACTION;
910 // Format the response. Note this requires some info about the request
911 response.requestHandle = info->ehRequest;
912 response.resourceHandle = info->collResource;
913 response.payload = clientResponse->payload;
914 response.numSendVendorSpecificHeaderOptions = 0;
915 memset(response.sendVendorSpecificHeaderOptions, 0,
916 sizeof response.sendVendorSpecificHeaderOptions);
917 memset(response.resourceUri, 0, sizeof response.resourceUri);
918 // Indicate that response is NOT in a persistent buffer
919 response.persistentBufferFlag = 0;
922 if (OCDoResponse(&response) != OC_STACK_OK)
924 OC_LOG(ERROR, TAG, "Error sending response");
925 return OC_STACK_DELETE_TRANSACTION;
928 RemoveClientRequestInfo(&clientRequstList, info);
932 return OC_STACK_KEEP_TRANSACTION;
935 void ActionSetCD(void *context)
940 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
943 OCStackResult ret = OC_STACK_ERROR;
950 OC_LOG(INFO, TAG, "Entering BuildActionJSON");
951 json = cJSON_CreateObject();
953 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
955 OCCapability* pointerCapa = action->head;
958 cJSON_AddStringToObject(body, pointerCapa->capability,
959 pointerCapa->status);
960 pointerCapa = pointerCapa->next;
963 jsonStr = cJSON_PrintUnformatted(json);
965 jsonLen = strlen(jsonStr);
966 if (jsonLen < *remaining)
968 strncat((char*) bufferPtr, jsonStr, jsonLen);
969 *remaining -= jsonLen;
970 bufferPtr += jsonLen;
980 OCPayload* BuildActionCBOR(OCAction* action)
982 OCRepPayload* payload = OCRepPayloadCreate();
986 OC_LOG(INFO, TAG, "Failed to create put payload object");
990 OCCapability* pointerCapa = action->head;
993 OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
994 pointerCapa = pointerCapa->next;
997 return (OCPayload*) payload;
1000 unsigned int GetNumOfTargetResource(OCAction *actionset)
1002 int numOfResource = 0;
1004 OCAction *pointerAction = actionset;
1006 while (pointerAction != NULL)
1009 pointerAction = pointerAction->next;
1012 return numOfResource;
1015 OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
1019 OCCallbackData cbData;
1020 cbData.cb = &ActionSetCB;
1021 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
1024 return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
1025 payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
1028 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
1029 OCServerRequest* requestHandle)
1031 OCStackResult result = OC_STACK_ERROR;
1033 if( NULL == actionset->head)
1038 OCAction *pointerAction = actionset->head;
1040 while (pointerAction != NULL)
1043 payload = BuildActionCBOR(pointerAction);
1050 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
1051 sizeof(ClientRequestInfo));
1056 return OC_STACK_NO_MEMORY;
1059 memset(info, 0, sizeof(ClientRequestInfo));
1061 info->collResource = resource;
1062 info->ehRequest = requestHandle;
1064 result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
1067 if (result != OC_STACK_OK)
1073 AddClientRequestInfo(&clientRequstList, info);
1075 pointerAction = pointerAction->next;
1081 void DoScheduledGroupAction()
1083 OC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
1084 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
1088 OC_LOG(INFO, TAG, "Target resource is NULL");
1091 else if (info->resource == NULL)
1093 OC_LOG(INFO, TAG, "Target resource is NULL");
1096 else if (info->actionset == NULL)
1098 OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1101 else if (info->ehRequest == NULL)
1103 OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1106 #ifndef WITH_ARDUINO
1107 pthread_mutex_lock(&lock);
1109 DoAction(info->resource, info->actionset, info->ehRequest);
1110 #ifndef WITH_ARDUINO
1111 pthread_mutex_unlock(&lock);
1114 if (info->actionset->type == RECURSIVE)
1116 ScheduledResourceInfo *schedule;
1117 schedule = (ScheduledResourceInfo *) OICMalloc(
1118 sizeof(ScheduledResourceInfo));
1122 OC_LOG(INFO, TAG, "Building New Call Info.");
1123 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1125 if (info->actionset->timesteps > 0)
1127 #ifndef WITH_ARDUINO
1128 pthread_mutex_lock(&lock);
1130 schedule->resource = info->resource;
1131 schedule->actionset = info->actionset;
1132 schedule->ehRequest = info->ehRequest;
1134 schedule->time = registerTimer(info->actionset->timesteps,
1135 &schedule->timer_id,
1136 &DoScheduledGroupAction);
1138 OC_LOG(INFO, TAG, "Reregisteration.");
1139 #ifndef WITH_ARDUINO
1140 pthread_mutex_unlock(&lock);
1142 AddScheduledResource(&scheduleResourceList, schedule);
1151 RemoveScheduledResource(&scheduleResourceList, info);
1158 OCStackResult BuildCollectionGroupActionCBORResponse(
1159 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1160 OCEntityHandlerRequest *ehRequest)
1162 OCStackResult stackRet = OC_STACK_ERROR;
1164 OC_LOG(INFO, TAG, "Group Action is requested.");
1166 char *doWhat = NULL;
1167 char *details = NULL;
1169 stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
1171 if(stackRet != OC_STACK_OK)
1173 OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1177 stackRet = OC_STACK_ERROR;
1179 if (method == OC_REST_PUT)
1181 OC_LOG(INFO, TAG, "Group Action[PUT].");
1183 if (strcmp(doWhat, ACTIONSET) == 0)
1185 OCActionSet *actionSet = NULL;
1186 stackRet = BuildActionSetFromString(&actionSet, details);
1188 if(stackRet == OC_STACK_OK)
1190 if (actionSet != NULL)
1192 stackRet = AddActionSet(&resource->actionsetHead,
1194 if (stackRet == OC_STACK_ERROR)
1196 if(actionSet != NULL)
1198 DeleteActionSet( &actionSet );
1200 OC_LOG(INFO, TAG, "Duplicated ActionSet ");
1205 stackRet = OC_STACK_ERROR;
1211 stackRet = OC_STACK_ERROR;
1215 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1217 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1219 stackRet = OC_STACK_OK;
1223 stackRet = OC_STACK_ERROR;
1227 OCRepPayload* payload = OCRepPayloadCreate();
1231 OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1232 stackRet = OC_STACK_ERROR;
1236 OCEntityHandlerResponse response = { 0 };
1238 if(stackRet == OC_STACK_OK)
1239 response.ehResult = OC_EH_OK;
1241 response.ehResult = OC_EH_ERROR;
1243 // Format the response. Note this requires some info about the request
1244 response.requestHandle = ehRequest->requestHandle;
1245 response.resourceHandle = ehRequest->resource;
1246 response.payload = (OCPayload*) payload;
1247 response.numSendVendorSpecificHeaderOptions = 0;
1248 memset(response.sendVendorSpecificHeaderOptions, 0,
1249 sizeof response.sendVendorSpecificHeaderOptions);
1250 memset(response.resourceUri, 0, sizeof response. resourceUri);
1251 // Indicate that response is NOT in a persistent buffer
1252 response.persistentBufferFlag = 0;
1253 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1255 // Send the response
1256 if (OCDoResponse(&response) != OC_STACK_OK)
1258 OC_LOG(ERROR, TAG, "Error sending response");
1259 stackRet = OC_STACK_ERROR;
1263 else if (method == OC_REST_POST)
1265 OCActionSet *actionset = NULL;
1267 OCRepPayload* payload = OCRepPayloadCreate();
1268 OCRepPayloadSetUri(payload, resource->uri);
1270 if ((strcmp(doWhat, DO_ACTION) == 0)
1271 || (strcmp(doWhat, "DoScheduledAction") == 0))
1273 char *pActionsetName = NULL;
1274 long int delay = -1;
1276 if (strcmp(doWhat, "DoScheduledAction") == 0)
1278 stackRet = ExtractActionSetNameAndDelaytime(details,
1279 &pActionsetName, &delay);
1282 details = pActionsetName;
1286 stackRet = OC_STACK_OK;
1289 if (stackRet == OC_STACK_OK)
1291 if (GetActionSet(details, resource->actionsetHead,
1292 &actionset) != OC_STACK_OK)
1294 OC_LOG(INFO, TAG, "ERROR");
1295 stackRet = OC_STACK_ERROR;
1298 if (actionset == NULL)
1300 OC_LOG(INFO, TAG, "Cannot Find ActionSet");
1301 stackRet = OC_STACK_ERROR;
1305 OC_LOG(INFO, TAG, "Group Action[POST].");
1306 if (actionset->type == NONE)
1308 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1309 actionset->actionsetName);
1310 unsigned int num = GetNumOfTargetResource(
1313 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1314 HandleAggregateResponse;
1315 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1318 DoAction(resource, actionset,
1319 (OCServerRequest*) ehRequest->requestHandle);
1320 stackRet = OC_STACK_OK;
1324 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1325 actionset->actionsetName);
1328 (delay == -1 ? actionset->timesteps : delay);
1330 ScheduledResourceInfo *schedule;
1331 schedule = (ScheduledResourceInfo *) OICMalloc(
1332 sizeof(ScheduledResourceInfo));
1336 OC_LOG(INFO, TAG, "Building New Call Info.");
1338 sizeof(ScheduledResourceInfo));
1339 #ifndef WITH_ARDUINO
1340 pthread_mutex_lock(&lock);
1342 schedule->resource = resource;
1343 schedule->actionset = actionset;
1344 schedule->ehRequest =
1345 (OCServerRequest*) ehRequest->requestHandle;
1346 #ifndef WITH_ARDUINO
1347 pthread_mutex_unlock(&lock);
1351 OC_LOG_V(INFO, TAG, "delay_time is %ld seconds.",
1352 actionset->timesteps);
1353 #ifndef WITH_ARDUINO
1354 pthread_mutex_lock(&lock);
1356 schedule->time = registerTimer(delay,
1357 &schedule->timer_id,
1358 &DoScheduledGroupAction);
1359 #ifndef WITH_ARDUINO
1360 pthread_mutex_unlock(&lock);
1362 AddScheduledResource(&scheduleResourceList,
1364 stackRet = OC_STACK_OK;
1368 stackRet = OC_STACK_ERROR;
1375 else if (strcmp(doWhat, "CancelAction") == 0)
1377 ScheduledResourceInfo *info =
1378 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1382 unregisterTimer(info->timer_id);
1384 RemoveScheduledResource(&scheduleResourceList, info);
1385 stackRet = OC_STACK_OK;
1389 stackRet = OC_STACK_ERROR;
1393 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1395 char *plainText = NULL;
1396 OCActionSet *actionset = NULL;
1398 GetActionSet(details, resource->actionsetHead, &actionset);
1399 if (actionset != NULL)
1401 BuildStringFromActionSet(actionset, &plainText);
1403 if (plainText != NULL)
1405 OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
1408 stackRet = OC_STACK_OK;
1414 OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1415 stackRet = OC_STACK_ERROR;
1419 OCEntityHandlerResponse response = { 0 };
1420 if(stackRet == OC_STACK_OK)
1421 response.ehResult = OC_EH_OK;
1423 response.ehResult = OC_EH_ERROR;
1425 // Format the response. Note this requires some info about the request
1426 response.requestHandle = ehRequest->requestHandle;
1427 response.resourceHandle = ehRequest->resource;
1428 response.payload = (OCPayload*) payload;
1429 response.numSendVendorSpecificHeaderOptions = 0;
1430 memset(response.sendVendorSpecificHeaderOptions, 0,
1431 sizeof response.sendVendorSpecificHeaderOptions);
1432 memset(response.resourceUri, 0, sizeof response.resourceUri);
1433 // Indicate that response is NOT in a persistent buffer
1434 response.persistentBufferFlag = 0;
1435 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1437 // Send the response
1438 if (OCDoResponse(&response) != OC_STACK_OK)
1440 OC_LOG(ERROR, TAG, "Error sending response");
1441 stackRet = OC_STACK_ERROR;