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