3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * @brief This file is the source file of implementation of functions for structures which is used in Service Adapter
24 #include <sync_agent.h>
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"
31 #ifndef OMADS_AGENT_LOG
33 #define LOG_TAG "OMA_DS_SA"
36 sa_error_type_e create_anchor(char *last, char *next, anchor_s ** anchor)
40 sa_error_type_e errorType = SA_INTERNAL_OK;
43 errorType = SA_INTERNAL_NOT_DEFINED;
47 *anchor = (anchor_s *) calloc(1, sizeof(anchor_s));
48 if (*anchor == NULL) {
49 errorType = SA_INTERNAL_NO_MEMORY;
54 (*anchor)->last_anchor = strdup(last);
56 (*anchor)->next_anchor = strdup(next);
64 sa_error_type_e set_item_anchor(item_s * item, anchor_s * anchor)
68 sa_error_type_e errorType = SA_INTERNAL_OK;
71 errorType = SA_INTERNAL_NOT_DEFINED;
76 errorType = SA_INTERNAL_NOT_DEFINED;
80 if (item->anchor != NULL) {
81 free_anchor(item->anchor);
85 anchor_s *temp_anchor = NULL;
86 errorType = create_anchor(anchor->last_anchor, anchor->next_anchor, &temp_anchor);
87 if (errorType != SA_INTERNAL_OK)
89 item->anchor = temp_anchor;
97 sa_error_type_e set_last_anchor(anchor_s * anchor, char *last_anchor)
100 _DEBUG_INFO("lastAnchor = %s\n", last_anchor);
102 sa_error_type_e errorType = SA_INTERNAL_OK;
104 if (anchor == NULL) {
105 errorType = SA_INTERNAL_NOT_DEFINED;
109 if (last_anchor == NULL) {
110 errorType = SA_INTERNAL_NOT_DEFINED;
114 anchor->last_anchor = last_anchor;
122 sa_error_type_e set_next_anchor(anchor_s * anchor, char *next_anchor)
125 _DEBUG_INFO("nextAnchor = %s\n", next_anchor);
127 sa_error_type_e errorType = SA_INTERNAL_OK;
129 if (anchor == NULL) {
130 errorType = SA_INTERNAL_NOT_DEFINED;
134 if (next_anchor == NULL) {
135 errorType = SA_INTERNAL_NOT_DEFINED;
139 anchor->next_anchor = next_anchor;
147 void free_anchor(anchor_s * anchor)
151 retm_if(anchor == NULL, "anchor is NULL");
153 if (anchor->last_anchor != NULL) {
154 free(anchor->last_anchor);
155 anchor->last_anchor = NULL;
158 if (anchor->next_anchor != NULL) {
159 free(anchor->next_anchor);
160 anchor->next_anchor = NULL;
170 sa_error_type_e create_location(char *loc_uri, char *loc_name, location_s ** location)
173 _DEBUG_INFO("locURI = %s, locName = %s\n", loc_uri, loc_name);
175 sa_error_type_e errorType = SA_INTERNAL_OK;
177 if (loc_uri == NULL) {
178 _DEBUG_ERROR("locURI is NULL");
179 errorType = SA_INTERNAL_NOT_DEFINED;
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;
190 (*location)->loc_uri = strdup(loc_uri);
192 if (loc_name != NULL)
193 (*location)->loc_name = strdup(loc_name);
201 location_s *dup_location(location_s * location)
205 sa_error_type_e errorType = SA_INTERNAL_OK;
206 location_s *temp_location = NULL;
208 if (location == NULL) {
209 _DEBUG_ERROR("location is NULL");
210 errorType = SA_INTERNAL_NOT_DEFINED;
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");
221 return temp_location;
230 char *get_location_loc_name(location_s * location)
234 retvm_if(location == NULL, NULL, "location is NULL");
237 return location->loc_name;
240 char *get_location_loc_uri(location_s * location)
243 retvm_if(location == NULL, NULL, "location is NULL");
246 return location->loc_uri;
249 void free_location(location_s * loc)
253 retm_if(loc == NULL, "loc is NULL");
255 _DEBUG_INFO("loc->locURI = %s", loc->loc_uri);
256 if (loc->loc_uri != NULL)
259 _DEBUG_INFO("loc->locName = %s", loc->loc_name);
260 if (loc->loc_name != NULL)
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)
274 sa_error_type_e errorType = SA_INTERNAL_OK;
276 if (user_name == NULL || !strlen(user_name)) {
277 errorType = SA_INTERNAL_NOT_DEFINED;
280 if (pwd == NULL || !strlen(pwd)) {
281 errorType = SA_INTERNAL_NOT_DEFINED;
285 errorType = SA_INTERNAL_NOT_DEFINED;
289 *cred = (cred_s *) calloc(1, sizeof(cred_s));
291 errorType = SA_INTERNAL_NO_MEMORY;
295 (*cred)->type = auth_type;
296 (*cred)->format = format_type;
297 (*cred)->user_name = strdup(user_name);
298 (*cred)->password = strdup(pwd);
300 (*cred)->data = strdup(data);
309 void free_cred(cred_s * cred)
313 retm_if(cred == NULL, "cred is NULL");
315 if (cred->data != NULL) {
320 if (cred->user_name != NULL) {
321 free(cred->user_name);
322 cred->user_name = NULL;
325 if (cred->password != NULL) {
326 free(cred->password);
327 cred->password = NULL;
337 void free_chal(chal_s * chal)
341 retm_if(chal == NULL, "chal is NULL");
343 if (chal->nonce_plain != NULL) {
344 free(chal->nonce_plain);
345 chal->nonce_plain = NULL;
348 if (chal->nonce_b64 != NULL) {
349 free(chal->nonce_b64);
350 chal->nonce_b64 = NULL;
359 cred_s *create_cred_with_data(auth_type_e auth_type, char *data)
363 sa_error_type_e errorType = SA_INTERNAL_OK;
365 cred_s *cred = (cred_s *) calloc(1, sizeof(cred_s));
367 errorType = SA_INTERNAL_NO_MEMORY;
371 cred->type = auth_type;
373 cred->data = strdup(data);
382 void set_cred_format_type(cred_s * cred, format_type_e format_type)
386 retm_if(cred == NULL, "cred is NULL");
388 cred->format = format_type;
393 sa_error_type_e create_syncml(sync_hdr_s * sync_hdr, GList * status, GList * commands, int is_final, syncml_s ** syncml)
397 sa_error_type_e errorType = SA_INTERNAL_OK;
399 if (sync_hdr == NULL) {
400 errorType = SA_INTERNAL_NOT_DEFINED;
404 *syncml = (syncml_s *) calloc(1, sizeof(syncml_s));
405 if (*syncml == NULL) {
406 errorType = SA_INTERNAL_NO_MEMORY;
410 (*syncml)->hdr = sync_hdr;
411 (*syncml)->status = status;
412 (*syncml)->commands = commands;
413 (*syncml)->final = is_final;
422 void free_syncml(syncml_s * syncml)
426 retm_if(syncml == NULL, "syncML is NULL");
428 free_sync_hdr(syncml->hdr);
431 free_statuses(syncml->status);
432 syncml->status = NULL;
434 free_commands(syncml->commands);
435 syncml->commands = NULL;
444 sa_error_type_e create_sync_hdr(session_s * session, sync_hdr_s ** sync_hdr)
448 sa_error_type_e errorType = SA_INTERNAL_OK;
450 if (!session->protocol_version) {
451 errorType = SA_INTERNAL_NOT_DEFINED;
455 if (!session->protocol_type) {
456 errorType = SA_INTERNAL_NOT_DEFINED;
460 if (session->source == NULL) {
461 errorType = SA_INTERNAL_NOT_DEFINED;
465 if (session->target == NULL) {
466 errorType = SA_INTERNAL_NOT_DEFINED;
470 *sync_hdr = (sync_hdr_s *) calloc(1, sizeof(sync_hdr_s));
471 if (*sync_hdr == NULL) {
472 errorType = SA_INTERNAL_NO_MEMORY;
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);
481 if (session->cred != NULL)
482 (*sync_hdr)->cred = dup_cred(session->cred);
484 if (session->session_id != NULL)
485 (*sync_hdr)->session_id = strdup(session->session_id); /*free */
487 (*sync_hdr)->message_id = ++session->msg_id;
489 (*sync_hdr)->max_msg_size = session->source_max_msg_size;
490 (*sync_hdr)->max_obj_size = session->source_max_obj_size;
498 void free_sync_hdr(sync_hdr_s * sync_hdr)
502 retm_if(sync_hdr == NULL, "sync_hdr is NULL");
504 if (sync_hdr->session_id != NULL) {
505 free(sync_hdr->session_id);
506 sync_hdr->session_id = NULL;
509 if (sync_hdr->response_uri != NULL) {
510 free(sync_hdr->response_uri);
511 sync_hdr->response_uri = NULL;
514 if (sync_hdr->source != NULL) {
515 free_location(sync_hdr->source);
516 sync_hdr->source = NULL;
519 if (sync_hdr->target != NULL) {
520 free_location(sync_hdr->target);
521 sync_hdr->target = NULL;
524 if (sync_hdr->cred != NULL) {
525 free_cred(sync_hdr->cred);
526 sync_hdr->cred = NULL;
535 item_s *create_item()
539 sa_error_type_e errorType = SA_INTERNAL_OK;
541 item_s *item = (item_s *) calloc(1, sizeof(item_s));
543 errorType = SA_INTERNAL_NO_MEMORY;
547 item->data_type = ITEM_UNKNOWN;
558 item_s *create_item_for_data(const char *data, unsigned int size)
562 sa_error_type_e errorType = SA_INTERNAL_OK;
564 item_s *item = create_item();
566 errorType = SA_INTERNAL_NO_MEMORY;
570 item->data_type = ITEM_DATA;
572 item->private.data = strdup(data);
585 item_s *create_item_for_devinf(devinf_s * devinf)
589 sa_error_type_e errorType = SA_INTERNAL_OK;
591 if (devinf == NULL) {
592 errorType = SA_INTERNAL_NOT_DEFINED;
596 item_s *item = create_item();
598 errorType = SA_INTERNAL_NO_MEMORY;
602 item->data_type = ITEM_DEVINF;
603 item->private.devinf = devinf;
614 void set_item_target(item_s * item, location_s * target)
618 item->target = target;
623 void set_item_source(item_s * item, location_s * source)
627 item->source = source;
632 void free_item(item_s * item)
636 retm_if(item == NULL, "item is NULL");
638 if (item->source != NULL) {
639 free_location(item->source);
643 if (item->target != NULL) {
644 free_location(item->target);
648 if (item->anchor != NULL) {
649 free_anchor(item->anchor);
653 switch (item->data_type) {
655 free(item->private.data);
658 /*devinf is pointed from session. so doesnot need to free here */
659 item->private.devinf = NULL;
666 if (item->content_type != NULL) {
667 free(item->content_type);
668 item->content_type = NULL;
678 chal_s *dup_chal(chal_s * chal)
682 retvm_if(chal == NULL, NULL, "chal is NULL");
684 chal_s *temp = (chal_s *) calloc(1, sizeof(chal_s));
685 retvm_if(temp == NULL, NULL, "temp is NULL");
687 if (chal->nonce_b64 != NULL)
688 temp->nonce_b64 = strdup(chal->nonce_b64);
690 if (chal->nonce_plain != NULL)
691 temp->nonce_plain = strdup(chal->nonce_plain);
693 temp->type = chal->type;
694 temp->format = chal->format;
695 temp->nonce_length = chal->nonce_length;
701 cred_s *dup_cred(cred_s * cred)
705 retvm_if(cred == NULL, NULL, "cred is NULL");
707 cred_s *temp = (cred_s *) calloc(1, sizeof(cred_s));
708 retvm_if(temp == NULL, NULL, "temp is NULL");
710 temp->type = cred->type;
711 temp->format = cred->format;
713 if (cred->user_name != NULL)
714 temp->user_name = strdup(cred->user_name);
716 if (cred->password != NULL)
717 temp->password = strdup(cred->password);
719 if (cred->data != NULL)
720 temp->data = strdup(cred->data);
726 sa_error_type_e compare_cred(cred_s * hdr_cred, cred_s * session_cred)
730 sa_error_type_e errorType = SA_INTERNAL_OK;
732 if (hdr_cred == NULL) {
733 errorType = SA_INTERNAL_NOT_DEFINED;
737 if (session_cred == NULL) {
738 errorType = SA_INTERNAL_NOT_DEFINED;
742 if (strcmp(hdr_cred->data, session_cred->data) == 0)
743 errorType = SA_INTERNAL_OK;
745 errorType = SA_INTERNAL_AUTHENTICATION_ERROR;
753 auth_type_e convert_auth_type(char *auth_type)
756 retvm_if(auth_type == NULL, AUTH_TYPE_UNKNOWN, "authType is NULL");
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;
765 return AUTH_TYPE_UNKNOWN;
768 format_type_e convert_format_type(char *format_type)
771 if (format_type == NULL)
772 return FORMAT_TYPE_UNKNOWN;
774 if (!strcmp(format_type, ELEMENT_FORMAT_BASE64)) {
775 return FORMAT_TYPE_BASE64;
779 return FORMAT_TYPE_UNKNOWN;
786 sa_error_type_e errorType = SA_INTERNAL_OK;
788 mem_s *mem = (mem_s *) calloc(1, sizeof(mem_s));
790 errorType = SA_INTERNAL_NO_MEMORY;
803 void set_mem_shared_mem(mem_s * mem, int shared_mem)
807 retm_if(mem == NULL, "mem is NULL");
809 mem->sharedmem = shared_mem;
814 void set_mem_free_mem(mem_s * mem, unsigned int free_mem)
818 retm_if(mem == NULL, "mem is NULL");
820 mem->free_mem = free_mem;
825 void set_mem_free_id(mem_s * mem, unsigned int free_id)
829 retm_if(mem == NULL, "mem is NULL");
831 mem->free_id = free_id;