/* * oma-ds-agent * Copyright (c) 2012 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @SA_Elements.c * @version 0.1 * @brief This file is the source file of implementation of functions for structures which is used in Service Adapter */ #include #include "service-adapter/sa_elements.h" #include "service-adapter/sa_elements_internal.h" #include "service-adapter/sa_session_internal.h" #include "service-adapter/sa_command.h" #ifndef OMADS_AGENT_LOG #undef LOG_TAG #define LOG_TAG "OMA_DS_SA" #endif sa_error_type_e create_anchor(char *last, char *next, anchor_s ** anchor) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; if (next == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } *anchor = (anchor_s *) calloc(1, sizeof(anchor_s)); if (*anchor == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } if (last != NULL) (*anchor)->last_anchor = strdup(last); (*anchor)->next_anchor = strdup(next); error: _EXTERN_FUNC_EXIT; return errorType; } sa_error_type_e set_item_anchor(item_s * item, anchor_s * anchor) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; if (item == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (anchor == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (item->anchor != NULL) { free_anchor(item->anchor); item->anchor = NULL; } anchor_s *temp_anchor = NULL; errorType = create_anchor(anchor->last_anchor, anchor->next_anchor, &temp_anchor); if (errorType != SA_INTERNAL_OK) goto error; item->anchor = temp_anchor; error: _EXTERN_FUNC_EXIT; return errorType; } sa_error_type_e set_last_anchor(anchor_s * anchor, char *last_anchor) { _EXTERN_FUNC_ENTER; _DEBUG_INFO("lastAnchor = %s\n", last_anchor); sa_error_type_e errorType = SA_INTERNAL_OK; if (anchor == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (last_anchor == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } anchor->last_anchor = last_anchor; error: _EXTERN_FUNC_EXIT; return errorType; } sa_error_type_e set_next_anchor(anchor_s * anchor, char *next_anchor) { _EXTERN_FUNC_ENTER; _DEBUG_INFO("nextAnchor = %s\n", next_anchor); sa_error_type_e errorType = SA_INTERNAL_OK; if (anchor == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (next_anchor == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } anchor->next_anchor = next_anchor; error: _EXTERN_FUNC_EXIT; return errorType; } void free_anchor(anchor_s * anchor) { _EXTERN_FUNC_ENTER; retm_if(anchor == NULL, "anchor is NULL"); if (anchor->last_anchor != NULL) { free(anchor->last_anchor); anchor->last_anchor = NULL; } if (anchor->next_anchor != NULL) { free(anchor->next_anchor); anchor->next_anchor = NULL; } free(anchor); anchor = NULL; _EXTERN_FUNC_EXIT; return; } sa_error_type_e create_location(char *loc_uri, char *loc_name, location_s ** location) { _EXTERN_FUNC_ENTER; _DEBUG_INFO("locURI = %s, locName = %s\n", loc_uri, loc_name); sa_error_type_e errorType = SA_INTERNAL_OK; if (loc_uri == NULL) { _DEBUG_ERROR("locURI is NULL"); errorType = SA_INTERNAL_NOT_DEFINED; goto error; } *location = (location_s *) calloc(1, sizeof(location_s)); if (*location == NULL) { _DEBUG_ERROR("location is NULL"); errorType = SA_INTERNAL_NO_MEMORY; goto error; } (*location)->loc_uri = strdup(loc_uri); if (loc_name != NULL) (*location)->loc_name = strdup(loc_name); error: _EXTERN_FUNC_EXIT; return errorType; } location_s *dup_location(location_s * location) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; location_s *temp_location = NULL; if (location == NULL) { _DEBUG_ERROR("location is NULL"); errorType = SA_INTERNAL_NOT_DEFINED; goto error; } errorType = create_location(location->loc_uri, location->loc_name, &temp_location); if (errorType != SA_INTERNAL_OK) { _DEBUG_ERROR("create_location is failed"); goto error; } _EXTERN_FUNC_EXIT; return temp_location; error: _EXTERN_FUNC_EXIT; return NULL; } char *get_location_loc_name(location_s * location) { _EXTERN_FUNC_ENTER; retvm_if(location == NULL, NULL, "location is NULL"); _EXTERN_FUNC_EXIT; return location->loc_name; } char *get_location_loc_uri(location_s * location) { _EXTERN_FUNC_ENTER; retvm_if(location == NULL, NULL, "location is NULL"); _EXTERN_FUNC_EXIT; return location->loc_uri; } void free_location(location_s * loc) { _EXTERN_FUNC_ENTER; retm_if(loc == NULL, "loc is NULL"); _DEBUG_INFO("loc->locURI = %s", loc->loc_uri); if (loc->loc_uri != NULL) free(loc->loc_uri); _DEBUG_INFO("loc->locName = %s", loc->loc_name); if (loc->loc_name != NULL) free(loc->loc_name); free(loc); loc = NULL; _EXTERN_FUNC_EXIT; return; } 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) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; if (user_name == NULL || !strlen(user_name)) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (pwd == NULL || !strlen(pwd)) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (data == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } *cred = (cred_s *) calloc(1, sizeof(cred_s)); if (*cred == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } (*cred)->type = auth_type; (*cred)->format = format_type; (*cred)->user_name = strdup(user_name); (*cred)->password = strdup(pwd); (*cred)->data = strdup(data); error: _EXTERN_FUNC_EXIT; return errorType; } void free_cred(cred_s * cred) { _EXTERN_FUNC_ENTER; retm_if(cred == NULL, "cred is NULL"); if (cred->data != NULL) { free(cred->data); cred->data = NULL; } if (cred->user_name != NULL) { free(cred->user_name); cred->user_name = NULL; } if (cred->password != NULL) { free(cred->password); cred->password = NULL; } free(cred); cred = NULL; _EXTERN_FUNC_EXIT; return; } void free_chal(chal_s * chal) { _EXTERN_FUNC_ENTER; retm_if(chal == NULL, "chal is NULL"); if (chal->nonce_plain != NULL) { free(chal->nonce_plain); chal->nonce_plain = NULL; } if (chal->nonce_b64 != NULL) { free(chal->nonce_b64); chal->nonce_b64 = NULL; } free(chal); _EXTERN_FUNC_EXIT; return; } cred_s *create_cred_with_data(auth_type_e auth_type, char *data) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; cred_s *cred = (cred_s *) calloc(1, sizeof(cred_s)); if (cred == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } cred->type = auth_type; if (data != NULL) cred->data = strdup(data); error: _EXTERN_FUNC_EXIT; return cred; } void set_cred_format_type(cred_s * cred, format_type_e format_type) { _EXTERN_FUNC_ENTER; retm_if(cred == NULL, "cred is NULL"); cred->format = format_type; _EXTERN_FUNC_EXIT; } sa_error_type_e create_syncml(sync_hdr_s * sync_hdr, GList * status, GList * commands, int is_final, syncml_s ** syncml) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; if (sync_hdr == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } *syncml = (syncml_s *) calloc(1, sizeof(syncml_s)); if (*syncml == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } (*syncml)->hdr = sync_hdr; (*syncml)->status = status; (*syncml)->commands = commands; (*syncml)->final = is_final; error: _EXTERN_FUNC_EXIT; return errorType; } void free_syncml(syncml_s * syncml) { _EXTERN_FUNC_ENTER; retm_if(syncml == NULL, "syncML is NULL"); free_sync_hdr(syncml->hdr); syncml->hdr = NULL; free_statuses(syncml->status); syncml->status = NULL; free_commands(syncml->commands); syncml->commands = NULL; free(syncml); _EXTERN_FUNC_EXIT; return; } sa_error_type_e create_sync_hdr(session_s * session, sync_hdr_s ** sync_hdr) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; if (!session->protocol_version) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (!session->protocol_type) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (session->source == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (session->target == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } *sync_hdr = (sync_hdr_s *) calloc(1, sizeof(sync_hdr_s)); if (*sync_hdr == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } (*sync_hdr)->version = session->protocol_version; (*sync_hdr)->protocol = session->protocol_type; (*sync_hdr)->target = dup_location(session->target); (*sync_hdr)->source = dup_location(session->source); if (session->cred != NULL) (*sync_hdr)->cred = dup_cred(session->cred); if (session->session_id != NULL) (*sync_hdr)->session_id = strdup(session->session_id); /*free */ (*sync_hdr)->message_id = ++session->msg_id; (*sync_hdr)->max_msg_size = session->source_max_msg_size; (*sync_hdr)->max_obj_size = session->source_max_obj_size; error: _EXTERN_FUNC_EXIT; return errorType; } void free_sync_hdr(sync_hdr_s * sync_hdr) { _EXTERN_FUNC_ENTER; retm_if(sync_hdr == NULL, "sync_hdr is NULL"); if (sync_hdr->session_id != NULL) { free(sync_hdr->session_id); sync_hdr->session_id = NULL; } if (sync_hdr->response_uri != NULL) { free(sync_hdr->response_uri); sync_hdr->response_uri = NULL; } if (sync_hdr->source != NULL) { free_location(sync_hdr->source); sync_hdr->source = NULL; } if (sync_hdr->target != NULL) { free_location(sync_hdr->target); sync_hdr->target = NULL; } if (sync_hdr->cred != NULL) { free_cred(sync_hdr->cred); sync_hdr->cred = NULL; } free(sync_hdr); _EXTERN_FUNC_EXIT; return; } item_s *create_item() { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; item_s *item = (item_s *) calloc(1, sizeof(item_s)); if (item == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } item->data_type = ITEM_UNKNOWN; _EXTERN_FUNC_EXIT; return item; error: _EXTERN_FUNC_EXIT; return NULL; } item_s *create_item_for_data(const char *data, unsigned int size) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; item_s *item = create_item(); if (item == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } item->data_type = ITEM_DATA; if (data != NULL) item->private.data = strdup(data); item->size = size; _EXTERN_FUNC_EXIT; return item; error: _EXTERN_FUNC_EXIT; return NULL; } item_s *create_item_for_devinf(devinf_s * devinf) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; if (devinf == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } item_s *item = create_item(); if (item == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } item->data_type = ITEM_DEVINF; item->private.devinf = devinf; _EXTERN_FUNC_EXIT; return item; error: _EXTERN_FUNC_EXIT; return NULL; } void set_item_target(item_s * item, location_s * target) { _EXTERN_FUNC_ENTER; if (item != NULL) item->target = target; _EXTERN_FUNC_EXIT; } void set_item_source(item_s * item, location_s * source) { _EXTERN_FUNC_ENTER; if (item != NULL) item->source = source; _EXTERN_FUNC_EXIT; } void free_item(item_s * item) { _EXTERN_FUNC_ENTER; retm_if(item == NULL, "item is NULL"); if (item->source != NULL) { free_location(item->source); item->source = NULL; } if (item->target != NULL) { free_location(item->target); item->target = NULL; } if (item->anchor != NULL) { free_anchor(item->anchor); item->anchor = NULL; } switch (item->data_type) { case ITEM_DATA: free(item->private.data); break; case ITEM_DEVINF: /*devinf is pointed from session. so doesnot need to free here */ item->private.devinf = NULL; break; case ITEM_UNKNOWN: /*noting to free */ break; } if (item->content_type != NULL) { free(item->content_type); item->content_type = NULL; } free(item); item = NULL; _EXTERN_FUNC_EXIT; return; } chal_s *dup_chal(chal_s * chal) { _EXTERN_FUNC_ENTER; retvm_if(chal == NULL, NULL, "chal is NULL"); chal_s *temp = (chal_s *) calloc(1, sizeof(chal_s)); retvm_if(temp == NULL, NULL, "temp is NULL"); if (chal->nonce_b64 != NULL) temp->nonce_b64 = strdup(chal->nonce_b64); if (chal->nonce_plain != NULL) temp->nonce_plain = strdup(chal->nonce_plain); temp->type = chal->type; temp->format = chal->format; temp->nonce_length = chal->nonce_length; _EXTERN_FUNC_EXIT; return temp; } cred_s *dup_cred(cred_s * cred) { _EXTERN_FUNC_ENTER; retvm_if(cred == NULL, NULL, "cred is NULL"); cred_s *temp = (cred_s *) calloc(1, sizeof(cred_s)); retvm_if(temp == NULL, NULL, "temp is NULL"); temp->type = cred->type; temp->format = cred->format; if (cred->user_name != NULL) temp->user_name = strdup(cred->user_name); if (cred->password != NULL) temp->password = strdup(cred->password); if (cred->data != NULL) temp->data = strdup(cred->data); _EXTERN_FUNC_EXIT; return temp; } sa_error_type_e compare_cred(cred_s * hdr_cred, cred_s * session_cred) { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; if (hdr_cred == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (session_cred == NULL) { errorType = SA_INTERNAL_NOT_DEFINED; goto error; } if (strcmp(hdr_cred->data, session_cred->data) == 0) errorType = SA_INTERNAL_OK; else errorType = SA_INTERNAL_AUTHENTICATION_ERROR; error: _EXTERN_FUNC_EXIT; return errorType; } auth_type_e convert_auth_type(char *auth_type) { _EXTERN_FUNC_ENTER; retvm_if(auth_type == NULL, AUTH_TYPE_UNKNOWN, "authType is NULL"); if (!strcmp(auth_type, ELEMENT_AUTH_BASIC)) { return AUTH_TYPE_BASIC; } else if (!strcmp(auth_type, ELEMENT_AUTH_MD5)) { return AUTH_TYPE_MD5; } _EXTERN_FUNC_EXIT; return AUTH_TYPE_UNKNOWN; } format_type_e convert_format_type(char *format_type) { _EXTERN_FUNC_ENTER; if (format_type == NULL) return FORMAT_TYPE_UNKNOWN; if (!strcmp(format_type, ELEMENT_FORMAT_BASE64)) { return FORMAT_TYPE_BASE64; } _EXTERN_FUNC_EXIT; return FORMAT_TYPE_UNKNOWN; } mem_s *create_mem() { _EXTERN_FUNC_ENTER; sa_error_type_e errorType = SA_INTERNAL_OK; mem_s *mem = (mem_s *) calloc(1, sizeof(mem_s)); if (mem == NULL) { errorType = SA_INTERNAL_NO_MEMORY; goto error; } _EXTERN_FUNC_EXIT; return mem; error: _EXTERN_FUNC_EXIT; return NULL; } void set_mem_shared_mem(mem_s * mem, int shared_mem) { _EXTERN_FUNC_ENTER; retm_if(mem == NULL, "mem is NULL"); mem->sharedmem = shared_mem; _EXTERN_FUNC_EXIT; } void set_mem_free_mem(mem_s * mem, unsigned int free_mem) { _EXTERN_FUNC_ENTER; retm_if(mem == NULL, "mem is NULL"); mem->free_mem = free_mem; _EXTERN_FUNC_EXIT; } void set_mem_free_id(mem_s * mem, unsigned int free_id) { _EXTERN_FUNC_ENTER; retm_if(mem == NULL, "mem is NULL"); mem->free_id = free_id; _EXTERN_FUNC_EXIT; }