864cced3f46b09235e3a68effdabe9067b76c02b
[platform/upstream/iotivity.git] / resource / csdk / stack / src / oicgroup.c
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #define _POSIX_C_SOURCE 200112L
22
23 #include <string.h>
24
25 #include "oicgroup.h"
26 #include "cJSON.h"
27 #include "cbor.h"
28 #include "ocpayload.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31 #include "occollection.h"
32 #include "logger.h"
33 #include "timer.h"
34
35 #ifndef WITH_ARDUINO
36 #include <pthread.h>
37 #endif
38
39 #define TAG "OICGROUP"
40
41 #define DESC_DELIMITER          "\""
42 #define ACTION_DELIMITER        "*"
43 #define ATTR_DELIMITER          "|"
44 #define ATTR_ASSIGN             "="
45
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"
52
53 #define DEFAULT_CONTEXT_VALUE 0x99
54
55 #define VARIFY_POINTER_NULL(pointer, result, toExit) \
56     if(pointer == NULL) \
57     {\
58         result = OC_STACK_NO_MEMORY;\
59         goto toExit;\
60     }
61 #define VARIFY_PARAM_NULL(pointer, result, toExit) \
62     if(pointer == NULL)\
63     {\
64         result = OC_STACK_INVALID_PARAM;\
65         goto exit;\
66     }
67
68 #define OCFREE(pointer) \
69     { \
70         OICFree(pointer); \
71         pointer = NULL; \
72     }
73
74 #ifndef WITH_ARDUINO
75 pthread_mutex_t lock;
76 #endif
77
78 enum ACTION_TYPE
79 {
80     NONE = 0, SCHEDULED, RECURSIVE
81 };
82
83 typedef struct scheduledresourceinfo
84 {
85     OCResource *resource;
86     OCActionSet *actionset;
87
88     int timer_id;
89
90     OCServerRequest *ehRequest;
91
92     time_t time;
93     struct scheduledresourceinfo* next;
94 } ScheduledResourceInfo;
95
96 ScheduledResourceInfo *scheduleResourceList = NULL;
97
98 void AddScheduledResource(ScheduledResourceInfo **head,
99         ScheduledResourceInfo* add)
100 {
101     OC_LOG(INFO, TAG, "AddScheduledResource Entering...");
102
103 #ifndef WITH_ARDUINO
104     pthread_mutex_lock(&lock);
105 #endif
106     ScheduledResourceInfo *tmp = NULL;
107
108     if (*head != NULL)
109     {
110         tmp = *head;
111
112         while (tmp->next)
113         {
114             tmp = tmp->next;
115         }
116         tmp->next = add;
117     }
118     else
119     {
120         *head = add;
121     }
122 #ifndef WITH_ARDUINO
123     pthread_mutex_unlock(&lock);
124 #endif
125 }
126
127 ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
128 {
129     OC_LOG(INFO, TAG, "GetScheduledResource Entering...");
130
131 #ifndef WITH_ARDUINO
132     pthread_mutex_lock(&lock);
133 #endif
134
135     time_t t_now;
136
137     ScheduledResourceInfo *tmp = NULL;
138     tmp = head;
139
140 #ifndef WITH_ARDUINO
141     time(&t_now);
142 #else
143     t_now = now();
144 #endif
145
146     if (tmp)
147     {
148         while (tmp)
149         {
150             time_t diffTm = 0;
151 #ifndef WITH_ARDUINO
152             diffTm = timespec_diff(tmp->time, t_now);
153 #else
154             diffTm = timespec_diff(tmp->time, t_now);
155 #endif
156
157             if (diffTm <= (time_t) 0)
158             {
159                 OC_LOG(INFO, TAG, "return Call INFO.");
160                 goto exit;
161             }
162
163             tmp = tmp->next;
164         }
165     }
166
167     exit:
168 #ifndef WITH_ARDUINO
169     pthread_mutex_unlock(&lock);
170 #endif
171     if (tmp == NULL)
172     {
173         OC_LOG(INFO, TAG, "Cannot Find Call Info.");
174     }
175     return tmp;
176 }
177
178 ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
179 {
180     OC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering...");
181
182 #ifndef WITH_ARDUINO
183     pthread_mutex_lock(&lock);
184 #endif
185     ScheduledResourceInfo *tmp = NULL;
186     tmp = head;
187
188     if (tmp)
189     {
190         while (tmp)
191         {
192             if (strcmp(tmp->actionset->actionsetName, setName) == 0)
193             {
194                 OC_LOG(INFO, TAG, "return Call INFO.");
195                 goto exit;
196             }
197             tmp = tmp->next;
198         }
199     }
200
201 exit:
202 #ifndef WITH_ARDUINO
203     pthread_mutex_unlock(&lock);
204 #endif
205     if (tmp == NULL)
206     {
207         OC_LOG(INFO, TAG, "Cannot Find Call Info.");
208     }
209     return tmp;
210 }
211
212 void RemoveScheduledResource(ScheduledResourceInfo **head,
213         ScheduledResourceInfo* del)
214 {
215 #ifndef WITH_ARDUINO
216     pthread_mutex_lock(&lock);
217 #endif
218     OC_LOG(INFO, TAG, "RemoveScheduledResource Entering...");
219     ScheduledResourceInfo *tmp = NULL;
220
221     if (del == NULL)
222     {
223 #ifndef WITH_ARDUINO
224     pthread_mutex_unlock(&lock);
225 #endif
226         return;
227     }
228
229     if (*head == del)
230     {
231         *head = (*head)->next;
232     }
233     else
234     {
235         tmp = *head;
236         while (tmp->next && (tmp->next != del))
237         {
238             tmp = tmp->next;
239         }
240         if (tmp->next)
241         {
242             tmp->next = del->next;
243         }
244     }
245
246     OCFREE(del)
247 #ifndef WITH_ARDUINO
248     pthread_mutex_unlock(&lock);
249 #endif
250 }
251
252 typedef struct aggregatehandleinfo
253 {
254     OCServerRequest *ehRequest;
255     OCDoHandle required;
256     OCResource *collResource;
257
258     struct aggregatehandleinfo *next;
259 } ClientRequestInfo;
260
261 ClientRequestInfo *clientRequstList = NULL;
262
263 void AddClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* add)
264 {
265     ClientRequestInfo *tmp = NULL;
266
267     if (*head != NULL)
268     {
269         tmp = *head;
270
271         while (tmp->next)
272         {
273             tmp = tmp->next;
274         }
275         tmp->next = add;
276     }
277     else
278     {
279         *head = add;
280     }
281 }
282
283 ClientRequestInfo* GetClientRequestInfo(ClientRequestInfo *head,
284         OCDoHandle handle)
285 {
286     ClientRequestInfo *tmp = NULL;
287
288     tmp = head;
289
290     if (tmp)
291     {
292         while (tmp)
293         {
294             if (tmp->required == handle)
295             {
296                 break;
297             }
298
299             tmp = tmp->next;
300         }
301
302         return tmp;
303     }
304     return NULL;
305 }
306
307 void RemoveClientRequestInfo(ClientRequestInfo **head, ClientRequestInfo* del)
308 {
309     ClientRequestInfo *tmp = NULL;
310
311     if (del == NULL)
312         return;
313
314     if (*head == del)
315     {
316         *head = (*head)->next;
317     }
318     else
319     {
320         tmp = *head;
321         while (tmp->next && (tmp->next != del))
322         {
323             tmp = tmp->next;
324         }
325         if (tmp->next)
326         {
327             tmp->next = del->next;
328         }
329     }
330 }
331
332 void AddCapability(OCCapability** head, OCCapability* node)
333 {
334     OCCapability *pointer = *head;
335     if (NULL == pointer)
336     {
337         *head = node;
338     }
339     else
340     {
341         while (pointer->next != NULL)
342         {
343             pointer = pointer->next;
344         }
345
346         pointer->next = node;
347     }
348 }
349
350 void AddAction(OCAction** head, OCAction* node)
351 {
352     OCAction *pointer = *head;
353     if (NULL == pointer)
354     {
355         *head = node;
356     }
357     else
358     {
359
360         while (pointer->next != NULL)
361         {
362             pointer = pointer->next;
363         }
364
365         pointer->next = node;
366     }
367 }
368
369 OCStackResult AddActionSet(OCActionSet **head, OCActionSet* node)
370 {
371     OCActionSet *pointer = *head;
372     OCActionSet *prev = NULL;
373     if(node == NULL)
374     {
375         return OC_STACK_ERROR;
376     }
377     if (NULL == pointer)
378     {
379         *head = node;
380     }
381     else
382     {
383         prev = pointer;
384         while (pointer != NULL)
385         {
386             // check the uniqeness of actionsetname.
387             if (strcmp(pointer->actionsetName, node->actionsetName) == 0)
388             {
389                 return OC_STACK_ERROR;
390             }
391
392             prev = pointer;
393             pointer = pointer->next;
394         }
395
396         prev->next = node;
397     }
398
399     return OC_STACK_OK;
400 }
401
402 void DeleteCapability(OCCapability *del)
403 {
404     OCFREE(del->capability)
405     del->capability = NULL;
406     OCFREE(del->status)
407     del->status = NULL;
408     OCFREE(del)
409 }
410
411 void DeleteAction(OCAction** action)
412 {
413     OCCapability* pointer = (*action)->head;
414     OCCapability* pDel = NULL;
415
416     while (pointer)
417     {
418         pDel = pointer;
419         pointer = pointer->next;
420
421         DeleteCapability(pDel);
422     }
423     OCFREE((*action)->resourceUri)
424     (*action)->next = NULL;
425     OCFREE(*action)
426 }
427
428 void DeleteActionSet(OCActionSet** actionset)
429 {
430     if(*actionset == NULL)
431         return;
432
433     OCAction* pointer = (*actionset)->head;
434     OCAction* pDel = NULL;
435
436     while (pointer)
437     {
438         pDel = pointer;
439         pointer = pointer->next;
440
441         DeleteAction(&pDel);
442     }
443     //    (*actionset)->head = NULL;
444     OCFREE((*actionset)->actionsetName)
445     OCFREE(*actionset)
446 }
447
448 OCStackResult FindAndDeleteActionSet(OCResource **resource,
449         const char * actionsetName)
450 {
451     if (*resource != NULL)
452     {
453         OCActionSet *pointer = NULL;
454         OCActionSet *pDel = NULL;
455
456         pointer = (*resource)->actionsetHead;
457
458         if (pointer == NULL)
459         {
460             return OC_STACK_ERROR;
461         }
462         else
463         {
464             if (strcmp(pointer->actionsetName, actionsetName) == 0)
465             {
466                 if (pointer->next != NULL)
467                     (*resource)->actionsetHead = pointer->next;
468                 else
469                     (*resource)->actionsetHead = NULL;
470
471                 DeleteActionSet(&pointer);
472             }
473             else if (pointer->next != NULL)
474             {
475                 while (pointer)
476                 {
477                     if (pointer->next != NULL)
478                     {
479                         if (strcmp(pointer->next->actionsetName, actionsetName)
480                                 == 0)
481                         {
482                             pDel = pointer->next;
483                             pointer->next = pointer->next->next;
484
485                             DeleteActionSet(&pDel);
486                         }
487                     }
488                     pointer = pointer->next;
489                 }
490             }
491
492             return OC_STACK_OK;
493         }
494
495     }
496     return OC_STACK_ERROR;
497 }
498
499 OCStackResult DeleteActionSets(OCResource** resource)
500 {
501     OCActionSet *pointer = (*resource)->actionsetHead;
502     OCActionSet *pDel = pointer;
503
504     while (pointer)
505     {
506         pDel = pointer;
507         pointer = pointer->next;
508
509         DeleteActionSet(&pDel);
510         pDel->next = NULL;
511     }
512
513     (*resource)->actionsetHead = NULL;
514     return OC_STACK_OK;
515 }
516
517 OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
518         OCActionSet** actionset)
519 {
520     OCActionSet *pointer = head;
521
522     while (pointer)
523     {
524         if (strcmp(pointer->actionsetName, actionName) == 0)
525         {
526             *actionset = pointer;
527             return OC_STACK_OK;
528         }
529
530         pointer = pointer->next;
531     }
532
533     return OC_STACK_ERROR;
534
535 }
536
537 OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest,
538                                         char **key, char **value)
539 {
540     OCStackResult result = OC_STACK_OK;
541
542     char *actionSetStr = NULL;
543
544     if( NULL == ehRequest->payload )
545     {
546         result = OC_STACK_ERROR;
547         goto exit;
548     }
549
550     OCRepPayload* input;
551
552     input = (OCRepPayload*)(ehRequest->payload);
553
554     if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
555     {
556         *key = OICStrdup(ACTIONSET);
557         VARIFY_POINTER_NULL(*key, result, exit);
558
559         *value = OICStrdup(actionSetStr);
560         VARIFY_POINTER_NULL(*value, result, exit);
561     }
562     else if(OCRepPayloadGetPropString(input, DO_ACTION, &actionSetStr))
563     {
564         *key = OICStrdup(DO_ACTION);
565         VARIFY_POINTER_NULL(*key, result, exit);
566
567         *value = OICStrdup(actionSetStr);
568         VARIFY_POINTER_NULL(*value, result, exit);
569     }
570     else if(OCRepPayloadGetPropString(input, GET_ACTIONSET, &actionSetStr))
571     {
572         *key = OICStrdup(GET_ACTIONSET);
573         VARIFY_POINTER_NULL(*key, result, exit);
574
575         *value = OICStrdup(actionSetStr);
576         VARIFY_POINTER_NULL(*value, result, exit);
577     }
578     else if(OCRepPayloadGetPropString(input, DELETE_ACTIONSET, &actionSetStr))
579     {
580         *key = OICStrdup(DELETE_ACTIONSET);
581         VARIFY_POINTER_NULL(*key, result, exit);
582
583         *value = OICStrdup(actionSetStr);
584         VARIFY_POINTER_NULL(*value, result, exit);
585     }
586     else if(OCRepPayloadGetPropString(input, CANCEL_ACTIONSET, &actionSetStr))
587     {
588         *key = OICStrdup(CANCEL_ACTIONSET);
589         VARIFY_POINTER_NULL(*key, result, exit);
590
591         *value = OICStrdup(actionSetStr);
592         VARIFY_POINTER_NULL(*value, result, exit);
593     }
594     else
595     {
596         result = OC_STACK_ERROR;
597     }
598
599 exit:
600     if (result != OC_STACK_OK)
601     {
602         OCFREE(*key)
603         OCFREE(*value)
604     }
605
606     OCFREE(actionSetStr);
607
608     return result;
609 }
610
611 OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
612         long int *pa)
613 {
614     char *token = NULL, *tokenPtr = NULL;
615     OCStackResult result = OC_STACK_OK;
616
617     token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
618     VARIFY_POINTER_NULL(token, result, exit)
619
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);
624
625     token = strtok_r(NULL, ACTION_DELIMITER, &tokenPtr);
626     VARIFY_POINTER_NULL(pa, result, exit)
627     VARIFY_PARAM_NULL(token, result, exit)
628     *pa = atoi(token);
629
630     return OC_STACK_OK;
631
632 exit:
633     OCFREE(*setName);
634     return result;
635 }
636
637 OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc)
638 {
639     OCStackResult result = OC_STACK_OK;
640
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;
646
647     OCAction *action = NULL;
648     OCCapability *capa = NULL;
649
650     OC_LOG(INFO, TAG, "Build ActionSet Instance.");
651
652     *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
653     VARIFY_POINTER_NULL(*set, result, exit)
654
655     iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
656
657     // ActionSet Name
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);
663
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)
670 #ifndef WITH_ARDUINO
671     if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
672     {
673         // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
674         goto exit;
675     }
676 #endif
677
678     OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
679
680     iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
681     while (iterToken)
682     {
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,
688                 &descIterTokenPtr);
689         while (descIterToken)
690         {
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);
695
696             attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
697                     &attrIterTokenPtr);
698             VARIFY_POINTER_NULL(attrIterToken, result, exit);
699
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);
704
705             attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
706                     &attrIterTokenPtr);
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);
711
712             if (strcmp(key, "uri") == 0)
713             {
714                 OC_LOG(INFO, TAG, "Build OCAction Instance.");
715
716                 if(action)
717                 {
718                     OICFree(action->resourceUri);
719                     OICFree(action);
720                 }
721                 action = (OCAction*) OICMalloc(sizeof(OCAction));
722                 VARIFY_POINTER_NULL(action, result, exit)
723                 memset(action, 0, sizeof(OCAction));
724                 action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
725                 VARIFY_POINTER_NULL(action->resourceUri, result, exit)
726                 VARIFY_PARAM_NULL(value, result, exit)
727                 strncpy(action->resourceUri, value, strlen(value) + 1);
728             }
729             else
730             {
731                 if ((key != NULL) && (value != NULL))
732                 {
733                     OC_LOG(INFO, TAG, "Build OCCapability Instance.");
734
735                     capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
736                     VARIFY_POINTER_NULL(capa, result, exit)
737                     memset(capa, 0, sizeof(OCCapability));
738
739                     capa->capability = (char *) OICMalloc(strlen(key) + 1);
740                     VARIFY_POINTER_NULL(capa->capability, result, exit)
741                     VARIFY_PARAM_NULL(key, result, exit)
742                     strncpy(capa->capability, key, strlen(key) + 1);
743
744                     capa->status = (char *) OICMalloc(strlen(value) + 1);
745                     VARIFY_POINTER_NULL(capa->status, result, exit)
746                     VARIFY_PARAM_NULL(value, result, exit)
747                     strncpy(capa->status, value, strlen(value) + 1);
748
749                     VARIFY_POINTER_NULL(action, result, exit)
750
751                     AddCapability(&action->head, capa);
752                 }
753             }
754
755             OCFREE(key)
756             OCFREE(value)
757             OCFREE(attr)
758
759             descIterToken = (char *) strtok_r(NULL, ATTR_DELIMITER,
760                     &descIterTokenPtr);
761         }
762
763         AddAction(&(*set)->head, action);
764         iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
765         OCFREE(desc);
766     }
767
768     return OC_STACK_OK;
769 exit:
770     OCFREE(attr)
771     OCFREE(desc)
772     OCFREE(capa)
773     OCFREE(action)
774     OCFREE(*set)
775     OCFREE(key)
776     OCFREE(value)
777     OCFREE(attr)
778
779     return result;
780 }
781
782 OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
783 {
784     // Can't use the macros here as they are hardcoded to 'exit' and will
785     // result in dereferencing a null pointer.
786     if (!actionset || !desc)
787     {
788         return OC_STACK_INVALID_PARAM;
789     }
790     char temp[1024] = { 0 };
791     size_t remaining = sizeof(temp) - 1;
792     OCStackResult res = OC_STACK_ERROR;
793
794     OCAction *action = actionset->head;
795
796     if (remaining >= strlen(actionset->actionsetName) + 1)
797     {
798         strcat(temp, actionset->actionsetName);
799         remaining -= strlen(actionset->actionsetName);
800         strcat(temp, ACTION_DELIMITER);
801         remaining--;
802     }
803     else
804     {
805         res = OC_STACK_ERROR;
806         goto exit;
807     }
808
809     while (action != NULL)
810     {
811         if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
812         {
813             res = OC_STACK_ERROR;
814             goto exit;
815         }
816         strcat(temp, "uri=");
817         remaining -= strlen("uri=");
818         strcat(temp, action->resourceUri);
819         remaining -= strlen(action->resourceUri);
820         strcat(temp, "|");
821         remaining--;
822
823         OCCapability *capas = action->head;
824         while (capas != NULL)
825         {
826             if (remaining < (strlen(capas->capability)
827                              + 1 + strlen(capas->status)))
828             {
829                 res = OC_STACK_ERROR;
830                 goto exit;
831             }
832
833             strcat(temp, capas->capability);
834             remaining -= strlen(capas->capability);
835             strcat(temp, "=");
836             remaining--;
837             strcat(temp, capas->status);
838             remaining -= strlen(capas->status);
839
840             capas = capas->next;
841             if (capas != NULL)
842             {
843                 if (remaining < 1)
844                 {
845                     res = OC_STACK_ERROR;
846                     goto exit;
847                 }
848                 strcat(temp, "|");
849             }
850         }
851
852         action = action->next;
853         if (action != NULL)
854         {
855             if (remaining < strlen(ACTION_DELIMITER))
856             {
857                 res = OC_STACK_ERROR;
858                 goto exit;
859             }
860             strcat(temp, ACTION_DELIMITER);
861             remaining--;
862         }
863     }
864
865     *desc = OICStrdup(temp);
866     VARIFY_POINTER_NULL(*desc, res, exit);
867
868     return OC_STACK_OK;
869
870 exit:
871     OCFREE(*desc);
872     return res;
873 }
874
875 OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
876         OCClientResponse* clientResponse)
877 {
878     (void)context;
879     (void)clientResponse;
880     OC_LOG(INFO, TAG, "Entering ActionSetCB");
881
882     ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
883
884     if (info)
885     {
886         OCEntityHandlerResponse response = { 0 };
887
888         response.ehResult = OC_EH_OK;
889
890         if(NULL == clientResponse->payload)
891         {
892             OC_LOG(ERROR, TAG, "Error sending response");
893             return OC_STACK_DELETE_TRANSACTION;
894         }
895
896         // Format the response.  Note this requires some info about the request
897         response.requestHandle = info->ehRequest;
898         response.resourceHandle = info->collResource;
899         response.payload = clientResponse->payload;
900         response.numSendVendorSpecificHeaderOptions = 0;
901         memset(response.sendVendorSpecificHeaderOptions, 0,
902                 sizeof response.sendVendorSpecificHeaderOptions);
903         memset(response.resourceUri, 0, sizeof response.resourceUri);
904         // Indicate that response is NOT in a persistent buffer
905         response.persistentBufferFlag = 0;
906
907         // Send the response
908         if (OCDoResponse(&response) != OC_STACK_OK)
909         {
910             OC_LOG(ERROR, TAG, "Error sending response");
911             return OC_STACK_DELETE_TRANSACTION;
912         }
913
914         RemoveClientRequestInfo(&clientRequstList, info);
915         OCFREE(info)
916     }
917
918     return OC_STACK_KEEP_TRANSACTION;
919 }
920
921 void ActionSetCD(void *context)
922 {
923     (void)context;
924 }
925
926 OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
927         uint16_t *remaining)
928 {
929     OCStackResult ret = OC_STACK_ERROR;
930     cJSON *json;
931     cJSON *body;
932
933     char *jsonStr;
934     uint16_t jsonLen;
935
936     OC_LOG(INFO, TAG, "Entering BuildActionJSON");
937     json = cJSON_CreateObject();
938
939     cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
940
941     OCCapability* pointerCapa = action->head;
942     while (pointerCapa)
943     {
944         cJSON_AddStringToObject(body, pointerCapa->capability,
945                 pointerCapa->status);
946         pointerCapa = pointerCapa->next;
947     }
948
949     jsonStr = cJSON_PrintUnformatted(json);
950
951     jsonLen = strlen(jsonStr);
952     if (jsonLen < *remaining)
953     {
954         strcat((char*) bufferPtr, jsonStr);
955         *remaining -= jsonLen;
956         bufferPtr += jsonLen;
957         ret = OC_STACK_OK;
958     }
959
960     cJSON_Delete(json);
961     free(jsonStr);
962
963     return ret;
964 }
965
966 OCPayload* BuildActionCBOR(OCAction* action)
967 {
968     OCRepPayload* payload = OCRepPayloadCreate();
969
970     if (!payload)
971     {
972         OC_LOG(INFO, TAG, "Failed to create put payload object");
973         return NULL;
974     }
975
976     OCCapability* pointerCapa = action->head;
977     while (pointerCapa)
978     {
979         OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
980         pointerCapa = pointerCapa->next;
981     }
982
983     return (OCPayload*) payload;
984 }
985
986 unsigned int GetNumOfTargetResource(OCAction *actionset)
987 {
988     int numOfResource = 0;
989
990     OCAction *pointerAction = actionset;
991
992     while (pointerAction != NULL)
993     {
994         numOfResource++;
995         pointerAction = pointerAction->next;
996     }
997
998     return numOfResource;
999 }
1000
1001 OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
1002         OCPayload *payload)
1003 {
1004
1005     OCCallbackData cbData;
1006     cbData.cb = &ActionSetCB;
1007     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
1008     cbData.cd = NULL;
1009
1010     return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
1011                        payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
1012 }
1013
1014 OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
1015         OCServerRequest* requestHandle)
1016 {
1017     OCStackResult result = OC_STACK_ERROR;
1018
1019     if( NULL == actionset->head)
1020     {
1021         return result;
1022     }
1023
1024     OCAction *pointerAction = actionset->head;
1025
1026     while (pointerAction != NULL)
1027     {
1028         OCPayload* payload;
1029         payload = BuildActionCBOR(pointerAction);
1030
1031         if(payload == NULL)
1032         {
1033             return result;
1034         }
1035
1036         ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
1037                 sizeof(ClientRequestInfo));
1038
1039         if( info == NULL )
1040         {
1041             OCFREE(payload);
1042             return OC_STACK_NO_MEMORY;
1043         }
1044
1045         memset(info, 0, sizeof(ClientRequestInfo));
1046
1047         info->collResource = resource;
1048         info->ehRequest = requestHandle;
1049
1050         result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
1051                 payload);
1052
1053         if (result != OC_STACK_OK)
1054         {
1055             OICFree(info);
1056             return result;
1057         }
1058
1059         AddClientRequestInfo(&clientRequstList, info);
1060
1061         pointerAction = pointerAction->next;
1062     }
1063
1064     return result;
1065 }
1066
1067 void DoScheduledGroupAction()
1068 {
1069     OC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
1070     ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
1071
1072     if (info == NULL)
1073     {
1074         OC_LOG(INFO, TAG, "Target resource is NULL");
1075         goto exit;
1076     }
1077     else if (info->resource == NULL)
1078     {
1079         OC_LOG(INFO, TAG, "Target resource is NULL");
1080         goto exit;
1081     }
1082     else if (info->actionset == NULL)
1083     {
1084         OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1085         goto exit;
1086     }
1087     else if (info->ehRequest == NULL)
1088     {
1089         OC_LOG(INFO, TAG, "Target ActionSet is NULL");
1090         goto exit;
1091     }
1092 #ifndef WITH_ARDUINO
1093     pthread_mutex_lock(&lock);
1094 #endif
1095     DoAction(info->resource, info->actionset, info->ehRequest);
1096 #ifndef WITH_ARDUINO
1097     pthread_mutex_unlock(&lock);
1098 #endif
1099
1100     if (info->actionset->type == RECURSIVE)
1101     {
1102         ScheduledResourceInfo *schedule;
1103         schedule = (ScheduledResourceInfo *) OICMalloc(
1104                 sizeof(ScheduledResourceInfo));
1105
1106         if (schedule)
1107         {
1108             OC_LOG(INFO, TAG, "Building New Call Info.");
1109             memset(schedule, 0, sizeof(ScheduledResourceInfo));
1110
1111             if (info->actionset->timesteps > 0)
1112             {
1113 #ifndef WITH_ARDUINO
1114                 pthread_mutex_lock(&lock);
1115 #endif
1116                 schedule->resource = info->resource;
1117                 schedule->actionset = info->actionset;
1118                 schedule->ehRequest = info->ehRequest;
1119
1120                 schedule->time = registerTimer(info->actionset->timesteps,
1121                         &schedule->timer_id,
1122                         &DoScheduledGroupAction);
1123
1124                 OC_LOG(INFO, TAG, "Reregisteration.");
1125 #ifndef WITH_ARDUINO
1126                 pthread_mutex_unlock(&lock);
1127 #endif
1128                 AddScheduledResource(&scheduleResourceList, schedule);
1129             }
1130             else
1131             {
1132                 OICFree(schedule);
1133             }
1134         }
1135     }
1136
1137     RemoveScheduledResource(&scheduleResourceList, info);
1138
1139     exit:
1140
1141     return;
1142 }
1143
1144 OCStackResult BuildCollectionGroupActionCBORResponse(
1145         OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
1146         OCEntityHandlerRequest *ehRequest)
1147 {
1148     OCStackResult stackRet = OC_STACK_ERROR;
1149
1150     OC_LOG(INFO, TAG, "Group Action is requested.");
1151
1152     char *doWhat = NULL;
1153     char *details = NULL;
1154
1155     stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
1156
1157     if(stackRet != OC_STACK_OK)
1158     {
1159         OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
1160         return stackRet;
1161     }
1162
1163     stackRet = OC_STACK_ERROR;
1164
1165     if (method == OC_REST_PUT)
1166     {
1167         OC_LOG(INFO, TAG, "Group Action[PUT].");
1168
1169         if (strcmp(doWhat, ACTIONSET) == 0)
1170         {
1171             OCActionSet *actionSet = NULL;
1172             stackRet = BuildActionSetFromString(&actionSet, details);
1173
1174             if(stackRet == OC_STACK_OK)
1175             {
1176                 if (actionSet != NULL)
1177                 {
1178                     stackRet = AddActionSet(&resource->actionsetHead,
1179                             actionSet);
1180                     if (stackRet == OC_STACK_ERROR)
1181                     {
1182                         if(actionSet != NULL)
1183                         {
1184                             DeleteActionSet( &actionSet );
1185                         }
1186                         OC_LOG(INFO, TAG, "Duplicated ActionSet ");
1187                     }
1188                 }
1189                 else
1190                 {
1191                     stackRet = OC_STACK_ERROR;
1192                     goto exit;
1193                 }
1194             }
1195             else
1196             {
1197                 stackRet = OC_STACK_ERROR;
1198             }
1199
1200         }
1201         else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
1202         {
1203             if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
1204             {
1205                 stackRet = OC_STACK_OK;
1206             }
1207             else
1208             {
1209                 stackRet = OC_STACK_ERROR;
1210             }
1211         }
1212
1213         OCRepPayload* payload = OCRepPayloadCreate();
1214
1215         if(!payload)
1216         {
1217             OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1218             stackRet = OC_STACK_ERROR;
1219         }
1220         else
1221         {
1222             OCEntityHandlerResponse response = { 0 };
1223
1224             if(stackRet == OC_STACK_OK)
1225                 response.ehResult = OC_EH_OK;
1226             else
1227                 response.ehResult = OC_EH_ERROR;
1228
1229             // Format the response.  Note this requires some info about the request
1230             response.requestHandle = ehRequest->requestHandle;
1231             response.resourceHandle = ehRequest->resource;
1232             response.payload = (OCPayload*) payload;
1233             response.numSendVendorSpecificHeaderOptions = 0;
1234             memset(response.sendVendorSpecificHeaderOptions, 0,
1235                     sizeof response.sendVendorSpecificHeaderOptions);
1236             memset(response.resourceUri, 0, sizeof response. resourceUri);
1237             // Indicate that response is NOT in a persistent buffer
1238             response.persistentBufferFlag = 0;
1239             response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1240
1241             // Send the response
1242             if (OCDoResponse(&response) != OC_STACK_OK)
1243             {
1244                 OC_LOG(ERROR, TAG, "Error sending response");
1245                 stackRet = OC_STACK_ERROR;
1246             }
1247         }
1248     }
1249     else if (method == OC_REST_POST)
1250     {
1251         OCActionSet *actionset = NULL;
1252
1253         OCRepPayload* payload = OCRepPayloadCreate();
1254         OCRepPayloadSetUri(payload, resource->uri);
1255
1256         if ((strcmp(doWhat, DO_ACTION) == 0)
1257                 || (strcmp(doWhat, "DoScheduledAction") == 0))
1258         {
1259             char *pActionsetName = NULL;
1260             long int delay = -1;
1261
1262             if (strcmp(doWhat, "DoScheduledAction") == 0)
1263             {
1264                 stackRet = ExtractActionSetNameAndDelaytime(details,
1265                         &pActionsetName, &delay);
1266
1267                 OCFREE(details)
1268                 details = pActionsetName;
1269             }
1270             else
1271             {
1272                 stackRet = OC_STACK_OK;
1273             }
1274
1275             if (stackRet == OC_STACK_OK)
1276             {
1277                 if (GetActionSet(details, resource->actionsetHead,
1278                         &actionset) != OC_STACK_OK)
1279                 {
1280                     OC_LOG(INFO, TAG, "ERROR");
1281                     stackRet = OC_STACK_ERROR;
1282                 }
1283
1284                 if (actionset == NULL)
1285                 {
1286                     OC_LOG(INFO, TAG, "Cannot Find ActionSet");
1287                     stackRet = OC_STACK_ERROR;
1288                 }
1289                 else
1290                 {
1291                     OC_LOG(INFO, TAG, "Group Action[POST].");
1292                     if (actionset->type == NONE)
1293                     {
1294                         OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
1295                                 actionset->actionsetName);
1296                         unsigned int num = GetNumOfTargetResource(
1297                                 actionset->head);
1298
1299                         ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
1300                                 HandleAggregateResponse;
1301                         ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
1302                                 num + 1;
1303
1304                         DoAction(resource, actionset,
1305                                 (OCServerRequest*) ehRequest->requestHandle);
1306                         stackRet = OC_STACK_OK;
1307                     }
1308                     else
1309                     {
1310                         OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
1311                                 actionset->actionsetName);
1312
1313                         delay =
1314                                 (delay == -1 ? actionset->timesteps : delay);
1315
1316                         ScheduledResourceInfo *schedule;
1317                         schedule = (ScheduledResourceInfo *) OICMalloc(
1318                                 sizeof(ScheduledResourceInfo));
1319
1320                         if (schedule)
1321                         {
1322                             OC_LOG(INFO, TAG, "Building New Call Info.");
1323                             memset(schedule, 0,
1324                                     sizeof(ScheduledResourceInfo));
1325 #ifndef WITH_ARDUINO
1326                             pthread_mutex_lock(&lock);
1327 #endif
1328                             schedule->resource = resource;
1329                             schedule->actionset = actionset;
1330                             schedule->ehRequest =
1331                                     (OCServerRequest*) ehRequest->requestHandle;
1332 #ifndef WITH_ARDUINO
1333                             pthread_mutex_unlock(&lock);
1334 #endif
1335                             if (delay > 0)
1336                             {
1337                                 OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
1338                                         actionset->timesteps);
1339 #ifndef WITH_ARDUINO
1340                                 pthread_mutex_lock(&lock);
1341 #endif
1342                                 schedule->time = registerTimer(delay,
1343                                         &schedule->timer_id,
1344                                         &DoScheduledGroupAction);
1345 #ifndef WITH_ARDUINO
1346                                 pthread_mutex_unlock(&lock);
1347 #endif
1348                                 AddScheduledResource(&scheduleResourceList,
1349                                         schedule);
1350                                 stackRet = OC_STACK_OK;
1351                             }
1352                             else
1353                             {
1354                                 stackRet = OC_STACK_ERROR;
1355                             }
1356                         }
1357                     }
1358                 }
1359             }
1360         }
1361         else if (strcmp(doWhat, "CancelAction") == 0)
1362         {
1363             ScheduledResourceInfo *info =
1364                     GetScheduledResourceByActionSetName(scheduleResourceList, details);
1365
1366             if(info != NULL)
1367             {
1368                 unregisterTimer(info->timer_id);
1369
1370                 RemoveScheduledResource(&scheduleResourceList, info);
1371                 stackRet = OC_STACK_OK;
1372             }
1373             else
1374             {
1375                 stackRet = OC_STACK_ERROR;
1376             }
1377         }
1378
1379         else if (strcmp(doWhat, GET_ACTIONSET) == 0)
1380         {
1381             char *plainText = NULL;
1382             OCActionSet *actionset = NULL;
1383
1384             GetActionSet(details, resource->actionsetHead, &actionset);
1385             if (actionset != NULL)
1386             {
1387                 BuildStringFromActionSet(actionset, &plainText);
1388
1389                 if (plainText != NULL)
1390                 {
1391                     OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
1392                 }
1393                 OICFree(plainText);
1394                 stackRet = OC_STACK_OK;
1395             }
1396         }
1397
1398         if(!payload)
1399         {
1400             OC_LOG(ERROR, TAG, "Failed to allocate Payload");
1401             stackRet = OC_STACK_ERROR;
1402         }
1403         else
1404         {
1405             OCEntityHandlerResponse response = { 0 };
1406             if(stackRet == OC_STACK_OK)
1407                 response.ehResult = OC_EH_OK;
1408             else
1409                 response.ehResult = OC_EH_ERROR;
1410
1411             // Format the response.  Note this requires some info about the request
1412             response.requestHandle = ehRequest->requestHandle;
1413             response.resourceHandle = ehRequest->resource;
1414             response.payload = (OCPayload*) payload;
1415             response.numSendVendorSpecificHeaderOptions = 0;
1416             memset(response.sendVendorSpecificHeaderOptions, 0,
1417                     sizeof response.sendVendorSpecificHeaderOptions);
1418             memset(response.resourceUri, 0, sizeof response.resourceUri);
1419             // Indicate that response is NOT in a persistent buffer
1420             response.persistentBufferFlag = 0;
1421             response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
1422
1423             // Send the response
1424             if (OCDoResponse(&response) != OC_STACK_OK)
1425             {
1426                 OC_LOG(ERROR, TAG, "Error sending response");
1427                 stackRet = OC_STACK_ERROR;
1428             }
1429         }
1430     }
1431
1432 exit:
1433
1434     OCFREE(doWhat)
1435     OCFREE(details)
1436
1437     return stackRet;
1438 }