upload tizen1.0 source
[pkgs/o/oma-ds-service.git] / ServiceAdapter / SA_Command.c
1 /*
2  * oma-ds-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHak Park <juhaki.park@samsung.com>,
7  *          JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8  *          SunBong Ha <sunbong.ha@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24
25
26
27 /*
28  * For any sort of issue you concern as to this software,
29  * you may use following point of contact.
30  * All resources contributed on this software
31  * are orinigally written by S-Core Inc., a member of Samsung Group.
32  *
33  * SeongWon Shim <seongwon.shim@samsung.com>
34  */
35
36 /**
37  *   @SA_Command.c
38  *   @version                                                                   0.1
39  *   @brief                                                                             This file is the source file of implementation of functions for command structure which is used in Service Adapter
40  */
41
42 #include <stdlib.h>
43 #include "agent-framework/Utility/fw_log.h"
44 #include "ServiceAdapter/SA_Command.h"
45 #include "ServiceAdapter/SA_Command_Internal.h"
46 #include "ServiceAdapter/SA_Elements.h"
47 #include "ServiceAdapter/SA_Elements_Internal.h"
48 #include "ServiceAdapter/SA_Session.h"
49 #include "ServiceAdapter/SA_Session_Internal.h"
50
51 #define LOG_TAG "OMA_DS_SA"
52
53 static Command *__create_command(Session *session, CommandType type);
54
55
56 static Command *__create_command(Session *session, CommandType type)
57 {
58         FW_LOGV("start");
59         SA_ErrorType errorType = SA_INTERNAL_OK;
60
61         if (!session) {
62                 errorType = SA_INTERNAL_NOT_DEFINED;
63                 goto error;
64         }
65
66         if (!type) {
67                 errorType = SA_INTERNAL_NOT_DEFINED;
68                 goto error;
69         }
70
71         Command *cmd = (Command *) calloc(1, sizeof(Command));
72         if (!cmd) {
73                 errorType = SA_INTERNAL_NO_MEMORY;
74                 goto error;
75         }
76
77
78         cmd->type = type;
79         cmd->msgID = session->msgID;
80         cmd->cmdID = session->cmdID;
81         cmd->refCount = 1;
82
83         session->cmdID++;
84
85         FW_LOGV("end");
86
87         return cmd;
88
89 error:
90         FW_LOGE("error : %d", errorType);
91         return NULL;
92 }
93
94 void free_commands(GList *commands)
95 {
96         FW_LOGV("start list length is %d", g_list_length(commands));
97
98         if (!commands) {
99                 FW_LOGV("List is NULL");
100                 return;
101         }
102
103
104         GList *iter = NULL;
105         Command *pCommand = NULL;
106         for (iter = commands; iter != NULL;) {
107                 pCommand = iter->data;
108                 FW_LOGV("command list length is %d command type is %d\n", g_list_length(commands), pCommand->type);
109
110                 iter = g_list_next(iter);
111                 if (pCommand->type != COMMAND_TYPE_UNKNOWN) {
112                         free_command(pCommand);
113                         commands = g_list_remove(commands, pCommand);
114                 }
115         }
116
117
118         g_list_free(commands);
119
120         FW_LOGV("end");
121
122         return;
123 }
124
125 SA_ErrorType create_alert_command(Session *session, AlertType  syncType, Location *source, Location *target,
126                                                                                                                 char *lastAnchor, char *nextAnchor, Cred *pCred, Command **pCommand)
127 {
128         FW_LOGV("start with syncType =%d", syncType);
129
130         SA_ErrorType errorType = SA_INTERNAL_OK;
131         Anchor *pAnchor = NULL;
132
133         if (!source) {
134                 errorType = SA_INTERNAL_NOT_DEFINED;
135                 goto error;
136         }
137
138         if (!target) {
139                 errorType = SA_INTERNAL_NOT_DEFINED;
140                 goto error;
141         }
142
143         *pCommand = __create_command(session, COMMAND_TYPE_ALERT);
144         if (*pCommand == NULL) {
145                 errorType = SA_INTERNAL_NO_MEMORY;
146                 goto error;
147         }
148
149         (*pCommand)->target = target;
150         (*pCommand)->source = source;
151
152         /*TODO  check that sync Type is ALERT_NEXT_MESSAGE(does not need last and next anchor)*/
153         /*TODO  check that sync Type is  ALERT_SLOW_SYNC(does not need last anchor)*/
154
155         if (nextAnchor) {
156                 errorType = create_anchor(lastAnchor, nextAnchor , &pAnchor);
157                 if (errorType != SA_INTERNAL_OK)
158                         goto error;
159
160                 (*pCommand)->private.alert.anchor = pAnchor;
161                 pAnchor = NULL;
162         }
163
164         if (pCred != NULL)
165                 (*pCommand)->cred = dup_cred(pCred);
166
167         (*pCommand)->private.alert.type = syncType;
168
169         FW_LOGV("end");
170
171         return errorType;
172
173 error:
174
175         if (pAnchor != NULL)
176                 free_anchor(pAnchor);
177
178         FW_LOGE("error : %d", errorType);
179         return errorType;
180 }
181
182 SA_ErrorType create_get_command(Session *session, Location* target, const char *contenttype, Command **pCommand)
183 {
184         FW_LOGV("start with content type = %s", contenttype);
185
186         SA_ErrorType errorType = SA_INTERNAL_OK;
187
188         if (!target) {
189                 errorType = SA_INTERNAL_NOT_DEFINED;
190                 goto error;
191         }
192
193         *pCommand = __create_command(session, COMMAND_TYPE_GET);
194         if (*pCommand == NULL) {
195                 errorType = SA_INTERNAL_NO_MEMORY;
196                 goto error;
197         }
198
199         (*pCommand)->private.access.item = create_item();
200         if ((*pCommand)->private.access.item == NULL) {
201                 errorType = SA_INTERNAL_NO_MEMORY;
202                 goto error;
203         }
204
205         set_item_target((*pCommand)->private.access.item, target);
206
207         if (contenttype != NULL)
208                 (*pCommand)->private.access.item->contenttype = strdup(contenttype);
209
210         FW_LOGV("end");
211
212         return errorType;
213
214 error:
215         FW_LOGE("error : %d", errorType);
216         return errorType;
217 }
218
219
220 SA_ErrorType create_put_command(Session *session, Location *source,  const char *contenttype,
221                                                                                                         DevInf *devInf , Command **pCommand)
222 {
223         FW_LOGV("start with content type = %s", contenttype);
224
225         SA_ErrorType errorType = SA_INTERNAL_OK;
226
227         if (!source) {
228                 errorType = SA_INTERNAL_NOT_DEFINED;
229                 goto error;
230         }
231
232         *pCommand = __create_command(session, COMMAND_TYPE_PUT);
233         if (*pCommand == NULL) {
234                 errorType = SA_INTERNAL_NO_MEMORY;
235                 goto error;
236         }
237
238         (*pCommand)->private.access.item = create_item_for_devinf(devInf);
239
240         if ((*pCommand)->private.access.item == NULL) {
241                 errorType = SA_INTERNAL_NO_MEMORY;
242                 goto error;
243         }
244
245         set_item_source((*pCommand)->private.access.item, source);
246
247         if (contenttype != NULL)
248                 (*pCommand)->private.access.item->contenttype = strdup(contenttype);
249
250         FW_LOGV("end");
251
252         return errorType;
253
254 error:
255         FW_LOGE("error : %d", errorType);
256         return errorType;
257 }
258
259
260 SA_ErrorType create_results_command(Session *session, Location *source,  const char *contenttype,
261                                                                                                                         DevInf *devInf , Command **pCommand)
262 {
263         FW_LOGV("start with content type = %s", contenttype);
264
265         SA_ErrorType errorType = SA_INTERNAL_OK;
266
267         if (!source) {
268                 errorType = SA_INTERNAL_NOT_DEFINED;
269                 goto error;
270         }
271
272         *pCommand = __create_command(session, COMMAND_TYPE_RESULTS);
273         if (*pCommand == NULL) {
274                 errorType = SA_INTERNAL_NO_MEMORY;
275                 goto error;
276         }
277
278         (*pCommand)->private.results.item = create_item_for_devinf(devInf);
279
280         if ((*pCommand)->private.results.item == NULL) {
281                 errorType = SA_INTERNAL_NO_MEMORY;
282                 goto error;
283         }
284
285         set_item_source((*pCommand)->private.results.item, source);
286
287         if (contenttype != NULL)
288                 (*pCommand)->private.results.item->contenttype = strdup(contenttype);
289
290         FW_LOGV("end");
291
292         return errorType;
293
294 error:
295         FW_LOGE("error : %d", errorType);
296         return errorType;
297 }
298
299 SA_ErrorType create_sync_start_command(Session *session, Location *source, Location *target,  Command **pCommand)
300 {
301         FW_LOGV("start");
302
303         SA_ErrorType errorType = SA_INTERNAL_OK;
304
305         if (!source) {
306                 errorType = SA_INTERNAL_NOT_DEFINED;
307                 goto error;
308         }
309
310         if (!target) {
311                 errorType = SA_INTERNAL_NOT_DEFINED;
312                 goto error;
313         }
314
315         *pCommand = __create_command(session, COMMAND_TYPE_SYNC_START);
316         if (*pCommand == NULL) {
317                 errorType = SA_INTERNAL_NO_MEMORY;
318                 goto error;
319         }
320
321         (*pCommand)->source = source;
322         (*pCommand)->target = target;
323
324
325         FW_LOGV("end");
326
327         return errorType;
328 error:
329         FW_LOGE("error : %d", errorType);
330         return errorType;
331 }
332
333 SA_ErrorType set_sync_start_command_numberofchanges(Command *pCommand, unsigned int numberOfChanges)
334 {
335         FW_LOGV("start");
336         SA_ErrorType errorType = SA_INTERNAL_OK;
337
338         if (pCommand == NULL) {
339                 errorType = SA_INTERNAL_NOT_DEFINED;
340                 goto error;
341         }
342
343         pCommand->private.sync.hasNumChanged = 1;
344         pCommand->private.sync.numChanged = numberOfChanges;
345
346         FW_LOGV("end");
347
348         return errorType;
349 error:
350         FW_LOGE("errorType = %d", errorType);
351         return errorType;
352 }
353
354 SA_ErrorType create_sync_end_command(Command **pCommand)
355 {
356         FW_LOGV("start");
357
358         SA_ErrorType errorType = SA_INTERNAL_OK;
359
360         *pCommand = (Command *) calloc(1, sizeof(Command));
361         if (*pCommand == NULL) {
362                 errorType = SA_INTERNAL_NO_MEMORY;
363                 goto error;
364         }
365
366         (*pCommand)->type = COMMAND_TYPE_SYNC_END;
367
368         FW_LOGV("end");
369
370         return errorType;
371 error:
372         FW_LOGE("errorType = %d", errorType);
373         return errorType;
374 }
375
376
377 SA_ErrorType create_add_command(Session *session, ChangeType type, char *luid, const char *contenttype,
378                                                                                                                 char *data, unsigned int size , int  moreData, Command **pCommand)
379 {
380         FW_LOGV("start with ChangeType = %d content type = %s", type, contenttype);
381
382         SA_ErrorType errorType = SA_INTERNAL_OK;
383         Item *temp = NULL;
384         Location *pLocation = NULL;
385
386         *pCommand = __create_command(session, COMMAND_TYPE_ADD);
387         if (*pCommand == NULL) {
388                 errorType = SA_INTERNAL_NO_MEMORY;
389                 goto error;
390         }
391
392         temp = create_item_for_data(data, size);
393         if (!temp) {
394                 errorType = SA_INTERNAL_NO_MEMORY;
395                 goto error;
396         }
397
398         (*pCommand)->private.change.type = type;
399         if (contenttype != NULL)
400                 temp->contenttype = strdup(contenttype);
401         temp->moreData = moreData;
402
403         errorType = create_location(luid, NULL, &pLocation);
404         if (errorType != SA_INTERNAL_OK)
405                 goto error;
406
407         set_item_source(temp, pLocation);
408         (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
409
410         FW_LOGV("end");
411
412         return errorType;
413 error:
414         FW_LOGE("errorType = %d", errorType);
415         return errorType;
416 }
417
418 SA_ErrorType create_replace_command(Session *session,  ChangeType type, char *luid, const char *contenttype,
419                                                                                                                         const char *data,  unsigned int size, int moreData,  Command **pCommand)
420 {
421         FW_LOGV("start with ChangeType = %d content type = %s", type, contenttype);
422
423         SA_ErrorType errorType = SA_INTERNAL_OK;
424         Item *temp = NULL;
425         Location *pLocation = NULL;
426
427         *pCommand = __create_command(session, COMMAND_TYPE_REPLACE);
428         if (*pCommand == NULL) {
429                 errorType = SA_INTERNAL_NO_MEMORY;
430                 goto error;
431         }
432
433         temp = create_item_for_data(data, size);
434         if (!temp) {
435                 errorType = SA_INTERNAL_NO_MEMORY;
436                 goto error;
437         }
438
439         (*pCommand)->private.change.type = type;
440         if (contenttype != NULL)
441                 temp->contenttype = strdup(contenttype);
442         temp->moreData = moreData;
443
444         errorType = create_location(luid, NULL, &pLocation);
445         if (errorType != SA_INTERNAL_OK)
446                 goto error;
447
448         set_item_source(temp, pLocation);
449         (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
450
451         FW_LOGV("end");
452
453         return errorType;
454 error:
455         FW_LOGE("errorType = %d", errorType);
456         return errorType;
457 }
458
459 SA_ErrorType create_delete_command(Session *session, ChangeType type, char *luid, const char *contenttype, Command **pCommand)
460 {
461         FW_LOGV("start with ChangeType = %d content type = %s", type, contenttype);
462
463         SA_ErrorType errorType = SA_INTERNAL_OK;
464         Item *temp = NULL;
465         Location *pLocation = NULL;
466
467         *pCommand = __create_command(session, COMMAND_TYPE_DELETE);
468         if (*pCommand == NULL) {
469                 errorType = SA_INTERNAL_NO_MEMORY;
470                 goto error;
471         }
472
473         temp = create_item();
474         if (!temp) {
475                 errorType = SA_INTERNAL_NO_MEMORY;
476                 goto error;
477         }
478
479         (*pCommand)->private.change.type = type;
480
481         errorType = create_location(luid, NULL, &pLocation);
482         if (errorType != SA_INTERNAL_OK)
483                 goto error;
484
485         set_item_source(temp, pLocation);
486         if (contenttype != NULL)
487                 temp->contenttype = strdup(contenttype);
488         (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
489
490         FW_LOGV("end");
491         return errorType;
492
493 error:
494         FW_LOGE("errorType = %d", errorType);
495         return errorType;
496
497 }
498
499 SA_ErrorType create_map_command(Session *session, Location *source, Location *target, Command **pCommand)
500 {
501         FW_LOGV("start");
502
503         SA_ErrorType errorType = SA_INTERNAL_OK;
504
505         if (!source) {
506                 errorType = SA_INTERNAL_NOT_DEFINED;
507                 goto error;
508         }
509
510         if (!target) {
511                 errorType = SA_INTERNAL_NOT_DEFINED;
512                 goto error;
513         }
514
515         *pCommand = __create_command(session, COMMAND_TYPE_MAP);
516         if (*pCommand == NULL) {
517                 errorType = SA_INTERNAL_NO_MEMORY;
518                 goto error;
519         }
520
521         (*pCommand)->source = source;
522         (*pCommand)->target = target;
523
524         FW_LOGV("end");
525
526         return errorType;
527 error:
528         FW_LOGE("errorType = %d", errorType);
529         return errorType;
530 }
531
532 SA_ErrorType set_map_command_item(Command *mapCommand, Item *temp)
533 {
534         FW_LOGV("start");
535
536         SA_ErrorType errorType = SA_INTERNAL_OK;
537
538         if (!mapCommand) {
539                 errorType = SA_INTERNAL_NOT_DEFINED;
540                 goto error;
541         }
542
543         mapCommand->private.map.items = g_list_append(mapCommand->private.map.items, temp);
544
545         FW_LOGV("end");
546
547         return errorType;
548 error:
549         FW_LOGE("errorType = %d", errorType);
550         return errorType;
551 }
552
553 SA_ErrorType increase_command_refcount(Command *pCommand)
554 {
555         FW_LOGV("start");
556         SA_ErrorType errorType = SA_INTERNAL_OK;
557
558         if (!pCommand) {
559                 errorType = SA_INTERNAL_NOT_DEFINED;
560                 goto error;
561         }
562
563         pCommand->refCount++;
564
565         FW_LOGV("end");
566
567         return errorType;
568 error:
569         FW_LOGE("errorType = %d", errorType);
570         return errorType;
571 }
572
573 SA_ErrorType decrease_command_refcount(Command *pCommand)
574 {
575         FW_LOGV("start");
576         SA_ErrorType errorType = SA_INTERNAL_OK;
577
578         if (!pCommand) {
579                 errorType = SA_INTERNAL_NOT_DEFINED;
580                 goto error;
581         }
582
583         pCommand->refCount--;
584
585         FW_LOGV("end");
586
587         return errorType;
588 error:
589         FW_LOGE("errorType = %d", errorType);
590         return errorType;
591 }
592
593 SA_ErrorType set_results_command_msgref(Command *pCommand, unsigned int msgRef)
594 {
595         FW_LOGV("start");
596         SA_ErrorType errorType = SA_INTERNAL_OK;
597
598         if (!pCommand) {
599                 errorType = SA_INTERNAL_NOT_DEFINED;
600                 goto error;
601         }
602
603         pCommand->private.results.msgRef = msgRef;
604
605         FW_LOGV("end");
606
607         return errorType;
608 error:
609         FW_LOGE("errorType = %d", errorType);
610         return errorType;
611 }
612
613 SA_ErrorType set_results_command_cmdref(Command *pCommand, unsigned int cmdRef)
614 {
615         FW_LOGV("start");
616         SA_ErrorType errorType = SA_INTERNAL_OK;
617
618         if (!pCommand) {
619                 errorType = SA_INTERNAL_NOT_DEFINED;
620                 goto error;
621         }
622
623         pCommand->private.results.cmdRef = cmdRef;
624
625         FW_LOGV("end");
626
627         return errorType;
628 error:
629         FW_LOGE("errorType = %d", errorType);
630         return errorType;
631 }
632
633 SA_ErrorType set_results_command_targetref(Command *pCommand, Location *pLocation)
634 {
635         SA_ErrorType errorType = SA_INTERNAL_OK;
636
637         if (!pCommand) {
638                 errorType = SA_INTERNAL_NOT_DEFINED;
639                 goto error;
640         }
641
642         FW_LOGV("start with Command Type =%d\n", pCommand->type);
643
644         pCommand->private.results.targetRef = dup_location(pLocation);
645
646         FW_LOGV("end");
647
648         return errorType;
649 error:
650         FW_LOGE("errorType = %d", errorType);
651         return errorType;
652 }
653
654 void free_command(Command *pCommand)
655 {
656         if (!pCommand)
657                 return;
658
659         FW_LOGV("start with  Command type is %d", pCommand->type);
660
661         GList *iter = NULL;
662
663         if (pCommand->refCount > 1) {
664                 FW_LOGV("Command's refCount is %d", pCommand->refCount);
665                 decrease_command_refcount(pCommand);
666                 return;
667         }
668
669
670         switch (pCommand->type) {
671                 case COMMAND_TYPE_ALERT:
672                         if (pCommand->private.alert.anchor) {
673                                 free_anchor(pCommand->private.alert.anchor);
674                                 pCommand->private.alert.anchor = NULL;
675                         }
676
677                         if (pCommand->private.alert.contentType) {
678                                 free(pCommand->private.alert.contentType);
679                                 pCommand->private.alert.contentType = NULL;
680                         }
681                         break;
682                 case COMMAND_TYPE_SYNC_START:
683                         /*nothing to free*/
684                         break;
685                 case COMMAND_TYPE_SYNC_END:
686                         /*nothing to free*/
687                         break;
688                 case COMMAND_TYPE_PUT:
689                         if (pCommand->private.access.type) {
690                                 free(pCommand->private.access.type);
691                                 pCommand->private.access.type = NULL;
692                         }
693
694                         if (pCommand->private.access.item) {
695                                 free_item(pCommand->private.access.item);
696                                 pCommand->private.access.item = NULL;
697                         }
698                         break;
699                 case COMMAND_TYPE_HEADER:
700                         /*COMMAND_TYPE_HEADER doesnot come here*/
701                         break;
702                 case COMMAND_TYPE_ADD:
703                 case COMMAND_TYPE_REPLACE:
704                 case COMMAND_TYPE_DELETE:
705                         for (iter = pCommand->private.change.items; iter != NULL; iter = g_list_next(iter))
706                                 free_item(iter->data);
707                         break;
708                 case COMMAND_TYPE_MAP:
709                         for (iter = pCommand->private.map.items; iter != NULL; iter = g_list_next(iter))
710                                 free_item(iter->data);
711                         break;
712                 case COMMAND_TYPE_GET:
713                         if (pCommand->private.access.type) {
714                                 free(pCommand->private.access.type);
715                                 pCommand->private.access.type = NULL;
716                         }
717
718                         if (pCommand->private.access.item) {
719                                 free_item(pCommand->private.access.item);
720                                 pCommand->private.access.item = NULL;
721                         }
722                         break;
723                 case COMMAND_TYPE_RESULTS:
724                         if (pCommand->private.results.type) {
725                                 free(pCommand->private.results.type);
726                                 pCommand->private.results.type = NULL;
727                         }
728
729                         if (pCommand->private.results.item) {
730                                 free_item(pCommand->private.results.item);
731                                 pCommand->private.results.item = NULL;
732                         }
733
734                         if (pCommand->private.results.targetRef) {
735                                 free_location(pCommand->private.results.targetRef);
736                                 pCommand->private.results.targetRef = NULL;
737                         }
738
739                         break;
740                 case COMMAND_TYPE_UNKNOWN:
741                         break;
742         }
743
744         if (pCommand->source) {
745                 free_location(pCommand->source);
746                 pCommand->source = NULL;
747         }
748
749         if (pCommand->target) {
750                 free_location(pCommand->target);
751                 pCommand->target = NULL;
752         }
753
754         if (pCommand->cred) {
755                 free_cred(pCommand->cred);
756                 pCommand->cred = NULL;
757         }
758
759         free(pCommand);
760
761         FW_LOGV("end");
762 }
763
764 SA_ErrorType create_new_status_location(Session *session, OMA_StatusType data, Command *command,
765                                                                                 Location *sourceref, Location *targetref, CommandType type, Status **pStatus)
766 {
767         FW_LOGV("start Errortype %d", data);
768
769         SA_ErrorType errorType = SA_INTERNAL_OK;
770
771         errorType = create_status(data, session->cmdID, session->lastRecievedMsgID,
772                                                                                         command->cmdID, sourceref, targetref, type, pStatus);
773         if (errorType != SA_INTERNAL_OK)
774                 goto error;
775
776         session->cmdID++;
777
778         FW_LOGV("end");
779
780         return errorType;
781 error:
782         FW_LOGE("error : %d", errorType);
783         return errorType;
784 }
785
786 SA_ErrorType create_new_status(Session *session, OMA_StatusType data, Command *command,
787                                                                                                         CommandType type, Status **pStatus)
788 {
789         FW_LOGV("start Errortype %d", data);
790
791         SA_ErrorType errorType = SA_INTERNAL_OK;
792
793         errorType = create_status(data, session->cmdID, session->lastRecievedMsgID, command->cmdID,
794                                                                                         command->source, command->target, type, pStatus);
795         if (errorType != SA_INTERNAL_OK)
796                 goto error;
797
798         session->cmdID++;
799
800         FW_LOGV("end");
801
802         return errorType;
803 error:
804         FW_LOGE("error : %d", errorType);
805         return errorType;
806 }
807
808 SA_ErrorType create_status(OMA_StatusType data,  unsigned int cmdID, unsigned int msgref , unsigned int cmdref,
809                                                                                         Location *sourceref, Location *targetref, CommandType type, Status **pStatus)
810 {
811         FW_LOGV("start Errortype %d", data);
812
813         SA_ErrorType errorType = SA_INTERNAL_OK;
814
815         *pStatus = (Status *)calloc(1, sizeof(Status));
816         if (*pStatus == NULL) {
817                 errorType = SA_INTERNAL_NO_MEMORY;
818                 goto error;
819         }
820
821         (*pStatus)->cmdID = cmdID;
822         (*pStatus)->msgRef = msgref;
823         (*pStatus)->cmdRef = cmdref;
824         (*pStatus)->type = type;
825
826         if (data)
827                 (*pStatus)->data = g_strdup_printf("%i", data);
828
829         if (sourceref)
830                 (*pStatus)->sourceRef = dup_location(sourceref);
831
832         if (targetref)
833                 (*pStatus)->targetRef = dup_location(targetref);
834
835         FW_LOGV("end");
836
837         return errorType;
838
839 error:
840         FW_LOGE("error : %d", errorType);
841         return errorType;
842 }
843
844 void free_statuses(GList *status)
845 {
846         FW_LOGV("start");
847
848         if (!status) {
849                 FW_LOGV("List is null");
850                 return;
851         }
852
853         GList *iter = NULL;
854         FW_LOGV("count : %d", g_list_length(status));
855         for (iter = status; iter != NULL; iter = g_list_next(iter))
856                 free_status(iter->data);
857
858         g_list_free(status);
859
860         FW_LOGV("end");
861 }
862
863 void free_status(Status *pStatus)
864 {
865         FW_LOGV("start");
866
867         if (!pStatus)
868                 return;
869
870         if (pStatus->data)
871                 free(pStatus->data);
872
873         if (pStatus->sourceRef)
874                 free_location(pStatus->sourceRef);
875
876         if (pStatus->targetRef)
877                 free_location(pStatus->targetRef);
878
879         if (pStatus->cred)
880                 free_cred(pStatus->cred);
881
882         if (pStatus->chal)
883                 free_chal(pStatus->chal);
884
885         free_item(pStatus->item);
886
887         free(pStatus);
888         pStatus = NULL;
889
890         FW_LOGV("end");
891 }
892
893 OMA_StatusType get_status_code(Status *status)
894 {
895         return atoi(status->data);
896 }
897
898 CommandType convert_command_type(char *type)
899 {
900         CommandType commandType = COMMAND_TYPE_UNKNOWN;
901         if (!type)
902                 return commandType;
903
904         if (!strcmp(type, "Alert")) {
905                 commandType = COMMAND_TYPE_ALERT;
906         } else if (!strcmp(type, "Sync")) {
907                 commandType = COMMAND_TYPE_SYNC_START;
908         } else if (!strcmp(type, "Put")) {
909                 commandType = COMMAND_TYPE_PUT;
910         } else if (!strcmp(type, "SyncHdr")) {
911                 commandType = COMMAND_TYPE_HEADER;
912         } else if (!strcmp(type, "Add")) {
913                 commandType = COMMAND_TYPE_ADD;
914         } else if (!strcmp(type, "Replace")) {
915                 commandType = COMMAND_TYPE_REPLACE;
916         } else if (!strcmp(type, "Map")) {
917                 commandType = COMMAND_TYPE_MAP;
918         } else if (!strcmp(type, "Delete")) {
919                 commandType = COMMAND_TYPE_DELETE;
920         } else if (!strcmp(type, "Results")) {
921                 commandType = COMMAND_TYPE_RESULTS;
922         } else if (!strcmp(type, "Get")) {
923                 commandType = COMMAND_TYPE_GET;
924         }
925
926         return commandType;
927 }
928
929 ChangeType convert_change_type_command_type(CommandType type)
930 {
931         ChangeType changeType = CHANGE_UNKNOWN;
932         switch (type) {
933         case COMMAND_TYPE_UNKNOWN:
934         case COMMAND_TYPE_ALERT:
935         case COMMAND_TYPE_SYNC_START:
936         case COMMAND_TYPE_SYNC_END:
937         case COMMAND_TYPE_PUT:
938         case COMMAND_TYPE_HEADER:
939         case COMMAND_TYPE_MAP:
940         case COMMAND_TYPE_GET:
941         case COMMAND_TYPE_RESULTS:
942                 /*never comes these commands*/
943                 break;
944         case COMMAND_TYPE_ADD:
945                 changeType = CHANGE_ADD;
946                 break;
947         case COMMAND_TYPE_REPLACE:
948                 changeType = CHANGE_REPLACE;
949                 break;
950         case COMMAND_TYPE_DELETE:
951                 changeType = CHANGE_DELETE;
952                 break;
953
954         }
955         return changeType;
956 }
957
958 CommandType convert_command_type_change_type(ChangeType type)
959 {
960         CommandType commandType = COMMAND_TYPE_UNKNOWN;
961
962         switch (type) {
963         case CHANGE_UNKNOWN:
964                 commandType = COMMAND_TYPE_UNKNOWN;
965                 break;
966         case CHANGE_ADD:
967                 commandType = COMMAND_TYPE_ADD;
968                 break;
969         case CHANGE_REPLACE:
970                 commandType = COMMAND_TYPE_REPLACE;
971                 break;
972         case CHANGE_DELETE:
973                 commandType = COMMAND_TYPE_DELETE;
974                 break;
975         }
976
977         return commandType;
978
979 }
980 /*
981 ChangeType convertToChangeType(unsigned int type) {
982
983         ChangeType changeType=CHANGE_UNKNOWN;
984         switch (type) {
985         case 1:
986                 changeType = CHANGE_ADD;
987                 break;
988         case 2:
989                 changeType = CHANGE_REPLACE;
990                 break;
991         case 3:
992                 changeType = CHANGE_DELETE;
993                 break;
994         }
995
996         return changeType;
997 }
998
999
1000 char *convertFromCommandType(CommandType type) {
1001         char *commandType=NULL;
1002
1003         switch (type) {
1004         case COMMAND_TYPE_ALERT:
1005                 commandType = "Alert";
1006                 break;
1007         case COMMAND_TYPE_SYNC_START:
1008         case COMMAND_TYPE_SYNC_END:
1009                 commandType = "Sync";
1010                 break;
1011         case COMMAND_TYPE_PUT:
1012                 commandType = "Put";
1013                 break;
1014         case COMMAND_TYPE_HEADER:
1015                 commandType = "SyncHdr";
1016                 break;
1017         case COMMAND_TYPE_ADD:
1018                 commandType = "Add";
1019                 break;
1020         case COMMAND_TYPE_REPLACE:
1021                 commandType = "Replace";
1022                 break;
1023         case COMMAND_TYPE_MAP:
1024                 commandType = "Map";
1025                 break;
1026         case COMMAND_TYPE_DELETE:
1027                 commandType = "Delete";
1028                 break;
1029         case COMMAND_TYPE_RESULTS:
1030                 commandType = "Results";
1031                 break;
1032         case COMMAND_TYPE_GET:
1033                 commandType = "Get";
1034                 break;
1035         default:
1036                 commandType="UNKNOWN";
1037         }
1038         return commandType;
1039 }
1040 */