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;
228 *head = (*head)->next;
233 while (tmp->next && (tmp->next != del))
239 tmp->next = del->next;
245 pthread_mutex_unlock(&lock);
249 typedef struct aggregatehandleinfo
251 OCServerRequest *ehRequest;
253 OCResource *collResource;
255 struct aggregatehandleinfo *next;
258 ClientRequestInfo *clientRequstList = NULL;
260 void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
262 ClientRequestInfo *tmp = NULL;
280 ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head,
283 ClientRequestInfo *tmp = NULL;
291 if (tmp->required == handle)
304 void RemoveClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* del)
306 ClientRequestInfo *tmp = NULL;
313 *head = (*head)->next;
318 while (tmp->next && (tmp->next != del))
324 tmp->next = del->next;
329 void AddCapability(OCCapability** head, OCCapability* node)
331 OCCapability *pointer = *head;
338 while (pointer->next != NULL)
340 pointer = pointer->next;
343 pointer->next = node;
347 void AddAction(OCAction** head, OCAction* node)
349 OCAction *pointer = *head;
357 while (pointer->next != NULL)
359 pointer = pointer->next;
362 pointer->next = node;
366 OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node)
368 OCActionSet *pointer = *head;
369 OCActionSet *prev = NULL;
372 return OC_STACK_ERROR;
381 while (pointer != NULL)
383 // check the uniqeness of actionsetname.
384 if (strcmp(pointer->actionsetName, node->actionsetName) == 0)
386 return OC_STACK_ERROR;
390 pointer = pointer->next;
399 void DeleteCapability(OCCapability *del)
401 OCFREE(del->capability)
402 del->capability = NULL;
408 void DeleteAction(OCAction** action)
410 OCCapability* pointer = (*action)->head;
411 OCCapability* pDel = NULL;
416 pointer = pointer->next;
418 DeleteCapability(pDel);
420 OCFREE((*action)->resourceUri)
421 (*action)->next = NULL;
425 void DeleteActionSet(OCActionSet** actionset)
427 if(*actionset == NULL)
430 OCAction* pointer = (*actionset)->head;
431 OCAction* pDel = NULL;
436 pointer = pointer->next;
440 // (*actionset)->head = NULL;
441 OCFREE((*actionset)->actionsetName)
445 OCStackResult FindAndDeleteActionSet(OCResource **resource,
446 const char * actionsetName)
448 if (*resource != NULL)
450 OCActionSet *pointer = NULL;
451 OCActionSet *pDel = NULL;
453 pointer = (*resource)->actionsetHead;
457 return OC_STACK_ERROR;
461 if (strcmp(pointer->actionsetName, actionsetName) == 0)
463 if (pointer->next != NULL)
464 (*resource)->actionsetHead = pointer->next;
466 (*resource)->actionsetHead = NULL;
468 DeleteActionSet(&pointer);
470 else if (pointer->next != NULL)
474 if (pointer->next != NULL)
476 if (strcmp(pointer->next->actionsetName, actionsetName)
479 pDel = pointer->next;
480 pointer->next = pointer->next->next;
482 DeleteActionSet(&pDel);
485 pointer = pointer->next;
493 return OC_STACK_ERROR;
496 OCStackResult DeleteActionSets(OCResource** resource)
498 OCActionSet *pointer = (*resource)->actionsetHead;
499 OCActionSet *pDel = pointer;
504 pointer = pointer->next;
506 DeleteActionSet(&pDel);
510 (*resource)->actionsetHead = NULL;
514 OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
515 OCActionSet** actionset)
517 OCActionSet *pointer = head;
521 if (strcmp(pointer->actionsetName, actionName) == 0)
523 *actionset = pointer;
527 pointer = pointer->next;
530 return OC_STACK_ERROR;
534 OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest,
535 char **key, char **value)
537 OCStackResult result = OC_STACK_OK;
541 if( NULL == ehRequest->payload )
543 result = OC_STACK_ERROR;
549 input = (OCRepPayload*)(ehRequest->payload);
551 if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
553 *key = OICStrdup(ACTIONSET);
554 VARIFY_POINTER_NULL(*key, result, exit);
556 *value = OICStrdup(actionSetStr);
557 VARIFY_POINTER_NULL(*value, result, exit);
559 else if(OCRepPayloadGetPropString(input, DO_ACTION, &actionSetStr))
561 *key = OICStrdup(DO_ACTION);
562 VARIFY_POINTER_NULL(*key, result, exit);
564 *value = OICStrdup(actionSetStr);
565 VARIFY_POINTER_NULL(*value, result, exit);
567 else if(OCRepPayloadGetPropString(input, GET_ACTIONSET, &actionSetStr))
569 *key = OICStrdup(GET_ACTIONSET);
570 VARIFY_POINTER_NULL(*key, result, exit);
572 *value = OICStrdup(actionSetStr);
573 VARIFY_POINTER_NULL(*value, result, exit);
575 else if(OCRepPayloadGetPropString(input, DELETE_ACTIONSET, &actionSetStr))
577 *key = OICStrdup(DELETE_ACTIONSET);
578 VARIFY_POINTER_NULL(*key, result, exit);
580 *value = OICStrdup(actionSetStr);
581 VARIFY_POINTER_NULL(*value, result, exit);
583 else if(OCRepPayloadGetPropString(input, CANCEL_ACTIONSET, &actionSetStr))
585 *key = OICStrdup(CANCEL_ACTIONSET);
586 VARIFY_POINTER_NULL(*key, result, exit);
588 *value = OICStrdup(actionSetStr);
589 VARIFY_POINTER_NULL(*value, result, exit);
593 result = OC_STACK_ERROR;
597 if (result != OC_STACK_OK)
603 OCFREE(actionSetStr);
608 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
611 char *token = NULL, *tokenPtr = NULL;
612 OCStackResult result = OC_STACK_OK;
614 token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
615 VARIFY_POINTER_NULL(token, result, exit)
617 *setName = (char *) OICMalloc(strlen(token) + 1);
618 VARIFY_POINTER_NULL(*setName, result, exit)
619 VARIFY_PARAM_NULL(token, result, exit)
620 strncpy(*setName, token, strlen(token) + 1);
622 token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
623 VARIFY_POINTER_NULL(pa, result, exit)
624 VARIFY_PARAM_NULL(token, result, exit)
634 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
636 OCStackResult result = OC_STACK_OK;
638 char *iterToken = NULL, *iterTokenPtr = NULL;
639 char *descIterToken = NULL, *descIterTokenPtr = NULL;
640 char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
641 char *desc = NULL, *attr = NULL;
642 char *key = NULL, *value = NULL;
644 OCAction *action = NULL;
645 OCCapability *capa = NULL;
647 OC_LOG(INFO, TAG, "Build ActionSet Instance.");
649 *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
650 VARIFY_POINTER_NULL(*set, result, exit)
652 iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
655 memset(*set, 0, sizeof(OCActionSet));
656 (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
657 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
658 VARIFY_PARAM_NULL(iterToken, result, exit)
659 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
661 // Time info. for Scheduled/Recursive Group action.
662 // d is meant Day of the week.
663 // T is meant ActionType.
664 // yyyy-mm-dd hh:mm:ss d
665 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
666 VARIFY_PARAM_NULL(iterToken, result, exit)
668 if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
670 // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
675 OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
677 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
680 desc = (char *) OICMalloc(strlen(iterToken) + 1);
681 VARIFY_POINTER_NULL(desc, result, exit)
682 VARIFY_PARAM_NULL(desc, result, exit)
683 strncpy(desc, iterToken, strlen(iterToken) + 1);
684 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
686 while (descIterToken)
688 attr = (char *) OICMalloc(strlen(descIterToken) + 1);
689 VARIFY_POINTER_NULL(attr, result, exit)
690 VARIFY_PARAM_NULL(descIterToken, result, exit)
691 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
693 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
695 VARIFY_POINTER_NULL(attrIterToken, result, exit);
697 key = (char *) OICMalloc(strlen(attrIterToken) + 1);
698 VARIFY_POINTER_NULL(key, result, exit)
699 VARIFY_PARAM_NULL(attrIterToken, result, exit)
700 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
702 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
704 value = (char *) OICMalloc(strlen(attrIterToken) + 1);
705 VARIFY_POINTER_NULL(value, result, exit)
706 VARIFY_PARAM_NULL(attrIterToken, result, exit)
707 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
709 if (strcmp(key, "uri") == 0)
711 OC_LOG(INFO, TAG, "Build OCAction Instance.");
715 OICFree(action->resourceUri);
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;
791 OCAction *action = actionset->head;
793 if (remaining >= strlen(actionset->actionsetName) + 1)
795 strcat(temp, actionset->actionsetName);
796 remaining -= strlen(actionset->actionsetName);
797 strcat(temp, ACTION_DELIMITER);
802 res = OC_STACK_ERROR;
806 while (action != NULL)
808 if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
810 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;
849 action = action->next;
852 if (remaining < strlen(ACTION_DELIMITER))
854 res = OC_STACK_ERROR;
857 strcat(temp, ACTION_DELIMITER);
862 *desc = OICStrdup(temp);
863 VARIFY_POINTER_NULL(*desc, res, exit);
872 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
873 OCClientResponse* clientResponse)
876 (void)clientResponse;
877 OC_LOG(INFO, TAG, "Entering ActionSetCB");
879 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
883 OCEntityHandlerResponse response = { 0 };
885 response.ehResult = OC_EH_OK;
887 if(NULL == clientResponse->payload)
889 OC_LOG(ERROR, TAG, "Error sending response");
890 return OC_STACK_DELETE_TRANSACTION;
893 // Format the response. Note this requires some info about the request
894 response.requestHandle = info->ehRequest;
895 response.resourceHandle = info->collResource;
896 response.payload = clientResponse->payload;
897 response.numSendVendorSpecificHeaderOptions = 0;
898 memset(response.sendVendorSpecificHeaderOptions, 0,
899 sizeof response.sendVendorSpecificHeaderOptions);
900 memset(response.resourceUri, 0, sizeof response.resourceUri);
901 // Indicate that response is NOT in a persistent buffer
902 response.persistentBufferFlag = 0;
905 if (OCDoResponse(&response) != OC_STACK_OK)
907 OC_LOG(ERROR, TAG, "Error sending response");
908 return OC_STACK_DELETE_TRANSACTION;
911 RemoveClientRequestInfo(&clientRequstList, info);
915 return OC_STACK_KEEP_TRANSACTION;
918 void ActionSetCD(void *context)
923 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
926 OCStackResult ret = OC_STACK_ERROR;
933 OC_LOG(INFO, TAG, "Entering BuildActionJSON");
934 json = cJSON_CreateObject();
936 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
938 OCCapability* pointerCapa = action->head;
941 cJSON_AddStringToObject(body, pointerCapa->capability,
942 pointerCapa->status);
943 pointerCapa = pointerCapa->next;
946 jsonStr = cJSON_PrintUnformatted(json);
948 jsonLen = strlen(jsonStr);
949 if (jsonLen < *remaining)
951 strcat((char*) bufferPtr, jsonStr);
952 *remaining -= jsonLen;
953 bufferPtr += jsonLen;
963 OCPayload* BuildActionCBOR(OCAction* action)
965 OCRepPayload* payload = OCRepPayloadCreate();
969 OC_LOG(INFO, TAG, "Failed to create put payload object");
973 OCCapability* pointerCapa = action->head;
976 OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
977 pointerCapa = pointerCapa->next;
980 return (OCPayload*) payload;
983 unsigned int GetNumOfTargetResource(OCAction *actionset)
985 int numOfResource = 0;
987 OCAction *pointerAction = actionset;
989 while (pointerAction != NULL)
992 pointerAction = pointerAction->next;
995 return numOfResource;
998 OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
1002 OCCallbackData cbData;
1003 cbData.cb = &ActionSetCB;
1004 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
1007 return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
1008 payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
1011 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
1012 OCServerRequest* requestHandle)
1014 OCStackResult result = OC_STACK_ERROR;
1016 if( NULL == actionset->head)
1021 OCAction *pointerAction = actionset->head;
1023 while (pointerAction != NULL)
1026 payload = BuildActionCBOR(pointerAction);
1033 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
1034 sizeof(ClientRequestInfo));
1039 return OC_STACK_NO_MEMORY;
1042 memset(info, 0, sizeof(ClientRequestInfo));
1044 info->collResource = resource;
1045 info->ehRequest = requestHandle;
1047 result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
1050 if (result != OC_STACK_OK)
1056 AddClientRequestInfo(&clientRequstList, info);
1058 pointerAction = pointerAction->next;
1064 void DoScheduledGroupAction()
1066 OC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
1067 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
1071 OC_LOG(INFO, TAG, "Target resource is NULL");
1074 else if (info->resource == NULL)
1076 OC_LOG(INFO, TAG, "Target resource is NULL");
1079 else if (info->actionset == NULL)
1081 OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1084 else if (info->ehRequest == NULL)
1086 OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1089 #ifndef WITH_ARDUINO
1090 pthread_mutex_lock(&lock);
1092 DoAction(info->resource, info->actionset, info->ehRequest);
1093 #ifndef WITH_ARDUINO
1094 pthread_mutex_unlock(&lock);
1097 if (info->actionset->type == RECURSIVE)
1099 ScheduledResourceInfo *schedule;
1100 schedule = (ScheduledResourceInfo *) OICMalloc(
1101 sizeof(ScheduledResourceInfo));
1105 OC_LOG(INFO, TAG, "Building New Call Info.");
1106 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1108 if (info->actionset->timesteps > 0)
1110 #ifndef WITH_ARDUINO
1111 pthread_mutex_lock(&lock);
1113 schedule->resource = info->resource;
1114 schedule->actionset = info->actionset;
1115 schedule->ehRequest = info->ehRequest;
1117 schedule->time = registerTimer(info->actionset->timesteps,
1118 &schedule->timer_id,
1119 &DoScheduledGroupAction);
1121 OC_LOG(INFO, TAG, "Reregisteration.");
1122 #ifndef WITH_ARDUINO
1123 pthread_mutex_unlock(&lock);
1125 AddScheduledResource(&scheduleResourceList, schedule);
1134 RemoveScheduledResource(&scheduleResourceList, info);
1141 OCStackResult BuildCollectionGroupActionCBORResponse(
1142 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1143 OCEntityHandlerRequest *ehRequest)
1145 OCStackResult stackRet = OC_STACK_ERROR;
1147 OC_LOG(INFO, TAG, "Group Action is requested.");
1149 char *doWhat = NULL;
1150 char *details = NULL;
1152 stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
1154 if(stackRet != OC_STACK_OK)
1156 OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1160 stackRet = OC_STACK_ERROR;
1162 if (method == OC_REST_PUT)
1164 OC_LOG(INFO, TAG, "Group Action[PUT].");
1166 if (strcmp(doWhat, ACTIONSET) == 0)
1168 OCActionSet *actionSet = NULL;
1169 stackRet = BuildActionSetFromString(&actionSet, details);
1171 if(stackRet == OC_STACK_OK)
1173 if (actionSet != NULL)
1175 stackRet = AddActionSet(&resource->actionsetHead,
1177 if (stackRet == OC_STACK_ERROR)
1179 if(actionSet != NULL)
1181 DeleteActionSet( &actionSet );
1183 OC_LOG(INFO, TAG, "Duplicated ActionSet ");
1188 stackRet = OC_STACK_ERROR;
1194 stackRet = OC_STACK_ERROR;
1198 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1200 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1202 stackRet = OC_STACK_OK;
1206 stackRet = OC_STACK_ERROR;
1210 OCRepPayload* payload = OCRepPayloadCreate();
1214 OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1215 stackRet = OC_STACK_ERROR;
1219 OCEntityHandlerResponse response = { 0 };
1221 if(stackRet == OC_STACK_OK)
1222 response.ehResult = OC_EH_OK;
1224 response.ehResult = OC_EH_ERROR;
1226 // Format the response. Note this requires some info about the request
1227 response.requestHandle = ehRequest->requestHandle;
1228 response.resourceHandle = ehRequest->resource;
1229 response.payload = (OCPayload*) payload;
1230 response.numSendVendorSpecificHeaderOptions = 0;
1231 memset(response.sendVendorSpecificHeaderOptions, 0,
1232 sizeof response.sendVendorSpecificHeaderOptions);
1233 memset(response.resourceUri, 0, sizeof response. resourceUri);
1234 // Indicate that response is NOT in a persistent buffer
1235 response.persistentBufferFlag = 0;
1236 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1238 // Send the response
1239 if (OCDoResponse(&response) != OC_STACK_OK)
1241 OC_LOG(ERROR, TAG, "Error sending response");
1242 stackRet = OC_STACK_ERROR;
1246 else if (method == OC_REST_POST)
1248 OCActionSet *actionset = NULL;
1250 OCRepPayload* payload = OCRepPayloadCreate();
1251 OCRepPayloadSetUri(payload, resource->uri);
1253 if ((strcmp(doWhat, DO_ACTION) == 0)
1254 || (strcmp(doWhat, "DoScheduledAction") == 0))
1256 char *pActionsetName = NULL;
1257 long int delay = -1;
1259 if (strcmp(doWhat, "DoScheduledAction") == 0)
1261 stackRet = ExtractActionSetNameAndDelaytime(details,
1262 &pActionsetName, &delay);
1265 details = pActionsetName;
1269 stackRet = OC_STACK_OK;
1272 if (stackRet == OC_STACK_OK)
1274 if (GetActionSet(details, resource->actionsetHead,
1275 &actionset) != OC_STACK_OK)
1277 OC_LOG(INFO, TAG, "ERROR");
1278 stackRet = OC_STACK_ERROR;
1281 if (actionset == NULL)
1283 OC_LOG(INFO, TAG, "Cannot Find ActionSet");
1284 stackRet = OC_STACK_ERROR;
1288 OC_LOG(INFO, TAG, "Group Action[POST].");
1289 if (actionset->type == NONE)
1291 OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1292 actionset->actionsetName);
1293 unsigned int num = GetNumOfTargetResource(
1296 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1297 HandleAggregateResponse;
1298 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1301 DoAction(resource, actionset,
1302 (OCServerRequest*) ehRequest->requestHandle);
1303 stackRet = OC_STACK_OK;
1307 OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1308 actionset->actionsetName);
1311 (delay == -1 ? actionset->timesteps : delay);
1313 ScheduledResourceInfo *schedule;
1314 schedule = (ScheduledResourceInfo *) OICMalloc(
1315 sizeof(ScheduledResourceInfo));
1319 OC_LOG(INFO, TAG, "Building New Call Info.");
1321 sizeof(ScheduledResourceInfo));
1323 schedule->resource = resource;
1324 schedule->actionset = actionset;
1325 schedule->ehRequest =
1326 (OCServerRequest*) ehRequest->requestHandle;
1330 OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
1331 actionset->timesteps);
1333 schedule->time = registerTimer(delay,
1334 &schedule->timer_id,
1335 &DoScheduledGroupAction);
1337 AddScheduledResource(&scheduleResourceList,
1339 stackRet = OC_STACK_OK;
1343 stackRet = OC_STACK_ERROR;
1352 else if (strcmp(doWhat, "CancelAction") == 0)
1354 ScheduledResourceInfo *info =
1355 GetScheduledResourceByActionSetName(scheduleResourceList, details);
1359 unregisterTimer(info->timer_id);
1361 RemoveScheduledResource(&scheduleResourceList, info);
1362 stackRet = OC_STACK_OK;
1366 stackRet = OC_STACK_ERROR;
1370 else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1372 char *plainText = NULL;
1373 OCActionSet *actionset = NULL;
1375 GetActionSet(details, resource->actionsetHead, &actionset);
1376 if (actionset != NULL)
1378 BuildStringFromActionSet(actionset, &plainText);
1380 if (plainText != NULL)
1382 OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
1385 stackRet = OC_STACK_OK;
1391 OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1392 stackRet = OC_STACK_ERROR;
1396 OCEntityHandlerResponse response = { 0 };
1397 if(stackRet == OC_STACK_OK)
1398 response.ehResult = OC_EH_OK;
1400 response.ehResult = OC_EH_ERROR;
1402 // Format the response. Note this requires some info about the request
1403 response.requestHandle = ehRequest->requestHandle;
1404 response.resourceHandle = ehRequest->resource;
1405 response.payload = (OCPayload*) payload;
1406 response.numSendVendorSpecificHeaderOptions = 0;
1407 memset(response.sendVendorSpecificHeaderOptions, 0,
1408 sizeof response.sendVendorSpecificHeaderOptions);
1409 memset(response.resourceUri, 0, sizeof response.resourceUri);
1410 // Indicate that response is NOT in a persistent buffer
1411 response.persistentBufferFlag = 0;
1412 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1414 // Send the response
1415 if (OCDoResponse(&response) != OC_STACK_OK)
1417 OC_LOG(ERROR, TAG, "Error sending response");
1418 stackRet = OC_STACK_ERROR;