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
23 #include "iotivity_config.h"
30 #include "ocpayload.h"
31 #include "oic_malloc.h"
32 #include "oic_string.h"
33 #include "occollection.h"
37 #define TAG "OIC_RI_GROUP"
39 #define DESC_DELIMITER "\""
40 #define ACTION_DELIMITER "*"
41 #define ATTR_DELIMITER "|"
42 #define ATTR_ASSIGN "="
44 // Definitions for operations related to actions
45 #define DO_ACTION "DoAction"
46 #define GET_ACTIONSET "GetActionSet"
47 #define ACTIONSET "ActionSet"
48 #define CANCEL_ACTIONSET "CancelAction"
49 #define DELETE_ACTIONSET "DelActionSet"
51 #define DEFAULT_CONTEXT_VALUE 0x99
53 #define VARIFY_POINTER_NULL(pointer, result, toExit) \
56 result = OC_STACK_NO_MEMORY;\
59 #define VARIFY_PARAM_NULL(pointer, result, toExit) \
62 result = OC_STACK_INVALID_PARAM;\
66 #define OCFREE(pointer) \
72 // Mutex implementation macros
73 #if defined(HAVE_PTHREAD_H)
77 #define MUTEX_LOCK(ARG_NAME) { pthread_mutex_lock(ARG_NAME); }
78 #define MUTEX_UNLOCK(ARG_NAME) { pthread_mutex_unlock(ARG_NAME); }
80 #elif defined(HAVE_WINDOWS_H)
83 CRITICAL_SECTION lock;
84 #define MUTEX_LOCK(ARG_NAME) { EnterCriticalSection(ARG_NAME); }
85 #define MUTEX_UNLOCK(ARG_NAME) { LeaveCriticalSection(ARG_NAME); }
87 #elif defined(WITH_ARDUINO)
89 #define MUTEX_LOCK(ARG_NAME) { }
90 #define MUTEX_UNLOCK(ARG_NAME) { }
94 ERROR Need mutex implementation on this platform
100 NONE = 0, SCHEDULED, RECURSIVE
103 typedef struct scheduledresourceinfo
105 OCResource *resource;
106 OCActionSet *actionset;
110 OCServerRequest *ehRequest;
113 struct scheduledresourceinfo* next;
114 } ScheduledResourceInfo;
116 ScheduledResourceInfo *scheduleResourceList = NULL;
118 void AddScheduledResource(ScheduledResourceInfo **head,
119 ScheduledResourceInfo* add)
121 OIC_LOG(INFO, TAG, "AddScheduledResource Entering...");
124 ScheduledResourceInfo *tmp = NULL;
143 ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
145 OIC_LOG(INFO, TAG, "GetScheduledResource Entering...");
151 ScheduledResourceInfo *tmp = NULL;
154 #if !defined(WITH_ARDUINO)
165 diffTm = timespec_diff(tmp->time, t_now);
167 if (diffTm <= (time_t) 0)
169 OIC_LOG(INFO, TAG, "return Call INFO.");
183 OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
188 ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
190 OIC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering...");
194 ScheduledResourceInfo *tmp = NULL;
201 if (strcmp(tmp->actionset->actionsetName, setName) == 0)
203 OIC_LOG(INFO, TAG, "return Call INFO.");
216 OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
221 void RemoveScheduledResource(ScheduledResourceInfo **head,
222 ScheduledResourceInfo* del)
227 OIC_LOG(INFO, TAG, "RemoveScheduledResource Entering...");
228 ScheduledResourceInfo *tmp = NULL;
240 *head = (*head)->next;
245 while (tmp->next && (tmp->next != del))
251 tmp->next = del->next;
261 typedef struct aggregatehandleinfo
263 OCServerRequest *ehRequest;
265 OCResource *collResource;
267 struct aggregatehandleinfo *next;
270 ClientRequestInfo *clientRequstList = NULL;
272 void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
274 ClientRequestInfo *tmp = NULL;
292 ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head,
295 ClientRequestInfo *tmp = NULL;
303 if (tmp->required == handle)
316 void RemoveClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* del)
318 ClientRequestInfo *tmp = NULL;
325 *head = (*head)->next;
330 while (tmp->next && (tmp->next != del))
336 tmp->next = del->next;
341 void AddCapability(OCCapability** head, OCCapability* node)
343 OCCapability *pointer = *head;
350 while (pointer->next != NULL)
352 pointer = pointer->next;
355 pointer->next = node;
359 void AddAction(OCAction** head, OCAction* node)
361 OCAction *pointer = *head;
369 while (pointer->next != NULL)
371 pointer = pointer->next;
374 pointer->next = node;
378 OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node)
380 OCActionSet *pointer = *head;
381 OCActionSet *prev = NULL;
384 return OC_STACK_ERROR;
393 while (pointer != NULL)
395 // check the uniqeness of actionsetname.
396 if (strcmp(pointer->actionsetName, node->actionsetName) == 0)
398 return OC_STACK_ERROR;
402 pointer = pointer->next;
411 void DeleteCapability(OCCapability *del)
413 OCFREE(del->capability)
414 del->capability = NULL;
420 void DeleteAction(OCAction** action)
422 OCCapability* pointer = (*action)->head;
423 OCCapability* pDel = NULL;
428 pointer = pointer->next;
430 DeleteCapability(pDel);
432 OCFREE((*action)->resourceUri)
433 (*action)->next = NULL;
437 void DeleteActionSet(OCActionSet** actionset)
439 if(*actionset == NULL)
442 OCAction* pointer = (*actionset)->head;
443 OCAction* pDel = NULL;
448 pointer = pointer->next;
452 // (*actionset)->head = NULL;
453 OCFREE((*actionset)->actionsetName)
457 OCStackResult FindAndDeleteActionSet(OCResource **resource,
458 const char * actionsetName)
460 if (*resource != NULL)
462 OCActionSet *pointer = NULL;
463 OCActionSet *pDel = NULL;
465 pointer = (*resource)->actionsetHead;
469 return OC_STACK_ERROR;
473 if (strcmp(pointer->actionsetName, actionsetName) == 0)
475 if (pointer->next != NULL)
476 (*resource)->actionsetHead = pointer->next;
478 (*resource)->actionsetHead = NULL;
480 DeleteActionSet(&pointer);
482 else if (pointer->next != NULL)
486 if (pointer->next != NULL)
488 if (strcmp(pointer->next->actionsetName, actionsetName)
491 pDel = pointer->next;
492 pointer->next = pointer->next->next;
494 DeleteActionSet(&pDel);
497 pointer = pointer->next;
505 return OC_STACK_ERROR;
508 OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
509 OCActionSet** actionset)
511 OCActionSet *pointer = head;
515 if (strcmp(pointer->actionsetName, actionName) == 0)
517 *actionset = pointer;
521 pointer = pointer->next;
524 return OC_STACK_ERROR;
528 OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest,
529 char **key, char **value)
531 OCStackResult result = OC_STACK_OK;
533 char *actionSetStr = NULL;
535 if( NULL == ehRequest->payload )
537 result = OC_STACK_ERROR;
543 input = (OCRepPayload*)(ehRequest->payload);
545 if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
547 *key = OICStrdup(ACTIONSET);
548 VARIFY_POINTER_NULL(*key, result, exit);
550 *value = OICStrdup(actionSetStr);
551 VARIFY_POINTER_NULL(*value, result, exit);
553 else if(OCRepPayloadGetPropString(input, DO_ACTION, &actionSetStr))
555 *key = OICStrdup(DO_ACTION);
556 VARIFY_POINTER_NULL(*key, result, exit);
558 *value = OICStrdup(actionSetStr);
559 VARIFY_POINTER_NULL(*value, result, exit);
561 else if(OCRepPayloadGetPropString(input, GET_ACTIONSET, &actionSetStr))
563 *key = OICStrdup(GET_ACTIONSET);
564 VARIFY_POINTER_NULL(*key, result, exit);
566 *value = OICStrdup(actionSetStr);
567 VARIFY_POINTER_NULL(*value, result, exit);
569 else if(OCRepPayloadGetPropString(input, DELETE_ACTIONSET, &actionSetStr))
571 *key = OICStrdup(DELETE_ACTIONSET);
572 VARIFY_POINTER_NULL(*key, result, exit);
574 *value = OICStrdup(actionSetStr);
575 VARIFY_POINTER_NULL(*value, result, exit);
577 else if(OCRepPayloadGetPropString(input, CANCEL_ACTIONSET, &actionSetStr))
579 *key = OICStrdup(CANCEL_ACTIONSET);
580 VARIFY_POINTER_NULL(*key, result, exit);
582 *value = OICStrdup(actionSetStr);
583 VARIFY_POINTER_NULL(*value, result, exit);
587 result = OC_STACK_ERROR;
591 if (result != OC_STACK_OK)
597 OCFREE(actionSetStr);
602 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
605 char *token = NULL, *tokenPtr = NULL;
606 OCStackResult result = OC_STACK_OK;
608 token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
609 VARIFY_POINTER_NULL(token, result, exit)
611 *setName = (char *) OICMalloc(strlen(token) + 1);
612 VARIFY_POINTER_NULL(*setName, result, exit)
613 VARIFY_PARAM_NULL(token, result, exit)
614 strncpy(*setName, token, strlen(token) + 1);
616 token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
617 VARIFY_POINTER_NULL(pa, result, exit)
618 VARIFY_PARAM_NULL(token, result, exit)
628 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
630 OCStackResult result = OC_STACK_OK;
632 char *iterToken = NULL, *iterTokenPtr = NULL;
633 char *descIterToken = NULL, *descIterTokenPtr = NULL;
634 char *attrIterToken = NULL, *attrIterTokenPtr = NULL;
635 char *desc = NULL, *attr = NULL;
636 char *key = NULL, *value = NULL;
638 OCAction *action = NULL;
639 OCCapability *capa = NULL;
641 OIC_LOG(INFO, TAG, "Build ActionSet Instance.");
643 *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
644 VARIFY_POINTER_NULL(*set, result, exit)
646 iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
647 VARIFY_POINTER_NULL(iterToken, result, exit);
650 memset(*set, 0, sizeof(OCActionSet));
651 (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
652 VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
653 VARIFY_PARAM_NULL(iterToken, result, exit)
654 strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
656 // Time info. for Scheduled/Recursive Group action.
657 // d is meant Day of the week.
658 // T is meant ActionType.
659 // yyyy-mm-dd hh:mm:ss d
660 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
661 VARIFY_PARAM_NULL(iterToken, result, exit)
662 #if !defined(WITH_ARDUINO)
663 if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
665 // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
670 OIC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
672 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
675 desc = (char *) OICMalloc(strlen(iterToken) + 1);
676 VARIFY_POINTER_NULL(desc, result, exit)
677 VARIFY_PARAM_NULL(desc, result, exit)
678 strncpy(desc, iterToken, strlen(iterToken) + 1);
679 descIterToken = (char *) strtok_r(desc, ATTR_DELIMITER,
681 while (descIterToken)
683 attr = (char *) OICMalloc(strlen(descIterToken) + 1);
684 VARIFY_POINTER_NULL(attr, result, exit)
685 VARIFY_PARAM_NULL(descIterToken, result, exit)
686 strncpy(attr, descIterToken, strlen(descIterToken) + 1);
688 attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
690 VARIFY_POINTER_NULL(attrIterToken, result, exit);
692 key = (char *) OICMalloc(strlen(attrIterToken) + 1);
693 VARIFY_POINTER_NULL(key, result, exit)
694 VARIFY_PARAM_NULL(attrIterToken, result, exit)
695 strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
697 attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
699 VARIFY_POINTER_NULL(attrIterToken, result, exit);
700 value = (char *) OICMalloc(strlen(attrIterToken) + 1);
701 VARIFY_POINTER_NULL(value, result, exit)
702 VARIFY_PARAM_NULL(attrIterToken, result, exit)
703 strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
705 if (strcmp(key, "uri") == 0)
707 OIC_LOG(INFO, TAG, "Build OCAction Instance.");
709 action = (OCAction*) OICMalloc(sizeof(OCAction));
710 VARIFY_POINTER_NULL(action, result, exit)
711 memset(action, 0, sizeof(OCAction));
712 action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
713 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
714 VARIFY_PARAM_NULL(value, result, exit)
715 strncpy(action->resourceUri, value, strlen(value) + 1);
719 if ((key != NULL) && (value != NULL))
721 OIC_LOG(INFO, TAG, "Build OCCapability Instance.");
723 capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
724 VARIFY_POINTER_NULL(capa, result, exit)
725 memset(capa, 0, sizeof(OCCapability));
727 capa->capability = (char *) OICMalloc(strlen(key) + 1);
728 VARIFY_POINTER_NULL(capa->capability, result, exit)
729 VARIFY_PARAM_NULL(key, result, exit)
730 strncpy(capa->capability, key, strlen(key) + 1);
732 capa->status = (char *) OICMalloc(strlen(value) + 1);
733 VARIFY_POINTER_NULL(capa->status, result, exit)
734 VARIFY_PARAM_NULL(value, result, exit)
735 strncpy(capa->status, value, strlen(value) + 1);
737 VARIFY_POINTER_NULL(action, result, exit)
739 AddCapability(&action->head, capa);
747 descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
751 AddAction(&(*set)->head, action);
752 iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
770 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
772 // Can't use the macros here as they are hardcoded to 'exit' and will
773 // result in dereferencing a null pointer.
774 if (!actionset || !desc)
776 return OC_STACK_INVALID_PARAM;
778 char temp[1024] = { 0 };
779 size_t remaining = sizeof(temp) - 1;
780 OCStackResult res = OC_STACK_ERROR;
781 char* actionTypeStr = NULL;
783 OCAction *action = actionset->head;
785 if (remaining >= strlen(actionset->actionsetName) + 1)
787 strncat(temp, actionset->actionsetName, strlen(actionset->actionsetName));
788 remaining -= strlen(actionset->actionsetName);
789 strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
794 res = OC_STACK_ERROR;
798 actionTypeStr = (char *)malloc(1024);
799 if(actionTypeStr != NULL)
801 sprintf(actionTypeStr, "%ld %u", actionset->timesteps, actionset->type);
802 if(remaining >= strlen(actionTypeStr) + strlen(ACTION_DELIMITER) + 1)
804 strncat(temp, actionTypeStr, strlen(actionTypeStr));
805 remaining -= strlen(actionTypeStr);
806 strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
807 remaining -= strlen(ACTION_DELIMITER);
813 res = OC_STACK_ERROR;
819 res = OC_STACK_ERROR;
823 while (action != NULL)
825 if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
827 res = OC_STACK_ERROR;
831 strcat(temp, "uri=");
832 remaining -= strlen("uri=");
833 strcat(temp, action->resourceUri);
834 remaining -= strlen(action->resourceUri);
838 OCCapability *capas = action->head;
839 while (capas != NULL)
841 if (remaining < (strlen(capas->capability)
842 + 1 + strlen(capas->status)))
844 res = OC_STACK_ERROR;
848 strcat(temp, capas->capability);
849 remaining -= strlen(capas->capability);
852 strcat(temp, capas->status);
853 remaining -= strlen(capas->status);
860 res = OC_STACK_ERROR;
868 action = action->next;
871 if (remaining < strlen(ACTION_DELIMITER))
873 res = OC_STACK_ERROR;
876 strcat(temp, ACTION_DELIMITER);
881 *desc = OICStrdup(temp);
882 VARIFY_POINTER_NULL(*desc, res, exit);
891 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
892 OCClientResponse* clientResponse)
895 (void)clientResponse;
896 OIC_LOG(INFO, TAG, "Entering ActionSetCB");
898 ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
902 OCEntityHandlerResponse response = { 0 };
904 response.ehResult = OC_EH_OK;
906 if(NULL == clientResponse->payload)
908 OIC_LOG(ERROR, TAG, "Error sending response");
909 return OC_STACK_DELETE_TRANSACTION;
912 // Format the response. Note this requires some info about the request
913 response.requestHandle = info->ehRequest;
914 response.resourceHandle = info->collResource;
915 response.payload = clientResponse->payload;
916 response.numSendVendorSpecificHeaderOptions = 0;
917 memset(response.sendVendorSpecificHeaderOptions, 0,
918 sizeof response.sendVendorSpecificHeaderOptions);
919 memset(response.resourceUri, 0, sizeof response.resourceUri);
920 // Indicate that response is NOT in a persistent buffer
921 response.persistentBufferFlag = 0;
924 if (OCDoResponse(&response) != OC_STACK_OK)
926 OIC_LOG(ERROR, TAG, "Error sending response");
927 return OC_STACK_DELETE_TRANSACTION;
930 RemoveClientRequestInfo(&clientRequstList, info);
934 return OC_STACK_KEEP_TRANSACTION;
937 void ActionSetCD(void *context)
942 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
945 OCStackResult ret = OC_STACK_ERROR;
952 OIC_LOG(INFO, TAG, "Entering BuildActionJSON");
953 json = cJSON_CreateObject();
955 cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
957 OCCapability* pointerCapa = action->head;
960 cJSON_AddStringToObject(body, pointerCapa->capability,
961 pointerCapa->status);
962 pointerCapa = pointerCapa->next;
965 jsonStr = cJSON_PrintUnformatted(json);
967 jsonLen = strlen(jsonStr);
968 if (jsonLen < *remaining)
970 strncat((char*) bufferPtr, jsonStr, jsonLen);
971 *remaining -= jsonLen;
972 bufferPtr += jsonLen;
982 OCPayload* BuildActionCBOR(OCAction* action)
984 OCRepPayload* payload = OCRepPayloadCreate();
988 OIC_LOG(INFO, TAG, "Failed to create put payload object");
992 OCCapability* pointerCapa = action->head;
995 OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
996 pointerCapa = pointerCapa->next;
999 return (OCPayload*) payload;
1002 unsigned int GetNumOfTargetResource(OCAction *actionset)
1004 int numOfResource = 0;
1006 OCAction *pointerAction = actionset;
1008 while (pointerAction != NULL)
1011 pointerAction = pointerAction->next;
1014 return numOfResource;
1017 OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
1021 OCCallbackData cbData;
1022 cbData.cb = &ActionSetCB;
1023 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
1026 return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
1027 payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
1030 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
1031 OCServerRequest* requestHandle)
1033 OCStackResult result = OC_STACK_ERROR;
1035 if( NULL == actionset->head)
1040 OCAction *pointerAction = actionset->head;
1042 while (pointerAction != NULL)
1045 payload = BuildActionCBOR(pointerAction);
1052 ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
1053 sizeof(ClientRequestInfo));
1058 return OC_STACK_NO_MEMORY;
1061 memset(info, 0, sizeof(ClientRequestInfo));
1063 info->collResource = resource;
1064 info->ehRequest = requestHandle;
1066 result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
1069 if (result != OC_STACK_OK)
1075 AddClientRequestInfo(&clientRequstList, info);
1077 pointerAction = pointerAction->next;
1083 void DoScheduledGroupAction()
1085 OIC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
1086 ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
1090 OIC_LOG(INFO, TAG, "Target resource is NULL");
1093 else if (info->resource == NULL)
1095 OIC_LOG(INFO, TAG, "Target resource is NULL");
1098 else if (info->actionset == NULL)
1100 OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
1103 else if (info->ehRequest == NULL)
1105 OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
1111 DoAction(info->resource, info->actionset, info->ehRequest);
1113 MUTEX_UNLOCK(&lock);
1116 if (info->actionset->type == RECURSIVE)
1118 ScheduledResourceInfo *schedule;
1119 schedule = (ScheduledResourceInfo *) OICMalloc(
1120 sizeof(ScheduledResourceInfo));
1124 OIC_LOG(INFO, TAG, "Building New Call Info.");
1125 memset(schedule, 0, sizeof(ScheduledResourceInfo));
1127 if (info->actionset->timesteps > 0)
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 OIC_LOG(INFO, TAG, "Reregisteration.");
1139 MUTEX_UNLOCK(&lock);
1140 AddScheduledResource(&scheduleResourceList, schedule);
1149 RemoveScheduledResource(&scheduleResourceList, info);
1156 OCStackResult BuildCollectionGroupActionCBORResponse(
1157 OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1158 OCEntityHandlerRequest *ehRequest)
1160 OCStackResult stackRet = OC_STACK_ERROR;
1162 OIC_LOG(INFO, TAG, "Group Action is requested.");
1164 char *doWhat = NULL;
1165 char *details = NULL;
1168 static bool initializedCriticalSection = false;
1170 if(false == initializedCriticalSection) {
1171 /** @todo Find a way to DeleteCriticalSection somewhere. */
1172 InitializeCriticalSection(&lock);
1173 initializedCriticalSection = true;
1177 stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
1179 if(stackRet != OC_STACK_OK)
1181 OIC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1185 stackRet = OC_STACK_ERROR;
1187 if (method == OC_REST_PUT)
1189 OIC_LOG(INFO, TAG, "Group Action[PUT].");
1191 if (strcmp(doWhat, ACTIONSET) == 0)
1193 OCActionSet *actionSet = NULL;
1194 stackRet = BuildActionSetFromString(&actionSet, details);
1196 if(stackRet == OC_STACK_OK)
1198 if (actionSet != NULL)
1200 stackRet = AddActionSet(&resource->actionsetHead,
1202 if (stackRet == OC_STACK_ERROR)
1204 if(actionSet != NULL)
1206 DeleteActionSet( &actionSet );
1208 OIC_LOG(INFO, TAG, "Duplicated ActionSet ");
1213 stackRet = OC_STACK_ERROR;
1219 stackRet = OC_STACK_ERROR;
1223 else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1225 if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1227 stackRet = OC_STACK_OK;
1231 stackRet = OC_STACK_ERROR;
1235 OCRepPayload* payload = OCRepPayloadCreate();
1239 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
1240 stackRet = OC_STACK_ERROR;
1244 OCEntityHandlerResponse response = { 0 };
1246 if(stackRet == OC_STACK_OK)
1247 response.ehResult = OC_EH_OK;
1249 response.ehResult = OC_EH_ERROR;
1251 // Format the response. Note this requires some info about the request
1252 response.requestHandle = ehRequest->requestHandle;
1253 response.resourceHandle = ehRequest->resource;
1254 response.payload = (OCPayload*) payload;
1255 response.numSendVendorSpecificHeaderOptions = 0;
1256 memset(response.sendVendorSpecificHeaderOptions, 0,
1257 sizeof response.sendVendorSpecificHeaderOptions);
1258 memset(response.resourceUri, 0, sizeof response. resourceUri);
1259 // Indicate that response is NOT in a persistent buffer
1260 response.persistentBufferFlag = 0;
1261 response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1263 // Send the response
1264 if (OCDoResponse(&response) != OC_STACK_OK)
1266 OIC_LOG(ERROR, TAG, "Error sending response");
1267 stackRet = OC_STACK_ERROR;
1271 else if (method == OC_REST_POST)
1273 OCActionSet *actionset = NULL;
1275 OCRepPayload* payload = OCRepPayloadCreate();
1276 OCRepPayloadSetUri(payload, resource->uri);
1278 if ((strcmp(doWhat, DO_ACTION) == 0)
1279 || (strcmp(doWhat, "DoScheduledAction") == 0))
1281 char *pActionsetName = NULL;
1282 long int delay = -1;
1284 if (strcmp(doWhat, "DoScheduledAction") == 0)
1286 stackRet = ExtractActionSetNameAndDelaytime(details,
1287 &pActionsetName, &delay);
1290 details = pActionsetName;
1294 stackRet = OC_STACK_OK;
1297 if (stackRet == OC_STACK_OK)
1299 if (GetActionSet(details, resource->actionsetHead,
1300 &actionset) != OC_STACK_OK)
1302 OIC_LOG(INFO, TAG, "ERROR");
1303 stackRet = OC_STACK_ERROR;
1306 if (actionset == NULL)
1308 OIC_LOG(INFO, TAG, "Cannot Find ActionSet");
1309 stackRet = OC_STACK_ERROR;
1313 OIC_LOG(INFO, TAG, "Group Action[POST].");
1314 if (actionset->type == NONE)
1316 OIC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1317 actionset->actionsetName);
1318 unsigned int num = GetNumOfTargetResource(
1321 ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1322 HandleAggregateResponse;
1323 ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1326 DoAction(resource, actionset,
1327 (OCServerRequest*) ehRequest->requestHandle);
1328 stackRet = OC_STACK_OK;
1332 OIC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1333 actionset->actionsetName);
1336 (delay == -1 ? actionset->timesteps : delay);
1338 ScheduledResourceInfo *schedule;
1339 schedule = (ScheduledResourceInfo *) OICMalloc(
1340 sizeof(ScheduledResourceInfo));
1344 OIC_LOG(INFO, TAG, "Building New Call Info.");
1346 sizeof(ScheduledResourceInfo));
1348 schedule->resource = resource;
1349 schedule->actionset = actionset;
1350 schedule->ehRequest =
1351 (OCServerRequest*) ehRequest->requestHandle;
1352 MUTEX_UNLOCK(&lock);
1355 OIC_LOG_V(INFO, TAG, "delay_time is %ld seconds.",
1356 actionset->timesteps);
1358 schedule->time = registerTimer(delay,
1359 &schedule->timer_id,
1360 &DoScheduledGroupAction);
1361 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 OIC_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 OIC_LOG(ERROR, TAG, "Error sending response");
1441 stackRet = OC_STACK_ERROR;