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 "OIC_RI_GROUP"
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 OIC_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 OIC_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 OIC_LOG(INFO, TAG, "return Call INFO.");
169 pthread_mutex_unlock(&lock);
173 OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
178 ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
180 OIC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering...");
183 pthread_mutex_lock(&lock);
185 ScheduledResourceInfo *tmp = NULL;
192 if (strcmp(tmp->actionset->actionsetName, setName) == 0)
194 OIC_LOG(INFO, TAG, "return Call INFO.");
203 pthread_mutex_unlock(&lock);
207 OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
212 void RemoveScheduledResource(ScheduledResourceInfo **head,
213 ScheduledResourceInfo* del)
216 pthread_mutex_lock(&lock);
218 OIC_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 GetActionSet(const char *actionName, OCActionSet *head,
500 OCActionSet** actionset)
502 OCActionSet *pointer = head;
506 if (strcmp(pointer->actionsetName, actionName) == 0)
508 *actionset = pointer;
512 pointer = pointer->next;
515 return OC_STACK_ERROR;
519 OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest,
520 char **key, char **value)
522 OCStackResult result = OC_STACK_OK;
524 char *actionSetStr = NULL;
526 if( NULL == ehRequest->payload )
528 result = OC_STACK_ERROR;
534 input = (OCRepPayload*)(ehRequest->payload);
536 if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
538 *key = OICStrdup(ACTIONSET);
539 VARIFY_POINTER_NULL(*key, result, exit);
541 *value = OICStrdup(actionSetStr);
542 VARIFY_POINTER_NULL(*value, result, exit);
544 else if(OCRepPayloadGetPropString(input, DO_ACTION, &actionSetStr))
546 *key = OICStrdup(DO_ACTION);
547 VARIFY_POINTER_NULL(*key, result, exit);
549 *value = OICStrdup(actionSetStr);
550 VARIFY_POINTER_NULL(*value, result, exit);
552 else if(OCRepPayloadGetPropString(input, GET_ACTIONSET, &actionSetStr))
554 *key = OICStrdup(GET_ACTIONSET);
555 VARIFY_POINTER_NULL(*key, result, exit);
557 *value = OICStrdup(actionSetStr);
558 VARIFY_POINTER_NULL(*value, result, exit);
560 else if(OCRepPayloadGetPropString(input, DELETE_ACTIONSET, &actionSetStr))
562 *key = OICStrdup(DELETE_ACTIONSET);
563 VARIFY_POINTER_NULL(*key, result, exit);
565 *value = OICStrdup(actionSetStr);
566 VARIFY_POINTER_NULL(*value, result, exit);
568 else if(OCRepPayloadGetPropString(input, CANCEL_ACTIONSET, &actionSetStr))
570 *key = OICStrdup(CANCEL_ACTIONSET);
571 VARIFY_POINTER_NULL(*key, result, exit);
573 *value = OICStrdup(actionSetStr);
574 VARIFY_POINTER_NULL(*value, result, exit);
578 result = OC_STACK_ERROR;
582 if (result != OC_STACK_OK)
588 OCFREE(actionSetStr);
593 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
596 char *token = NULL, *tokenPtr = NULL;
597 OCStackResult result = OC_STACK_OK;
599 token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
600 VARIFY_POINTER_NULL(token, result, exit)
602 *setName = (char *) OICMalloc(strlen(token) + 1);
603 VARIFY_POINTER_NULL(*setName, result, exit)
604 VARIFY_PARAM_NULL(token, result, exit)
605 strncpy(*setName, token, strlen(token) + 1);
607 token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
608 VARIFY_POINTER_NULL(pa, result, exit)
609 VARIFY_PARAM_NULL(token, result, exit)
619 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
621 OCStackResult result = OC_STACK_OK;
623 char *iterToken = NULL, *iterTokenPtr = NULL;
624 char *descIterToken = NULL, *descIterTokenPtr = NULL;
625 char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
626 char *desc = NULL, *attr = NULL;
627 char *key = NULL, *value = NULL;
629 OCAction *action = NULL;
630 OCCapability *capa = NULL;
632 OIC_LOG(INFO, TAG, "Build ActionSet Instance.");
634 *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
635 VARIFY_POINTER_NULL(*set, result, exit)
637 iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
638 VARIFY_POINTER_NULL(iterToken, result, exit);
641 memset(*set, 0, sizeof(OCActionSet));
642 (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
643 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
644 VARIFY_PARAM_NULL(iterToken, result, exit)
645 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
647 // Time info. for Scheduled/Recursive Group action.
648 // d is meant Day of the week.
649 // T is meant ActionType.
650 // yyyy-mm-dd hh:mm:ss d
651 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
652 VARIFY_PARAM_NULL(iterToken, result, exit)
654 if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
656 // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
661 OIC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
663 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
666 desc = (char *) OICMalloc(strlen(iterToken) + 1);
667 VARIFY_POINTER_NULL(desc, result, exit)
668 VARIFY_PARAM_NULL(desc, result, exit)
669 strncpy(desc, iterToken, strlen(iterToken) + 1);
670 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
672 while (descIterToken)
674 attr = (char *) OICMalloc(strlen(descIterToken) + 1);
675 VARIFY_POINTER_NULL(attr, result, exit)
676 VARIFY_PARAM_NULL(descIterToken, result, exit)
677 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
679 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
681 VARIFY_POINTER_NULL(attrIterToken, result, exit);
683 key = (char *) OICMalloc(strlen(attrIterToken) + 1);
684 VARIFY_POINTER_NULL(key, result, exit)
685 VARIFY_PARAM_NULL(attrIterToken, result, exit)
686 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
688 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
690 VARIFY_POINTER_NULL(attrIterToken, result, exit);
691 value = (char *) OICMalloc(strlen(attrIterToken) + 1);
692 VARIFY_POINTER_NULL(value, result, exit)
693 VARIFY_PARAM_NULL(attrIterToken, result, exit)
694 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
696 if (strcmp(key, "uri") == 0)
698 OIC_LOG(INFO, TAG, "Build OCAction Instance.");
700 action = (OCAction*) OICMalloc(sizeof(OCAction));
701 VARIFY_POINTER_NULL(action, result, exit)
702 memset(action, 0, sizeof(OCAction));
703 action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
704 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
705 VARIFY_PARAM_NULL(value, result, exit)
706 strncpy(action->resourceUri, value, strlen(value) + 1);
710 if ((key != NULL) && (value != NULL))
712 OIC_LOG(INFO, TAG, "Build OCCapability Instance.");
714 capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
715 VARIFY_POINTER_NULL(capa, result, exit)
716 memset(capa, 0, sizeof(OCCapability));
718 capa->capability = (char *) OICMalloc(strlen(key) + 1);
719 VARIFY_POINTER_NULL(capa->capability, result, exit)
720 VARIFY_PARAM_NULL(key, result, exit)
721 strncpy(capa->capability, key, strlen(key) + 1);
723 capa->status = (char *) OICMalloc(strlen(value) + 1);
724 VARIFY_POINTER_NULL(capa->status, result, exit)
725 VARIFY_PARAM_NULL(value, result, exit)
726 strncpy(capa->status, value, strlen(value) + 1);
728 VARIFY_POINTER_NULL(action, result, exit)
730 AddCapability(&action->head, capa);
738 descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
742 AddAction(&(*set)->head, action);
743 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
761 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
763 // Can't use the macros here as they are hardcoded to 'exit' and will
764 // result in dereferencing a null pointer.
765 if (!actionset || !desc)
767 return OC_STACK_INVALID_PARAM;
769 char temp[1024] = { 0 };
770 size_t remaining = sizeof(temp) - 1;
771 OCStackResult res = OC_STACK_ERROR;
772 char* actionTypeStr = NULL;
774 OCAction *action = actionset->head;
776 if (remaining >= strlen(actionset->actionsetName) + 1)
778 strncat(temp, actionset->actionsetName, strlen(actionset->actionsetName));
779 remaining -= strlen(actionset->actionsetName);
780 strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
785 res = OC_STACK_ERROR;
789 actionTypeStr = (char *)malloc(1024);
790 if(actionTypeStr != NULL && remaining >= strlen(actionTypeStr) + strlen(ACTION_DELIMITER) + 1)
792 sprintf(actionTypeStr, "%ld %u", actionset->timesteps, actionset->type);
793 strncat(temp, actionTypeStr, strlen(actionTypeStr));
794 remaining -= strlen(actionTypeStr);
796 strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
797 remaining -= strlen(ACTION_DELIMITER);
801 res = OC_STACK_ERROR;
805 while (action != NULL)
807 if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
809 res = OC_STACK_ERROR;
813 strcat(temp, "uri=");
814 remaining -= strlen("uri=");
815 strcat(temp, action->resourceUri);
816 remaining -= strlen(action->resourceUri);
820 OCCapability *capas = action->head;
821 while (capas != NULL)
823 if (remaining < (strlen(capas->capability)
824 + 1 + strlen(capas->status)))
826 res = OC_STACK_ERROR;
830 strcat(temp, capas->capability);
831 remaining -= strlen(capas->capability);
834 strcat(temp, capas->status);
835 remaining -= strlen(capas->status);
842 res = OC_STACK_ERROR;
850 action = action->next;
853 if (remaining < strlen(ACTION_DELIMITER))
855 res = OC_STACK_ERROR;
858 strcat(temp, ACTION_DELIMITER);
863 *desc = OICStrdup(temp);
864 VARIFY_POINTER_NULL(*desc, res, exit);
873 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
874 OCClientResponse* clientResponse)
877 (void)clientResponse;
878 OIC_LOG(INFO, TAG, "Entering ActionSetCB");
880 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
884 OCEntityHandlerResponse response = { 0 };
886 response.ehResult = OC_EH_OK;
888 if(NULL == clientResponse->payload)
890 OIC_LOG(ERROR, TAG, "Error sending response");
891 return OC_STACK_DELETE_TRANSACTION;
894 // Format the response. Note this requires some info about the request
895 response.requestHandle = info->ehRequest;
896 response.resourceHandle = info->collResource;
897 response.payload = clientResponse->payload;
898 response.numSendVendorSpecificHeaderOptions = 0;
899 memset(response.sendVendorSpecificHeaderOptions, 0,
900 sizeof response.sendVendorSpecificHeaderOptions);
901 memset(response.resourceUri, 0, sizeof response.resourceUri);
902 // Indicate that response is NOT in a persistent buffer
903 response.persistentBufferFlag = 0;
906 if (OCDoResponse(&response) != OC_STACK_OK)
908 OIC_LOG(ERROR, TAG, "Error sending response");
909 return OC_STACK_DELETE_TRANSACTION;
912 RemoveClientRequestInfo(&clientRequstList, info);
916 return OC_STACK_KEEP_TRANSACTION;
919 void ActionSetCD(void *context)
924 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
927 OCStackResult ret = OC_STACK_ERROR;
934 OIC_LOG(INFO, TAG, "Entering BuildActionJSON");
935 json = cJSON_CreateObject();
937 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
939 OCCapability* pointerCapa = action->head;
942 cJSON_AddStringToObject(body, pointerCapa->capability,
943 pointerCapa->status);
944 pointerCapa = pointerCapa->next;
947 jsonStr = cJSON_PrintUnformatted(json);
949 jsonLen = strlen(jsonStr);
950 if (jsonLen < *remaining)
952 strncat((char*) bufferPtr, jsonStr, jsonLen);
953 *remaining -= jsonLen;
954 bufferPtr += jsonLen;
964 OCPayload* BuildActionCBOR(OCAction* action)
966 OCRepPayload* payload = OCRepPayloadCreate();
970 OIC_LOG(INFO, TAG, "Failed to create put payload object");
974 OCCapability* pointerCapa = action->head;
977 OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
978 pointerCapa = pointerCapa->next;
981 return (OCPayload*) payload;
984 unsigned int GetNumOfTargetResource(OCAction *actionset)
986 int numOfResource = 0;
988 OCAction *pointerAction = actionset;
990 while (pointerAction != NULL)
993 pointerAction = pointerAction->next;
996 return numOfResource;
999 OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
1003 OCCallbackData cbData;
1004 cbData.cb = &ActionSetCB;
1005 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
1008 return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
1009 payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
1012 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
1013 OCServerRequest* requestHandle)
1015 OCStackResult result = OC_STACK_ERROR;
1017 if( NULL == actionset->head)
1022 OCAction *pointerAction = actionset->head;
1024 while (pointerAction != NULL)
1027 payload = BuildActionCBOR(pointerAction);
1034 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
1035 sizeof(ClientRequestInfo));
1040 return OC_STACK_NO_MEMORY;
1043 memset(info, 0, sizeof(ClientRequestInfo));
1045 info->collResource = resource;
1046 info->ehRequest = requestHandle;
1048 result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
1051 if (result != OC_STACK_OK)
1057 AddClientRequestInfo(&clientRequstList, info);
1059 pointerAction = pointerAction->next;
1065 void DoScheduledGroupAction()
1067 OIC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
1068 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
1072 OIC_LOG(INFO, TAG, "Target resource is NULL");
1075 else if (info->resource == NULL)
1077 OIC_LOG(INFO, TAG, "Target resource is NULL");
1080 else if (info->actionset == NULL)
1082 OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
1085 else if (info->ehRequest == NULL)
1087 OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
1090 #ifndef WITH_ARDUINO
1091 pthread_mutex_lock(&lock);
1093 DoAction(info->resource, info->actionset, info->ehRequest);
1094 #ifndef WITH_ARDUINO
1095 pthread_mutex_unlock(&lock);
1098 if (info->actionset->type == RECURSIVE)
1100 ScheduledResourceInfo *schedule;
1101 schedule = (ScheduledResourceInfo *) OICMalloc(
1102 sizeof(ScheduledResourceInfo));
1106 OIC_LOG(INFO, TAG, "Building New Call Info.");
1107 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1109 if (info->actionset->timesteps > 0)
1111 #ifndef WITH_ARDUINO
1112 pthread_mutex_lock(&lock);
1114 schedule->resource = info->resource;
1115 schedule->actionset = info->actionset;
1116 schedule->ehRequest = info->ehRequest;
1118 schedule->time = registerTimer(info->actionset->timesteps,
1119 &schedule->timer_id,
1120 &DoScheduledGroupAction);
1122 OIC_LOG(INFO, TAG, "Reregisteration.");
1123 #ifndef WITH_ARDUINO
1124 pthread_mutex_unlock(&lock);
1126 AddScheduledResource(&scheduleResourceList, schedule);
1135 RemoveScheduledResource(&scheduleResourceList, info);
1142 OCStackResult BuildCollectionGroupActionCBORResponse(
1143 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1144 OCEntityHandlerRequest *ehRequest)
1146 OCStackResult stackRet = OC_STACK_ERROR;
1148 OIC_LOG(INFO, TAG, "Group Action is requested.");
1150 char *doWhat = NULL;
1151 char *details = NULL;
1153 stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
1155 if(stackRet != OC_STACK_OK)
1157 OIC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1161 stackRet = OC_STACK_ERROR;
1163 if (method == OC_REST_PUT)
1165 OIC_LOG(INFO, TAG, "Group Action[PUT].");
1167 if (strcmp(doWhat, ACTIONSET) == 0)
1169 OCActionSet *actionSet = NULL;
1170 stackRet = BuildActionSetFromString(&actionSet, details);
1172 if(stackRet == OC_STACK_OK)
1174 if (actionSet != NULL)
1176 stackRet = AddActionSet(&resource->actionsetHead,
1178 if (stackRet == OC_STACK_ERROR)
1180 if(actionSet != NULL)
1182 DeleteActionSet( &actionSet );
1184 OIC_LOG(INFO, TAG, "Duplicated ActionSet ");
1189 stackRet = OC_STACK_ERROR;
1195 stackRet = OC_STACK_ERROR;
1199 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1201 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1203 stackRet = OC_STACK_OK;
1207 stackRet = OC_STACK_ERROR;
1211 OCRepPayload* payload = OCRepPayloadCreate();
1215 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
1216 stackRet = OC_STACK_ERROR;
1220 OCEntityHandlerResponse response = { 0 };
1222 if(stackRet == OC_STACK_OK)
1223 response.ehResult = OC_EH_OK;
1225 response.ehResult = OC_EH_ERROR;
1227 // Format the response. Note this requires some info about the request
1228 response.requestHandle = ehRequest->requestHandle;
1229 response.resourceHandle = ehRequest->resource;
1230 response.payload = (OCPayload*) payload;
1231 response.numSendVendorSpecificHeaderOptions = 0;
1232 memset(response.sendVendorSpecificHeaderOptions, 0,
1233 sizeof response.sendVendorSpecificHeaderOptions);
1234 memset(response.resourceUri, 0, sizeof response. resourceUri);
1235 // Indicate that response is NOT in a persistent buffer
1236 response.persistentBufferFlag = 0;
1237 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1239 // Send the response
1240 if (OCDoResponse(&response) != OC_STACK_OK)
1242 OIC_LOG(ERROR, TAG, "Error sending response");
1243 stackRet = OC_STACK_ERROR;
1247 else if (method == OC_REST_POST)
1249 OCActionSet *actionset = NULL;
1251 OCRepPayload* payload = OCRepPayloadCreate();
1252 OCRepPayloadSetUri(payload, resource->uri);
1254 if ((strcmp(doWhat, DO_ACTION) == 0)
1255 || (strcmp(doWhat, "DoScheduledAction") == 0))
1257 char *pActionsetName = NULL;
1258 long int delay = -1;
1260 if (strcmp(doWhat, "DoScheduledAction") == 0)
1262 stackRet = ExtractActionSetNameAndDelaytime(details,
1263 &pActionsetName, &delay);
1266 details = pActionsetName;
1270 stackRet = OC_STACK_OK;
1273 if (stackRet == OC_STACK_OK)
1275 if (GetActionSet(details, resource->actionsetHead,
1276 &actionset) != OC_STACK_OK)
1278 OIC_LOG(INFO, TAG, "ERROR");
1279 stackRet = OC_STACK_ERROR;
1282 if (actionset == NULL)
1284 OIC_LOG(INFO, TAG, "Cannot Find ActionSet");
1285 stackRet = OC_STACK_ERROR;
1289 OIC_LOG(INFO, TAG, "Group Action[POST].");
1290 if (actionset->type == NONE)
1292 OIC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1293 actionset->actionsetName);
1294 unsigned int num = GetNumOfTargetResource(
1297 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1298 HandleAggregateResponse;
1299 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1302 DoAction(resource, actionset,
1303 (OCServerRequest*) ehRequest->requestHandle);
1304 stackRet = OC_STACK_OK;
1308 OIC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1309 actionset->actionsetName);
1312 (delay == -1 ? actionset->timesteps : delay);
1314 ScheduledResourceInfo *schedule;
1315 schedule = (ScheduledResourceInfo *) OICMalloc(
1316 sizeof(ScheduledResourceInfo));
1320 OIC_LOG(INFO, TAG, "Building New Call Info.");
1322 sizeof(ScheduledResourceInfo));
1323 #ifndef WITH_ARDUINO
1324 pthread_mutex_lock(&lock);
1326 schedule->resource = resource;
1327 schedule->actionset = actionset;
1328 schedule->ehRequest =
1329 (OCServerRequest*) ehRequest->requestHandle;
1330 #ifndef WITH_ARDUINO
1331 pthread_mutex_unlock(&lock);
1335 OIC_LOG_V(INFO, TAG, "delay_time is %ld seconds.",
1336 actionset->timesteps);
1337 #ifndef WITH_ARDUINO
1338 pthread_mutex_lock(&lock);
1340 schedule->time = registerTimer(delay,
1341 &schedule->timer_id,
1342 &DoScheduledGroupAction);
1343 #ifndef WITH_ARDUINO
1344 pthread_mutex_unlock(&lock);
1346 AddScheduledResource(&scheduleResourceList,
1348 stackRet = OC_STACK_OK;
1352 stackRet = OC_STACK_ERROR;
1359 else if (strcmp(doWhat, "CancelAction") == 0)
1361 ScheduledResourceInfo *info =
1362 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1366 unregisterTimer(info->timer_id);
1368 RemoveScheduledResource(&scheduleResourceList, info);
1369 stackRet = OC_STACK_OK;
1373 stackRet = OC_STACK_ERROR;
1377 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1379 char *plainText = NULL;
1380 OCActionSet *actionset = NULL;
1382 GetActionSet(details, resource->actionsetHead, &actionset);
1383 if (actionset != NULL)
1385 BuildStringFromActionSet(actionset, &plainText);
1387 if (plainText != NULL)
1389 OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
1392 stackRet = OC_STACK_OK;
1398 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
1399 stackRet = OC_STACK_ERROR;
1403 OCEntityHandlerResponse response = { 0 };
1404 if(stackRet == OC_STACK_OK)
1405 response.ehResult = OC_EH_OK;
1407 response.ehResult = OC_EH_ERROR;
1409 // Format the response. Note this requires some info about the request
1410 response.requestHandle = ehRequest->requestHandle;
1411 response.resourceHandle = ehRequest->resource;
1412 response.payload = (OCPayload*) payload;
1413 response.numSendVendorSpecificHeaderOptions = 0;
1414 memset(response.sendVendorSpecificHeaderOptions, 0,
1415 sizeof response.sendVendorSpecificHeaderOptions);
1416 memset(response.resourceUri, 0, sizeof response.resourceUri);
1417 // Indicate that response is NOT in a persistent buffer
1418 response.persistentBufferFlag = 0;
1419 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1421 // Send the response
1422 if (OCDoResponse(&response) != OC_STACK_OK)
1424 OIC_LOG(ERROR, TAG, "Error sending response");
1425 stackRet = OC_STACK_ERROR;