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);
658 memset(*set, 0, sizeof(OCActionSet));
659 (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
660 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
661 VARIFY_PARAM_NULL(iterToken, result, exit)
662 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
664 // Time info. for Scheduled/Recursive Group action.
665 // d is meant Day of the week.
666 // T is meant ActionType.
667 // yyyy-mm-dd hh:mm:ss d
668 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
669 VARIFY_PARAM_NULL(iterToken, result, exit)
671 if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
673 // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
678 OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
680 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
683 desc = (char *) OICMalloc(strlen(iterToken) + 1);
684 VARIFY_POINTER_NULL(desc, result, exit)
685 VARIFY_PARAM_NULL(desc, result, exit)
686 strncpy(desc, iterToken, strlen(iterToken) + 1);
687 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
689 while (descIterToken)
691 attr = (char *) OICMalloc(strlen(descIterToken) + 1);
692 VARIFY_POINTER_NULL(attr, result, exit)
693 VARIFY_PARAM_NULL(descIterToken, result, exit)
694 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
696 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
698 VARIFY_POINTER_NULL(attrIterToken, result, exit);
700 key = (char *) OICMalloc(strlen(attrIterToken) + 1);
701 VARIFY_POINTER_NULL(key, result, exit)
702 VARIFY_PARAM_NULL(attrIterToken, result, exit)
703 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
705 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
707 value = (char *) OICMalloc(strlen(attrIterToken) + 1);
708 VARIFY_POINTER_NULL(value, result, exit)
709 VARIFY_PARAM_NULL(attrIterToken, result, exit)
710 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
712 if (strcmp(key, "uri") == 0)
714 OC_LOG(INFO, TAG, "Build OCAction Instance.");
716 action = (OCAction*) OICMalloc(sizeof(OCAction));
717 VARIFY_POINTER_NULL(action, result, exit)
718 memset(action, 0, sizeof(OCAction));
719 action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
720 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
721 VARIFY_PARAM_NULL(value, result, exit)
722 strncpy(action->resourceUri, value, strlen(value) + 1);
726 if ((key != NULL) && (value != NULL))
728 OC_LOG(INFO, TAG, "Build OCCapability Instance.");
730 capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
731 VARIFY_POINTER_NULL(capa, result, exit)
732 memset(capa, 0, sizeof(OCCapability));
734 capa->capability = (char *) OICMalloc(strlen(key) + 1);
735 VARIFY_POINTER_NULL(capa->capability, result, exit)
736 VARIFY_PARAM_NULL(key, result, exit)
737 strncpy(capa->capability, key, strlen(key) + 1);
739 capa->status = (char *) OICMalloc(strlen(value) + 1);
740 VARIFY_POINTER_NULL(capa->status, result, exit)
741 VARIFY_PARAM_NULL(value, result, exit)
742 strncpy(capa->status, value, strlen(value) + 1);
744 VARIFY_POINTER_NULL(action, result, exit)
746 AddCapability(&action->head, capa);
754 descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
758 AddAction(&(*set)->head, action);
759 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
777 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
779 // Can't use the macros here as they are hardcoded to 'exit' and will
780 // result in dereferencing a null pointer.
781 if (!actionset || !desc)
783 return OC_STACK_INVALID_PARAM;
785 char temp[1024] = { 0 };
786 size_t remaining = sizeof(temp) - 1;
787 OCStackResult res = OC_STACK_ERROR;
789 OCAction *action = actionset->head;
791 if (remaining >= strlen(actionset->actionsetName) + 1)
793 strcat(temp, actionset->actionsetName);
794 remaining -= strlen(actionset->actionsetName);
795 strcat(temp, ACTION_DELIMITER);
800 res = OC_STACK_ERROR;
804 while (action != NULL)
806 if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
808 res = OC_STACK_ERROR;
811 strcat(temp, "uri=");
812 remaining -= strlen("uri=");
813 strcat(temp, action->resourceUri);
814 remaining -= strlen(action->resourceUri);
818 OCCapability *capas = action->head;
819 while (capas != NULL)
821 if (remaining < (strlen(capas->capability)
822 + 1 + strlen(capas->status)))
824 res = OC_STACK_ERROR;
828 strcat(temp, capas->capability);
829 remaining -= strlen(capas->capability);
832 strcat(temp, capas->status);
833 remaining -= strlen(capas->status);
840 res = OC_STACK_ERROR;
847 action = action->next;
850 if (remaining < strlen(ACTION_DELIMITER))
852 res = OC_STACK_ERROR;
855 strcat(temp, ACTION_DELIMITER);
860 *desc = OICStrdup(temp);
861 VARIFY_POINTER_NULL(*desc, res, exit);
870 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
871 OCClientResponse* clientResponse)
874 (void)clientResponse;
875 OC_LOG(INFO, TAG, "Entering ActionSetCB");
877 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
881 OCEntityHandlerResponse response = { 0 };
883 response.ehResult = OC_EH_OK;
885 if(NULL == clientResponse->payload)
887 OC_LOG(ERROR, TAG, "Error sending response");
888 return OC_STACK_DELETE_TRANSACTION;
891 // Format the response. Note this requires some info about the request
892 response.requestHandle = info->ehRequest;
893 response.resourceHandle = info->collResource;
894 response.payload = clientResponse->payload;
895 response.numSendVendorSpecificHeaderOptions = 0;
896 memset(response.sendVendorSpecificHeaderOptions, 0,
897 sizeof response.sendVendorSpecificHeaderOptions);
898 memset(response.resourceUri, 0, sizeof response.resourceUri);
899 // Indicate that response is NOT in a persistent buffer
900 response.persistentBufferFlag = 0;
903 if (OCDoResponse(&response) != OC_STACK_OK)
905 OC_LOG(ERROR, TAG, "Error sending response");
906 return OC_STACK_DELETE_TRANSACTION;
909 RemoveClientRequestInfo(&clientRequstList, info);
913 return OC_STACK_KEEP_TRANSACTION;
916 void ActionSetCD(void *context)
921 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
924 OCStackResult ret = OC_STACK_ERROR;
931 OC_LOG(INFO, TAG, "Entering BuildActionJSON");
932 json = cJSON_CreateObject();
934 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
936 OCCapability* pointerCapa = action->head;
939 cJSON_AddStringToObject(body, pointerCapa->capability,
940 pointerCapa->status);
941 pointerCapa = pointerCapa->next;
944 jsonStr = cJSON_PrintUnformatted(json);
946 jsonLen = strlen(jsonStr);
947 if (jsonLen < *remaining)
949 strcat((char*) bufferPtr, jsonStr);
950 *remaining -= jsonLen;
951 bufferPtr += jsonLen;
961 OCPayload* BuildActionCBOR(OCAction* action)
963 OCRepPayload* payload = OCRepPayloadCreate();
967 OC_LOG(INFO, TAG, "Failed to create put payload object");
971 OCCapability* pointerCapa = action->head;
974 OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
975 pointerCapa = pointerCapa->next;
978 return (OCPayload*) payload;
981 unsigned int GetNumOfTargetResource(OCAction *actionset)
983 int numOfResource = 0;
985 OCAction *pointerAction = actionset;
987 while (pointerAction != NULL)
990 pointerAction = pointerAction->next;
993 return numOfResource;
996 OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
1000 OCCallbackData cbData;
1001 cbData.cb = &ActionSetCB;
1002 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
1005 return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
1006 payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
1009 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
1010 OCServerRequest* requestHandle)
1012 OCStackResult result = OC_STACK_ERROR;
1014 if( NULL == actionset->head)
1019 OCAction *pointerAction = actionset->head;
1021 while (pointerAction != NULL)
1024 payload = BuildActionCBOR(pointerAction);
1031 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
1032 sizeof(ClientRequestInfo));
1037 return OC_STACK_NO_MEMORY;
1040 memset(info, 0, sizeof(ClientRequestInfo));
1042 info->collResource = resource;
1043 info->ehRequest = requestHandle;
1045 result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
1048 if (result != OC_STACK_OK)
1054 AddClientRequestInfo(&clientRequstList, info);
1056 pointerAction = pointerAction->next;
1062 void DoScheduledGroupAction()
1064 OC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
1065 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
1069 OC_LOG(INFO, TAG, "Target resource is NULL");
1072 else if (info->resource == NULL)
1074 OC_LOG(INFO, TAG, "Target resource is NULL");
1077 else if (info->actionset == NULL)
1079 OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1082 else if (info->ehRequest == NULL)
1084 OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1087 #ifndef WITH_ARDUINO
1088 pthread_mutex_lock(&lock);
1090 DoAction(info->resource, info->actionset, info->ehRequest);
1091 #ifndef WITH_ARDUINO
1092 pthread_mutex_unlock(&lock);
1095 if (info->actionset->type == RECURSIVE)
1097 ScheduledResourceInfo *schedule;
1098 schedule = (ScheduledResourceInfo *) OICMalloc(
1099 sizeof(ScheduledResourceInfo));
1103 OC_LOG(INFO, TAG, "Building New Call Info.");
1104 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1106 if (info->actionset->timesteps > 0)
1108 #ifndef WITH_ARDUINO
1109 pthread_mutex_lock(&lock);
1111 schedule->resource = info->resource;
1112 schedule->actionset = info->actionset;
1113 schedule->ehRequest = info->ehRequest;
1115 schedule->time = registerTimer(info->actionset->timesteps,
1116 &schedule->timer_id,
1117 &DoScheduledGroupAction);
1119 OC_LOG(INFO, TAG, "Reregisteration.");
1120 #ifndef WITH_ARDUINO
1121 pthread_mutex_unlock(&lock);
1123 AddScheduledResource(&scheduleResourceList, schedule);
1132 RemoveScheduledResource(&scheduleResourceList, info);
1139 OCStackResult BuildCollectionGroupActionCBORResponse(
1140 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1141 OCEntityHandlerRequest *ehRequest)
1143 OCStackResult stackRet = OC_STACK_ERROR;
1145 OC_LOG(INFO, TAG, "Group Action is requested.");
1147 char *doWhat = NULL;
1148 char *details = NULL;
1150 stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
1152 if(stackRet != OC_STACK_OK)
1154 OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1158 stackRet = OC_STACK_ERROR;
1160 if (method == OC_REST_PUT)
1162 OC_LOG(INFO, TAG, "Group Action[PUT].");
1164 if (strcmp(doWhat, ACTIONSET) == 0)
1166 OCActionSet *actionSet = NULL;
1167 stackRet = BuildActionSetFromString(&actionSet, details);
1169 if(stackRet == OC_STACK_OK)
1171 if (actionSet != NULL)
1173 stackRet = AddActionSet(&resource->actionsetHead,
1175 if (stackRet == OC_STACK_ERROR)
1177 if(actionSet != NULL)
1179 DeleteActionSet( &actionSet );
1181 OC_LOG(INFO, TAG, "Duplicated ActionSet ");
1186 stackRet = OC_STACK_ERROR;
1192 stackRet = OC_STACK_ERROR;
1196 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1198 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1200 stackRet = OC_STACK_OK;
1204 stackRet = OC_STACK_ERROR;
1208 OCRepPayload* payload = OCRepPayloadCreate();
1212 OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1213 stackRet = OC_STACK_ERROR;
1217 OCEntityHandlerResponse response = { 0 };
1219 if(stackRet == OC_STACK_OK)
1220 response.ehResult = OC_EH_OK;
1222 response.ehResult = OC_EH_ERROR;
1224 // Format the response. Note this requires some info about the request
1225 response.requestHandle = ehRequest->requestHandle;
1226 response.resourceHandle = ehRequest->resource;
1227 response.payload = (OCPayload*) payload;
1228 response.numSendVendorSpecificHeaderOptions = 0;
1229 memset(response.sendVendorSpecificHeaderOptions, 0,
1230 sizeof response.sendVendorSpecificHeaderOptions);
1231 memset(response.resourceUri, 0, sizeof response. resourceUri);
1232 // Indicate that response is NOT in a persistent buffer
1233 response.persistentBufferFlag = 0;
1234 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1236 // Send the response
1237 if (OCDoResponse(&response) != OC_STACK_OK)
1239 OC_LOG(ERROR, TAG, "Error sending response");
1240 stackRet = OC_STACK_ERROR;
1244 else if (method == OC_REST_POST)
1246 OCActionSet *actionset = NULL;
1248 OCRepPayload* payload = OCRepPayloadCreate();
1249 OCRepPayloadSetUri(payload, resource->uri);
1251 if ((strcmp(doWhat, DO_ACTION) == 0)
1252 || (strcmp(doWhat, "DoScheduledAction") == 0))
1254 char *pActionsetName = NULL;
1255 long int delay = -1;
1257 if (strcmp(doWhat, "DoScheduledAction") == 0)
1259 stackRet = ExtractActionSetNameAndDelaytime(details,
1260 &pActionsetName, &delay);
1263 details = pActionsetName;
1267 stackRet = OC_STACK_OK;
1270 if (stackRet == OC_STACK_OK)
1272 if (GetActionSet(details, resource->actionsetHead,
1273 &actionset) != OC_STACK_OK)
1275 OC_LOG(INFO, TAG, "ERROR");
1276 stackRet = OC_STACK_ERROR;
1279 if (actionset == NULL)
1281 OC_LOG(INFO, TAG, "Cannot Find ActionSet");
1282 stackRet = OC_STACK_ERROR;
1286 OC_LOG(INFO, TAG, "Group Action[POST].");
1287 if (actionset->type == NONE)
1289 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1290 actionset->actionsetName);
1291 unsigned int num = GetNumOfTargetResource(
1294 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1295 HandleAggregateResponse;
1296 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1299 DoAction(resource, actionset,
1300 (OCServerRequest*) ehRequest->requestHandle);
1301 stackRet = OC_STACK_OK;
1305 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1306 actionset->actionsetName);
1309 (delay == -1 ? actionset->timesteps : delay);
1311 ScheduledResourceInfo *schedule;
1312 schedule = (ScheduledResourceInfo *) OICMalloc(
1313 sizeof(ScheduledResourceInfo));
1317 OC_LOG(INFO, TAG, "Building New Call Info.");
1319 sizeof(ScheduledResourceInfo));
1320 #ifndef WITH_ARDUINO
1321 pthread_mutex_lock(&lock);
1323 schedule->resource = resource;
1324 schedule->actionset = actionset;
1325 schedule->ehRequest =
1326 (OCServerRequest*) ehRequest->requestHandle;
1327 #ifndef WITH_ARDUINO
1328 pthread_mutex_unlock(&lock);
1332 OC_LOG_V(INFO, TAG, "delay_time is %ld seconds.",
1333 actionset->timesteps);
1334 #ifndef WITH_ARDUINO
1335 pthread_mutex_lock(&lock);
1337 schedule->time = registerTimer(delay,
1338 &schedule->timer_id,
1339 &DoScheduledGroupAction);
1340 #ifndef WITH_ARDUINO
1341 pthread_mutex_unlock(&lock);
1343 AddScheduledResource(&scheduleResourceList,
1345 stackRet = OC_STACK_OK;
1349 stackRet = OC_STACK_ERROR;
1356 else if (strcmp(doWhat, "CancelAction") == 0)
1358 ScheduledResourceInfo *info =
1359 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1363 unregisterTimer(info->timer_id);
1365 RemoveScheduledResource(&scheduleResourceList, info);
1366 stackRet = OC_STACK_OK;
1370 stackRet = OC_STACK_ERROR;
1374 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1376 char *plainText = NULL;
1377 OCActionSet *actionset = NULL;
1379 GetActionSet(details, resource->actionsetHead, &actionset);
1380 if (actionset != NULL)
1382 BuildStringFromActionSet(actionset, &plainText);
1384 if (plainText != NULL)
1386 OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
1389 stackRet = OC_STACK_OK;
1395 OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1396 stackRet = OC_STACK_ERROR;
1400 OCEntityHandlerResponse response = { 0 };
1401 if(stackRet == OC_STACK_OK)
1402 response.ehResult = OC_EH_OK;
1404 response.ehResult = OC_EH_ERROR;
1406 // Format the response. Note this requires some info about the request
1407 response.requestHandle = ehRequest->requestHandle;
1408 response.resourceHandle = ehRequest->resource;
1409 response.payload = (OCPayload*) payload;
1410 response.numSendVendorSpecificHeaderOptions = 0;
1411 memset(response.sendVendorSpecificHeaderOptions, 0,
1412 sizeof response.sendVendorSpecificHeaderOptions);
1413 memset(response.resourceUri, 0, sizeof response.resourceUri);
1414 // Indicate that response is NOT in a persistent buffer
1415 response.persistentBufferFlag = 0;
1416 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1418 // Send the response
1419 if (OCDoResponse(&response) != OC_STACK_OK)
1421 OC_LOG(ERROR, TAG, "Error sending response");
1422 stackRet = OC_STACK_ERROR;