upload tizen1.0 source
[pkgs/o/oma-ds-service.git] / ServiceAdapter / SA_Elements.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_Elements.c
38  *   @version                                                                   0.1
39  *   @brief                                                                             This file is the source file of implementation of functions for structures which is used in Service Adapter
40  */
41
42 #include "agent-framework/Utility/fw_log.h"
43 #include "ServiceAdapter/SA_Elements.h"
44 #include "ServiceAdapter/SA_Elements_Internal.h"
45 #include "ServiceAdapter/SA_Session_Internal.h"
46 #include "ServiceAdapter/SA_Command.h"
47
48 #define LOG_TAG "OMA_DS_SA"
49
50 SA_ErrorType create_anchor(char *last, char *next, Anchor **pAnchor)
51 {
52         FW_LOGV("start");
53
54         SA_ErrorType errorType = SA_INTERNAL_OK;
55
56         if (!next) {
57                 errorType = SA_INTERNAL_NOT_DEFINED;
58                 goto error;
59         }
60
61         *pAnchor = (Anchor *)calloc(1, sizeof(Anchor));
62         if (*pAnchor == NULL) {
63                 errorType = SA_INTERNAL_NO_MEMORY;
64                 goto error;
65         }
66
67         if (last)
68                 (*pAnchor)->lastAnchor = strdup(last);
69
70         (*pAnchor)->nextAnchor = strdup(next);
71
72         FW_LOGV("end");
73
74         return errorType;
75 error:
76         FW_LOGE("error :%d", errorType);
77         return errorType;
78 }
79
80 SA_ErrorType set_item_anchor(Item *item, Anchor *anchor)
81 {
82         FW_LOGV("start");
83
84         SA_ErrorType errorType = SA_INTERNAL_OK;
85
86         if (!item) {
87                 errorType = SA_INTERNAL_NOT_DEFINED;
88                 goto error;
89         }
90
91         if (!anchor) {
92                 errorType = SA_INTERNAL_NOT_DEFINED;
93                 goto error;
94         }
95
96         if (item->anchor) {
97                 free_anchor(item->anchor);
98                 item->anchor = NULL;
99         }
100
101         Anchor *pAnchor = NULL;
102         errorType = create_anchor(anchor->lastAnchor, anchor->nextAnchor, &pAnchor);
103         if (errorType != SA_INTERNAL_OK)
104                 goto error;
105         item->anchor = pAnchor;
106
107         FW_LOGV("end");
108
109         return errorType;
110 error:
111         FW_LOGE("error :%d", errorType);
112         return errorType;
113 }
114
115 SA_ErrorType set_last_anchor(Anchor *anchor, char *lastAnchor)
116 {
117         FW_LOGV("start with lastAnchor = %s\n", lastAnchor);
118
119         SA_ErrorType errorType = SA_INTERNAL_OK;
120
121         if (!anchor) {
122                 errorType = SA_INTERNAL_NOT_DEFINED;
123                 goto error;
124         }
125
126         if (lastAnchor) {
127                 errorType = SA_INTERNAL_NOT_DEFINED;
128                 goto error;
129         }
130
131         anchor->lastAnchor = lastAnchor;
132
133         FW_LOGV("end");
134         return errorType;
135
136 error:
137         FW_LOGE("error :%d", errorType);
138         return errorType;
139 }
140
141 SA_ErrorType set_next_anchor(Anchor *anchor, char *nextAnchor)
142 {
143         FW_LOGV("start with nextAnchor = %s\n", nextAnchor);
144
145         SA_ErrorType errorType = SA_INTERNAL_OK;
146
147         if (!anchor) {
148                 errorType = SA_INTERNAL_NOT_DEFINED;
149                 goto error;
150         }
151
152         if (nextAnchor) {
153                 errorType = SA_INTERNAL_NOT_DEFINED;
154                 goto error;
155         }
156
157         anchor->nextAnchor = nextAnchor;
158
159         FW_LOGV("end");
160         return errorType;
161
162 error:
163         FW_LOGE("error :%d", errorType);
164         return errorType;
165 }
166
167 void free_anchor(Anchor *pAnchor)
168 {
169         FW_LOGV("start");
170
171         if (!pAnchor)
172                 return;
173
174         if (pAnchor->lastAnchor) {
175                 free(pAnchor->lastAnchor);
176                 pAnchor->lastAnchor = NULL;
177         }
178
179         if (pAnchor->nextAnchor) {
180                 free(pAnchor->nextAnchor);
181                 pAnchor->nextAnchor = NULL;
182         }
183
184         free(pAnchor);
185         pAnchor = NULL;
186
187         FW_LOGV("end");
188
189         return;
190 }
191
192 SA_ErrorType create_location(char *locURI, char *locName, Location **pLocation)
193 {
194         FW_LOGV("start with locURI = %s, locName = %s\n", locURI, locName);
195
196         SA_ErrorType errorType = SA_INTERNAL_OK;
197
198         if (!locURI) {
199                 errorType = SA_INTERNAL_NOT_DEFINED;
200                 goto error;
201         }
202
203         *pLocation = (Location *) calloc(1, sizeof(Location));
204         /*              calloc된 *pLocation에 대한 null check로 변경
205          *              2011.09.28 seokgil kang
206          */
207         if (!(*pLocation)) {
208                 errorType = SA_INTERNAL_NO_MEMORY;
209                 goto error;
210         }
211
212         (*pLocation)->locURI = strdup(locURI);
213
214         if (locName)
215                 (*pLocation)->locName = strdup(locName);
216
217         FW_LOGV("end");
218         return errorType;
219
220 error:
221         FW_LOGE("error :%d", errorType);
222         return errorType;
223 }
224
225 Location *dup_location(Location* pLocation)
226 {
227         FW_LOGV("start");
228
229         SA_ErrorType errorType = SA_INTERNAL_OK;
230
231         if (!pLocation) {
232                 errorType = SA_INTERNAL_NOT_DEFINED;
233                 goto error;
234         }
235
236         Location *location = NULL;
237         errorType = create_location(pLocation->locURI, pLocation->locName, &location);
238         if (errorType != SA_INTERNAL_OK)
239                 goto error;
240
241         FW_LOGV("end");
242
243         return location;
244
245 error:
246         FW_LOGE("error :%d", errorType);
247         return NULL;
248
249 }
250
251 char  *get_location_locname(Location *location)
252 {
253         if (!location)
254                 return NULL;
255
256         return location->locName;
257 }
258
259 char *get_location_locuri(Location *location)
260 {
261         if (!location)
262                 return NULL;
263
264         return location->locURI;
265 }
266
267 void free_location(Location *loc)
268 {
269         FW_LOGV("start");
270
271         if (!loc)
272                 return;
273
274         FW_LOGV("loc->locURI = %s", loc->locURI);
275         if (loc->locURI)
276                 free(loc->locURI);
277
278         FW_LOGV("loc->locName = %s", loc->locName);
279         if (loc->locName)
280                 free(loc->locName);
281
282         free(loc);
283         loc = NULL;
284
285         FW_LOGV("end");
286
287         return;
288 }
289
290 SA_ErrorType create_cred(char *userName, char *pwd, AuthType authType, FormatType formatType , char *data, Cred **cred)
291 {
292         FW_LOGV("start");
293
294         SA_ErrorType errorType = SA_INTERNAL_OK;
295
296         if (userName ==  NULL || !strlen(userName)) {
297                 errorType = SA_INTERNAL_NOT_DEFINED;
298                 goto error;
299         }
300         if (pwd ==  NULL || !strlen(pwd)) {
301                 errorType = SA_INTERNAL_NOT_DEFINED;
302                 goto error;
303         }
304         if (data == NULL) {
305                 errorType = SA_INTERNAL_NOT_DEFINED;
306                 goto error;
307         }
308
309         *cred = (Cred *)calloc(1, sizeof(Cred));
310         if (*cred == NULL) {
311                 errorType = SA_INTERNAL_NO_MEMORY;
312                 goto error;
313         }
314
315         (*cred)->type = authType;
316         (*cred)->format = formatType;
317         (*cred)->username = strdup(userName);
318         (*cred)->password = strdup(pwd);
319
320         (*cred)->data = strdup(data);
321
322         FW_LOGV("end");
323
324         return errorType;
325 error:
326         FW_LOGE("error :%d", errorType);
327         return errorType;
328
329 }
330
331 void free_cred(Cred *cred)
332 {
333         FW_LOGV("start");
334
335         if (!cred)
336                 return;
337
338         if (cred->data) {
339                 free(cred->data);
340                 cred->data = NULL;
341         }
342
343         if (cred->username) {
344                 free(cred->username);
345                 cred->username = NULL;
346         }
347
348         if (cred->password) {
349                 free(cred->password);
350                 cred->password = NULL;
351         }
352
353         free(cred);
354         cred = NULL;
355
356         FW_LOGV("end");
357
358         return;
359 }
360
361 void free_chal(Chal *chal)
362 {
363         FW_LOGV("start");
364
365         if (!chal)
366                 return;
367
368         if (chal->nonce_plain) {
369                 free(chal->nonce_plain);
370                 chal->nonce_plain = NULL;
371         }
372
373         if (chal->nonce_b64) {
374                 free(chal->nonce_b64);
375                 chal->nonce_b64 = NULL;
376         }
377
378         free(chal);
379
380         FW_LOGV("end");
381
382         return;
383 }
384
385 Cred *create_cred_with_data(AuthType authType, char *data)
386 {
387         FW_LOGV("start");
388
389         SA_ErrorType errorType = SA_INTERNAL_OK;
390
391         Cred *cred = (Cred *)calloc(1, sizeof(Cred));
392         if (!cred) {
393                 errorType = SA_INTERNAL_NO_MEMORY;
394                 goto error;
395         }
396
397         cred->type = authType;
398         if (data != NULL)
399                 cred->data = strdup(data);
400
401         FW_LOGV("end");
402
403         return cred;
404 error:
405         FW_LOGE("error :%d", errorType);
406         return NULL;
407
408 }
409
410 void set_cred_format_type(Cred *cred, FormatType formatType)
411 {
412         if (!cred)
413                 return;
414
415         cred->format = formatType;
416 }
417
418 SA_ErrorType create_syncml(SyncHdr *syncHdr, GList *status, GList *commands, int isFinal , SyncML **pSyncML)
419 {
420         FW_LOGV("start");
421
422         SA_ErrorType errorType = SA_INTERNAL_OK;
423
424         if (!syncHdr) {
425                 errorType = SA_INTERNAL_NOT_DEFINED;
426                 goto error;
427         }
428
429         *pSyncML = (SyncML *) calloc(1, sizeof(SyncML));
430
431         if (*pSyncML == NULL) {
432                 errorType = SA_INTERNAL_NO_MEMORY;
433                 goto error;
434         }
435
436         (*pSyncML)->hdr = syncHdr ;
437         (*pSyncML)->status = status;
438         (*pSyncML)->commands = commands;
439         (*pSyncML)->final = isFinal;
440
441         FW_LOGV("end");
442
443         return errorType;
444 error:
445         FW_LOGE("error :%d", errorType);
446         return errorType;
447
448 }
449
450 void free_syncml(SyncML *syncML)
451 {
452         FW_LOGV("start");
453
454         if (!syncML)
455                 return;
456
457         free_synchdr(syncML->hdr);
458         syncML->hdr = NULL;
459
460         free_statuses(syncML->status);
461         syncML->status = NULL;
462
463         free_commands(syncML->commands);
464         syncML->commands = NULL;
465
466         free(syncML);
467
468         FW_LOGV("end");
469
470         return;
471 }
472
473 SA_ErrorType create_synchdr(Session *session , SyncHdr **pSyncHdr)
474 {
475         FW_LOGV("start");
476
477         SA_ErrorType errorType = SA_INTERNAL_OK;
478
479         if (!session->protocolVersion) {
480                 errorType = SA_INTERNAL_NOT_DEFINED;
481                 goto error;
482         }
483
484         if (!session->protocolType) {
485                 errorType = SA_INTERNAL_NOT_DEFINED;
486                 goto error;
487         }
488
489         if (!session->source) {
490                 errorType = SA_INTERNAL_NOT_DEFINED;
491                 goto error;
492         }
493
494         if (!session->target) {
495                 errorType = SA_INTERNAL_NOT_DEFINED;
496                 goto error;
497         }
498
499         *pSyncHdr = (SyncHdr *)calloc(1, sizeof(SyncHdr));
500
501         if (*pSyncHdr == NULL) {
502                 errorType = SA_INTERNAL_NO_MEMORY;
503                 goto error;
504         }
505
506         (*pSyncHdr)->version =  session->protocolVersion;
507         (*pSyncHdr)->protocol = session->protocolType;
508         (*pSyncHdr)->target = dup_location(session->target);
509         (*pSyncHdr)->source = dup_location(session->source);
510
511         if (session->cred)
512                 (*pSyncHdr)->cred  = dup_cred(session->cred);
513
514         if (session->sessionID)
515                 (*pSyncHdr)->sessionID = strdup(session->sessionID);            /*free*/
516
517         (*pSyncHdr)->messageID = ++session->msgID;
518
519         (*pSyncHdr)->maxmsgsize = session->sourceMaxMsgSize;
520         (*pSyncHdr)->maxobjsize = session->sourceMaxObjSize;
521
522         FW_LOGV("end");
523
524         return errorType;
525
526 error:
527         FW_LOGE("error :%d", errorType);
528         return errorType;
529 }
530
531 void free_synchdr(SyncHdr *syncHdr)
532 {
533         FW_LOGV("start");
534
535         if (!syncHdr)
536                 return;
537
538         if (syncHdr->sessionID) {
539                 free(syncHdr->sessionID);
540                 syncHdr->sessionID = NULL;
541         }
542
543         if (syncHdr->responseURI) {
544                 free(syncHdr->responseURI);
545                 syncHdr->responseURI = NULL;
546         }
547
548         if (syncHdr->source) {
549                 free_location(syncHdr->source);
550                 syncHdr->source = NULL;
551         }
552
553         if (syncHdr->target) {
554                 free_location(syncHdr->target);
555                 syncHdr->target = NULL;
556         }
557
558         if (syncHdr->cred) {
559                 free_cred(syncHdr->cred);
560                 syncHdr->cred = NULL;
561         }
562
563         free(syncHdr);
564
565         FW_LOGV("end");
566
567         return;
568 }
569
570 Item *create_item()
571 {
572         FW_LOGV("start");
573
574         SA_ErrorType errorType = SA_INTERNAL_OK;
575
576         Item *item = (Item *)calloc(1, sizeof(Item));
577         if (!item) {
578                 errorType = SA_INTERNAL_NO_MEMORY;
579                 goto error;
580         }
581
582         item->dataType = ITEM_UNKNOWN;
583         FW_LOGV("end");
584
585         return item;
586
587 error:
588         FW_LOGE("error :%d", errorType);
589         return NULL;
590 }
591
592 Item *create_item_for_data(const char *data, unsigned int size)
593 {
594         FW_LOGV("start");
595
596         SA_ErrorType errorType = SA_INTERNAL_OK;
597
598         Item *item = create_item();
599         if (!item) {
600                 errorType = SA_INTERNAL_NO_MEMORY;
601                 goto error;
602         }
603
604         item->dataType = ITEM_DATA;
605         if (data != NULL)
606                 item->private.data = strdup(data);
607
608         item->size = size;
609
610         FW_LOGV("end");
611
612         return item;
613 error:
614         FW_LOGE("error :%d", errorType);
615         return NULL;
616 }
617
618 Item *create_item_for_devinf(DevInf *devInf)
619 {
620         FW_LOGV("start");
621
622         SA_ErrorType errorType = SA_INTERNAL_OK;
623
624         if (!devInf) {
625                 errorType = SA_INTERNAL_NOT_DEFINED;
626                 goto error;
627         }
628
629         Item *item = create_item();
630         if (!item) {
631                 errorType = SA_INTERNAL_NO_MEMORY;
632                 goto error;
633         }
634
635         item->dataType = ITEM_DEVINF;
636         item->private.devInf = devInf;
637
638         FW_LOGV("end");
639
640         return item;
641 error:
642         FW_LOGE("error :%d", errorType);
643         return NULL;
644 }
645
646 void set_item_target(Item *item, Location *target)
647 {
648         if (item)
649                 item->target = target;
650 }
651
652 void set_item_source(Item *item, Location *source)
653 {
654         if (item)
655                 item->source = source;
656 }
657
658 void free_item(Item *item)
659 {
660         FW_LOGV("start");
661
662         if (!item)
663                 return;
664
665         if (item->source) {
666                 free_location(item->source);
667                 item->source = NULL;
668         }
669
670         if (item->target) {
671                 free_location(item->target);
672                 item->target = NULL;
673         }
674
675         if (item->anchor) {
676                 free_anchor(item->anchor);
677                 item->anchor = NULL;
678         }
679
680         switch (item->dataType) {
681         case ITEM_DATA:
682                 free(item->private.data);
683                 break;
684         case ITEM_DEVINF:
685                 /*devinf is pointed from session. so doesnot need to free here*/
686                 item->private.devInf = NULL;
687                 break;
688         case ITEM_UNKNOWN:
689                 /*noting to free*/
690                 break;
691         }
692
693         if (item->contenttype) {
694                 free(item->contenttype);
695                 item->contenttype = NULL;
696         }
697
698         free(item);
699         item = NULL;
700
701         FW_LOGV("end");
702
703         return;
704 }
705
706 Chal *dup_chal(Chal *pChal)
707 {
708         FW_LOGV("start");
709
710         if (!pChal) {
711                 FW_LOGV("pChal is null");
712                 return NULL;
713         }
714
715         Chal *temp = (Chal *)calloc(1, sizeof(Chal));
716
717         if (pChal->type)
718                 temp->type = pChal->type;
719
720         if (pChal->format)
721                 temp->format = pChal->format;
722
723         if (pChal->nonce_b64)
724                 temp->nonce_b64 = strdup(pChal->nonce_b64);
725
726         if (pChal->nonce_length)
727                 temp->nonce_length = pChal->nonce_length;
728
729         if (pChal->nonce_plain)
730                 temp->nonce_plain = strdup(pChal->nonce_plain);
731
732         FW_LOGV("end");
733
734         return temp;
735 }
736
737 Cred *dup_cred(Cred *pCred)
738 {
739         FW_LOGV("start");
740
741         if (!pCred) {
742                 FW_LOGV("pCred is null");
743                 return NULL;
744         }
745
746         Cred *temp = (Cred *)calloc(1, sizeof(Cred));
747
748         if (pCred->type)
749                 temp->type = pCred->type;
750
751         if (pCred->format)
752                 temp->format = pCred->format;
753
754         if (pCred->username)
755                 temp->username = strdup(pCred->username);
756
757         if (pCred->password)
758                 temp->password = strdup(pCred->password);
759
760         if (pCred->data)
761                 temp->data = strdup(pCred->data);
762
763         FW_LOGV("end");
764
765         return temp;
766 }
767
768 SA_ErrorType compare_cred(Cred *hdrCred, Cred *sessionCred)
769 {
770         FW_LOGV("start");
771
772         SA_ErrorType errorType = SA_INTERNAL_OK;
773
774         if (!hdrCred) {
775                 errorType = SA_INTERNAL_NOT_DEFINED;
776                 goto error;
777         }
778
779         if (!sessionCred) {
780                 errorType = SA_INTERNAL_NOT_DEFINED;
781                 goto error;
782         }
783
784         if (strcmp(hdrCred->data, sessionCred->data) == 0)
785                 errorType = SA_INTERNAL_OK;
786         else
787                 errorType = SA_INTERNAL_AUTHENTICATION_ERROR;
788
789         return errorType;
790 error:
791         FW_LOGE("error :%d", errorType);
792         return errorType;
793 }
794
795 AuthType convert_auth_type(char *authType)
796 {
797         if (!authType)
798                 return AUTH_TYPE_UNKNOWN;
799
800         if (!strcmp(authType, ELEMENT_AUTH_BASIC)) {
801                 return AUTH_TYPE_BASIC;
802         } else if (!strcmp(authType, ELEMENT_AUTH_MD5)) {
803                 return AUTH_TYPE_MD5;
804         }
805
806         return AUTH_TYPE_UNKNOWN;
807 }
808
809 FormatType convert_format_type(char *formatType)
810 {
811         if (!formatType)
812                 return FORMAT_TYPE_UNKNOWN;
813
814         if (!strcmp(formatType, ELEMENT_FORMAT_BASE64)) {
815                 return FORMAT_TYPE_BASE64;
816         }
817
818         return FORMAT_TYPE_UNKNOWN;
819 }