Initialize Tizen 2.3
[framework/system/oma-dm-agent.git] / src / agent / serviceadapter / sa_command.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*sync-agent*/
19 #include <sync_agent.h>
20
21 /*dm-agent*/
22 #include "common/dm_common.h"
23 #include "serviceadapter/sa_command.h"
24 #include "serviceadapter/sa_command_internal.h"
25 #include "serviceadapter/sa_elements.h"
26 #include "serviceadapter/sa_elements_internal.h"
27 #include "serviceadapter/sa_session.h"
28 #include "serviceadapter/sa_session_internal.h"
29
30 #ifndef OMADM_AGENT_LOG
31 #undef LOG_TAG
32 #define LOG_TAG "OMA_DM_SA"
33 #endif
34
35 static Command *_create_Cmd(Session * pSession, CommandType type);
36
37 static Command *_create_Cmd(Session * pSession, CommandType type)
38 {
39         _INNER_FUNC_ENTER;
40
41         DM_ERROR ret = DM_OK;
42
43         retvm_if((pSession) == NULL, NULL, "pSession is NULL!!");
44         retvm_if((type) == COMMAND_TYPE_UNKNOWN, NULL, "type is NULL!!");
45
46         _DEBUG_TRACE(" start type : %d  msgID : %d cmdID : %d\n", type, pSession->msgID, pSession->cmdID);
47
48         Command *cmd = (Command *) calloc(1, sizeof(Command));
49         if (cmd == NULL) {
50                 ret = COMMON_ERR_ALLOC;
51                 goto error;
52         }
53
54         cmd->type = type;
55         cmd->msgID = pSession->msgID;
56         cmd->cmdID = pSession->cmdID;
57         cmd->refCount = 1;
58
59         PendingStatus *temp = NULL;
60         ret = create_prependingstatus(pSession->msgID, pSession->cmdID, &temp);
61         if (ret != DM_OK)
62                 goto error;
63
64         pSession->pendingStatus = g_list_append(pSession->pendingStatus, temp);
65         pSession->cmdID++;
66         _DEBUG_INFO("session cmdId : %d", pSession->cmdID);
67
68         _DEBUG_TRACE(" end\n");
69         _INNER_FUNC_EXIT;
70         return cmd;
71
72  error:
73         free_command(cmd);
74         _DEBUG_TRACE(" error : %d\n", ret);
75         _INNER_FUNC_EXIT;
76         return NULL;
77
78 }
79
80 DM_ERROR duplicate_command(Command * source, Command ** target)
81 {
82         _EXTERN_FUNC_ENTER;
83
84         retvm_if((source) == NULL, COMMON_ERR_IS_NULL, "command is NULL!!");
85         DM_ERROR ret = DM_OK;
86
87         CommandType type = source->type;
88
89         switch (type) {
90         case COMMAND_TYPE_RESULTS:
91                 {
92                         _DEBUG_INFO("dup result command");
93                         *target = (Command *) calloc(1, sizeof(Command));
94                         if (*target == NULL) {
95                                 ret = COMMON_ERR_ALLOC;
96                                 goto error;
97                         }
98
99                         (*target)->type = type;
100                         (*target)->msgID = source->msgID;
101                         (*target)->cmdID = source->cmdID;
102                         (*target)->refCount = 1;
103                         if (source->source != NULL) {
104                                 _DEBUG_INFO("source location");
105                                 (*target)->source = dup_location(source->source);
106                         }
107                         if (source->target) {
108                                 _DEBUG_INFO("taget location");
109                                 (*target)->target = dup_location(source->target);
110                         }
111
112                         _DEBUG_INFO("result data cmdRef : %d, msgRef : %d", source->private.results.cmdRef, source->private.results.msgRef);
113                         (*target)->private.results.cmdRef = source->private.results.cmdRef;
114                         (*target)->private.results.msgRef = source->private.results.msgRef;
115
116                         if (source->private.results.targetRef) {
117                                 _DEBUG_INFO("target ref");
118                                 (*target)->private.results.targetRef = dup_location(source->private.results.targetRef);
119                         }
120                         if (source->private.results.type) {
121                                 _DEBUG_INFO("type");
122                                 (*target)->private.results.type = strdup(source->private.results.type);
123                         }
124                         if (source->private.results.items) {
125                                 _DEBUG_INFO("result item list");
126                                 GList *iter_list = NULL;
127                                 Item *temp = NULL;
128
129                                 for (iter_list = source->private.results.items; iter_list != NULL; iter_list = g_list_next(iter_list)) {
130                                         _DEBUG_INFO("result item");
131                                         temp = (Item *) iter_list->data;
132                                         Item *dup_item = NULL;
133                                         if (temp->source != NULL) {
134                                                 if (temp->source->locURI != NULL) {
135                                                         _DEBUG_INFO("loc uri : %s", temp->source->locURI);
136                                                 }
137                                                 ret = construct_Item(temp->source->locURI, temp->format, temp->contenttype, temp->private.data, temp->size, temp->moreData, &dup_item);
138                                                 _DEBUG_INFO("construct item : %d", ret);
139
140                                                 (*target)->private.results.items = g_list_append((*target)->private.results.items, dup_item);
141                                         } else {
142                                                 _DEBUG_INFO("source is null");
143                                         }
144                                 }
145                         }
146                 }
147                 break;
148         case COMMAND_TYPE_ALERT:
149         case COMMAND_TYPE_HEADER:
150         case COMMAND_TYPE_ADD:
151         case COMMAND_TYPE_REPLACE:
152         case COMMAND_TYPE_DELETE:
153         case COMMAND_TYPE_GET:
154         case COMMAND_TYPE_EXEC:
155         case COMMAND_TYPE_SEQUENCE:
156         case COMMAND_TYPE_ATOMIC:
157         case COMMAND_TYPE_COPY:
158         case COMMAND_TYPE_UNKNOWN:
159                 break;
160
161         }
162         _EXTERN_FUNC_EXIT;
163         return ret;
164
165  error:
166         _EXTERN_FUNC_EXIT;
167         return ret;
168
169 }
170
171 void free_commands(GList * commands)
172 {
173         _EXTERN_FUNC_ENTER;
174
175         if (commands == NULL) {
176                 _EXTERN_FUNC_EXIT;
177                 return;
178         }
179
180         GList *iter = NULL;
181         Command *pCommand = NULL;
182         for (iter = commands; iter != NULL;) {
183                 pCommand = iter->data;
184                 _DEBUG_INFO("command type is %d\n", pCommand->type);
185
186                 iter = g_list_next(iter);
187                 commands = g_list_remove(commands, pCommand);
188                 if (pCommand->type != COMMAND_TYPE_UNKNOWN) {
189                         free_command(pCommand);
190                         _DEBUG_INFO("free command");
191                 }
192         }
193         _DEBUG_INFO("free command list");
194         g_list_free(commands);
195
196         _EXTERN_FUNC_EXIT;
197         return;
198 }
199
200 DM_ERROR set_correlator(Command ** pAlertCommand, char *pCorrelator)
201 {
202         _EXTERN_FUNC_ENTER;
203
204         DM_ERROR ret = DM_OK;
205
206         retvm_if((*pAlertCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pAlertCommand is NULL!!");
207         retvm_if((pCorrelator) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCorrelator is NULL!!");
208
209         (*pAlertCommand)->private.alert.Correlator = g_strdup(pCorrelator);
210
211         _EXTERN_FUNC_EXIT;
212         return ret;
213
214 }
215
216 DM_ERROR create_alert_cmd(Session * session, AlertType syncType, Command ** pCommand)
217 {
218         _EXTERN_FUNC_ENTER;
219
220         DM_ERROR ret = DM_OK;
221
222         *pCommand = _create_Cmd(session, COMMAND_TYPE_ALERT);
223         if ((*pCommand) == NULL) {
224                 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
225                 goto error;
226         }
227
228         (*pCommand)->private.alert.type = syncType;
229
230         _EXTERN_FUNC_EXIT;
231         return ret;
232
233  error:
234         _DEBUG_INFO(" error : %d\n", ret);
235         _EXTERN_FUNC_EXIT;
236         return ret;
237 }
238
239 DM_ERROR create_replace_cmd(Session * session, Command ** pCommand)
240 {
241         _EXTERN_FUNC_ENTER;
242
243         DM_ERROR ret = DM_OK;
244
245         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
246
247         (*pCommand) = _create_Cmd(session, COMMAND_TYPE_REPLACE);
248         if ((*pCommand) == NULL) {
249                 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
250                 goto error;
251         }
252
253         (*pCommand)->private.change.type = CHANGE_REPLACE;
254
255         _EXTERN_FUNC_EXIT;
256         return ret;
257  error:
258         _DEBUG_INFO(" error : %d\n", ret);
259         _EXTERN_FUNC_EXIT;
260         return ret;
261 }
262
263 DM_ERROR create_results_cmd(Session * session, Command ** pCommand)
264 {
265         _EXTERN_FUNC_ENTER;
266
267         DM_ERROR ret = DM_OK;
268
269         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
270
271         *pCommand = _create_Cmd(session, COMMAND_TYPE_RESULTS);
272         if ((*pCommand) == NULL) {
273                 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
274                 goto error;
275         }
276
277         _EXTERN_FUNC_EXIT;
278         return ret;
279  error:
280         _DEBUG_INFO(" error : %d\n", ret);
281         _EXTERN_FUNC_EXIT;
282         return ret;
283 }
284
285 DM_ERROR create_add_cmd(Session * session, char *luid, const char *contenttype, char *data, unsigned int size, int moreData, Command ** pCommand)
286 {
287         _EXTERN_FUNC_ENTER;
288
289         DM_ERROR ret = DM_OK;
290
291         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
292         retvm_if((luid) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "luid is NULL!!");
293         retvm_if((contenttype) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "contenttype is NULL!!");
294
295         Item *temp = NULL;
296         Location *pLocation = NULL;
297
298         *pCommand = _create_Cmd(session, COMMAND_TYPE_ADD);
299         if (*pCommand == NULL) {
300                 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
301                 goto error;
302         }
303
304         ret = create_item_data(data, size, &temp);
305         if (ret != DM_OK) {
306                 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
307                 goto error;
308         }
309
310         (*pCommand)->private.change.type = CHANGE_ADD;
311         temp->contenttype = g_strdup(contenttype);
312         temp->moreData = moreData;
313
314         ret = create_location(luid, NULL, &pLocation);
315         if (ret != DM_OK)
316                 goto error;
317
318         set_itemsource(temp, pLocation);
319         (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
320
321         _EXTERN_FUNC_EXIT;
322         return ret;
323
324  error:
325         free_Item(temp);
326         _DEBUG_INFO(" error : %d\n", ret);
327         _EXTERN_FUNC_EXIT;
328         return ret;
329 }
330
331 DM_ERROR create_delete_cmd(Session * session, char *luid, const char *contenttype, Command ** pCommand)
332 {
333         _EXTERN_FUNC_ENTER;
334
335         DM_ERROR ret = DM_OK;
336
337         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
338         retvm_if((luid) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "luid is NULL!!");
339         retvm_if((contenttype) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "contenttype is NULL!!");
340
341         Item *temp = NULL;
342         Location *pLocation = NULL;
343
344         *pCommand = _create_Cmd(session, COMMAND_TYPE_DELETE);
345         if (*pCommand == NULL) {
346                 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
347                 goto error;
348         }
349
350         temp = create_Item();
351         if (temp == NULL) {
352                 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
353                 goto error;
354         }
355
356         (*pCommand)->private.change.type = CHANGE_DELETE;
357
358         ret = create_location(luid, NULL, &pLocation);
359         if (ret != DM_OK)
360                 goto error;
361
362         set_itemsource(temp, pLocation);
363         temp->contenttype = g_strdup(contenttype);
364         (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
365
366         _EXTERN_FUNC_EXIT;
367         return ret;
368
369  error:
370         free_Item(temp);
371         _DEBUG_INFO(" error : %d\n", ret);
372         _EXTERN_FUNC_EXIT;
373         return ret;
374
375 }
376
377 void free_command(Command * pCommand)
378 {
379         _EXTERN_FUNC_ENTER;
380         if (pCommand == NULL) {
381                 _EXTERN_FUNC_EXIT;
382                 return;
383         }
384
385         GList *iter = NULL;
386         _DEBUG_INFO(" Command type is %d", pCommand->type);
387
388         if (pCommand->refCount > 1) {
389                 _DEBUG_INFO("Command's refCount is %d\n", pCommand->refCount);
390                 /*decrease_command_refcount(pCommand); */
391                 _EXTERN_FUNC_EXIT;
392                 return;
393         }
394
395         switch (pCommand->type) {
396         case COMMAND_TYPE_ALERT:
397                 _DEBUG_INFO("alert type : %d", pCommand->private.alert.type);
398                 if ((pCommand->private.alert.type >= DM_ALERT_DISPLAY && pCommand->private.alert.type <= DM_ALERT_MULTIPLE_CHOICE)
399                     && pCommand->private.alert.items != NULL) {
400                         for (iter = pCommand->private.alert.items; iter != NULL; iter = g_list_next(iter)) {
401                                 free_Item((Item *) iter->data);
402                         }
403                 }
404                 _DEBUG_INFO("alert correlator");
405                 if (pCommand->private.alert.Correlator != NULL) {
406                         free(pCommand->private.alert.Correlator);
407                         pCommand->private.alert.Correlator = NULL;
408                 }
409                 break;
410         case COMMAND_TYPE_HEADER:
411                 _DEBUG_INFO("header type");
412                 //COMMAND_TYPE_HEADER doesnot come here
413                 break;
414         case COMMAND_TYPE_ADD:
415         case COMMAND_TYPE_REPLACE:
416         case COMMAND_TYPE_DELETE:
417         case COMMAND_TYPE_COPY:
418                 _DEBUG_INFO("add repalce delete type");
419                 if (pCommand->private.change.items != NULL) {
420                         for (iter = pCommand->private.change.items; iter != NULL; iter = g_list_next(iter)) {
421                                 free_Item(iter->data);
422                         }
423                 }
424                 break;
425         case COMMAND_TYPE_GET:
426                 _DEBUG_INFO("get type");
427                 if (pCommand->private.access.type != NULL) {
428                         free(pCommand->private.access.type);
429                         pCommand->private.access.type = NULL;
430                 }
431
432                 if (pCommand->private.access.items != NULL) {
433                         for (iter = pCommand->private.access.items; iter != NULL; iter = g_list_next(iter)) {
434                                 free_Item(iter->data);
435                         }
436                 }
437                 break;
438         case COMMAND_TYPE_RESULTS:
439                 _DEBUG_INFO("result type");
440                 if (pCommand->private.results.type != NULL) {
441                         free(pCommand->private.results.type);
442                         pCommand->private.results.type = NULL;
443                 }
444                 if (pCommand->private.results.items != NULL) {
445                         for (iter = pCommand->private.results.items; iter != NULL; iter = g_list_next(iter)) {
446                                 free_Item(iter->data);
447                         }
448                 }
449
450                 break;
451         case COMMAND_TYPE_EXEC:
452                 _DEBUG_INFO("exec type");
453                 if (pCommand->private.exec.correlator != NULL) {
454                         free(pCommand->private.exec.correlator);
455                         pCommand->private.exec.correlator = NULL;
456                 }
457                 if (pCommand->private.exec.item != NULL) {
458                         free_Item(pCommand->private.exec.item);
459                 }
460                 break;
461         case COMMAND_TYPE_UNKNOWN:
462                 _DEBUG_INFO("unknown type");
463                 break;
464         case COMMAND_TYPE_SEQUENCE:
465         case COMMAND_TYPE_ATOMIC:
466                 _DEBUG_INFO("sequence atomic copy type");
467                 if (pCommand->private.sequence_atomic.commands != NULL) {
468                         for (iter = pCommand->private.sequence_atomic.commands; iter != NULL; iter = g_list_next(iter)) {
469                                 free_command(iter->data);
470                         }
471                 }
472                 break;
473         default:
474                 break;
475         }
476         _DEBUG_INFO("command source");
477         if (pCommand->source != NULL) {
478                 free_location(pCommand->source);
479                 pCommand->source = NULL;
480         }
481
482         _DEBUG_INFO("command target");
483         if (pCommand->target != NULL) {
484                 free_location(pCommand->target);
485                 pCommand->target = NULL;
486         }
487
488         _DEBUG_INFO("free command ");
489         free(pCommand);
490         pCommand = NULL;
491
492         _DEBUG_INFO("free command end");
493         _EXTERN_FUNC_EXIT;
494 }
495
496 void free_statuses(GList * status)
497 {
498         _EXTERN_FUNC_ENTER;
499
500         if (!status) {
501                 _DEBUG_INFO(" List is null\n");
502                 _EXTERN_FUNC_EXIT;
503                 return;
504         }
505
506         GList *iter = NULL;
507         int i = 0;
508         _DEBUG_INFO(" count : %d\n", g_list_length(status));
509         for (iter = status; iter != NULL; iter = g_list_next(iter)) {
510                 _DEBUG_INFO(" count : %d\n", ++i);
511                 free_status(iter->data);
512         }
513
514         g_list_free(status);
515
516         _EXTERN_FUNC_EXIT;
517 }
518
519 void free_status(Status * pStatus)
520 {
521         _EXTERN_FUNC_ENTER;
522
523         if (pStatus == NULL) {
524                 _EXTERN_FUNC_EXIT;
525                 return;
526         }
527
528         if (pStatus->data != NULL)
529                 free(pStatus->data);
530
531         if (pStatus->sourceRef != NULL)
532                 free_location(pStatus->sourceRef);
533
534         if (pStatus->targetRef != NULL)
535                 free_location(pStatus->targetRef);
536 /*
537         if(pStatus->res_chal)
538                 free_chal(pStatus->res_chal);
539
540         if(pStatus->cred)
541                 free_cred(pStatus->cred);*/
542
543         //free_Item(pStatus->item);
544         GList *iter = NULL;
545         for (iter = pStatus->items; iter != NULL; iter = g_list_next(iter))
546                 free_Item(iter->data);
547
548         if (pStatus != NULL) {
549                 free(pStatus);
550                 pStatus = NULL;
551         }
552
553         _EXTERN_FUNC_EXIT;
554 }
555
556 DM_ERROR get_statuscode(Status * status)
557 {
558         _EXTERN_FUNC_ENTER;
559
560         retvm_if((status) == NULL, DM_ERR_FORBIDDEN, "status is NULL!!");
561
562         _EXTERN_FUNC_EXIT;
563         return atoi(status->data);
564 }
565
566 ChangeType converttochangetype(unsigned int type)
567 {
568         _EXTERN_FUNC_ENTER;
569
570         ChangeType changeType = CHANGE_UNKNOWN;
571         switch (type) {
572         case 1:
573                 changeType = CHANGE_ADD;
574                 break;
575         case 2:
576                 changeType = CHANGE_REPLACE;
577                 break;
578         case 3:
579                 changeType = CHANGE_DELETE;
580                 break;
581         case 4:
582                 changeType = CHANGE_COPY;
583                 break;
584         }
585
586         _EXTERN_FUNC_EXIT;
587         return changeType;
588 }
589
590 unsigned int convertfromchangetype(ChangeType changeType)
591 {
592         _EXTERN_FUNC_ENTER;
593
594         unsigned int type = 0;
595
596         switch (changeType) {
597         case CHANGE_ADD:
598                 type = 1;
599                 break;
600         case CHANGE_REPLACE:
601                 type = 2;
602                 break;
603         case CHANGE_DELETE:
604                 type = 3;
605                 break;
606         case CHANGE_UNKNOWN:
607                 break;
608         case CHANGE_COPY:
609                 type = 4;
610                 break;
611         default:
612                 break;
613         }
614
615         _EXTERN_FUNC_EXIT;
616         return type;
617 }
618
619 CommandType converttocommandtype(char *type)
620 {
621         _EXTERN_FUNC_ENTER;
622
623         retvm_if((type) == NULL, COMMAND_TYPE_UNKNOWN, "type is NULL!!");
624
625         if (strcmp(type, "Alert") == 0) {
626                 _EXTERN_FUNC_EXIT;
627                 return COMMAND_TYPE_ALERT;
628         } else if (strcmp(type, "SyncHdr") == 0) {
629                 _EXTERN_FUNC_EXIT;
630                 return COMMAND_TYPE_HEADER;
631         } else if (strcmp(type, "Add") == 0) {
632                 _EXTERN_FUNC_EXIT;
633                 return COMMAND_TYPE_ADD;
634         } else if (strcmp(type, "Replace") == 0) {
635                 _EXTERN_FUNC_EXIT;
636                 return COMMAND_TYPE_REPLACE;
637         } else if (strcmp(type, "Delete") == 0) {
638                 _EXTERN_FUNC_EXIT;
639                 return COMMAND_TYPE_DELETE;
640         } else if (strcmp(type, "Results") == 0) {
641                 _EXTERN_FUNC_EXIT;
642                 return COMMAND_TYPE_RESULTS;
643         } else if (strcmp(type, "Get") == 0) {
644                 _EXTERN_FUNC_EXIT;
645                 return COMMAND_TYPE_GET;
646         } else if (strcmp(type, "Exec") == 0) {
647                 _EXTERN_FUNC_EXIT;
648                 return COMMAND_TYPE_EXEC;
649         } else if (strcmp(type, "Sequence") == 0) {
650                 _EXTERN_FUNC_EXIT;
651                 return COMMAND_TYPE_SEQUENCE;
652         } else if (strcmp(type, "Atomic") == 0) {
653                 _EXTERN_FUNC_EXIT;
654                 return COMMAND_TYPE_ATOMIC;
655         } else if (strcmp(type, "Copy") == 0) {
656                 _EXTERN_FUNC_EXIT;
657                 return COMMAND_TYPE_COPY;
658         }
659         _EXTERN_FUNC_EXIT;
660         return COMMAND_TYPE_UNKNOWN;
661 }
662
663 char *convertfromcommandtype(CommandType type)
664 {
665         _EXTERN_FUNC_ENTER;
666
667         char *commandType = NULL;
668
669         switch (type) {
670         case COMMAND_TYPE_ALERT:
671                 commandType = "Alert";
672                 break;
673         case COMMAND_TYPE_HEADER:
674                 commandType = "SyncHdr";
675                 break;
676         case COMMAND_TYPE_ADD:
677                 commandType = "Add";
678                 break;
679         case COMMAND_TYPE_REPLACE:
680                 commandType = "Replace";
681                 break;
682         case COMMAND_TYPE_DELETE:
683                 commandType = "Delete";
684                 break;
685         case COMMAND_TYPE_RESULTS:
686                 commandType = "Results";
687                 break;
688         case COMMAND_TYPE_GET:
689                 commandType = "Get";
690                 break;
691         case COMMAND_TYPE_EXEC:
692                 commandType = "Exec";
693                 break;
694         case COMMAND_TYPE_ATOMIC:
695                 commandType = "Atomic";
696                 break;
697         case COMMAND_TYPE_SEQUENCE:
698                 commandType = "Sequence";
699                 break;
700         case COMMAND_TYPE_COPY:
701                 commandType = "Copy";
702                 break;
703         default:
704                 commandType = "UNKNOWN";
705         }
706         _EXTERN_FUNC_EXIT;
707         return commandType;
708 }
709
710 ChangeType convertToChangeTypeFromCommandType(CommandType type)
711 {
712         _EXTERN_FUNC_ENTER;
713
714         ChangeType changeType = CHANGE_UNKNOWN;
715         switch (type) {
716
717         case COMMAND_TYPE_ADD:
718                 changeType = CHANGE_ADD;
719                 break;
720         case COMMAND_TYPE_REPLACE:
721                 changeType = CHANGE_REPLACE;
722                 break;
723         case COMMAND_TYPE_DELETE:
724                 changeType = CHANGE_DELETE;
725                 break;
726         case COMMAND_TYPE_COPY:
727                 changeType = CHANGE_COPY;
728                 break;
729         case COMMAND_TYPE_UNKNOWN:
730         case COMMAND_TYPE_ALERT:
731         case COMMAND_TYPE_HEADER:
732         case COMMAND_TYPE_GET:
733         case COMMAND_TYPE_RESULTS:
734         case COMMAND_TYPE_EXEC:
735         case COMMAND_TYPE_SEQUENCE:
736         case COMMAND_TYPE_ATOMIC:
737                 _DEBUG_INFO("not changetype command");
738                 break;
739         default:
740                 break;
741         }
742         _EXTERN_FUNC_EXIT;
743         return changeType;
744 }
745
746 CommandType convertToCommandTypeFromChangeType(ChangeType type)
747 {
748         _EXTERN_FUNC_ENTER;
749
750         CommandType commandType = COMMAND_TYPE_UNKNOWN;
751
752         switch (type) {
753         case CHANGE_UNKNOWN:
754                 commandType = COMMAND_TYPE_UNKNOWN;
755                 break;
756         case CHANGE_ADD:
757                 commandType = COMMAND_TYPE_ADD;
758                 break;
759         case CHANGE_REPLACE:
760                 commandType = COMMAND_TYPE_REPLACE;
761                 break;
762         case CHANGE_DELETE:
763                 commandType = COMMAND_TYPE_DELETE;
764                 break;
765         case CHANGE_COPY:
766                 commandType = COMMAND_TYPE_COPY;
767                 break;
768         }
769
770         _EXTERN_FUNC_EXIT;
771         return commandType;
772
773 }
774
775 DM_ERROR create_newstatuslocation(Session * session, DM_ERROR data, Command * command, Location * sourceref, Location * targetref, CommandType type, Status ** pStatus)
776 {
777
778         _EXTERN_FUNC_ENTER;
779
780         DM_ERROR ret = DM_OK;
781
782         retvm_if((session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
783         retvm_if((command) == NULL, COMMON_ERR_IS_NULL, "command is NULL!!");
784
785         ret = create_status(data, session->cmdID, session->lastRecievedMsgID, command->cmdID, sourceref, targetref, NULL, type, pStatus);
786         if (ret != DM_OK)
787                 goto error;
788
789         session->cmdID++;
790         _DEBUG_INFO("session cmdId : %d", session->cmdID);
791
792         _DEBUG_INFO(" end  %d\n", data);
793         _EXTERN_FUNC_EXIT;
794         return ret;
795  error:
796         _DEBUG_INFO(" error : %d\n", ret);
797         _EXTERN_FUNC_EXIT;
798         return ret;
799 }
800
801 DM_ERROR create_newstatus(Session * session, DM_ERROR data, Command * command, Location * sourceref, Location * targetref, CommandType type, Status ** pStatus)
802 {
803         _EXTERN_FUNC_ENTER;
804
805         DM_ERROR ret = DM_OK;
806
807         retvm_if((session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
808         retvm_if((command) == NULL, COMMON_ERR_IS_NULL, "command is NULL!!");
809
810         ret = create_status(data, session->cmdID, session->lastRecievedMsgID, command->cmdID, sourceref, targetref, NULL, type, pStatus);
811         if (ret != DM_OK)
812                 goto error;
813
814         session->cmdID++;
815         _DEBUG_INFO("session cmdId : %d", session->cmdID);
816
817         _EXTERN_FUNC_EXIT;
818         return ret;
819  error:
820         _DEBUG_INFO(" error : %d\n", ret);
821         _EXTERN_FUNC_EXIT;
822         return ret;
823 }
824
825 DM_ERROR create_status(DM_ERROR data, unsigned int cmdID, unsigned int msgref, unsigned int cmdref, Location * sourceref, Location * targetref, Chal * pChal, CommandType type, Status ** pStatus)
826 {
827         _EXTERN_FUNC_ENTER;
828
829         DM_ERROR ret = DM_OK;
830
831         _DEBUG_INFO("cmdref : %d msgref : %d", cmdref, msgref);
832         _DEBUG_INFO("create status : %d", data);
833
834         *pStatus = (Status *) calloc(1, sizeof(Status));
835         if (*pStatus == NULL) {
836                 ret = COMMON_ERR_ALLOC;
837                 goto error;
838         }
839
840         (*pStatus)->cmdID = cmdID;
841         (*pStatus)->msgRef = msgref;
842         (*pStatus)->cmdRef = cmdref;
843         (*pStatus)->type = type;
844
845         (*pStatus)->data = g_strdup_printf("%d", data);
846
847         if (sourceref != NULL) {
848                 (*pStatus)->sourceRef = dup_location(sourceref);
849         }
850
851         if (targetref != NULL) {
852                 (*pStatus)->targetRef = dup_location(targetref);
853                 if (targetref->locName != NULL) {
854                         _DEBUG_INFO("targetref : %s", targetref->locName);
855                 }
856                 if (targetref->locURI != NULL) {
857                         _DEBUG_INFO("targetref : %s", targetref->locURI);
858                 }
859         }
860
861         if (pChal != NULL)
862                 (*pStatus)->chal = dup_chal(pChal);
863
864         _EXTERN_FUNC_EXIT;
865         return ret;
866
867  error:
868         _DEBUG_INFO(" error : %d\n", ret);
869         _EXTERN_FUNC_EXIT;
870         return ret;
871 }
872
873 DM_ERROR set_status_data(Session * session, Command * pCommand, Item * item, DM_ERROR data)
874 {
875         _EXTERN_FUNC_ENTER;
876
877         DM_ERROR ret = DM_OK;
878
879         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
880         retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
881         retvm_if((item) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "item is NULL!!");
882
883         GList *status_iter = NULL;
884         Status *temp = NULL;
885         char *target_url = get_location_locuri(item->target);
886
887         for (status_iter = session->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
888                 temp = status_iter->data;
889
890                 if (pCommand->cmdID == temp->cmdRef && strcmp(temp->targetRef->locURI, target_url) == 0) {
891                         status_iter->data = g_strdup_printf("%d", data);
892                         break;
893                 }
894         }
895
896         _EXTERN_FUNC_EXIT;
897         return ret;
898 }
899
900 DM_ERROR set_resultscommand_msgref(Command * pCommand, unsigned int msgRef)
901 {
902
903         _EXTERN_FUNC_ENTER;
904         DM_ERROR ret = DM_OK;
905
906         retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
907
908         pCommand->private.results.msgRef = msgRef;
909
910         _EXTERN_FUNC_EXIT;
911         return ret;
912
913 }
914
915 DM_ERROR set_resultscommand_cmdref(Command * pCommand, unsigned int cmdRef)
916 {
917         _EXTERN_FUNC_ENTER;
918
919         DM_ERROR ret = DM_OK;
920
921         retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
922
923         pCommand->private.results.cmdRef = cmdRef;
924
925         _EXTERN_FUNC_EXIT;
926         return ret;
927
928 }
929
930 DM_ERROR set_resultscommand_targetref(Command * pCommand, Location * pLocation)
931 {
932         _EXTERN_FUNC_ENTER;
933
934         DM_ERROR ret = DM_OK;
935
936         retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
937         retvm_if((pLocation) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pLocation is NULL!!");
938
939         pCommand->private.results.targetRef = dup_location(pLocation);
940
941         _EXTERN_FUNC_EXIT;
942         return ret;
943
944 }