diff options
Diffstat (limited to 'src/agent')
21 files changed, 23857 insertions, 0 deletions
diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt new file mode 100755 index 0000000..64fb834 --- /dev/null +++ b/src/agent/CMakeLists.txt @@ -0,0 +1,80 @@ + +############################################# +# +# Step 1. Set Variable and Build Dependency +# + +# set a name for the entire project +PROJECT(oma-ds) + +# set variables +SET(EXENAME "${PROJECT_NAME}-agent") +SET(BINDIR "${PREFIX}/bin") +SET(OMADS_SRC_DIR "${CMAKE_SOURCE_DIR}/src/agent") +SET(OMADS_COMPONETS "main;common;framework/san-parser;framework/event;framework/task;service-adapter;service-adapter/network-binder;service-adapter/protocol-binder;service-engine") + +# checks for build dependency modules : a pkg-config module for CMake +INCLUDE(FindPkgConfig) +pkg_check_modules(LPKGS REQUIRED + glib-2.0 + libsoup-2.4 + sqlite3 + sync-agent + vconf + libwbxml2 + dlog + aul + dbus-glib-1 + ) + +############################################# +# +# Step 2. Set Compile Environment +# + +# set extra cflags from build dependency +STRING(REPLACE ";" " " EXTRA_CFLAGS "${LPKGS_CFLAGS}") + +# find all source files in a directory. +FOREACH(OMADS_COMPONET ${OMADS_COMPONETS}) + AUX_SOURCE_DIRECTORY(${OMADS_SRC_DIR}/${OMADS_COMPONET} SRCS) +ENDFOREACH(OMADS_COMPONET) + +# add internal include directories to the build. +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/san-parser) + +# add define macros +ADD_DEFINITIONS("-DGLIB_VERSION_MIN_REQUIRED=(2<<16|32<<8) ") + +############################################# +# +# Step 3. Set Link Environment +# + +# add an executable to the project using the specified source files. +ADD_EXECUTABLE(${EXENAME} ${SRCS}) + +# link a target to given libraries. +TARGET_LINK_LIBRARIES(${EXENAME} ${LPKGS_LIBRARIES}) + +# sets additional compiler flags used to build sources within the target. +SET_TARGET_PROPERTIES(${EXENAME} PROPERTIES COMPILE_FLAGS ${EXTRA_CFLAGS}) + +############################################# +# +# Step 4. Install packages +# + +# install executable file +INSTALL(TARGETS ${EXENAME} DESTINATION ${BINDIR}) + +# install oma ds configuration files +INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/oma-ds-cfg DESTINATION /usr/share) + +# install oma ds dbus file +INSTALL(FILES ${CMAKE_SOURCE_DIR}/com.samsung.omadsagent.service DESTINATION /usr/share/dbus-1/services) + +# install booting script file +INSTALL(PROGRAMS ${CMAKE_SOURCE_DIR}/${PROJECT_NAME} DESTINATION /etc/init.d/) + diff --git a/src/agent/common/common_define.c b/src/agent/common/common_define.c new file mode 100755 index 0000000..3954bb4 --- /dev/null +++ b/src/agent/common/common_define.c @@ -0,0 +1,750 @@ +/* + * 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. + */ + +/** + * @Common_Define_Internal.c + * @version 0.1 + * @brief This file is the source file of implementation of defined Common structure + */ + +#include <sync_agent.h> + +#include "common/common_define.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_COMMON" +#endif + +datastore_s *datastoreinfo_per_content_type[4]; + +datastore_info_s *create_datastore_info(char *target, char *source) +{ + _EXTERN_FUNC_ENTER; + + datastore_info_s *datastore_info = (datastore_info_s *) calloc(1, sizeof(datastore_info_s)); + retvm_if(datastore_info == NULL, NULL, "datastore_info is NULL"); + + if (target != NULL) + datastore_info->target = strdup(target); + if (source != NULL) + datastore_info->source = strdup(source); + + _EXTERN_FUNC_EXIT; + + return datastore_info; +} + +void free_datastore_infos(GList * datastore_infos) +{ + _EXTERN_FUNC_ENTER; + retm_if(datastore_infos == NULL, "List is NULL"); + + GList *iter = NULL; + for (iter = datastore_infos; iter != NULL; iter = g_list_next(iter)) + free_datastore_info(iter->data); + + g_list_free(datastore_infos); + + _EXTERN_FUNC_EXIT; +} + +void free_datastore_info(datastore_info_s * datastore_info) +{ + _EXTERN_FUNC_ENTER; + retm_if(datastore_info == NULL, "pDatastoreInfo is NULL"); + + if (datastore_info->target != NULL) { + free(datastore_info->target); + datastore_info->target = NULL; + } + + if (datastore_info->source != NULL) { + free(datastore_info->source); + datastore_info->source = NULL; + } + + if (datastore_info->last_anchor != NULL) { + free(datastore_info->last_anchor); + datastore_info->last_anchor = NULL; + } + + if (datastore_info->next_anchor != NULL) { + free(datastore_info->next_anchor); + datastore_info->next_anchor = NULL; + } + + free(datastore_info); + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_info_sync_type(datastore_info_s * datastore_info, alert_type_e sync_type) +{ + _EXTERN_FUNC_ENTER; + if (datastore_info != NULL) + datastore_info->sync_type = sync_type; + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_info_last_anchor(datastore_info_s * datastore_info, char *last_anchor) +{ + _EXTERN_FUNC_ENTER; + if (datastore_info != NULL) { + if (last_anchor != NULL) + datastore_info->last_anchor = strdup(last_anchor); + } + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_info_next_anchor(datastore_info_s * datastore_info, char *next_anchor) +{ + _EXTERN_FUNC_ENTER; + if (datastore_info != NULL) { + if (next_anchor != NULL) + datastore_info->next_anchor = strdup(next_anchor); + } + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_info_max_obj_size(datastore_info_s * datastore_info, unsigned int max_obj_size) +{ + _EXTERN_FUNC_ENTER; + if (datastore_info != NULL) + datastore_info->max_obj_size = max_obj_size; + + _EXTERN_FUNC_EXIT; +} + +changed_item_s *create_changed_item(change_type_e type, char *luid) +{ + _EXTERN_FUNC_ENTER; + + changed_item_s *changed_item = (changed_item_s *) calloc(1, sizeof(changed_item_s)); + retvm_if(changed_item == NULL, NULL, "changed_item is NULL"); + + changed_item->change_type = type; + + if (luid != NULL) + changed_item->luid = strdup(luid); + + changed_item->content_type = NULL; + + _EXTERN_FUNC_EXIT; + + return changed_item; +} + +void set_changed_item_content_type(changed_item_s * changed_item, char *content_type) +{ + _EXTERN_FUNC_ENTER; + + retm_if(changed_item == NULL, "pChangedItem is NULL"); + + if (content_type != NULL) + changed_item->content_type = strdup(content_type); + + _EXTERN_FUNC_EXIT; +} + +void free_changed_item(changed_item_s * changed_item) +{ + _EXTERN_FUNC_ENTER; + + retm_if(changed_item == NULL, "pChangedItem is NULL"); + + if (changed_item->luid != NULL) { + free(changed_item->luid); + changed_item->luid = NULL; + } + + if (changed_item->content_type != NULL) { + free(changed_item->content_type); + changed_item->content_type = NULL; + } + + if (changed_item->data != NULL) { + free(changed_item->data); + changed_item->data = NULL; + } + + if (changed_item != NULL) + free(changed_item); + + _EXTERN_FUNC_EXIT; +} + +void set_changed_item_data(changed_item_s * changed_item, char *data) +{ + _EXTERN_FUNC_ENTER; + + retm_if(changed_item == NULL, "pChangedItem is NULL"); + + if (data != NULL) + changed_item->data = strdup(data); + + _EXTERN_FUNC_EXIT; +} + +void set_changed_item_index_of_datastore(changed_item_s * changed_item, unsigned int index_of_datastore) +{ + _EXTERN_FUNC_ENTER; + + retm_if(changed_item == NULL, "changed_item is NULL"); + + changed_item->index_of_datastore = index_of_datastore; + + _EXTERN_FUNC_EXIT; +} + +changed_datastore_s *create_changed_datastore(char *source, char *target, int has_number_of_changes, unsigned int number_of_changes) +{ + _EXTERN_FUNC_ENTER; + + changed_datastore_s *changed_datastore = (changed_datastore_s *) calloc(1, sizeof(changed_datastore_s)); + retvm_if(changed_datastore == NULL, NULL, "changed_datastore is NULL"); + + if (source != NULL) + changed_datastore->source = strdup(source); + if (target != NULL) + changed_datastore->target = strdup(target); + + changed_datastore->need_sync_command = 1; + changed_datastore->number_of_changes = number_of_changes; + changed_datastore->has_number_of_changes = has_number_of_changes; + + _EXTERN_FUNC_EXIT; + + return changed_datastore; +} + +void free_changed_datastores(GList * changed_datastores) +{ + _EXTERN_FUNC_ENTER; + + retm_if(changed_datastores == NULL, "List is NULL"); + + GList *iter = NULL; + for (iter = changed_datastores; iter != NULL; iter = g_list_next(iter)) + free_changed_datastore(iter->data); + + g_list_free(changed_datastores); + + _EXTERN_FUNC_EXIT; +} + +void free_changed_datastore(changed_datastore_s * changed_datastore) +{ + _EXTERN_FUNC_ENTER; + + retm_if(changed_datastore == NULL, "pChangedDatastore is NULL"); + + if (changed_datastore->source != NULL) { + free(changed_datastore->source); + changed_datastore->source = NULL; + } + + if (changed_datastore->target != NULL) { + free(changed_datastore->target); + changed_datastore->target = NULL; + } + + GList *iter = NULL; + for (iter = changed_datastore->change_item; iter != NULL; iter = g_list_next(iter)) + free_changed_item(iter->data); + + g_list_free(changed_datastore->change_item); + changed_datastore->change_item = NULL; + + for (iter = changed_datastore->sent_item; iter != NULL; iter = g_list_next(iter)) + free_changed_item(iter->data); + + g_list_free(changed_datastore->sent_item); + changed_datastore->sent_item = NULL; + + if (changed_datastore != NULL) + free(changed_datastore); + + _EXTERN_FUNC_EXIT; +} + +void set_changed_datastore_changed_item(changed_datastore_s * changed_datastore, GList * changed_items) +{ + _EXTERN_FUNC_ENTER; + + if (changed_datastore != NULL) + changed_datastore->change_item = changed_items; + + _EXTERN_FUNC_EXIT; +} + +void add_changed_datastore_changed_item(changed_datastore_s * changed_datastore, changed_item_s * changed_item) +{ + _EXTERN_FUNC_ENTER; + + retm_if(changed_datastore == NULL, "changed_datastore is NULL"); + + changed_datastore->change_item = g_list_append(changed_datastore->change_item, changed_item); + + _EXTERN_FUNC_EXIT; +} + +applied_status_s *create_applied_status(char *luid, change_type_e change_type, int status) +{ + _EXTERN_FUNC_ENTER; + + applied_status_s *applied_status = (applied_status_s *) calloc(1, sizeof(applied_status_s)); + retvm_if(applied_status == NULL, NULL, "appliedStatus is NULL"); + + if (luid != NULL) + applied_status->luid = strdup(luid); + + applied_status->change_type = change_type; + applied_status->status = status; + + _EXTERN_FUNC_EXIT; + + return applied_status; +} + +void free_applied_statuses(GList * applied_statuses) +{ + _EXTERN_FUNC_ENTER; + + retm_if(applied_statuses == NULL, "List is NULL"); + + GList *iter = NULL; + for (iter = applied_statuses; iter != NULL; iter = g_list_next(iter)) + free_applied_status(iter->data); + + g_list_free(applied_statuses); + + _EXTERN_FUNC_EXIT; +} + +void free_applied_status(applied_status_s * applied_status) +{ + _EXTERN_FUNC_ENTER; + + retm_if(applied_status == NULL, "List is NULL"); + + if (applied_status->luid != NULL) { + free(applied_status->luid); + applied_status->luid = NULL; + } + + free(applied_status); + + _EXTERN_FUNC_EXIT; +} + +sending_status_s *create_sending_status(char *source, char *target) +{ + _EXTERN_FUNC_ENTER; + + sending_status_s *sending_status = (sending_status_s *) calloc(1, sizeof(sending_status_s)); + retvm_if(sending_status == NULL, NULL, "sending_status is NULL"); + + if (source != NULL) + sending_status->source = strdup(source); + + if (target != NULL) + sending_status->target = strdup(target); + + _EXTERN_FUNC_EXIT; + + return sending_status; +} + +void free_sending_statuses(GList * sending_statuses) +{ + _EXTERN_FUNC_ENTER; + + retm_if(sending_statuses == NULL, "List is NULL"); + + GList *iter = NULL; + for (iter = sending_statuses; iter != NULL; iter = g_list_next(iter)) + free_sending_status(iter->data); + + g_list_free(sending_statuses); + + _EXTERN_FUNC_EXIT; +} + +void free_sending_status(sending_status_s * sending_status) +{ + _EXTERN_FUNC_ENTER; + + retm_if(sending_status == NULL, "pSendingStatus is NULL"); + + if (sending_status->source != NULL) { + free(sending_status->source); + sending_status->source = NULL; + } + + if (sending_status->target != NULL) { + free(sending_status->target); + sending_status->target = NULL; + } + + GList *iter = NULL; + for (iter = sending_status->items; iter != NULL; iter = g_list_next(iter)) + free_applied_status(iter->data); + + g_list_free(sending_status->items); + + free(sending_status); + + _EXTERN_FUNC_EXIT; +} + +void add_sending_status_applied_status(sending_status_s * sending_status, applied_status_s * applied_status) +{ + _EXTERN_FUNC_ENTER; + + retm_if(sending_status == NULL, "sending_status is NULL"); + + sending_status->items = g_list_append(sending_status->items, applied_status); + + _EXTERN_FUNC_EXIT; +} + +datastore_s *create_datastore(char *target, char *source) +{ + _EXTERN_FUNC_ENTER; + + datastore_s *datastore = (datastore_s *) calloc(1, sizeof(datastore_s)); + retvm_if(datastore == NULL, NULL, "datastore is NULL"); + + if (target != NULL) + datastore->target = strdup(target); + + if (source != NULL) + datastore->source = strdup(source); + + _EXTERN_FUNC_EXIT; + + return datastore; +} + +void set_datastore_content_type_info(datastore_s * datastore, int datastore_id, int folder_type_id) +{ + _EXTERN_FUNC_ENTER; + + if (datastore != NULL) { + datastore->datastore_id = datastore_id; + datastore->folder_type_id = folder_type_id; + } + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_account_info(datastore_s * datastore, char *account_id, char *account_pw) +{ + _EXTERN_FUNC_ENTER; + + if (datastore != NULL) { + if (account_id != NULL) + datastore->id = strdup(account_id); + if (account_pw != NULL) + datastore->pw = strdup(account_pw); + } + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_client_sync_type(datastore_s * datastore, alert_type_e sync_type) +{ + _EXTERN_FUNC_ENTER; + + if (datastore != NULL) + datastore->client_sync_type = sync_type; + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_server_sync_type(datastore_s * datastore, alert_type_e sync_type) +{ + _EXTERN_FUNC_ENTER; + + if (datastore != NULL) + datastore->server_sync_type = sync_type; + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_client_anchor(datastore_s * datastore, char *last_anchor, char *next_anchor) +{ + _EXTERN_FUNC_ENTER; + + if (datastore != NULL) { + if (last_anchor != NULL) + datastore->last_anchor_client = strdup(last_anchor); + if (next_anchor != NULL) + datastore->next_anchor_client = strdup(next_anchor); + } + + _EXTERN_FUNC_EXIT; +} + +void set_datastore_server_anchor(datastore_s * datastore, char *last_anchor, char *next_anchor) +{ + _EXTERN_FUNC_ENTER; + + if (datastore != NULL) { + if (last_anchor != NULL) + datastore->last_anchor_server = strdup(last_anchor); + if (next_anchor != NULL) + datastore->next_anchor_server = strdup(next_anchor); + } + + _EXTERN_FUNC_EXIT; +} + +sync_result_s *create_sync_result() +{ + _EXTERN_FUNC_ENTER; + + sync_result_s *syncResult = calloc(1, sizeof(sync_result_s)); + retvm_if(syncResult == NULL, NULL, "syncResult is NULL"); + + _EXTERN_FUNC_EXIT; + + return syncResult; +} + +sync_result_s *dup_sync_result(sync_result_s * org_sync_result) +{ + _EXTERN_FUNC_ENTER; + + retvm_if(org_sync_result == NULL, NULL, "orgSyncResult is NULL"); + + sync_result_s *syn_cre_sult = calloc(1, sizeof(sync_result_s)); + retvm_if(syn_cre_sult == NULL, NULL, "syn_cre_sult is NULL"); + + syn_cre_sult->session_result = org_sync_result->session_result; + syn_cre_sult->number_of_change = org_sync_result->number_of_change; + syn_cre_sult->received_count = org_sync_result->received_count; + syn_cre_sult->add_count = org_sync_result->add_count; + syn_cre_sult->replace_count = org_sync_result->replace_count; + syn_cre_sult->delete_count = org_sync_result->delete_count; + + _EXTERN_FUNC_EXIT; + + return syn_cre_sult; + +} + +void set_number_of_change(sync_result_s * sync_result, unsigned int number_of_change) +{ + _EXTERN_FUNC_ENTER; + + if (sync_result != NULL) + sync_result->number_of_change = number_of_change; + + _EXTERN_FUNC_EXIT; +} + +void add_add_count(sync_result_s * sync_result, unsigned int add_cnt) +{ + _EXTERN_FUNC_ENTER; + + if (sync_result != NULL) + sync_result->add_count += add_cnt; + + _EXTERN_FUNC_EXIT; +} + +void add_receive_count(sync_result_s * sync_result, unsigned int received_count) +{ + _EXTERN_FUNC_ENTER; + + if (sync_result != NULL) + sync_result->received_count += received_count; + + _EXTERN_FUNC_EXIT; +} + +void add_replace_count(sync_result_s * sync_result, unsigned int replace_cnt) +{ + _EXTERN_FUNC_ENTER; + + if (sync_result != NULL) + sync_result->replace_count += replace_cnt; + + _EXTERN_FUNC_EXIT; +} + +void add_delete_count(sync_result_s * sync_result, unsigned int delete_cnt) +{ + _EXTERN_FUNC_ENTER; + + if (sync_result != NULL) + sync_result->delete_count += delete_cnt; + + _EXTERN_FUNC_EXIT; +} + +void free_pre_sync_return_obj(pre_sync_return_obj_s * pre_sync_return_obj) +{ + _EXTERN_FUNC_ENTER; + + retm_if(pre_sync_return_obj == NULL, "pre_sync_return_obj is NULL"); + + GList *iter = NULL; + for (iter = pre_sync_return_obj->datastore_info; iter != NULL; iter = g_list_next(iter)) + free_datastore_info(iter->data); + g_list_free(pre_sync_return_obj->datastore_info); + pre_sync_return_obj->datastore_info = NULL; + + if (pre_sync_return_obj->dev_id != NULL) { + free(pre_sync_return_obj->dev_id); + pre_sync_return_obj->dev_id = NULL; + } + + free(pre_sync_return_obj); + pre_sync_return_obj = NULL; + + _EXTERN_FUNC_EXIT; +} + +void free_sync_obj(sync_obj_s * sync_obj) +{ + _EXTERN_FUNC_ENTER; + + retm_if(sync_obj == NULL, "sync_obj is NULL"); + + GList *iter = NULL; + for (iter = sync_obj->changed_datastore; iter != NULL; iter = g_list_next(iter)) + free_changed_datastore(iter->data); + g_list_free(sync_obj->changed_datastore); + sync_obj->changed_datastore = NULL; + + for (iter = sync_obj->sending_status; iter != NULL; iter = g_list_next(iter)) + free_sending_status(iter->data); + g_list_free(sync_obj->sending_status); + sync_obj->sending_status = NULL; + + free(sync_obj); + sync_obj = NULL; + + _EXTERN_FUNC_EXIT; +} + +void free_sync_return_obj(sync_return_obj_s * sync_return_obj) +{ + _EXTERN_FUNC_ENTER; + + retm_if(sync_return_obj == NULL, "pSyncReturnObj is NULL"); + + GList *iter = NULL; + for (iter = sync_return_obj->changed_datastore; iter != NULL; iter = g_list_next(iter)) + free_changed_datastore(iter->data); + g_list_free(sync_return_obj->changed_datastore); + sync_return_obj->changed_datastore = NULL; + + for (iter = sync_return_obj->status; iter != NULL; iter = g_list_next(iter)) + free_applied_status(iter->data); + g_list_free(sync_return_obj->status); + sync_return_obj->status = NULL; + + free(sync_return_obj); + sync_return_obj = NULL; + + _EXTERN_FUNC_EXIT; +} + +void free_datastore(datastore_s * datastore) +{ + _EXTERN_FUNC_ENTER; + + retm_if(datastore == NULL, "pDatastore is NULL"); + + if (datastore->target != NULL) { + free(datastore->target); + datastore->target = NULL; + } + + if (datastore->source != NULL) { + free(datastore->source); + datastore->source = NULL; + } + + if (datastore->id != NULL) { + free(datastore->id); + datastore->id = NULL; + } + + if (datastore->pw != NULL) { + free(datastore->pw); + datastore->pw = NULL; + } + + if (datastore->last_anchor_client != NULL) { + free(datastore->last_anchor_client); + datastore->last_anchor_client = NULL; + } + + if (datastore->next_anchor_client != NULL) { + free(datastore->next_anchor_client); + datastore->next_anchor_client = NULL; + } + + if (datastore->last_anchor_server != NULL) { + free(datastore->last_anchor_server); + datastore->last_anchor_server = NULL; + } + + if (datastore->next_anchor_server != NULL) { + free(datastore->next_anchor_server); + datastore->next_anchor_server = NULL; + } + + if (datastore->client_sync_result != NULL) + free(datastore->client_sync_result); + + if (datastore->server_sync_result != NULL) + free(datastore->server_sync_result); + + free(datastore); + + _EXTERN_FUNC_EXIT; +} + +void free_sync_service(sync_service_s * category) +{ + _EXTERN_FUNC_ENTER; + + retm_if(category == NULL, "category is NULL"); + + if (category->src_uri != NULL) + free(category->src_uri); + + if (category->tgt_uri != NULL) + free(category->tgt_uri); + + if (category->id != NULL) + free(category->id); + + if (category->password != NULL) + free(category->password); + + _EXTERN_FUNC_EXIT; +} diff --git a/src/agent/common/common_util.c b/src/agent/common/common_util.c new file mode 100755 index 0000000..7c1bb70 --- /dev/null +++ b/src/agent/common/common_util.c @@ -0,0 +1,281 @@ +/* + * 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. + */ + +/** + * @Common_Util.c + * @version 0.1 + * @brief This file is the source file of implementation of utility function + */ + +#include <sync_agent.h> + +#include "common/common_util.h" +#include "common/common_define_internal.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_COMMON" +#endif + +#define MAX_RETRY_COUNT 10 + +bool create_config_str(int account_id, char *key, char *value, char *type, char *access_name, sync_agent_da_config_s ** config) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + + da_err = sync_agent_create_config(config); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_create_config is failed"); + goto error; + } + + (*config)->config_id = account_id; + (*config)->key = strdup(key); + (*config)->value = value != NULL ? strcmp(value, "") == 0 ? NULL : strdup(value) : NULL; + (*config)->type = strdup(type); + (*config)->access_name = strdup(access_name); + + error: + + _EXTERN_FUNC_EXIT; + + if (da_err == SYNC_AGENT_DA_SUCCESS) + return true; + else + return false; + +} + +bool set_config_str(int accountID, char *key, char *value, char *type, char *access_name) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_da_return_e result; + sync_agent_da_config_s config; + + config.config_id = accountID; + config.key = key; + config.value = value != NULL ? strcmp(value, "") == 0 ? NULL : value : NULL; + config.type = type; + config.access_name = access_name; + + result = sync_agent_update_config(&config); + + _EXTERN_FUNC_EXIT; + + if (result == SYNC_AGENT_DA_SUCCESS) + return true; + else + return false; +} + +bool set_config_int(int accountID, char *key, int value, char *type, char *access_name) +{ + _EXTERN_FUNC_ENTER; + + char *value_str = NULL; + value_str = g_strdup_printf("%u", value); + + sync_agent_da_return_e result; + sync_agent_da_config_s config; + + config.config_id = accountID; + config.key = key; + config.value = value_str; + config.type = type; + config.access_name = access_name; + + result = sync_agent_update_config(&config); + + if (value_str != NULL) + free(value_str); + + _EXTERN_FUNC_EXIT; + + if (result == SYNC_AGENT_DA_SUCCESS) + return true; + else + return false; +} + +bool get_config(int account_id, char *key, char **value) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_da_return_e result = SYNC_AGENT_DA_SUCCESS; + sync_agent_da_config_s *config = NULL; + + *value = NULL; + + result = sync_agent_create_config(&config); + if (result != SYNC_AGENT_DA_SUCCESS) + return false; + + result = sync_agent_get_config(account_id, key, &config); + if (result == SYNC_AGENT_DA_SUCCESS) { + if (config != NULL) { + if (config->value != NULL) { + *value = strdup(config->value); + _DEBUG_INFO("value = %s", *value); + } + } + } else { + /* FIXME temporary solution + * Try MAX_RETRY_COUNT when fail to get_config + * */ + int i; + bool success = false; + for (i = 0; i < MAX_RETRY_COUNT; i++) { + result = sync_agent_get_config(account_id, key, &config); + if (result == SYNC_AGENT_DA_SUCCESS) { + if (config != NULL) { + if (config->value != NULL) { + *value = strdup(config->value); + _DEBUG_INFO("value = %s", *value); + success = true; + break; + } + } + } + } + if (success == false) { + sync_agent_free_config(config); + _EXTERN_FUNC_EXIT; + return false; + } + } + + sync_agent_free_config(config); + _EXTERN_FUNC_EXIT; + return true; +} + +int get_account_id(char *profile, bool open) +{ + _EXTERN_FUNC_ENTER; + + retvm_if(profile == NULL, -1, "profile is NULL"); + + int accountId = -1; + char *profileDirName = NULL; + bool result; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + GList *account_info_list = NULL; + GList *iter = NULL; + + if (open == false) { + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_agent"); + goto error; + } + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_NONE; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto error; + } + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + fw_account = (sync_agent_fw_account_s *) iter->data; + + if (profileDirName != NULL) + free(profileDirName); + + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + goto error; + } + + _DEBUG_INFO("profileDirName = %s", profileDirName); + if (profileDirName == NULL) + continue; + + if (strcmp(profile, profileDirName) == 0) { + _DEBUG_INFO("account_list[i] = %d", fw_account->account_id); + accountId = fw_account->account_id; + break; + } + } + + _EXTERN_FUNC_EXIT; + + error: + + sync_agent_free_fw_account_list(account_info_list); + + if (profileDirName != NULL) + free(profileDirName); + + if (open == false) + sync_agent_close_agent(); + + return accountId; +} + +/*int convert_synctype_value(char *syncType_str) +{ + int syncType_value; + + if (strcmp(syncType_str, DEFINE_ALERT_SLOW_SYNC_STR) ==0) + syncType_value = ALERT_SLOW_SYNC ; + else if (strcmp(syncType_str, DEFINE_ALERT_TWO_WAY_STR) ==0) + syncType_value = ALERT_TWO_WAY ; + else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) ==0) + syncType_value = ALERT_ONE_WAY_FROM_CLIENT; + else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) ==0) + syncType_value = ALERT_ONE_WAY_FROM_SERVER; + else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) ==0) + syncType_value = ALERT_REFRESH_FROM_SERVER; + else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) ==0) + syncType_value = ALERT_REFRESH_FROM_CLIENT; + else + syncType_value = ALERT_UNKNOWN; + + return syncType_value; +}*/ + +/*char *convert_synctype_str(char *syncType_value) +{ + char *syncType_str = NULL; + + if (strcmp(syncType_value, DEFINE_ALERT_SLOW_SYNC_VALUE) ==0) + syncType_str = DEFINE_ALERT_SLOW_SYNC_STR; + else if (strcmp(syncType_value, DEFINE_ALERT_TWO_WAY_VALUE) ==0) + syncType_str = DEFINE_ALERT_TWO_WAY_STR; + else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0) + syncType_str = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR; + else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0) + syncType_str = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR; + else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_SERVER_VALUE) ==0) + syncType_str = DEFINE_ALERT_REFRESH_FROM_SERVER_STR; + else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_CLIENT_VALUE) ==0) + syncType_str = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR; + else + syncType_str = DEFINE_ALERT_SLOW_SYNC_STR; + + return syncType_str; +}*/ diff --git a/src/agent/common/common_vconf.c b/src/agent/common/common_vconf.c new file mode 100755 index 0000000..e874d37 --- /dev/null +++ b/src/agent/common/common_vconf.c @@ -0,0 +1,191 @@ +/* + * 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. + */ + +/** + * @Common_Util.c + * @version 0.1 + * @brief This file is the source file of implementation of wrapping vconf function + */ + +#include <vconf.h> +#include <stdio.h> +#include <stdlib.h> +#include <glib.h> +#include <string.h> + +#include <sync_agent.h> + +#include "common/common_vconf.h" + +char *get_vconf_str(char *base_key, char *key) +{ + _EXTERN_FUNC_ENTER; + + char path[128]; + char *value = NULL; + + snprintf(path, sizeof(path), "%s%s", base_key, key); + value = vconf_get_str(path); + + if (value != NULL) { + if (strcmp(value, "") == 0) { + + if (value != NULL) + free(value); + + _EXTERN_FUNC_EXIT; + return NULL; + } else { + _EXTERN_FUNC_EXIT; + return value; + } + } else { + _EXTERN_FUNC_EXIT; + return NULL; + } + +} + +char *get_vconf_str_key(char *key) +{ + _EXTERN_FUNC_ENTER; + + char *value = NULL; + value = vconf_get_str(key); + + if (value != NULL) { + if (strcmp(value, "") == 0) { + + if (value != NULL) + free(value); + + _EXTERN_FUNC_EXIT; + return NULL; + } else { + _EXTERN_FUNC_EXIT; + return value; + } + } else { + _EXTERN_FUNC_EXIT; + return NULL; + } +} + +bool get_vconf_int(char *base_key, char *key, int *value) +{ + _EXTERN_FUNC_ENTER; + + char path[128]; + int temp = 0; + int result; + + snprintf(path, sizeof(path), "%s%s", base_key, key); + result = vconf_get_int(path, &temp); + + _EXTERN_FUNC_EXIT; + + if (result == 0) { + *value = temp; + return true; + } else + return false; +} + +bool get_vconf_int_key(char *key, int *value) +{ + _EXTERN_FUNC_ENTER; + + int temp = 0; + int result; + + result = vconf_get_int(key, &temp); + + _EXTERN_FUNC_EXIT; + + if (result == 0) { + *value = temp; + return true; + } else + return false; +} + +bool set_vconf_str(char *base_key, char *key, char *value) +{ + _EXTERN_FUNC_ENTER; + + char path[128]; + int result; + + snprintf(path, sizeof(path), "%s%s", base_key, key); + result = vconf_set_str(path, value); + + _EXTERN_FUNC_EXIT; + + if (result == 0) + return true; + else + return false; +} + +bool set_vconf_str_key(char *key, char *value) +{ + _EXTERN_FUNC_ENTER; + + int result; + result = vconf_set_str(key, value); + + _EXTERN_FUNC_EXIT; + + if (result == 0) + return true; + else + return false; +} + +bool set_vconf_int(char *base_key, char *key, int value) +{ + _EXTERN_FUNC_ENTER; + + char path[128]; + int result; + + snprintf(path, sizeof(path), "%s%s", base_key, key); + result = vconf_set_int(path, value); + + _EXTERN_FUNC_EXIT; + + if (result == 0) + return true; + else + return false; +} + +bool set_vconf_int_key(char *key, int value) +{ + _EXTERN_FUNC_ENTER; + + int result; + + result = vconf_set_int(key, value); + + _EXTERN_FUNC_EXIT; + + if (result == 0) + return true; + else + return false; +} diff --git a/src/agent/framework/event/oma_ds_event_handler.c b/src/agent/framework/event/oma_ds_event_handler.c new file mode 100755 index 0000000..01d5102 --- /dev/null +++ b/src/agent/framework/event/oma_ds_event_handler.c @@ -0,0 +1,908 @@ +/* + * 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. + */ + +/** + * @OMA_DS_Event_Handler.c + * @version 0.1 + * @brief This file is the source file of implementation of event callback function(from ui) + */ + +#include <glib.h> +#include <stdlib.h> +#include <string.h> + +#include <sync_agent.h> + +#include "framework/event/oma_ds_event_handler.h" +#include "framework/event/oma_ds_platform_event_handler.h" +#include "framework/task/oma_ds_engine_controller_task.h" +#include "common/common_define.h" +#include "common/common_vconf.h" +#include "common/common_util.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_COMMON" +#endif + +#define NOTI_KEY "OMADS" + +static void _request_manual_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data); + +static void _request_manual_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data) +{ + _INNER_FUNC_ENTER; + + unsigned int request_msg_id_to_cancel = 0; + get_manual_sync_request_id(&request_msg_id_to_cancel); + + _INNER_FUNC_EXIT; +} + +int event_callback_add_profile_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + char *profile_dir_name = NULL; + char *profile_name = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + char *sync_mode = NULL; + char *sync_type = NULL; + char *interval = NULL; + int category_count; + int result = 0; + int account_id; + + GList *categorys = NULL; + GList *iter = NULL; + + sync_agent_get_event_data_param(request, &profile_dir_name); + sync_agent_get_event_data_param(request, &profile_name); + sync_agent_get_event_data_param(request, &addr); + sync_agent_get_event_data_param(request, &id); + sync_agent_get_event_data_param(request, &password); + sync_agent_get_event_data_param(request, &sync_mode); + sync_agent_get_event_data_param(request, &sync_type); + sync_agent_get_event_data_param(request, &interval); + sync_agent_get_event_data_param(request, &category_count); + + /* + _DEBUG_INFO("request param : %s", profile_dir_name); + _DEBUG_INFO("request param : %s", profile_name); + _DEBUG_INFO("request param : %s", addr); + _DEBUG_INFO("request param : %s", id); + _DEBUG_INFO("request param : %s", password); + _DEBUG_INFO("request param : %s", sync_mode); + _DEBUG_INFO("request param : %s", sync_type); + _DEBUG_INFO("request param : %s", interval); + _DEBUG_INFO("request param : %d", category_count); + */ + + int i = 0; + for (; i < category_count; i++) { + sync_service_s *category = (sync_service_s *) calloc(1, sizeof(sync_service_s)); + if (category == NULL) { + _DEBUG_ERROR("category is NULL !"); + + if (profile_dir_name != NULL) + free(profile_dir_name); + + if (profile_name != NULL) + free(profile_name); + + if (addr != NULL) + free(addr); + + if (id != NULL) + free(id); + + if (password != NULL) + free(password); + + if (sync_mode != NULL) + free(sync_mode); + + if (sync_type != NULL) + free(sync_type); + + if (interval != NULL) + free(interval); + + return 0; + } + + sync_agent_get_event_data_param(request, &(category->service_type)); + sync_agent_get_event_data_param(request, &(category->enabled)); + sync_agent_get_event_data_param(request, &(category->src_uri)); + sync_agent_get_event_data_param(request, &(category->tgt_uri)); + sync_agent_get_event_data_param(request, &(category->id)); + sync_agent_get_event_data_param(request, &(category->password)); + + categorys = g_list_append(categorys, category); + + /* + _DEBUG_INFO("category[i]->content_type : %d", category->content_type); + _DEBUG_INFO("category[i]->enabled : %d", category->enabled); + _DEBUG_INFO("category[i]->srcURI : %s", category->src_uri); + _DEBUG_INFO("category[i]->tgtURI : %s", category->tgt_uri); + _DEBUG_INFO("category[i]->id : %s", category->id); + _DEBUG_INFO("category[i]->password : %s", category->password); + */ + } + + /* send to engine controller */ + void *in_param_value_array[9] = { &profile_dir_name, &profile_name, &addr, &id, &password, &sync_mode, &sync_type, &interval, &categorys }; + int in_param_index_array[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; + sync_agent_ec_value_type_e in_param_value_type_array[9] = { SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, + SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, + SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT + }; + unsigned int request_msg_id = 0; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_ADD_PROFILE, 0, 9, in_param_index_array, in_param_value_type_array, in_param_value_array, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &account_id); + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &account_id); + + for (iter = categorys; iter != NULL; iter = g_list_next(iter)) { + sync_service_s *category = (sync_service_s *) iter->data; + + if (category != NULL) + free_sync_service(category); + } + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_edit_profile_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + char *profile_dir_name = NULL; + char *profile_name = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + char *sync_mode = NULL; + char *sync_type = NULL; + char *interval = NULL; + int category_count; + int result = 0; + + GList *categorys = NULL; + GList *iter = NULL; + + sync_agent_get_event_data_param(request, &profile_dir_name); + if (profile_dir_name == NULL) { + _DEBUG_ERROR("profile_dir_name is not defined"); + result = 0; + goto error; + } + _DEBUG_INFO("request param : %s", profile_dir_name); + + int account_id = get_account_id(profile_dir_name, false); + + sync_agent_get_event_data_param(request, &profile_name); + sync_agent_get_event_data_param(request, &addr); + sync_agent_get_event_data_param(request, &id); + sync_agent_get_event_data_param(request, &password); + sync_agent_get_event_data_param(request, &sync_mode); + sync_agent_get_event_data_param(request, &sync_type); + sync_agent_get_event_data_param(request, &interval); + sync_agent_get_event_data_param(request, &category_count); + + _DEBUG_INFO("request param : %s", profile_name); + _DEBUG_INFO("request param : %s", addr); + _DEBUG_INFO("request param : %s", id); + _DEBUG_INFO("request param : %s", password); + _DEBUG_INFO("request param : %s", sync_mode); + _DEBUG_INFO("request param : %s", sync_type); + _DEBUG_INFO("request param : %s", interval); + _DEBUG_INFO("request param : %d", category_count); + + int i = 0; + for (; i < category_count; i++) { + sync_service_s *category = (sync_service_s *) calloc(1, sizeof(sync_service_s)); + if (category == NULL) { + _DEBUG_ERROR("category is NULL !"); + + if (profile_dir_name != NULL) + free(profile_dir_name); + + if (profile_name != NULL) + free(profile_name); + + if (addr != NULL) + free(addr); + + if (id != NULL) + free(id); + + if (password != NULL) + free(password); + + if (sync_mode != NULL) + free(sync_mode); + + if (sync_type != NULL) + free(sync_type); + + if (interval != NULL) + free(interval); + + return 0; + } + + sync_agent_get_event_data_param(request, &(category->service_type)); + sync_agent_get_event_data_param(request, &(category->enabled)); + sync_agent_get_event_data_param(request, &(category->src_uri)); + sync_agent_get_event_data_param(request, &(category->tgt_uri)); + sync_agent_get_event_data_param(request, &(category->id)); + sync_agent_get_event_data_param(request, &(category->password)); + + categorys = g_list_append(categorys, category); + + _DEBUG_INFO("category[i]->content_type : %d", category->service_type); + _DEBUG_INFO("category[i]->enabled : %d", category->enabled); + _DEBUG_INFO("category[i]->srcURI : %s", category->src_uri); + _DEBUG_INFO("category[i]->tgtURI : %s", category->tgt_uri); + _DEBUG_INFO("category[i]->id : %s", category->id); + _DEBUG_INFO("category[i]->password : %s", category->password); + } + + /* send to engine controller */ + void *in_param_value_array[9] = { &account_id, &profile_name, &addr, &id, &password, &sync_mode, &sync_type, &interval, &categorys }; + int in_param_index_array[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; + sync_agent_ec_value_type_e in_param_value_type_array[9] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, + SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, + SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT + }; + unsigned int request_msg_id = 0; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_EDIT_PROFILE, 0, 9, in_param_index_array, in_param_value_type_array, in_param_value_array, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + + error: + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + + for (iter = categorys; iter != NULL; iter = g_list_next(iter)) { + sync_service_s *category = (sync_service_s *) iter->data; + + if (category != NULL) + free_sync_service(category); + } + + if (profile_dir_name != NULL) + free(profile_dir_name); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_delete_profile_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + int result = 0; + int count; + sync_agent_get_event_data_param(request, &count); + GList *profiles = NULL; + int *account_id = 0; + + int i; + char *profile = NULL; + for (i = 0; i < count; i++) { + if (profile != NULL) { + free(profile); + profile = NULL; + } + + sync_agent_get_event_data_param(request, &profile); + if (profile == NULL) { + _DEBUG_ERROR("profile is not defined"); + result = 0; + goto error; + } + _DEBUG_INFO("request param : %s", profile); + + account_id = (int *)calloc(1, sizeof(int)); + if (account_id == NULL) { + _DEBUG_ERROR("account_id is NULL"); + result = 0; + goto error; + } + + *account_id = get_account_id(profile, false); + + profiles = g_list_append(profiles, account_id); + } + + /* send to engine controller */ + void *in_param_value_array[1] = { &profiles }; + int in_param_index_array[1] = { 0 }; + sync_agent_ec_value_type_e in_param_value_type_array[1] = { SYNC_AGENT_EC_VALUE_TYPE_STRUCT }; + unsigned int request_msg_id = 0; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_DELETE_PROFILE, 0, 1, in_param_index_array, in_param_value_type_array, in_param_value_array, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + + error: + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + + if (profile != NULL) + free(profile); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_request_sync_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + char *profile = NULL; + int result = 0; + + sync_agent_get_event_data_param(request, &profile); + if (profile == NULL) { + _DEBUG_ERROR("profile is not defined"); + result = 0; + goto error; + } + _DEBUG_INFO("request param : %s", profile); + + int account_id = get_account_id(profile, false); + char *sync_mode = strdup(DEFINE_SYNC_MODE_MANUAL); + + void *in_param_value_array[3] = { &account_id, &sync_mode, NULL }; + int in_param_index_array[3] = { 0, 1, 2 }; + sync_agent_ec_value_type_e in_param_value_type_array[3] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT }; + unsigned int request_msg_id = 0; + sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REQUEST, 0, 3, in_param_index_array, in_param_value_type_array, in_param_value_array, _request_manual_sync_task_finish_callback, NULL, (int *)&request_msg_id); + + insert_request_msg_info(SYNC_MODE_MANUAL, request_msg_id); + + error: + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + + if (profile != NULL) + free(profile); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_cancel_sync_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + char *profile = NULL; + int result = 0; + bool res; + + sync_agent_get_event_data_param(request, &profile); + if (profile == NULL) { + _DEBUG_ERROR("profile is not defined"); + result = 0; + goto error; + } + + _DEBUG_INFO("request param : %s", profile); + + res = cancel_current_sync_task(); + if (res == true) + result = 1; + else + result = 0; + + error: + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + + if (profile != NULL) + free(profile); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_get_profile_data_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + int result = 0; + char *profile = NULL; + char *profile_name = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + char *sync_mode = NULL; + char *sync_type = NULL; + char *interval = NULL; + int last_session_status = 0; + int last_session_time = 0; + + sync_agent_get_event_data_param(request, &profile); + if (profile == NULL) { + _DEBUG_ERROR("profile is not defined"); + result = 0; + goto error; + } + + _DEBUG_INFO("request param : %s", profile); + + int account_id = get_account_id(profile, false); + + int in_param_index_array[1] = { 0 }; + void *in_param_value_array[1] = { &account_id }; + sync_agent_ec_value_type_e in_param_value_type_array[1] = { SYNC_AGENT_EC_VALUE_TYPE_INT }; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + unsigned int request_msg_id = 0; + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_DATA, 0, 1, in_param_index_array, in_param_value_type_array, in_param_value_array, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profile_name); + sync_agent_get_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &addr); + sync_agent_get_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &id); + sync_agent_get_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &password); + sync_agent_get_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &sync_mode); + sync_agent_get_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &sync_type); + sync_agent_get_param_value(&(param_array[7]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &interval); + sync_agent_get_param_value(&(param_array[8]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &last_session_status); + sync_agent_get_param_value(&(param_array[9]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &last_session_time); + + error: + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_name); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)addr); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)id); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)password); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)sync_mode); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)sync_type); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)interval); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &last_session_status); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &last_session_time); + + if (profile != NULL) + free(profile); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_get_profile_sync_category_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + char *profile = NULL; + int content_type = 0; + int enabled = 0; + char *srcURI = NULL; + char *tgtURI = NULL; + char *id = NULL; + char *password = NULL; + int result = 0; + + sync_agent_get_event_data_param(request, &profile); + if (profile == NULL) { + _DEBUG_ERROR("profile is not defined"); + result = 0; + goto error; + } + + sync_agent_get_event_data_param(request, &content_type); + + _DEBUG_INFO("request param : %s", profile); + _DEBUG_INFO("request param : %d", content_type); + + int account_id = get_account_id(profile, false); + _DEBUG_INFO(" account_id: %d", account_id); + + int in_param_index_array[2] = { 0, 1 }; + void *in_param_value_array[2] = { &account_id, &content_type }; + sync_agent_ec_value_type_e in_param_value_type_array[2] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_INT }; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + unsigned int request_msg_id = 0; + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_SYNC_CATEGORY, 0, 2, in_param_index_array, in_param_value_type_array, in_param_value_array, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &enabled); + sync_agent_get_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &srcURI); + sync_agent_get_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &tgtURI); + sync_agent_get_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &id); + sync_agent_get_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &password); + + /* + _DEBUG_INFO("result = %d", result); + _DEBUG_INFO("enabled = %d", enabled); + _DEBUG_INFO("srcURI = %s", srcURI); + _DEBUG_INFO("tgtURI = %s", tgtURI); + _DEBUG_INFO("id = %s", id); + _DEBUG_INFO("password = %s", password); + */ + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &enabled); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)srcURI); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)tgtURI); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)id); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)password); + + error: + + if (profile != NULL) + free(profile); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_get_profile_last_statistics_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + char *profile = NULL; + int content_type = 0; + char *dbSynced = "NO"; + int last_session_time = -1; + int server2Client_Total = 0; + int server2Client_NrOfAdd = 0; + int server2Client_NrOfDelete = 0; + int server2Client_NrOfReplace = 0; + int client2Server_Total = 0; + int client2Server_NrOfAdd = 0; + int client2Server_NrOrDelete = 0; + int client2Server_NrOfReplace = 0; + int result = 0; + + sync_agent_get_event_data_param(request, &profile); + if (profile == NULL) { + _DEBUG_ERROR("profile is not defined"); + result = 0; + goto error; + } + + sync_agent_get_event_data_param(request, &content_type); + + _DEBUG_INFO("request param : %s", profile); + _DEBUG_INFO("request param : %d", content_type); + + int account_id = get_account_id(profile, false); + + int in_param_index_array[2] = { 0, 1 }; + void *in_param_value_array[2] = { &account_id, &content_type }; + sync_agent_ec_value_type_e in_param_value_type_array[2] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_INT }; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + unsigned int request_msg_id = 0; + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_STATISTICS, 0, 2, in_param_index_array, in_param_value_type_array, in_param_value_array, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &dbSynced); + sync_agent_get_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &last_session_time); + sync_agent_get_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_Total); + sync_agent_get_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_NrOfAdd); + sync_agent_get_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_NrOfDelete); + sync_agent_get_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_NrOfReplace); + sync_agent_get_param_value(&(param_array[7]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_Total); + sync_agent_get_param_value(&(param_array[8]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_NrOfAdd); + sync_agent_get_param_value(&(param_array[9]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_NrOrDelete); + sync_agent_get_param_value(&(param_array[10]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_NrOfReplace); + + /* + _DEBUG_INFO("result = %d", result); + _DEBUG_INFO("dbSynced = %s\n", dbSynced); + _DEBUG_INFO("last_session_time = %d\n", last_session_time); + _DEBUG_INFO("server2Client_Total = %d\n", server2Client_Total); + _DEBUG_INFO("server2Client_NrOfAdd = %d\n", server2Client_NrOfAdd); + _DEBUG_INFO("server2Client_NrOfDelete = %d\n", server2Client_NrOfDelete); + _DEBUG_INFO("server2Client_NrOfReplace = %d\n", server2Client_NrOfReplace); + _DEBUG_INFO("client2Server_Total = %d\n", client2Server_Total); + _DEBUG_INFO("client2Server_NrOfAdd = %d\n", client2Server_NrOfAdd); + _DEBUG_INFO("client2Server_NrOrDelete = %d\n", client2Server_NrOrDelete); + _DEBUG_INFO("client2Server_NrOfReplace = %d\n", client2Server_NrOfReplace); + */ + + error: + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)dbSynced); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &last_session_time); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &server2Client_Total); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &server2Client_NrOfAdd); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &server2Client_NrOfDelete); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &server2Client_NrOfReplace); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &client2Server_Total); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &client2Server_NrOfAdd); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &client2Server_NrOrDelete); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &client2Server_NrOfReplace); + + if (profile != NULL) + free(profile); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_add_profile_cp_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + char *profile_name = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + int category_count; + int result = 0; + int account_id; + + sync_agent_get_event_data_param(request, &profile_name); + sync_agent_get_event_data_param(request, &addr); + sync_agent_get_event_data_param(request, &id); + sync_agent_get_event_data_param(request, &password); + sync_agent_get_event_data_param(request, &category_count); + + /* + _DEBUG_INFO("request param : %s", profile_name); + _DEBUG_INFO("request param : %s", addr); + _DEBUG_INFO("request param : %s", id); + _DEBUG_INFO("request param : %s", password); + _DEBUG_INFO("request param : %d", category_count); + */ + + GList *categorys = NULL; + int i = 0; + for (; i < category_count; i++) { + resource_cp_s *category = (resource_cp_s *) calloc(1, sizeof(resource_cp_s)); + if (category == NULL) { + _DEBUG_ERROR("category is NULL !"); + + if (profile_name != NULL) + free(profile_name); + + if (addr != NULL) + free(addr); + + if (id != NULL) + free(id); + + if (password != NULL) + free(password); + + return 0; + } + + sync_agent_get_event_data_param(request, &(category->name)); + sync_agent_get_event_data_param(request, &(category->accept)); + sync_agent_get_event_data_param(request, &(category->id)); + sync_agent_get_event_data_param(request, &(category->password)); + sync_agent_get_event_data_param(request, &(category->auth_type)); + sync_agent_get_event_data_param(request, &(category->auth_data)); + + categorys = g_list_append(categorys, category); + + /* + _DEBUG_INFO("category[%d]->name : %s", i, category->name); + _DEBUG_INFO("category[%d]->accept : %s", i, category->accept); + _DEBUG_INFO("category[%d]->id : %s", i, category->id); + _DEBUG_INFO("category[%d]->password : %s", i, category->password); + _DEBUG_INFO("category[%d]->authType : %s", i, category->auth_type); + _DEBUG_INFO("category[%d]->authData : %s\n", i, category->auth_data); + */ + } + + /* send to engine controller */ + void *in_param_value_array[5] = { &profile_name, &addr, &id, &password, &categorys }; + int in_param_index_array[5] = { 0, 1, 2, 3, 4 }; + sync_agent_ec_value_type_e in_param_value_type_array[5] = { SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, + SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT + }; + unsigned int request_msg_id = 0; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_ADD_PROFILE_CP, 0, 5, in_param_index_array, in_param_value_type_array, in_param_value_array, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &account_id); + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &account_id); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_request_calllog_sync_async(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + char *data = NULL; + int result = 0; + + sync_agent_get_event_data_param(request, &data); + if (data == NULL) { + _DEBUG_ERROR("data is NULL"); + result = 0; + goto error; + } + _DEBUG_INFO("data = %s", data); + + result = send_ip_push_sync_msg(data); + + error: + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_request_reset_all_data_sync(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + int result; + + /* 1. cancel current sync session */ + cancel_current_sync_task(); + + /* 2. reset all data */ + unsigned int request_msg_id = 0; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + + unsigned int out_param_count; + sync_agent_ec_param_param_s **param_array = NULL; + + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_RESET_ALL_DATA, 0, 0, NULL, NULL, NULL, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + _DEBUG_INFO("result = %d", result); + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + + _EXTERN_FUNC_EXIT; + return 0; +} + +int event_callback_request_get_all_profiles_data(sync_agent_event_data_s * request, sync_agent_event_data_s * response) +{ + _EXTERN_FUNC_ENTER; + + int result = 0; + GList *profiles_data = NULL; + GList *profile_iter = NULL; + GList *category_iter = NULL; + + int profile_count = 0; + int category_count = 0; + sync_agent_ds_profile_info *profile_info = NULL; + sync_agent_ds_service_info *category_info = NULL; + + unsigned int out_param_count; + sync_agent_ec_task_error_e task_error = SYNC_AGENT_EC_TASK_ERROR_NOT_YET_RUN; + sync_agent_ec_param_param_s **param_array = NULL; + + unsigned int request_msg_id = 0; + sync_agent_request_sync_task(EC_MSG_TYPE_SYNC_TASK_GET_ALL_PROFILES_DATA, 0, 0, NULL, NULL, NULL, (int *)&request_msg_id, &task_error, &out_param_count, ¶m_array); + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profiles_data); + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &result); + _DEBUG_INFO("result = %d", result); + + if (profiles_data != NULL) { + profile_count = g_list_length(profiles_data); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &profile_count); + _DEBUG_INFO("profile_count = %d", profile_count); + + for (profile_iter = profiles_data; profile_iter != NULL; profile_iter = g_list_next(profile_iter)) { + profile_info = (sync_agent_ds_profile_info *) profile_iter->data; + + sync_agent_ds_server_info *server_info = &profile_info->server_info; + sync_agent_ds_sync_info *sync_info = &profile_info->sync_info; + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_name); + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->addr); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->id); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->password); + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)sync_info->sync_mode); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)sync_info->sync_type); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)sync_info->interval); + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &profile_info->last_sync_status); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &profile_info->last_sync_time); + + _DEBUG_INFO("profile_info->profile_dir_name = %s", profile_info->profile_dir_name); + _DEBUG_INFO("profile_info->profile_name = %s", profile_info->profile_name); + + _DEBUG_INFO("server_info->addr = %s", server_info->addr); + _DEBUG_INFO("server_info->id = %s", server_info->id); + _DEBUG_INFO("server_info->password = %s", server_info->password); + + _DEBUG_INFO("sync_info->sync_mode = %s", sync_info->sync_mode); + _DEBUG_INFO("sync_info->sync_type = %s", sync_info->sync_type); + _DEBUG_INFO("sync_info->interval = %s", sync_info->interval); + + _DEBUG_INFO("profile_info->last_sync_status = %d", profile_info->last_sync_status); + _DEBUG_INFO("profile_info->last_sync_time = %d", profile_info->last_sync_time); + + category_count = g_list_length(profile_info->service_list); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &category_count); + + _DEBUG_INFO("category_count = %d", category_count); + + for (category_iter = profile_info->service_list; category_iter != NULL; category_iter = g_list_next(category_iter)) { + category_info = (sync_agent_ds_service_info *) category_iter->data; + + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &category_info->service_type); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &category_info->enabled); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->src_uri); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->tgt_uri); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->id); + sync_agent_append_event_data_param(response, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->password); + + _DEBUG_INFO("category_info->service_type = %d", category_info->service_type); + _DEBUG_INFO("category_info->enabled = %d", category_info->enabled); + _DEBUG_INFO("category_info->src_uri = %s", category_info->src_uri); + _DEBUG_INFO("category_info->tgt_uri = %s", category_info->tgt_uri); + _DEBUG_INFO("category_info->id = %s", category_info->id); + _DEBUG_INFO("category_info->password = %s", category_info->password); + } + } + } + + _EXTERN_FUNC_EXIT; + return 0; +} diff --git a/src/agent/framework/event/oma_ds_platform_event_handler.c b/src/agent/framework/event/oma_ds_platform_event_handler.c new file mode 100755 index 0000000..ddc3547 --- /dev/null +++ b/src/agent/framework/event/oma_ds_platform_event_handler.c @@ -0,0 +1,355 @@ +/* + * 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. + */ + +/** + * @OMA_DS_Platform_Event_Handler.c + * @version 0.1 + * @brief This file is the source file of implementation of event callback function(from platform) + */ + +#include <sync_agent.h> +#include <plugin/plugin_slp_sysnoti_wap_push.h> + +#include "framework/event/oma_ds_platform_event_handler.h" +#include "framework/task/oma_ds_engine_controller_task.h" +#include "framework/san-parser/pm_sanparser.h" +#include "common/common_define_internal.h" +#include "common/common_util.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_COMMON" +#endif + +static void _request_periodic_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data); + +static void _request_san_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data); + +static void _request_periodic_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data) +{ + _INNER_FUNC_ENTER; + + unsigned int request_msg_id_to_cancel = 0; + get_periodic_sync_request_id(&request_msg_id_to_cancel); + + _INNER_FUNC_EXIT; + +} + +static void _request_san_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data) +{ + _INNER_FUNC_ENTER; + + unsigned int request_msg_id_to_cancel = 0; + get_san_sync_request_id(&request_msg_id_to_cancel); + + _INNER_FUNC_EXIT; +} + +int san_callback_parse(int item_id, void *user_data) +{ + _EXTERN_FUNC_ENTER; + san_package_s *pSanPackage = NULL; + int accountID = -1; + char *sync_mode = NULL; + char *server_id = NULL; + bool result; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + GList *account_info_list = NULL; + GList *iter = NULL; + + pmci_san_incoming_s *incoming_data = (pmci_san_incoming_s *) user_data; + + switch (incoming_data->version) { + case 11: + { + pSanPackage = san_package_11_parser(incoming_data->msg_body, incoming_data->msg_size); + } + break; + case 12: + { + pSanPackage = san_package_12_parser(incoming_data->msg_body, incoming_data->msg_size); + } + break; + default: + break; + } + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("da_err = %d", da_err); + goto return_part; + } + + if (pSanPackage == NULL) { + _DEBUG_ERROR("pSanPackage is NULL"); + goto return_part; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_NONE; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto return_part; + } + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + fw_account = (sync_agent_fw_account_s *) iter->data; + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SYNC_MODE, &sync_mode); + if (result == true) { + /*One device MUST NOT register multi account at same server... */ + if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) { + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &server_id); + if (result == true) { + /*One device MUST NOT register multi account at same server... */ + if (strcmp(pSanPackage->server_id, server_id) == 0) { + accountID = fw_account->account_id; + break; + } + } else { + _DEBUG_ERROR("failed in get_Config"); + goto return_part; + } + } + } else { + _DEBUG_ERROR("failed in get_Config"); + goto return_part; + } + + } + + if (accountID < 0) + goto return_part; + + char *syncMode = strdup(DEFINE_SYNC_MODE_PUSH); + + void *in_param_value_array[3] = { &accountID, &syncMode, &pSanPackage }; + int in_param_index_array[3] = { 0, 1, 2 }; + sync_agent_ec_value_type_e in_param_type[3] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT }; + unsigned int request_msg_id = 0; + sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REQUEST, 0, 3, in_param_index_array, in_param_type, in_param_value_array, _request_san_sync_task_finish_callback, NULL, (int *)&request_msg_id); + + insert_request_msg_info(SYNC_MODE_SAN, request_msg_id); + + sync_agent_close_agent(); + + if (pSanPackage != NULL) + sanPackageParserFree((void *)pSanPackage); + + _EXTERN_FUNC_EXIT; + return 1; + + return_part: + + sync_agent_free_fw_account_list(account_info_list); + + if (sync_mode != NULL) + free(sync_mode); + + if (server_id != NULL) + free(server_id); + +/* if( incoming_data != NULL){ + if (incoming_data->msg_body != NULL) + free((void *)incoming_data->msg_body); + + free(incoming_data); + }*/ + + if (pSanPackage != NULL) + sanPackageParserFree((void *)pSanPackage); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + return 0; +} + +void send_periodic_sync_msg(int scheduler_id, void *data) +{ + _EXTERN_FUNC_ENTER; + + _DEBUG_INFO("#######Scheduler Send Msg Success!!!!!##########"); + _DEBUG_INFO("schedulerId = %d", scheduler_id); + + int accountId = -1; + int alarmId = 0; + char *alarmId_str = NULL; + bool result; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + GList *account_info_list = NULL; + GList *iter = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_agent"); + goto error; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_NONE; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto error; + } + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + fw_account = (sync_agent_fw_account_s *) iter->data; + if (alarmId_str != NULL) + free(alarmId_str); + + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, &alarmId_str); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + goto error; + } + alarmId = atoi(alarmId_str); + _DEBUG_INFO("alarm id = %d", alarmId); + if (alarmId == scheduler_id) { + _DEBUG_INFO("account_list[i] = %d", fw_account->account_id); + accountId = fw_account->account_id; + break; + } + } + + if (accountId != -1) { + int in_param_index_array[3] = { 0, 1, 2 }; + sync_agent_ec_value_type_e in_param_value_type_array[3] = { SYNC_AGENT_EC_VALUE_TYPE_INT, + SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT + }; + char *syncMode = strdup(DEFINE_SYNC_MODE_PERIODIC); + + void *in_param_value_array[3] = { &accountId, &syncMode, NULL }; + + unsigned int request_msg_id = 0; + sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REQUEST, 0, 3, in_param_index_array, in_param_value_type_array, in_param_value_array, _request_periodic_sync_task_finish_callback, NULL, (int *)&request_msg_id); + + insert_request_msg_info(SYNC_MODE_PERIODIC, request_msg_id); + } + + error: + + sync_agent_free_fw_account_list(account_info_list); + + if (alarmId_str != NULL) + free(alarmId_str); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; +} + +int send_ip_push_sync_msg(char *data) +{ + _EXTERN_FUNC_ENTER; + + san_package_s *pSanPackage = NULL; + char *id = NULL; + char *server_id = NULL; + int accountId = -1; + bool result = false; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + GList *account_info_list = NULL; + GList *iter = NULL; + sync_agent_fw_account_s *account_info = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_agent"); + goto error; + } + + result = parse_ip_push_msg(data, &pSanPackage, &id); + if (result != true) { + _DEBUG_ERROR("Failed in parseIPPushMsg"); + goto error; + } + + cancel_current_sync_task(); + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_ACCESS_NAME; + query.access_name = "DIVE"; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + account_info = (sync_agent_fw_account_s *) iter->data; + + _DEBUG_INFO("accountId = %d", account_info->account_id); + result = get_config(account_info->account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &server_id); + if (result == true) { + if (strcmp(pSanPackage->server_id, server_id) == 0) { + accountId = account_info->account_id; + break; + } + } else { + _DEBUG_ERROR("failed in get_Config"); + goto error; + } + } + + if (accountId < 0) + goto error; + + char *syncMode = strdup(DEFINE_SYNC_MODE_PUSH); + + void *in_param_value_array[3] = { &accountId, &syncMode, &pSanPackage }; + int in_param_index_array[3] = { 0, 1, 2 }; + sync_agent_ec_value_type_e in_param_value_type_array[3] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT }; + unsigned int request_msg_id = 0; + sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REQUEST, 0, 3, in_param_index_array, in_param_value_type_array, in_param_value_array, _request_san_sync_task_finish_callback, NULL, (int *)&request_msg_id); + + insert_request_msg_info(SYNC_MODE_SAN, request_msg_id); + + sync_agent_free_fw_account_list(account_info_list); + + if (server_id != NULL) + free(server_id); + + if (pSanPackage != NULL) + sanPackageParserFree((void *)pSanPackage); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + return 1; + + error: + + sync_agent_free_fw_account_list(account_info_list); + + if (server_id != NULL) + free(server_id); + + if (pSanPackage != NULL) + sanPackageParserFree((void *)pSanPackage); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + return 0; +} diff --git a/src/agent/framework/san-parser/pm_sanparser.c b/src/agent/framework/san-parser/pm_sanparser.c new file mode 100755 index 0000000..bbb21e7 --- /dev/null +++ b/src/agent/framework/san-parser/pm_sanparser.c @@ -0,0 +1,633 @@ +/* + * 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. + */ + +/** + * @PM_SanParser.c + * @version 0.1 + * @brief This file is the source file of implementation of San Parser + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <inttypes.h> +#include <glib.h> + +#include <sync_agent.h> + +#include "wbxml/wbxml.h" +#include "wbxml/wbxml_tree.h" +#include "framework/san-parser/pm_sanparser.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_COMMON" +#endif + +static san_content_type_s contentTypeSupported[] = { + {0x00, NULL}, + {0x03, "text/plain"}, + {0x06, "text/x-vcalendar"}, + {0x07, "text/x-vcard"}, + {0x0305, "text/calendar"}, + {0x0306, "application/vnd.omads-email+xml"}, + {0x0307, "application/vnd.omads-file+xml"}, + {0x0308, "application/vnd.omads-folder+xml"}, + {0x0309, "text/vcard"} +}; + +san_package_s *san_package_12_parser(const char *msg_body, unsigned int msg_size) +{ + _EXTERN_FUNC_ENTER; + + unsigned int idLength = (uint8_t) msg_body[23]; + retvm_if(msg_size < (25 + idLength), NULL, "[sanPackage12Parser] SAN package size is smaller than its minimal size specified in the spec, related to [Header] part."); + + san_package_s *san = (san_package_s *) calloc(1, sizeof(san_package_s)); + retvm_if(san == NULL, NULL, "[sanPackage12Parser] SAN package memory allocation fail. [Container]"); + + /* MSG BODY WITHOUT DIGEST */ + san->msg_body_without_digest = (char *)calloc(msg_size - 16, sizeof(char)); + if (san->msg_body_without_digest == NULL) { + _DEBUG_ERROR("[sanPackage12Parser] SAN package memory allocation fail. [Msg body]"); + goto error; + } + memcpy(san->msg_body_without_digest, msg_body + 16, msg_size - 16); + san->msg_body_without_digest_length = msg_size - 16; + + /* DIGEST */ + san->digest = (char *)calloc(16, sizeof(char)); + if (san->digest == NULL) { + _DEBUG_ERROR("[sanPackage12Parser] SAN package memory allocation fail. [Digest]"); + goto error; + } + memcpy(san->digest, msg_body, 16); + + /* VERSION */ + unsigned int version = ((uint8_t) msg_body[16]) << 2; + version = version | ((uint8_t) msg_body[17]) >> 6; + + if (version != 12) { + _DEBUG_ERROR("[sanPackage12Parser] Not supported SAN version %d.", version); + goto error; + } + san->version = version; + + /* UI MODE */ + san->ui_mode = (((uint8_t) msg_body[17]) & 0x30) >> 4; + + /* INITIATOR */ + san->initiator = (((uint8_t) msg_body[17]) & 0x08) >> 3; + + /* SESSION ID */ +/*san->sessionID = ((uint8_t)msgBody[21]) << 8; + san->sessionID = san->sessionID | (uint8_t)msgBody[22];*/ + san->session_id = atoi(g_strdup_printf("%02X%02X", msg_body[21], msg_body[22])); + _DEBUG_INFO("session id : %d \n", san->session_id); + + /* SERVER ID */ + if (idLength) { + san->server_id = (char *)calloc(idLength + 1, sizeof(char)); + if (san->server_id == NULL) { + _DEBUG_ERROR("[sanPackage12Parser] SAN package memory allocation fail. [Server ID]"); + goto error; + } + memcpy(san->server_id, msg_body + 24, idLength); + } + + san->cnt_sync_alerts = ((uint8_t) msg_body[24 + idLength]) >> 4; + + if (san->cnt_sync_alerts == 0) { + if (msg_size > 24 + idLength + 1) { + _DEBUG_INFO("[sanPackage12Parser] There are remaining bytes at the end of the package. (w/o alerts info)"); + } + + /* If number of sync alerts equals 0, should sync all data store in the client */ + _EXTERN_FUNC_EXIT; + return san; + } + + san->sync_alerts = (san_sync_alert_s *) calloc(san->cnt_sync_alerts, sizeof(san_sync_alert_s)); + if (san->sync_alerts == NULL) { + _DEBUG_ERROR("[sanPackage12Parser] SAN package memory allocation fail. [syncAlerts]"); + goto error; + } + + msg_body += 25 + idLength; + unsigned int alertLength = 25 + idLength; + unsigned int i; + + for (i = 0; i < san->cnt_sync_alerts; i++) { + + idLength = (uint8_t) msg_body[4]; + if (msg_size < (alertLength + 5 + idLength)) { + _DEBUG_ERROR("[sanPackage12Parser] SAN package size is smaller than"); + _DEBUG_ERROR("[sanPackage12Parser] its minimal size specified in the spec, related to [Alerts] part."); + goto error; + } + alertLength = alertLength + 5 + idLength; + + /* SYNC TYPE */ + san_sync_type_e alert_type = (((uint8_t) msg_body[0]) >> 4) + 200; + if (alert_type < 206 || alert_type > 210) { + _DEBUG_ERROR("[sanPackage12Parser] SAN doesn't support the sync type %d.", alert_type); + goto error; + } + + unsigned int contentType = ((uint8_t) msg_body[1]) << 16; + contentType = contentType | ((uint8_t) msg_body[2]) << 8; + contentType = contentType | ((uint8_t) msg_body[3]); + + /* CONTENT TYPE */ + char *alert_ct = NULL; + + int j; + int cnt = (int)sizeof(contentTypeSupported) / sizeof(san_content_type_s); + bool isContentSupported = false; + + for (j = 0; j < cnt; j++) { + if (contentType == contentTypeSupported[j].type) { + alert_ct = contentTypeSupported[j].strType; + isContentSupported = true; + break; + } + } + + if (!isContentSupported) { + _DEBUG_ERROR("[sanPackage12Parser] SAN doesn't support the content type %d.", contentType); + goto error; + } + + /* SERVER URI */ + char *alert_uri = NULL; + + if (idLength) { + alert_uri = (char *)calloc(idLength + 1, sizeof(char)); + if (alert_uri == NULL) { + _DEBUG_ERROR("[sanPackage12Parser] SAN package memory allocation fail. [Server URI]"); + goto error; + } + memcpy(alert_uri, msg_body + 5, idLength); + } + msg_body += 5 + idLength; + + san->sync_alerts[i].sync_type = alert_type; + san->sync_alerts[i].content_type = alert_ct; + san->sync_alerts[i].server_uri = alert_uri; + + } + + if (msg_size > alertLength) { + _DEBUG_INFO("[sanPackage12Parser] There are remaining bytes at the end of the package. (with alerts info)"); + } + + _EXTERN_FUNC_EXIT; + return san; + + error: + + sanPackageParserFree(san); + + _EXTERN_FUNC_EXIT; + return NULL; + +} + +san_package_s *san_package_11_parser(const char *msg_body, unsigned int msg_size) +{ + _EXTERN_FUNC_ENTER; + + san_package_s *san = (san_package_s *) calloc(1, sizeof(san_package_s)); + if (san == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [Container]"); + + _EXTERN_FUNC_EXIT; + return NULL; + } + + WBXMLTree *wbxml_tree = NULL; + WBXMLError wbxml_err = wbxml_tree_from_wbxml((unsigned char *)msg_body, msg_size, WBXML_LANG_UNKNOWN, WBXML_CHARSET_UNKNOWN, &wbxml_tree); + + if (wbxml_err != WBXML_OK) { + _DEBUG_ERROR("[sanPackage11Parser] Libwbxml2 failed to parse WBXML STREAM to WBXML TREE, error code : %s", wbxml_errors_string(wbxml_err)); + goto error; + } + + WBXMLTreeNode *synchdr_node; + if ((synchdr_node = wbxml_tree_node_elt_get_from_name(wbxml_tree->root, "SyncHdr", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [SyncHdr]"); + goto error; + } + WBXMLTreeNode *child_node = NULL; + const char *child_node_name = NULL; + + for (child_node = synchdr_node->children; child_node != NULL; child_node = child_node->next) { + child_node_name = (const char *)wbxml_tag_get_xml_name(child_node->name); + + if ((strcmp(child_node_name, "VerDTD") == 0) || (strcmp(child_node_name, "VerProto") == 0)) { + char *version = NULL; + if (child_node->children != NULL && child_node->children->type == WBXML_TREE_TEXT_NODE && child_node->children->content != NULL) { + + version = (char *)wbxml_buffer_get_cstr(child_node->children->content); + + if (strcmp(version, "1.1") && strcmp(version, "SyncML/1.1")) { + _DEBUG_ERROR("[sanPackage11Parser] Not supported SAN version %s.", version); + goto error; + } + san->version = 11; + } + } else if (strcmp(child_node_name, "SessionID") == 0) { + char *sessionID = NULL; + if (child_node->children != NULL && child_node->children->type == WBXML_TREE_TEXT_NODE && child_node->children->content != NULL) { + + sessionID = (char *)wbxml_buffer_get_cstr(child_node->children->content); + + if (sessionID == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL sessionID detected. sessionID MUST NOT be NULL."); + goto error; + } + san->session_id = atoi(sessionID); + } + } else if (strcmp(child_node_name, "Source") == 0) { + char *serverID = NULL; + unsigned serverIDlen = 0; + WBXMLTreeNode *serverid_node; + if ((serverid_node = wbxml_tree_node_elt_get_from_name(child_node, "LocURI", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [LocURI]"); + goto error; + } + + if (serverid_node->children != NULL && serverid_node->children->type == WBXML_TREE_TEXT_NODE && serverid_node->children->content != NULL) { + + serverID = (char *)wbxml_buffer_get_cstr(serverid_node->children->content); + serverIDlen = wbxml_buffer_len(serverid_node->children->content); + if (serverID == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL serverID detected. serverID MUST NOT be NULL."); + goto error; + } + + san->server_id = (char *)calloc(serverIDlen, sizeof(char)); + if (san->server_id == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [Server ID]"); + goto error; + } + memcpy(san->server_id, serverID, serverIDlen); + } + } else if (strcmp(child_node_name, "Cred") == 0) { + + san->cred = (san_cred_s *) calloc(1, sizeof(san_cred_s)); + if (san->cred == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [cred]"); + goto error; + } + + char *credFormat = NULL; + unsigned credFormatLen = 0; + WBXMLTreeNode *credformat_node; + if ((credformat_node = wbxml_tree_node_elt_get_from_name(child_node, "Format", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [Format]"); + goto error; + } + + if (credformat_node->children != NULL && credformat_node->children->type == WBXML_TREE_TEXT_NODE && credformat_node->children->content != NULL) { + + credFormat = (char *)wbxml_buffer_get_cstr(credformat_node->children->content); + credFormatLen = wbxml_buffer_len(credformat_node->children->content); + if (credFormat == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL credFormat detected. credFormat MUST NOT be NULL."); + goto error; + } + + san->cred->cred_format = (char *)calloc(credFormatLen, sizeof(char)); + if (san->cred->cred_format == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [credFormat]"); + goto error; + } + memcpy(san->cred->cred_format, credFormat, credFormatLen); + } + + char *credAuth = NULL; + unsigned credAuthLen = 0; + WBXMLTreeNode *credauth_node; + if ((credauth_node = wbxml_tree_node_elt_get_from_name(child_node, "Type", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [Type]"); + goto error; + } + + if (credauth_node->children != NULL && credauth_node->children->type == WBXML_TREE_TEXT_NODE && credauth_node->children->content != NULL) { + + credAuth = (char *)wbxml_buffer_get_cstr(credauth_node->children->content); + credAuthLen = wbxml_buffer_len(credauth_node->children->content); + if (credAuth == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL credAuth detected. credAuth MUST NOT be NULL."); + goto error; + } + + san->cred->cred_auth = (char *)calloc(credAuthLen, sizeof(char)); + if (san->cred->cred_auth == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [credAuth]"); + goto error; + } + memcpy(san->cred->cred_auth, credAuth, credAuthLen); + } + + char *credData = NULL; + unsigned credDataLen = 0; + WBXMLTreeNode *creddata_node; + if ((creddata_node = wbxml_tree_node_elt_get_from_name(child_node, "Data", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [Data]"); + goto error; + } + + if (creddata_node->children != NULL && creddata_node->children->type == WBXML_TREE_TEXT_NODE && creddata_node->children->content != NULL) { + + credData = (char *)wbxml_buffer_get_cstr(creddata_node->children->content); + credDataLen = wbxml_buffer_len(creddata_node->children->content); + if (credData == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL credData detected. credData MUST NOT be NULL."); + goto error; + } + + san->cred->cred_data = (char *)calloc(credDataLen, sizeof(char)); + if (san->cred->cred_data == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [credData]"); + goto error; + } + memcpy(san->cred->cred_data, credData, credDataLen); + } + + } + + } + + WBXMLTreeNode *syncbody_node; + if ((syncbody_node = wbxml_tree_node_elt_get_from_name(wbxml_tree->root, "SyncBody", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [SyncBody]"); + goto error; + } + + WBXMLList *alertnode_list = wbxml_tree_node_get_all_children(syncbody_node); + unsigned int alertnode_list_len = wbxml_list_len(alertnode_list); + + child_node = (WBXMLTreeNode *) wbxml_list_get(alertnode_list, alertnode_list_len - 1); + child_node_name = (const char *)wbxml_tag_get_xml_name(child_node->name); + if (strcmp(child_node_name, "Final") == 0) + san->cnt_sync_alerts = alertnode_list_len - 1; + + if (san->cnt_sync_alerts == 0) { + /* If number of sync alerts equals 0, should sync all data store in the client */ + return san; + } else { + san->sync_alerts = (san_sync_alert_s *) calloc(san->cnt_sync_alerts, sizeof(san_sync_alert_s)); + if (san->sync_alerts == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [syncAlerts]"); + goto error; + } + + unsigned int indexNode; + for (indexNode = 0; indexNode < san->cnt_sync_alerts; indexNode++) { + + WBXMLTreeNode *alert_node = (WBXMLTreeNode *) wbxml_list_get(alertnode_list, indexNode); + + char *alertData = NULL; + WBXMLTreeNode *alertdata_node; + if ((alertdata_node = wbxml_tree_node_elt_get_from_name(alert_node, "Data", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [Data]"); + goto error; + } + + if (alertdata_node->children != NULL && alertdata_node->children->type == WBXML_TREE_TEXT_NODE && alertdata_node->children->content != NULL) { + + alertData = (char *)wbxml_buffer_get_cstr(alertdata_node->children->content); + if (alertData == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL alertData detected. alertData MUST NOT be NULL."); + goto error; + } + + if (atoi(alertData) < 206 || atoi(alertData) > 210) { + _DEBUG_ERROR("[sanPackage11Parser] SAN doesn't support the sync type %d.", atoi(alertData)); + goto error; + } + + san->sync_alerts[indexNode].sync_type = atoi(alertData); + } + + char *alertURI = NULL; + unsigned alertURIlen = 0; + WBXMLTreeNode *alerturi_node; + if ((alerturi_node = wbxml_tree_node_elt_get_from_name(alert_node, "LocURI", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [LocURI]"); + goto error; + } + + if (alerturi_node->children != NULL && alerturi_node->children->type == WBXML_TREE_TEXT_NODE && alerturi_node->children->content != NULL) { + + alertURI = (char *)wbxml_buffer_get_cstr(alerturi_node->children->content); + alertURIlen = wbxml_buffer_len(alerturi_node->children->content); + if (alertURI == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL alertURI detected. alertURI MUST NOT be NULL."); + goto error; + } + + san->sync_alerts[indexNode].server_uri = (char *)calloc(alertURIlen, sizeof(char)); + if (san->sync_alerts[indexNode].server_uri == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] SAN package memory allocation fail. [serverURI]"); + goto error; + } + memcpy(san->sync_alerts[indexNode].server_uri, alertURI, alertURIlen); + } + + char *alertContentType = NULL; + unsigned alertContentTypeLen = 0; + WBXMLTreeNode *alertcontenttype_node; + if ((alertcontenttype_node = wbxml_tree_node_elt_get_from_name(alert_node, "Type", TRUE)) == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL from wbxml_tree_node_elt_get_from_name. [Type]"); + goto error; + } + + if (alertcontenttype_node->children != NULL && alertcontenttype_node->children->type == WBXML_TREE_TEXT_NODE && alertcontenttype_node->children->content != NULL) { + + alertContentType = (char *)wbxml_buffer_get_cstr(alertcontenttype_node->children->content); + alertContentTypeLen = wbxml_buffer_len(alertcontenttype_node->children->content); + + if (alertContentType == NULL) { + _DEBUG_ERROR("[sanPackage11Parser] NULL alertContentType detected. alertContentType MUST NOT be NULL."); + goto error; + } + + int j; + int cnt = (int)sizeof(contentTypeSupported) / sizeof(san_content_type_s); + bool isContentSupported = false; + + for (j = 0; j < cnt; j++) { + if (contentTypeSupported[j].strType == NULL) + continue; + if (strcmp(alertContentType, contentTypeSupported[j].strType) == 0) { + san->sync_alerts[indexNode].content_type = contentTypeSupported[j].strType; + isContentSupported = true; + break; + } + } + + if (!isContentSupported) { + _DEBUG_ERROR("[sanPackage11Parser] SAN doesn't support the content type %s.", alertContentType); + goto error; + } + } + } + } + + wbxml_tree_destroy(wbxml_tree); + + _EXTERN_FUNC_EXIT; + return san; + + error: + + sanPackageParserFree(san); + + _EXTERN_FUNC_EXIT; + return NULL; + +} + +void sanPackageParserFree(void *point) +{ + _EXTERN_FUNC_ENTER; + + san_package_s *san = (san_package_s *) point; + if (san != NULL) { + if (san->msg_body_without_digest != NULL) + free(san->msg_body_without_digest); + if (san->digest != NULL) + free(san->digest); + if (san->cred != NULL) { + if (san->cred->cred_format != NULL) + free(san->cred->cred_format); + if (san->cred->cred_auth != NULL) + free(san->cred->cred_auth); + if (san->cred->cred_data != NULL) + free(san->cred->cred_data); + free(san->cred); + } + if (san->server_id != NULL) + free(san->server_id); + if (san->sync_alerts != NULL) { + int i; + for (i = 0; i < san->cnt_sync_alerts; i++) { + if (san->sync_alerts[i].server_uri != NULL) { + free(san->sync_alerts[i].server_uri); + } + } + free(san->sync_alerts); + } + free(san); + } + + _EXTERN_FUNC_EXIT; +} + +void sanPrintMsg(san_package_s * san) +{ + _EXTERN_FUNC_ENTER; + + int i; + + _DEBUG_INFO("Printing SAN package ============================\n\n"); + + _DEBUG_INFO("MsgBody without Digest :\n\t"); + for (i = 0; i < san->msg_body_without_digest_length; i++) { + _DEBUG_INFO("%02x ", san->msg_body_without_digest[i]); + if ((i + 1) % 16 == 0) + _DEBUG_INFO("\n\t"); + } + _DEBUG_INFO("\n"); + + _DEBUG_INFO("Digest : %s\n", san->digest); + if (san->cred != NULL) { + if (san->cred->cred_format != NULL) + _DEBUG_INFO("Cred Format : %s\n", san->cred->cred_format); + if (san->cred->cred_auth != NULL) + _DEBUG_INFO("Cred Type : %s\n", san->cred->cred_auth); + if (san->cred->cred_data != NULL) + _DEBUG_INFO("Cred Data : %s\n", san->cred->cred_data); + } + _DEBUG_INFO("Version : %d\n", san->version); + _DEBUG_INFO("UI mode : %d\n", san->ui_mode); + _DEBUG_INFO("Initiator : %d\n", san->initiator); + _DEBUG_INFO("Session ID : %u\n", san->session_id); + _DEBUG_INFO("Server ID : %s\n", san->server_id); + _DEBUG_INFO("No. of Sync : %u\n", san->cnt_sync_alerts); + + for (i = 0; i < san->cnt_sync_alerts; i++) { + _DEBUG_INFO("\n\t=== Sync No. %d ============\n", i + 1); + _DEBUG_INFO("\tSync type : %d\n", san->sync_alerts[i].sync_type); + _DEBUG_INFO("\tContent type : %s\n", san->sync_alerts[i].content_type); + _DEBUG_INFO("\tServer URI : %s\n", san->sync_alerts[i].server_uri); + }; + + _EXTERN_FUNC_EXIT; +} + +int parse_ip_push_msg(char *data, san_package_s ** san_package, char **id) +{ + _EXTERN_FUNC_ENTER; + + retvm_if(data == NULL, 0, "data is NULL"); + + char *pay = NULL; + char *body = NULL; + char *decoded_body = NULL; + unsigned int decoded_size; + + *id = strtok(data, "|"); + pay = strtok(NULL, "|"); + body = strtok(NULL, "|"); + + _DEBUG_INFO("id = %s", *id); + _DEBUG_INFO("pay = %s", pay); + _DEBUG_INFO("after = %s", body); + + decoded_body = (char *)g_base64_decode(body, &decoded_size); + + _DEBUG_INFO("decoded_body = %s", decoded_body); + _DEBUG_INFO("decoded_size = %d", decoded_size); + + *san_package = san_package_12_parser(decoded_body, decoded_size); + + if ((*san_package) != NULL) { + _DEBUG_INFO("serverId = %s", (*san_package)->server_id); + _DEBUG_INFO("sessionID = %d", (*san_package)->session_id); + _DEBUG_INFO("cntSyncAlerts = %d", (*san_package)->cnt_sync_alerts); + int i; + for (i = 0; i < (*san_package)->cnt_sync_alerts; i++) { + _DEBUG_INFO("syncAlerts = %d", (*san_package)->sync_alerts[i].sync_type); + } + } else { + _DEBUG_ERROR("pSanPackage is NULL"); + goto error; + } + + _EXTERN_FUNC_EXIT; + return 1; + + error: + + _EXTERN_FUNC_EXIT; + return 0; +} diff --git a/src/agent/framework/task/oma_ds_engine_controller_task.c b/src/agent/framework/task/oma_ds_engine_controller_task.c new file mode 100755 index 0000000..ba7012d --- /dev/null +++ b/src/agent/framework/task/oma_ds_engine_controller_task.c @@ -0,0 +1,1137 @@ +/* + * 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. + */ + +/** + * @OMA_DS_Engine_Controller_Task.c + * @version 0.1 + * @brief This file is the source file of implementation of make and register task to Engine Controller + */ + +#include <string.h> + +#include <sync_agent.h> + +#include "framework/task/oma_ds_engine_controller_task.h" +#include "service-engine/se_account.h" +#include "service-engine/se_sync.h" +#include "service-engine/se_storage.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_COMMON" +#endif + +static GList *sync_request_id_list = NULL; + +static void _free_sync_categorie_list(void *list); +static void _free_delete_profile_list(void *list); +static void _free_resource_cp_list(void *list); + +static void _request_msg_info_free(request_msg_info_t * info); +static void *_string_copy_struct(void *string); +static request_msg_info_t *_create_request_msg_info(int sync_type, unsigned int request_id); + +static void __free_resource_cp(resource_cp_s * resource); + +static void _free_sync_categorie_list(void *list) +{ + _INNER_FUNC_ENTER; + retm_if(list == NULL, "list is NULL"); + + GList *categories = (GList *) list; + GList *iter = NULL; + for (iter = categories; iter != NULL; iter = g_list_next(iter)) + free_sync_service(iter->data); + + g_list_free(categories); + _INNER_FUNC_EXIT; +} + +static void _free_delete_profile_list(void *list) +{ + _INNER_FUNC_ENTER; + retm_if(list == NULL, "list is NULL"); + + GList *profiles = (GList *) list; + GList *iter = NULL; + for (iter = profiles; iter != NULL; iter = g_list_next(iter)) + free(iter->data); + + g_list_free(profiles); + _INNER_FUNC_EXIT; +} + +static void _free_resource_cp_list(void *list) +{ + _INNER_FUNC_ENTER; + retm_if(list == NULL, "list is NULL"); + + GList *profiles = (GList *) list; + GList *iter = NULL; + for (iter = profiles; iter != NULL; iter = g_list_next(iter)) + __free_resource_cp(iter->data); + + g_list_free(profiles); + _INNER_FUNC_EXIT; +} + +static void __free_resource_cp(resource_cp_s * resource) +{ + _INNER_FUNC_ENTER; + retm_if(resource == NULL, "resource is NULL"); + + if (resource->name != NULL) + free(resource->name); + + if (resource->accept != NULL) + free(resource->accept); + + if (resource->id != NULL) + free(resource->id); + + if (resource->password != NULL) + free(resource->password); + + if (resource->auth_type != NULL) + free(resource->auth_type); + + if (resource->auth_data != NULL) + free(resource->auth_data); + + free(resource); + _INNER_FUNC_EXIT; +} + +static request_msg_info_t *_create_request_msg_info(int sync_type, unsigned int request_id) +{ + _INNER_FUNC_ENTER; + request_msg_info_t *pInfo = (request_msg_info_t *) calloc(1, sizeof(request_msg_info_t)); + retvm_if(pInfo == NULL, NULL, "pInfo is NULL"); + + pInfo->sync_type = sync_type; + pInfo->request_id = request_id; + + _INNER_FUNC_EXIT; + return pInfo; +} + +static void _request_msg_info_free(request_msg_info_t * info) +{ + _INNER_FUNC_ENTER; + + if (info != NULL) + free(info); + + _INNER_FUNC_EXIT; +} + +static void *_string_copy_struct(void *string) +{ + _INNER_FUNC_ENTER; + + void *data = NULL; + data = strdup((const char *)string); + + _INNER_FUNC_EXIT; + return data; +} + +sync_agent_ec_task_spec_s *make_synchronize_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("accountId", true, false, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_structure("syncMode", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec3 = sync_agent_alloc_param_spec_structure("sanPackage", NULL, sanPackageParserFree, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *param_spec_array[3] = { pParam_spec1, pParam_spec2, pParam_spec3 }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("synchronize", + synchronize_task_process, NULL, NULL, + 3, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + sync_agent_unref_param_spec(pParam_spec3); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_add_profile_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_structure("profileDirName", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_structure("profileName", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec3 = sync_agent_alloc_param_spec_structure("addr", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec4 = sync_agent_alloc_param_spec_structure("id", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec5 = sync_agent_alloc_param_spec_structure("password", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec6 = sync_agent_alloc_param_spec_structure("syncMode", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec7 = sync_agent_alloc_param_spec_structure("syncType", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec8 = sync_agent_alloc_param_spec_structure("interval", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec9 = sync_agent_alloc_param_spec_structure("categories", NULL, _free_sync_categorie_list, + true, false, false, NULL, false, NULL, false, NULL); + + sync_agent_ec_param_spec_s *pParam_spec10 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec11 = sync_agent_alloc_param_spec_int("accountId", false, true, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *param_spec_array[11] = { pParam_spec1, pParam_spec2, pParam_spec3, pParam_spec4, pParam_spec5, + pParam_spec6, pParam_spec7, pParam_spec8, pParam_spec9, pParam_spec10, pParam_spec11 + }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("add_profile", + add_profile_task_process, NULL, NULL, + 11, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + sync_agent_unref_param_spec(pParam_spec3); + sync_agent_unref_param_spec(pParam_spec4); + sync_agent_unref_param_spec(pParam_spec5); + sync_agent_unref_param_spec(pParam_spec6); + sync_agent_unref_param_spec(pParam_spec7); + sync_agent_unref_param_spec(pParam_spec8); + sync_agent_unref_param_spec(pParam_spec9); + sync_agent_unref_param_spec(pParam_spec10); + sync_agent_unref_param_spec(pParam_spec11); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_edit_profile_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("accountId", true, false, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_structure("profileName", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec3 = sync_agent_alloc_param_spec_structure("addr", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec4 = sync_agent_alloc_param_spec_structure("id", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec5 = sync_agent_alloc_param_spec_structure("password", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec6 = sync_agent_alloc_param_spec_structure("syncMode", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec7 = sync_agent_alloc_param_spec_structure("syncType", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec8 = sync_agent_alloc_param_spec_structure("interval", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec9 = sync_agent_alloc_param_spec_structure("categories", NULL, _free_sync_categorie_list, + true, false, false, NULL, false, NULL, false, NULL); + + sync_agent_ec_param_spec_s *pParam_spec10 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *param_spec_array[10] = { pParam_spec1, pParam_spec2, pParam_spec3, pParam_spec4, pParam_spec5, + pParam_spec6, pParam_spec7, pParam_spec8, pParam_spec9, pParam_spec10 + }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("edit_profile", + edit_profile_task_process, NULL, NULL, + 10, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + sync_agent_unref_param_spec(pParam_spec3); + sync_agent_unref_param_spec(pParam_spec4); + sync_agent_unref_param_spec(pParam_spec5); + sync_agent_unref_param_spec(pParam_spec6); + sync_agent_unref_param_spec(pParam_spec7); + sync_agent_unref_param_spec(pParam_spec8); + sync_agent_unref_param_spec(pParam_spec9); + sync_agent_unref_param_spec(pParam_spec10); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_delete_profile_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_structure("profiles", NULL, _free_delete_profile_list, + true, false, false, NULL, false, NULL, false, NULL); + + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *param_spec_array[2] = { pParam_spec1, pParam_spec2 }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("delete_profile", + delete_profile_task_process, NULL, NULL, + 2, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_get_profile_data_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("accountId", true, false, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec3 = sync_agent_alloc_param_spec_structure("profileName", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec4 = sync_agent_alloc_param_spec_structure("addr", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec5 = sync_agent_alloc_param_spec_structure("id", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec6 = sync_agent_alloc_param_spec_structure("password", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec7 = sync_agent_alloc_param_spec_structure("syncMode", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec8 = sync_agent_alloc_param_spec_structure("syncType", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec9 = sync_agent_alloc_param_spec_structure("interval", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec10 = sync_agent_alloc_param_spec_int("lastSessionStatus", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec11 = sync_agent_alloc_param_spec_int("lastSessionTime", false, true, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *param_spec_array[11] = { pParam_spec1, pParam_spec2, pParam_spec3, + pParam_spec4, pParam_spec5, pParam_spec6, pParam_spec7, pParam_spec8, + pParam_spec9, pParam_spec10, pParam_spec11 + }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("get_profile_data", + get_profile_data_task_process, NULL, NULL, + 11, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + sync_agent_unref_param_spec(pParam_spec3); + sync_agent_unref_param_spec(pParam_spec4); + sync_agent_unref_param_spec(pParam_spec5); + sync_agent_unref_param_spec(pParam_spec6); + sync_agent_unref_param_spec(pParam_spec7); + sync_agent_unref_param_spec(pParam_spec8); + sync_agent_unref_param_spec(pParam_spec9); + sync_agent_unref_param_spec(pParam_spec10); + sync_agent_unref_param_spec(pParam_spec11); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_get_profile_sync_category_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("accountId", true, false, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_int("contentType", true, false, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *pParam_spec3 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec4 = sync_agent_alloc_param_spec_int("enabled", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec5 = sync_agent_alloc_param_spec_structure("srcURI", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec6 = sync_agent_alloc_param_spec_structure("tgtURI", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec7 = sync_agent_alloc_param_spec_structure("id", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec8 = sync_agent_alloc_param_spec_structure("password", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + + sync_agent_ec_param_spec_s *param_spec_array[8] = { pParam_spec1, pParam_spec2, pParam_spec3, pParam_spec4, + pParam_spec5, pParam_spec6, pParam_spec7, pParam_spec8 + }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("get_profile_sync_category", + get_profile_sync_category_task_process, NULL, NULL, + 8, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + sync_agent_unref_param_spec(pParam_spec3); + sync_agent_unref_param_spec(pParam_spec4); + sync_agent_unref_param_spec(pParam_spec5); + sync_agent_unref_param_spec(pParam_spec6); + sync_agent_unref_param_spec(pParam_spec7); + sync_agent_unref_param_spec(pParam_spec8); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_get_profile_statistics_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("accountId", true, false, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_int("contentType", true, false, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *pParam_spec3 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec4 = sync_agent_alloc_param_spec_structure("dbSynced", _string_copy_struct, free, + false, true, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec5 = sync_agent_alloc_param_spec_int("lastSessionTime", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec6 = sync_agent_alloc_param_spec_int("server2Client_Total", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec7 = sync_agent_alloc_param_spec_int("server2Client_NrOfAdd", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec8 = sync_agent_alloc_param_spec_int("server2Client_NrOfDelete", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec9 = sync_agent_alloc_param_spec_int("server2Client_NrOfReplace", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec10 = sync_agent_alloc_param_spec_int("client2Server_Total", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec11 = sync_agent_alloc_param_spec_int("client2Server_NrOfAdd", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec12 = sync_agent_alloc_param_spec_int("client2Server_NrOfDelete", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec13 = sync_agent_alloc_param_spec_int("client2Server_NrOfReplace", false, true, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *param_spec_array[13] = { pParam_spec1, pParam_spec2, pParam_spec3, + pParam_spec4, pParam_spec5, pParam_spec6, pParam_spec7, pParam_spec8, + pParam_spec9, pParam_spec10, pParam_spec11, pParam_spec12, pParam_spec13 + }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("get_profile_statistics", + get_profile_statistics_task_process, NULL, NULL, + 13, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + sync_agent_unref_param_spec(pParam_spec3); + sync_agent_unref_param_spec(pParam_spec4); + sync_agent_unref_param_spec(pParam_spec5); + sync_agent_unref_param_spec(pParam_spec6); + sync_agent_unref_param_spec(pParam_spec7); + sync_agent_unref_param_spec(pParam_spec8); + sync_agent_unref_param_spec(pParam_spec9); + sync_agent_unref_param_spec(pParam_spec10); + sync_agent_unref_param_spec(pParam_spec11); + sync_agent_unref_param_spec(pParam_spec12); + sync_agent_unref_param_spec(pParam_spec13); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_reset_synchronizing_profiles_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("reset_synchronizing_profiles", + reset_synchronizing_profiles_task_process, NULL, NULL, + 0, NULL); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_add_profile_cp_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_structure("profileName", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_structure("addr", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec3 = sync_agent_alloc_param_spec_structure("id", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec4 = sync_agent_alloc_param_spec_structure("password", _string_copy_struct, free, + true, false, false, NULL, false, NULL, false, NULL); + sync_agent_ec_param_spec_s *pParam_spec5 = sync_agent_alloc_param_spec_structure("categories", NULL, _free_resource_cp_list, + true, false, false, NULL, false, NULL, false, NULL); + + sync_agent_ec_param_spec_s *pParam_spec6 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec7 = sync_agent_alloc_param_spec_int("accountId", false, true, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *param_spec_array[7] = { pParam_spec1, pParam_spec2, pParam_spec3, pParam_spec4, pParam_spec5, + pParam_spec6, pParam_spec7 + }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("add_profile_cp", + add_profile_cp_task_process, NULL, NULL, + 7, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + sync_agent_unref_param_spec(pParam_spec2); + sync_agent_unref_param_spec(pParam_spec3); + sync_agent_unref_param_spec(pParam_spec4); + sync_agent_unref_param_spec(pParam_spec5); + sync_agent_unref_param_spec(pParam_spec6); + sync_agent_unref_param_spec(pParam_spec7); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_refresh_from_service_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("accountId", true, false, false, 0, 0, false, 0, false, 0, 0); + + sync_agent_ec_param_spec_s *param_spec_array[1] = { pParam_spec1 }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("refresh_from_service", + refresh_from_service_task_process, NULL, NULL, + 1, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_cancel_request_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("cancel_sync_request", + cancel_sync_request_task_process, NULL, NULL, + 0, NULL); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_reset_all_data_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *param_spec_array[1] = { pParam_spec1 }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("reset_all_data", + reset_all_data_task_process, NULL, NULL, + 1, param_spec_array); + + sync_agent_unref_param_spec(pParam_spec1); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_spec_s *make_get_all_profiles_data_task() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_param_spec_s *pParam_spec1 = sync_agent_alloc_param_spec_int("result", false, true, false, 0, 0, false, 0, false, 0, 0); + sync_agent_ec_param_spec_s *pParam_spec2 = sync_agent_alloc_param_spec_structure("profilesData", NULL, NULL, + false, true, false, NULL, false, NULL, false, NULL); + + sync_agent_ec_param_spec_s *param_spec_array[2] = { pParam_spec1, pParam_spec2 }; + + sync_agent_ec_task_spec_s *pTask_spec = sync_agent_alloc_simple_task_spec("get_all_profiles_data", + get_all_profiles_data_task_process, NULL, NULL, + 2, param_spec_array); + + _EXTERN_FUNC_EXIT; + return pTask_spec; +} + +sync_agent_ec_task_error_e synchronize_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + int accountId = 0; + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + retvm_if(accountId < 0, SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED, "account id = %d", accountId); + + char *syncMode = NULL; + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &syncMode); + + san_package_s *pSanPackage = NULL; + sync_agent_get_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &pSanPackage); + + _DEBUG_INFO("accountId = %d", accountId); + _DEBUG_INFO("syncMode = %s", syncMode); + + bool result = synchronize(accountId, syncMode, pSanPackage); + if (result == false) + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e add_profile_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + char *profileDirName = NULL; + char *profileName = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + char *syncMode = NULL; + char *syncType = NULL; + char *interval = NULL; + GList *categories = NULL; + int accountId; + + bool res; + int result; + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profileDirName); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profileName); + sync_agent_get_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &addr); + sync_agent_get_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &id); + sync_agent_get_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &password); + sync_agent_get_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &syncMode); + sync_agent_get_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &syncType); + sync_agent_get_param_value(&(param_array[7]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &interval); + sync_agent_get_param_value(&(param_array[8]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &categories); + + res = add_profile(profileDirName, profileName, addr, id, password, syncMode, syncType, interval, categories, &accountId); + if (res == false) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[9]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_set_param_value(&(param_array[10]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + + /*step 2 : start refresh_from_service for added account */ + void *in_param_value_array[1] = { &accountId }; + int in_param_index_array[1] = { 0 }; + sync_agent_ec_value_type_e in_param_value_type_array[1] = { SYNC_AGENT_EC_VALUE_TYPE_INT }; + unsigned int request_msg_id = 0; + sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REFRESH_FROM_SERVICE, 0, 1, in_param_index_array, in_param_value_type_array, in_param_value_array, NULL, NULL, (int *)&request_msg_id); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e edit_profile_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + int accountId; + char *profileName = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + char *syncMode = NULL; + char *syncType = NULL; + char *interval = NULL; + GList *categories = NULL; + + bool res; + int result; + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profileName); + sync_agent_get_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &addr); + sync_agent_get_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &id); + sync_agent_get_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &password); + sync_agent_get_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &syncMode); + sync_agent_get_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &syncType); + sync_agent_get_param_value(&(param_array[7]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &interval); + sync_agent_get_param_value(&(param_array[8]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &categories); + + res = edit_profile(accountId, profileName, addr, id, password, syncMode, syncType, interval, categories); + if (res == false) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[9]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e delete_profile_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + bool res; + int result; + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + GList *profiles = NULL; + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profiles); + + res = delete_profiles(profiles); + if (res == 0) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + + _EXTERN_FUNC_EXIT; + return err; + +} + +sync_agent_ec_task_error_e get_profile_data_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + int accountId = 0; + char *profileName = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + char *syncMode = NULL; + char *syncType = NULL; + char *interval = NULL; + int lastSessionStatus; + int lastSessionTime; + int result; + bool res; + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + retvm_if(accountId < 0, SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED, "account id = %d", accountId); + + res = get_profile_data(accountId, &profileName, &addr, &id, &password, &syncMode, &syncType, &interval, &lastSessionStatus, &lastSessionTime); + if (res == false) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_set_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profileName); + sync_agent_set_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &addr); + sync_agent_set_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &id); + sync_agent_set_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &password); + sync_agent_set_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &syncMode); + sync_agent_set_param_value(&(param_array[7]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &syncType); + sync_agent_set_param_value(&(param_array[8]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &interval); + sync_agent_set_param_value(&(param_array[9]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &lastSessionStatus); + sync_agent_set_param_value(&(param_array[10]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &lastSessionTime); + + _EXTERN_FUNC_EXIT; + return err; + +} + +sync_agent_ec_task_error_e get_profile_sync_category_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + int accountId = 0; + int contentType; + + int enabled = 0; + char *srcURI = NULL; + char *tgtURI = NULL; + char *id = NULL; + char *password = NULL; + bool res; + int result = 0; + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + retvm_if(accountId < 0, SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED, "account id = %d", accountId); + + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &contentType); + + res = get_profile_sync_category(accountId, contentType, &enabled, &srcURI, &tgtURI, &id, &password); + if (res == false) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_set_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &enabled); + sync_agent_set_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &srcURI); + sync_agent_set_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &tgtURI); + sync_agent_set_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &id); + sync_agent_set_param_value(&(param_array[7]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &password); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e get_profile_statistics_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + int accountId = 0; + int contentType; + + char *dbSynced = NULL; + int lastSessionTime = -1; + int server2Client_Total = 0; + int server2Client_NrOfAdd = 0; + int server2Client_NrOfDelete = 0; + int server2Client_NrOfReplace = 0; + int client2Server_Total = 0; + int client2Server_NrOfAdd = 0; + int client2Server_NrOfDelete = 0; + int client2Server_NrOfReplace = 0; + + bool res; + int result; + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + retvm_if(accountId < 0, SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED, "account id = %d", accountId); + + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &contentType); + + res = get_profile_statistics(accountId, contentType, &dbSynced, &lastSessionTime, + &server2Client_Total, &server2Client_NrOfAdd, &server2Client_NrOfDelete, &server2Client_NrOfReplace, &client2Server_Total, &client2Server_NrOfAdd, &client2Server_NrOfDelete, &client2Server_NrOfReplace); + + if (res == false) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_set_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &dbSynced); + sync_agent_set_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &lastSessionTime); + sync_agent_set_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_Total); + sync_agent_set_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_NrOfAdd); + sync_agent_set_param_value(&(param_array[7]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_NrOfDelete); + sync_agent_set_param_value(&(param_array[8]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &server2Client_NrOfReplace); + sync_agent_set_param_value(&(param_array[9]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_Total); + sync_agent_set_param_value(&(param_array[10]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_NrOfAdd); + sync_agent_set_param_value(&(param_array[11]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_NrOfDelete); + sync_agent_set_param_value(&(param_array[12]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &client2Server_NrOfReplace); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e reset_synchronizing_profiles_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + reset_synchronizing_profiles(); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e add_profile_cp_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + char *profileName = NULL; + char *addr = NULL; + char *id = NULL; + char *password = NULL; + GList *categories = NULL; + int accountId; + + bool res; + int result; + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profileName); + sync_agent_get_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &addr); + sync_agent_get_param_value(&(param_array[2]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &id); + sync_agent_get_param_value(&(param_array[3]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &password); + sync_agent_get_param_value(&(param_array[4]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &categories); + + res = add_profile_cp(profileName, addr, id, password, categories, &accountId); + if (res == false) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[5]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_set_param_value(&(param_array[6]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e refresh_from_service_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + int accountId; + bool res; + + sync_agent_get_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &accountId); + + res = refresh_from_service_all(accountId); + if (res == false) + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e cancel_sync_request_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + cancel_sync_request(); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e reset_all_data_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + int result = 1; + + /* delete all profiles */ + result = delete_all_profiles(); + + /* delete all agent default table */ + result = delete_all_agent_default_table(); + + sync_agent_set_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_ec_task_error_e get_all_profiles_data_task_process(unsigned int param_cnt, sync_agent_ec_param_param_s ** param_array) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_ec_task_error_e err = SYNC_AGENT_EC_TASK_ERROR_RUN_SUCCESS; + + bool res; + int result; + GList *profiles_data = NULL; + + res = get_all_profiles_data(&profiles_data); + + if (res == false) { + err = SYNC_AGENT_EC_TASK_ERROR_RUN_FAILED; + result = 0; + } else + result = 1; + + sync_agent_set_param_value(&(param_array[0]->param_value), SYNC_AGENT_EC_VALUE_TYPE_INT, &result); + sync_agent_set_param_value(&(param_array[1]->param_value), SYNC_AGENT_EC_VALUE_TYPE_STRUCT, &profiles_data); + + _EXTERN_FUNC_EXIT; + return err; + +} + +void insert_request_msg_info(int sync_type, unsigned int request_id) +{ + _EXTERN_FUNC_ENTER; + + sync_request_id_list = g_list_prepend(sync_request_id_list, _create_request_msg_info(sync_type, request_id)); + + _EXTERN_FUNC_EXIT; +} + +void delete_request_msg_info(unsigned int request_id) +{ + _EXTERN_FUNC_ENTER; + + GList *iter = NULL; + request_msg_info_t *pMsg_info = NULL; + + for (iter = sync_request_id_list; iter != NULL; iter = g_list_next(iter)) { + pMsg_info = iter->data; + + if (pMsg_info->request_id == request_id) { + sync_request_id_list = g_list_remove_link(sync_request_id_list, iter); + break; + } + } + + if (iter != NULL) { + _request_msg_info_free(iter->data); + g_list_free_1(iter); + } + _EXTERN_FUNC_EXIT; +} + +bool get_synchronizing_request_id(unsigned int *pRequest_id) +{ + _EXTERN_FUNC_ENTER; + + bool success = false; + request_msg_info_t *pMsg_info = NULL; + GList *iter = NULL; + + for (iter = sync_request_id_list; iter != NULL; iter = g_list_next(iter)) { + pMsg_info = iter->data; + + if (pMsg_info->sync_type == SYNC_MODE_MANUAL || pMsg_info->sync_type == SYNC_MODE_PERIODIC || pMsg_info->sync_type == SYNC_MODE_SAN) { + success = true; + *pRequest_id = pMsg_info->request_id; + + _request_msg_info_free(pMsg_info); + sync_request_id_list = g_list_delete_link(sync_request_id_list, iter); + + break; + } + } + + _EXTERN_FUNC_EXIT; + return success; +} + +bool get_manual_sync_request_id(unsigned int *pRequest_id) +{ + _EXTERN_FUNC_ENTER; + + bool success = false; + request_msg_info_t *pMsg_info = NULL; + GList *iter = NULL; + + for (iter = sync_request_id_list; iter != NULL; iter = g_list_next(iter)) { + pMsg_info = iter->data; + + if (pMsg_info->sync_type == SYNC_MODE_MANUAL) { + success = true; + *pRequest_id = pMsg_info->request_id; + + _request_msg_info_free(pMsg_info); + sync_request_id_list = g_list_delete_link(sync_request_id_list, iter); + break; + } + } + + _EXTERN_FUNC_EXIT; + return success; +} + +bool get_periodic_sync_request_id(unsigned int *pRequest_id) +{ + _EXTERN_FUNC_ENTER; + + bool success = false; + request_msg_info_t *pMsg_info = NULL; + GList *iter = NULL; + + for (iter = sync_request_id_list; iter != NULL; iter = g_list_next(iter)) { + pMsg_info = iter->data; + + if (pMsg_info->sync_type == SYNC_MODE_PERIODIC) { + success = true; + *pRequest_id = pMsg_info->request_id; + + _request_msg_info_free(pMsg_info); + sync_request_id_list = g_list_delete_link(sync_request_id_list, iter); + + break; + } + } + + _EXTERN_FUNC_EXIT; + return success; +} + +bool get_san_sync_request_id(unsigned int *pRequest_id) +{ + _EXTERN_FUNC_ENTER; + + bool success = false; + request_msg_info_t *pMsg_info = NULL; + GList *iter = NULL; + + for (iter = sync_request_id_list; iter != NULL; iter = g_list_next(iter)) { + pMsg_info = iter->data; + + if (pMsg_info->sync_type == SYNC_MODE_SAN) { + success = true; + *pRequest_id = pMsg_info->request_id; + + _request_msg_info_free(pMsg_info); + sync_request_id_list = g_list_delete_link(sync_request_id_list, iter); + + break; + } + } + + _EXTERN_FUNC_EXIT; + return success; +} + +bool get_auto_configure_request_id(unsigned int *pRequest_id) +{ + _EXTERN_FUNC_ENTER; + + bool success = false; + request_msg_info_t *pMsg_info = NULL; + GList *iter = NULL; + + for (iter = sync_request_id_list; iter != NULL; iter = g_list_next(iter)) { + pMsg_info = iter->data; + + if (pMsg_info->sync_type == SYNC_MODE_AUTOCONFIG) { + success = true; + *pRequest_id = pMsg_info->request_id; + + _request_msg_info_free(pMsg_info); + sync_request_id_list = g_list_delete_link(sync_request_id_list, iter); + + break; + } + } + + _EXTERN_FUNC_EXIT; + return success; +} + +bool cancel_current_sync_task() +{ + _EXTERN_FUNC_ENTER; + + unsigned int request_msg_id_to_cancel = 0; + unsigned int request_msg_id = 0; + bool has_autoconfig = get_auto_configure_request_id(&request_msg_id_to_cancel); + if (has_autoconfig) { + _DEBUG_INFO("has_autoconfig [%d] ", request_msg_id_to_cancel); + sync_agent_cancel_task(request_msg_id_to_cancel); + } else { + bool has_sync = get_synchronizing_request_id(&request_msg_id_to_cancel); + if (has_sync) { + _DEBUG_INFO("has_manual_sync [%d] ", request_msg_id_to_cancel); + sync_agent_cancel_task(request_msg_id_to_cancel); + + /*request network session cancel */ + sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_CANCEL_SYNC_REQUEST, 0, 0, NULL, NULL, NULL, NULL, NULL, (int *)&request_msg_id); + } else { + /* nothing to do */ + } + } + + _EXTERN_FUNC_EXIT; + return true; +} diff --git a/src/agent/main/main.c b/src/agent/main/main.c new file mode 100755 index 0000000..b6795de --- /dev/null +++ b/src/agent/main/main.c @@ -0,0 +1,395 @@ +/* + * 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. + */ + +/** + * @main.c + * @version 0.1 + * @brief This file is the source file of implementation of main function + */ + +typedef enum { + OMA_DS_ADD_PROFILE = 1, + OMA_DS_EDIT_PROFILE = 2, + OMA_DS_DELETE_PROFILE = 3, + OMA_DS_REQUEST_SYNC = 4, + OMA_DS_CANCEL_SYNC = 5, + OMA_DS_GET_PROFILE_DATA = 6, + OMA_DS_GET_PROFILE_SYNC_CATEGORY = 7, + OMA_DS_GET_PROFILE_SYNC_STATISTICS = 8, + OMA_DS_GET_ALL_PROFILES_DATA = 9, + OMA_DS_ADD_PROFILE_CP = 10, + OMA_DS_REQUEST_CALLLOG_SYNC = 11, + OMA_DS_RESET_ALL_DATA = 12 +} oma_ds_event_e; + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> + +#include <dbus/dbus-glib.h> +#include <dbus/dbus.h> + +#include <sync_agent.h> + +#include "framework/event/oma_ds_platform_event_handler.h" +#include "framework/event/oma_ds_event_handler.h" +#include "framework/task/oma_ds_engine_controller_task.h" +#include "common/csc_keys.h" +#include "common/common_define_internal.h" +#include "common/common_vconf.h" +#include "service-engine/se_account.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_MAIN" +#endif + +#define MC_SERVICE_OBJECT_PATH "/com/samsung/omadsagent" /* Don't use special character */ +#define MC_SERVICE_DBUS "com.samsung.omadsagent" +#define MC_SERVICE_INTERFACE "com.samsung.omadsagent" + +static DBusConnection *connection = NULL; + +static void _unregistered_path(DBusConnection * connection, void *user_data) +{ + /* connection was finalized */ + _INNER_FUNC_ENTER; + + _INNER_FUNC_EXIT; +} + +static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data) +{ + _INNER_FUNC_ENTER; + if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) { + DBusMessage *reply; + _DEBUG_INFO("Recived the HelloFromSelf message\n"); + + reply = dbus_message_new_method_return(message); + if (reply == NULL) + _DEBUG_ERROR("no memory\n"); + + if (!dbus_connection_send(connection, reply, NULL)) + _DEBUG_ERROR("no memory\n"); + } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Hello_Agent")) { + _DEBUG_INFO("Hello_Agent"); + } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Goodbye_Agent")) { + _DEBUG_INFO("Goodbye_Agent"); + _INNER_FUNC_EXIT; + exit(1); + } else { + _DEBUG_INFO("can't match message"); + } + + _INNER_FUNC_EXIT; + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static DBusObjectPathVTable echo_vtable = { + _unregistered_path, + _message_path, + NULL, +}; + +static int _register_dbus_service(void) +{ + _INNER_FUNC_ENTER; + int result; + DBusError error; + void *d; + + dbus_error_init(&error); + connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); + if (connection == NULL) { + _DEBUG_ERROR("*** Failed to open connection to activating message bus: %s\n", error.message); + dbus_error_free(&error); + return 1; + } + + if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) { + _DEBUG_ERROR("no memory\n"); + goto failed; + } + + if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) { + _DEBUG_ERROR("no memory\n"); + goto failed; + } + + if (d != (void *)0xdeadbeef) { + _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n"); + goto failed; + } + + result = dbus_bus_request_name(connection, MC_SERVICE_DBUS, 0, &error); + if (dbus_error_is_set(&error)) { + _DEBUG_ERROR("Error %s\n", error.message); + dbus_error_free(&error); + dbus_connection_unref(connection); + _INNER_FUNC_EXIT; + exit(1); + } + + _INNER_FUNC_EXIT; + return 0; + + failed: + dbus_connection_unref(connection); + _INNER_FUNC_EXIT; + return 1; +} + +//static void __update_csc() +//{ +// _INNER_FUNC_ENTER; +// +// char profile[128]; +// int numberOfProfiles = 0; +// char *contents = "check"; +// +// bool result = get_vconf_Int_key(CSC_VCONF_KEY_SYNCMLDS_NBDATASYNC, &numberOfProfiles); +// if (result == false) { +// _DEBUG_ERROR("failed in get_vconf_Int_key"); +// goto error; +// } +// +// int i; +// for (i = 1; i <= numberOfProfiles; i++) { +// snprintf(profile, sizeof(profile), "Sync%d", i); +// add_profile_csc(i); +// } +// +// sync_agent_write_whole_file(OMA_DS_CSC_CHECK_PATH, contents, strlen(contents), false); +// +//error: +// +// _INNER_FUNC_EXIT; +//} + +static void _request_reset_synchronizing_profiles() +{ + _INNER_FUNC_ENTER; + + unsigned int request_msg_id = 0; + sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_RESET_SYNCHRONIZING_PROFILES, 0, 0, NULL, NULL, NULL, NULL, NULL, (int *)&request_msg_id); + _INNER_FUNC_EXIT; +} + +static void _check_csc() +{ + _INNER_FUNC_ENTER; + + bool exist = false; + + /*FIXME(temporary do not process csc for ui reason) + check csc_check file is existed + if false do csc update + if true check csc xml file has been changed */ + + /*exist = sync_agent_is_existing_fs(OMA_DS_CSC_CHECK_PATH); */ + exist = true; + _DEBUG_TRACE("update = %d", exist); + + /* for prevent */ +// if (exist == false) +// __update_csc(); + + _INNER_FUNC_EXIT; +} + +static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context) +{ + _INNER_FUNC_ENTER; + + switch (signo) { + case SIGTERM: + _DEBUG_TRACE("Got SIGTERM"); + + /* stop gmain loop */ + sync_agent_stop_main_loop(0); + + break; + + default: + break; + } + + _INNER_FUNC_EXIT; +} + +int main() +{ + _EXTERN_FUNC_ENTER; + + struct sigaction sig_act; + + sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS; + sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS; + + if (sync_agent_daemonize() < 0) { + _DEBUG_ERROR("daemonize error"); + return -1; + } + + _register_dbus_service(); + + sync_agent_init_error_e init_error = sync_agent_init("/usr/share/oma-ds-cfg/omads_fw_config.xml"); + if (init_error != SYNC_AGENT_INIT_SUCCESS) { + _DEBUG_ERROR("Failed to sync_agent_init() : %d", init_error); + } else { + _DEBUG_INFO("done init_Framework"); + } + + sync_agent_ec_task_spec_s *pSyncTaskSpec = make_synchronize_task(); + sync_agent_ec_task_spec_s *pAddProfileTaskSpec = make_add_profile_task(); + sync_agent_ec_task_spec_s *pEditProfileTaskSpec = make_edit_profile_task(); + sync_agent_ec_task_spec_s *pDeleteProfileTaskSpec = make_delete_profile_task(); + sync_agent_ec_task_spec_s *pGetProfileDataTaskSpec = make_get_profile_data_task(); + sync_agent_ec_task_spec_s *pGetProfileSyncCategoryTaskSpec = make_get_profile_sync_category_task(); + sync_agent_ec_task_spec_s *pGetProfileStatisticsTaskSpec = make_get_profile_statistics_task(); + sync_agent_ec_task_spec_s *pResetSynchronizingProfilesTaskSpec = make_reset_synchronizing_profiles_task(); + sync_agent_ec_task_spec_s *pAddProfileCPTaskSpec = make_add_profile_cp_task(); + sync_agent_ec_task_spec_s *pRefreshFromServiceTaskSpec = make_refresh_from_service_task(); + sync_agent_ec_task_spec_s *pCancelSyncRequestTaskSpec = make_cancel_request_task(); + sync_agent_ec_task_spec_s *pResetAllDataTaskSpec = make_reset_all_data_task(); + sync_agent_ec_task_spec_s *pGetAllProfilesData = make_get_all_profiles_data_task(); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_REQUEST, EC_MSG_TYPE_NAME_SYNC_TASK_REQUEST, pSyncTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_ADD_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_ADD_PROFILE, pAddProfileTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_EDIT_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_EDIT_PROFILE, pEditProfileTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_DELETE_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_DELETE_PROFILE, pDeleteProfileTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_DATA, EC_MSG_TYPE_NAME_SYNC_TASK_GET_PROFILE_DATA, pGetProfileDataTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_SYNC_CATEGORY, EC_MSG_TYPE_NAME_SYNC_TASK_GET_PROFILE_SYNC_CATEGORY, pGetProfileSyncCategoryTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_STATISTICS, EC_MSG_TYPE_NAME_SYNC_TASK_GET_PROFILE_STATISTICS, pGetProfileStatisticsTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_RESET_SYNCHRONIZING_PROFILES, EC_MSG_TYPE_NAME_SYNC_TASK_RESET_SYNCHRONIZING_PROFILES, pResetSynchronizingProfilesTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_ADD_PROFILE_CP, EC_MSG_TYPE_NAME_SYNC_TASK_ADD_PROFILE_CP, pAddProfileCPTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_REFRESH_FROM_SERVICE, EC_MSG_TYPE_NAME_SYNC_TASK_REFRESH_FROM_SERVICE, pRefreshFromServiceTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_CANCEL_SYNC_REQUEST, EC_MSG_TYPE_NAME_SYNC_TASK_CANCEL_SYNC_REQUEST, pCancelSyncRequestTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_RESET_ALL_DATA, EC_MSG_TYPE_NAME_SYNC_TASK_RESET_ALL_DATA, pResetAllDataTaskSpec, NULL); + + sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_GET_ALL_PROFILES_DATA, EC_MSG_TYPE_NAME_SYNC_TASK_GET_ALL_PROFILES_DATA, pGetAllProfilesData, NULL); + + sync_agent_ec_queuing_rule_spec_s *pQueuing_rule = sync_agent_create_queuing_rule_spec_outline("sync_queueing_rule"); + + sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_REFRESH_FROM_SERVICE, pSyncTaskSpec, 0); + + sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_REQUEST, pSyncTaskSpec, 0); + + sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_RESET_ALL_DATA, pSyncTaskSpec, 0); + + sync_agent_register_async_queuing_rule_spec(pQueuing_rule, NULL, NULL); + + sync_agent_unref_queuing_rule_spec(pQueuing_rule); + + _request_reset_synchronizing_profiles(); /*reset synchronizing flag 0 every account & construct item tbl */ + + error = sync_agent_set_event_callback(OMA_DS_ADD_PROFILE, event_callback_add_profile_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_EDIT_PROFILE, event_callback_edit_profile_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_DELETE_PROFILE, event_callback_delete_profile_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_REQUEST_SYNC, event_callback_request_sync_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_CANCEL_SYNC, event_callback_cancel_sync_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_GET_PROFILE_DATA, event_callback_get_profile_data_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_GET_PROFILE_SYNC_CATEGORY, event_callback_get_profile_sync_category_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_GET_PROFILE_SYNC_STATISTICS, event_callback_get_profile_last_statistics_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_GET_ALL_PROFILES_DATA, event_callback_request_get_all_profiles_data); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_ADD_PROFILE_CP, event_callback_add_profile_cp_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_REQUEST_CALLLOG_SYNC, event_callback_request_calllog_sync_async); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + error = sync_agent_set_event_callback(OMA_DS_RESET_ALL_DATA, event_callback_request_reset_all_data_sync); + if (error != SYNC_AGENT_EVENT_SUCCESS) + _DEBUG_ERROR("failed sync_agent_set_event_callback()"); + + sync_agent_pm_return_e san_err = sync_agnet_register_user_callback(3, NULL, 1, san_callback_parse); + if (san_err != SYNC_AGENT_PM_SUCCESS) + _DEBUG_ERROR("failed in sync_agnet_register_user_callback"); + + sync_agent_pm_return_e scheduler_err = sync_agnet_register_user_callback(1, NULL, 1, send_periodic_sync_msg); + if (scheduler_err != SYNC_AGENT_PM_SUCCESS) + _DEBUG_ERROR("failed in sync_agnet_register_user_callback"); + + /*register profiles from csc */ + _check_csc(); + + /* Block SIGPIPE signal (client may close socket abnormally) */ + signal(SIGPIPE, SIG_IGN); + + /* register signal handler. will be called by kill command */ + sig_act.sa_handler = NULL; + sig_act.sa_sigaction = _agent_daemon_signal_handler; + sig_act.sa_flags = SA_SIGINFO; + sigemptyset(&sig_act.sa_mask); + sigaction(SIGTERM, &sig_act, NULL); + + sync_agent_run_main_loop(0); + + if (connection) { + dbus_connection_unref(connection); + connection = NULL; + } + + deinit_error = sync_agent_deinit(); + if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) + _DEBUG_ERROR("failed in sync_agent_deinit"); + + _EXTERN_FUNC_EXIT; + + return 0; +} diff --git a/src/agent/service-adapter/network-binder/na_binder.c b/src/agent/service-adapter/network-binder/na_binder.c new file mode 100755 index 0000000..4c9ff49 --- /dev/null +++ b/src/agent/service-adapter/network-binder/na_binder.c @@ -0,0 +1,142 @@ +/* + * 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. + */ + +/** + * @NA_Binder.c + * @version 0.1 + * @brief This file is the source file of implementation of header binder function for Network Adapter + */ + +#include <sync_agent.h> + +#include "service-adapter/network-binder/na_binder.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SA" +#endif + +int na_http_header_binder(char *acc_type, char *loc_uri, bool is_xml, char *jsession_id, GList ** list) +{ + _EXTERN_FUNC_ENTER; + + retvm_if(acc_type == NULL, 0, "[NA Header Binder] accType is NULL"); + retvm_if(loc_uri == NULL, 0, "[NA Header Binder] loc_uri is NULL"); + + if (!strcmp(acc_type, "OMA_DS")) { + sync_agent_na_common_header_info_s *method = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(method == NULL, 0, "[NA Header Binder] 'method' malloc fail !!"); + + memset(method, 0x00, sizeof(sync_agent_na_common_header_info_s)); + method->key = "method"; + method->value = "post"; + *list = g_list_append(*list, method); + + sync_agent_na_common_header_info_s *uri = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(uri == NULL, 0, "[NA Header Binder] 'uri' malloc fail !!"); + + memset(uri, 0x00, sizeof(sync_agent_na_common_header_info_s)); + uri->key = "uri"; + uri->value = loc_uri; + *list = g_list_append(*list, uri); + + sync_agent_na_common_header_info_s *cache_control = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(cache_control == NULL, 0, "[NA Header Binder] 'cache_control' malloc fail !!"); + + memset(cache_control, 0x00, sizeof(sync_agent_na_common_header_info_s)); + cache_control->key = "Cache-Control"; + cache_control->value = "no-store, private"; + *list = g_list_append(*list, cache_control); + + sync_agent_na_common_header_info_s *connection = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(connection == NULL, 0, "[NA Header Binder] 'connection' malloc fail !!"); + + memset(connection, 0x00, sizeof(sync_agent_na_common_header_info_s)); + connection->key = "Connection"; + connection->value = "Keep-Alive"; + *list = g_list_append(*list, connection); + + sync_agent_na_common_header_info_s *accept = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(accept == NULL, 0, "[NA Header Binder] 'accept' malloc fail !!"); + + memset(accept, 0x00, sizeof(sync_agent_na_common_header_info_s)); + accept->key = "Accept"; + if (is_xml == true) + accept->value = "application/vnd.syncml+xml"; + else + accept->value = "application/vnd.syncml+wbxml"; + *list = g_list_append(*list, accept); + + sync_agent_na_common_header_info_s *accept_language = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(accept_language == NULL, 0, "[NA Header Binder] 'accept_language' malloc fail !!"); + + memset(accept_language, 0x00, sizeof(sync_agent_na_common_header_info_s)); + accept_language->key = "Accept-Language"; + accept_language->value = "en-US"; + *list = g_list_append(*list, accept_language); + + sync_agent_na_common_header_info_s *accept_charset = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(accept_charset == NULL, 0, "[NA Header Binder] 'accept_charset' malloc fail !!"); + + memset(accept_charset, 0x00, sizeof(sync_agent_na_common_header_info_s)); + accept_charset->key = "Accept-Charset"; + accept_charset->value = "UTF-8"; + *list = g_list_append(*list, accept_charset); + + sync_agent_na_common_header_info_s *content_type = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(content_type == NULL, 0, "[NA Header Binder] 'content_type' malloc fail !!"); + + memset(content_type, 0x00, sizeof(sync_agent_na_common_header_info_s)); + content_type->key = "Content-Type"; + if (is_xml == true) + content_type->value = "application/vnd.syncml+xml"; + else + content_type->value = "application/vnd.syncml+wbxml"; + *list = g_list_append(*list, content_type); + + if (jsession_id != NULL) { + _DEBUG_INFO(" jsessionid : %s\n", jsession_id); + sync_agent_na_common_header_info_s *cookie = (sync_agent_na_common_header_info_s *) malloc(sizeof(sync_agent_na_common_header_info_s)); + retvm_if(cookie == NULL, 0, "[NA Header Binder] 'cookie' malloc fail !!"); + + memset(cookie, 0x00, sizeof(sync_agent_na_common_header_info_s)); + cookie->key = "cookie"; + cookie->value = jsession_id; + *list = g_list_append(*list, cookie); + _DEBUG_INFO(" cookie : %s\n", cookie->value); + } + } + + GList *iter = NULL; + for (iter = *list; iter != NULL; iter = g_list_next(iter)) { + _DEBUG_INFO("[NA_Header_Binder] key : %s, value : %s", ((sync_agent_na_common_header_info_s *) (iter->data))->key, ((sync_agent_na_common_header_info_s *) (iter->data))->value); + } + + _EXTERN_FUNC_EXIT; + return 1; +} + +void free_header_info(sync_agent_na_common_header_info_s * common_header_info) +{ + _EXTERN_FUNC_ENTER; + + retm_if(common_header_info == NULL, "pCommon_header_info is NULL"); + + free(common_header_info); + + _EXTERN_FUNC_EXIT; +} diff --git a/src/agent/service-adapter/protocol-binder/oma_ds_protocol_binder.c b/src/agent/service-adapter/protocol-binder/oma_ds_protocol_binder.c new file mode 100755 index 0000000..b6b14db --- /dev/null +++ b/src/agent/service-adapter/protocol-binder/oma_ds_protocol_binder.c @@ -0,0 +1,5256 @@ +/* + * 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. + */ + +/** + * @oma_ds_protocol_binder.c + * @version 0.1 + * @brief This file is the source file of implementation of callback function for Protocol Binder + */ + +#include <glib.h> + +#include <sync_agent.h> + +#include "service-adapter/protocol-binder/oma_ds_protocol_binder.h" +#include "service-adapter/sa_elements_internal.h" +#include "service-adapter/sa_command_internal.h" +#include "service-adapter/sa_devinf_internal.h" +#include "service-adapter/sa_command.h" +#include "service-adapter/sa_elements.h" +#include "service-adapter/sa_devinf.h" +#include "service-adapter/sa_define.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_BINDER" +#endif + +static sync_agent_pb_error_e __append_location_to_wbxml_node(location_s * location, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node); + +static sync_agent_pb_error_e __append_anchor_to_wbxml_node(anchor_s * anchor, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node); + +static sync_agent_pb_error_e __append_devinf_contenttype_to_wbxml_node(devinf_content_type_s * devinf_content_type, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, char *node); + +static sync_agent_pb_error_e __append_cred_to_wbxml_node(cred_s * cred, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node); + +static sync_agent_pb_error_e _oma_ds_binder_devinf_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_syncml_start_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_header_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_final_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_body_start_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_status_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_alert_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_results_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_put_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_get_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_sync_start_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_sync_end_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_add_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_replace_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_delete_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); +static sync_agent_pb_error_e _oma_ds_binder_map_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node); + +static protocol_version_e __get_oma_ds_protocol_version(char *verdtd); +static devinf_version_e __get_oma_ds_devInf_version(char *verdtd); +static devinf_devtyp_e __get_oma_ds_devInf_device_type(char *dev_type); +static devinf_content_type_s *__get_devinf_contenttype(WBXMLTreeNode * node); +static location_s *_get_location(WBXMLTreeNode * node); +static anchor_s *_get_anchor(WBXMLTreeNode * node); +static cred_s *__get_cred(WBXMLTreeNode * node); +static chal_s *_get_chal(WBXMLTreeNode * node); + +static sync_agent_pb_error_e _oma_ds_binder_devinf_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_header_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_results_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_put_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_get_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_alert_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_sync_end_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_sync_start_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_add_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_replace_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_delete_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_final_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); +static sync_agent_pb_error_e _oma_ds_binder_status_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content); + +/* + PE_ADD, + PE_DELETE, + PE_REPLACE, + PE_ALERT, + PE_GET, + PE_MAP, + PE_PUT, + PE_RESULTS, + PE_STATUS, + PE_SYNC_START, + PE_SYNC_END, + PE_HEADER, + PE_FINAL + + PE_COPY, + PE_EXEC, + PE_SEARCH, + PE_SEQUENCE_START, + PE_SEQUENCE_END, + PE_ATOMIC_START, + PE_ATOMIC_END, + PE_PUT_GET, + PE_CMD_GROUP, + PE_GENERIC, + */ +const sync_agent_pb_function_info_s oma_ds_binder_function_info[] = { + {PE_SYNCML_START, "SyncML", _oma_ds_binder_syncml_start_converter_function, NULL, false}, + {PE_SYNCML_END, "/SyncML", NULL, NULL, false}, + {PE_HEADER, "SyncHdr", _oma_ds_binder_header_converter_function, _oma_ds_binder_header_reverse_converter_function, true}, + {PE_BODY_START, "SyncBody", _oma_ds_binder_body_start_converter_function, NULL, false}, + {PE_BODY_END, "/SyncBody", NULL, NULL, false}, + {PE_FINAL, "Final", _oma_ds_binder_final_converter_function, _oma_ds_binder_final_reverse_converter_function, true}, + {PE_ALERT, "Alert", _oma_ds_binder_alert_command_converter_function, _oma_ds_binder_alert_command_reverse_converter_function, true}, + {PE_STATUS, "Status", _oma_ds_binder_status_converter_function, _oma_ds_binder_status_reverse_converter_function, true}, + {PE_RESULTS_START, "Results", _oma_ds_binder_results_command_converter_function, _oma_ds_binder_results_command_reverse_converter_function, false}, + {PE_RESULTS_END, "/Results", NULL, NULL, false}, + {PE_PUT_START, "Put", _oma_ds_binder_put_command_converter_function, _oma_ds_binder_put_command_reverse_converter_function, false}, + {PE_PUT_END, "/Put", NULL, NULL, false}, + {PE_GET, "Get", _oma_ds_binder_get_command_converter_function, _oma_ds_binder_get_command_reverse_converter_function, true}, + {PE_SYNC_START, "Sync", _oma_ds_binder_sync_start_command_converter_function, _oma_ds_binder_sync_start_command_reverse_converter_function, false}, + {PE_SYNC_END, "/Sync", _oma_ds_binder_sync_end_command_converter_function, _oma_ds_binder_sync_end_command_reverse_converter_function, false}, + {PE_ADD, "Add", _oma_ds_binder_add_command_converter_function, _oma_ds_binder_add_command_reverse_converter_function, true}, + {PE_REPLACE, "Replace", _oma_ds_binder_replace_command_converter_function, _oma_ds_binder_replace_command_reverse_converter_function, true}, + {PE_DELETE, "Delete", _oma_ds_binder_delete_command_converter_function, _oma_ds_binder_delete_command_reverse_converter_function, true}, + {PE_MAP, "Map", _oma_ds_binder_map_command_converter_function, NULL, true}, + {PE_DEVINF, "DevInf", _oma_ds_binder_devinf_converter_function, _oma_ds_binder_devinf_reverse_converter_function, true} +}; + +static sync_agent_pb_error_e __append_location_to_wbxml_node(location_s * location, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(location == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "location is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + if (location->loc_uri != NULL) { + WBXMLTreeNode *LocURI = sync_agent_add_child_text_node(binder, + parent_node, ELEMENT_LOCURI, location->loc_uri, strlen(location->loc_uri)); + if (LocURI == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } else { + /* error case */ + } + + if (location->loc_name != NULL) { + WBXMLTreeNode *locName = sync_agent_add_child_text_node(binder, + parent_node, ELEMENT_LOCNAME, location->loc_name, strlen(location->loc_name)); + if (locName == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e __append_anchor_to_wbxml_node(anchor_s * anchor, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(anchor == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "anchor is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, parent_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *anchor_node = sync_agent_add_child_node(binder, meta_node, ELEMENT_ANCHOR); + if (anchor_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (anchor->last_anchor != NULL) { + WBXMLTreeNode *last_node = sync_agent_add_child_text_node(binder, + anchor_node, ELEMENT_LAST, anchor->last_anchor, strlen(anchor->last_anchor)); + if (last_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (anchor->next_anchor != NULL) { + WBXMLTreeNode *next_node = sync_agent_add_child_text_node(binder, + anchor_node, ELEMENT_NEXT, anchor->next_anchor, strlen(anchor->next_anchor)); + if (next_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e __append_devinf_contenttype_to_wbxml_node(devinf_content_type_s * devinf_content_type, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, char *node) +{ + _INNER_FUNC_ENTER; + + retvm_if(devinf_content_type == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "devinf_content_type is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + WBXMLTreeNode *xnode = sync_agent_add_child_node(binder, parent_node, node); + if (xnode == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cttype_node = sync_agent_add_child_text_node(binder, + xnode, "CTType", devinf_content_type->ct_type, strlen(devinf_content_type->ct_type)); + if (cttype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *verct_node = sync_agent_add_child_text_node(binder, + xnode, "VerCT", devinf_content_type->verct, strlen(devinf_content_type->verct)); + if (verct_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + error: + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e __append_cred_to_wbxml_node(cred_s * cred, sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(cred == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "cred is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* check validity */ + WBXMLTreeNode *cred_node = sync_agent_add_child_node(binder, parent_node, ELEMENT_CRED); + if (cred_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, cred_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *type_node = NULL; + + switch (cred->type) { + case AUTH_TYPE_BASIC: + + if (cred->format == FORMAT_TYPE_BASE64) { + WBXMLTreeNode *format_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_FORMAT, ELEMENT_FORMAT_BASE64, strlen(ELEMENT_FORMAT_BASE64)); + if (format_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + type_node = sync_agent_add_child_text_node(binder, meta_node, ELEMENT_TYPE, ELEMENT_AUTH_BASIC, strlen(ELEMENT_AUTH_BASIC)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (cred->data != NULL) { + WBXMLTreeNode *data_node = sync_agent_add_child_text_node(binder, + cred_node, ELEMENT_DATA, cred->data, strlen(cred->data)); + if (data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + break; + case AUTH_TYPE_MD5: + + if (cred->format == FORMAT_TYPE_BASE64) { + WBXMLTreeNode *format_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_FORMAT, ELEMENT_FORMAT_BASE64, strlen(ELEMENT_FORMAT_BASE64)); + if (format_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + type_node = sync_agent_add_child_text_node(binder, meta_node, ELEMENT_TYPE, ELEMENT_AUTH_MD5, strlen(ELEMENT_AUTH_MD5)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (cred->data != NULL) { + WBXMLTreeNode *data_node = sync_agent_add_child_text_node(binder, + cred_node, ELEMENT_DATA, cred->data, strlen(cred->data)); + if (data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + break; + default: + break; + } + + error: + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_devinf_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + item_s *pItem = (item_s *) content; + devinf_s *pDevInf = pItem->private.devinf; + + if (pItem != NULL) { + WBXMLTreeNode *item_node = sync_agent_create_node(binder, ELEMENT_ITEM); + if (item_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, item_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pItem->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) + goto error; + + WBXMLTreeNode *data_node = sync_agent_add_child_node(binder, item_node, ELEMENT_DATA); + if (data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + /* devinf language setting */ + sync_agent_switch_protocol(binder, SYNC_AGENT_PB_PROTOCOL_SYNCML_DEVINF12); + WBXMLTree *devinf_tree = sync_agent_create_wbxml_tree(binder); + if (devinf_tree == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + sync_agent_add_wbxml_tree_to_wbxml_node(data_node, devinf_tree); + + /* create root node of devinf tree */ + WBXMLTreeNode *devinf_node = sync_agent_create_node(binder, ELEMENT_DEVINF); + if (devinf_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + sync_agent_set_wbxml_tree_root(devinf_tree, devinf_node); + + WBXMLTreeNode *verDTD_node = NULL; + switch (pDevInf->version) { + case DEVINF_VERSION_UNKNOWN: + verDTD_node = sync_agent_add_child_node(binder, devinf_node, ELEMENT_VERDTD); + break; + case DEVINF_VERSION_10: + verDTD_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_VERDTD, ELEMENT_VERDRD_10, strlen(ELEMENT_VERDRD_10)); + break; + case DEVINF_VERSION_11: + verDTD_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_VERDTD, ELEMENT_VERDRD_11, strlen(ELEMENT_VERDRD_11)); + break; + case DEVINF_VERSION_12: + verDTD_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_VERDTD, ELEMENT_VERDRD_12, strlen(ELEMENT_VERDRD_12)); + break; + } + + if (verDTD_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *man_node = sync_agent_add_child_text_node(binder, + devinf_node, ELEMENT_MAN, pDevInf->manufacturer, strlen(pDevInf->manufacturer)); + if (man_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *mod_node = sync_agent_add_child_text_node(binder, + devinf_node, ELEMENT_MOD, pDevInf->model, strlen(pDevInf->model)); + if (mod_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pDevInf->oem != NULL) { + WBXMLTreeNode *oem_node = sync_agent_add_child_text_node(binder, + devinf_node, ELEMENT_OEM, pDevInf->oem, strlen(pDevInf->oem)); + if (oem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + WBXMLTreeNode *FwV_node = sync_agent_add_child_text_node(binder, + devinf_node, ELEMENT_FWV, pDevInf->firmware_version, strlen(pDevInf->firmware_version)); + if (FwV_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *SwV_node = sync_agent_add_child_text_node(binder, + devinf_node, ELEMENT_SWV, pDevInf->software_version, strlen(pDevInf->software_version)); + if (SwV_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *HwV_node = sync_agent_add_child_text_node(binder, + devinf_node, ELEMENT_HWV, pDevInf->hardware_version, strlen(pDevInf->hardware_version)); + if (HwV_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *devID_node = sync_agent_add_child_text_node(binder, + devinf_node, ELEMENT_DEVID, pDevInf->dev_id, strlen(pDevInf->dev_id)); + if (devID_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *devTyp_node = NULL; + switch (pDevInf->devtyp) { + case DEVINF_DEVTYPE_UNKNOWN: + devTyp_node = sync_agent_add_child_node(binder, devinf_node, ELEMENT_DEVTYP); + break; + case DEVINF_DEVTYPE_PAGER: + devTyp_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_DEVTYP, ELEMENT_PAGER, strlen(ELEMENT_PAGER)); + break; + case DEVINF_DEVTYPE_HANDHELD: + devTyp_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_DEVTYP, ELEMENT_HANDHELD, strlen(ELEMENT_HANDHELD)); + break; + case DEVINF_DEVTYPE_PDA: + devTyp_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_DEVTYP, ELEMENT_PDA, strlen(ELEMENT_PDA)); + break; + case DEVINF_DEVTYPE_PHONE: + devTyp_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_DEVTYP, ELEMENT_PHONE, strlen(ELEMENT_PHONE)); + break; + case DEVINF_DEVTYPE_SMARTPHONE: + devTyp_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_DEVTYP, ELEMENT_SMARTPHONE, strlen(ELEMENT_SMARTPHONE)); + break; + case DEVINF_DEVTYPE_SERVER: + devTyp_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_DEVTYP, ELEMENT_SERVER, strlen(ELEMENT_SERVER)); + break; + case DEVINF_DEVTYPE_WORKSTATION: + devTyp_node = sync_agent_add_child_text_node(binder, devinf_node, ELEMENT_DEVTYP, ELEMENT_WORKSTATION, strlen(ELEMENT_WORKSTATION)); + break; + } + + if (devTyp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pDevInf->supports_utc) { + WBXMLTreeNode *supportUTC_node = sync_agent_add_child_node(binder, + devinf_node, ELEMENT_UTC); + if (supportUTC_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pDevInf->supports_large_objs) { + WBXMLTreeNode *supportLargeObjs_node = sync_agent_add_child_node(binder, + devinf_node, ELEMENT_SUPPORTLARGEOBJS); + if (supportLargeObjs_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pDevInf->supports_number_of_changes) { + WBXMLTreeNode *supportNumberOfChanges_node = sync_agent_add_child_node(binder, + devinf_node, ELEMENT_SUPPORTNUMBEROFCHANGES); + if (supportNumberOfChanges_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + GList *datastoreIter = NULL; + devinf_datastore_s *devInfDataStore = NULL; + for (datastoreIter = pDevInf->datastores; datastoreIter != NULL; datastoreIter = g_list_next(datastoreIter)) { + devInfDataStore = datastoreIter->data; + + WBXMLTreeNode *datastore_node = sync_agent_add_child_node(binder, + devinf_node, ELEMENT_DATASTORE); + if (datastore_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *sourceRef_node = sync_agent_add_child_text_node(binder, + datastore_node, ELEMENT_SOURCEREF, devInfDataStore->source_ref, strlen(devInfDataStore->source_ref)); + if (sourceRef_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (devInfDataStore->display_name != NULL) { + WBXMLTreeNode *displayname_node = sync_agent_add_child_text_node(binder, + datastore_node, ELEMENT_DISPLAYNAME, devInfDataStore->display_name, strlen(devInfDataStore->display_name)); + if (displayname_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfDataStore->max_guid_size) { + char *str_maxguidsize = g_strdup_printf("%u", devInfDataStore->max_guid_size); + if (str_maxguidsize == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *maxguidsize_node = sync_agent_add_child_text_node(binder, + datastore_node, ELEMENT_MAXGUIDSIZE, str_maxguidsize, strlen(str_maxguidsize)); + + if (str_maxguidsize != NULL) + free(str_maxguidsize); + + if (maxguidsize_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + __append_devinf_contenttype_to_wbxml_node(devInfDataStore->rx_pref, binder, datastore_node, ELEMENT_RX_PREF); + if (err != SYNC_AGENT_PB_RETURN_OK) + goto error; + + GList *rxIter = NULL; + devinf_content_type_s *devInfContentType = NULL; + for (rxIter = devInfDataStore->rx; rxIter != NULL; rxIter = g_list_next(rxIter)) { + devInfContentType = rxIter->data; + __append_devinf_contenttype_to_wbxml_node(devInfContentType, binder, datastore_node, ELEMENT_RX); + if (err != SYNC_AGENT_PB_RETURN_OK) + goto error; + } + + __append_devinf_contenttype_to_wbxml_node(devInfDataStore->tx_pref, binder, datastore_node, ELEMENT_TX_PREF); + if (err != SYNC_AGENT_PB_RETURN_OK) + goto error; + + GList *txIter = NULL; + for (txIter = devInfDataStore->tx; txIter != NULL; txIter = g_list_next(txIter)) { + devInfContentType = txIter->data; + __append_devinf_contenttype_to_wbxml_node(devInfContentType, binder, datastore_node, ELEMENT_TX); + if (err != SYNC_AGENT_PB_RETURN_OK) + goto error; + + } + + GList *ctcapIter = NULL; + devinf_ct_cap_s *devInfCTCap = NULL; + for (ctcapIter = devInfDataStore->ct_caps; ctcapIter != NULL; ctcapIter = g_list_next(ctcapIter)) { + devInfCTCap = ctcapIter->data; + + WBXMLTreeNode *ctcap_node = sync_agent_add_child_node(binder, + datastore_node, ELEMENT_CTCAP); + if (ctcap_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cttype_node = sync_agent_add_child_text_node(binder, + ctcap_node, ELEMENT_CTTYPE, devInfCTCap->ct->ct_type, strlen(devInfCTCap->ct->ct_type)); + if (cttype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *verct_node = sync_agent_add_child_text_node(binder, + ctcap_node, ELEMENT_VERCT, devInfCTCap->ct->verct, strlen(devInfCTCap->ct->verct)); + if (verct_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + /*TODO FieldLevel */ + + GList *propertyIter = NULL; + devinf_property_s *devInfProperty = NULL; + for (propertyIter = devInfCTCap->properties; propertyIter != NULL; propertyIter = g_list_next(propertyIter)) { + devInfProperty = propertyIter->data; + + WBXMLTreeNode *property_node = sync_agent_add_child_node(binder, + ctcap_node, ELEMENT_PROPERTY); + if (property_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *propname_node = sync_agent_add_child_text_node(binder, + property_node, ELEMENT_PROPNAME, devInfProperty->prop_name, strlen(devInfProperty->prop_name)); + if (propname_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (devInfProperty->data_type != NULL) { + WBXMLTreeNode *datatype_node = sync_agent_add_child_text_node(binder, + property_node, ELEMENT_DATATYPE, devInfProperty->data_type, strlen(devInfProperty->data_type)); + if (datatype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfProperty->max_occur) { + char *str_maxoccur = g_strdup_printf("%u", devInfProperty->max_occur); + if (str_maxoccur == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *maxoccur_node = sync_agent_add_child_text_node(binder, + property_node, ELEMENT_MAXOCCUR, str_maxoccur, strlen(str_maxoccur)); + + if (str_maxoccur != NULL) + free(str_maxoccur); + + if (maxoccur_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfProperty->max_size) { + char *str_maxsize = g_strdup_printf("%u", devInfProperty->max_size); + if (str_maxsize == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *maxsize_node = sync_agent_add_child_text_node(binder, + property_node, ELEMENT_MAXSIZE, str_maxsize, strlen(str_maxsize)); + + if (str_maxsize != NULL) + free(str_maxsize); + + if (maxsize_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfProperty->no_truncate) { + WBXMLTreeNode *notruncate_node = sync_agent_add_child_node(binder, + property_node, ELEMENT_NOTRUNCATE); + if (notruncate_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + GList *valenum = NULL; + char *val = NULL; + for (valenum = devInfProperty->val_enums; valenum != NULL; valenum = g_list_next(valenum)) { + val = valenum->data; + WBXMLTreeNode *valenum_node = sync_agent_add_child_text_node(binder, + property_node, ELEMENT_VALENUM, val, strlen(val)); + if (valenum_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfProperty->display_name != NULL) { + WBXMLTreeNode *displayname_node = sync_agent_add_child_text_node(binder, + property_node, ELEMENT_DISPLAYNAME, devInfProperty->display_name, strlen(devInfProperty->display_name)); + if (displayname_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + GList *propparamIter = NULL; + devinf_prop_param_s *devInfPropParam = NULL; + for (propparamIter = devInfProperty->prop_params; propparamIter != NULL; propparamIter = g_list_next(propparamIter)) { + devInfPropParam = propparamIter->data; + + WBXMLTreeNode *propparam_node = sync_agent_add_child_node(binder, + property_node, ELEMENT_PROPPARAM); + if (propparam_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *paramname_node = sync_agent_add_child_text_node(binder, + propparam_node, ELEMENT_PARAMNAME, devInfPropParam->param_name, strlen(devInfPropParam->param_name)); + if (paramname_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (devInfPropParam->data_type != NULL) { + WBXMLTreeNode *datatype_node = sync_agent_add_child_text_node(binder, + propparam_node, ELEMENT_DATATYPE, devInfPropParam->data_type, strlen(devInfPropParam->data_type)); + if (datatype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + valenum = NULL; + val = NULL; + for (valenum = devInfPropParam->val_enums; valenum != NULL; valenum = g_list_next(valenum)) { + val = valenum->data; + WBXMLTreeNode *valenum_node = sync_agent_add_child_text_node(binder, + propparam_node, ELEMENT_VALENUM, val, strlen(val)); + if (valenum_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfPropParam->display_name != NULL) { + WBXMLTreeNode *displayname_node = sync_agent_add_child_text_node(binder, + propparam_node, ELEMENT_DISPLAYNAME, devInfPropParam->display_name, strlen(devInfPropParam->display_name)); + if (displayname_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + } + } + + /* DSMem */ + WBXMLTreeNode *dsmem_node = sync_agent_add_child_node(binder, + datastore_node, ELEMENT_DSMEM); + if (dsmem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (devInfDataStore->shared_mem) { + WBXMLTreeNode *sharedmem_node = sync_agent_add_child_node(binder, + dsmem_node, ELEMENT_SHAREDMEM); + if (sharedmem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfDataStore->max_mem) { + char *str_maxmem = g_strdup_printf("%u", devInfDataStore->max_mem); + WBXMLTreeNode *maxmem_node = sync_agent_add_child_text_node(binder, + dsmem_node, ELEMENT_MAXMEM, str_maxmem, strlen(str_maxmem)); + + if (str_maxmem != NULL) + free(str_maxmem); + + if (maxmem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (devInfDataStore->max_id) { + char *str_maxid = g_strdup_printf("%u", devInfDataStore->max_id); + WBXMLTreeNode *maxid_node = sync_agent_add_child_text_node(binder, + dsmem_node, ELEMENT_MAXID, str_maxid, strlen(str_maxid)); + + if (str_maxid != NULL) + free(str_maxid); + + if (maxid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + /* SupportHierarchicalSync */ + if (devInfDataStore->supports_hierarchical_sync) { + WBXMLTreeNode *supportshierarchicalsync_node = sync_agent_add_child_node(binder, + datastore_node, ELEMENT_SUPPORTHIERARCHICALSYNC); + if (supportshierarchicalsync_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + WBXMLTreeNode *synccap_node = sync_agent_add_child_node(binder, + datastore_node, ELEMENT_SYNCCAP); + if (synccap_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (get_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_TWO_WAY)) { + WBXMLTreeNode *synctype_node = sync_agent_add_child_text_node(binder, + synccap_node, ELEMENT_SYNCTYPE, ELEMENT_SYNCTYPE_1, strlen(ELEMENT_SYNCTYPE_1)); + if (synctype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + if (get_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_SLOW_SYNC)) { + WBXMLTreeNode *synctype_node = sync_agent_add_child_text_node(binder, + synccap_node, ELEMENT_SYNCTYPE, ELEMENT_SYNCTYPE_2, strlen(ELEMENT_SYNCTYPE_2)); + if (synctype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + if (get_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_ONE_WAY_FROM_CLIENT)) { + WBXMLTreeNode *synctype_node = sync_agent_add_child_text_node(binder, + synccap_node, ELEMENT_SYNCTYPE, ELEMENT_SYNCTYPE_3, strlen(ELEMENT_SYNCTYPE_3)); + if (synctype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + if (get_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_REFRESH_FROM_CLIENT)) { + WBXMLTreeNode *synctype_node = sync_agent_add_child_text_node(binder, + synccap_node, ELEMENT_SYNCTYPE, ELEMENT_SYNCTYPE_4, strlen(ELEMENT_SYNCTYPE_4)); + if (synctype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + if (get_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_ONE_WAY_FROM_SERVER)) { + WBXMLTreeNode *synctype_node = sync_agent_add_child_text_node(binder, + synccap_node, ELEMENT_SYNCTYPE, ELEMENT_SYNCTYPE_5, strlen(ELEMENT_SYNCTYPE_5)); + if (synctype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + if (get_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_REFRESH_FROM_SERVER)) { + WBXMLTreeNode *synctype_node = sync_agent_add_child_text_node(binder, + synccap_node, ELEMENT_SYNCTYPE, ELEMENT_SYNCTYPE_6, strlen(ELEMENT_SYNCTYPE_6)); + if (synctype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + if (get_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_SERVER_ALERTED_SYNC)) { + WBXMLTreeNode *synctype_node = sync_agent_add_child_text_node(binder, + synccap_node, ELEMENT_SYNCTYPE, ELEMENT_SYNCTYPE_7, strlen(ELEMENT_SYNCTYPE_7)); + if (synctype_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + /*TODO filter-rx, filterCap */ + } + sync_agent_undo_switch_protocol(binder); + *wbxml_dom_node = item_node; + } + + error: + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_syncml_start_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + WBXMLTreeNode *syncml_node = sync_agent_create_node(binder, ELEMENT_SYNCML); + if (syncml_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + *wbxml_dom_node = syncml_node; + + error: + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_header_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + sync_hdr_s *pSyncHdr = (sync_hdr_s *) content; + + /* add SyncHdr, VerDTD, VerProto node */ + WBXMLTreeNode *synchdr_node = sync_agent_create_node(binder, ELEMENT_SYNCHDR); + if (synchdr_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + char *verDTD = ELEMENT_VERDTD; + char *verProto = ELEMENT_VERPROTO; + + char *verDTD_data = NULL; + char *verProto_data = NULL; + if (pSyncHdr->version == VERSION_10) { + verDTD_data = ELEMENT_VERDRD_10; + verProto_data = ELEMENT_VERPROTO_10; + } else if (pSyncHdr->version == VERSION_11) { + verDTD_data = ELEMENT_VERDRD_11; + verProto_data = ELEMENT_VERPROTO_11; + } else if (pSyncHdr->version == VERSION_12) { + verDTD_data = ELEMENT_VERDRD_12; + verProto_data = ELEMENT_VERPROTO_12; + } + + if (verDTD_data == NULL || verProto_data == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + WBXMLTreeNode *verdtd_node = sync_agent_add_child_text_node(binder, + synchdr_node, verDTD, verDTD_data, strlen(verDTD_data)); + if (verdtd_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *verProto_node = sync_agent_add_child_text_node(binder, + synchdr_node, verProto, verProto_data, strlen(verProto_data)); + if (verProto_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + /* session ID */ + WBXMLTreeNode *sessionID_node = NULL; + if (pSyncHdr->session_id != NULL) + sessionID_node = sync_agent_add_child_text_node(binder, synchdr_node, ELEMENT_SESSIONID, pSyncHdr->session_id, strlen(pSyncHdr->session_id)); + else + sessionID_node = sync_agent_add_child_node(binder, synchdr_node, ELEMENT_SESSIONID); + + if (sessionID_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + /* msgID */ + char *msgID_data = g_strdup_printf("%u", pSyncHdr->message_id); + if (msgID_data == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *msgID_node = sync_agent_add_child_text_node(binder, + synchdr_node, ELEMENT_MSGID, msgID_data, strlen(msgID_data)); + + if (msgID_data != NULL) + free(msgID_data); + + if (msgID_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + synchdr_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pSyncHdr->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, + synchdr_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pSyncHdr->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + if (pSyncHdr->response_uri != NULL) { + WBXMLTreeNode *respuri_node = sync_agent_add_child_text_node(binder, + synchdr_node, ELEMENT_RESPURI, pSyncHdr->response_uri, strlen(pSyncHdr->response_uri)); + if (respuri_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pSyncHdr->no_response) { + WBXMLTreeNode *noResponse_node = sync_agent_add_child_node(binder, + synchdr_node, ELEMENT_NORESP); + if (noResponse_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pSyncHdr->cred != NULL) { + err = __append_cred_to_wbxml_node(pSyncHdr->cred, binder, synchdr_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + char *meta = ELEMENT_META; + if (pSyncHdr->max_msg_size != 0 || pSyncHdr->max_obj_size != 0) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, + synchdr_node, meta); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + unsigned int maxmsgsize = pSyncHdr->max_msg_size; + unsigned int maxobjsize = pSyncHdr->max_obj_size; + + if (maxmsgsize != 0) { + char *str_maxmsgsize = g_strdup_printf("%u", maxmsgsize); + if (str_maxmsgsize == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *maxmsgsize_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_MAXMSGSIZE, str_maxmsgsize, strlen(str_maxmsgsize)); + if (str_maxmsgsize != NULL) + free(str_maxmsgsize); + + if (maxmsgsize_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (maxobjsize != 0) { + char *str_maxobjsize = g_strdup_printf("%u", maxobjsize); + if (str_maxobjsize == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *maxobjsize_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_MAXOBJSIZE, str_maxobjsize, strlen(str_maxobjsize)); + if (str_maxobjsize != NULL) + free(str_maxobjsize); + + if (maxobjsize_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + + *wbxml_dom_node = synchdr_node; + synchdr_node = NULL; + + error: + + if (synchdr_node != NULL) + sync_agent_destroy_wbxml_node(synchdr_node); + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_final_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + WBXMLTreeNode *final_node = sync_agent_create_node(binder, "Final"); + if (final_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + *wbxml_dom_node = final_node; + + error: + _INNER_FUNC_EXIT; + return err; + +} + +static sync_agent_pb_error_e _oma_ds_binder_body_start_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + WBXMLTreeNode *syncbody_node = sync_agent_create_node(binder, ELEMENT_SYNCBODY); + if (syncbody_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + *wbxml_dom_node = syncbody_node; + + error: + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_status_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + status_s *pStatus = (status_s *) content; + + command_type_e type = pStatus->type; + + char *str_cmdID = NULL; + char *str_msgRef = NULL; + char *str_cmdRef = NULL; + + WBXMLTreeNode *status_node = sync_agent_create_node(binder, ELEMENT_STATUS); + if (status_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + unsigned int cmdID = pStatus->cmd_id; + str_cmdID = g_strdup_printf("%u", cmdID); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + status_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + unsigned int msgRef = pStatus->msg_ref; + str_msgRef = g_strdup_printf("%u", msgRef); + if (str_msgRef == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *msgref_node = sync_agent_add_child_text_node(binder, + status_node, ELEMENT_MSGREF, str_msgRef, strlen(str_msgRef)); + if (msgref_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + unsigned int cmdRef = pStatus->cmd_ref; + str_cmdRef = g_strdup_printf("%u", cmdRef); + if (str_cmdRef == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdref_node = sync_agent_add_child_text_node(binder, + status_node, ELEMENT_CMDREF, str_cmdRef, strlen(str_cmdRef)); + if (cmdref_node == NULL) { + goto error; + } + + char *str_command = NULL; + switch (type) { + case COMMAND_TYPE_ALERT: + { + str_command = ELEMENT_ALERT; + } + break; + case COMMAND_TYPE_SYNC_START: + { + str_command = ELEMENT_SYNC; + } + break; + case COMMAND_TYPE_PUT: + { + str_command = ELEMENT_PUT; + } + break; + case COMMAND_TYPE_HEADER: + { + str_command = ELEMENT_SYNCHDR; + } + break; + case COMMAND_TYPE_ADD: + { + str_command = ELEMENT_ADD; + } + break; + case COMMAND_TYPE_REPLACE: + { + str_command = ELEMENT_REPLACE; + } + break; + case COMMAND_TYPE_DELETE: + { + str_command = ELEMENT_DELETE; + } + break; + case COMMAND_TYPE_MAP: + { + str_command = ELEMENT_MAP; + } + break; + case COMMAND_TYPE_GET: + { + str_command = ELEMENT_GET; + } + break; + case COMMAND_TYPE_RESULTS: + { + str_command = ELEMENT_RESULTS; + } + break; + default: + break; + } + + if (str_command == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + WBXMLTreeNode *command_node = sync_agent_add_child_text_node(binder, + status_node, ELEMENT_CMD, str_command, strlen(str_command)); + if (command_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pStatus->target_ref != NULL) { + WBXMLTreeNode *targetref_node = sync_agent_add_child_text_node(binder, + status_node, ELEMENT_TARGETREF, pStatus->target_ref->loc_uri, strlen(pStatus->target_ref->loc_uri)); + if (targetref_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pStatus->source_ref != NULL) { + WBXMLTreeNode *sourceref_node = sync_agent_add_child_text_node(binder, + status_node, ELEMENT_SOURCEREF, pStatus->source_ref->loc_uri, strlen(pStatus->source_ref->loc_uri)); + if (sourceref_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pStatus->cred != NULL) { + err = __append_cred_to_wbxml_node(pStatus->cred, binder, status_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (pStatus->chal != NULL) { + /*TODO chal */ + } + + WBXMLTreeNode *data_node = sync_agent_add_child_text_node(binder, + status_node, ELEMENT_DATA, pStatus->data, strlen(pStatus->data)); + if (data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (type == COMMAND_TYPE_ALERT) { + if (pStatus->item != NULL) { + WBXMLTreeNode *item_node = sync_agent_add_child_node(binder, + status_node, ELEMENT_ITEM); + if (item_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *item_data_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_DATA); + if (item_data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *item_data_Anchor_node = sync_agent_add_child_node(binder, + item_data_node, ELEMENT_ANCHOR); + if (item_data_Anchor_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pStatus->item->anchor != NULL) { + WBXMLTreeNode *item_data_Anchor_Next_node = sync_agent_add_child_text_node(binder, + item_data_Anchor_node, ELEMENT_NEXT, pStatus->item->anchor->next_anchor, + strlen(pStatus->item->anchor->next_anchor)); + + if (item_data_Anchor_Next_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + } + + *wbxml_dom_node = status_node; + status_node = NULL; + + error: + + if (status_node != NULL) + sync_agent_destroy_wbxml_node(status_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + if (str_cmdRef != NULL) + free(str_cmdRef); + + if (str_msgRef != NULL) + free(str_msgRef); + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_alert_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + char *str_cmdID = NULL; + char *str_alert_type = NULL; + + WBXMLTreeNode *alert_node = NULL; + + command_s *pCommand = (command_s *) content; + + command_alert_private_s *pAlert = (command_alert_private_s *) (&(pCommand->private)); + if (pAlert == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + if (pAlert->type == ALERT_UNKNOWN) { + /* error case */ + err = SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER; + goto error; + } + + alert_node = sync_agent_create_node(binder, ELEMENT_ALERT); + if (alert_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + alert_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->no_resp) { + WBXMLTreeNode *noResp_node = sync_agent_add_child_node(binder, + alert_node, ELEMENT_NORESP); + if (noResp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, alert_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + str_alert_type = g_strdup_printf("%u", pAlert->type); + if (str_alert_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *data_node = sync_agent_add_child_text_node(binder, + alert_node, ELEMENT_DATA, str_alert_type, strlen(str_alert_type)); + if (data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + /* item */ + if (pCommand->source != NULL && pCommand->target != NULL) { + WBXMLTreeNode *item_node = sync_agent_add_child_node(binder, + alert_node, ELEMENT_ITEM); + if (item_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pCommand->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pCommand->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + if (pAlert->anchor != NULL) { + err = __append_anchor_to_wbxml_node(pAlert->anchor, binder, item_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + } + + *wbxml_dom_node = alert_node; + alert_node = NULL; + + error: + + if (alert_node != NULL) + sync_agent_destroy_wbxml_node(alert_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + if (str_alert_type != NULL) + free(str_alert_type); + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_results_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + char *str_cmdID = NULL; + char *str_cmdRef = NULL; + char *str_msgRef = NULL; + WBXMLTreeNode *results_node = NULL; + + command_s *pCommand = (command_s *) content; + command_results_private_s *pResults = (command_results_private_s *) (&(pCommand->private)); + if (pResults == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + if (pResults->item == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + char *content_type = (pResults->item)->content_type; + if (content_type == NULL) { + /* error case */ + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + results_node = sync_agent_create_node(binder, ELEMENT_RESULTS); + if (results_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID != NULL) { + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + results_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pResults->msg_ref) { + str_msgRef = g_strdup_printf("%u", pResults->msg_ref); + if (str_msgRef == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *msgref_node = sync_agent_add_child_text_node(binder, + results_node, ELEMENT_MSGREF, str_msgRef, strlen(str_msgRef)); + if (msgref_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + str_cmdRef = g_strdup_printf("%u", pResults->cmd_ref); + if (str_cmdRef == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *msgref_node = sync_agent_add_child_text_node(binder, + results_node, ELEMENT_CMDREF, str_cmdRef, strlen(str_cmdRef)); + if (msgref_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (content_type != NULL) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, + results_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *type_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_TYPE, content_type, strlen(content_type)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pResults->target_ref != NULL) { + WBXMLTreeNode *targetref_node = sync_agent_add_child_text_node(binder, + results_node, ELEMENT_TARGETREF, pResults->target_ref->loc_uri, strlen(pResults->target_ref->loc_uri)); + if (targetref_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + *wbxml_dom_node = results_node; + results_node = NULL; + + error: + + if (results_node != NULL) + sync_agent_destroy_wbxml_node(results_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + if (str_cmdRef != NULL) + free(str_cmdRef); + + if (str_msgRef != NULL) + free(str_msgRef); + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_put_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + char *str_cmdID = NULL; + WBXMLTreeNode *put_node = NULL; + + command_s *pCommand = (command_s *) content; + command_access_private_s *pAccess = (command_access_private_s *) (&(pCommand->private)); + if (pAccess == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + char *content_type = (pAccess->item)->content_type; + if (content_type == NULL) { + /* error case */ + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + put_node = sync_agent_create_node(binder, ELEMENT_PUT); + if (put_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + put_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->no_resp) { + WBXMLTreeNode *noResp_node = sync_agent_add_child_node(binder, + put_node, ELEMENT_NORESP); + if (noResp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, put_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (content_type != NULL) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, + put_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *type_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_TYPE, content_type, strlen(content_type)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + *wbxml_dom_node = put_node; + put_node = NULL; + + error: + + if (put_node != NULL) + sync_agent_destroy_wbxml_node(put_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_get_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + char *str_cmdID = NULL; + WBXMLTreeNode *get_node = NULL; + + command_s *pCommand = (command_s *) content; + command_access_private_s *pAccess = (command_access_private_s *) (&(pCommand->private)); + if (pAccess == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + if (pAccess->item == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + char *content_type = (pAccess->item)->content_type; + if (content_type == NULL) { + /* error case */ + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + get_node = sync_agent_create_node(binder, ELEMENT_GET); + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + get_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->no_resp) { + WBXMLTreeNode *noResp_node = sync_agent_add_child_node(binder, + get_node, ELEMENT_NORESP); + if (noResp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, get_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (content_type != NULL) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, + get_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *type_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_TYPE, content_type, strlen(content_type)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pAccess->item != NULL) { + WBXMLTreeNode *item_node = sync_agent_add_child_node(binder, + get_node, ELEMENT_ITEM); + if (item_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pAccess->item->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + *wbxml_dom_node = get_node; + get_node = NULL; + + error: + + if (get_node != NULL) + sync_agent_destroy_wbxml_node(get_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_sync_start_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + command_s *pCommand = (command_s *) content; + + char *str_cmdID = NULL; + char *numChanged_str = NULL; + char *str_freemem = NULL; + char *str_freeid = NULL; + + WBXMLTreeNode *sync_node = sync_agent_create_node(binder, ELEMENT_SYNC); + if (sync_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + sync_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->no_resp) { + WBXMLTreeNode *noResp_node = sync_agent_add_child_node(binder, + sync_node, ELEMENT_NORESP); + if (noResp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, sync_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (pCommand->source != NULL && pCommand->target != NULL) { + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + sync_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pCommand->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, + sync_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pCommand->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + } + + /* Mem */ + if (pCommand->private.sync.mem != NULL) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, sync_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *mem_node = sync_agent_add_child_node(binder, meta_node, ELEMENT_MEM); + if (mem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->private.sync.mem->sharedmem) { + WBXMLTreeNode *sharedmem_node = sync_agent_add_child_node(binder, mem_node, ELEMENT_SHAREDMEM); + if (sharedmem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->private.sync.mem->free_mem) { + str_freemem = g_strdup_printf("%u", pCommand->private.sync.mem->free_mem); + WBXMLTreeNode *freemem_node = sync_agent_add_child_text_node(binder, + mem_node, ELEMENT_FREEMEM, str_freemem, strlen(str_freemem)); + if (freemem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->private.sync.mem->free_id) { + str_freeid = g_strdup_printf("%u", pCommand->private.sync.mem->free_id); + WBXMLTreeNode *freeid_node = sync_agent_add_child_text_node(binder, + mem_node, ELEMENT_FREEID, str_freeid, strlen(str_freeid)); + if (freeid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + + if (pCommand->private.sync.has_num_changed) { + numChanged_str = g_strdup_printf("%u", pCommand->private.sync.num_changed); + if (numChanged_str == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *numberOfChanges_node = sync_agent_add_child_text_node(binder, + sync_node, ELEMENT_NUMBEROFCHANGES, numChanged_str, strlen(numChanged_str)); + if (numberOfChanges_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + *wbxml_dom_node = sync_node; + sync_node = NULL; + + error: + + if (sync_node != NULL) + sync_agent_destroy_wbxml_node(sync_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + if (numChanged_str != NULL) + free(numChanged_str); + + if (str_freemem != NULL) + free(str_freemem); + + _INNER_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_sync_end_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + _EXTERN_FUNC_EXIT; + + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_add_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + char *str_cmdID = NULL; + char *str_size = NULL; + WBXMLTreeNode *add_node = NULL; + + command_s *pCommand = (command_s *) content; + + command_change_private_s *pChange = (command_change_private_s *) (&(pCommand->private)); + if (pChange == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + add_node = sync_agent_create_node(binder, ELEMENT_ADD); + if (add_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + add_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->no_resp) { + WBXMLTreeNode *noResp_node = sync_agent_add_child_node(binder, + add_node, ELEMENT_NORESP); + if (noResp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, add_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + GList *Iter = NULL; + item_s *item = NULL; + for (Iter = pChange->items; Iter != NULL; Iter = g_list_next(Iter)) { + item = (Iter->data); + + WBXMLTreeNode *item_node = sync_agent_add_child_node(binder, + add_node, ELEMENT_ITEM); + if (item_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (item->source != NULL) { + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(item->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (item->target != NULL) { + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(item->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (item->content_type != NULL) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *type_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_TYPE, item->content_type, strlen(item->content_type)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (item->more_data) { + if (item->size) { + str_size = g_strdup_printf("%u", item->size); + if (str_size == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *size_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_SIZE, str_size, strlen(str_size)); + if (size_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + } + + if (item->data_type == ITEM_DATA) { + if (item->private.data != NULL) { + WBXMLTreeNode *data_node = sync_agent_add_child_cdata_node(binder, + item_node, ELEMENT_DATA, item->private.data, strlen(item->private.data)); + if (data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + + if (item->more_data) { + WBXMLTreeNode *moredata_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_MOREDATA); + if (moredata_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + + *wbxml_dom_node = add_node; + add_node = NULL; + + error: + + if (add_node != NULL) + sync_agent_destroy_wbxml_node(add_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + if (str_size != NULL) + free(str_size); + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_replace_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + char *str_cmdID = NULL; + char *str_size = NULL; + WBXMLTreeNode *replace_node = NULL; + + command_s *pCommand = (command_s *) content; + + command_change_private_s *pChange = (command_change_private_s *) (&(pCommand->private)); + + if (pChange == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + replace_node = sync_agent_create_node(binder, ELEMENT_REPLACE); + if (replace_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + replace_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->no_resp) { + WBXMLTreeNode *noResp_node = sync_agent_add_child_node(binder, + replace_node, ELEMENT_NORESP); + if (noResp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, replace_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + GList *Iter = NULL; + item_s *item = NULL; + for (Iter = pChange->items; Iter != NULL; Iter = g_list_next(Iter)) { + item = (Iter->data); + + WBXMLTreeNode *item_node = sync_agent_add_child_node(binder, + replace_node, ELEMENT_ITEM); + if (item_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (item->source != NULL) { + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + err = __append_location_to_wbxml_node(item->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (item->target != NULL) { + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + err = __append_location_to_wbxml_node(item->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (item->content_type != NULL) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *type_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_TYPE, item->content_type, strlen(item->content_type)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (item->more_data) { + if (item->size) { + str_size = g_strdup_printf("%u", item->size); + if (str_size == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *size_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_SIZE, str_size, strlen(str_size)); + if (size_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + } + + if (item->data_type == ITEM_DATA) { + if (item->private.data != NULL) { + WBXMLTreeNode *data_node = sync_agent_add_child_cdata_node(binder, + item_node, ELEMENT_DATA, item->private.data, strlen(item->private.data)); + if (data_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + + if (item->more_data) { + WBXMLTreeNode *moredata_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_MOREDATA); + if (moredata_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + } + + *wbxml_dom_node = replace_node; + replace_node = NULL; + + error: + + if (replace_node != NULL) + sync_agent_destroy_wbxml_node(replace_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + if (str_size != NULL) + free(str_size); + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_delete_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + char *str_cmdID = NULL; + char *str_size = NULL; + WBXMLTreeNode *delete_node = NULL; + + command_s *pCommand = (command_s *) content; + + command_change_private_s *pChange = (command_change_private_s *) (&(pCommand->private)); + if (pChange == NULL) { + err = SYNC_AGENT_PB_RETURN_NOT_DEFINED; + goto error; + } + + delete_node = sync_agent_create_node(binder, ELEMENT_DELETE); + if (delete_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + delete_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->no_resp) { + WBXMLTreeNode *noResp_node = sync_agent_add_child_node(binder, + delete_node, ELEMENT_NORESP); + if (noResp_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + + /*TODO Archive */ + /*TODO sftDel */ + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, delete_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + GList *Iter = NULL; + item_s *item = NULL; + for (Iter = pCommand->private.change.items; Iter != NULL; Iter = g_list_next(Iter)) { + item = (Iter->data); + + WBXMLTreeNode *item_node = sync_agent_add_child_node(binder, + delete_node, ELEMENT_ITEM); + if (item_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (item->source != NULL) { + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(item->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (item->target != NULL) { + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(item->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (item->content_type != NULL) { + WBXMLTreeNode *meta_node = sync_agent_add_child_node(binder, + item_node, ELEMENT_META); + if (meta_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *type_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_TYPE, item->content_type, strlen(item->content_type)); + if (type_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (item->size) { + str_size = g_strdup_printf("%u", item->size); + if (str_size == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *size_node = sync_agent_add_child_text_node(binder, + meta_node, ELEMENT_SIZE, str_size, strlen(str_size)); + if (size_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + } + } + } + + *wbxml_dom_node = delete_node; + delete_node = NULL; + + error: + + if (delete_node != NULL) + sync_agent_destroy_wbxml_node(delete_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + if (str_size != NULL) + free(str_size); + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_map_command_converter_function(sync_agent_pb_protocol_binder_info_s * binder, Content_Ptr content, WBXMLTreeNode ** wbxml_dom_node) +{ + _INNER_FUNC_ENTER; + + retvm_if(content == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "content is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + char *str_cmdID = NULL; + + command_s *pCommand = (command_s *) content; + + WBXMLTreeNode *map_node = sync_agent_create_node(binder, ELEMENT_MAP); + if (map_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + str_cmdID = g_strdup_printf("%u", pCommand->cmd_id); + if (str_cmdID == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + WBXMLTreeNode *cmdid_node = sync_agent_add_child_text_node(binder, + map_node, ELEMENT_CMDID, str_cmdID, strlen(str_cmdID)); + if (cmdid_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (pCommand->source != NULL && pCommand->target != NULL) { + WBXMLTreeNode *target_node = sync_agent_add_child_node(binder, + map_node, ELEMENT_TARGET); + if (target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pCommand->target, binder, target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + WBXMLTreeNode *source_node = sync_agent_add_child_node(binder, + map_node, ELEMENT_SOURCE); + if (source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(pCommand->source, binder, source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + if (pCommand->cred != NULL) { + err = __append_cred_to_wbxml_node(pCommand->cred, binder, map_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + + GList *iter = NULL; + item_s *item = NULL; + for (iter = pCommand->private.map.items; iter != NULL; iter = g_list_next(iter)) { + item = iter->data; + WBXMLTreeNode *mapItem_node = sync_agent_add_child_node(binder, + map_node, ELEMENT_MAPITEM); + if (mapItem_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + if (item->source != NULL && item->target != NULL) { + WBXMLTreeNode *item_target_node = sync_agent_add_child_node(binder, + mapItem_node, ELEMENT_TARGET); + if (item_target_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(item->target, binder, item_target_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + + WBXMLTreeNode *item_source_node = sync_agent_add_child_node(binder, + mapItem_node, ELEMENT_SOURCE); + if (item_source_node == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto error; + } + + err = __append_location_to_wbxml_node(item->source, binder, item_source_node); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto error; + } + } + } + + *wbxml_dom_node = map_node; + map_node = NULL; + + error: + + if (map_node != NULL) + sync_agent_destroy_wbxml_node(map_node); + + if (str_cmdID != NULL) + free(str_cmdID); + + _EXTERN_FUNC_EXIT; + return err; +} + +static protocol_version_e __get_oma_ds_protocol_version(char *verdtd) +{ + _INNER_FUNC_ENTER; + + retvm_if(verdtd == NULL, VERSION_UNKNOWN, "verdtd is NULL"); + + protocol_version_e protocol_version = VERSION_UNKNOWN; + if (strcmp(verdtd, ELEMENT_VERDRD_12) == 0) { + protocol_version = VERSION_12; + } else if (strcmp(verdtd, ELEMENT_VERDRD_11) == 0) { + protocol_version = VERSION_11; + } else if (strcmp(verdtd, ELEMENT_VERDRD_10) == 0) { + protocol_version = VERSION_10; + } + + _INNER_FUNC_EXIT; + return protocol_version; +} + +static devinf_version_e __get_oma_ds_devInf_version(char *verdtd) +{ + _INNER_FUNC_ENTER; + + retvm_if(verdtd == NULL, DEVINF_VERSION_UNKNOWN, "verdtd is NULL"); + + devinf_version_e devInf_version = DEVINF_VERSION_UNKNOWN; + if (strcmp(verdtd, ELEMENT_VERDRD_12) == 0) { + devInf_version = DEVINF_VERSION_12; + } else if (strcmp(verdtd, ELEMENT_VERDRD_11) == 0) { + devInf_version = DEVINF_VERSION_11; + } else if (strcmp(verdtd, ELEMENT_VERDRD_10) == 0) { + devInf_version = DEVINF_VERSION_10; + } + + _INNER_FUNC_EXIT; + return devInf_version; +} + +static devinf_devtyp_e __get_oma_ds_devInf_device_type(char *dev_type) +{ + _INNER_FUNC_ENTER; + + retvm_if(dev_type == NULL, DEVINF_DEVTYPE_UNKNOWN, "dev_type is NULL"); + + devinf_devtyp_e device_type = DEVINF_DEVTYPE_UNKNOWN; + if (strcmp(dev_type, ELEMENT_PAGER) == 0) { + device_type = DEVINF_DEVTYPE_PAGER; + } else if (strcmp(dev_type, ELEMENT_HANDHELD) == 0) { + device_type = DEVINF_DEVTYPE_HANDHELD; + } else if (strcmp(dev_type, ELEMENT_PDA) == 0) { + device_type = DEVINF_DEVTYPE_PDA; + } else if (strcmp(dev_type, ELEMENT_PHONE) == 0) { + device_type = DEVINF_DEVTYPE_PHONE; + } else if (strcmp(dev_type, ELEMENT_SMARTPHONE) == 0) { + device_type = DEVINF_DEVTYPE_SMARTPHONE; + } else if (strcmp(dev_type, ELEMENT_SERVER) == 0) { + device_type = DEVINF_DEVTYPE_SERVER; + } else if (strcmp(dev_type, ELEMENT_WORKSTATION) == 0) { + device_type = DEVINF_DEVTYPE_WORKSTATION; + } + + _INNER_FUNC_EXIT; + return device_type; +} + +static devinf_content_type_s *__get_devinf_contenttype(WBXMLTreeNode * node) +{ + _INNER_FUNC_ENTER; + + retvm_if(node == NULL, NULL, "node is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + devinf_content_type_s *devinf_content_type = NULL; + + devinf_content_type = (devinf_content_type_s *) calloc(1, sizeof(devinf_content_type_s)); + if (devinf_content_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("devinf_content_type is null"); + goto return_part; + } + + /* get first child */ + WBXMLTreeNode *child = node->children; + for (; child != NULL && child->type != WBXML_TREE_ELEMENT_NODE; child = child->next) { + } + + const char *first_child_name = NULL; + if (child != NULL) { + first_child_name = (const char *)wbxml_tag_get_xml_name(child->name); + if (strcmp(first_child_name, ELEMENT_CTTYPE) == 0) { + char *ctType = NULL; + err = sync_agent_get_text_from_node(child, &ctType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + if (ctType != NULL) + devinf_content_type->ct_type = strdup(ctType); + } else { + /* error */ + goto return_part; + } + + /* get second child */ + child = child->next; + for (; child != NULL && child->type != WBXML_TREE_ELEMENT_NODE; child = child->next) { + } + + if (child != NULL) { + const char *second_child_name = NULL; + second_child_name = (const char *)wbxml_tag_get_xml_name(child->name); + if (strcmp(second_child_name, ELEMENT_VERCT) == 0) { + char *verCT = NULL; + err = sync_agent_get_text_from_node(child, &verCT); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + if (verCT != NULL) + devinf_content_type->verct = strdup(verCT); + } else { + /* error */ + goto return_part; + } + } + } + + _INNER_FUNC_EXIT; + return devinf_content_type; + + return_part: + + /* for prevent */ + if (devinf_content_type != NULL) + free(devinf_content_type); + + _INNER_FUNC_EXIT; + return NULL; + +} + +static location_s *_get_location(WBXMLTreeNode * node) +{ + _INNER_FUNC_ENTER; + + retvm_if(node == NULL, NULL, "node is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + location_s *location = NULL; + + WBXMLTreeNode *child = NULL; + + char *locURI = NULL; + char *locName = NULL; + + /* get first child : required LocURI */ + child = node->children; + for (; child != NULL && child->type != WBXML_TREE_ELEMENT_NODE; child = child->next) { + } + + const char *first_child_name = NULL; + if (child != NULL) { + first_child_name = (const char *)wbxml_tag_get_xml_name(child->name); + _DEBUG_TRACE("child_name = %s", first_child_name); + if (strcmp(first_child_name, ELEMENT_LOCURI) == 0) { + err = sync_agent_get_text_from_node(child, &locURI); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + } else { + /* error */ + goto return_part; + } + + /* get second child : optional LocName */ + child = child->next; + for (; child != NULL && child->type != WBXML_TREE_ELEMENT_NODE; child = child->next) { + } + + if (child != NULL) { + const char *second_child_name = NULL; + second_child_name = (const char *)wbxml_tag_get_xml_name(child->name); + _DEBUG_TRACE("child_name = %s", second_child_name); + if (strcmp(second_child_name, ELEMENT_LOCNAME) == 0) { + err = sync_agent_get_text_from_node(child, &locName); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + } else { + /* error */ + goto return_part; + } + } + + sa_error_type_e error = create_location(locURI, locName, &location); + if (error != SA_INTERNAL_OK) { + _DEBUG_ERROR("create_location() failed !, error = %d", error); + goto return_part; + } + } + + _EXTERN_FUNC_EXIT; + return location; + + return_part: + + _EXTERN_FUNC_EXIT; + return NULL; +} + +static anchor_s *_get_anchor(WBXMLTreeNode * node) +{ + _INNER_FUNC_ENTER; + + retvm_if(node == NULL, NULL, "node is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + anchor_s *anchor = NULL; + + WBXMLTreeNode *child = NULL; + + char *lastAnchor = NULL; + char *nextAnchor = NULL; + /* get first child : optional lastAnchor */ + child = node->children; + for (; child != NULL && child->type != WBXML_TREE_ELEMENT_NODE; child = child->next) { + } + + const char *first_child_name = NULL; + if (child != NULL) { + first_child_name = (const char *)wbxml_tag_get_xml_name(child->name); + _DEBUG_TRACE("child_name = %s", first_child_name); + if (strcmp(first_child_name, ELEMENT_LAST) == 0) { + err = sync_agent_get_text_from_node(child, &lastAnchor); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + child = child->next; + for (; child != NULL && child->type != WBXML_TREE_ELEMENT_NODE; child = child->next) { + } + } else if (strcmp(first_child_name, ELEMENT_NEXT) == 0) { + err = sync_agent_get_text_from_node(child, &nextAnchor); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + child = NULL; + } + + /* get second child : required nextAnchor */ + if (child != NULL) { + const char *second_child_name = NULL; + second_child_name = (const char *)wbxml_tag_get_xml_name(child->name); + _DEBUG_TRACE("child_name = %s", second_child_name); + if (strcmp(second_child_name, ELEMENT_NEXT) == 0) { + err = sync_agent_get_text_from_node(child, &nextAnchor); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + } else { + /* error */ + goto return_part; + } + } + + sa_error_type_e error = create_anchor(lastAnchor, nextAnchor, &anchor); + if (error != SA_INTERNAL_OK) { + _DEBUG_ERROR("create_anchor() failed !, error = %d", error); + goto return_part; + } + } + + _EXTERN_FUNC_EXIT; + return anchor; + + return_part: + + _EXTERN_FUNC_EXIT; + return NULL; +} + +static cred_s *__get_cred(WBXMLTreeNode * node) +{ + _INNER_FUNC_ENTER; + + retvm_if(node == NULL, NULL, "node is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + cred_s *cred = NULL; + + char *data = NULL; + char *type = NULL; + char *format = NULL; + + WBXMLTreeNode *child = NULL; + + for (child = node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_META) == 0) { + WBXMLTreeNode *grand_child = child->children; + + const char *grand_child_xml_name = NULL; + grand_child_xml_name = (const char *)wbxml_tag_get_xml_name(grand_child->name); + + if (strcmp(grand_child_xml_name, ELEMENT_TYPE) == 0) { + err = sync_agent_get_text_from_node(grand_child, &type); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_VERBOSE("Type = [%s]", type); + } else if (strcmp(grand_child_xml_name, ELEMENT_FORMAT) == 0) { + err = sync_agent_get_text_from_node(grand_child, &format); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_VERBOSE("Type = [%s]", format); + } + } else if (strcmp(child_xml_name, ELEMENT_DATA) == 0) { + err = sync_agent_get_text_from_node(child, &data); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_VERBOSE("Data = [%s]", data); + } + + } + + cred = create_cred_with_data(convert_auth_type(type), data); + + format_type_e formatType = FORMAT_TYPE_UNKNOWN; + if (format != NULL) { + if (strcmp(format, ELEMENT_B64) == 0) + formatType = FORMAT_TYPE_BASE64; + } + + set_cred_format_type(cred, formatType); + + _INNER_FUNC_EXIT; + return cred; + + return_part: + + _INNER_FUNC_EXIT; + return NULL; +} + +static chal_s *_get_chal(WBXMLTreeNode * node) +{ + _INNER_FUNC_ENTER; + + retvm_if(node == NULL, NULL, "node is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + chal_s *pChal = (chal_s *) calloc(1, sizeof(chal_s)); + if (pChal == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pChal is null"); + goto return_part; + } + + WBXMLTreeNode *chal_child = node->children; + + WBXMLTreeNode *meta_child = NULL; + + for (meta_child = chal_child->children; meta_child != NULL; meta_child = meta_child->next) { + if (meta_child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *meta_child_xml_name = NULL; + meta_child_xml_name = (const char *)wbxml_tag_get_xml_name(meta_child->name); + + if (strcmp(meta_child_xml_name, ELEMENT_TYPE) == 0) { + char *type = NULL; + err = sync_agent_get_text_from_node(meta_child, &type); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Type = [%s]", type); + + pChal->type = convert_auth_type(type); + } else if (strcmp(meta_child_xml_name, ELEMENT_FORMAT) == 0) { + char *format = NULL; + err = sync_agent_get_text_from_node(meta_child, &format); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Format = [%s]", format); + + pChal->format = convert_format_type(format); + } else if (strcmp(meta_child_xml_name, ELEMENT_NEXTNONCE) == 0) { + char *nextnonce = NULL; + unsigned int nonce_len = 0; + err = sync_agent_get_binary_from_node(meta_child, &nextnonce, &nonce_len); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("NextNonce = [%s]", nextnonce); + if (pChal->format == FORMAT_TYPE_BASE64) { + if (nextnonce != NULL) + pChal->nonce_b64 = strdup(nextnonce); + } else { + pChal->nonce_plain = (char *)malloc(nonce_len + 1); + if (pChal->nonce_plain == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + memcpy(pChal->nonce_plain, nextnonce, nonce_len); + pChal->nonce_plain[nonce_len] = '\0'; + } + + pChal->nonce_length = nonce_len; + } + } + + _EXTERN_FUNC_EXIT; + return pChal; + + return_part: + + if (pChal != NULL) + free_chal(pChal); + + _EXTERN_FUNC_EXIT; + return NULL; +} + +static sync_agent_pb_error_e _oma_ds_binder_devinf_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + devinf_s *pDevInf = (devinf_s *) calloc(1, sizeof(devinf_s)); + if (pDevInf == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pDevInf is null"); + goto return_part; + } + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_VERDTD) == 0) { + char *verdtd = NULL; + err = sync_agent_get_text_from_node(child, &verdtd); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("VerDTD = [%s]", verdtd); + + pDevInf->version = __get_oma_ds_devInf_version(verdtd); + } else if (strcmp(child_xml_name, ELEMENT_MAN) == 0) { + char *man = NULL; + err = sync_agent_get_text_from_node(child, &man); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Man = [%s]", man); + if (man != NULL) + pDevInf->manufacturer = strdup(man); + } else if (strcmp(child_xml_name, ELEMENT_MOD) == 0) { + char *mod = NULL; + err = sync_agent_get_text_from_node(child, &mod); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Mod = [%s]", mod); + if (mod != NULL) + pDevInf->model = strdup(mod); + } else if (strcmp(child_xml_name, ELEMENT_OEM) == 0) { + char *oem = NULL; + err = sync_agent_get_text_from_node(child, &oem); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("OEM = [%s]", oem); + if (oem != NULL) + pDevInf->oem = strdup(oem); + } else if (strcmp(child_xml_name, ELEMENT_FWV) == 0) { + char *fwv = NULL; + err = sync_agent_get_text_from_node(child, &fwv); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("FwV = [%s]", fwv); + if (fwv != NULL) + pDevInf->firmware_version = strdup(fwv); + } else if (strcmp(child_xml_name, ELEMENT_SWV) == 0) { + char *swv = NULL; + err = sync_agent_get_text_from_node(child, &swv); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("SwV = [%s]", swv); + if (swv != NULL) + pDevInf->software_version = strdup(swv); + } else if (strcmp(child_xml_name, ELEMENT_HWV) == 0) { + char *hwv = NULL; + err = sync_agent_get_text_from_node(child, &hwv); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("HwV = [%s]", hwv); + if (hwv != NULL) + pDevInf->hardware_version = strdup(hwv); + } else if (strcmp(child_xml_name, ELEMENT_DEVID) == 0) { + char *devID = NULL; + err = sync_agent_get_text_from_node(child, &devID); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("DevID = [%s]", devID); + if (devID != NULL) + pDevInf->dev_id = strdup(devID); + } else if (strcmp(child_xml_name, ELEMENT_DEVTYP) == 0) { + char *devtyp = NULL; + err = sync_agent_get_text_from_node(child, &devtyp); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("DevTyp = [%s]", devtyp); + + pDevInf->devtyp = __get_oma_ds_devInf_device_type(devtyp); + } else if (strcmp(child_xml_name, ELEMENT_UTC) == 0) { + _DEBUG_TRACE("UTC"); + pDevInf->supports_utc = 1; + } else if (strcmp(child_xml_name, ELEMENT_SUPPORTLARGEOBJS) == 0) { + _DEBUG_TRACE("SupportLargeObjs"); + pDevInf->supports_large_objs = 1; + } else if (strcmp(child_xml_name, ELEMENT_SUPPORTNUMBEROFCHANGES) == 0) { + _DEBUG_TRACE("SupportNumberOfChanges"); + pDevInf->supports_number_of_changes = 1; + } else if (strcmp(child_xml_name, ELEMENT_DATASTORE) == 0) { + + devinf_datastore_s *pDevInfDataStore = (devinf_datastore_s *) calloc(1, sizeof(devinf_datastore_s)); + if (pDevInfDataStore == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pDevInfDataStore is null"); + goto return_part; + } + + WBXMLTreeNode *datastore_child = NULL; + + for (datastore_child = child->children; datastore_child != NULL; datastore_child = datastore_child->next) { + if (datastore_child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *datastore_child_xml_name = NULL; + datastore_child_xml_name = (const char *)wbxml_tag_get_xml_name(datastore_child->name); + + if (strcmp(datastore_child_xml_name, ELEMENT_SOURCEREF) == 0) { + char *sourceref = NULL; + err = sync_agent_get_text_from_node(datastore_child, &sourceref); + if (err != SYNC_AGENT_PB_RETURN_OK) { + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + _DEBUG_TRACE("SourceRef = [%s]", sourceref); + if (sourceref != NULL) + pDevInfDataStore->source_ref = strdup(sourceref); + } else if (strcmp(datastore_child_xml_name, ELEMENT_DISPLAYNAME) == 0) { + char *displayname = NULL; + err = sync_agent_get_text_from_node(datastore_child, &displayname); + if (err != SYNC_AGENT_PB_RETURN_OK) { + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + _DEBUG_TRACE("DisplayName = [%s]", displayname); + if (displayname != NULL) + pDevInfDataStore->display_name = strdup(displayname); + } else if (strcmp(datastore_child_xml_name, ELEMENT_MAXGUIDSIZE) == 0) { + char *maxguidsize = NULL; + err = sync_agent_get_text_from_node(datastore_child, &maxguidsize); + if (err != SYNC_AGENT_PB_RETURN_OK) { + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + _DEBUG_TRACE("MaxGUIDSize = [%s]", maxguidsize); + if (maxguidsize != NULL) + pDevInfDataStore->max_guid_size = atoi(maxguidsize); + } else if (strcmp(datastore_child_xml_name, ELEMENT_RX_PREF) == 0) { + devinf_content_type_s *devinf_content_type = __get_devinf_contenttype(datastore_child); + if (devinf_content_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + pDevInfDataStore->rx_pref = devinf_content_type; + } else if (strcmp(datastore_child_xml_name, ELEMENT_RX) == 0) { + devinf_content_type_s *devinf_content_type = __get_devinf_contenttype(datastore_child); + if (devinf_content_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + pDevInfDataStore->rx = g_list_append(pDevInfDataStore->rx, devinf_content_type); + } else if (strcmp(datastore_child_xml_name, ELEMENT_TX_PREF) == 0) { + devinf_content_type_s *devinf_content_type = __get_devinf_contenttype(datastore_child); + if (devinf_content_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + pDevInfDataStore->tx_pref = devinf_content_type; + } else if (strcmp(datastore_child_xml_name, ELEMENT_TX) == 0) { + devinf_content_type_s *devinf_content_type = __get_devinf_contenttype(datastore_child); + if (devinf_content_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + pDevInfDataStore->tx = g_list_append(pDevInfDataStore->tx, devinf_content_type); + } else if (strcmp(datastore_child_xml_name, ELEMENT_CTCAP) == 0) { + + devinf_ct_cap_s *pDevInfCtCap = (devinf_ct_cap_s *) calloc(1, sizeof(devinf_ct_cap_s)); + if (pDevInfCtCap == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pDevInfCtCap is null"); + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + + devinf_content_type_s *devinf_content_type = NULL; + WBXMLTreeNode *ctcap_child = NULL; + + for (ctcap_child = datastore_child->children; ctcap_child != NULL; ctcap_child = ctcap_child->next) { + if (ctcap_child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *ctcap_child_xml_name = NULL; + ctcap_child_xml_name = (const char *)wbxml_tag_get_xml_name(ctcap_child->name); + + if (strcmp(ctcap_child_xml_name, ELEMENT_CTTYPE) == 0) { + if (devinf_content_type == NULL) { + devinf_content_type = (devinf_content_type_s *) calloc(1, sizeof(devinf_content_type_s)); + if (devinf_content_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("devinf_content_type is null"); + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + goto return_part; + } + } + + char *cttype = NULL; + err = sync_agent_get_text_from_node(ctcap_child, &cttype); + if (err != SYNC_AGENT_PB_RETURN_OK) { + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + goto return_part; + } + _DEBUG_TRACE("CTType = [%s]", cttype); + if (cttype != NULL) + devinf_content_type->ct_type = strdup(cttype); + } else if (strcmp(ctcap_child_xml_name, ELEMENT_VERCT) == 0) { + if (devinf_content_type == NULL) { + devinf_content_type = (devinf_content_type_s *) calloc(1, sizeof(devinf_content_type_s)); + if (devinf_content_type == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("devinf_content_type is null"); + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + goto return_part; + } + } + + char *verct = NULL; + err = sync_agent_get_text_from_node(ctcap_child, &verct); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + goto return_part; + } + _DEBUG_TRACE("VerCT = [%s]", verct); + if (verct != NULL) + devinf_content_type->verct = strdup(verct); + } else if (strcmp(ctcap_child_xml_name, ELEMENT_FIELDLEVEL) == 0) { + /*TODO fieldlevel */ + + } else if (strcmp(ctcap_child_xml_name, ELEMENT_PROPERTY) == 0) { + + devinf_property_s *pDevInfProperty = (devinf_property_s *) calloc(1, sizeof(devinf_property_s)); + if (pDevInfProperty == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pDevInfProperty is null"); + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + goto return_part; + } + + WBXMLTreeNode *property_child = NULL; + for (property_child = ctcap_child->children; property_child != NULL; property_child = property_child->next) { + if (property_child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *property_child_xml_name = NULL; + property_child_xml_name = (const char *)wbxml_tag_get_xml_name(property_child->name); + + if (strcmp(property_child_xml_name, ELEMENT_PROPNAME) == 0) { + char *propname = NULL; + err = sync_agent_get_text_from_node(property_child, &propname); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("PropName = [%s]", propname); + if (propname != NULL) + pDevInfProperty->prop_name = strdup(propname); + } else if (strcmp(property_child_xml_name, ELEMENT_DATATYPE) == 0) { + char *datatype = NULL; + err = sync_agent_get_text_from_node(property_child, &datatype); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("DataType = [%s]", datatype); + if (datatype != NULL) + pDevInfProperty->data_type = strdup(datatype); + } else if (strcmp(property_child_xml_name, ELEMENT_MAXOCCUR) == 0) { + char *maxoccur = NULL; + err = sync_agent_get_text_from_node(property_child, &maxoccur); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("MaxOccur = [%s]", maxoccur); + if (maxoccur != NULL) + pDevInfProperty->max_occur = atoi(maxoccur); + } else if (strcmp(property_child_xml_name, ELEMENT_MAXSIZE) == 0) { + char *maxsize = NULL; + err = sync_agent_get_text_from_node(property_child, &maxsize); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("MaxSize = [%s]", maxsize); + if (maxsize != NULL) + pDevInfProperty->max_size = atoi(maxsize); + } else if (strcmp(property_child_xml_name, ELEMENT_NOTRUNCATE) == 0) { + _DEBUG_TRACE("NoTruncate"); + pDevInfProperty->no_truncate = 1; + } else if (strcmp(property_child_xml_name, ELEMENT_VALENUM) == 0) { + char *valenum = NULL; + err = sync_agent_get_text_from_node(property_child, &valenum); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("ValEnum = [%s]", valenum); + if (valenum != NULL) + pDevInfProperty->val_enums = g_list_append(pDevInfProperty->val_enums, strdup(valenum)); + } else if (strcmp(property_child_xml_name, ELEMENT_DISPLAYNAME) == 0) { + char *displayname = NULL; + err = sync_agent_get_text_from_node(property_child, &displayname); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("DisplayName = [%s]", displayname); + if (displayname != NULL) + pDevInfProperty->display_name = strdup(displayname); + } else if (strcmp(property_child_xml_name, ELEMENT_PROPPARAM) == 0) { + + devinf_prop_param_s *pDevInfPropParam = (devinf_prop_param_s *) calloc(1, sizeof(devinf_prop_param_s)); + if (pDevInfPropParam == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pDevInfPropParam is null"); + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + + WBXMLTreeNode *propparam_child = NULL; + + for (propparam_child = property_child->children; propparam_child != NULL; propparam_child = propparam_child->next) { + if (propparam_child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *propparam_child_xml_name = NULL; + propparam_child_xml_name = (const char *)wbxml_tag_get_xml_name(propparam_child->name); + + if (strcmp(propparam_child_xml_name, ELEMENT_PARAMNAME) == 0) { + char *paramname = NULL; + err = sync_agent_get_text_from_node(propparam_child, ¶mname); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("ParamName = [%s]", paramname); + if (paramname != NULL) + pDevInfPropParam->param_name = strdup(paramname); + } else if (strcmp(propparam_child_xml_name, ELEMENT_DATATYPE) == 0) { + char *datatype = NULL; + err = sync_agent_get_text_from_node(propparam_child, &datatype); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("DataType = [%s]", datatype); + if (datatype != NULL) + pDevInfPropParam->data_type = strdup(datatype); + } else if (strcmp(propparam_child_xml_name, ELEMENT_VALENUM) == 0) { + char *valenum = NULL; + err = sync_agent_get_text_from_node(propparam_child, &valenum); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("ValEnum = [%s]", valenum); + if (valenum != NULL) + pDevInfPropParam->val_enums = g_list_append(pDevInfPropParam->val_enums, strdup(valenum)); + } else if (strcmp(propparam_child_xml_name, ELEMENT_DISPLAYNAME) == 0) { + char *displayname = NULL; + err = sync_agent_get_text_from_node(propparam_child, &displayname); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + if (pDevInfCtCap != NULL) + free(pDevInfCtCap); + + if (devinf_content_type != NULL) + free(devinf_content_type); + + if (pDevInfProperty != NULL) + free(pDevInfProperty); + + goto return_part; + } + _DEBUG_TRACE("DisplayName = [%s]", displayname); + if (displayname != NULL) + pDevInfPropParam->display_name = strdup(displayname); + } + } + + pDevInfProperty->prop_params = g_list_append(pDevInfProperty->prop_params, pDevInfPropParam); + } + } + + pDevInfCtCap->properties = g_list_append(pDevInfCtCap->properties, pDevInfProperty); + } + } + pDevInfCtCap->ct = devinf_content_type; + pDevInfDataStore->ct_caps = g_list_append(pDevInfDataStore->ct_caps, pDevInfCtCap); + } else if (strcmp(datastore_child_xml_name, ELEMENT_DSMEM) == 0) { + + /*TODO DSMem */ + } else if (strcmp(datastore_child_xml_name, ELEMENT_SUPPORTHIERARCHICALSYNC) == 0) { + _DEBUG_TRACE("SupportHierarchicalSync"); + pDevInfDataStore->supports_hierarchical_sync = 1; + } else if (strcmp(datastore_child_xml_name, ELEMENT_SYNCCAP) == 0) { + + WBXMLTreeNode *synccap_child = NULL; + + for (synccap_child = datastore_child->children; synccap_child != NULL; synccap_child = synccap_child->next) { + if (synccap_child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *synccap_child_xml_name = NULL; + synccap_child_xml_name = (const char *)wbxml_tag_get_xml_name(synccap_child->name); + + if (strcmp(synccap_child_xml_name, ELEMENT_SYNCTYPE) == 0) { + char *synctype_str = NULL; + unsigned int synctype = 0; + err = sync_agent_get_text_from_node(synccap_child, &synctype_str); + if (err != SYNC_AGENT_PB_RETURN_OK) { + + if (pDevInfDataStore != NULL) + free(pDevInfDataStore); + + goto return_part; + } + _DEBUG_TRACE("SyncType = [%s]", synctype_str); + if (synctype_str != NULL) { + synctype = atoi(synctype_str); + synctype = 1 << (synctype - 1); + } + /*DevInfSyncCap synccap = convert_devinf_synccap(synctype); */ + set_devinf_datastore_sync_cap(pDevInfDataStore, synctype, 1); + } + } + } else if (strcmp(datastore_child_xml_name, ELEMENT_FILTER_RX) == 0) { + /*TODO filter-rx */ + } else if (strcmp(datastore_child_xml_name, ELEMENT_FILTERCAP) == 0) { + /*TODO filtercap */ + } + } + pDevInf->datastores = g_list_append(pDevInf->datastores, pDevInfDataStore); + } + } + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pDevInf; + } else { + _DEBUG_ERROR("error"); + free_devinf(pDevInf); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_header_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + sync_hdr_s *pSyncHdr = (sync_hdr_s *) calloc(1, sizeof(sync_hdr_s)); + if (pSyncHdr == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pSyncHdr is null"); + goto return_part; + } + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_VERDTD) == 0) { + char *verdtd = NULL; + err = sync_agent_get_text_from_node(child, &verdtd); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("VerDTD = [%s]", verdtd); + + pSyncHdr->version = __get_oma_ds_protocol_version(verdtd); + } else if (strcmp(child_xml_name, ELEMENT_SESSIONID) == 0) { + char *sessionID = NULL; + err = sync_agent_get_text_from_node(child, &sessionID); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("SessionID = [%s]", sessionID); + + if (sessionID != NULL) + pSyncHdr->session_id = strdup(sessionID); + else + pSyncHdr->session_id = NULL; + } else if (strcmp(child_xml_name, ELEMENT_MSGID) == 0) { + char *msgID = NULL; + err = sync_agent_get_text_from_node(child, &msgID); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("MsgID = [%s]", msgID); + if (msgID != NULL) + pSyncHdr->message_id = atoi(msgID); + } else if (strcmp(child_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(child); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pSyncHdr->target = pTargetLocation; + } else if (strcmp(child_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(child); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pSyncHdr->source = pSourceLocation; + } else if (strcmp(child_xml_name, ELEMENT_RESPURI) == 0) { + char *resp_uri = NULL; + err = sync_agent_get_text_from_node(child, &resp_uri); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("resp_uri = [%s]", resp_uri); + if (resp_uri != NULL) + pSyncHdr->response_uri = strdup(resp_uri); + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("noResp\n"); + pSyncHdr->no_response = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pSyncHdr->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + + } + } + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pSyncHdr; + } else { + _DEBUG_ERROR("error"); + free_sync_hdr(pSyncHdr); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_results_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + command_s *pResults = (command_s *) calloc(1, sizeof(command_s)); + if (pResults == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pResults is null"); + goto return_part; + } + + pResults->type = COMMAND_TYPE_RESULTS; + pResults->msg_id = pSyncHdr->message_id; + pResults->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pResults->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_MSGREF) == 0) { + char *msg_ref = NULL; + err = sync_agent_get_text_from_node(child, &msg_ref); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("MsgRef = [%s]", msg_ref); + if (msg_ref != NULL) + pResults->private.results.msg_ref = atoi(msg_ref); + } else if (strcmp(child_xml_name, ELEMENT_CMDREF) == 0) { + char *cmd_ref = NULL; + err = sync_agent_get_text_from_node(child, &cmd_ref); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("MsgRef = [%s]", cmd_ref); + if (cmd_ref != NULL) + pResults->private.results.cmd_ref = atoi(cmd_ref); + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + pResults->private.results.type = strdup(contentType); + } + } + } else if (strcmp(child_xml_name, ELEMENT_TARGETREF) == 0) { + /*TODO targetRef */ + } else if (strcmp(child_xml_name, ELEMENT_SOURCEREF) == 0) { + /*TODO sourceRef */ + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + /*create item in results command */ + item_s *item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pResults->private.results.item = item; + + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pResults->private.results.item->source = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(grandchild); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pResults->private.results.item->target = pTargetLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_DATA) == 0) { + /* TODO data */ + } + } + } + } + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pResults; + } else { + _DEBUG_ERROR("error"); + free_command(pResults); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_put_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + command_s *pPut = (command_s *) calloc(1, sizeof(command_s)); + if (pPut == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pPut is null"); + goto return_part; + } + + pPut->type = COMMAND_TYPE_PUT; + pPut->msg_id = pSyncHdr->message_id; + pPut->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pPut->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("NoResp"); + pPut->no_resp = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pPut->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + pPut->private.access.type = strdup(contentType); + } + } + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + /*create item in put command */ + item_s *item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pPut->private.access.item = item; + + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pPut->private.access.item->source = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(grandchild); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pPut->private.access.item->target = pTargetLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_DATA) == 0) { + /* TODO data */ + } + } + } + } + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pPut; + } else { + _DEBUG_ERROR("error"); + free_command(pPut); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_get_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + command_s *pGet = (command_s *) calloc(1, sizeof(command_s)); + if (pGet == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pGet is null"); + goto return_part; + } + + pGet->type = COMMAND_TYPE_GET; + pGet->msg_id = pSyncHdr->message_id; + pGet->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pGet->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("NoResp"); + pGet->no_resp = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pGet->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + pGet->private.access.type = strdup(contentType); + } + } + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + /*create item in put command */ + item_s *item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pGet->private.access.item = item; + + WBXMLTreeNode *grandchild = NULL; + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pGet->private.access.item->source = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(grandchild); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pGet->private.access.item->target = pTargetLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_DATA) == 0) { + /* TODO data */ + } + } + } + } + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pGet; + } else { + _DEBUG_ERROR("error"); + free_command(pGet); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_alert_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + command_s *pAlert = (command_s *) calloc(1, sizeof(command_s)); + if (pAlert == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pAlert is null"); + goto return_part; + } + + pAlert->type = COMMAND_TYPE_ALERT; + pAlert->msg_id = pSyncHdr->message_id; + pAlert->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pAlert->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("NoResp"); + pAlert->no_resp = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pAlert->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_DATA) == 0) { + char *data = NULL; + err = sync_agent_get_text_from_node(child, &data); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Data = [%s]", data); + if (data != NULL) + pAlert->private.alert.type = atoi(data); + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pAlert->source = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(grandchild); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pAlert->target = pTargetLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_META) == 0) { + + WBXMLTreeNode *grandgrandchild = NULL; + for (grandgrandchild = grandchild->children; grandgrandchild != NULL; grandgrandchild = grandgrandchild->next) { + if (grandgrandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandgrandchild_xml_name = NULL; + grandgrandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandgrandchild->name); + + if (strcmp(grandgrandchild_xml_name, ELEMENT_ANCHOR) == 0) { + anchor_s *anchor = _get_anchor(grandgrandchild); + if (anchor == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pAlert->private.alert.anchor = anchor; + } else if (strcmp(grandgrandchild_xml_name, ELEMENT_MAXOBJSIZE) == 0) { + char *maxObjSize = NULL; + err = sync_agent_get_text_from_node(grandgrandchild, &maxObjSize); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("MaxObjSize = [%s]", maxObjSize); + if (maxObjSize != NULL) + pAlert->private.alert.max_obj_size = atoi(maxObjSize); + } + } + } + } + } + } + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pAlert; + } else { + _DEBUG_ERROR("error"); + free_command(pAlert); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_sync_end_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + command_s *pSyncEnd = (command_s *) calloc(1, sizeof(command_s)); + if (pSyncEnd == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pSyncEnd is null"); + goto return_part; + } + + pSyncEnd->type = COMMAND_TYPE_SYNC_END;; + pSyncEnd->msg_id = pSyncHdr->message_id; + pSyncEnd->ref_count = 1; + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pSyncEnd; + } else { + _DEBUG_ERROR("error"); + free_command(pSyncEnd); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_sync_start_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + /* get data from dom tree */ + command_s *pSync = (command_s *) calloc(1, sizeof(command_s)); + if (pSync == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pSync is null"); + goto return_part; + } + + pSync->type = COMMAND_TYPE_SYNC_START;; + pSync->msg_id = pSyncHdr->message_id; + pSync->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pSync->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("NoResp"); + pSync->no_resp = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pSync->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(child); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pSync->target = pTargetLocation; + } else if (strcmp(child_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(child); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pSync->source = pSourceLocation; + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + /*TODO meta */ + } else if (strcmp(child_xml_name, ELEMENT_NUMBEROFCHANGES) == 0) { + char *numOfChanges = NULL; + err = sync_agent_get_text_from_node(child, &numOfChanges); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("NumberOfChanges = [%s]", numOfChanges); + + pSync->private.sync.has_num_changed = 1; + if (numOfChanges != NULL) + pSync->private.sync.num_changed = atoi(numOfChanges); + } + } + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pSync; + } else { + _DEBUG_ERROR("error"); + free_command(pSync); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_add_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + item_s *item = NULL; + + /* get data from dom tree */ + command_s *pAdd = (command_s *) calloc(1, sizeof(command_s)); + if (pAdd == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pAdd is null"); + goto return_part; + } + + pAdd->type = COMMAND_TYPE_ADD;; + pAdd->msg_id = pSyncHdr->message_id; + pAdd->private.change.type = CHANGE_ADD; + pAdd->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pAdd->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("NoResp"); + pAdd->no_resp = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pAdd->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + WBXMLTreeNode *grandchild = NULL; + + if (item == NULL) { + item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + } + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + item->content_type = strdup(contentType); + } else if (strcmp(grandchild_xml_name, ELEMENT_SIZE) == 0) { + char *size = NULL; + err = sync_agent_get_text_from_node(grandchild, &size); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Size = [%s]", size); + if (size != NULL) + item->size = atoi(size); + } + } + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + + if (item == NULL) { + item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + } + + WBXMLTreeNode *grandchild = NULL; + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + item->source = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(grandchild); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + item->target = pTargetLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_META) == 0) { + WBXMLTreeNode *grandgrandchild = NULL; + + for (grandgrandchild = grandchild->children; grandgrandchild != NULL; grandgrandchild = grandgrandchild->next) { + if (grandgrandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandgrandchild_xml_name = NULL; + grandgrandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandgrandchild->name); + + if (strcmp(grandgrandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandgrandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + item->content_type = strdup(contentType); + } else if (strcmp(grandgrandchild_xml_name, ELEMENT_SIZE) == 0) { + char *size = NULL; + err = sync_agent_get_text_from_node(grandgrandchild, &size); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Size = [%s]", size); + if (size != NULL) + item->size = atoi(size); + } + } + } else if (strcmp(grandchild_xml_name, ELEMENT_DATA) == 0) { + char *data = NULL; + err = sync_agent_get_text_from_cdata_node(grandchild, &data); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Data = [%s]", data); + if (data != NULL) + item->private.data = strdup(data); + item->data_type = ITEM_DATA; + } else if (strcmp(grandchild_xml_name, ELEMENT_MOREDATA) == 0) { + _DEBUG_TRACE("MoreData"); + item->more_data = 1; + } + } + pAdd->private.change.items = g_list_append(pAdd->private.change.items, item); + item = NULL; + } + } + + return_part: + + if (item != NULL) + free_item(item); + + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pAdd; + } else { + _DEBUG_ERROR("error"); + free_command(pAdd); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_replace_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + item_s *item = NULL; + + /* get data from dom tree */ + command_s *pReplace = (command_s *) calloc(1, sizeof(command_s)); + if (pReplace == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pReplace is null"); + goto return_part; + } + + pReplace->type = COMMAND_TYPE_REPLACE;; + pReplace->msg_id = pSyncHdr->message_id; + pReplace->private.change.type = CHANGE_REPLACE; + pReplace->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pReplace->cmd_id = atoi(cmd_id); + + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("NoResp"); + pReplace->no_resp = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pReplace->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + + if (item == NULL) { + item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + } + WBXMLTreeNode *grandchild = NULL; + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + item->content_type = strdup(contentType); + } else if (strcmp(grandchild_xml_name, ELEMENT_SIZE) == 0) { + char *size = NULL; + err = sync_agent_get_text_from_node(grandchild, &size); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Size = [%s]", size); + if (size != NULL) + item->size = atoi(size); + } + } + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + + if (item == NULL) { + item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + } + + WBXMLTreeNode *grandchild = NULL; + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + item->source = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(grandchild); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + item->target = pTargetLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_META) == 0) { + WBXMLTreeNode *grandgrandchild = NULL; + + for (grandgrandchild = grandchild->children; grandgrandchild != NULL; grandgrandchild = grandgrandchild->next) { + if (grandgrandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandgrandchild_xml_name = NULL; + grandgrandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandgrandchild->name); + + if (strcmp(grandgrandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandgrandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + item->content_type = strdup(contentType); + } else if (strcmp(grandgrandchild_xml_name, ELEMENT_SIZE) == 0) { + char *size = NULL; + err = sync_agent_get_text_from_node(grandgrandchild, &size); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Size = [%s]", size); + if (size != NULL) + item->size = atoi(size); + } + } + + } else if (strcmp(grandchild_xml_name, ELEMENT_DATA) == 0) { + char *data = NULL; + err = sync_agent_get_text_from_cdata_node(grandchild, &data); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Data = [%s]", data); + if (data != NULL) + item->private.data = strdup(data); + item->data_type = ITEM_DATA; + } + } + pReplace->private.change.items = g_list_append(pReplace->private.change.items, item); + item = NULL; + } + } + + return_part: + + if (item != NULL) + free_item(item); + + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pReplace; + } else { + _DEBUG_ERROR("error"); + free_command(pReplace); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_delete_command_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + retvm_if(reverse_binder == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "reverse_binder is NULL"); + + sync_hdr_s *pSyncHdr = (sync_hdr_s *) sync_agent_get_user_data_from_reverse_protocol_binder(reverse_binder); + retvm_if(pSyncHdr == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "pSyncHdr is NULL"); + + item_s *item = NULL; + + /* get data from dom tree */ + command_s *pDelete = (command_s *) calloc(1, sizeof(command_s)); + if (pDelete == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pDelete is null"); + goto return_part; + } + + pDelete->type = COMMAND_TYPE_DELETE; + pDelete->msg_id = pSyncHdr->message_id; + pDelete->private.change.type = CHANGE_DELETE; + pDelete->ref_count = 1; + + WBXMLTreeNode *child = NULL; + + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pDelete->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_NORESP) == 0) { + _DEBUG_TRACE("NoResp"); + pDelete->no_resp = 1; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pDelete->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_META) == 0) { + WBXMLTreeNode *grandchild = NULL; + + /* create Item */ + if (item == NULL) { + item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + } + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_TYPE) == 0) { + char *contentType = NULL; + err = sync_agent_get_text_from_node(grandchild, &contentType); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Content Type = [%s]", contentType); + if (contentType != NULL) + item->content_type = strdup(contentType); + } + } + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + /* create Item */ + if (item == NULL) { + item = create_item(); + if (item == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + } + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + item->source = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + location_s *pTargetLocation = _get_location(grandchild); + if (pTargetLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + item->target = pTargetLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_META) == 0) { + + } else if (strcmp(grandchild_xml_name, ELEMENT_DATA) == 0) { + + } + } + pDelete->private.change.items = g_list_append(pDelete->private.change.items, item); + item = NULL; + } + } + + return_part: + + if (item != NULL) + free_item(item); + + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pDelete; + } else { + _DEBUG_ERROR("error"); + free_command(pDelete); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_final_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + int *pFinal = (int *)1; + *content = pFinal; + + _EXTERN_FUNC_EXIT; + return err; +} + +static sync_agent_pb_error_e _oma_ds_binder_status_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * content) +{ + _INNER_FUNC_ENTER; + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_NOT_DEFINED, "wbxml_dom_node is NULL"); + + /* get data from dom tree */ + status_s *pStatus = (status_s *) calloc(1, sizeof(status_s)); + if (pStatus == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + _DEBUG_ERROR("pStatus is null"); + goto return_part; + } + + WBXMLTreeNode *child = NULL; + for (child = wbxml_dom_node->children; child != NULL; child = child->next) { + if (child->type != WBXML_TREE_ELEMENT_NODE) + continue; + const char *child_xml_name = NULL; + child_xml_name = (const char *)wbxml_tag_get_xml_name(child->name); + + if (strcmp(child_xml_name, ELEMENT_CMDID) == 0) { + char *cmd_id = NULL; + err = sync_agent_get_text_from_node(child, &cmd_id); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdID = [%s]", cmd_id); + if (cmd_id != NULL) + pStatus->cmd_id = atoi(cmd_id); + } else if (strcmp(child_xml_name, ELEMENT_MSGREF) == 0) { + char *msgref = NULL; + err = sync_agent_get_text_from_node(child, &msgref); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("MsgRef = [%s]", msgref); + if (msgref != NULL) + pStatus->msg_ref = atoi(msgref); + } else if (strcmp(child_xml_name, ELEMENT_CMDREF) == 0) { + char *cmdref = NULL; + err = sync_agent_get_text_from_node(child, &cmdref); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("CmdRef = [%s]", cmdref); + if (cmdref != NULL) + pStatus->cmd_ref = atoi(cmdref); + } else if (strcmp(child_xml_name, ELEMENT_CMD) == 0) { + char *cmd = NULL; + err = sync_agent_get_text_from_node(child, &cmd); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Cmd = [%s]", cmd); + + pStatus->type = convert_command_type(cmd); + } else if (strcmp(child_xml_name, ELEMENT_TARGETREF) == 0) { + char *targetref = NULL; + location_s *location = NULL; + err = sync_agent_get_text_from_node(child, &targetref); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("TargetRef = [%s]", targetref); + + sa_error_type_e error = create_location(targetref, NULL, &location); + if (error != SA_INTERNAL_OK) { + _DEBUG_ERROR("create_location() failed !, error = %d", error); + goto return_part; + } + pStatus->target_ref = location; + } else if (strcmp(child_xml_name, ELEMENT_SOURCEREF) == 0) { + char *sourceref = NULL; + location_s *location = NULL; + err = sync_agent_get_text_from_node(child, &sourceref); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("SourceRef = [%s]", sourceref); + + sa_error_type_e error = create_location(sourceref, NULL, &location); + if (error != SA_INTERNAL_OK) { + _DEBUG_ERROR("create_location() failed !, error = %d", error); + goto return_part; + } + pStatus->source_ref = location; + } else if (strcmp(child_xml_name, ELEMENT_CRED) == 0) { + _DEBUG_TRACE("Cred\n"); + cred_s *cred = NULL; + cred = __get_cred(child); + if (cred == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pStatus->cred = cred; + } else if (strcmp(child_xml_name, ELEMENT_CHAL) == 0) { + _DEBUG_TRACE("Chal\n"); + chal_s *pChal = NULL; + pChal = _get_chal(child); + if (pChal == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pStatus->chal = pChal; + } else if (strcmp(child_xml_name, ELEMENT_DATA) == 0) { + char *data = NULL; + err = sync_agent_get_text_from_node(child, &data); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + _DEBUG_TRACE("Data = [%s]\n", data); + if (data != NULL) + pStatus->data = strdup(data); + } else if (strcmp(child_xml_name, ELEMENT_ITEM) == 0) { + WBXMLTreeNode *grandchild = NULL; + + for (grandchild = child->children; grandchild != NULL; grandchild = grandchild->next) { + if (grandchild->type != WBXML_TREE_ELEMENT_NODE) + continue; + + const char *grandchild_xml_name = NULL; + grandchild_xml_name = (const char *)wbxml_tag_get_xml_name(grandchild->name); + + if (strcmp(grandchild_xml_name, ELEMENT_SOURCE) == 0) { + location_s *pSourceLocation = _get_location(grandchild); + if (pSourceLocation == NULL) { + err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY; + goto return_part; + } + pStatus->source_ref = pSourceLocation; + } else if (strcmp(grandchild_xml_name, ELEMENT_TARGET) == 0) { + /* need ? */ + } else if (strcmp(grandchild_xml_name, ELEMENT_DATA) == 0) { + /* need ? */ + } + } + } + } + + return_part: + if (err == SYNC_AGENT_PB_RETURN_OK) { + *content = pStatus; + } else { + _DEBUG_ERROR("error"); + free_status(pStatus); + } + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_pb_error_e init_oma_ds_1_2_binder_function_set(sync_agent_pb_protocol_binder_function_set_s ** binder_function_set) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + err = sync_agent_init_binder_function_set(sizeof(oma_ds_binder_function_info) / sizeof(sync_agent_pb_function_info_s), (sync_agent_pb_function_info_s *) oma_ds_binder_function_info, binder_function_set); + + _EXTERN_FUNC_EXIT; + return err; +} + +void free_oma_ds_1_2_binder_function_set(sync_agent_pb_protocol_binder_function_set_s * binder_function_set) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_free_binder_function_set(binder_function_set); + + _EXTERN_FUNC_EXIT; +} + +sync_agent_pb_protocol_binder_info_s *oma_ds_1_2_binder_init(sync_agent_pb_protocol_binder_info_s * binder, sync_agent_pb_encoding_e enc, bool encode_xml_header, + bool use_strtbl, bool produce_anonymous, bool use_flow_mode, bool text_public_id, sync_agent_pb_protocol_binder_function_set_s * binder_function_set) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_pb_protocol_binder_info_s *pOutBinder = NULL; + + pOutBinder = sync_agent_init_protocol_binder(binder, SYNC_AGENT_PB_PROTOCOL_SYNCML_SYNCML12, NULL, enc, encode_xml_header, use_strtbl, produce_anonymous, use_flow_mode, text_public_id, NULL, NULL, binder_function_set); + + if (pOutBinder == NULL) { + goto return_part; + } + + return_part: + + _EXTERN_FUNC_EXIT; + return pOutBinder; +} + +sync_agent_pb_error_e oma_ds_1_2_binder_append(sync_agent_pb_protocol_binder_info_s * binder, oma_ds_protocol_element_e protocol_element, Content_Ptr content) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + err = sync_agent_append_element(binder, protocol_element, content); + + _EXTERN_FUNC_EXIT; + return err; +} + +void oma_ds_1_2_binder_terminate(sync_agent_pb_protocol_binder_info_s * binder) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_destroy_protocol_binder(binder); + + _EXTERN_FUNC_EXIT; +} + +sync_agent_pb_error_e oma_ds_1_2_binder_get_stream(sync_agent_pb_protocol_binder_info_s * binder, char **byte_stream, unsigned int *byte_stream_size) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + err = sync_agent_get_stream_from_protocol_binder(binder, byte_stream, byte_stream_size); + if (err != SYNC_AGENT_PB_RETURN_OK) { + goto return_part; + } + + return_part: + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_pb_error_e oma_ds_1_2_binder_get_stream_size(sync_agent_pb_protocol_binder_info_s * binder, unsigned int *byte_stream_size) +{ + + _EXTERN_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + err = sync_agent_get_stream_size_from_protocol_binder(binder, byte_stream_size); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_pb_error_e oma_ds_1_2_binder_get_encoding(sync_agent_pb_protocol_binder_info_s * binder, sync_agent_pb_encoding_e * enc) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + + err = sync_agent_get_encoding_from_protocol_binder(binder, enc); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_pb_error_e reverse_oma_ds_1_2_binder_init(char *byte_stream, + unsigned int byte_stream_len, sync_agent_pb_decoding_e dec, sync_agent_pb_protocol_binder_function_set_s * binder_function_set, void *user_data, sync_agent_pb_protocol_binder_reverse_info_s ** binder) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_pb_protocol_e protocol = SYNC_AGENT_PB_PROTOCOL_UNKNOWN; + sync_agent_pb_error_e err = sync_agent_init_reverse_protocol_binder(byte_stream, byte_stream_len, dec, + &protocol, binder_function_set, user_data, binder); + + _EXTERN_FUNC_EXIT; + return err; +} + +sync_agent_pb_error_e reverse_oma_ds_1_2_binder_next(sync_agent_pb_protocol_binder_reverse_info_s * pBinder, oma_ds_protocol_element_e * protocol_element, char **protocol_element_name, Content_Ptr * content) +{ + _EXTERN_FUNC_ENTER; + + sync_agent_pb_error_e err = sync_agent_next_element(pBinder, + protocol_element, protocol_element_name, content); + + _EXTERN_FUNC_EXIT; + return err; +} diff --git a/src/agent/service-adapter/sa_command.c b/src/agent/service-adapter/sa_command.c new file mode 100755 index 0000000..7a65503 --- /dev/null +++ b/src/agent/service-adapter/sa_command.c @@ -0,0 +1,1016 @@ +/* + * 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_Command.c + * @version 0.1 + * @brief This file is the source file of implementation of functions for command structure which is used in Service Adapter + */ + +#include <stdlib.h> + +#include <sync_agent.h> + +#include "service-adapter/sa_command.h" +#include "service-adapter/sa_command_internal.h" +#include "service-adapter/sa_elements.h" +#include "service-adapter/sa_elements_internal.h" +#include "service-adapter/sa_session.h" +#include "service-adapter/sa_session_internal.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SA" +#endif + +static command_s *_create_command(session_s * session, command_type_e type); + +static command_s *_create_command(session_s * session, command_type_e type) +{ + _INNER_FUNC_ENTER; + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (session == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (!type) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + command_s *cmd = (command_s *) calloc(1, sizeof(command_s)); + if (cmd == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + cmd->type = type; + cmd->msg_id = session->msg_id; + cmd->cmd_id = session->cmd_id; + cmd->ref_count = 1; + + session->cmd_id++; + + _INNER_FUNC_EXIT; + return cmd; + + error: + _INNER_FUNC_EXIT; + return NULL; +} + +void free_commands(GList * commands) +{ + _EXTERN_FUNC_ENTER; + + retm_if(commands == NULL, "List is NULL"); + + _DEBUG_INFO("start list length is %d", g_list_length(commands)); + + GList *iter = NULL; + command_s *pCommand = NULL; + for (iter = commands; iter != NULL;) { + pCommand = iter->data; + _DEBUG_INFO("command list length is %d command type is %d\n", g_list_length(commands), pCommand->type); + + iter = g_list_next(iter); + if (pCommand->type != COMMAND_TYPE_UNKNOWN) { + commands = g_list_remove(commands, pCommand); + free_command(pCommand); + } + } + + g_list_free(commands); + + _EXTERN_FUNC_EXIT; + return; +} + +sa_error_type_e create_alert_command(session_s * session, alert_type_e sync_type, location_s * source, location_s * target, char *last_anchor, char *next_anchor, cred_s * cred, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start with syncType =%d", sync_type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + anchor_s *pAnchor = NULL; + + if (source == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (target == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *command = _create_command(session, COMMAND_TYPE_ALERT); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->target = target; + (*command)->source = source; + + /*TODO check that sync Type is ALERT_NEXT_MESSAGE(does not need last and next anchor) */ + /*TODO check that sync Type is ALERT_SLOW_SYNC(does not need last anchor) */ + + if (next_anchor != NULL) { + errorType = create_anchor(last_anchor, next_anchor, &pAnchor); + if (errorType != SA_INTERNAL_OK) + goto error; + + (*command)->private.alert.anchor = pAnchor; + pAnchor = NULL; + } + + if (cred != NULL) + (*command)->cred = dup_cred(cred); + + (*command)->private.alert.type = sync_type; + + _EXTERN_FUNC_EXIT; + return errorType; + + error: + + if (pAnchor != NULL) + free_anchor(pAnchor); + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_get_command(session_s * session, location_s * target, const char *content_type, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start with content type = %s", content_type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (target == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *command = _create_command(session, COMMAND_TYPE_GET); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->private.access.item = create_item(); + if ((*command)->private.access.item == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + set_item_target((*command)->private.access.item, target); + + if (content_type != NULL) + (*command)->private.access.item->content_type = strdup(content_type); + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_put_command(session_s * session, location_s * source, const char *content_type, devinf_s * devinf, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start with content type = %s", content_type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (source == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *command = _create_command(session, COMMAND_TYPE_PUT); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->private.access.item = create_item_for_devinf(devinf); + + if ((*command)->private.access.item == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + set_item_source((*command)->private.access.item, source); + + if (content_type != NULL) + (*command)->private.access.item->content_type = strdup(content_type); + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_results_command(session_s * session, location_s * source, const char *content_type, devinf_s * devinf, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start with content type = %s", content_type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (source == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *command = _create_command(session, COMMAND_TYPE_RESULTS); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->private.results.item = create_item_for_devinf(devinf); + + if ((*command)->private.results.item == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + set_item_source((*command)->private.results.item, source); + + if (content_type != NULL) + (*command)->private.results.item->content_type = strdup(content_type); + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_sync_start_command(session_s * session, location_s * source, location_s * target, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (source == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (target == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *command = _create_command(session, COMMAND_TYPE_SYNC_START); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->source = source; + (*command)->target = target; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e set_sync_start_command_number_of_changes(command_s * command, unsigned int number_of_changes) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + command->private.sync.has_num_changed = 1; + command->private.sync.num_changed = number_of_changes; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e set_sync_start_command_mem(command_s * command, mem_s * mem) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + command->private.sync.mem = mem; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; + +} + +sa_error_type_e create_sync_end_command(command_s ** command) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + *command = (command_s *) calloc(1, sizeof(command_s)); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->type = COMMAND_TYPE_SYNC_END; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_add_command(session_s * session, change_type_e type, char *luid, const char *content_type, char *data, unsigned int size, int more_data, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start with ChangeType = %d content type = %s", type, content_type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + item_s *temp = NULL; + location_s *pLocation = NULL; + + *command = _create_command(session, COMMAND_TYPE_ADD); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + temp = create_item_for_data(data, size); + if (!temp) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->private.change.type = type; + if (content_type != NULL) + temp->content_type = strdup(content_type); + temp->more_data = more_data; + + errorType = create_location(luid, NULL, &pLocation); + if (errorType != SA_INTERNAL_OK) + goto error; + + set_item_source(temp, pLocation); + (*command)->private.change.items = g_list_append((*command)->private.change.items, temp); + temp = NULL; + + error: + + if (temp != NULL) + free(temp); + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_replace_command(session_s * session, change_type_e type, char *luid, const char *content_type, const char *data, unsigned int size, int more_data, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start with ChangeType = %d content type = %s", type, content_type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + item_s *temp = NULL; + location_s *pLocation = NULL; + + *command = _create_command(session, COMMAND_TYPE_REPLACE); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + temp = create_item_for_data(data, size); + if (!temp) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->private.change.type = type; + if (content_type != NULL) + temp->content_type = strdup(content_type); + temp->more_data = more_data; + + errorType = create_location(luid, NULL, &pLocation); + if (errorType != SA_INTERNAL_OK) + goto error; + + set_item_source(temp, pLocation); + (*command)->private.change.items = g_list_append((*command)->private.change.items, temp); + temp = NULL; + + error: + + if (temp != NULL) + free(temp); + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_delete_command(session_s * session, change_type_e type, char *luid, const char *content_type, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start with ChangeType = %d content type = %s", type, content_type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + item_s *temp = NULL; + location_s *pLocation = NULL; + + *command = _create_command(session, COMMAND_TYPE_DELETE); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + temp = create_item(); + if (temp == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->private.change.type = type; + + errorType = create_location(luid, NULL, &pLocation); + if (errorType != SA_INTERNAL_OK) + goto error; + + set_item_source(temp, pLocation); + if (content_type != NULL) + temp->content_type = strdup(content_type); + (*command)->private.change.items = g_list_append((*command)->private.change.items, temp); + temp = NULL; + + error: + + if (temp != NULL) + free(temp); + + _EXTERN_FUNC_EXIT; + return errorType; + +} + +sa_error_type_e create_map_command(session_s * session, location_s * source, location_s * target, command_s ** command) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (source == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (target == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *command = _create_command(session, COMMAND_TYPE_MAP); + if (*command == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command)->source = source; + (*command)->target = target; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e set_map_command_item(command_s * map_command, item_s * temp) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (map_command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + map_command->private.map.items = g_list_append(map_command->private.map.items, temp); + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e increase_command_ref_count(command_s * command) +{ + _EXTERN_FUNC_ENTER; + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + command->ref_count++; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e decrease_command_ref_count(command_s * command) +{ + _EXTERN_FUNC_ENTER; + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + command->ref_count--; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e set_results_command_msg_ref(command_s * command, unsigned int msg_ref) +{ + _EXTERN_FUNC_ENTER; + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + command->private.results.msg_ref = msg_ref; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e set_results_command_cmd_ref(command_s * command, unsigned int cmd_ref) +{ + _EXTERN_FUNC_ENTER; + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + command->private.results.cmd_ref = cmd_ref; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e set_results_command_target_ref(command_s * command, location_s * location) +{ + _EXTERN_FUNC_ENTER; + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (command == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + _DEBUG_INFO("start with Command Type =%d\n", command->type); + + command->private.results.target_ref = dup_location(location); + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +void free_command(command_s * command) +{ + _EXTERN_FUNC_ENTER; + + retm_if(command == NULL, "pCommand is NULL"); + + _DEBUG_INFO("start with Command type is %d", command->type); + + GList *iter = NULL; + + if (command->ref_count > 1) { + _DEBUG_INFO("Command's refCount is %d", command->ref_count); + decrease_command_ref_count(command); + return; + } + + switch (command->type) { + case COMMAND_TYPE_ALERT: + if (command->private.alert.anchor != NULL) { + free_anchor(command->private.alert.anchor); + command->private.alert.anchor = NULL; + } + + if (command->private.alert.content_type != NULL) { + free(command->private.alert.content_type); + command->private.alert.content_type = NULL; + } + break; + case COMMAND_TYPE_SYNC_START: + /*nothing to free */ + break; + case COMMAND_TYPE_SYNC_END: + /*nothing to free */ + break; + case COMMAND_TYPE_PUT: + if (command->private.access.type != NULL) { + free(command->private.access.type); + command->private.access.type = NULL; + } + + if (command->private.access.item != NULL) { + free_item(command->private.access.item); + command->private.access.item = NULL; + } + break; + case COMMAND_TYPE_HEADER: + /*COMMAND_TYPE_HEADER doesnot come here */ + break; + case COMMAND_TYPE_ADD: + case COMMAND_TYPE_REPLACE: + case COMMAND_TYPE_DELETE: + for (iter = command->private.change.items; iter != NULL; iter = g_list_next(iter)) + free_item(iter->data); + break; + case COMMAND_TYPE_MAP: + for (iter = command->private.map.items; iter != NULL; iter = g_list_next(iter)) + free_item(iter->data); + break; + case COMMAND_TYPE_GET: + if (command->private.access.type != NULL) { + free(command->private.access.type); + command->private.access.type = NULL; + } + + if (command->private.access.item != NULL) { + free_item(command->private.access.item); + command->private.access.item = NULL; + } + break; + case COMMAND_TYPE_RESULTS: + if (command->private.results.type != NULL) { + free(command->private.results.type); + command->private.results.type = NULL; + } + + if (command->private.results.item != NULL) { + free_item(command->private.results.item); + command->private.results.item = NULL; + } + + if (command->private.results.target_ref != NULL) { + free_location(command->private.results.target_ref); + command->private.results.target_ref = NULL; + } + + break; + case COMMAND_TYPE_UNKNOWN: + break; + } + + if (command->source != NULL) { + free_location(command->source); + command->source = NULL; + } + + if (command->target != NULL) { + free_location(command->target); + command->target = NULL; + } + + if (command->cred != NULL) { + free_cred(command->cred); + command->cred = NULL; + } + + free(command); + + _EXTERN_FUNC_EXIT; +} + +sa_error_type_e create_new_status_location(session_s * session, oma_status_type_e data, command_s * command, location_s * source_ref, location_s * target_ref, command_type_e type, status_s ** status) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start Errortype %d", data); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + errorType = create_status(data, session->cmd_id, session->last_recieved_msg_id, command->cmd_id, source_ref, target_ref, type, status); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->cmd_id++; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_new_status(session_s * session, oma_status_type_e data, command_s * command, command_type_e type, status_s ** status) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start Errortype %d", data); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + errorType = create_status(data, session->cmd_id, session->last_recieved_msg_id, command->cmd_id, command->source, command->target, type, status); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->cmd_id++; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_status(oma_status_type_e data, unsigned int cmd_id, unsigned int msg_ref, unsigned int cmd_ref, location_s * source_ref, location_s * target_ref, command_type_e type, status_s ** status) +{ + _EXTERN_FUNC_ENTER; + _DEBUG_INFO("start Errortype %d", data); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + *status = (status_s *) calloc(1, sizeof(status_s)); + if (*status == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*status)->cmd_id = cmd_id; + (*status)->msg_ref = msg_ref; + (*status)->cmd_ref = cmd_ref; + (*status)->type = type; + (*status)->data = g_strdup_printf("%d", data); + + if (source_ref != NULL) + (*status)->source_ref = dup_location(source_ref); + + if (target_ref != NULL) + (*status)->target_ref = dup_location(target_ref); + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +void free_statuses(GList * statuses) +{ + _EXTERN_FUNC_ENTER; + + retm_if(statuses == NULL, "List is NULL"); + + GList *iter = NULL; + status_s *status = NULL; + _DEBUG_INFO("count : %d", g_list_length(statuses)); + for (iter = statuses; iter != NULL; iter = g_list_next(iter)) { + status = (status_s *) iter->data; + free_status(status); + } + + g_list_free(statuses); + + _EXTERN_FUNC_EXIT; +} + +void free_status(status_s * status) +{ + _EXTERN_FUNC_ENTER; + + retm_if(status == NULL, "pStatus is NULL"); + + if (status->data != NULL) + free(status->data); + + if (status->source_ref != NULL) + free_location(status->source_ref); + + if (status->target_ref != NULL) + free_location(status->target_ref); + + if (status->cred != NULL) + free_cred(status->cred); + + if (status->chal != NULL) + free_chal(status->chal); + + free_item(status->item); + + free(status); + status = NULL; + + _EXTERN_FUNC_EXIT; +} + +oma_status_type_e get_status_code(status_s * status) +{ + _EXTERN_FUNC_ENTER; + + _EXTERN_FUNC_EXIT; + + return atoi(status->data); +} + +command_type_e convert_command_type(char *type) +{ + _EXTERN_FUNC_ENTER; + + command_type_e commandType = COMMAND_TYPE_UNKNOWN; + + retvm_if(type == NULL, commandType, "type is NULL"); + + if (!strcmp(type, "Alert")) { + commandType = COMMAND_TYPE_ALERT; + } else if (!strcmp(type, "Sync")) { + commandType = COMMAND_TYPE_SYNC_START; + } else if (!strcmp(type, "Put")) { + commandType = COMMAND_TYPE_PUT; + } else if (!strcmp(type, "SyncHdr")) { + commandType = COMMAND_TYPE_HEADER; + } else if (!strcmp(type, "Add")) { + commandType = COMMAND_TYPE_ADD; + } else if (!strcmp(type, "Replace")) { + commandType = COMMAND_TYPE_REPLACE; + } else if (!strcmp(type, "Map")) { + commandType = COMMAND_TYPE_MAP; + } else if (!strcmp(type, "Delete")) { + commandType = COMMAND_TYPE_DELETE; + } else if (!strcmp(type, "Results")) { + commandType = COMMAND_TYPE_RESULTS; + } else if (!strcmp(type, "Get")) { + commandType = COMMAND_TYPE_GET; + } + + _EXTERN_FUNC_EXIT; + return commandType; +} + +change_type_e convert_change_type_command_type(command_type_e type) +{ + _EXTERN_FUNC_ENTER; + + change_type_e changeType = CHANGE_UNKNOWN; + switch (type) { + case COMMAND_TYPE_UNKNOWN: + case COMMAND_TYPE_ALERT: + case COMMAND_TYPE_SYNC_START: + case COMMAND_TYPE_SYNC_END: + case COMMAND_TYPE_PUT: + case COMMAND_TYPE_HEADER: + case COMMAND_TYPE_MAP: + case COMMAND_TYPE_GET: + case COMMAND_TYPE_RESULTS: + /*never comes these commands */ + break; + case COMMAND_TYPE_ADD: + changeType = CHANGE_ADD; + break; + case COMMAND_TYPE_REPLACE: + changeType = CHANGE_REPLACE; + break; + case COMMAND_TYPE_DELETE: + changeType = CHANGE_DELETE; + break; + } + + _EXTERN_FUNC_EXIT; + return changeType; +} + +command_type_e convert_command_type_change_type(change_type_e type) +{ + _EXTERN_FUNC_ENTER; + + command_type_e commandType = COMMAND_TYPE_UNKNOWN; + + switch (type) { + case CHANGE_UNKNOWN: + commandType = COMMAND_TYPE_UNKNOWN; + break; + case CHANGE_ADD: + commandType = COMMAND_TYPE_ADD; + break; + case CHANGE_REPLACE: + commandType = COMMAND_TYPE_REPLACE; + break; + case CHANGE_DELETE: + commandType = COMMAND_TYPE_DELETE; + break; + } + + _EXTERN_FUNC_EXIT; + return commandType; +} + +/* +ChangeType convertToChangeType(unsigned int type) { + + ChangeType changeType=CHANGE_UNKNOWN; + switch (type) { + case 1: + changeType = CHANGE_ADD; + break; + case 2: + changeType = CHANGE_REPLACE; + break; + case 3: + changeType = CHANGE_DELETE; + break; + } + + return changeType; +} + +char *convertFromCommandType(CommandType type) { + char *commandType=NULL; + + switch (type) { + case COMMAND_TYPE_ALERT: + commandType = "Alert"; + break; + case COMMAND_TYPE_SYNC_START: + case COMMAND_TYPE_SYNC_END: + commandType = "Sync"; + break; + case COMMAND_TYPE_PUT: + commandType = "Put"; + break; + case COMMAND_TYPE_HEADER: + commandType = "SyncHdr"; + break; + case COMMAND_TYPE_ADD: + commandType = "Add"; + break; + case COMMAND_TYPE_REPLACE: + commandType = "Replace"; + break; + case COMMAND_TYPE_MAP: + commandType = "Map"; + break; + case COMMAND_TYPE_DELETE: + commandType = "Delete"; + break; + case COMMAND_TYPE_RESULTS: + commandType = "Results"; + break; + case COMMAND_TYPE_GET: + commandType = "Get"; + break; + default: + commandType="UNKNOWN"; + } + return commandType; +} +*/ diff --git a/src/agent/service-adapter/sa_common_interface.c b/src/agent/service-adapter/sa_common_interface.c new file mode 100755 index 0000000..614aded --- /dev/null +++ b/src/agent/service-adapter/sa_common_interface.c @@ -0,0 +1,2812 @@ +/* + * 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_Common_Interface.c + * @version 0.1 + * @brief This file is the source file of implementation of interface of Service Adapter + */ + +#include <sync_agent.h> + +#include "common/common_vconf.h" +#include "common/common_define.h" +#include "common/common_util.h" +#include "service-adapter/protocol-binder/oma_ds_protocol_binder.h" +#include "service-adapter/network-binder/na_binder.h" +#include "service-adapter/sa_define.h" +#include "service-adapter/sa_common_interface.h" +#include "service-adapter/sa_elements.h" +#include "service-adapter/sa_elements_internal.h" +#include "service-adapter/sa_command.h" +#include "service-adapter/sa_command_internal.h" +#include "service-adapter/sa_session.h" +#include "service-adapter/sa_session_internal.h" +#include "service-adapter/sa_devinf_internal.h" +#include "service-adapter/sa_util.h" +#include "service-adapter/sa_devinf.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SA" +#endif + +session_s *static_session = NULL; + +static sa_error_type_e _create_session(int account_id, char *session_id, bool server_flag, session_s ** session); +static sa_error_type_e _generate_presync_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size); +static sa_error_type_e _generate_msg(session_s * session, void **sync_obj, bool server_flag, char **msg, unsigned int *msg_size); +static sa_error_type_e _generate_autoconfigure_msg(char *id, char *pwd, char *target_url, char **msg, unsigned int *msg_size, session_s * session); +static sa_error_type_e _generate_suspend_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size); +static sa_error_type_e _exchange_msg(session_s * session, char *acc_type, int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length); + +static sa_error_type_e _process_recv_msg(session_s * session, char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish); +static sa_error_type_e _process_autoconfigure_recv_msg(char *recv_msg, unsigned int recv_msg_length, session_s * session); +static common_error_type_e _convert_error_type(sa_error_type_e error_type); + +static sa_error_type_e __get_account_info(int account_id, char **id, char **pwd, char **server_url, char **next_nonce); +static sa_error_type_e __object_binder(syncml_s * syncml, bool server_flag, char **msg, unsigned int *msg_size); +static sa_error_type_e __reverse_object_binder(syncml_s ** syncml, char *recv_msg, unsigned int recv_msg_length); +static sa_error_type_e __generate_msg_status_map_command(session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last); +static sa_error_type_e __generate_msg_exist_sending_map_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, GList ** commands, GList ** commands_last); +static sa_error_type_e __generate_msg_changes_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last, bool * is_need_next_msg); +static void __clean_up_sa(session_s * session); +static sa_error_type_e __process_jssion_id(session_s * session, GList * recv_header); +static sa_error_type_e __check_resume_session(session_s * session, bool * resume); + +static sa_error_type_e __get_account_info(int account_id, char **id, char **pwd, char **server_url, char **next_nonce) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + GList *config_list = NULL; + GList *iter = NULL; + sync_agent_da_config_s *config_data = NULL; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + + if (!account_id) { + errorType = SA_INTERNAL_MISCONFIGURATION; + goto error; + } + + acc_err = sync_agent_create_fw_account(&fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_fw_account"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + acc_err = sync_agent_get_fw_account(account_id, &fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_update_fw_account"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + + if (fw_account->email != NULL) + *id = strdup(fw_account->email); + + if (fw_account->password != NULL) + *pwd = strdup(fw_account->password); + + da_err = sync_agent_get_config_list(account_id, &config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + for (iter = config_list; iter != NULL; iter = g_list_next(iter)) { + config_data = (sync_agent_da_config_s *) iter->data; + + if (config_data != NULL) { + if (config_data->key != NULL) { + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP) == 0) { + if (config_data->value != NULL) + *server_url = strdup(config_data->value); + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_NEXT_NONCE) == 0) { + if (config_data->value != NULL) + *next_nonce = strdup(config_data->value); + } + } + } + } + + error: + + sync_agent_free_fw_account(fw_account); + + sync_agent_free_config_list(config_list); + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _create_session(int account_id, char *session_id, bool server_flag, session_s ** session) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + char *id = NULL; + char *pwd = NULL; + char *targetUrl = NULL; + char *sourceUrl = NULL; + char *sourceName = NULL; + char *nextNonce = NULL; + chal_s *pTempChal = NULL; + unsigned int sessionID = 0; + + location_s *pSourceLocation = NULL; + location_s *pTargetLocation = NULL; + + if ((*session) != NULL) { + if ((*session)->chal != NULL) { + pTempChal = (*session)->chal; + (*session)->chal = NULL; + } + + if ((*session)->has_opend) + sessionID = (*session)->naci_session_id; + + free_session(*session); + *session = NULL; + } + + sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + _DEBUG_ERROR("failed to get devinfo"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + + errorType = __get_account_info(account_id, &id, &pwd, &targetUrl, &nextNonce); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("faied in __get_accountinfo"); + goto error; + } + + if (server_flag == true) { + sourceName = id; + } + + errorType = create_location(sourceUrl, sourceName, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_location"); + goto error; + } + + errorType = create_location(targetUrl, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_location"); + goto error; + } + + /* create session info */ + errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, account_id, session_id, pSourceLocation, pTargetLocation, session); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create session"); + goto error; + } + + if (pTempChal != NULL) { + (*session)->chal = pTempChal; + pTempChal = NULL; + } + + (*session)->naci_session_id = sessionID; + + error: + + if (id != NULL) + free(id); + if (pwd != NULL) + free(pwd); + if (targetUrl != NULL) + free(targetUrl); + if (sourceUrl != NULL) + free(sourceUrl); + if (nextNonce != NULL) + free(nextNonce); + if (pTempChal != NULL) + free_chal(pTempChal); + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _generate_presync_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + char *id = NULL; + char *pwd = NULL; + char *targetUrl = NULL; + char *nextNonce = NULL; + char *credData = NULL; + char *decoded_nonce = NULL; + bool resume = false; + char *value = NULL; + alert_type_e alert_type = ALERT_UNKNOWN; + + /*pkg1 always has final tag */ + int isFinal = 1; + + sync_hdr_s *pSyncHdr = NULL; + cred_s *pCred = NULL; + devinf_s *devInfObj = NULL; + syncml_s *syncml = NULL; + + /*case SAN, MUST use sessionid from SAN message.. */ + if (session->session_id == NULL) { + session->session_id = g_strdup_printf("%ld", time(NULL)); + if (session->session_id == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + } + + errorType = __get_account_info(session->account_id, &id, &pwd, &targetUrl, &nextNonce); + if (errorType != SA_INTERNAL_OK) + goto error; + + _DEBUG_TRACE("id = %s", id); + _DEBUG_TRACE("pwd = %s", pwd); + _DEBUG_TRACE("sessionId = %s", session->session_id); + _DEBUG_TRACE("serverUrl = %s", targetUrl); + + /* set pkg status */ + session->pkg_status = SYNCML_PKG_1; + + chal_s *chal = session->chal; + auth_type_e authType; + format_type_e formatType = FORMAT_TYPE_UNKNOWN; + unsigned int nonce_size = 0; + if (chal == NULL) { + _DEBUG_TRACE("CHAL DOES NOT EXIST"); + if (nextNonce != NULL) { + /*server sent nonce info in previous sync session */ + authType = AUTH_TYPE_MD5; + decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size); + formatType = FORMAT_TYPE_BASE64; + } else { + authType = AUTH_TYPE_BASIC; + formatType = FORMAT_TYPE_BASE64; + } + + errorType = create_cred_string(authType, id, pwd, decoded_nonce, nonce_size, &credData); + + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create cred string"); + goto error; + } + + errorType = create_cred(id, pwd, authType, formatType, credData, &pCred); + } else { + _DEBUG_TRACE("CHAL DOES EXIST"); + if (chal->type == AUTH_TYPE_MD5) { + if (chal->format == FORMAT_TYPE_BASE64) + decoded_nonce = (char *)g_base64_decode(chal->nonce_b64, &nonce_size); + else { + if (chal->nonce_plain != NULL) { + decoded_nonce = strdup(chal->nonce_plain); + nonce_size = chal->nonce_length; + } + } + } + + errorType = create_cred_string(chal->type, id, pwd, decoded_nonce, nonce_size, &credData); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create cred string"); + goto error; + } + + errorType = create_cred(id, pwd, chal->type, FORMAT_TYPE_BASE64, credData, &pCred); + } + + if (credData != NULL) { + free(credData); + credData = NULL; + } + + if (decoded_nonce != NULL) { + free(decoded_nonce); + decoded_nonce = NULL; + } + + if (nextNonce != NULL) { + free(nextNonce); + nextNonce = NULL; + } + nonce_size = 0; + + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create cred"); + goto error; + } + + set_session_cred(session, pCred); + pCred = NULL; + + /* create SyncHdr */ + errorType = create_sync_hdr(session, &pSyncHdr); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create synchdr"); + goto error; + } + + /* create devInf */ + errorType = create_devinf(session, &devInfObj); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInf"); + goto error; + } + + set_session_devinf(session, devInfObj); + + errorType = __check_resume_session(session, &resume); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __check_resume_session"); + goto error; + } + + GList *commands = NULL; + GList *commands_last = NULL; + + char *sourceDevInf = NULL; + char *contentType = NULL; + + if (session->protocol_version == VERSION_10) + sourceDevInf = ELEMENT_DEVINF_10; + else if (session->protocol_version == VERSION_11) + sourceDevInf = ELEMENT_DEVINF_11; + else if (session->protocol_version == VERSION_12) + sourceDevInf = ELEMENT_DEVINF_12; + + if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML) + contentType = ELEMENT_DEVINF_XML; + else + contentType = ELEMENT_DEVINF_WBXML; + + if (resume != true) { + if (server_flag != true) { + /* create Put command */ + command_s *pPutCommand = NULL; + location_s *pLocation = NULL; + + errorType = create_location(sourceDevInf, NULL, &pLocation); + if (errorType != SA_INTERNAL_OK) + goto error; + + errorType = create_put_command(session, pLocation, contentType, devInfObj, &pPutCommand); + if (errorType != SA_INTERNAL_OK) + goto error; + put_into_list(&commands, &commands_last, pPutCommand); + } + } + + /* if it is not dive server */ + if (server_flag != true) { + /* create Get command */ + command_s *pGetCommand = NULL; + location_s *location = NULL; + errorType = create_location(sourceDevInf, NULL, &location); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_get_command(session, location, contentType, &pGetCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create get command"); + goto error; + } + put_into_list(&commands, &commands_last, pGetCommand); + } + + command_s *pAlertCommand = NULL; + location_s *pSourceLocation = NULL; + location_s *pTargetLocation = NULL; + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + + /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error + when SA_INTERNAL_NOT_DEFINED error case */ + if (errorType == SA_INTERNAL_NOT_DEFINED) + errorType = SA_INTERNAL_MISCONFIGURATION; + + goto error; + } + + errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + + /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error + when SA_INTERNAL_NOT_DEFINED error case */ + if (errorType == SA_INTERNAL_NOT_DEFINED) + errorType = SA_INTERNAL_MISCONFIGURATION; + goto error; + } + + _DEBUG_TRACE("datastoreinfo_per_content_type[%d]->id = %d", content_type, datastoreinfo_per_content_type[content_type]->id); + _DEBUG_TRACE("datastoreinfo_per_content_type[%d]->pw = %d", content_type, datastoreinfo_per_content_type[content_type]->pw); + + if (datastoreinfo_per_content_type[content_type]->id != NULL && datastoreinfo_per_content_type[content_type]->pw != NULL) { + + /*FIXME is there a nextNonce for datastore ?? */ + if (nextNonce != NULL) { + /*server sent nonce info in previous sync session */ + authType = AUTH_TYPE_MD5; + decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size); + } else { + authType = AUTH_TYPE_BASIC; + formatType = FORMAT_TYPE_BASE64; + } + + errorType = create_cred_string(authType, datastoreinfo_per_content_type[content_type]->id, datastoreinfo_per_content_type[content_type]->pw, decoded_nonce, nonce_size, &credData); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create cred string"); + goto error; + } + + errorType = create_cred(id, pwd, authType, FORMAT_TYPE_BASE64, credData, &pCred); + + if (credData != NULL) { + free(credData); + credData = NULL; + } + } + + if (resume == true) + alert_type = ALERT_RESUME; + else + alert_type = datastoreinfo_per_content_type[content_type]->client_sync_type; + + errorType = create_alert_command(session, alert_type, + pSourceLocation, pTargetLocation, datastoreinfo_per_content_type[content_type]->last_anchor_client, datastoreinfo_per_content_type[content_type]->next_anchor_client, pCred, &pAlertCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create alert command"); + goto error; + } + } else + continue; + put_into_list(&commands, &commands_last, pAlertCommand); + } + } + + errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &syncml); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create syncml"); + goto error; + } + + session->is_sending_final = isFinal; + if (isFinal) + session->pkg_status = SYNCML_PKG_2; + + /* convert Msg */ + errorType = __object_binder(syncml, server_flag, msg, msg_size); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in objectBinder"); + goto error; + } + + /*free generate structure */ + free_syncml(syncml); + + if (id != NULL) + free(id); + if (pwd != NULL) + free(pwd); + if (targetUrl != NULL) + free(targetUrl); + if (nextNonce != NULL) + free(nextNonce); + if (value != NULL) + free(value); + if (pCred != NULL) + free_cred(pCred); + + _INNER_FUNC_EXIT; + return errorType; + + error: + + if (credData != NULL) + free(credData); + + if (id != NULL) + free(id); + if (pwd != NULL) + free(pwd); + if (targetUrl != NULL) + free(targetUrl); + if (nextNonce != NULL) + free(nextNonce); + if (value != NULL) + free(value); + if (syncml != NULL) + free_syncml(syncml); + if (pSyncHdr != NULL) + free_sync_hdr(pSyncHdr); + if (pCred != NULL) + free_cred(pCred); + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _generate_msg(session_s * session, void **sync_obj, bool server_flag, char **msg, unsigned int *msg_size) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + bool isNeedNextMsg = false; + + sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL; + sync_agent_pb_protocol_binder_info_s *binder = NULL; + + sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set); + + if (server_flag != true) + binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, false, false, true, false, binder_function_set); + else + binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, true, false, true, true, binder_function_set); + + /* create SyncHdr */ + sync_hdr_s *pSyncHdr = NULL; + errorType = create_sync_hdr(session, &pSyncHdr); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create synchdr"); + goto error; + } + + err = oma_ds_1_2_binder_append(binder, PE_SYNCML_START, NULL); + err = oma_ds_1_2_binder_append(binder, PE_HEADER, pSyncHdr); + err = oma_ds_1_2_binder_append(binder, PE_BODY_START, NULL); + + sync_obj_s **sync = (sync_obj_s **) sync_obj; + GList *commands = NULL; + GList *commands_last = NULL; + int isFinal = 0; + + errorType = __generate_msg_status_map_command(&session, sync, &commands, &commands_last); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __generate_msg_status_map_command"); + goto error; + } + + /*append status command */ + GList *status_iter = NULL; + status_s *pStatus = NULL; + for (status_iter = session->status; status_iter != NULL; status_iter = g_list_next(status_iter)) { + pStatus = (status_s *) status_iter->data; + err = oma_ds_1_2_binder_append(binder, PE_STATUS, pStatus); + } + + /*append results command */ + GList *results_iter = NULL; + command_s *pResultsCommand = NULL; + for (results_iter = session->results_command; results_iter != NULL; results_iter = g_list_next(results_iter)) { + pResultsCommand = (command_s *) results_iter->data; + put_into_list(&commands, &commands_last, pResultsCommand); + err = oma_ds_1_2_binder_append(binder, PE_RESULTS_START, pResultsCommand); + + if (pResultsCommand->type == COMMAND_TYPE_RESULTS) { + if (pResultsCommand->private.results.item != NULL) { + if (pResultsCommand->private.results.item->data_type == ITEM_DEVINF) { + err = oma_ds_1_2_binder_append(binder, PE_DEVINF, pResultsCommand->private.results.item); + } + } + + err = oma_ds_1_2_binder_append(binder, PE_RESULTS_END, NULL); + } + } + + /*append map command */ + GList *map_iter = NULL; + command_s *pMapCommand = NULL; + for (map_iter = session->map_command; map_iter != NULL; map_iter = g_list_next(map_iter)) { + pMapCommand = (command_s *) map_iter->data; + err = oma_ds_1_2_binder_append(binder, PE_MAP, pMapCommand); + } + + /* free sendgStatus structure from SE */ + free_sending_statuses((*sync)->sending_status); + (*sync)->sending_status = NULL; + + if (session->pkg_status == SYNCML_PKG_3) { + + errorType = __generate_msg_exist_sending_map_command(binder, &session, &commands, &commands_last); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __generate_msg_status_map_command"); + goto error; + } + + errorType = __generate_msg_changes_command(binder, &session, sync, &commands, &commands_last, &isNeedNextMsg); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __generate_msg_status_map_command"); + goto error; + } + + if (!isNeedNextMsg) + isFinal = 1; + } + + if (session->pkg_status == SYNCML_PKG_5 && session->is_receiving_final) + isFinal = 1; + + /*if we have to send alert command(222,223 etc) */ + GList *alert_iter = NULL; + command_s *pAlertCommand = NULL; + for (alert_iter = session->alert_command; alert_iter != NULL; alert_iter = g_list_next(alert_iter)) { + pAlertCommand = alert_iter->data; + put_into_list(&commands, &commands_last, pAlertCommand); + err = oma_ds_1_2_binder_append(binder, PE_ALERT, pAlertCommand); + } + + if (isFinal) + err = oma_ds_1_2_binder_append(binder, PE_FINAL, NULL); + + session->is_sending_final = isFinal; + + err = oma_ds_1_2_binder_append(binder, PE_BODY_END, NULL); + err = oma_ds_1_2_binder_append(binder, PE_SYNCML_END, NULL); + + oma_ds_1_2_binder_get_stream(binder, msg, msg_size); + _DEBUG_TRACE("final msg size = %d", *msg_size); + + /*FIXME for debugging */ + char *xml = NULL; + unsigned int xml_len = 0; + if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML) + set_xml_to_file(*msg, OMA_DS_MSG_PATH); + else { + err = sync_agent_get_xml_from_protocol_binder(binder, &xml, &xml_len); + if (err == SYNC_AGENT_PB_RETURN_OK) { + set_xml_to_file(xml, OMA_DS_MSG_PATH); + + if (xml != NULL) { + free(xml); + xml = NULL; + } + } + } + + oma_ds_1_2_binder_terminate(binder); + + if (session->pkg_status == SYNCML_PKG_3 && session->is_sending_final) + session->pkg_status = SYNCML_PKG_4; + else if (session->pkg_status == SYNCML_PKG_5 && session->is_sending_final) + session->pkg_status = SYNCML_PKG_6; + + free_sync_hdr(pSyncHdr); + if (session->status != NULL) { + if (session->suspend_status != NULL) { + free_statuses(session->suspend_status); + session->suspend_status = NULL; + } + session->suspend_status = static_session->status; + session->status = NULL; + } + free_commands(commands); + + session->alert_command = NULL; + session->results_command = NULL; + + _INNER_FUNC_EXIT; + return errorType; + + error: + + free_sync_hdr(pSyncHdr); + if (session->status != NULL) { + free_statuses(session->status); + session->status = NULL; + } + free_commands(commands); + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e __generate_msg_status_map_command(session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + + GList *iter = NULL; + sending_status_s *sendingStatus = NULL; + /* send status of sync, add, replace, delete command */ + for (iter = (*sync)->sending_status; iter != NULL; iter = g_list_next(iter)) { + sendingStatus = (iter->data); + + int needMap = 0; + GList *sendingIter = NULL; + applied_status_s *appliedStatus = NULL; + for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) { + appliedStatus = (sendingIter->data); + + /* set data in status of add, replace, delete command */ + GList *statusIter = NULL; + status_s *status = NULL; + for (statusIter = (*session)->temp_status; statusIter != NULL; statusIter = g_list_next(statusIter)) { + status = statusIter->data; + + if (status->type == COMMAND_TYPE_ADD || status->type == COMMAND_TYPE_REPLACE || status->type == COMMAND_TYPE_DELETE) { + + /* if there is a status for add command it have to be checked + because map command has to be generated + otherwise replace, delete command doesnot need to generate map command */ + if (status->type == COMMAND_TYPE_ADD) + needMap = 1; + + if (status->source_ref != NULL) { + /*add command */ + sync_agent_da_mapping_s *mapping = NULL; + da_err = sync_agent_create_mapping(&mapping); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_mapping !!"); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_da_get_mapping_query_s query; + query.account_id = (*session)->account_id; + query.luid = appliedStatus->luid; + + da_err = sync_agent_get_mapping(&query, &mapping); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_mapping !!"); + errorType = SA_INTERNAL_DA_ERROR; + sync_agent_free_mapping(mapping); + goto error; + } + + if (mapping != NULL) { + if (mapping->guid == NULL) { + errorType = SA_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed to get guid"); + sync_agent_free_mapping(mapping); + goto error; + } + + if (strcmp(mapping->guid, get_location_loc_uri(status->source_ref)) == 0) { + if (status->data != NULL) { + free(status->data); + status->data = NULL; + } + status->data = g_strdup_printf("%d", appliedStatus->status); + /*move to status list from tempStatus */ + (*session)->status = g_list_append((*session)->status, status); + (*session)->temp_status = g_list_remove((*session)->temp_status, status); + + sync_agent_free_mapping(mapping); + + break; + } + + sync_agent_free_mapping(mapping); + + } else { + _DEBUG_ERROR("mapping is NULL !!"); + errorType = SA_INTERNAL_DA_ERROR; + sync_agent_free_mapping(mapping); + goto error; + } + } + + if (status->target_ref != NULL) { + /*replace, delete */ + if (strcmp(appliedStatus->luid, get_location_loc_uri(status->target_ref)) == 0) { + if (status->data != NULL) { + free(status->data); + status->data = NULL; + } + status->data = g_strdup_printf("%d", appliedStatus->status); + /*move to status list from tempStatus */ + (*session)->status = g_list_append((*session)->status, status); + (*session)->temp_status = g_list_remove((*session)->temp_status, status); + break; + } + } + } + } + } + + if (needMap) { + /* create map command */ + command_s *mapCommand = NULL; + location_s *pSourceLocation = NULL; + location_s *pTargetLocation = NULL; + errorType = create_location(sendingStatus->source, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_location(sendingStatus->target, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_map_command(*session, pSourceLocation, pTargetLocation, &mapCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create map command"); + goto error; + } + + GList *sendingIter = NULL; + applied_status_s *appliedStatus = NULL; + + for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) { + appliedStatus = (sendingIter->data); + + if (appliedStatus->change_type == CHANGE_ADD) { + + if (appliedStatus->status == ITEM_ADDED) { + item_s *temp = create_item(); + if (temp == NULL) { + _DEBUG_ERROR("failed to create item"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + errorType = create_location(appliedStatus->luid, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + sync_agent_da_mapping_s *mapping = NULL; + da_err = sync_agent_create_mapping(&mapping); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_mapping !!"); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_da_get_mapping_query_s query; + query.account_id = (*session)->account_id; + query.luid = appliedStatus->luid; + + da_err = sync_agent_get_mapping(&query, &mapping); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_mapping !!"); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + if (mapping != NULL) { + if (mapping->guid == NULL) { + errorType = SA_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed to get guid"); + goto error; + } + errorType = create_location(mapping->guid, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + if (mapping->guid != NULL) { + free(mapping->guid); + mapping->guid = NULL; + } + } else { + _DEBUG_ERROR("mapping is NULL !!"); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + set_item_source(temp, pSourceLocation); + set_item_target(temp, pTargetLocation); + set_map_command_item(mapCommand, temp); + } else { + sync_agent_da_delete_mapping_query_s query; + query.option = SYNC_AGENT_DA_DELETE_MAPPING_OPTION_LUID; + query.account_id = (*session)->account_id; + query.luid = appliedStatus->luid; + + da_err = sync_agent_delete_mapping(&query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + errorType = SA_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed in sync_agent_delete_mapping !!"); + goto error; + } + } + } + } + put_into_list(commands, commands_last, mapCommand); + (*session)->map_command = g_list_append((*session)->map_command, mapCommand); + increase_command_ref_count(mapCommand); + } + } + + error: + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e __generate_msg_exist_sending_map_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, GList ** commands, GList ** commands_last) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS; + + /*if there is something in the mapping_tbl it have to be sent before sync command */ + int existSendingMap = sync_agent_is_exist_mapping_by_account_id((*session)->account_id); + + if (existSendingMap) { + GList *mapping_list = NULL; + sync_agent_da_get_mapping_list_query_s query; + query.option = SYNC_AGENT_DA_GET_MAPPING_LIST_OPTION_ACCOUNT_ID; + query.account_id = (*session)->account_id; + + ret = sync_agent_get_mapping_list(&query, &mapping_list); + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_mapping_list !!"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + + location_s *pSourceLocation = NULL; + location_s *pTargetLocation = NULL; + + bool exist = false; + int content_type; + sync_agent_da_mapping_s *mapping_data = (sync_agent_da_mapping_s *) ((GList *) (g_list_nth(mapping_list, g_list_length(mapping_list) - 1))->data); + int lastItemTypeId = mapping_data->data_store_id; + + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->datastore_id = %d", content_type, datastoreinfo_per_content_type[content_type]->datastore_id); + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + if (lastItemTypeId == datastoreinfo_per_content_type[content_type]->datastore_id) { + errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + exist = true; + break; + } + } + } + } + + if (exist == false) { + _DEBUG_VERBOSE("Mapping data is not equal with any synchronizing datastore id"); + goto error; + } + + /* create map command using first item */ + command_s *mapCommand = NULL; + errorType = create_map_command(*session, pSourceLocation, pTargetLocation, &mapCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create map command"); + goto error; + } + + GList *iter = NULL; + sync_agent_da_mapping_s *iter_data; + for (iter = mapping_list; iter != NULL; iter = g_list_next(iter)) { + iter_data = NULL; + iter_data = (sync_agent_da_mapping_s *) (iter->data); + + if (lastItemTypeId != iter_data->data_store_id) { + err = oma_ds_1_2_binder_append(binder, PE_MAP, mapCommand); + put_into_list(commands, commands_last, mapCommand); + (*session)->map_command = g_list_append((*session)->map_command, mapCommand); + increase_command_ref_count(mapCommand); + + bool exist = false; + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + if (lastItemTypeId == datastoreinfo_per_content_type[content_type]->datastore_id) { + errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + exist = true; + break; + } + } + } + } + if (exist == false) { + _DEBUG_ERROR("Mapping data is not equal with any synchronizing datastore id"); + goto error; + } + } + + lastItemTypeId = iter_data->data_store_id; + item_s *temp = create_item(); + if (temp == NULL) { + _DEBUG_ERROR("failed to create item"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + errorType = create_location(iter_data->luid, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_location(iter_data->guid, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + set_item_source(temp, pSourceLocation); + set_item_target(temp, pTargetLocation); + set_map_command_item(mapCommand, temp); + } + + sync_agent_free_mapping_list(mapping_list); + + err = oma_ds_1_2_binder_append(binder, PE_MAP, mapCommand); + put_into_list(commands, commands_last, mapCommand); + (*session)->map_command = g_list_append((*session)->map_command, mapCommand); + increase_command_ref_count(mapCommand); + } + + error: + + _INNER_FUNC_EXIT; + return errorType; + +} + +static sa_error_type_e __generate_msg_changes_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last, bool * is_need_next_msg) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + sync_agent_da_service_item_s *service_item = NULL; + sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK; + unsigned int stream_size = 0; + + char *data = NULL; /*have to be freed */ + char *sendingData = NULL; + + /* generate sync , add, replace, delete command when there is a item in ChangedDatastore */ + GList *iter = NULL; + command_s *syncCommand = NULL; + changed_datastore_s *changedDatastore = NULL; + bool needNextMsg = false; + for (iter = (*sync)->changed_datastore; iter != NULL; iter = g_list_next(iter)) { + changedDatastore = (iter->data); + + if (changedDatastore->need_sync_command) { + /* create sync Command */ + location_s *pSourceLocation = NULL; + location_s *pTargetLocation = NULL; + errorType = create_location(changedDatastore->source, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_location(changedDatastore->target, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + errorType = create_sync_start_command(*session, pSourceLocation, pTargetLocation, &syncCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create sync start command"); + goto error; + } + + /*set numberOfChanges */ + if (changedDatastore->has_number_of_changes) { + /*if it is first sync command set numberOfChanges */ + set_sync_start_command_number_of_changes(syncCommand, changedDatastore->number_of_changes); + changedDatastore->has_number_of_changes = 0; + } + + /*set Mem */ + /* TODO get current datastore dynamic memory size & recods */ + /* And set Mem structure */ + /*Mem *mem = create_mem(); + set_mem_sharedmem(mem, 1); + set_mem_freemem(mem, 8100); + set_mem_freeid(mem, 81); + set_sync_start_command_mem(syncCommand, mem); */ + + err = oma_ds_1_2_binder_append(binder, PE_SYNC_START, syncCommand); + put_into_list(commands, commands_last, syncCommand); + + GList *itemIter = NULL; + command_s *changeCommand = NULL; + changed_item_s *changedItem = NULL; + oma_ds_protocol_element_e protocol_element = PE_UNDEF; + for (itemIter = changedDatastore->change_item; itemIter != NULL;) { + changedItem = (itemIter->data); + + oma_ds_1_2_binder_get_stream_size(binder, &stream_size); + _DEBUG_VERBOSE("stream_size = %d", stream_size); + _DEBUG_VERBOSE("(*session)->targetMaxMsgSize = %d", (*session)->target_max_msg_size); + + if ((*session)->target_max_msg_size > stream_size) { + /*there is a space for command */ + if (changedItem->change_type == CHANGE_DELETE) { + /* create delete Command */ + errorType = create_delete_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, &changeCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create delete command"); + goto error; + } + } else { + int isFirstLargeObj; + if (changedItem->data == NULL) { + char *folderId = NULL; + GList *list = NULL; + sync_agent_da_get_folder_id_list_query_s query; + query.option = SYNC_AGENT_DA_GET_FOLDER_ID_OPTION_FOLDER_TYPE_ID; + query.account_id = (*session)->account_id; + query.item_type_id = datastoreinfo_per_content_type[changedItem->index_of_datastore]->datastore_id; + query.folder_type_id = datastoreinfo_per_content_type[changedItem->index_of_datastore]->folder_type_id; + + da_err = sync_agent_get_folder_id_list(&query, &list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_get_folder_id_list_inferface() failed !!"); + goto error; + } + + if (g_list_length(list) > 0) { + GList *iter = g_list_nth(list, 0); + folderId = (char *)(iter->data); + } else { + _DEBUG_ERROR("failed to get folderId"); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->datastore_id = %d", changedItem->index_of_datastore, datastoreinfo_per_content_type[changedItem->index_of_datastore]->datastore_id); + _DEBUG_VERBOSE("account_id = %d", (*session)->account_id); + _DEBUG_VERBOSE("folderId = %s", folderId); + _DEBUG_VERBOSE("changedItem->luid = %s", changedItem->luid); + + da_err = sync_agent_create_service_item(&service_item); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + errorType = SA_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err); + goto error; + } + + da_err = sync_agent_get_service_item(changedItem->luid, &service_item); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + errorType = SA_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed in sync_agent_get_service_item() = %d", da_err); + goto error; + } + data = g_strdup((char *)(service_item->data)); + + _DEBUG_VERBOSE("data = %s", data); + sync_agent_free_folder_id_list(list); + if (data == NULL) { + da_err = sync_agent_delete_item(changedItem->luid, 1); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_delete_item !!"); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + itemIter = g_list_next(itemIter); + changedDatastore->change_item = g_list_remove(changedDatastore->change_item, changedItem); + free_changed_item(changedItem); + continue; + } + + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_service_item =%d", da_err); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + isFirstLargeObj = 1; + } else { + data = changedItem->data; + changedItem->data = NULL; + isFirstLargeObj = 0; + } + + unsigned int sizeOfData = 0; + if (data != NULL) + sizeOfData = strlen(data); + + _DEBUG_VERBOSE("sizeOfData = %d", sizeOfData); + if ((*session)->target_max_msg_size < stream_size + sizeOfData) { + /*can be LargeObj */ + int availableSize = (*session)->target_max_msg_size - stream_size; + if ((*session)->remote_devinf->supports_large_objs /*&& availableSize > session->targetMaxMsgSize / 50 */ ) { + + /*if server support LargeObj & clearly LargeObj */ + sendingData = calloc(availableSize + 1, sizeof(char)); + if (sendingData == NULL) { + _DEBUG_ERROR("failed to allocate sendingData"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + memcpy(sendingData, data, availableSize); + _DEBUG_VERBOSE("sendingData = %s", sendingData); + _DEBUG_VERBOSE("sendingData size = %d", strlen(sendingData)); + + char *remainingData = calloc(sizeOfData - availableSize + 1, sizeof(char)); + if (remainingData == NULL) { + _DEBUG_ERROR("failed to allocate remainingData"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + memcpy(remainingData, data + availableSize, sizeOfData - availableSize); + _DEBUG_VERBOSE("sizeOfData - availableSize = %d", sizeOfData - availableSize); + _DEBUG_VERBOSE("remainingData = %s", remainingData); + _DEBUG_VERBOSE("remainingData size = %d", strlen(remainingData)); + + if (changedItem->data != NULL) + free(changedItem->data); + + set_changed_item_data(changedItem, remainingData); + free(remainingData); + + command_status_s *pTemp = NULL; + errorType = create_command_status((*session)->msg_id, (*session)->cmd_id, &pTemp); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create pending status"); + goto error; + } + (*session)->large_obj = pTemp; + + if (changedItem->change_type == CHANGE_ADD) { + /* create add Command */ + errorType = create_add_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1, &changeCommand); + } else if (changedItem->change_type == CHANGE_REPLACE) { + /* create replace Command */ + errorType = create_replace_command(*session, changedItem->change_type, changedItem->luid, + changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1, &changeCommand); + } + + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create add(replace) command"); + goto error; + } + + if (sendingData != NULL) { + free(sendingData); + sendingData = NULL; + } + } else { + needNextMsg = true; + break; + } + } else { + /*can append more commnad */ + if (changedItem->change_type == CHANGE_ADD) { + /* create add Command */ + errorType = create_add_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, data, 0, 0, &changeCommand); + } else if (changedItem->change_type == CHANGE_REPLACE) { + /* create replace Command */ + errorType = create_replace_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, data, 0, 0, &changeCommand); + } + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create add(replace) command"); + goto error; + } + } + } + } else { + needNextMsg = true; + break; + } + + if (changeCommand->type == COMMAND_TYPE_ADD) + protocol_element = PE_ADD; + else if (changeCommand->type == COMMAND_TYPE_REPLACE) + protocol_element = PE_REPLACE; + else if (changeCommand->type == COMMAND_TYPE_DELETE) + protocol_element = PE_DELETE; + + err = oma_ds_1_2_binder_append(binder, protocol_element, changeCommand); + oma_ds_1_2_binder_get_stream_size(binder, &stream_size); + _DEBUG_VERBOSE("stream size after Changes Command= %d", stream_size); + put_into_list(commands, commands_last, changeCommand); + + if (changedItem->data == NULL) { + itemIter = g_list_next(itemIter); + changedDatastore->sent_item = g_list_append(changedDatastore->sent_item, changedItem); + changedDatastore->change_item = g_list_remove(changedDatastore->change_item, changedItem); + } else { + needNextMsg = true; + break; + } + + if (data != NULL) { + free(data); + data = NULL; + } + } + + /* create sync end Command */ + errorType = create_sync_end_command(&syncCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create sync end command"); + goto error; + } + err = oma_ds_1_2_binder_append(binder, PE_SYNC_END, syncCommand); + put_into_list(commands, commands_last, syncCommand); + + if (needNextMsg) + break; + + int chageItemCount = g_list_length(changedDatastore->change_item); + if (chageItemCount == 0) + changedDatastore->need_sync_command = 0; + } + } + + *is_need_next_msg = needNextMsg; + + error: + + if (sendingData != NULL) { + free(sendingData); + sendingData = NULL; + } + + if (data != NULL) { + free(data); + data = NULL; + } + + if (service_item != NULL) { + sync_agent_free_service_item(service_item); + } + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _generate_suspend_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + char *id = NULL; + char *pwd = NULL; + char *sourceUrl = NULL; + char *targetUrl = NULL; + char *nextNonce = NULL; + + sync_hdr_s *pSyncHdr = NULL; + syncml_s *syncml = NULL; + + GList *commands = NULL; + GList *commands_last = NULL; + + command_s *pAlertCommand = NULL; + location_s *pSourceLocation = NULL; + location_s *pTargetLocation = NULL; + + /* change pkgstatus tp SYNCML_SUSPEND */ + session->pkg_status = SYNCML_SUSPEND; + + sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + _DEBUG_ERROR("failed to get devinfo"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + + errorType = __get_account_info(session->account_id, &id, &pwd, &targetUrl, &nextNonce); + if (errorType != SA_INTERNAL_OK) + goto error; + + _DEBUG_TRACE("id = %s", id); + _DEBUG_TRACE("pwd = %s", pwd); + _DEBUG_TRACE("clientUrl = %s", sourceUrl); + _DEBUG_TRACE("serverUrl = %s", targetUrl); + _DEBUG_TRACE("nextNonce = %s", nextNonce); + + /* create SyncHdr */ + errorType = create_sync_hdr(session, &pSyncHdr); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create synchdr"); + goto error; + } + +/* Command *pMapCommand = NULL; + GList *iter = NULL; + for (iter = session->mapCommand; iter != NULL; iter = g_list_next(iter)) { + pMapCommand = (Command *)iter->data; + put_into_list(&commands, &commands_last, pMapCommand); + increase_command_refcount(pMapCommand); + }*/ + + errorType = create_location(targetUrl, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + + /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error + when SA_INTERNAL_NOT_DEFINED error case */ + if (errorType == SA_INTERNAL_NOT_DEFINED) + errorType = SA_INTERNAL_MISCONFIGURATION; + goto error; + } + + errorType = create_location(sourceUrl, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + + /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error + when SA_INTERNAL_NOT_DEFINED error case */ + if (errorType == SA_INTERNAL_NOT_DEFINED) + errorType = SA_INTERNAL_MISCONFIGURATION; + goto error; + } + + errorType = create_alert_command(session, ALERT_SUSPEND, pSourceLocation, pTargetLocation, NULL, NULL, NULL, &pAlertCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create alert command"); + goto error; + } + + put_into_list(&commands, &commands_last, pAlertCommand); + + errorType = create_syncml(pSyncHdr, session->suspend_status, commands, 0, &syncml); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create syncml"); + goto error; + } + + /* convert Msg */ + errorType = __object_binder(syncml, server_flag, msg, msg_size); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in objectBinder"); + goto error; + } + + /*free generate structure */ + free_syncml(syncml); + session->suspend_status = NULL; + + if (id != NULL) + free(id); + if (pwd != NULL) + free(pwd); + if (sourceUrl != NULL) + free(sourceUrl); + if (targetUrl != NULL) + free(targetUrl); + if (nextNonce != NULL) + free(nextNonce); + + _INNER_FUNC_EXIT; + return errorType; + + error: + + if (id != NULL) + free(id); + if (pwd != NULL) + free(pwd); + if (sourceUrl != NULL) + free(sourceUrl); + if (targetUrl != NULL) + free(targetUrl); + if (nextNonce != NULL) + free(nextNonce); + if (syncml != NULL) + free_syncml(syncml); + if (pSyncHdr != NULL) + free_sync_hdr(pSyncHdr); + + session->suspend_status = NULL; + + _INNER_FUNC_EXIT; + return errorType; + +} + +static sa_error_type_e _exchange_msg(session_s * session, char *acc_type, int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length) +{ + _INNER_FUNC_ENTER; + + sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS; + sa_error_type_e errorType = SA_INTERNAL_OK; + bool isXML; + char *targetUri = NULL; + char *jsessionId = NULL; + GList *header_info = NULL; + GList *recv_header = NULL; + + if (session != NULL) { + if (session->target != NULL) + targetUri = session->target->loc_uri; + + if (session->jsession_id != NULL) + jsessionId = session->jsession_id; + } else { + _DEBUG_ERROR("session is NULL !"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + + if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML) + isXML = true; + else + isXML = false; + + na_http_header_binder(acc_type, targetUri, isXML, jsessionId, &header_info); + res = sync_agent_send_msg( /*acc_info (id, pw, uri, ...), */ + header_info, transport_type, send_msg, send_msg_length, &recv_header, (unsigned char **)recv_msg, recv_msg_length, SYNC_AGENT_NA_SEND_TYPE_SEND_N_RECEIVE, session->naci_session_id); + + if (res != SYNC_AGENT_NA_SUCCESS) { + _DEBUG_ERROR("res = %d", res); + if (res == SYNC_AGENT_NA_SEND_MSG_CANCEL) + errorType = SA_INTERNAL_CANCEL; + else + errorType = SA_INTERNAL_CONNECTION_ERROR; + goto error; + } + + __process_jssion_id(session, recv_header); + + error: + + if (recv_header != NULL) { + GList *iter = NULL; + sync_agent_na_common_header_info_s *iter_data; + for (iter = recv_header; iter != NULL;) { + iter_data = NULL; + iter_data = ((sync_agent_na_common_header_info_s *) (iter->data)); + + iter = g_list_next(iter); + recv_header = g_list_remove(recv_header, iter_data); + + if (iter_data->key != NULL) + free(iter_data->key); + if (iter_data->value != NULL) + free(iter_data->value); + free(iter_data); + } + g_list_free(recv_header); + } + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _process_recv_msg(session_s * session, char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + reset_cmd_id_session(session); + + syncml_s *syncml = (syncml_s *) calloc(1, sizeof(syncml_s)); + if (syncml == NULL) { + _DEBUG_ERROR("failed to allocate syncml"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + GList *pReturnStatus = NULL; + GList *pReturnDatastore = NULL; + + errorType = __reverse_object_binder(&syncml, recv_msg, recv_msg_length); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in reverseObjectBinder"); + goto error; + } + + /* check receive msg */ + errorType = receive_header(session, syncml->hdr); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in receive_Header"); + goto error; + } + + errorType = receive_statuses(session, syncml->status, &pReturnStatus); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in receive_Statuses"); + goto error; + } + + errorType = receive_commands(session, syncml->commands, false, &pReturnDatastore); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in receive_Commands"); + goto error; + } + + /* do not need to update pkgstatus when suspend */ + if (session->pkg_status == SYNCML_SUSPEND) { + _DEBUG_TRACE("Suspend"); + __clean_up_sa(session); + static_session = NULL; + goto error; + } + + if (syncml->final) { + session->is_sending_final = 0; + session->is_receiving_final = 1; + } else + session->is_receiving_final = 0; + + if (session->pkg_status == SYNCML_PKG_2) { + pre_sync_return_obj_s **pre = (pre_sync_return_obj_s **) sync_return_obj; + + if ((*pre) != NULL) + (*pre)->datastore_info = pReturnDatastore; + + if (session->remote_devinf != NULL) { + if (session->remote_devinf->dev_id != NULL) + (*pre)->dev_id = strdup(session->remote_devinf->dev_id); + } + } else { + sync_return_obj_s **syncReturn = (sync_return_obj_s **) sync_return_obj; + + if ((*syncReturn) != NULL) { + (*syncReturn)->status = pReturnStatus; + (*syncReturn)->changed_datastore = pReturnDatastore; + } + } + + if (session->pkg_status == SYNCML_PKG_2 && session->is_receiving_final) + session->pkg_status = SYNCML_PKG_3; + else if (session->pkg_status == SYNCML_PKG_4 && session->is_receiving_final) { + if (only_from_client) { + *is_finish = 1; + __clean_up_sa(session); + static_session = NULL; + } else + session->pkg_status = SYNCML_PKG_5; + } else if (session->pkg_status == SYNCML_PKG_6 && session->is_receiving_final) { + *is_finish = 1; + __clean_up_sa(session); + static_session = NULL; + } + + error: + + if (syncml != NULL) + free_syncml(syncml); + + _INNER_FUNC_EXIT; + return errorType; + +} + +static sa_error_type_e _generate_autoconfigure_msg(char *id, char *pwd, char *target_url, char **msg, unsigned int *msg_size, session_s * session) +{ + _INNER_FUNC_ENTER; + _DEBUG_TRACE("id = %s, pwd = %s, target_url = %s", id, pwd, target_url); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + int isFinal = 1; + char *credData = NULL; + cred_s *pCred = NULL; + sync_hdr_s *pSyncHdr = NULL; + syncml_s *syncml = NULL; + + GList *commands = NULL; + GList *commands_last = NULL; + + errorType = create_cred_string(AUTH_TYPE_BASIC, id, pwd, NULL, 0, &credData); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create cred string"); + goto error; + } + + errorType = create_cred(id, pwd, AUTH_TYPE_BASIC, FORMAT_TYPE_BASE64, credData, &pCred); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create cred"); + goto error; + } + + if (credData != NULL) { + free(credData); + credData = NULL; + } + + set_session_cred(session, pCred); + + errorType = create_sync_hdr(session, &pSyncHdr); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create synchdr"); + goto error; + } + + char *sourceDevInf = NULL; + if (session->protocol_version == VERSION_10) + sourceDevInf = ELEMENT_DEVINF_10; + else if (session->protocol_version == VERSION_11) + sourceDevInf = ELEMENT_DEVINF_11; + else if (session->protocol_version == VERSION_12) + sourceDevInf = ELEMENT_DEVINF_12; + + location_s *pLocation = NULL; + errorType = create_location(sourceDevInf, NULL, &pLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create location"); + goto error; + } + + char *contentType = NULL; + if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML) + contentType = ELEMENT_DEVINF_XML; + else + contentType = ELEMENT_DEVINF_WBXML; + + /* create Get command */ + command_s *pGetCommand = NULL; + errorType = create_get_command(session, pLocation, contentType, &pGetCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to get command"); + goto error; + } + put_into_list(&commands, &commands_last, pGetCommand); + + errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &syncml); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to get syncml"); + goto error; + } + + errorType = __object_binder(syncml, false, msg, msg_size); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in objectBinder"); + goto error; + } + + /*free generate structure */ + free_syncml(syncml); + syncml = NULL; + + _INNER_FUNC_EXIT; + return errorType; + + error: + if (pSyncHdr != NULL) + free_sync_hdr(pSyncHdr); + if (pCred != NULL) + free_cred(pCred); + if (credData != NULL) + free(credData); + if (commands != NULL) + free_commands(commands); + if (syncml != NULL) + free(syncml); + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _process_autoconfigure_recv_msg(char *recv_msg, unsigned int recv_msg_length, session_s * session) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + syncml_s *syncml = (syncml_s *) calloc(1, sizeof(syncml_s)); + if (syncml == NULL) { + _DEBUG_ERROR("failed to allocate syncml"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + errorType = __reverse_object_binder(&syncml, recv_msg, recv_msg_length); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in reverseObjectBinder"); + goto error; + } + + /* check receive msg */ + errorType = receive_header(session, syncml->hdr); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in receive_Header"); + goto error; + } + + errorType = receive_statuses(session, syncml->status, NULL); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in receive_Statuses"); + goto error; + } + + errorType = receive_commands(session, syncml->commands, true, NULL); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in receive_Commands"); + goto error; + } + + error: + + if (syncml != NULL) + free_syncml(syncml); + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e __object_binder(syncml_s * syncml, bool server_flag, char **msg, unsigned int *msg_size) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL; + + sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set); + + sync_agent_pb_protocol_binder_info_s *binder = NULL; + char *xml = NULL; + unsigned int xml_len = 0; + + if (server_flag != true) + binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, false, false, true, false, binder_function_set); + else + binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, true, false, true, true, binder_function_set); + + err = oma_ds_1_2_binder_append(binder, PE_SYNCML_START, NULL); + err = oma_ds_1_2_binder_append(binder, PE_HEADER, syncml->hdr); + err = oma_ds_1_2_binder_append(binder, PE_BODY_START, NULL); + + GList *status_iter = NULL; + status_s *pStatus = NULL; + for (status_iter = syncml->status; status_iter != NULL; status_iter = g_list_next(status_iter)) { + pStatus = status_iter->data; + err = oma_ds_1_2_binder_append(binder, PE_STATUS, pStatus); + } + + GList *iter = NULL; + command_s *pCommand = NULL; + oma_ds_protocol_element_e protocol_element = PE_UNDEF; + for (iter = syncml->commands; iter != NULL; iter = g_list_next(iter)) { + pCommand = (command_s *) (iter->data); + + command_type_e type = pCommand->type; + switch (type) { + case COMMAND_TYPE_UNKNOWN: + protocol_element = PE_UNDEF; + break; + case COMMAND_TYPE_ALERT: + protocol_element = PE_ALERT; + break; + case COMMAND_TYPE_SYNC_START: + protocol_element = PE_SYNC_START; + break; + case COMMAND_TYPE_SYNC_END: + protocol_element = PE_SYNC_END; + break; + case COMMAND_TYPE_PUT: + protocol_element = PE_PUT_START; + break; + case COMMAND_TYPE_ADD: + protocol_element = PE_ADD; + break; + case COMMAND_TYPE_REPLACE: + protocol_element = PE_REPLACE; + break; + case COMMAND_TYPE_DELETE: + protocol_element = PE_DELETE; + break; + case COMMAND_TYPE_MAP: + protocol_element = PE_MAP; + break; + case COMMAND_TYPE_GET: + protocol_element = PE_GET; + break; + case COMMAND_TYPE_RESULTS: + protocol_element = PE_RESULTS_START; + break; + case COMMAND_TYPE_HEADER: + protocol_element = PE_HEADER; + break; + } + err = oma_ds_1_2_binder_append(binder, protocol_element, pCommand); + + if (type == COMMAND_TYPE_RESULTS) { + err = oma_ds_1_2_binder_append(binder, PE_RESULTS_END, NULL); + } + + if (type == COMMAND_TYPE_PUT) { + if (pCommand->private.access.item != NULL) { + if (pCommand->private.access.item->private.devinf != NULL) { + err = oma_ds_1_2_binder_append(binder, PE_DEVINF, pCommand->private.access.item); + } + } + err = oma_ds_1_2_binder_append(binder, PE_PUT_END, NULL); + } + } + + if (syncml->final) { + err = oma_ds_1_2_binder_append(binder, PE_FINAL, NULL); + } + + err = oma_ds_1_2_binder_append(binder, PE_BODY_END, NULL); + err = oma_ds_1_2_binder_append(binder, PE_SYNCML_END, NULL); + + oma_ds_1_2_binder_get_stream(binder, msg, msg_size); + + /*FIXME for debugging */ + if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML) + set_xml_to_file(*msg, OMA_DS_MSG_PATH); + else { + err = sync_agent_get_xml_from_protocol_binder(binder, &xml, &xml_len); + if (err == SYNC_AGENT_PB_RETURN_OK) { + set_xml_to_file(xml, OMA_DS_MSG_PATH); + + if (xml != NULL) { + free(xml); + xml = NULL; + } + } + } + + oma_ds_1_2_binder_terminate(binder); + + if (err != SYNC_AGENT_PB_RETURN_OK) + errorType = SA_INTERNAL_BINDER_ERROR; + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e __reverse_object_binder(syncml_s ** syncml, char *recv_msg, unsigned int recv_msg_length) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL; + + sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set); + sync_agent_pb_protocol_binder_reverse_info_s *binder = NULL; + char *xml = NULL; + unsigned int xml_len = 0; + + sync_agent_pb_decoding_e dec; + if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML) + dec = SYNC_AGENT_PB_DECODING_XML; + else + dec = SYNC_AGENT_PB_DECODING_WBXML; + + err = reverse_oma_ds_1_2_binder_init(recv_msg, recv_msg_length, dec, binder_function_set, NULL, &binder); + + if (err != SYNC_AGENT_PB_RETURN_OK) { + errorType = SA_INTERNAL_BINDER_ERROR; + _DEBUG_ERROR("reverse_oma_ds_1_2_binder_init error =%d", err); + return errorType; + } + + /*FIXME for debugging */ + if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML) + set_xml_to_file(recv_msg, OMA_DS_MSG_PATH); + else { + err = sync_agent_get_xml_from_reverse_protocol_binder(binder, &xml, &xml_len); + if (err == SYNC_AGENT_PB_RETURN_OK) { + set_xml_to_file(xml, OMA_DS_MSG_PATH); + + if (xml != NULL) { + free(xml); + xml = NULL; + } + } + } + + oma_ds_protocol_element_e protocol_element = PE_UNDEF; + char *protocol_element_name = NULL; + Content_Ptr pContent = NULL; + command_s *tempCommandfordevInf = NULL; + while (err == SYNC_AGENT_PB_RETURN_OK) { + protocol_element = PE_UNDEF; + err = reverse_oma_ds_1_2_binder_next(binder, &protocol_element, &protocol_element_name, &pContent); + switch (protocol_element) { + case PE_ADD: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + break; + case PE_ATOMIC_START: + case PE_ATOMIC_END: + case PE_COPY: + case PE_DELETE: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + break; + case PE_EXEC: + case PE_GET: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + break; + case PE_MAP: + case PE_PUT_START: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + tempCommandfordevInf = pContent; + break; + case PE_SEARCH: + case PE_SEQUENCE_START: + case PE_SEQUENCE_END: + case PE_SYNC_START: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + break; + case PE_SYNC_END: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + protocol_element = PE_UNDEF; + break; + case PE_REPLACE: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + break; + case PE_ALERT: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + break; + case PE_RESULTS_START: + (*syncml)->commands = g_list_append((*syncml)->commands, pContent); + tempCommandfordevInf = pContent; + break; + case PE_STATUS: + (*syncml)->status = g_list_append((*syncml)->status, pContent); + protocol_element = PE_UNDEF; + break; + case PE_HEADER: + (*syncml)->hdr = pContent; + sync_agent_set_user_data_in_reverse_protocol_binder(binder, (*syncml)->hdr); + break; + case PE_PUT_GET: + case PE_CMD_GROUP: + case PE_GENERIC: + case PE_FINAL: + (*syncml)->final = (int)pContent; + break; + case PE_DEVINF: + if (tempCommandfordevInf != NULL) { + tempCommandfordevInf->private.results.item->private.devinf = pContent; + tempCommandfordevInf = NULL; + } + break; + default: + break; + } + + if (protocol_element_name != NULL) { + free(protocol_element_name); + protocol_element_name = NULL; + } + + if (err == SYNC_AGENT_PB_RETURN_HAS_NO_MORE_ELEMENT) { + break; + } + } + + sync_agent_destroy_reverse_protocol_binder(binder); + + free_oma_ds_1_2_binder_function_set(binder_function_set); + + if (err != SYNC_AGENT_PB_RETURN_OK && err != SYNC_AGENT_PB_RETURN_HAS_NO_MORE_ELEMENT) + errorType = SA_INTERNAL_BINDER_ERROR; + + _INNER_FUNC_EXIT; + return errorType; +} + +static common_error_type_e _convert_error_type(sa_error_type_e error_type) +{ + _INNER_FUNC_ENTER; + + common_error_type_e sa_errorType = COMMON_OK; + + if (error_type == SA_INTERNAL_OK) + sa_errorType = COMMON_OK; + else if (error_type == SA_INTERNAL_MISCONFIGURATION) + sa_errorType = COMMON_MISCONFIGURATION; + else if (error_type == SA_INTERNAL_AUTHENTICATION_ERROR) + sa_errorType = COMMON_AUTHENTICATION_ERROR; + else if (error_type == SA_INTERNAL_NOT_FOUND) + sa_errorType = COMMON_NOT_FOUND; + else if (error_type == SA_INTERNAL_NO_MEMORY) + sa_errorType = COMMON_NO_MEMORY; + else if (error_type == SA_INTERNAL_NOT_DEFINED || error_type == SA_INTERNAL_BINDER_ERROR || error_type == SA_INTERNAL_ERROR || error_type == SA_INTERNAL_DA_ERROR) + sa_errorType = COMMON_INTERNAL_ERROR; + else if (error_type == SA_INTERNAL_SERVER_ERROR || error_type == SA_INTERNAL_SERVER_FAILURE) + sa_errorType = COMMON_SERVER_ERROR; + else if (error_type == SA_INTERNAL_CONNECTION_ERROR) + sa_errorType = COMMON_CONNECTION_ERROR; + else if (error_type == SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER) + sa_errorType = COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER; + else if (error_type == SA_INTERNAL_CANCEL) + sa_errorType = COMMON_CANCEL; + else if (error_type == SA_INTERNAL_BUSY_SIGNALING) + sa_errorType = COMMON_BUSY_SIGNALING; + else if (error_type == SA_INTERNAL_SUSPEND_FAIL) + sa_errorType = COMMON_SUSPEND_FAIL; + + _INNER_FUNC_EXIT; + return sa_errorType; + + /*from processRecvMsg + 401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore) + 407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore) + 404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user) + + 1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error + + 1513 -> ERROR_INTERNAL_NOT_DEFINED + 1512 -> ERROR_INTERNAL_NO_MEMORY + 1503 -> ERROR_INTERNAL_BINDER_ERROR + + 500 -> ERROR_GENERIC (our problem -> do not ever never receive this error) + 511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error) + + can be return to engine value + from generatePreSyncMsg + 1506 -> ERROR_INTERNAL_MISCONFIGURATION : need configure infomation + 1500 -> ERROR_INTERNAL_OK : generatePreSyncMsg has been completed without any error + 1512 -> ERROR_INTERNAL_NO_MEMORY : failed to allocate memory + 1513 -> ERROR_INTERNAL_NOT_DEFINED : something is not defined that must have + 1503 -> ERROR_INTERNAL_BINDER_ERROR : binder error + + from exchangeMsg + + from processRecvMsg + 401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore) + 407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore) + 404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user) + + 1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error + + 1513 -> ERROR_INTERNAL_NOT_DEFINED + 1512 -> ERROR_INTERNAL_NO_MEMORY + 1503 -> ERROR_INTERNAL_BINDER_ERROR + + 500 -> ERROR_GENERIC (our problem -> do not ever never receive this error) + 511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error) */ + +} + +static void __clean_up_sa(session_s * session) +{ + _INNER_FUNC_ENTER; + + if (session != NULL) { + + if (session->has_opend) { + sync_agent_close_connection(TRANSPORT_TYPE, session->naci_session_id); + session->has_opend = 0; + } + + free_session(session); + session = NULL; + } + + _INNER_FUNC_EXIT; +} + +static sa_error_type_e __process_jssion_id(session_s * session, GList * recv_header) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS; + + _DEBUG_VERBOSE(" CKECK JSESSION\n"); + char *temp_jsessionid = NULL; + char *real_jsessionid = NULL; + res = sync_agent_get_header_info(1, recv_header, "Set-Cookie", &temp_jsessionid); + if (res == SYNC_AGENT_NA_SUCCESS) { + if (temp_jsessionid != NULL) { + real_jsessionid = strstr(temp_jsessionid, "JSESSIONID"); + if (real_jsessionid != NULL) { + session->jsession_id = strdup(real_jsessionid); + _DEBUG_VERBOSE("session->jsessionId = %s", session->jsession_id); + + } + } + } else + _DEBUG_ERROR("failed in sync_agent_get_header_info = %d", res); + + if (temp_jsessionid != NULL) + free(temp_jsessionid); + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e __check_resume_session(session_s * session, bool * resume) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + char *value = NULL; + bool result; + + result = get_config(session->account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, &value); + _DEBUG_VERBOSE("resume = %s", value); + if (result == false) { + _DEBUG_ERROR("failed in get_config"); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + if (strcmp(value, "1") == 0) + *resume = true; + else + *resume = false; + + error: + + if (value != NULL) + free(value); + + _INNER_FUNC_EXIT; + return errorType; + +} + +int pre_sync(int transportType, int account_id, char *session_id, int server_flag, void **pre_sync_return_obj) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + char *msg = NULL; + unsigned int msg_size = 0; + char *recv_msg = NULL; + unsigned int recv_msg_size = 0; + unsigned int naci_session_id; + + /* create session structure */ + errorType = _create_session(account_id, session_id, server_flag, &static_session); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __create_session"); + goto error; + } + + /* generate preSync Msg */ + errorType = _generate_presync_msg(static_session, server_flag, &msg, &msg_size); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __generate_presync_msg"); + goto error; + } + + if (!static_session->has_opend) { + sync_agent_na_result_e res = sync_agent_open_connection(transportType, NETWORK_TIMEOUT, &naci_session_id); + if (res != SYNC_AGENT_NA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_connection res = %d", res); + errorType = SA_INTERNAL_CONNECTION_ERROR; + goto error; + } + static_session->naci_session_id = naci_session_id; + static_session->has_opend = 1; + _DEBUG_INFO("naci_session_id = %d", naci_session_id); + } + /* exchange Msg */ + errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transportType, msg, msg_size, &recv_msg, &recv_msg_size); + if (errorType != SA_INTERNAL_OK) + goto error; + + errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, pre_sync_return_obj, NULL); + if (errorType == SA_INTERNAL_AUTHENTICATION_ERROR) { + /*when errortype is ERROR_AUTH_REQUIRED try request using chal just one more time + it can be only happend when server required MD5 authentication(we send basic authentication every time by default) */ + + /* create session structure */ + errorType = _create_session(account_id, session_id, server_flag, &static_session); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __create_session"); + goto error; + } + + if (msg != NULL) { + free(msg); + msg = NULL; + } + + if (recv_msg != NULL) { + free(recv_msg); + recv_msg = NULL; + } + + errorType = _generate_presync_msg(static_session, server_flag, &msg, &msg_size); + if (errorType != SA_INTERNAL_OK) + goto error; + + errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transportType, msg, msg_size, &recv_msg, &recv_msg_size); + if (errorType != SA_INTERNAL_OK) + goto error; + + errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, pre_sync_return_obj, NULL); + if (errorType != SA_INTERNAL_OK) + goto error; + } + + error: + + if (msg != NULL) { + free(msg); + msg = NULL; + } + + if (recv_msg != NULL) { + free(recv_msg); + recv_msg = NULL; + } + + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("errorType : %d", errorType); + __clean_up_sa(static_session); + static_session = NULL; + } + + _EXTERN_FUNC_EXIT; + return _convert_error_type(errorType); +} + +int generate_msg(void **syncObj, int server_flag, char **msg, unsigned int *msg_size) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + errorType = _generate_msg(static_session, syncObj, server_flag, msg, msg_size); + if (errorType != SA_INTERNAL_OK) + goto error; + + error: + + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("errorType : %d", errorType); + __clean_up_sa(static_session); + static_session = NULL; + } + + _EXTERN_FUNC_EXIT; + return _convert_error_type(errorType); +} + +int exchange_msg(int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transport_type, send_msg, send_msg_length, recv_msg, recv_msg_length); + if (errorType != SA_INTERNAL_OK) + goto error; + + error: + + if (errorType != SA_INTERNAL_OK && errorType != SA_INTERNAL_CANCEL) { + _DEBUG_ERROR("errorType : %d", errorType); + __clean_up_sa(static_session); + static_session = NULL; + } + + _EXTERN_FUNC_EXIT; + return _convert_error_type(errorType); +} + +int process_recv_msg(char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + errorType = _process_recv_msg(static_session, recv_msg, recv_msg_length, only_from_client, sync_return_obj, is_finish); + if (errorType != SA_INTERNAL_OK) + goto error; + + error: + + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("errorType : %d", errorType); + __clean_up_sa(static_session); + static_session = NULL; + } + + _EXTERN_FUNC_EXIT; + return _convert_error_type(errorType); +} + +void clean_up_sa() +{ + _EXTERN_FUNC_ENTER; + + __clean_up_sa(static_session); + static_session = NULL; + + _EXTERN_FUNC_EXIT; +} + +int auto_configure(char *addr, char *id, char *password, GList ** configure) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + session_s *session = NULL; + + char *sourceUrl = NULL; + char *sessionId = NULL; + + char *msg = NULL; + unsigned int msg_size = 0; + + char *recv_msg = NULL; + unsigned int recv_msg_size = 0; + + bool cancel_flag = false; + unsigned int session_id; + + location_s *pSourceLocation = NULL; + location_s *pTargetLocation = NULL; + + sessionId = g_strdup_printf("%ld", time(NULL)); + if (sessionId == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + errorType = create_location(sourceUrl, NULL, &pSourceLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_location"); + goto error; + } + + errorType = create_location(addr, NULL, &pTargetLocation); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_location"); + goto error; + } + + errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, 0, sessionId, pSourceLocation, pTargetLocation, &session); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_session"); + goto error; + } + + if (session == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + errorType = _generate_autoconfigure_msg(id, password, addr, &msg, &msg_size, session); + if (errorType != SA_INTERNAL_OK) + goto error; + + if (!session->has_opend) { + sync_agent_na_result_e res = sync_agent_open_connection(TRANSPORT_TYPE, NETWORK_TIMEOUT, &session_id); + if (res != SYNC_AGENT_NA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_connection res = %d", res); + errorType = SA_INTERNAL_CONNECTION_ERROR; + goto error; + } + session->naci_session_id = session_id; + session->has_opend = 1; + _DEBUG_INFO("session_id = %d", session_id); + } + + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + errorType = SA_INTERNAL_CANCEL; + goto error; + } + + /* exchange Msg */ + errorType = _exchange_msg(session, DEFINE_PROTOCOL_TYPE, TRANSPORT_TYPE, msg, msg_size, &recv_msg, &recv_msg_size); + if (errorType != SA_INTERNAL_OK) + goto error; + + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + errorType = SA_INTERNAL_CANCEL; + goto error; + } + + errorType = _process_autoconfigure_recv_msg(recv_msg, recv_msg_size, session); + if (errorType != SA_INTERNAL_OK) + goto error; + + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + errorType = SA_INTERNAL_CANCEL; + goto error; + } + + devinf_s *devInf = session->remote_devinf; + if (devInf != NULL) { + + GList *tempConfigure = NULL; + + GList *contactCandidate = NULL; + GList *calendarCandidate = NULL; + GList *memoCandidate = NULL; + + /*set inputted name by user */ + contactCandidate = g_list_append(contactCandidate, strdup(DEFINE_SOURCE_CONTACT_URI)); + /* + char *target = NULL; + target= get_vconf_str(profilePath, DS_SETTING_CONTACT_TGTURI); + if (target != NULL) { + _DEBUG_INFO("Contacts target =%s", target); + if (strcmp(target, "") !=0) + contactCandidate = g_list_append(contactCandidate, strdup(target)); + + free(target); + target = NULL; + } + */ + + calendarCandidate = g_list_append(calendarCandidate, strdup(DEFINE_SOURCE_CALENDAR_URI)); + /* + target = get_vconf_str(profilePath, DS_SETTING_CALENDAR_TGTURI); + if (target != NULL) { + _DEBUG_INFO("Organizer target =%s", target); + if (strcmp(target, "") !=0) + calendarCandidate = g_list_append(calendarCandidate, strdup(target)); + + free(target); + target = NULL; + } + */ + + memoCandidate = g_list_append(memoCandidate, strdup(DEFINE_SOURCE_MEMO_URI)); + /* + target = get_vconf_str(profilePath, DS_SETTING_MEMO_TGTURI); + if (target != NULL) { + _DEBUG_INFO("Memo target =%s", target); + if (strcmp(target, "") !=0) + memoCandidate = g_list_append(memoCandidate, strdup(target)); + + free(target); + target = NULL; + } + */ + + GList *iter = NULL; + GList *innerIter = NULL; + devinf_datastore_s *devInfDataStore = NULL; + int exist; + char *candidate; + for (iter = devInf->datastores; iter != NULL; iter = g_list_next(iter)) { + devInfDataStore = iter->data; + exist = 0; + candidate = NULL; + + _DEBUG_INFO("devInfDataStore->sourceref = %s", devInfDataStore->source_ref); + + if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCARD) == 0 || strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCARD_30) == 0) { + + for (innerIter = g_list_next(contactCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) { + candidate = innerIter->data; + + if (strcmp(candidate, devInfDataStore->source_ref) == 0) + exist = 1; + } + if (!exist) { + if (devInfDataStore->source_ref != NULL) + contactCandidate = g_list_append(contactCandidate, strdup(devInfDataStore->source_ref)); + } + + } else if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCAL) == 0 || strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_ICAL) == 0) { + + for (innerIter = g_list_next(calendarCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) { + candidate = innerIter->data; + + if (strcmp(candidate, devInfDataStore->source_ref) == 0) + exist = 1; + } + if (!exist) { + if (devInfDataStore->source_ref != NULL) + calendarCandidate = g_list_append(calendarCandidate, strdup(devInfDataStore->source_ref)); + } + + } else if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_PLAIN) == 0) { + + for (innerIter = g_list_next(memoCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) { + candidate = innerIter->data; + + if (strcmp(candidate, devInfDataStore->source_ref) == 0) + exist = 1; + } + if (!exist) { + if (devInfDataStore->source_ref != NULL) + memoCandidate = g_list_append(memoCandidate, strdup(devInfDataStore->source_ref)); + } + } + } + + tempConfigure = g_list_append(tempConfigure, contactCandidate); + tempConfigure = g_list_append(tempConfigure, calendarCandidate); + tempConfigure = g_list_append(tempConfigure, memoCandidate); + + *configure = tempConfigure; + +// *congifure = g_list_append(*congifure, contactCandidate); +// *congifure = g_list_append(*congifure, calendarCandidate); +// *congifure = g_list_append(*congifure, memoCandidate); + + } else { + errorType = SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER; + goto error; + } + + error: + + /*close network */ + if (session != NULL) { + if (session->has_opend) + sync_agent_close_connection(TRANSPORT_TYPE, session->naci_session_id); + } + + if (msg != NULL) { + free(msg); + msg = NULL; + } + + if (recv_msg != NULL) { + free(recv_msg); + recv_msg = NULL; + } + + if (sessionId != NULL) + free(sessionId); + + if (sourceUrl != NULL) + free(sourceUrl); + + if (session != NULL) { + free_session(session); + session = NULL; + } + + if (errorType != SA_INTERNAL_OK) + _DEBUG_ERROR("errorType =%d", errorType); + + _EXTERN_FUNC_EXIT; + return _convert_error_type(errorType); +} + +int check_cancel_status() +{ + _EXTERN_FUNC_ENTER; + + bool result = false; + retvm_if(static_session == NULL, false, "session is NULL"); + + if (static_session->pkg_status == SYNCML_PKG_5 || static_session->pkg_status == SYNCML_PKG_6) + result = false; + else + result = true; + + _EXTERN_FUNC_EXIT; + + return result; +} + +int suspend_sync(int transport_type, int account_id, int server_flag) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + char *msg = NULL; + unsigned int msg_size = 0; + char *recv_msg = NULL; + unsigned int recv_msg_size = 0; + + /* generate preSync Msg */ + errorType = _generate_suspend_msg(static_session, server_flag, &msg, &msg_size); + if (errorType != SA_INTERNAL_OK) + goto error; + + /* exchange Msg */ + errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transport_type, msg, msg_size, &recv_msg, &recv_msg_size); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("errorType = %d", errorType); + goto error; + } + + errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, NULL, NULL); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("errorType = %d", errorType); + goto error; + } + + error: + + if (msg != NULL) { + free(msg); + msg = NULL; + } + + if (recv_msg != NULL) { + free(recv_msg); + recv_msg = NULL; + } + + if (errorType != SA_INTERNAL_OK) + _DEBUG_ERROR("errorType =%d", errorType); + + _EXTERN_FUNC_EXIT; + return _convert_error_type(errorType); +} + +int cancel_connection_sync_request(int transport_type) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e err = SA_INTERNAL_OK; + sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS; + + if (static_session != NULL) { + _DEBUG_INFO("sessionId = %d", static_session->naci_session_id); + + res = sync_agent_cancel_msg(transport_type, static_session->naci_session_id); + if (res != SYNC_AGENT_NA_SUCCESS) { + _DEBUG_ERROR("res = %d", res); + err = SA_INTERNAL_CONNECTION_ERROR; + goto error; + } + } + + error: + + _EXTERN_FUNC_EXIT; + return _convert_error_type(err); +} diff --git a/src/agent/service-adapter/sa_devinf.c b/src/agent/service-adapter/sa_devinf.c new file mode 100755 index 0000000..d65bad5 --- /dev/null +++ b/src/agent/service-adapter/sa_devinf.c @@ -0,0 +1,1069 @@ +/* + * 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_DevInf.c + * @version 0.1 + * @brief This file is the source file of implementation of functions for device information structure which is used in Service Adapter + */ + +#include <sync_agent.h> + +#include "service-adapter/sa_devinf.h" +#include "service-adapter/sa_devinf_internal.h" +#include "service-adapter/sa_session_internal.h" +#include "common/common_define.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SA" +#endif + +static sa_error_type_e _create_devinf_new(char *devid, devinf_devtyp_e devtyp, devinf_s ** devinf); +static sa_error_type_e _create_devinf_contenttype(char *cttype, char *verct, devinf_content_type_s ** devinf_content_type); +static sa_error_type_e _create_devinf_ctcap(devinf_content_type_s * devinf_content_type, devinf_ct_cap_s ** devinf_ctcap); +static sa_error_type_e _create_devinf_property(char *prop_name, devinf_property_s ** devinf_property); +static sa_error_type_e _create_devinf_datastore(char *datastore, devinf_datastore_s ** devinf_datastore); + +static void _free_devinf_datastores(GList * devinf_datastores); +static void __free_devinf_datastore(devinf_datastore_s * devinf_datastore); +static void ___free_devinf_contenttype(devinf_content_type_s * devinf_content_type); +static void ___free_devinf_ctcaps(devinf_ct_cap_s * devinf_ctcap); +static void ___free_devinf_property(devinf_property_s * devinf_property); +static void ___free_devinf_propparam(devinf_prop_param_s * devinf_prop_param); + +static void _set_devinf_version(devinf_s * devinf, devinf_version_e version); +static void _set_devinf_manufacturer(devinf_s * devinf, char *manufacturer); +static void _set_devinf_model(devinf_s * devinf, char *model); +static void _set_devinf_oem(devinf_s * devinf, char *oem); +static void _set_devinf_software_version(devinf_s * devinf, char *software_version); +static void _set_devinf_hardware_version(devinf_s * devinf, char *hardware_version); +static void _set_devinf_firmware_version(devinf_s * devinf, char *firmware_version); +static void _set_devinf_datastore(devinf_s * devinf, devinf_datastore_s * devinf_datastore); +static void _set_devinf_datastore_rxpref(devinf_datastore_s * devinf_datastore, devinf_content_type_s * devinf_content_type); +static void _set_devinf_datastore_txpref(devinf_datastore_s * devinf_datastore, devinf_content_type_s * devinf_content_type); +static void _set_devinf_datastore_ctcap(devinf_datastore_s * devinf_datastore, devinf_ct_cap_s * devinf_ctcap); +static void _set_devinf_datastore_sharedmem(devinf_datastore_s * devinf_datastore, int shared_mem); +static void _set_devinf_datastore_maxmem(devinf_datastore_s * devinf_datastore, unsigned int max_mem); +static void _set_devinf_datastore_maxid(devinf_datastore_s * devinf_datastore, unsigned int max_id); +static void _set_devinf_ctcap_property(devinf_ct_cap_s * devinf_ctcap, devinf_property_s * devinf_property); +static void _set_devinf_property_valenums(devinf_property_s * devinf_property, char *val_enum); +static devinf_devtyp_e _convert_devtyp(char *dev_typ); + +/* unused function + * keep for in case + */ +/* +static ErrorType create_DevInfPropParam(char *paramName, DevInfPropParam **devinfPropParam); +static void set_DevInfDataStoreDisplayname(DevInfDataStore* devinf_datastore, char *displayname); +static void set_DevInfDataStoreRx(DevInfDataStore *devinf_datastore, DevInfContentType *devinf_content_type); +static void set_DevInfDataStoreTx(DevInfDataStore *devinf_datastore, DevInfContentType *devinf_content_type); +static void set_DevInfPropertyDataType(DevInfProperty *devinfProperty, char *dataType); +static void set_DevInfPropertyMaxOccur(DevInfProperty *devinfProperty, unsigned int maxOccur); +static void set_DevInfPropertyMaxSize(DevInfProperty *devinfProperty, unsigned int maxSize); +static void set_DevInfPropertyNoTruncate(DevInfProperty *devinfProperty, int noTruncate); +static void set_DevInfPropertyDisplayName(DevInfProperty *devinfProperty, char *displayName); +static void set_DevInfPropertyPropParam(DevInfProperty *devinfProperty, DevInfPropParam *devinfPropParam); +static void set_DevInfPropParamDataType(DevInfPropParam *devinfPropParam, char *dataType); +static void set_DevInfPropParamDisplayName(DevInfPropParam *devinfPropParam, char *displayName); +static void set_DevInfPropParamValEnums(DevInfPropParam *devinfPropParam, char *valEnum); +*/ + +static sa_error_type_e _create_devinf_new(char *devid, devinf_devtyp_e devtyp, devinf_s ** devinf) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (devid == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (!devtyp) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *devinf = (devinf_s *) calloc(1, sizeof(devinf_s)); + if (*devinf == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + if (devid != NULL) + (*devinf)->dev_id = strdup(devid); + (*devinf)->devtyp = devtyp; + + error: + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _create_devinf_contenttype(char *cttype, char *verct, devinf_content_type_s ** devinf_content_type) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (cttype == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (verct == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *devinf_content_type = (devinf_content_type_s *) calloc(1, sizeof(devinf_content_type_s)); + if (*devinf_content_type == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*devinf_content_type)->ct_type = strdup(cttype); + (*devinf_content_type)->verct = strdup(verct); + + error: + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _create_devinf_ctcap(devinf_content_type_s * devinf_content_type, devinf_ct_cap_s ** devinf_ctcap) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (devinf_content_type == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *devinf_ctcap = (devinf_ct_cap_s *) calloc(1, sizeof(devinf_ct_cap_s)); + if (*devinf_ctcap == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*devinf_ctcap)->ct = devinf_content_type; + + error: + + _INNER_FUNC_EXIT; + return errorType; + +} + +static sa_error_type_e _create_devinf_property(char *prop_name, devinf_property_s ** devinf_property) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (prop_name == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *devinf_property = (devinf_property_s *) calloc(1, sizeof(devinf_property_s)); + if (*devinf_property == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*devinf_property)->prop_name = strdup(prop_name); + + error: + + _INNER_FUNC_EXIT; + return errorType; +} + +static sa_error_type_e _create_devinf_datastore(char *datastore, devinf_datastore_s ** devinf_datastore) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (datastore == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + *devinf_datastore = (devinf_datastore_s *) calloc(1, sizeof(devinf_datastore_s)); + if (*devinf_datastore == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*devinf_datastore)->source_ref = strdup(datastore); + + error: + + _INNER_FUNC_EXIT; + return errorType; +} + +static void _free_devinf_datastores(GList * devinf_datastores) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastores == NULL, "List is NULL"); + + GList *iter = NULL; + for (iter = devinf_datastores; iter != NULL; iter = g_list_next(iter)) + __free_devinf_datastore(iter->data); + + g_list_free(devinf_datastores); + + _INNER_FUNC_EXIT; +} + +static void __free_devinf_datastore(devinf_datastore_s * devinf_datastore) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastore == NULL, "devinf_datastore is NULL"); + + if (devinf_datastore->source_ref != NULL) { + free(devinf_datastore->source_ref); + devinf_datastore->source_ref = NULL; + } + + if (devinf_datastore->display_name != NULL) { + free(devinf_datastore->display_name); + devinf_datastore->display_name = NULL; + } + + ___free_devinf_contenttype(devinf_datastore->rx_pref); + devinf_datastore->rx_pref = NULL; + + ___free_devinf_contenttype(devinf_datastore->tx_pref); + devinf_datastore->tx_pref = NULL; + + /*free_FilterRx(filter_rx); */ + /*free_FilterCap(filterCap); */ + + GList *iter = NULL; + for (iter = devinf_datastore->rx; iter != NULL; iter = g_list_next(iter)) + ___free_devinf_contenttype(iter->data); + g_list_free(devinf_datastore->rx); + + for (iter = devinf_datastore->tx; iter != NULL; iter = g_list_next(iter)) + ___free_devinf_contenttype(iter->data); + g_list_free(devinf_datastore->tx); + + for (iter = devinf_datastore->ct_caps; iter != NULL; iter = g_list_next(iter)) + ___free_devinf_ctcaps(iter->data); + g_list_free(devinf_datastore->ct_caps); + + free(devinf_datastore); + + _INNER_FUNC_EXIT; +} + +static void ___free_devinf_contenttype(devinf_content_type_s * devinf_content_type) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_content_type == NULL, "devinf_content_type is NULL"); + + if (devinf_content_type->ct_type != NULL) { + free(devinf_content_type->ct_type); + devinf_content_type->ct_type = NULL; + } + + if (devinf_content_type->verct != NULL) { + free(devinf_content_type->verct); + devinf_content_type->verct = NULL; + } + + free(devinf_content_type); + + _INNER_FUNC_EXIT; +} + +static void ___free_devinf_ctcaps(devinf_ct_cap_s * devinf_ctcap) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_ctcap == NULL, "devinf_ctcap is NULL"); + + ___free_devinf_contenttype(devinf_ctcap->ct); + devinf_ctcap->ct = NULL; + + GList *iter = NULL; + for (iter = devinf_ctcap->properties; iter != NULL; iter = g_list_next(iter)) + ___free_devinf_property(iter->data); + + g_list_free(devinf_ctcap->properties); + + free(devinf_ctcap); + + _INNER_FUNC_EXIT; +} + +static void ___free_devinf_property(devinf_property_s * devinf_property) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_property == NULL, "devinf_property is NULL"); + + if (devinf_property->prop_name != NULL) { + free(devinf_property->prop_name); + devinf_property->prop_name = NULL; + } + + if (devinf_property->data_type != NULL) { + free(devinf_property->data_type); + devinf_property->data_type = NULL; + } + + if (devinf_property->display_name != NULL) { + free(devinf_property->display_name); + devinf_property->display_name = NULL; + } + + GList *iter = NULL; + for (iter = devinf_property->val_enums; iter != NULL; iter = g_list_next(iter)) + free(iter->data); + + g_list_free(devinf_property->val_enums); + + for (iter = devinf_property->prop_params; iter != NULL; iter = g_list_next(iter)) + ___free_devinf_propparam(iter->data); + + g_list_free(devinf_property->prop_params); + + free(devinf_property); + + _INNER_FUNC_EXIT; +} + +static void ___free_devinf_propparam(devinf_prop_param_s * devinf_prop_param) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_prop_param == NULL, "devinfPropParam is NULL"); + + if (devinf_prop_param->param_name != NULL) { + free(devinf_prop_param->param_name); + devinf_prop_param->param_name = NULL; + } + + if (devinf_prop_param->data_type != NULL) { + free(devinf_prop_param->data_type); + devinf_prop_param->data_type = NULL; + } + + if (devinf_prop_param->display_name != NULL) { + free(devinf_prop_param->display_name); + devinf_prop_param->display_name = NULL; + } + + GList *iter = NULL; + for (iter = devinf_prop_param->val_enums; iter != NULL; iter = g_list_next(iter)) + free(iter->data); + + g_list_free(devinf_prop_param->val_enums); + + free(devinf_prop_param); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_version(devinf_s * devinf, devinf_version_e version) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + devinf->version = version; + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_manufacturer(devinf_s * devinf, char *manufacturer) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + if (manufacturer != NULL) + devinf->manufacturer = strdup(manufacturer); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_model(devinf_s * devinf, char *model) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + if (model != NULL) + devinf->model = strdup(model); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_oem(devinf_s * devinf, char *oem) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + if (oem != NULL) + devinf->oem = strdup(oem); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_software_version(devinf_s * devinf, char *software_version) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + if (software_version != NULL) + devinf->software_version = strdup(software_version); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_hardware_version(devinf_s * devinf, char *hardware_version) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + if (hardware_version != NULL) + devinf->hardware_version = strdup(hardware_version); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_firmware_version(devinf_s * devinf, char *firmware_version) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + if (firmware_version != NULL) + devinf->firmware_version = strdup(firmware_version); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_datastore(devinf_s * devinf, devinf_datastore_s * devinf_datastore) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + devinf->datastores = g_list_append(devinf->datastores, devinf_datastore); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_datastore_rxpref(devinf_datastore_s * devinf_datastore, devinf_content_type_s * devinf_content_type) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastore == NULL, "devinf_datastore is NULL"); + + devinf_datastore->rx_pref = devinf_content_type; + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_datastore_txpref(devinf_datastore_s * devinf_datastore, devinf_content_type_s * devinf_content_type) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastore == NULL, "devinf_datastore is NULL"); + + devinf_datastore->tx_pref = devinf_content_type; + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_datastore_ctcap(devinf_datastore_s * devinf_datastore, devinf_ct_cap_s * devinf_ctcap) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastore == NULL, "devinf_datastore is NULL"); + + devinf_datastore->ct_caps = g_list_append(devinf_datastore->ct_caps, devinf_ctcap); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_datastore_sharedmem(devinf_datastore_s * devinf_datastore, int shared_mem) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastore == NULL, "devinf_datastore is NULL"); + + devinf_datastore->shared_mem = shared_mem; + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_datastore_maxmem(devinf_datastore_s * devinf_datastore, unsigned int max_mem) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastore == NULL, "devinf_datastore is NULL"); + + devinf_datastore->max_mem = max_mem; + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_datastore_maxid(devinf_datastore_s * devinf_datastore, unsigned int max_id) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_datastore == NULL, "devinf_datastore is NULL"); + + devinf_datastore->max_id = max_id; + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_ctcap_property(devinf_ct_cap_s * devinf_ctcap, devinf_property_s * devinf_property) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_ctcap == NULL, "devinfCTCap is NULL"); + + devinf_ctcap->properties = g_list_append(devinf_ctcap->properties, devinf_property); + + _INNER_FUNC_EXIT; +} + +static void _set_devinf_property_valenums(devinf_property_s * devinf_property, char *val_enum) +{ + _INNER_FUNC_ENTER; + + retm_if(devinf_property == NULL, "devinfCTCap is NULL"); + + if (val_enum != NULL) + devinf_property->val_enums = g_list_append(devinf_property->val_enums, strdup(val_enum)); + + _INNER_FUNC_EXIT; +} + +static devinf_devtyp_e _convert_devtyp(char *dev_typ) +{ + _INNER_FUNC_ENTER; + + devinf_devtyp_e devinfDevTyp = DEVINF_DEVTYPE_UNKNOWN; + + if (strcmp(dev_typ, DEFINE_DEVICE_TYPE_PAGER) == 0) + devinfDevTyp = DEVINF_DEVTYPE_PAGER; + else if (strcmp(dev_typ, DEFINE_DEVICE_TYPE_HANDHELD) == 0) + devinfDevTyp = DEVINF_DEVTYPE_HANDHELD; + else if (strcmp(dev_typ, DEFINE_DEVICE_TYPE_PDA) == 0) + devinfDevTyp = DEVINF_DEVTYPE_PDA; + else if (strcmp(dev_typ, DEFINE_DEVICE_TYPE_PHONE) == 0) + devinfDevTyp = DEVINF_DEVTYPE_PHONE; + else if (strcmp(dev_typ, DEFINE_DEVICE_TYPE_SMARTPHONE) == 0) + devinfDevTyp = DEVINF_DEVTYPE_SMARTPHONE; + else if (strcmp(dev_typ, DEFINE_DEVICE_TYPE_SERVER) == 0) + devinfDevTyp = DEVINF_DEVTYPE_SERVER; + else if (strcmp(dev_typ, DEFINE_DEVICE_TYPE_WORKSTATION) == 0) + devinfDevTyp = DEVINF_DEVTYPE_WORKSTATION; + + _INNER_FUNC_EXIT; + return devinfDevTyp; +} + +sa_error_type_e create_devinf(session_s * session, devinf_s ** devinf) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_dev_return_e err = SYNC_AGENT_DEV_RETURN_SUCCESS; + + char *devID = NULL; + char *devTyp = NULL; + char *man = NULL; + char *model = NULL; + char *oem = NULL; + char *swv = NULL; + char *hwv = NULL; + char *fwv = "1.0"; + + devinf_s *temp_devinf = NULL; + devinf_datastore_s *devInfDataStore = NULL; + devinf_ct_cap_s *devInfCTCap = NULL; + devinf_property_s *devInfProperty = NULL; + + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &devID); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevTyp", &devTyp); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + devinf_devtyp_e devInfDevTyp = DEVINF_DEVTYPE_UNKNOWN; + if (devTyp != NULL) + devInfDevTyp = _convert_devtyp(devTyp); + + devinf_version_e devInfVersion = DEVINF_VERSION_UNKNOWN; + if (session->protocol_version == VERSION_10) + devInfVersion = DEVINF_VERSION_10; + else if (session->protocol_version == VERSION_11) + devInfVersion = DEVINF_VERSION_11; + else if (session->protocol_version == VERSION_12) + devInfVersion = DEVINF_VERSION_12; + + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "Man", &man); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "Mod", &model); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "OEM", &oem); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "SwV", &swv); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "HwV", &hwv); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = SA_INTERNAL_ERROR; + goto error; + } + + /* + err = sync_agent_get_devinfo(DEFINE_PLATFORM, "FwV", &fwv); + if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + errorType = ERROR_INTERNAL_ERROR; + goto error; + } + */ + + _DEBUG_INFO("devID = %s", devID); + _DEBUG_INFO("devTyp = %s", devTyp); + _DEBUG_INFO("man = %s", man); + _DEBUG_INFO("model = %s", model); + _DEBUG_INFO("oem = %s", oem); + _DEBUG_INFO("swv = %s", swv); + _DEBUG_INFO("hwv = %s", hwv); + _DEBUG_INFO("fwv = %s", fwv); + + errorType = _create_devinf_new(devID, devInfDevTyp, &temp_devinf); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInf New"); + goto error; + } + + _set_devinf_version(temp_devinf, devInfVersion); + _set_devinf_manufacturer(temp_devinf, man); + _set_devinf_model(temp_devinf, model); + _set_devinf_oem(temp_devinf, oem); + _set_devinf_software_version(temp_devinf, swv); + _set_devinf_hardware_version(temp_devinf, hwv); + _set_devinf_firmware_version(temp_devinf, fwv); + + temp_devinf->supports_large_objs = 1; + temp_devinf->supports_utc = 1; + temp_devinf->supports_number_of_changes = 1; + + /*TODO Implements call log datastore's datastore for device info */ + /* TYPE_CONTENT_COUNT -1 -> not include calllog datastore */ + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT - 1; content_type++) { + + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->source != NULL) { + errorType = _create_devinf_datastore(datastoreinfo_per_content_type[content_type]->source, &devInfDataStore); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInfDataStore"); + goto error; + } + + sync_agent_plugin_object_info_s *datastore = sync_agent_get_obj_info(content_type); + + _DEBUG_INFO("datastore->type = %s", datastore->type); + _DEBUG_INFO("datastore->version = %s", datastore->version); + _DEBUG_INFO("datastore->field_cnt = %d", datastore->field_cnt); + + devinf_content_type_s *devInfContentTypeRxPref = NULL; + errorType = _create_devinf_contenttype(datastore->type, datastore->version, &devInfContentTypeRxPref); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInfContentType"); + goto error; + } + _set_devinf_datastore_rxpref(devInfDataStore, devInfContentTypeRxPref); + + devinf_content_type_s *devInfContentTypeTxPref = NULL; + errorType = _create_devinf_contenttype(datastore->type, datastore->version, &devInfContentTypeTxPref); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInfContentType"); + goto error; + } + _set_devinf_datastore_txpref(devInfDataStore, devInfContentTypeTxPref); + + devinf_content_type_s *devInfContentTypeCTCap = NULL; + errorType = _create_devinf_contenttype(datastore->type, datastore->version, &devInfContentTypeCTCap); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInfContentType"); + goto error; + } + + errorType = _create_devinf_ctcap(devInfContentTypeCTCap, &devInfCTCap); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInfCTCap"); + goto error; + } + + sync_agent_plugin_field_info_s field_list; + int i; + for (i = 0; i < datastore->field_cnt; i++) { + field_list = datastore->field_list[i]; + errorType = _create_devinf_property(field_list.field_name, &devInfProperty); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed to create devInfProperty"); + goto error; + } + + sync_agent_plugin_field_info_s children_field_list; + int j; + for (j = 0; j < field_list.field_child_cnt; j++) { + children_field_list = field_list.field_child_list[j]; + _set_devinf_property_valenums(devInfProperty, children_field_list.field_name); + } + _set_devinf_ctcap_property(devInfCTCap, devInfProperty); + devInfProperty = NULL; + } + + _set_devinf_datastore_ctcap(devInfDataStore, devInfCTCap); + devInfCTCap = NULL; + + /* TODO Get information about static memory for each datastore + * It will be used to fill DSMem and children element(SharedMem, MaxMem, MaxID) */ + /* Set SharedMem */ + _set_devinf_datastore_sharedmem(devInfDataStore, 0); + /* Set MaxMem */ + _set_devinf_datastore_maxmem(devInfDataStore, 0); + /* Set MaxID */ + _set_devinf_datastore_maxid(devInfDataStore, 0); + + set_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_TWO_WAY, 1); + set_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_SLOW_SYNC, 1); + set_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_ONE_WAY_FROM_CLIENT, 1); + set_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_REFRESH_FROM_CLIENT, 1); + set_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_ONE_WAY_FROM_SERVER, 1); + set_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_REFRESH_FROM_SERVER, 1); + set_devinf_datastore_sync_cap(devInfDataStore, DEVINF_SYNCTYPE_SERVER_ALERTED_SYNC, 1); + + _set_devinf_datastore(temp_devinf, devInfDataStore); + devInfDataStore = NULL; + + /*free datastore */ + sync_agent_free_obj_info(content_type, datastore); + } + } + } + + *devinf = temp_devinf; + temp_devinf = NULL; + + error: + + if (temp_devinf != NULL) + free(temp_devinf); + + __free_devinf_datastore(devInfDataStore); + ___free_devinf_ctcaps(devInfCTCap); + ___free_devinf_property(devInfProperty); + + if (devID != NULL) + free(devID); + if (devTyp != NULL) + free(devTyp); + if (man != NULL) + free(man); + if (model != NULL) + free(model); + if (oem != NULL) + free(oem); + if (swv != NULL) + free(swv); + if (hwv != NULL) + free(hwv); +/* if (fwv != NULL) + free(fwv);*/ + + _EXTERN_FUNC_EXIT; + return errorType; +} + +void free_devinf(devinf_s * devinf) +{ + _EXTERN_FUNC_ENTER; + + retm_if(devinf == NULL, "devinf is NULL"); + + if (devinf->manufacturer != NULL) { + free(devinf->manufacturer); + devinf->manufacturer = NULL; + } + + if (devinf->model != NULL) { + free(devinf->model); + devinf->model = NULL; + } + + if (devinf->oem != NULL) { + free(devinf->oem); + devinf->oem = NULL; + } + + if (devinf->software_version != NULL) { + free(devinf->software_version); + devinf->software_version = NULL; + } + + if (devinf->hardware_version != NULL) { + free(devinf->hardware_version); + devinf->hardware_version = NULL; + } + + if (devinf->firmware_version != NULL) { + free(devinf->firmware_version); + devinf->firmware_version = NULL; + } + + if (devinf->dev_id != NULL) { + free(devinf->dev_id); + devinf->dev_id = NULL; + } + + _free_devinf_datastores(devinf->datastores); + devinf->datastores = NULL; + + free(devinf); + + _EXTERN_FUNC_EXIT; +} + +void set_devinf_datastore_sync_cap(devinf_datastore_s * devinf_datastore, devinf_sync_cap_e cap, int supported) +{ + _EXTERN_FUNC_ENTER; + + if (supported) + devinf_datastore->sync_cap = devinf_datastore->sync_cap | cap; + else + devinf_datastore->sync_cap = devinf_datastore->sync_cap & ~cap; + + _EXTERN_FUNC_EXIT; +} + +int get_devinf_datastore_sync_cap(const devinf_datastore_s * devinf_datastore, devinf_sync_cap_e cap) +{ + _EXTERN_FUNC_ENTER; + + int data; + data = devinf_datastore->sync_cap & cap ? 1 : 0; + + _EXTERN_FUNC_EXIT; + return data; +} + +devinf_sync_cap_e convert_devinf_synccap(unsigned int id) +{ + _EXTERN_FUNC_ENTER; + + devinf_sync_cap_e result = DEVINF_SYNCTYPE_UNKNOWN; + + switch (id) { + case DEVINF_SYNCTYPE_TWO_WAY: + result = DEVINF_SYNCTYPE_TWO_WAY; + break; + case DEVINF_SYNCTYPE_SLOW_SYNC: + result = DEVINF_SYNCTYPE_SLOW_SYNC; + break; + case DEVINF_SYNCTYPE_ONE_WAY_FROM_CLIENT: + result = DEVINF_SYNCTYPE_ONE_WAY_FROM_CLIENT; + break; + case DEVINF_SYNCTYPE_REFRESH_FROM_CLIENT: + result = DEVINF_SYNCTYPE_REFRESH_FROM_CLIENT; + break; + case DEVINF_SYNCTYPE_ONE_WAY_FROM_SERVER: + result = DEVINF_SYNCTYPE_ONE_WAY_FROM_SERVER; + break; + case DEVINF_SYNCTYPE_REFRESH_FROM_SERVER: + result = DEVINF_SYNCTYPE_REFRESH_FROM_SERVER; + break; + case DEVINF_SYNCTYPE_SERVER_ALERTED_SYNC: + result = DEVINF_SYNCTYPE_SERVER_ALERTED_SYNC; + break; + default: + return DEVINF_SYNCTYPE_UNKNOWN; + break; + } + + _EXTERN_FUNC_EXIT; + return result; +} + +/* +static ErrorType create_DevInfPropParam(char *paramName, DevInfPropParam **devinfPropParam) +{ + _EXTERN_FUNC_ENTER; + + ErrorType errorType = ERROR_INTERNAL_OK; + + if (!paramName) { + errorType = ERROR_INTERNAL_NOT_DEFINED; + goto error; + } + + *devinfPropParam = (DevInfPropParam *)calloc(1, sizeof(DevInfPropParam)); + if (*devinfPropParam == NULL) { + errorType = ERROR_INTERNAL_NO_MEMORY; + goto error; + } + + (*devinfPropParam)->paramName = strdup(paramName); + +error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +static void set_DevInfDataStoreDisplayname(DevInfDataStore* devinf_datastore, char *displayname) { + + if (!devinf_datastore) + return; + + devinf_datastore->displayname = strdup(displayname); +} + +static void set_DevInfDataStoreRx(DevInfDataStore *devinf_datastore, DevInfContentType *devinf_content_type) { + if (!devinf_datastore) + return; + + devinf_datastore->rx = g_list_append(devinf_datastore->rx, devinf_content_type); +} + +static void set_DevInfDataStoreTx(DevInfDataStore *devinf_datastore, DevInfContentType *devinf_content_type) { + if (!devinf_datastore) + return; + + devinf_datastore->tx = g_list_append(devinf_datastore->tx, devinf_content_type); +} + +static void set_DevInfPropertyDataType(DevInfProperty *devinfProperty, char *dataType) { + + if (!devinfProperty) + return; + + devinfProperty->dataType = strdup(dataType); +} + +static void set_DevInfPropertyMaxOccur(DevInfProperty *devinfProperty, unsigned int maxOccur) { + + if (!devinfProperty) + return; + + devinfProperty->maxOccur = maxOccur; +} + +static void set_DevInfPropertyMaxSize(DevInfProperty *devinfProperty, unsigned int maxSize) { + + if (!devinfProperty) + return; + + devinfProperty->maxSize = maxSize; +} + +static void set_DevInfPropertyNoTruncate(DevInfProperty *devinfProperty, int noTruncate) { + + if (!devinfProperty) + return; + + devinfProperty->noTruncate = noTruncate; +} + +static void set_DevInfPropertyDisplayName(DevInfProperty *devinfProperty, char *displayName) { + + if (!devinfProperty) + return; + + devinfProperty->displayName = strdup(displayName); +} + +static void set_DevInfPropertyPropParam(DevInfProperty *devinfProperty, DevInfPropParam *devinfPropParam) { + + if (!devinfProperty) + return; + + devinfProperty->propParams = g_list_append(devinfProperty->propParams, devinfPropParam); +} + +static void set_DevInfPropParamDataType(DevInfPropParam *devinfPropParam, char *dataType) { + + if (!devinfPropParam) + return; + + devinfPropParam->dataType = strdup(dataType); +} + +static void set_DevInfPropParamDisplayName(DevInfPropParam *devinfPropParam, char *displayName) { + + if (!devinfPropParam) + return; + + devinfPropParam->displayName = strdup(displayName); +} + +static void set_DevInfPropParamValEnums(DevInfPropParam *devinfPropParam, char *valEnum) { + + if (!devinfPropParam) + return; + + devinfPropParam->valEnums = g_list_append(devinfPropParam->valEnums, strdup(valEnum)); + } +*/ diff --git a/src/agent/service-adapter/sa_elements.c b/src/agent/service-adapter/sa_elements.c new file mode 100755 index 0000000..7d64963 --- /dev/null +++ b/src/agent/service-adapter/sa_elements.c @@ -0,0 +1,834 @@ +/* + * 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 <sync_agent.h> + +#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; +} diff --git a/src/agent/service-adapter/sa_session.c b/src/agent/service-adapter/sa_session.c new file mode 100755 index 0000000..3ff27a1 --- /dev/null +++ b/src/agent/service-adapter/sa_session.c @@ -0,0 +1,1578 @@ +/* + * 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_Session.c + * @version 0.1 + * @brief This file is the source file of implementation of functions for Session structure which is used in Service Adapter and processing receive command & status + */ + +#include <sync_agent.h> + +#include "common/common_util.h" +#include "service-adapter/sa_session.h" +#include "service-adapter/sa_session_internal.h" +#include "service-adapter/sa_elements.h" +#include "service-adapter/sa_elements_internal.h" +#include "service-adapter/sa_command.h" +#include "service-adapter/sa_command_internal.h" +#include "service-adapter/sa_devinf.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SA" +#endif + +static sa_error_type_e _receive_alert_status(session_s * session, status_s * status); +static sa_error_type_e _receive_put_status(session_s * session, status_s * status); +static sa_error_type_e _receive_get_status(session_s * session, status_s * status); +static sa_error_type_e _receive_results_status(session_s * session, status_s * status); +static sa_error_type_e _receive_sync_status(session_s * session, status_s * status); +static sa_error_type_e _receive_changes_status(session_s * session, status_s * status, GList ** return_status); +static sa_error_type_e _receive_map_status(session_s * session, status_s * status); + +static sa_error_type_e _receive_alert_command(session_s * session, command_s * command, GList ** return_datastore); +static sa_error_type_e _receive_put_command(session_s * session, command_s * command); +static sa_error_type_e _receive_get_command(session_s * session, command_s * command); +static sa_error_type_e _receive_results_command(session_s * session, command_s * command); +static sa_error_type_e _receive_sync_command(session_s * session, command_s * command, changed_datastore_s ** changed_datastore); +static sa_error_type_e _receive_changes_command(session_s * session, command_s * command, char **luid_str_list, int *index, changed_datastore_s ** changed_datastore); + +static void __free_pending_status(command_status_s * command_status); +static sa_error_type_e __add_mapping(session_s * session, char *guid, char *luid, int datastore_id); + +static sa_error_type_e _receive_alert_status(session_s * session, status_s * status) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + unsigned int code = get_status_code(status); + _DEBUG_TRACE("code = %d", code); + + if (code >= 400 && code != ERROR_REQUIRE_REFRESH) { + /*401 unauthorized + 407 Authentication required + 405 command not allowed + 406 optional feature not supported + 500 command failed + 412 Incomplete command + 415 unsupported media type or format + 404 not found ->it have to be considered + TODO error handling it's error~!!!!! */ + + if (code == ERROR_AUTH_REJECTED || code == ERROR_AUTH_REQUIRED) + errorType = SA_INTERNAL_AUTHENTICATION_ERROR; + else if (code == ERROR_COMMAND_NOT_ALLOWED || code == ERROR_UNSUPPORTED_FEATURE || code == ERROR_GENERIC) + errorType = SA_INTERNAL_SERVER_ERROR; + else if (code == ERROR_NOT_FOUND) { + /*do not need to return errorType when code is ERROR_NOT_FOUND */ + + } + } + + /* delete mapping when receive 508 status for alert command */ + if (code == ERROR_REQUIRE_REFRESH) { + sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS; + + sync_agent_da_delete_mapping_query_s query; + query.option = SYNC_AGENT_DA_DELETE_MAPPING_OPTION_ACCOUNT_ID; + query.account_id = session->account_id; + + ret = sync_agent_delete_mapping(&query); + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_delete_mapping !!"); + } + } + + /* TODO off resume flag if status is not 200 */ + if (session->pkg_status == SYNCML_SUSPEND) { + if (code != 200) { + errorType = SA_INTERNAL_SUSPEND_FAIL; + } + } + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_put_status(session_s * session, status_s * status) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + unsigned int code = get_status_code(status); + _DEBUG_TRACE("code = %d", code); + + if (code >= 400) { + /*TODO error handling it's error~!!!!! + 401 unauthorized + 407 authentication required + 411 size required + 413 request entity too large + 416 requested size too big + 415 unspported media type or format + 420 device full + 500 command failed */ + if (code == ERROR_AUTH_REJECTED || code == ERROR_AUTH_REQUIRED) + errorType = SA_INTERNAL_AUTHENTICATION_ERROR; + else if (code == ERROR_GENERIC) + errorType = SA_INTERNAL_SERVER_ERROR; + else + errorType = SA_INTERNAL_ERROR; + } + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_get_status(session_s * session, status_s * status) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + unsigned int code = get_status_code(status); + _DEBUG_TRACE("code = %d", code); + + if (code >= 400) { + /*TODO error handling it's error~!!!!! + 401 unauthorized + 407 authentication required + 404 not found + 413 request entity too large + 415 unspported media type or format + 500 command failed */ + if (code == ERROR_AUTH_REJECTED || code == ERROR_AUTH_REQUIRED) + errorType = SA_INTERNAL_AUTHENTICATION_ERROR; + else if (code == ERROR_NOT_FOUND) + errorType = SA_INTERNAL_NOT_FOUND; + else if (code == ERROR_GENERIC) + errorType = SA_INTERNAL_SERVER_ERROR; + else + errorType = SA_INTERNAL_ERROR; + } + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_results_status(session_s * session, status_s * status) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + unsigned int code = get_status_code(status); + _DEBUG_TRACE("code = %d", code); + + if (code >= 400) { + /*TODO error handling it's error~!!!!! + 401 unauthorized + 407 authentication required + 404 not found + 413 request entity too large + 415 unspported media type or format + 500 command failed */ + if (code == ERROR_AUTH_REJECTED || code == ERROR_AUTH_REQUIRED) + errorType = SA_INTERNAL_AUTHENTICATION_ERROR; + else if (code == ERROR_NOT_FOUND) + errorType = SA_INTERNAL_NOT_FOUND; + else if (code == ERROR_GENERIC) + errorType = SA_INTERNAL_SERVER_ERROR; + else + errorType = SA_INTERNAL_ERROR; + } + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_sync_status(session_s * session, status_s * status) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + unsigned int code = get_status_code(status); + _DEBUG_TRACE("code = %d", code); + + if (code >= 400 && code != ERROR_REQUIRE_REFRESH) { + /*TODO error handling it's error~!!!!! + 401 unauthorized + 407 authentication required + 403 forbidden + 404 not found + 405 command not allowed + 508 refresh required + 500 command failed */ + if (code == ERROR_AUTH_REJECTED || code == ERROR_AUTH_REQUIRED) + errorType = SA_INTERNAL_AUTHENTICATION_ERROR; + else if (code == ERROR_NOT_FOUND) + errorType = SA_INTERNAL_NOT_FOUND; + else if (code == ERROR_GENERIC) + errorType = SA_INTERNAL_SERVER_ERROR; + else + errorType = SA_INTERNAL_ERROR; + } + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_changes_status(session_s * session, status_s * status, GList ** return_status) +{ + _INNER_FUNC_ENTER; + + _DEBUG_TRACE("cmdID = %d", status->cmd_id); + _DEBUG_TRACE("msgRef = %d", status->msg_ref); + _DEBUG_TRACE("cmdRef = %d", status->cmd_ref); + _DEBUG_TRACE("type = %d", status->type); + _DEBUG_TRACE("data = %s", status->data); + + unsigned int code = get_status_code(status); + _DEBUG_TRACE("code = %d", code); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (session->large_obj != NULL) { + /*LargeObj status it does not need to pass engine */ + command_status_s *largeObj = session->large_obj; + if (largeObj->cmd_id == status->cmd_ref && largeObj->msg_id == status->msg_ref) { + + if (code == CHUNK_ACCEPTED) { + __free_pending_status(largeObj); + session->large_obj = NULL; + return errorType; + } else if (code == ERROR_REQUESTED_SIZE_TOO_BIG) { + /*TODO error handling for large obj + can not send this item */ + } else if (code == ERROR_SIZE_REQUIRED) { + + } + } + } + + /*pass to engine */ + if (status->source_ref != NULL) { + applied_status_s *appliedStatus = create_applied_status(get_location_loc_uri(status->source_ref), + convert_change_type_command_type(status->type), code); + if (appliedStatus == NULL) { + _DEBUG_ERROR("failed in create_appliedstatus"); + return SA_INTERNAL_NO_MEMORY; + } + *return_status = g_list_append(*return_status, appliedStatus); + } + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_map_status(session_s * session, status_s * status) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_da_return_e da_err; + + GList *iter = NULL; + command_s *pCommand = NULL; + unsigned int code = 0; + for (iter = session->map_command; iter != NULL; iter = g_list_next(iter)) { + pCommand = iter->data; + + if (pCommand->msg_id == status->msg_ref && pCommand->cmd_id == status->cmd_ref) { + code = get_status_code(status); + _DEBUG_TRACE("code = %d", code); + + if (code == 200) { + GList *itemIter = NULL; + item_s *item = NULL; + for (itemIter = pCommand->private.map.items; itemIter != NULL; itemIter = g_list_next(itemIter)) { + item = itemIter->data; + _DEBUG_TRACE(" LUID = %s has been removed\n", get_location_loc_uri(item->source)); + + sync_agent_da_delete_mapping_query_s query; + query.option = SYNC_AGENT_DA_DELETE_MAPPING_OPTION_LUID; + query.account_id = session->account_id; + query.luid = get_location_loc_uri(item->source); + + da_err = sync_agent_delete_mapping(&query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + errorType = SA_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed in sync_agent_delete_mapping !!"); + return errorType; + } + } + session->map_command = g_list_remove(session->map_command, pCommand); + free_command(pCommand); + break; + } else if (code >= 400) { + /*401 unauthorized + 407 authentication required + 420 device full + 510 data store failure + 500 command failed */ + + if (code >= 500) { + /*map command has failed so delete failed map command from session->mapCpmmand */ + GList *itemIter = NULL; + item_s *item = NULL; + for (itemIter = pCommand->private.map.items; itemIter != NULL; itemIter = g_list_next(itemIter)) { + item = itemIter->data; + _DEBUG_TRACE("LUID = %s has been removed\n", get_location_loc_uri(item->source)); + + sync_agent_da_delete_mapping_query_s query; + query.option = SYNC_AGENT_DA_DELETE_MAPPING_OPTION_LUID; + query.account_id = session->account_id; + query.luid = get_location_loc_uri(item->source); + + da_err = sync_agent_delete_mapping(&query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + errorType = SA_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed in sync_agent_delete_mapping !!"); + return errorType; + } + } + session->map_command = g_list_remove(session->map_command, pCommand); + free_command(pCommand); + break; + } + if (code == ERROR_AUTH_REJECTED || code == ERROR_AUTH_REQUIRED) + errorType = SA_INTERNAL_AUTHENTICATION_ERROR; + else if (code == ERROR_NOT_FOUND) + errorType = SA_INTERNAL_NOT_FOUND; + /* for prevent */ +// else if (code == ERROR_GENERIC) +// errorType = SA_INTERNAL_SERVER_ERROR; + else + errorType = SA_INTERNAL_ERROR; + } + } + } + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_alert_command(session_s * session, command_s * command, GList ** return_datastore) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + status_s *temp = NULL; + + if (command->source == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (command->target == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + /*401 unauthorized + 407 Authentication required + 405 command not allowed + 406 optional feature not supported + 500 command failed + 412 Incomplete command + 415 unsupported media type or format */ + +// if (strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_CONTACT]->source) == 0 || +// strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_CALENDAR]->source) == 0 || +// strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_MEMO]->source) == 0 || +// strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_CALLLOG]->source) == 0) { +// +// DatastoreInfo *datastore = create_datastoreinfo(get_location_locuri(command->target), get_location_locuri(command->source)); +// if (datastore == NULL) { +// errorType = SA_INTERNAL_NO_MEMORY; +// goto error; +// } +// +// if (command->private.alert.anchor != NULL){ +// set_datastoreinfo_lastanchor(datastore, command->private.alert.anchor->lastAnchor); +// set_datastoreinfo_nextanchor(datastore, command->private.alert.anchor->nextAnchor); +// } +// +// set_datastoreInfo_synctype(datastore, command->private.alert.type); +// set_datastoreinfo_maxobjsize(datastore, command->private.alert.maxObjSize); +// +// *returnDatastore = g_list_append(*returnDatastore, datastore); +// +// errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_ALERT , &temp); +// if (errorType != SA_INTERNAL_OK) +// goto error; +// +// temp->item = create_item(); +// if (temp->item == NULL) { +// errorType = SA_INTERNAL_NO_MEMORY; +// goto error; +// } +// set_item_anchor(temp->item, command->private.alert.anchor); +// session->status = g_list_append(session->status, temp); +// +// } else if (strcmp(get_location_locuri(command->target), get_location_locuri(session->source)) == 0 && +// strcmp(get_location_locuri(command->source), get_location_locuri(session->target)) == 0) { +// +// errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_ALERT , &temp); +// if (errorType != SA_INTERNAL_OK) +// goto error; +// +// session->status = g_list_append(session->status, temp); +// } else +// goto not_found_error; + + int content_type; + bool is_data_store = false; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) + if (strcmp(get_location_loc_uri(command->target), datastoreinfo_per_content_type[content_type]->source) == 0) { + is_data_store = true; + break; + } + } + + if (is_data_store) { + datastore_info_s *datastore = create_datastore_info(get_location_loc_uri(command->target), get_location_loc_uri(command->source)); + if (datastore == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + if (command->private.alert.anchor != NULL) { + set_datastore_info_last_anchor(datastore, command->private.alert.anchor->last_anchor); + set_datastore_info_next_anchor(datastore, command->private.alert.anchor->next_anchor); + } + + set_datastore_info_sync_type(datastore, command->private.alert.type); + set_datastore_info_max_obj_size(datastore, command->private.alert.max_obj_size); + + *return_datastore = g_list_append(*return_datastore, datastore); + + errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_ALERT, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + temp->item = create_item(); + if (temp->item == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + set_item_anchor(temp->item, command->private.alert.anchor); + session->status = g_list_append(session->status, temp); + + } else if (strcmp(get_location_loc_uri(command->target), get_location_loc_uri(session->source)) == 0 && strcmp(get_location_loc_uri(command->source), get_location_loc_uri(session->target)) == 0) { + + errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_ALERT, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + } else + goto not_found_error; + + _INNER_FUNC_ENTER; + return errorType; + + not_found_error: + errorType = create_new_status(session, ERROR_NOT_FOUND, command, COMMAND_TYPE_ALERT, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + _INNER_FUNC_ENTER; + return SA_INTERNAL_NOT_FOUND; + + error: + + if (temp != NULL) + free(temp); + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_put_command(session_s * session, command_s * command) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + /*401 unauthorized + 407 authentication required + 411 size required + 413 request entity too large + 416 requested size too big + 415 unspported media type or format + 420 device full + 500 command failed */ + + status_s *temp = NULL; + + if (strcmp(command->private.access.type, ELEMENT_DEVINF_XML) == 0 || strcmp(command->private.access.type, ELEMENT_DEVINF_WBXML) == 0) { + /*if it is devinf */ + if (command->private.access.item != NULL) { + session->remote_devinf = command->private.access.item->private.devinf; + } + errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_PUT, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + } else + goto not_found_error; + + session->status = g_list_append(session->status, temp); + + _INNER_FUNC_ENTER; + return errorType; + + not_found_error: + errorType = create_new_status(session, ERROR_NOT_FOUND, command, COMMAND_TYPE_PUT, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + _INNER_FUNC_ENTER; + return SA_INTERNAL_NOT_FOUND; + + error: + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_get_command(session_s * session, command_s * command) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + status_s *temp = NULL; + command_s *pCommand = NULL; + location_s *pLocation = NULL; + + if (strcmp(command->private.access.type, ELEMENT_DEVINF_XML) == 0 || strcmp(command->private.access.type, ELEMENT_DEVINF_WBXML) == 0) { + + errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_GET, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + char *sourceDevInf = NULL; + if (session->protocol_version == VERSION_10) + sourceDevInf = ELEMENT_DEVINF_10; + else if (session->protocol_version == VERSION_11) + sourceDevInf = ELEMENT_DEVINF_11; + else if (session->protocol_version == VERSION_12) + sourceDevInf = ELEMENT_DEVINF_12; + + errorType = create_location(sourceDevInf, NULL, &pLocation); + if (errorType != SA_INTERNAL_OK) + goto error; + + errorType = create_results_command(session, pLocation, ELEMENT_DEVINF_XML, session->devinf, &pCommand); + if (errorType != SA_INTERNAL_OK) + goto error; + + set_results_command_msg_ref(pCommand, command->msg_id); + set_results_command_cmd_ref(pCommand, command->cmd_id); + set_results_command_target_ref(pCommand, command->private.access.item->target); + + session->results_command = g_list_append(session->results_command, pCommand); + } else + goto not_found_error; + + _INNER_FUNC_ENTER; + return errorType; + + not_found_error: + errorType = create_new_status(session, ERROR_NOT_FOUND, command, COMMAND_TYPE_GET, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + _INNER_FUNC_ENTER; + return SA_INTERNAL_NOT_FOUND; + + error: + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_results_command(session_s * session, command_s * command) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + status_s *temp = NULL; + + if (strcmp(command->private.results.type, ELEMENT_DEVINF_XML) == 0 || strcmp(command->private.results.type, ELEMENT_DEVINF_WBXML) == 0) { + /*if it is devinf */ + if (command->private.results.item != NULL) { + session->remote_devinf = command->private.results.item->private.devinf; + + errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_RESULTS, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + } + } else + goto not_found_error; + + _INNER_FUNC_ENTER; + return errorType; + + not_found_error: + errorType = create_new_status(session, ERROR_NOT_FOUND, command, COMMAND_TYPE_RESULTS, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + _INNER_FUNC_ENTER; + return SA_INTERNAL_NOT_FOUND; + + error: + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_sync_command(session_s * session, command_s * command, changed_datastore_s ** changed_datastore) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + status_s *temp = NULL; + + retvm_if(command == NULL, SA_INTERNAL_NOT_DEFINED, "command is NULL"); + + retvm_if(command->source == NULL, SA_INTERNAL_NOT_DEFINED, "command->source is NULL"); + retvm_if(command->target == NULL, SA_INTERNAL_NOT_DEFINED, "command->target is NULL"); + + /*TODO + need to check that if target has not exist in client + it's a error(return status) + and return Datastore index + + 401 unauthorized + 407 authentication required + 403 forbidden + 404 not founc + 405 command not allowed + 508 refresh required + 500 command failed + + TODO compare with alert command(?) */ + +// if (strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_CONTACT]->source) != 0 && +// strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_CALENDAR]->source) != 0 && +// strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_MEMO]->source) != 0 && +// strcmp(get_location_locuri(command->target), datastoreinfo_per_content_type[TYPE_CALLLOG]->source) != 0) +// goto not_found_error; +// +// Status *temp = NULL; +// errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_SYNC_START , &temp); +// if (errorType != SA_INTERNAL_OK) +// goto error; +// +// session->status = g_list_append(session->status, temp); +// +// _DEBUG_TRACE("hasNumChanged = %d",command->private.sync.hasNumChanged); +// _DEBUG_TRACE("numberOfChanges = %d",command->private.sync.numChanged); +// +// /*for return to engine*/ +// ChangedDatastore *pChangedDatastore = create_changeddatastore(get_location_locuri(command->source), +// get_location_locuri(command->target), command->private.sync.hasNumChanged, +// command->private.sync.hasNumChanged ? command->private.sync.numChanged : 0); +// if (pChangedDatastore == NULL) { +// errorType = SA_INTERNAL_NO_MEMORY; +// goto error; +// } else +// *changedDatastore = pChangedDatastore; + + int content_type; + bool exist = false; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) + if (strcmp(get_location_loc_uri(command->target), datastoreinfo_per_content_type[content_type]->source) == 0) { + exist = true; + break; + } + } + + if (!exist) + goto not_found_error; + + errorType = create_new_status(session, NO_ERROR, command, COMMAND_TYPE_SYNC_START, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + _DEBUG_TRACE("hasNumChanged = %d", command->private.sync.has_num_changed); + _DEBUG_TRACE("numberOfChanges = %d", command->private.sync.num_changed); + + /*for return to engine */ + changed_datastore_s *pChangedDatastore = create_changed_datastore(get_location_loc_uri(command->source), + get_location_loc_uri(command->target), command->private.sync.has_num_changed, + command->private.sync.has_num_changed ? command->private.sync.num_changed : 0); + if (pChangedDatastore == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } else + *changed_datastore = pChangedDatastore; + + _INNER_FUNC_ENTER; + return errorType; + + not_found_error: + errorType = create_new_status(session, ERROR_NOT_FOUND, command, COMMAND_TYPE_SYNC_START, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + _INNER_FUNC_ENTER; + return SA_INTERNAL_NOT_FOUND; + + error: + + _INNER_FUNC_ENTER; + return errorType; +} + +static sa_error_type_e _receive_changes_command(session_s * session, command_s * command, char **luid_str_list, int *index, changed_datastore_s ** changed_datastore) +{ + _INNER_FUNC_ENTER; + _DEBUG_TRACE("start command type : %d\n", command->private.change.type); + + sa_error_type_e errorType = SA_INTERNAL_OK; + + GList *iter = NULL; + changed_item_s *changed = NULL; + char *luid; + change_type_e changeType = command->private.change.type; + for (iter = command->private.change.items; iter != NULL; iter = g_list_next(iter)) { + item_s *changedItem = (iter->data); + + if (changedItem->more_data) { + if (session->large_obj_cmd != NULL) { + /*if there is a chunked item before... + it's not firest chunked item.... data have to be merged(check source location) + there will be another chunked item */ + + command_s *pLargeObjcmd = session->large_obj_cmd; + if (pLargeObjcmd->private.change.items != NULL) { + /*moreData item must be last item in item list */ + GList *largeObjItems = session->large_obj_cmd->private.change.items; + GList *largeObjLastItem = g_list_nth(largeObjItems, g_list_length(largeObjItems) - 1); + item_s *item = largeObjLastItem->data; + alert_type_e alertType = ALERT_UNKNOWN; + if (strcmp(get_location_loc_uri(item->source), get_location_loc_uri(changedItem->source)) == 0) { + /*two item's source are equal + append incomming string */ + if (item->private.data != NULL) { + char *tmp = g_strdup_printf("%s%s", item->private.data, changedItem->private.data); + free(item->private.data); + item->private.data = tmp; + } else { + item->private.data = strdup(changedItem->private.data); + } + status_s *temp = NULL; + errorType = create_new_status_location(session, CHUNK_ACCEPTED, command, changedItem->source, changedItem->target, convert_command_type_change_type(changeType), &temp); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_new_status_location"); + goto error; + } + session->status = g_list_append(session->status, temp); + alertType = ALERT_NEXT_MESSAGE; + } else { + /* it's a new data object or command but this command also have a moreData */ + alertType = ALERT_NO_END_OF_DATA; + } + /* create alert command */ + command_s *pAlertCommand = NULL; + errorType = create_alert_command(session, alertType, dup_location(session->source), dup_location(session->target), NULL, NULL, NULL, &pAlertCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_alert_command"); + goto error; + } + session->alert_command = g_list_append(session->alert_command, pAlertCommand); + } + } else { + /*first chunked item + just buffered it. does not generate LUID, and does not pass to engine */ + oma_status_type_e statusErrorType = ERROR_UNKNOWN; + if (changedItem->size == 0) { + /*size required */ + statusErrorType = ERROR_SIZE_REQUIRED; + } else { + /* size is specified */ + if (changedItem->size > session->source_max_obj_size) { + /*but it is bigger than client maxObjSize */ + statusErrorType = ERROR_REQUESTED_SIZE_TOO_BIG; + } else { + /*chunked item accepted */ + session->large_obj_cmd = command; + increase_command_ref_count(command); + + statusErrorType = CHUNK_ACCEPTED; + + /* create alert command */ + command_s *pAlertCommand = NULL; + errorType = create_alert_command(session, ALERT_NEXT_MESSAGE, dup_location(session->source), dup_location(session->target), NULL, NULL, NULL, &pAlertCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_alert_command"); + goto error; + } + session->alert_command = g_list_append(session->alert_command, pAlertCommand); + } + + status_s *temp = NULL; + errorType = create_new_status_location(session, statusErrorType, command, changedItem->source, changedItem->target, convert_command_type_change_type(changeType), &temp); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_new_status_location"); + goto error; + } + session->status = g_list_append(session->status, temp); + } + } + } else { + if (session->large_obj_cmd != NULL) { + + /*if there is a chunked item before... + it's not firest chunked item.... data have to be merged(check source location) + there will be no more chunked item. this command have to be pass to engine */ + + command_s *pLargeObjcmd = session->large_obj_cmd; + if (pLargeObjcmd->private.change.items != NULL) { + /*moreData item must be last item in item list */ + GList *largeObjItems = session->large_obj_cmd->private.change.items; + GList *largeObjLastItem = g_list_nth(largeObjItems, g_list_length(largeObjItems) - 1); + item_s *item = largeObjLastItem->data; + + if (strcmp(get_location_loc_uri(item->source), get_location_loc_uri(changedItem->source)) == 0) { + /* two item's source are equal + append incomming string */ + if (item->private.data != NULL) { + char *tmp = g_strdup_printf("%s%s", item->private.data, changedItem->private.data); + free(item->private.data); + item->private.data = tmp; + } else { + item->private.data = strdup(changedItem->private.data); + + if (item->private.data == NULL) { + _DEBUG_ERROR("item->private.data is null !!"); + goto error; + } + } + + if (item->size == strlen(item->private.data)) { + /*delete pointing from pLargeObjCmd */ + largeObjLastItem->data = NULL; + _DEBUG_TRACE("delete pointing from pLargeObjCmd"); + /*free Item from incomming Cmd */ + free_item(changedItem); + _DEBUG_TRACE("free Item from incomming Cmd"); + /*pointing to Merged item in incomming Cmd */ + iter->data = item; + changedItem = item; + _DEBUG_TRACE("pointing to Merged item in incomming Cmd"); + + free_command(session->large_obj_cmd); + session->large_obj_cmd = NULL; + } else { + status_s *temp = NULL; + errorType = create_new_status_location(session, ERROR_SIZE_MISMATCH, command, changedItem->source, changedItem->target, convert_command_type_change_type(changeType), &temp); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_new_status_location"); + goto error; + } + session->status = g_list_append(session->status, temp); + } + } else { + /* it's a new data object or command send 223 + create alert command */ + command_s *pAlertCommand = NULL; + errorType = create_alert_command(session, ALERT_NO_END_OF_DATA, dup_location(session->source), dup_location(session->target), NULL, NULL, NULL, &pAlertCommand); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_alert_command"); + goto error; + } + session->alert_command = g_list_append(session->alert_command, pAlertCommand); + } + } + } + if (changeType == CHANGE_ADD) { + int datastore_id = 0; + if (strcmp(changedItem->content_type, ELEMENT_TEXT_VCARD) == 0 || strcmp(changedItem->content_type, ELEMENT_TEXT_VCARD_30) == 0) + datastore_id = TYPE_CONTACT; + else if (strcmp(changedItem->content_type, ELEMENT_TEXT_VCAL) == 0) + datastore_id = TYPE_CALENDAR; + else if (strcmp(changedItem->content_type, ELEMENT_TEXT_PLAIN) == 0) + datastore_id = TYPE_MEMO; + + luid = luid_str_list[(*index)++]; + if (luid == NULL) { + _DEBUG_ERROR("luid is null"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + + _DEBUG_TRACE("index = %d", *index); + _DEBUG_TRACE("luid = %s", luid); + + errorType = __add_mapping(session, get_location_loc_uri(changedItem->source), luid, datastore_id); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in __add_mapping"); + goto error; + } + } else { + luid = get_location_loc_uri(changedItem->target); + if (luid == NULL) { + _DEBUG_ERROR("luid is null"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + } + + changed = create_changed_item(changeType, luid); + if (changed == NULL) { + _DEBUG_ERROR("changed is null"); + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + if (changeType != CHANGE_DELETE) { + set_changed_item_content_type(changed, changedItem->content_type); + set_changed_item_data(changed, changedItem->private.data); + } + + status_s *temp = NULL; + errorType = create_new_status_location(session, ERROR_UNKNOWN, command, changedItem->source, changedItem->target, convert_command_type_change_type(changeType), &temp); + if (errorType != SA_INTERNAL_OK) { + _DEBUG_ERROR("failed in create_new_status_location"); + goto error; + } + session->temp_status = g_list_append(session->temp_status, temp); + + (*changed_datastore)->change_item = g_list_append((*changed_datastore)->change_item, changed); + changed = NULL; + } + } + + error: + + if (changed != NULL) + free_changed_item(changed); + + _INNER_FUNC_ENTER; + return errorType; +} + +static void __free_pending_status(command_status_s * command_status) +{ + _INNER_FUNC_ENTER; + + retm_if(command_status == NULL, "pendingStatus is NULL"); + + free(command_status); + command_status = NULL; + + _INNER_FUNC_EXIT; + return; +} + +static sa_error_type_e __add_mapping(session_s * session, char *guid, char *luid, int datastore_id) +{ + _INNER_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_ERRORS; + + sync_agent_da_mapping_s *mapping = NULL; + da_err = sync_agent_create_mapping(&mapping); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_mapping !!"); + goto error; + } + + mapping->account_id = session->account_id; + mapping->data_store_id = datastore_id; + mapping->luid = strdup(luid); + mapping->guid = strdup(guid); + mapping->access_name = strdup("SA"); + + _DEBUG_VERBOSE("account_id = %d", mapping->account_id); + _DEBUG_VERBOSE("data_store_id = %d", mapping->data_store_id); + _DEBUG_VERBOSE("luid = %s", mapping->luid); + _DEBUG_VERBOSE("guid = %s", mapping->guid); + _DEBUG_VERBOSE("access_name = %s", mapping->access_name); + + da_err = sync_agent_add_mapping(mapping); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_add_mapping = %d", da_err); + sync_agent_free_mapping(mapping); + errorType = SA_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_free_mapping(mapping); + + error: + + _INNER_FUNC_EXIT; + return errorType; +} + +sa_error_type_e create_session(protocol_version_e protocol_version, protocol_type_e protocol_type, int account_id, char *sess_id, location_s * source_location, location_s * target_location, session_s ** session) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + *session = (session_s *) calloc(1, sizeof(session_s)); + if (*session == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + if (sess_id != NULL) + (*session)->session_id = strdup(sess_id); + + if (source_location != NULL) + (*session)->source = source_location; + + if (target_location != NULL) + (*session)->target = target_location; + + (*session)->protocol_type = protocol_type; + (*session)->protocol_version = protocol_version; + (*session)->account_id = account_id; + + (*session)->msg_id = 0; + (*session)->cmd_id = 1; + + (*session)->source_max_msg_size = OMA_DS_HTTP_DEFAULT_CLIENT_MAX_MSG_SIZE; + (*session)->source_max_obj_size = OMA_DS_HTTP_DEFAULT_CLIENT_MAX_OBJ_SIZE; + + (*session)->target_max_msg_size = 0; + (*session)->target_max_obj_size = 0; + + (*session)->naci_session_id = 0; + (*session)->has_opend = 0; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +void free_session(session_s * session) +{ + _EXTERN_FUNC_ENTER; + + retm_if(session == NULL, "session is NULL"); + + if (session->session_id != NULL) { + free(session->session_id); + session->session_id = NULL; + } + + if (session->jsession_id != NULL) { + free(session->jsession_id); + session->jsession_id = NULL; + } + + _DEBUG_INFO("session->status"); + free_statuses(session->status); + session->status = NULL; + + _DEBUG_INFO("session->tempStatus"); + free_statuses(session->temp_status); + session->temp_status = NULL; + + _DEBUG_INFO("session->suspendStatus"); + free_statuses(session->suspend_status); + session->suspend_status = NULL; + + if (session->target != NULL) { + free_location(session->target); + session->target = NULL; + } + + if (session->source != NULL) { + free_location(session->source); + session->source = NULL; + } + + if (session->org_target != NULL) { + free_location(session->org_target); + session->org_target = NULL; + } + + if (session->cred != NULL) { + free_cred(session->cred); + session->cred = NULL; + } + + if (session->chal != NULL) { + free_chal(session->chal); + session->chal = NULL; + } + + if (session->devinf != NULL) { + free_devinf(session->devinf); + session->devinf = NULL; + } + + if (session->remote_devinf != NULL) { + free_devinf(session->remote_devinf); + session->remote_devinf = NULL; + } + + if (session->large_obj_cmd != NULL) { + free_command(session->large_obj_cmd); + session->large_obj_cmd = NULL; + } + + free_commands(session->map_command); + session->map_command = NULL; + + free_commands(session->alert_command); + session->alert_command = NULL; + + free_commands(session->results_command); + session->results_command = NULL; + + if (session->large_obj != NULL) + free(session->large_obj); + + if (session != NULL) + free(session); + + _EXTERN_FUNC_EXIT; + return; +} + +sa_error_type_e create_command_status(unsigned int msg_id, unsigned int cmd_id, command_status_s ** command_status) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + *command_status = (command_status_s *) calloc(1, sizeof(command_status_s)); + if (*command_status == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + + (*command_status)->msg_id = msg_id; + (*command_status)->cmd_id = cmd_id; + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +void set_session_cred(session_s * session, cred_s * cred) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (session == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (cred == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + session->cred = cred; + + error: + + _EXTERN_FUNC_EXIT; + return; + +} + +void set_session_devinf(session_s * session, devinf_s * devinf) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + if (session == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (devinf == NULL) { + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + session->devinf = devinf; + + error: + + _EXTERN_FUNC_EXIT; + return; + +} + +sa_error_type_e receive_header(session_s * session, sync_hdr_s * header) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + location_s *pLocation = NULL; + + if (session == NULL) { + _DEBUG_ERROR("session is NULL"); + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + + if (header == NULL) { + _DEBUG_ERROR("header is NULL"); + errorType = SA_INTERNAL_NOT_DEFINED; + goto error; + } + if (session->session_id && header->session_id) { + if (strcmp(session->session_id, header->session_id) != 0) { + errorType = SA_INTERNAL_SERVER_ERROR; + goto error; + } + } + + session->last_recieved_msg_id = header->message_id; + + if (0 < header->max_msg_size) + session->target_max_msg_size = header->max_msg_size; + else + session->target_max_msg_size = OMA_DS_HTTP_DEFAULT_SERVER_MAX_MSG_SIZE; + + if (0 < header->max_obj_size) + session->target_max_obj_size = header->max_obj_size; + + if (header->response_uri != NULL) { + if (session->org_target == NULL) { + session->org_target = session->target; + session->target = NULL; + } + + if (session->target != NULL) + free_location(session->target); + + errorType = create_location(header->response_uri, get_location_loc_name(session->org_target), &pLocation); + if (errorType != SA_INTERNAL_OK) + goto error; + session->target = pLocation; + + if (session->target == NULL) { + errorType = SA_INTERNAL_NO_MEMORY; + goto error; + } + } + + status_s *temp = NULL; + oma_status_type_e statusData = ERROR_UNKNOWN; + if (header->cred != NULL) { + errorType = compare_cred(header->cred, session->cred); + if (errorType == SA_INTERNAL_OK) + statusData = AUTH_ACCEPTED; + else if (errorType == SA_INTERNAL_AUTHENTICATION_ERROR) { + statusData = ERROR_AUTH_REJECTED; + } else + goto error; + } else { + statusData = NO_ERROR; + } + + errorType = create_status(statusData, session->cmd_id++, session->last_recieved_msg_id, 0, header->source, header->target, COMMAND_TYPE_HEADER, &temp); + if (errorType != SA_INTERNAL_OK) + goto error; + + session->status = g_list_append(session->status, temp); + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e receive_statuses(session_s * session, GList * receive_status, GList ** return_status) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + + GList *statusItem = receive_status; + status_s *status = NULL; + while (statusItem) { + status = statusItem->data; + + if (status->cmd_ref == 0) { + /*status of SyncHdr */ + assert(status->type == COMMAND_TYPE_HEADER); + assert(status->data); + oma_status_type_e statusType = atoi(status->data); + + /* TODO off resume flag if status is 400(bad request) */ + if (session->pkg_status == SYNCML_SUSPEND) { + if (statusType == ERROR_BAD_REQUEST) { + errorType = SA_INTERNAL_SUSPEND_FAIL; + goto error; + } + } + + if (statusType == AUTH_ACCEPTED) { + /*212 + when auth type is AUTH_TYPE_BASIC does not need to send cred in syncHdr in same session + when auth type is AUTH_TYPE_MD5 the next nonce in Chal MUST used for the digest when the next sync session is started. */ + if (session->cred->type == AUTH_TYPE_MD5) { + chal_s *chal = status->chal; + if (chal != NULL) { + /*chal in status have to be stored in config_tbl because it have to be used next sync session */ + _DEBUG_INFO("format type :%d", chal->format); + + char *value; + sync_agent_da_config_s config; + config.config_id = session->account_id; + + if (chal->format == FORMAT_TYPE_BASE64) + value = chal->nonce_b64; + else + value = g_base64_encode((unsigned char *)chal->nonce_plain, chal->nonce_length); + + bool result = set_config_str(session->account_id, DEFINE_CONFIG_KEY_PROFILE_NEXT_NONCE, value, "string", "SA"); + if (result == false) { + errorType = SA_INTERNAL_ERROR; + _DEBUG_ERROR("failed in set_Config"); + goto error; + } + } + } else if (session->cred->type == AUTH_TYPE_BASIC) { + /*do not need cred anymore + but we just send it again */ + } + } else if (statusType == NO_ERROR) { + /*200 + when auth type is AUTH_TYPE_BASIC sam credentials must be sent within the next request + when auth type is AUTH_TYPE_MD5 The next nonce in Chal MUST used when the next request is sent */ + if (session->cred->type == AUTH_TYPE_MD5) { + /*if auth type is AUTH_TYPE_MD5 */ + if (status->chal != NULL) { + /*if there is a chal in status duplicate to session + chal have to used when next request is sent */ + if (session->chal != NULL) { + free_chal(session->chal); + session->chal = NULL; + } + session->chal = status->chal; + status->chal = NULL; + } + } + } else if (statusType == ERROR_AUTH_REQUIRED || statusType == ERROR_AUTH_REJECTED) { + if (status->chal != NULL) { + if (session->chal != NULL) { + free_chal(session->chal); + session->chal = NULL; + } + session->chal = status->chal; + status->chal = NULL; + } + errorType = SA_INTERNAL_AUTHENTICATION_ERROR; + goto error; + } else if (statusType == ERROR_SERVER_FAILURE) { + errorType = SA_INTERNAL_SERVER_FAILURE; + goto error; + } else if (statusType == IN_PROGRESS) { + /*busy signaling */ + errorType = SA_INTERNAL_BUSY_SIGNALING; + goto error; + } + } else { + /*status except status of SyncHdr */ + if (status->type == COMMAND_TYPE_ALERT) { + errorType = _receive_alert_status(session, status); + } else if (status->type == COMMAND_TYPE_PUT) { + errorType = _receive_put_status(session, status); + } else if (status->type == COMMAND_TYPE_GET) { + errorType = _receive_get_status(session, status); + } else if (status->type == COMMAND_TYPE_RESULTS) { + errorType = _receive_results_status(session, status); + } else if (status->type == COMMAND_TYPE_SYNC_START) { + errorType = _receive_sync_status(session, status); + } else if (status->type == COMMAND_TYPE_MAP) { + errorType = _receive_map_status(session, status); + } else if (status->type == COMMAND_TYPE_ADD || status->type == COMMAND_TYPE_REPLACE || status->type == COMMAND_TYPE_DELETE) { + errorType = _receive_changes_status(session, status, return_status); + } + + if (errorType != SA_INTERNAL_OK) + goto error; + } + statusItem = g_list_next(statusItem); + } + + error: + + _EXTERN_FUNC_EXIT; + return errorType; +} + +sa_error_type_e receive_commands(session_s * session, GList * receive_command, bool auto_config, GList ** return_datastore) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errorType = SA_INTERNAL_OK; + int item_luid_count = 0; + int index = 0; + int i; + char **luid_str_list = NULL; + changed_datastore_s *changedDatastore = NULL; + + GList *iter = NULL; + command_s *command = NULL; + for (iter = receive_command; iter != NULL; iter = g_list_next(iter)) { + command = iter->data; + + if (command->type == COMMAND_TYPE_ADD) + item_luid_count += g_list_length(command->private.change.items); + } + + _DEBUG_INFO("item_luid_count = %d", item_luid_count); + + if (item_luid_count > 0) { + luid_str_list = sync_agent_generate_item_luid(1, item_luid_count); + if (luid_str_list == NULL) { + _DEBUG_ERROR("failed in sync_agent_generate_item_luid"); + errorType = SA_INTERNAL_ERROR; + goto error; + } + } + + for (iter = receive_command; iter != NULL; iter = g_list_next(iter)) { + command = iter->data; + + if (auto_config != true || (auto_config == true && command->type == COMMAND_TYPE_RESULTS)) { + switch (command->type) { + case COMMAND_TYPE_UNKNOWN: + case COMMAND_TYPE_HEADER: + case COMMAND_TYPE_MAP: + /*never receive theses commands */ + break; + case COMMAND_TYPE_ALERT: + errorType = _receive_alert_command(session, command, return_datastore); + break; + case COMMAND_TYPE_PUT: + errorType = _receive_put_command(session, command); + break; + case COMMAND_TYPE_GET: + errorType = _receive_get_command(session, command); + break; + case COMMAND_TYPE_RESULTS: + errorType = _receive_results_command(session, command); + break; + case COMMAND_TYPE_SYNC_START: + errorType = _receive_sync_command(session, command, &changedDatastore); + break; + case COMMAND_TYPE_SYNC_END: + { + if (changedDatastore != NULL) { + if (changedDatastore->has_number_of_changes || g_list_length(changedDatastore->change_item) > 0) { + *return_datastore = g_list_append(*return_datastore, changedDatastore); + changedDatastore = NULL; + } else { + if (changedDatastore != NULL) { + free_changed_datastore(changedDatastore); + changedDatastore = NULL; + } + } + } + } + break; + case COMMAND_TYPE_ADD: + case COMMAND_TYPE_REPLACE: + case COMMAND_TYPE_DELETE: + { + if (command->type != COMMAND_TYPE_ADD || luid_str_list != NULL) { + errorType = _receive_changes_command(session, command, luid_str_list, &index, &changedDatastore); + } else { + _DEBUG_ERROR("luid_str_list is NULL !!"); + errorType = SA_INTERNAL_ERROR; + if (changedDatastore != NULL) { + free_changed_datastore(changedDatastore); + changedDatastore = NULL; + } + goto error; + } + } + break; + } + + if (errorType != SA_INTERNAL_OK) { + if (changedDatastore != NULL) { + free_changed_datastore(changedDatastore); + changedDatastore = NULL; + } + goto error; + } + } + } + + error: + + if (changedDatastore != NULL) + free_changed_datastore(changedDatastore); + + if (luid_str_list != NULL) { + /*free luid_str_list */ + for (i = 0; i < item_luid_count; i++) + free(luid_str_list[i]); + + free(luid_str_list); + } + + _EXTERN_FUNC_EXIT; + return errorType; +} + +void reset_cmd_id_session(session_s * session) +{ + _EXTERN_FUNC_ENTER; + + if (session != NULL) + session->cmd_id = 1; + + _EXTERN_FUNC_EXIT; +} diff --git a/src/agent/service-adapter/sa_util.c b/src/agent/service-adapter/sa_util.c new file mode 100755 index 0000000..6538765 --- /dev/null +++ b/src/agent/service-adapter/sa_util.c @@ -0,0 +1,177 @@ +/* + * 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_Util.c + * @version 0.1 + * @brief This file is the source file of implementation of utility function + */ + +#include <sync_agent.h> + +#include "service-adapter/sa_util.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SA" +#endif + +void put_into_list(GList ** commands, GList ** commands_last, void *command) +{ + _EXTERN_FUNC_ENTER; + + GList *temp = NULL; + if (*commands_last == NULL) { + *commands_last = *commands = g_list_append(*commands, command); + } else { + temp = g_list_append(*commands_last, command); + *commands_last = g_list_next(*commands_last); + } + + _EXTERN_FUNC_EXIT; +} + +sa_error_type_e create_cred_string(auth_type_e type, const char *user_name, const char *password, const char *nonce, unsigned int nonce_size, char **cred) +{ + _EXTERN_FUNC_ENTER; + + sa_error_type_e errortype = SA_INTERNAL_OK; + + switch (type) { + case AUTH_TYPE_BASIC: + { + char *plain = g_strjoin(":", user_name, password, NULL); + *cred = g_base64_encode((unsigned char *)plain, strlen(plain)); + if (*cred == NULL) { + free(plain); + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + free(plain); + + break; + } + case AUTH_TYPE_MD5: + { + /* How does syncml:auth-md5 works? + * + * base64( + * md5( + * base64( + * md5( + * username + ":" + password + * ) + * ) + + * ":" + nonce + * ) + * ) + */ + + /* Let's determine the string for the comparison. */ + char *auth = g_strjoin(":", user_name, password, NULL); + _DEBUG_INFO("username:password = %s", auth); + unsigned char *digest = NULL; + digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, strlen(auth)); + free(auth); + *cred = g_base64_encode(digest, 16); + free(digest); + if (*cred == NULL) { + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + + if (nonce != NULL) { + _DEBUG_INFO("nonce = %s", nonce); + _DEBUG_INFO("nonce_size = %d", nonce_size); + + int auth_size = strlen(*cred) + nonce_size + 1; + auth = (char *)calloc(auth_size + 1, sizeof(char)); + if (auth == NULL) { + _DEBUG_ERROR("auth is NULL"); + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + + memcpy(auth, *cred, strlen(*cred)); + auth[strlen(*cred)] = ':'; + memcpy(auth + strlen(*cred) + 1, nonce, nonce_size); + _DEBUG_INFO("base64[md5[username:password]] = %s", *cred); + _DEBUG_INFO("before last base64 encoding = %s", auth); + free(*cred); + + /*MD5GetDigest (auth, strlen(auth), digest); */ + + /* + GChecksum* pMd5 = g_checksum_new(G_CHECKSUM_MD5); + g_checksum_update(pMd5, auth, auth_size); + gsize temp = 16; + digest = (unsigned char*)calloc(16, sizeof(unsigned char)); + if (digest == NULL) { + _DEBUG_ERROR("digest is NULL"); + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + g_checksum_get_digest(pMd5, digest, &temp); + */ + + digest = sync_agent_encrypt_cryptograhic_hash(SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUNTION_MD5, auth, auth_size); + _DEBUG_INFO("md5[base64[md5[username:password]]] = %s", digest); + + free(auth); + *cred = g_base64_encode(digest, 16); + free(digest); + _DEBUG_INFO("base64[md5[base64[md5[username:password]]]] = %s", *cred); + if (*cred == NULL) { + errortype = SA_INTERNAL_NO_MEMORY; + goto error; + } + } + break; + case AUTH_TYPE_UNKNOWN: + break; + + } + } + + error: + + _EXTERN_FUNC_EXIT; + return errortype; +} + +void set_xml_to_file(char *xml, const char *path) +{ + _EXTERN_FUNC_ENTER; + + FILE *pFile = NULL; + + if (xml != NULL) + pFile = fopen(path, "a"); + + retm_if(pFile == NULL, "pFile is NULL"); + + fputs("==================================================================================", pFile); + fputs("\n", pFile); + + if (xml != NULL) + fputs(xml, pFile); + + if (xml != NULL) + fclose(pFile); + + _EXTERN_FUNC_EXIT; +} diff --git a/src/agent/service-engine/se_account.c b/src/agent/service-engine/se_account.c new file mode 100755 index 0000000..803f9d3 --- /dev/null +++ b/src/agent/service-engine/se_account.c @@ -0,0 +1,1865 @@ +/* + * 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. + */ + +/** + * @SE_Account.c + * @version 0.1 + * @brief This file is the source file of implementation of account in Service Engine + */ + +#include <sync_agent.h> +#include <plugin/plugin_slp_sysnoti_alarm.h> + +#include "common/csc_keys.h" +#include "common/common_vconf.h" +#include "common/common_define.h" +#include "common/common_util.h" +#include "framework/event/oma_ds_platform_event_handler.h" +#include "service-engine/se_account.h" +#include "service-engine/se_error.h" +#include "service-engine/se_common.h" +#include "service-engine/se_notification.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SE" +#endif + +#define MAX_DATASTORE_COUNT 3 + +typedef struct { + char *serverName; + int index; +} datastore_cp_info; + +static char *predefinedDatastore[][MAX_DATASTORE_COUNT][2] = { + {{"contacts", "text/x-vcard"}, {"calendar", "text/x-vcalendar"}, {"notes", "text/plain"}}, + {{"Contacts", "text/x-vcard"}, {"Calendar", "text/x-vcalendar"}, {"Notes", "text/plain"}} +}; + +static datastore_cp_info predefinedServer[] = { + {"O3SIS SyncML", 0}, + {"Everdroid", 1} +}; + +static se_error_type_e _add_profile(char *addr, char *id, char *password, char *access_name, int *account_id); +static se_error_type_e _set_profile_info(int account_id, char *profile_name, char *sync_mode, char *sync_type, char *interval); +static se_error_type_e _set_profile_category(int account_id, sync_service_s * category); +static se_error_type_e _set_periodic_sync_config(int account_id, char *sync_mode, char *interval); +static se_error_type_e _get_empty_profile_dir_name(char **empty_profile_dir_name); + +static se_error_type_e _set_profile_info_into_config_list(int account_id, char *profile_dir_name, char *addr, char *profile_name, char *sync_mode, char *sync_type, char *interval, char *nonce, char *server_id, GList ** config_list); +static se_error_type_e _set_profile_category_info(int account_id, sync_service_s * category, GList ** config_list); +static se_error_type_e _set_profile_category_info_csc(int account_id, int content_type, int index, GList ** config_list); +static se_error_type_e _set_profile_category_info_cp(int account_id, int content_type, resource_cp_s * category, GList ** config_list); +static se_error_type_e __set_profile_category_info_into_cofnig_list(int account_id, int content_type, int enabled, char *src_uri, char *tgt_uri, char *id, char *password, GList ** config_list); +static se_error_type_e ___append_config_value(int account_id, char *key, char *value, char *type, char *access_name, GList ** list); + +static se_error_type_e _add_profile(char *addr, char *id, char *password, char *access_name, int *account_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_acc_error_e account_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + + int account; + char *folderID = NULL; + + account_err = sync_agent_create_fw_account(&fw_account); + if (account_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_create_fw_account is fail"); + err = SE_INTERNAL_ERROR; + goto error; + } + + fw_account->email = strdup(id); + fw_account->password = strdup(password); + fw_account->enable = 1; + fw_account->access_name = strdup(access_name); + + account_err = sync_agent_add_fw_account(fw_account, &account); + if (account_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_add_fw_account is fail"); + err = SE_INTERNAL_ERROR; + goto error; + } + + _DEBUG_TRACE("account_id = %d", account); + + if (account < 0) { + _DEBUG_ERROR("account_id = %d", account); + err = SE_INTERNAL_ERROR; + goto error; + } + + folderID = sync_agent_generate_folder_luid(); + if (folderID == NULL) { + _DEBUG_ERROR("failed in sync_agent_generate_folder_luid"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS; + sync_agent_da_folder_s *folder = NULL; + ret = sync_agent_create_folder(&folder); + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_create_folder() failed !!"); + goto error; + } + + folder->account_id = account; + folder->folder_id = strdup(folderID); + folder->data_store_id = TYPE_CONTACT; + folder->folder_type_id = 0; /* Default contacts folder */ + folder->parent_folder_id = NULL; + folder->service_id = strdup("0"); + folder->access_name = strdup("EventHandler"); + + char *new_folder_id = NULL; + ret = sync_agent_add_folder(folder, &new_folder_id); + sync_agent_free_folder(folder); + folder = NULL; + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("Agent Add_Folder Failed_In sync_agent_add_account()!"); + _DEBUG_ERROR("[process_Event_Add_Account] Agent Add_Folder Failed_In sync_agent_add_account()!"); + err = SE_INTERNAL_ERROR; + goto error; + } + + _DEBUG_TRACE("[process_Event_Add_Account] Successed Agent Add_Folder in sync_agent_add_account(), added Folder ID : %s", new_folder_id); + + if (folderID != NULL) { + free(folderID); + folderID = NULL; + } + + if (new_folder_id != NULL) { + free(new_folder_id); + new_folder_id = NULL; + } + + folderID = sync_agent_generate_folder_luid(); + if (folderID == NULL) { + _DEBUG_ERROR("failed in sync_agent_generate_folder_luid"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + ret = sync_agent_create_folder(&folder); + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_create_folder() failed !!"); + goto error; + } + + folder->account_id = account; + folder->folder_id = strdup(folderID); + folder->data_store_id = TYPE_CALENDAR; + folder->folder_type_id = 0; /* Default contacts folder */ + folder->parent_folder_id = NULL; + folder->service_id = strdup("1"); + folder->access_name = strdup("EventHandler"); + + ret = sync_agent_add_folder(folder, &new_folder_id); + sync_agent_free_folder(folder); + folder = NULL; + + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("Agent Add_Folder Failed_In sync_agent_add_account()!"); + _DEBUG_ERROR("[process_Event_Add_Account] Agent Add_Folder Failed_In sync_agent_add_account()!"); + err = SE_INTERNAL_ERROR; + goto error; + } + + _DEBUG_TRACE("[process_Event_Add_Account] Successed Agent Add_Folder in sync_agent_add_account(), added Folder ID : %s", new_folder_id); + + if (folderID != NULL) { + free(folderID); + folderID = NULL; + } + + if (new_folder_id != NULL) { + free(new_folder_id); + new_folder_id = NULL; + } + + folderID = sync_agent_generate_folder_luid(); + if (folderID == NULL) { + _DEBUG_ERROR("failed in sync_agent_generate_folder_luid"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + ret = sync_agent_create_folder(&folder); + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_create_folder() failed !!"); + goto error; + } + + folder->account_id = account; + folder->folder_id = strdup(folderID); + folder->data_store_id = TYPE_MEMO; + folder->folder_type_id = 0; /* Default contacts folder */ + folder->parent_folder_id = NULL; + folder->service_id = strdup("0"); + folder->access_name = strdup("EventHandler"); + + ret = sync_agent_add_folder(folder, &new_folder_id); + sync_agent_free_folder(folder); + folder = NULL; + + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("Agent Add_Folder Failed_In sync_agent_add_account()!"); + _DEBUG_ERROR("[process_Event_Add_Account] Agent Add_Folder Failed_In sync_agent_add_account()!"); + err = SE_INTERNAL_ERROR; + goto error; + } + + _DEBUG_TRACE("[process_Event_Add_Account] Successed Agent Add_Folder in sync_agent_add_account(), added Folder ID : %s", new_folder_id); + + if (folderID != NULL) { + free(folderID); + folderID = NULL; + } + + if (new_folder_id != NULL) { + free(new_folder_id); + new_folder_id = NULL; + } + + /* call log folder ID */ + folderID = sync_agent_generate_folder_luid(); + if (folderID == NULL) { + _DEBUG_ERROR("failed in sync_agent_generate_folder_luid"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + ret = sync_agent_create_folder(&folder); + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_create_folder() failed !!"); + goto error; + } + + folder->account_id = account; + folder->folder_id = strdup(folderID); + folder->data_store_id = TYPE_CALLLOG; + folder->folder_type_id = 0; /* Default contacts folder */ + folder->parent_folder_id = NULL; + folder->service_id = strdup("0"); + folder->access_name = strdup("EventHandler"); + + ret = sync_agent_add_folder(folder, &new_folder_id); + sync_agent_free_folder(folder); + folder = NULL; + + if (ret != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("Agent Add_Folder Failed_In sync_agent_add_account()!"); + _DEBUG_ERROR("[process_Event_Add_Account] Agent Add_Folder Failed_In sync_agent_add_account()!"); + err = SE_INTERNAL_ERROR; + goto error; + } + + _DEBUG_TRACE("[process_Event_Add_Account] Successed Agent Add_Folder in sync_agent_add_account(), added Folder ID : %s", new_folder_id); + + if (folderID != NULL) { + free(folderID); + folderID = NULL; + } + + if (new_folder_id != NULL) { + free(new_folder_id); + new_folder_id = NULL; + } + + *account_id = account; + + error: + + if (folderID != NULL) + free(folderID); + + sync_agent_free_fw_account(fw_account); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _set_profile_info(int account_id, char *profile_name, char *sync_mode, char *sync_type, char *interval) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_NAME, profile_name, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNC_MODE, sync_mode, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE, sync_type, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_SYNC_TYPE, sync_type, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNC_INTERVAL, interval, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _set_profile_category(int account_id, sync_service_s * category) +{ + _INNER_FUNC_ENTER; + + _DEBUG_TRACE("category->content_type : %d", category->service_type); + _DEBUG_TRACE("category->enabled : %d", category->enabled); + _DEBUG_TRACE("category->src_uri : %s", category->src_uri); + _DEBUG_TRACE("category->tgt_uri : %s", category->tgt_uri); + _DEBUG_TRACE("category->id : %s", category->id); + _DEBUG_TRACE("category->password : %s", category->password); + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + + char *datastore = NULL; + char datastore_target[128]; + char datastore_source[128]; + char datastore_id[128]; + char datastore_pw[128]; + + if (category->service_type == TYPE_CONTACT) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + else if (category->service_type == TYPE_CALENDAR) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + else if (category->service_type == TYPE_MEMO) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + else if (category->service_type == TYPE_CALLLOG) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + + snprintf(datastore_target, sizeof(datastore_target), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET); + snprintf(datastore_source, sizeof(datastore_source), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + snprintf(datastore_id, sizeof(datastore_id), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID); + snprintf(datastore_pw, sizeof(datastore_pw), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD); + + result = set_config_int(account_id, datastore, category->enabled, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, datastore_source, category->src_uri, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, datastore_target, category->tgt_uri, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, datastore_id, category->id, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, datastore_pw, category->password, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; + +} + +static se_error_type_e _set_periodic_sync_config(int account_id, char *sync_mode, char *interval) +{ + _INNER_FUNC_ENTER; + _DEBUG_TRACE("sync_mode = %s", sync_mode); + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_pm_return_e scheduler_err; + bool result; + char *value = NULL; + int alarm_id = 0; + + pmci_alarm_s *alarm_info = (pmci_alarm_s *) calloc(1, sizeof(pmci_alarm_s)); + if (alarm_info == NULL) { + _DEBUG_ERROR("alarm_info is NULL"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, &value); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + alarm_id = atoi(value); + + if (alarm_id != 0) { + scheduler_err = sync_agent_remove_service_data(1, alarm_id); + if (scheduler_err != SYNC_AGENT_PM_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_remove_service_data"); + err = SE_INTERNAL_SCHEDULER_ERROR; + goto error; + } + } + + if (strcmp(sync_mode, DEFINE_SYNC_MODE_PERIODIC) == 0 && interval != NULL) { + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE, DEFINE_ALERT_TWO_WAY_STR, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + int week[] = { DAY_SUN, DAY_MON, DAY_TUE, DAY_WED, DAY_THU, DAY_FRI, DAY_SAT }; + time_t current_time; + struct tm *struct_time; + time(¤t_time); + struct_time = localtime(¤t_time); + if (struct_time == NULL) { + _DEBUG_ERROR("struct_time is NULL"); + err = SE_INTERNAL_ERROR;; + goto error; + } + + alarm_info->start_alarm_time = g_strdup_printf("%.2d-%.2d-%.2dT%.2d:%.2d:%.2dZ", struct_time->tm_year + 1900, struct_time->tm_mon + 1, struct_time->tm_mday, struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec); + alarm_info->is_disposable = 0; + + if (strcmp(interval, "5 minutes") == 0) { + _DEBUG_TRACE("5 minutes"); + alarm_info->repeat_type = REPEAT; + alarm_info->repeat_value = 5 * 60; + } else if (strcmp(interval, "15 minutes") == 0) { + _DEBUG_TRACE("15 minutes"); + alarm_info->repeat_type = REPEAT; + alarm_info->repeat_value = 15 * 60; + } else if (strcmp(interval, "1 hour") == 0) { + _DEBUG_TRACE("1 hour"); + alarm_info->repeat_type = REPEAT; + alarm_info->repeat_value = ((1 * 60) * 60); + } else if (strcmp(interval, "4 hours") == 0) { + _DEBUG_TRACE("4 hours"); + alarm_info->repeat_type = REPEAT; + alarm_info->repeat_value = ((4 * 60) * 60); + } else if (strcmp(interval, "12 hours") == 0) { + _DEBUG_TRACE("12 hours"); + alarm_info->repeat_type = REPEAT; + alarm_info->repeat_value = ((12 * 60) * 60); + } else if (strcmp(interval, "1 day") == 0) { + _DEBUG_TRACE("1 day"); + alarm_info->repeat_type = REPEAT; + alarm_info->repeat_value = ((24 * 60) * 60); + } else if (strcmp(interval, "1 week") == 0) { + _DEBUG_TRACE("1 week"); + alarm_info->repeat_type = REPEAT_WEEKLY; + alarm_info->repeat_value = week[struct_time->tm_wday]; + } + + scheduler_err = sync_agent_add_service_data(1, alarm_info, &alarm_id); + if (scheduler_err != SYNC_AGENT_PM_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_add_service_data"); + err = SE_INTERNAL_SCHEDULER_ERROR; + goto error; + } + _DEBUG_TRACE("alarm_id =%d", alarm_id); + + result = set_config_int(account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, alarm_id, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + } else { + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, "0", "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + + error: + + if (value != NULL) + free(value); + + if (alarm_info != NULL) { + if (alarm_info->start_alarm_time != NULL) + free(alarm_info->start_alarm_time); + + free(alarm_info); + } + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _set_profile_info_into_config_list(int account_id, char *profile_dir_name, char *addr, char *profile_name, char *sync_mode, char *sync_type, char *interval, char *nonce, char *server_id, GList ** config_list) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, profile_dir_name, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP, addr, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_NAME, profile_name, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNC_MODE, sync_mode != NULL ? sync_mode : "Push", "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE, sync_type != NULL ? sync_type : "Full", "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_SYNC_TYPE, sync_type != NULL ? sync_type : "Full", "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNC_INTERVAL, interval != NULL ? interval : "5 minutes", "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_NEXT_NONCE, nonce, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, server_id, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_STATUS, "-1", "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_TIME, "-1", "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, "0", "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "0", "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, "0", "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in _append_config_value"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _set_profile_category_info(int account_id, sync_service_s * category, GList ** config_list) +{ + _INNER_FUNC_ENTER; + + retvm_if(category == NULL, SE_INTERNAL_NOT_DEFINED, "category is NULL"); + + se_error_type_e err = SE_INTERNAL_OK; + err = __set_profile_category_info_into_cofnig_list(account_id, category->service_type, category->enabled, category->src_uri, category->tgt_uri, category->id, category->password, config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in _set_profile_category_info_into_cofnig_list"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _set_profile_category_info_csc(int account_id, int content_type, int index, GList ** config_list) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + char *is_enabled_path = NULL; + char *target_uri_path = NULL; + char *id_path = NULL; + char *pwd_path = NULL; + + char *datastore = NULL; + + int is_enabled = 0; + char *target_uri = NULL; + char *id = NULL; + char *pwd = NULL; + char profile_path[128]; + + char *source_uri = NULL; + + bool result; + + if (content_type == TYPE_CONTACT) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + source_uri = DEFINE_SOURCE_CONTACT_URI; + is_enabled_path = CSC_VCONF_KEY_SYNCMLDS_CONTACTS_ENABLED; + target_uri_path = CSC_VCONF_KEY_SYNCMLDS_CONTACTS_TGTURI; + id_path = CSC_VCONF_KEY_SYNCMLDS_CONTACTS_AUTHNAME; + pwd_path = CSC_VCONF_KEY_SYNCMLDS_CONTACTS_AUTHPWD; + } else if (content_type == TYPE_CALENDAR) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + source_uri = DEFINE_SOURCE_CALENDAR_URI; + is_enabled_path = CSC_VCONF_KEY_SYNCMLDS_CALENDAR_ENABLED; + target_uri_path = CSC_VCONF_KEY_SYNCMLDS_CALENDAR_TGTURI; + id_path = CSC_VCONF_KEY_SYNCMLDS_CALENDAR_AUTHNAME; + pwd_path = CSC_VCONF_KEY_SYNCMLDS_CALENDAR_AUTHPWD; + } else if (content_type == TYPE_MEMO) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + source_uri = DEFINE_SOURCE_MEMO_URI; + is_enabled_path = CSC_VCONF_KEY_SYNCMLDS_MEMO_ENABLED; + target_uri_path = CSC_VCONF_KEY_SYNCMLDS_MEMO_TGTURI; + id_path = CSC_VCONF_KEY_SYNCMLDS_MEMO_AUTHNAME; + pwd_path = CSC_VCONF_KEY_SYNCMLDS_MEMO_AUTHPWD; + } else if (content_type == TYPE_CALLLOG) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + source_uri = DEFINE_SOURCE_CALLLOG_URI; + } else { + err = SE_INTERNAL_ERROR; + _DEBUG_ERROR("No content type"); + goto error; + } + + if (content_type != TYPE_CALLLOG) { + snprintf(profile_path, sizeof(profile_path), is_enabled_path, index); + result = get_vconf_int_key(profile_path, &is_enabled); + if (result == false) { + _DEBUG_ERROR("No value in vconf key = %s", profile_path); + goto error; + } + + snprintf(profile_path, sizeof(profile_path), target_uri_path, index); + target_uri = get_vconf_str_key(profile_path); + + snprintf(profile_path, sizeof(profile_path), id_path, index); + id = get_vconf_str_key(profile_path); + + snprintf(profile_path, sizeof(profile_path), pwd_path, index); + pwd = get_vconf_str_key(profile_path); + } + + err = __set_profile_category_info_into_cofnig_list(account_id, content_type, is_enabled, source_uri, target_uri, id, pwd, config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in _set_profile_category_info_into_cofnig_list"); + goto error; + } + + error: + + if (target_uri != NULL) + free(target_uri); + + if (id != NULL) + free(id); + + if (pwd != NULL) + free(pwd); + + _INNER_FUNC_EXIT; + return err; + +} + +static se_error_type_e _set_profile_category_info_cp(int account_id, int content_type, resource_cp_s * category, GList ** config_list) +{ + _INNER_FUNC_ENTER; + + retvm_if(category == NULL, SE_INTERNAL_NOT_DEFINED, "category is NULL"); + + se_error_type_e err = SE_INTERNAL_OK; + char *sourceuri = NULL; + + if (content_type == TYPE_CONTACT) + sourceuri = DEFINE_SOURCE_CONTACT_URI; + else if (content_type == TYPE_CALENDAR) + sourceuri = DEFINE_SOURCE_CALENDAR_URI; + else if (content_type == TYPE_MEMO) + sourceuri = DEFINE_SOURCE_MEMO_URI; + else if (content_type == TYPE_CALLLOG) + sourceuri = DEFINE_SOURCE_CALLLOG_URI; + else { + err = SE_INTERNAL_ERROR; + _DEBUG_ERROR("No content type"); + goto error; + } + + err = __set_profile_category_info_into_cofnig_list(account_id, content_type, 0, sourceuri, category->name, category->id, category->password, config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in _set_profile_category_info_into_cofnig_list"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; + +} + +static se_error_type_e __set_profile_category_info_into_cofnig_list(int account_id, int content_type, int enabled, char *src_uri, char *tgt_uri, char *id, char *password, GList ** config_list) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + char *enable = NULL; + + char *datastore = NULL; + char datastore_target[128]; + char datastore_source[128]; + char datastore_id[128]; + char datastore_pw[128]; + + char datastore_dbSynced[128]; + char datastore_lastSessionTime[128]; + char datastore_s2c_Total[128]; + char datastore_s2c_NrOfAdd[128]; + char datastore_s2c_NrOfDelete[128]; + char datastore_s2c_NrOfReplace[128]; + char datastore_c2s_Total[128]; + char datastore_c2s_NrOfAdd[128]; + char datastore_c2s_NrOfDelete[128]; + char datastore_c2s_NrOfReplace[128]; + + if (content_type == TYPE_CONTACT) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + else if (content_type == TYPE_CALENDAR) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + else if (content_type == TYPE_MEMO) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + else if (content_type == TYPE_CALLLOG) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + + snprintf(datastore_target, sizeof(datastore_target), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET); + snprintf(datastore_source, sizeof(datastore_source), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + snprintf(datastore_id, sizeof(datastore_id), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID); + snprintf(datastore_pw, sizeof(datastore_pw), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD); + + snprintf(datastore_dbSynced, sizeof(datastore_dbSynced), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_DBSYNCED); + snprintf(datastore_lastSessionTime, sizeof(datastore_lastSessionTime), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_LAST_TIME); + + snprintf(datastore_s2c_Total, sizeof(datastore_s2c_Total), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_TOTAL); + snprintf(datastore_s2c_NrOfAdd, sizeof(datastore_s2c_NrOfAdd), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFADD); + snprintf(datastore_s2c_NrOfDelete, sizeof(datastore_s2c_NrOfDelete), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFDELETE); + snprintf(datastore_s2c_NrOfReplace, sizeof(datastore_s2c_NrOfReplace), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFREPLACE); + + snprintf(datastore_c2s_Total, sizeof(datastore_c2s_Total), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_TOTAL); + snprintf(datastore_c2s_NrOfAdd, sizeof(datastore_c2s_NrOfAdd), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFADD); + snprintf(datastore_c2s_NrOfDelete, sizeof(datastore_c2s_NrOfDelete), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFDELETE); + snprintf(datastore_c2s_NrOfReplace, sizeof(datastore_c2s_NrOfReplace), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFREPLACE); + + enable = g_strdup_printf("%u", enabled); + result = ___append_config_value(account_id, datastore, enable, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_source, src_uri, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_target, tgt_uri, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_id, id, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_pw, password, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_dbSynced, DEFINE_DBSYNC_NO, "string", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_lastSessionTime, "-1", "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_s2c_Total, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_s2c_NrOfAdd, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_s2c_NrOfDelete, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_s2c_NrOfReplace, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_c2s_Total, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_c2s_NrOfAdd, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_c2s_NrOfDelete, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = ___append_config_value(account_id, datastore_c2s_NrOfReplace, 0, "int", "SE", config_list); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + if (enable != NULL) + free(enable); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e ___append_config_value(int account_id, char *key, char *value, char *type, char *access_name, GList ** list) +{ + _INNER_FUNC_ENTER; + se_error_type_e err = SE_INTERNAL_OK; + + bool result; + + sync_agent_da_config_s *config = NULL; + + result = create_config_str(account_id, key, value, type, access_name, &config); + if (result == false) { + _DEBUG_ERROR("failed in create_config_str"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + if (config != NULL) { + *list = g_list_append(*list, config); + config = NULL; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _get_empty_profile_dir_name(char **empty_profile_dir_name) +{ + _INNER_FUNC_ENTER; + se_error_type_e err = SE_INTERNAL_OK; + + bool result; + char *profileDirName = NULL; + int profile[MAXIMUM_ACCOUNT_COUNT] = { 0 }; + char profileIndex[2]; + int index; + int i; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + GList *account_info_list = NULL; + GList *iter = NULL; + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_NONE; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto error; + } + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + fw_account = (sync_agent_fw_account_s *) iter->data; + + if (profileDirName != NULL) { + free(profileDirName); + profileDirName = NULL; + } + + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + _DEBUG_TRACE("profile_Dir_name = %s", profileDirName); + if (profileDirName == NULL) + continue; + + strcpy(profileIndex, profileDirName + strlen(profileDirName) - 1); + _DEBUG_TRACE("profileIndex = %s", profileIndex); + + index = atoi(profileIndex); + profile[index - 1] = 1; + } + + for (i = 0; i < MAXIMUM_ACCOUNT_COUNT; i++) { + if (profile[i] != 1) { + *empty_profile_dir_name = g_strdup_printf("Sync%d", i + 1); + break; + } + } + + error: + + sync_agent_free_fw_account_list(account_info_list); + + if (profileDirName != NULL) + free(profileDirName); + + _INNER_FUNC_EXIT; + return err; +} + +bool add_profile(char *profile_dir_name, char *profile_name, char *addr, char *id, char *password, char *sync_mode, char *sync_type, char *interval, GList * categories, int *account_id) +{ + _EXTERN_FUNC_ENTER; + + _DEBUG_INFO("profile_name = %s", profile_name); + _DEBUG_INFO("addr = %s", addr); + _DEBUG_INFO("id = %s", id); + _DEBUG_INFO("password = %s", password); + _DEBUG_INFO("sync_mode = %s", sync_mode); + _DEBUG_INFO("sync_type = %s", sync_type); + _DEBUG_INFO("interval = %s", interval); + + se_error_type_e err = SE_INTERNAL_OK; + int account = -1; + GList *config_list = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + err = _add_profile(addr, id, password, "SE", &account); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + err = _set_profile_info_into_config_list(account, profile_dir_name, addr, profile_name, sync_mode, sync_type, interval, NULL, NULL, &config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + sync_service_s *category = NULL; + GList *iter = NULL; + for (iter = categories; iter != NULL; iter = g_list_next(iter)) { + category = iter->data; + err = _set_profile_category_info(account, category, &config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __set_profile_category"); + goto error; + } + } + + da_err = sync_agent_add_config_list(config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + err = _set_periodic_sync_config(account, sync_mode, interval); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in _set_periodic_sync_config"); + goto error; + } + + *account_id = account; + + error: + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + _DEBUG_INFO("err = %d", err); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool add_profile_csc(int index) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + char profile_path[128]; + char *profile_dir_name = NULL; + char *profile_name = NULL; + char *id = NULL; + char *password = NULL; + char *server_ip = NULL; + char *sync_mode = NULL; + char *sync_type = NULL; + int account_id; + + GList *config_list = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + err = _get_empty_profile_dir_name(&profile_dir_name); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __get_empty_profileDirName"); + goto error; + } + + if (profile_dir_name == NULL) { + _DEBUG_ERROR("Maximum profiles count is %d, no more profile", MAXIMUM_ACCOUNT_COUNT); + err = SE_INTERNAL_ERROR; + goto error; + } + + snprintf(profile_path, sizeof(profile_path), CSC_VCONF_KEY_SYNCMLDS_AUTHNAME, index); + id = get_vconf_str_key(profile_path); + + snprintf(profile_path, sizeof(profile_path), CSC_VCONF_KEY_SYNCMLDS_AUTHPWD, index); + password = get_vconf_str_key(profile_path); + + snprintf(profile_path, sizeof(profile_path), CSC_VCONF_KEY_SYNCMLDS_ADDR, index); + server_ip = get_vconf_str_key(profile_path); + + snprintf(profile_path, sizeof(profile_path), CSC_VCONF_KEY_SYNCMLDS_NAME, index); + profile_name = get_vconf_str_key(profile_path); + + snprintf(profile_path, sizeof(profile_path), CSC_VCONF_KEY_SYNCMLDS_SYNCMODE, index); + sync_mode = get_vconf_str_key(profile_path); + + snprintf(profile_path, sizeof(profile_path), CSC_VCONF_KEY_SYNCMLDS_SYNCTYPE, index); + sync_type = get_vconf_str_key(profile_path); + + _DEBUG_INFO("email = %s", id); + _DEBUG_INFO("password = %s", password); + _DEBUG_INFO("serverIP = %s", server_ip); + _DEBUG_INFO("profileName = %s", profile_name); + _DEBUG_INFO("syncMode = %s", sync_mode); + _DEBUG_INFO("syncType = %s", sync_type); + + err = _add_profile(server_ip, id, password, "SE", &account_id); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + err = _set_profile_info_into_config_list(account_id, profile_dir_name, server_ip, profile_name, sync_mode, sync_type, NULL, NULL, NULL, &config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + err = _set_profile_category_info_csc(account_id, content_type, index, &config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in set_DatastoreConfig"); + goto error; + } + } + + da_err = sync_agent_add_config_list(config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + if (profile_dir_name != NULL) + free(profile_dir_name); + if (profile_name != NULL) + free(profile_name); + if (id != NULL) + free(id); + if (password != NULL) + free(password); + if (server_ip != NULL) + free(server_ip); + if (sync_mode != NULL) + free(sync_mode); + if (sync_type != NULL) + free(sync_type); + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool add_profile_cp(char *profile_name, char *addr, char *id, char *password, GList * categories, int *account_id) +{ + _EXTERN_FUNC_ENTER; + + _DEBUG_INFO("profilename = %s", profile_name); + _DEBUG_INFO("addr = %s", addr); + _DEBUG_INFO("id = %s", id); + _DEBUG_INFO("password = %s", password); + + se_error_type_e err = SE_INTERNAL_OK; + int account = -1; + + char *profile_dir_name = NULL; + GList *config_list = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + err = _get_empty_profile_dir_name(&profile_dir_name); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __get_empty_profileDirName"); + goto error; + } + + if (profile_dir_name == NULL) { + _DEBUG_ERROR("Maximum profiles count is %d, no more profile", MAXIMUM_ACCOUNT_COUNT); + err = SE_INTERNAL_ERROR; + goto error; + } + + err = _add_profile(addr, id, password, "SE", &account); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + err = _set_profile_info_into_config_list(account, profile_dir_name, addr, profile_name, NULL, NULL, NULL, NULL, NULL, &config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + int i; + int cnt = (int)sizeof(predefinedServer) / sizeof(datastore_cp_info); + _DEBUG_INFO("cnt = %d", cnt); + for (i = 0; i < cnt; i++) { + + _DEBUG_INFO("predefinedServer[%d].serverName = %s", i, predefinedServer[i].serverName); + if (strcmp(predefinedServer[i].serverName, profile_name) == 0) { + + int j; + for (j = 0; j < MAX_DATASTORE_COUNT; j++) { + resource_cp_s *category = NULL; + GList *iter = NULL; + _DEBUG_INFO("categories length = %d", g_list_length(categories)); + for (iter = categories; iter != NULL; iter = g_list_next(iter)) { + category = (resource_cp_s *) iter->data; + _DEBUG_INFO("category->name = %s", category->name); + _DEBUG_INFO("category->accept = %s", category->accept); + + _DEBUG_INFO("predefinedDatastore[i][j][0] = %s", predefinedDatastore[i][j][0]); + _DEBUG_INFO("predefinedDatastore[i][j][1] = %s", predefinedDatastore[i][j][1]); + + if (predefinedDatastore[i][j][0] != NULL && category->name != NULL && predefinedDatastore[i][j][1] != NULL && category->accept != NULL) { + + if (strcasecmp(predefinedDatastore[i][j][0], category->name) == 0 && strcmp(predefinedDatastore[i][j][1], category->accept) == 0) { + + err = _set_profile_category_info_cp(account, j, category, &config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __set_profile_category"); + goto error; + } + break; + } + } + } + } + } + } + + da_err = sync_agent_add_config_list(config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + *account_id = account; + + error: + + if (profile_dir_name != NULL) + free(profile_dir_name); + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool add_profile_dive(int *account_id) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_dev_return_e dci_err = SYNC_AGENT_DEV_RETURN_SUCCESS; + char *id = NULL; + char *password = NULL; + char *encoded_nextnonce = NULL; + int account = -1; + + GList *config_list = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + /* FIXME get id from somewhere */ + id = "ss.linux.test@gmail.com"; + + /*get IMEI from Platform */ + dci_err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &password); + if (dci_err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + _DEBUG_ERROR("failed to get devinfo"); + goto error; + } + + err = _add_profile(DEFINE_DIVE_SERVER_URL_HTTPS, id, password, "DIVE", &account); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + encoded_nextnonce = g_base64_encode((unsigned char *)DEFINE_DIVE_DEFAULT_NEXT_NONCE, strlen(DEFINE_DIVE_DEFAULT_NEXT_NONCE)); + + err = _set_profile_info_into_config_list(account, NULL, DEFINE_DIVE_SERVER_URL_HTTPS, NULL, NULL, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR, NULL, encoded_nextnonce, DEFINE_DIVE_SERVER_ID, &config_list); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in add_Account"); + goto error; + } + + /*set calllog config value */ + __set_profile_category_info_into_cofnig_list(account, TYPE_CALLLOG, 1, DEFINE_SOURCE_CALLLOG_URI, "./CallLog", NULL, NULL, &config_list); + + da_err = sync_agent_add_config_list(config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + *account_id = account; + + error: + + if (password != NULL) + free(password); + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool edit_profile(int account_id, char *profile_name, char *addr, char *id, char *password, char *sync_mode, char *sync_type, char *interval, GList * categories) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + bool result; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + acc_err = sync_agent_create_fw_account(&fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + err = SE_INTERNAL_ERROR; + goto error; + } + + fw_account->account_id = account_id; + fw_account->email = strdup(id); + fw_account->password = strdup(password); + fw_account->enable = 1; + fw_account->access_name = strdup("SE"); + + acc_err = sync_agent_update_fw_account(fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP, addr, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + err = _set_profile_info(account_id, profile_name, sync_mode, sync_type, interval); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __set_profile_info"); + goto error; + } + + err = _set_periodic_sync_config(account_id, sync_mode, interval); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __set_profile_info"); + goto error; + } + + sync_service_s *category = NULL; + GList *iter = NULL; + for (iter = categories; iter != NULL; iter = g_list_next(iter)) { + category = iter->data; + err = _set_profile_category(account_id, category); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __set_profile_category"); + goto error; + } + } + + error: + + sync_agent_free_fw_account(fw_account); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool delete_profiles(GList * profiles) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + int *accountId = 0; + char *value = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + GList *iter = NULL; + int alarmId = 0; + + bool result; + for (iter = profiles; iter != NULL; iter = g_list_next(iter)) { + accountId = (int *)(iter->data); + + if (accountId != NULL) { + _DEBUG_INFO("accountId = %d", *accountId); + /*if deleteing Account's sync mode is periodic sync it has to be deleted from schedule_tbl */ + result = get_config(*accountId, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, &value); + if (result == false) { + _DEBUG_ERROR("failed in get_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + alarmId = atoi(value); + if (alarmId != 0) { + sync_agent_pm_return_e scheduler_err = sync_agent_remove_service_data(1, alarmId); + if (scheduler_err != SYNC_AGENT_PM_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_remove_service_data"); + err = SE_INTERNAL_SCHEDULER_ERROR; + goto error; + } + } + sync_agent_delete_fw_account(*accountId); + if (accountId != 0) { + free(accountId); + accountId = 0; + } + } else { + _DEBUG_ERROR("accountId is NULL !"); + } + } + + error: + + if (value != NULL) + free(value); + + if (accountId != NULL) + free(accountId); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool delete_profile_dive() +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + GList *account_info_list = NULL; + GList *iter = NULL; + sync_agent_fw_account_s *account_info = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_agent"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_ACCESS_NAME; + query.access_name = "DIVE"; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + account_info = (sync_agent_fw_account_s *) iter->data; + _DEBUG_INFO("accountId = %d", account_info->account_id); + sync_agent_delete_fw_account(account_info->account_id); + } + + error: + + sync_agent_free_fw_account_list(account_info_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool delete_profile(char *access_name) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + GList *account_info_list = NULL; + GList *iter = NULL; + sync_agent_fw_account_s *account_info = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_agent"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_ACCESS_NAME; + query.access_name = access_name; + + /*delete account */ + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + account_info = (sync_agent_fw_account_s *) iter->data; + _DEBUG_INFO("accountId = %d", account_info->account_id); + sync_agent_delete_fw_account(account_info->account_id); + } + + error: + + sync_agent_free_fw_account_list(account_info_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool delete_all_profiles() +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + GList *account_info_list = NULL; + GList *iter = NULL; + + int alarm_id = 0; + bool result; + char *value = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_NONE; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto error; + } + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + fw_account = (sync_agent_fw_account_s *) iter->data; + + _DEBUG_INFO("accountId = %d", fw_account->account_id); + /*if deleteing Account's sync mode is periodic sync it has to be deleted from schedule_tbl */ + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, &value); + if (result == false) { + _DEBUG_ERROR("failed in get_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + if (value != NULL) { + alarm_id = atoi(value); + if (alarm_id != 0) { + sync_agent_pm_return_e scheduler_err = sync_agent_remove_service_data(1, alarm_id); + if (scheduler_err != SYNC_AGENT_PM_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_remove_service_data"); + err = SE_INTERNAL_SCHEDULER_ERROR; + goto error; + } + } + + free(value); + value = NULL; + + sync_agent_delete_fw_account(fw_account->account_id); + } else { + _DEBUG_ERROR("value is NULL !"); + } + } + + error: + + if (value != NULL) + free(value); + + sync_agent_free_fw_account_list(account_info_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool get_synchronising_profile(char **profile_Dir_name, char **sync_type) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + bool result; + char *value = NULL; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + GList *account_info_list = NULL; + GList *iter = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_NONE; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto error; + } + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + fw_account = (sync_agent_fw_account_s *) iter->data; + + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, &value); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + if (strcmp(value, "1") == 0) { + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, profile_Dir_name); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_SYNC_TYPE, sync_type); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + break; + } + + free(value); + value = NULL; + } + + error: + + if (value != NULL) + free(value); + + sync_agent_free_fw_account_list(account_info_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool delete_all_agent_default_table() +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + da_err = sync_agent_empty_all_agent_default_table(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} diff --git a/src/agent/service-engine/se_notification.c b/src/agent/service-engine/se_notification.c new file mode 100755 index 0000000..1fdf1c5 --- /dev/null +++ b/src/agent/service-engine/se_notification.c @@ -0,0 +1,133 @@ +/* + * 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. + */ + +/** + * @SE_Notification.c + * @version 0.1 + * @brief This file is the source file of implementation of notify to ui + */ + +#include <aul.h> + +#include <sync_agent.h> + +#include "service-engine/se_notification.h" +#include "service-engine/se_sync.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SE" +#endif + +#define NOTI_KEY "OMADS" +#define OMA_DS_UI_PKG "ug-setting-synchronise-efl" + +se_error_type_e send_noti_session_process(char *profile, char *sync_type, char *progress, char *error) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + int notiType = 1; + sync_agent_event_data_s *noti = NULL; + + if (profile == NULL) { + _DEBUG_ERROR("Not Defined profile"); + err = SE_INTERNAL_NOT_DEFINED; + goto error; + } + + noti = sync_agent_create_noti(notiType); + if (noti == NULL) { + _DEBUG_ERROR("failed to sync_agent_create_noti"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)sync_type); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)progress); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)error); + + sync_agent_event_error_e event_err; + sync_agent_send_noti(NOTI_KEY, noti, NULL, NULL, &event_err); + if (event_err != SYNC_AGENT_EVENT_SUCCESS) { + if (aul_app_is_running(OMA_DS_UI_PKG)) { + _DEBUG_ERROR("failed to sync_agent_send_noti"); + err = SE_INTERNAL_EVENT_ERROR; + goto error; + } + } + + error: + + sync_agent_free_noti(noti); + + _EXTERN_FUNC_EXIT; + return err; +} + +se_error_type_e send_noti_process_update(char *profile_dir_name, char *sync_type, char *uri, char *progress_status, char *operation_type, int is_from_server, int total_per_operation, int synced_per_operation, int total_per_db, int synced_per_db) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + int notiType = 2; + sync_agent_event_data_s *noti = NULL; + + if (profile_dir_name == NULL) { + _DEBUG_ERROR("Not Defined profileDirName"); + err = SE_INTERNAL_NOT_DEFINED; + goto error; + } + + noti = sync_agent_create_noti(notiType); + if (noti == NULL) { + _DEBUG_ERROR("failed to sync_agent_create_noti"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_dir_name); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)sync_type); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)uri); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)progress_status); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)operation_type); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &is_from_server); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &total_per_operation); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &synced_per_operation); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &total_per_db); + sync_agent_append_event_data_param(noti, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &synced_per_db); + + sync_agent_event_error_e event_err; + sync_agent_send_noti(NOTI_KEY, noti, NULL, NULL, &event_err); + if (event_err != SYNC_AGENT_EVENT_SUCCESS) { + if (aul_app_is_running(OMA_DS_UI_PKG)) { + _DEBUG_ERROR("failed to sync_agent_send_noti"); + err = SE_INTERNAL_EVENT_ERROR; + goto error; + } + } + + error: + + sync_agent_free_noti(noti); + + _EXTERN_FUNC_EXIT; + return err; +} diff --git a/src/agent/service-engine/se_storage.c b/src/agent/service-engine/se_storage.c new file mode 100755 index 0000000..51beeff --- /dev/null +++ b/src/agent/service-engine/se_storage.c @@ -0,0 +1,1000 @@ +/* + * 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. + */ + +/** + * @SE_Storage.c + * @version 0.1 + * @brief This file is the source file of implementation of functions which saves and gets sync results + */ + +#include <stdlib.h> + +#include <sync_agent.h> + +#include "common/common_util.h" +#include "service-engine/se_storage.h" +#include "service-engine/se_common.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SE" +#endif + +static se_error_type_e _write_sync_type(int account_id, alert_type_e alert_type); +static se_error_type_e _write_last_session_values(int account_id, sync_session_result_e sync_session_result, int last_session_time); + +static se_error_type_e _write_sync_type(int account_id, alert_type_e alert_type) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + + char *syncType = NULL; + switch (alert_type) { + case ALERT_SLOW_SYNC: + syncType = DEFINE_ALERT_SLOW_SYNC_STR; + break; + case ALERT_TWO_WAY: + syncType = DEFINE_ALERT_TWO_WAY_STR; + break; + case ALERT_ONE_WAY_FROM_CLIENT: + syncType = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR; + break; + case ALERT_ONE_WAY_FROM_SERVER: + syncType = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR; + break; + case ALERT_REFRESH_FROM_SERVER: + syncType = DEFINE_ALERT_REFRESH_FROM_SERVER_STR; + break; + case ALERT_REFRESH_FROM_CLIENT: + syncType = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR; + break; + default: + break; + } + + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE, syncType, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; + +} + +static se_error_type_e _write_last_session_values(int account_id, sync_session_result_e sync_session_result, int last_session_time) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + + result = set_config_int(account_id, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_STATUS, sync_session_result, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_int(account_id, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_TIME, last_session_time, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +se_error_type_e write_profile_data(int account_id, alert_type_e alert_type, sync_session_result_e sync_session_result, int last_session_time, int only_from_client) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + err = _write_sync_type(account_id, alert_type); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in writeSyncType"); + goto error; + } + + err = _write_last_session_values(account_id, sync_session_result, last_session_time); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in writeLastSessionValues"); + goto error; + } + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + err = write_sync_resource_info(account_id, content_type, last_session_time, only_from_client, + datastoreinfo_per_content_type[content_type]->client_sync_result, datastoreinfo_per_content_type[content_type]->server_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in writeSyncResourceInfo"); + goto error; + } + + err = write_sync_statistics(account_id, content_type, true, datastoreinfo_per_content_type[content_type]->server_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in writeSyncStatistics"); + goto error; + } + + err = write_sync_statistics(account_id, content_type, false, datastoreinfo_per_content_type[content_type]->client_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in writeSyncStatistics"); + goto error; + } + } + } + } + + error: + + _EXTERN_FUNC_EXIT; + return err; +} + +se_error_type_e write_sync_statistics(int account_id, int content_type, bool is_from_server, sync_result_s * sync_result) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + bool result; + char *datastore = NULL; + char *side = NULL; + char numberOfChangesPath[128]; + char addCountPath[128]; + char replaceCountPath[128]; + char deleteCountPath[128]; + + if (content_type == TYPE_CONTACT) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + else if (content_type == TYPE_CALENDAR) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + else if (content_type == TYPE_MEMO) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + else if (content_type == TYPE_CALLLOG) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + + if (is_from_server == true) + side = DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER; + else + side = DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT; + + _DEBUG_INFO("pSyncResult->numberOfChange = %d", sync_result->number_of_change); + _DEBUG_INFO("pSyncResult->add_count = %d", sync_result->add_count); + _DEBUG_INFO("pSyncResult->replace_count = %d", sync_result->replace_count); + _DEBUG_INFO("pSyncResult->delete_count = %d", sync_result->delete_count); + + snprintf(numberOfChangesPath, sizeof(numberOfChangesPath), "%s_%s_%s", datastore, side, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_TOTAL); + snprintf(addCountPath, sizeof(addCountPath), "%s_%s_%s", datastore, side, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFADD); + snprintf(replaceCountPath, sizeof(replaceCountPath), "%s_%s_%s", datastore, side, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFREPLACE); + snprintf(deleteCountPath, sizeof(deleteCountPath), "%s_%s_%s", datastore, side, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFDELETE); + + result = set_config_int(account_id, numberOfChangesPath, sync_result->number_of_change, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_int(account_id, addCountPath, sync_result->add_count, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_int(account_id, replaceCountPath, sync_result->replace_count, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_int(account_id, deleteCountPath, sync_result->delete_count, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _EXTERN_FUNC_EXIT; + return err; + +} + +se_error_type_e write_sync_resource_info(int account_id, int content_type, int last_session_time, int only_from_client, sync_result_s * client_sync_result, sync_result_s * server_sync_result) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + char *datastore = NULL; + char dbSyncedPath[128]; + char lastSessionTimePath[128]; + + if (client_sync_result == NULL) { + _DEBUG_ERROR("clientSyncResult is NULL"); + err = SE_INTERNAL_NOT_DEFINED; + goto error; + } + + if (server_sync_result == NULL) { + _DEBUG_ERROR("serverSyncResult is NULL"); + err = SE_INTERNAL_NOT_DEFINED; + goto error; + } + + if (content_type == TYPE_CONTACT) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + else if (content_type == TYPE_CALENDAR) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + else if (content_type == TYPE_MEMO) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + else if (content_type == TYPE_CALLLOG) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + + _DEBUG_INFO("clientSyncResult->sessionResult = %d", client_sync_result->session_result); + _DEBUG_INFO("serverSyncResult->sessionResult = %d", server_sync_result->session_result); + + char *dbSynced; + if (client_sync_result->session_result == SYNC_SESSION_SUCCEEDED && (server_sync_result->session_result == SYNC_SESSION_SUCCEEDED || only_from_client)) + dbSynced = DEFINE_DBSYNC_SUCCESS; + else if (client_sync_result->session_result == SYNC_SESSION_STOPPED) + dbSynced = DEFINE_DBSYNC_STOP; + else + dbSynced = DEFINE_DBSYNC_FAIL; + + _DEBUG_INFO("dbSynced = %s", dbSynced); + + snprintf(dbSyncedPath, sizeof(dbSyncedPath), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_DBSYNCED); + snprintf(lastSessionTimePath, sizeof(lastSessionTimePath), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_LAST_TIME); + + result = set_config_str(account_id, dbSyncedPath, dbSynced, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + result = set_config_int(account_id, lastSessionTimePath, last_session_time, "int", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _EXTERN_FUNC_EXIT; + return err; +} + +bool get_profile_data(int account_id, char **profile_name, char **addr, char **id, char **password, char **sync_mode, char **sync_type, char **interval, int *last_session_status, int *last_session_time) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + + GList *config_list = NULL; + GList *iter = NULL; + sync_agent_da_config_s *config_data = NULL; + + char *lastSessionStatus_str = NULL; + char *lastSessionTime_str = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + acc_err = sync_agent_create_fw_account(&fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_fw_account"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + acc_err = sync_agent_get_fw_account(account_id, &fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_update_fw_account"); + err = SE_INTERNAL_ERROR; + goto error; + } + + if (fw_account->email != NULL) + *id = strdup(fw_account->email); + + if (fw_account->password != NULL) + *password = strdup(fw_account->password); + + da_err = sync_agent_get_config_list(account_id, &config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + for (iter = config_list; iter != NULL; iter = g_list_next(iter)) { + config_data = (sync_agent_da_config_s *) iter->data; + + if (config_data != NULL) { + if (config_data->key != NULL) { + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_NAME) == 0) { + if (config_data->value != NULL) { + *profile_name = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP) == 0) { + if (config_data->value != NULL) { + *addr = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SYNC_MODE) == 0) { + if (config_data->value != NULL) { + *sync_mode = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE) == 0) { + if (config_data->value != NULL) { + *sync_type = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SYNC_INTERVAL) == 0) { + if (config_data->value != NULL) { + *interval = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_STATUS) == 0) { + if (config_data->value != NULL) { + lastSessionStatus_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_TIME) == 0) { + if (config_data->value != NULL) { + lastSessionTime_str = strdup(config_data->value); + continue; + } + } + } + } + } + + if (lastSessionStatus_str != NULL) + *last_session_status = atoi(lastSessionStatus_str); + + if (lastSessionTime_str != NULL) + *last_session_time = atoi(lastSessionTime_str); + + error: + + sync_agent_free_fw_account(fw_account); + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + if (lastSessionStatus_str != NULL) + free(lastSessionStatus_str); + + if (lastSessionTime_str != NULL) + free(lastSessionTime_str); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool get_profile_sync_category(int account_id, int content_type, int *enabled, char **src_uri, char **tgt_uri, char **id, char **password) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + char *datastore = NULL; + char *enabled_str = NULL; + + char datastore_source[128]; + char datastore_target[128]; + char datastore_id[128]; + char datastore_pw[128]; + + GList *config_list = NULL; + GList *iter = NULL; + sync_agent_da_config_s *config_data = NULL; + + if (content_type == TYPE_CONTACT) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + else if (content_type == TYPE_CALENDAR) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + else if (content_type == TYPE_MEMO) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + else if (content_type == TYPE_CALLLOG) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + + snprintf(datastore_source, sizeof(datastore_source), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + snprintf(datastore_target, sizeof(datastore_target), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET); + snprintf(datastore_id, sizeof(datastore_id), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID); + snprintf(datastore_pw, sizeof(datastore_pw), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD); + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + da_err = sync_agent_get_config_list(account_id, &config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + for (iter = config_list; iter != NULL; iter = g_list_next(iter)) { + config_data = (sync_agent_da_config_s *) iter->data; + + if (config_data != NULL) { + if (config_data->key != NULL) { + if (datastore != NULL) { + if (strcmp(config_data->key, datastore) == 0) { + if (config_data->value != NULL) { + enabled_str = strdup(config_data->value); + continue; + } + } + } else { + _DEBUG_ERROR("datastore is NULL !!"); + err = SE_INTERNAL_ERROR; + goto error; + } + + if (strcmp(config_data->key, datastore_source) == 0) { + if (config_data->value != NULL) { + *src_uri = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_target) == 0) { + if (config_data->value != NULL) { + *tgt_uri = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_id) == 0) { + if (config_data->value != NULL) { + *id = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_pw) == 0) { + if (config_data->value != NULL) { + *password = strdup(config_data->value); + continue; + } + } + } + } + } + + if (enabled_str != NULL) + *enabled = atoi(enabled_str); + + error: + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + if (enabled_str != NULL) + free(enabled_str); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; + +} + +bool get_profile_statistics(int account_id, int content_type, char **db_synced, int *last_session_time, + int *server2client_total, int *server2client_nrofadd, int *server2client_nrofdelete, int *server2client_nrofreplace, + int *client2server_total, int *client2server_nrofadd, int *client2server_nrofdelete, int *client2server_nrofreplace) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + char *datastore = NULL; + char datastore_dbsynced[128]; + char datastore_lastsessiontime[128]; + char datastore_s2c_total[128]; + char datastore_s2c_add[128]; + char datastore_s2c_replace[128]; + char datastore_s2c_delete[128]; + char datastore_c2s_total[128]; + char datastore_c2s_add[128]; + char datastore_c2s_replace[128]; + char datastore_c2s_delete[128]; + + char *lastSessionTime_str = NULL; + char *server2Client_Total_str = NULL; + char *server2Client_NrOfAdd_str = NULL; + char *server2Client_NrOfDelete_str = NULL; + char *server2Client_NrOfReplace_str = NULL; + char *client2Server_Total_str = NULL; + char *client2Server_NrOfAdd_str = NULL; + char *client2Server_NrOfDelete_str = NULL; + char *client2Server_NrOfReplace_str = NULL; + + GList *config_list = NULL; + GList *iter = NULL; + sync_agent_da_config_s *config_data = NULL; + + if (content_type == TYPE_CONTACT) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + else if (content_type == TYPE_CALENDAR) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + else if (content_type == TYPE_MEMO) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + else if (content_type == TYPE_CALLLOG) + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + + snprintf(datastore_dbsynced, sizeof(datastore_dbsynced), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_DBSYNCED); + snprintf(datastore_lastsessiontime, sizeof(datastore_lastsessiontime), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_LAST_TIME); + snprintf(datastore_s2c_total, sizeof(datastore_s2c_total), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_TOTAL); + snprintf(datastore_s2c_add, sizeof(datastore_s2c_add), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFADD); + snprintf(datastore_s2c_delete, sizeof(datastore_s2c_delete), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFDELETE); + snprintf(datastore_s2c_replace, sizeof(datastore_s2c_replace), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_SERVER, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFREPLACE); + + snprintf(datastore_c2s_total, sizeof(datastore_c2s_total), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_TOTAL); + snprintf(datastore_c2s_add, sizeof(datastore_c2s_add), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFADD); + snprintf(datastore_c2s_delete, sizeof(datastore_c2s_delete), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFDELETE); + snprintf(datastore_c2s_replace, sizeof(datastore_c2s_replace), "%s_%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_CLIENT, DEFINE_CONFIG_KEY_PROFILE_STATISTICS_NROFREPLACE); + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + da_err = sync_agent_get_config_list(account_id, &config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + for (iter = config_list; iter != NULL; iter = g_list_next(iter)) { + config_data = (sync_agent_da_config_s *) iter->data; + + if (config_data != NULL) { + if (config_data->key != NULL) { + if (strcmp(config_data->key, datastore_dbsynced) == 0) { + if (config_data->value != NULL) { + *db_synced = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_lastsessiontime) == 0) { + if (config_data->value != NULL) { + lastSessionTime_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_s2c_total) == 0) { + if (config_data->value != NULL) { + server2Client_Total_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_s2c_add) == 0) { + if (config_data->value != NULL) { + server2Client_NrOfAdd_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_s2c_delete) == 0) { + if (config_data->value != NULL) { + server2Client_NrOfDelete_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_s2c_replace) == 0) { + if (config_data->value != NULL) { + server2Client_NrOfReplace_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_c2s_total) == 0) { + if (config_data->value != NULL) { + client2Server_Total_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_c2s_add) == 0) { + if (config_data->value != NULL) { + client2Server_NrOfAdd_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_c2s_delete) == 0) { + if (config_data->value != NULL) { + client2Server_NrOfDelete_str = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_c2s_replace) == 0) { + if (config_data->value != NULL) { + client2Server_NrOfReplace_str = strdup(config_data->value); + continue; + } + } + } + } + } + + if (lastSessionTime_str != NULL) + *last_session_time = atoi(lastSessionTime_str); + + if (server2Client_Total_str != NULL) + *server2client_total = atoi(server2Client_Total_str); + + if (server2Client_NrOfAdd_str != NULL) + *server2client_nrofadd = atoi(server2Client_NrOfAdd_str); + + if (server2Client_NrOfDelete_str != NULL) + *server2client_nrofdelete = atoi(server2Client_NrOfDelete_str); + + if (server2Client_NrOfReplace_str != NULL) + *server2client_nrofreplace = atoi(server2Client_NrOfReplace_str); + + if (client2Server_Total_str != NULL) + *client2server_total = atoi(client2Server_Total_str); + + if (client2Server_NrOfAdd_str != NULL) + *client2server_nrofadd = atoi(client2Server_NrOfAdd_str); + + if (client2Server_NrOfDelete_str != NULL) + *client2server_nrofdelete = atoi(client2Server_NrOfDelete_str); + + if (client2Server_NrOfReplace_str != NULL) + *client2server_nrofreplace = atoi(client2Server_NrOfReplace_str); + + error: + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + if (lastSessionTime_str != NULL) + free(lastSessionTime_str); + + if (server2Client_Total_str != NULL) + free(server2Client_Total_str); + + if (server2Client_NrOfAdd_str != NULL) + free(server2Client_NrOfAdd_str); + + if (server2Client_NrOfDelete_str != NULL) + free(server2Client_NrOfDelete_str); + + if (server2Client_NrOfReplace_str != NULL) + free(server2Client_NrOfReplace_str); + + if (client2Server_Total_str != NULL) + free(client2Server_Total_str); + + if (client2Server_NrOfAdd_str != NULL) + free(client2Server_NrOfAdd_str); + + if (client2Server_NrOfDelete_str != NULL) + free(client2Server_NrOfDelete_str); + + if (client2Server_NrOfReplace_str != NULL) + free(client2Server_NrOfReplace_str); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +bool get_all_profiles_data(GList ** list) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + + GList *account_info_list = NULL; + GList *account_iter = NULL; + sync_agent_fw_account_s *fw_account = NULL; + + GList *config_list = NULL; + GList *iter = NULL; + sync_agent_da_config_s *config_data = NULL; + + sync_agent_ds_profile_info *profile_info = NULL; + GList *account_list = NULL; + GList *free_iter = NULL; + + sync_agent_ds_service_info *category_info = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_ACCESS_NAME; + query.access_name = "SE"; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto error; + } + + for (account_iter = account_info_list; account_iter != NULL; account_iter = g_list_next(account_iter)) { + fw_account = (sync_agent_fw_account_s *) account_iter->data; + + profile_info = (sync_agent_ds_profile_info *) calloc(1, sizeof(sync_agent_ds_profile_info)); + if (profile_info == NULL) { + _DEBUG_ERROR("profile_info is NULL"); + goto error; + } + + sync_agent_ds_server_info *server_info = &profile_info->server_info; + sync_agent_ds_sync_info *sync_info = &profile_info->sync_info; + + if (fw_account->email != NULL) + server_info->id = strdup(fw_account->email); + + if (fw_account->password != NULL) + server_info->password = strdup(fw_account->password); + + da_err = sync_agent_get_config_list(fw_account->account_id, &config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + for (iter = config_list; iter != NULL; iter = g_list_next(iter)) { + config_data = (sync_agent_da_config_s *) iter->data; + + if (config_data != NULL) { + if (config_data->key != NULL) { + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME) == 0) { + if (config_data->value != NULL) { + profile_info->profile_dir_name = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_NAME) == 0) { + if (config_data->value != NULL) { + profile_info->profile_name = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP) == 0) { + if (config_data->value != NULL) { + server_info->addr = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SYNC_MODE) == 0) { + if (config_data->value != NULL) { + sync_info->sync_mode = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE) == 0) { + if (config_data->value != NULL) { + sync_info->sync_type = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SYNC_INTERVAL) == 0) { + if (config_data->value != NULL) { + sync_info->interval = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_STATUS) == 0) { + if (config_data->value != NULL) { + profile_info->last_sync_status = atoi(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_LAST_SESSION_TIME) == 0) { + if (config_data->value != NULL) { + profile_info->last_sync_time = atoi(config_data->value); + continue; + } + } + } + } + } + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + + category_info = (sync_agent_ds_service_info *) calloc(1, sizeof(sync_agent_ds_service_info)); + if (category_info == NULL) { + _DEBUG_ERROR("category_info is NULL"); + goto error; + } + + char *datastore = NULL; + + char datastore_source[128]; + char datastore_target[128]; + char datastore_id[128]; + char datastore_pw[128]; + + if (content_type == TYPE_CONTACT) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + category_info->service_type = SYNC_AGENT_CONTACT; + } else if (content_type == TYPE_CALENDAR) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + category_info->service_type = SYNC_AGENT_CALENDAR; + } else if (content_type == TYPE_MEMO) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + category_info->service_type = SYNC_AGENT_MEMO; + } else if (content_type == TYPE_CALLLOG) { + datastore = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + category_info->service_type = SYNC_AGENT_CALLLOG; + } + + snprintf(datastore_source, sizeof(datastore_source), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + snprintf(datastore_target, sizeof(datastore_target), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET); + snprintf(datastore_id, sizeof(datastore_id), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID); + snprintf(datastore_pw, sizeof(datastore_pw), "%s_%s", datastore, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD); + + for (iter = config_list; iter != NULL; iter = g_list_next(iter)) { + config_data = (sync_agent_da_config_s *) iter->data; + + if (config_data != NULL) { + if (config_data->key != NULL) { + if (strcmp(config_data->key, datastore) == 0) { + if (config_data->value != NULL) { + category_info->enabled = atoi(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_source) == 0) { + if (config_data->value != NULL) { + category_info->src_uri = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_target) == 0) { + if (config_data->value != NULL) { + category_info->tgt_uri = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_id) == 0) { + if (config_data->value != NULL) { + category_info->id = strdup(config_data->value); + continue; + } + } + + if (strcmp(config_data->key, datastore_pw) == 0) { + if (config_data->value != NULL) { + category_info->password = strdup(config_data->value); + continue; + } + } + } + } + } + + profile_info->service_list = g_list_append(profile_info->service_list, category_info); + category_info = NULL; + } + + sync_agent_free_config_list(config_list); + config_list = NULL; + + account_list = g_list_append(account_list, profile_info); + profile_info = NULL; + } + + *list = account_list; + account_list = NULL; + + error: + + for (free_iter = account_list; free_iter != NULL; free_iter = g_list_next(free_iter)) { + sync_agent_ds_free_profile_info((ds_profile_h) free_iter->data); + _DEBUG_INFO("free sync_agent_ds_profile_info"); + } + + if (profile_info != NULL) + free(profile_info); + + sync_agent_free_fw_account_list(account_info_list); + + sync_agent_free_config_list(config_list); + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} diff --git a/src/agent/service-engine/se_sync.c b/src/agent/service-engine/se_sync.c new file mode 100755 index 0000000..c946921 --- /dev/null +++ b/src/agent/service-engine/se_sync.c @@ -0,0 +1,3245 @@ +/* + * 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. + */ + +/** + * @SE_Sync.c + * @version 0.1 + * @brief This file is the source file of implementation of functions which process sync request, auto configure etc.. + */ + +#include <sys/time.h> + +#include <sync_agent.h> +#include <plugin/plugin_slp_device.h> + +#include "service-engine/se_sync.h" +#include "service-engine/se_storage.h" +#include "service-engine/se_common.h" +#include "service-engine/se_notification.h" +#include "service-adapter/sa_common_interface.h" +#include "common/common_util.h" +#include "common/common_define_internal.h" + +#ifndef OMADS_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DS_SE" +#endif + +static se_error_type_e _session_process(int account_id, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error); +static se_error_type_e __process_update(int account_id, alert_type_e server_sync_type, sync_progress_status_e progress_status, operation_type_e operation_type, int content_type, bool is_from_server, bool need_to_save, sync_result_s * sync_result); +static se_error_type_e _write_sync_data(int account_id, alert_type_e alert_type, sync_session_result_e sync_session_result, int last_session_time, int only_from_client); +static se_error_type_e ___write_sync_resource_info(int account_id, int content_type, int last_session_time, int only_from_client, sync_result_s * client_sync_result, sync_result_s * server_sync_result); + +static se_error_type_e __init_datastore_info_array(int account_id); +static se_error_type_e ___set_datastore_info_array(int account_id, char *config_key, service_type_e content_type); +static se_error_type_e ___generate_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore); +static se_error_type_e __init_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore); +static se_error_type_e ___set_datastore_config(int account_id, int content_type, datastore_s ** datastore); +static se_error_type_e __get_sync_type(int account_id, alert_type_e * sync_type); + +static se_error_type_e __on_synchronising_account(int account_id); +static se_error_type_e _off_synchronising_account(int account_id); + +static se_error_type_e __on_resume_flag(int account_id); +static se_error_type_e _off_resume_flag(int account_id); + +static se_error_type_e _assemble_changed_datastores(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj); +static se_error_type_e _prepare_pre_sync(int account_id, char *sync_mode, san_package_s * san_package, alert_type_e * sync_type); +static se_error_type_e __set_config_based_on_sync_mode(int account_id, char *sync_mode, san_package_s * san_package); +static se_error_type_e _execute_pre_sync(int account_id, int session_time, pre_sync_return_obj_s * pre_sync_return_obj, alert_type_e * server_sync_type); +static se_error_type_e __execute_pre_sync_set_server_id(int account_id, char *dev_id); +static se_error_type_e __execute_pre_sync_datastore(int account_id, int session_time, GList * datastore_info, alert_type_e * server_sync_type); +static se_error_type_e _execute_sync(int account_id, alert_type_e client_sync_type, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj); +static se_error_type_e __execute_sync_arrange_changelog(int account_id, alert_type_e sync_type, GList * status); +static se_error_type_e __execute_sync_status(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj); +static se_error_type_e __execute_sync_change(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj); +static se_error_type_e _update_sync_result(int account_id); + +static command_result_e ___convert_return_status(sync_agent_da_return_e da_err); +static char *__convert_cttype_str(int itemTypeId); +static int ___convert_sync_type_value(char *sync_type_str); +static char *__convert_sync_type_str(alert_type_e sync_type); +static char *___convert_sync_progress_status_str(sync_progress_status_e progress_status); +static char *___convert_operation_type_str(operation_type_e operation_type); +static char *_convert_sync_progress_str(sync_progress_e process); +static char *_convert_sync_error_str(sync_error_e error); +static se_error_type_e _check_low_battery(); +static se_error_type_e _open_services(); +static se_error_type_e _close_services(); + +static inline long myclock() +{ + struct timeval tv; + gettimeofday(&tv, 0); + return (tv.tv_sec * 1000 + tv.tv_usec / 1000); +} + +static se_error_type_e _session_process(int account_id, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error) +{ + _INNER_FUNC_ENTER; + + char *profileDirName = NULL; + se_error_type_e err = SE_INTERNAL_OK; + bool result; + + result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + if (profileDirName != NULL) { + err = session_process(profileDirName, server_sync_type, process, error); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed to send noti"); + goto error; + } + } + + error: + if (profileDirName != NULL) + free(profileDirName); + + _INNER_FUNC_EXIT; + + return err; +} + +static se_error_type_e __process_update(int account_id, alert_type_e server_sync_type, sync_progress_status_e progress_status, operation_type_e operation_type, int content_type, bool is_from_server, bool need_to_save, sync_result_s * sync_result) +{ + _INNER_FUNC_ENTER; + + _DEBUG_VERBOSE("account_id =%d, progress_status = %d, operation_type = %d, content_type = %d, isFromServer = %d, needToSave = %d ", account_id, progress_status, operation_type, content_type, is_from_server, need_to_save); + _DEBUG_VERBOSE("numberOfChange = %d, received_count = %d, syncCount = %d", sync_result->number_of_change, sync_result->received_count, sync_result->add_count + sync_result->replace_count + sync_result->delete_count); + + se_error_type_e err = SE_INTERNAL_OK; + char *profileDirName = NULL; + char *uri = NULL; + char *sync_type = NULL; + char *operation = NULL; + char *progress = NULL; + + bool result; + result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + /* do not need to send update noti to ui */ + if (profileDirName == NULL) { + _DEBUG_VERBOSE("profileDirName is NULL"); + goto error; + } + + if (need_to_save == true) { + err = write_sync_statistics(account_id, content_type, is_from_server, sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in writeSyncStatistics"); + goto error; + } + } + + sync_type = __convert_sync_type_str(server_sync_type); + + progress = ___convert_sync_progress_status_str(progress_status); + + operation = ___convert_operation_type_str(operation_type); + + if (sync_type == NULL || progress == NULL || operation == NULL) { + err = SE_INTERNAL_NOT_DEFINED; + goto error; + } + + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->source != NULL) + uri = strdup(datastoreinfo_per_content_type[content_type]->source); + } + + err = send_noti_process_update(profileDirName, sync_type, uri, progress, operation, is_from_server, 0, 0, sync_result->number_of_change, sync_result->add_count + sync_result->replace_count + sync_result->delete_count); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in send_noti_process_update"); + goto error; + } + + error: + + if (profileDirName != NULL) + free(profileDirName); + + if (uri != NULL) + free(uri); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _write_sync_data(int account_id, alert_type_e alert_type, sync_session_result_e sync_session_result, int last_session_time, int only_from_client) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + err = write_profile_data(account_id, alert_type, sync_session_result, last_session_time, only_from_client); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in writeProfileData"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e ___write_sync_resource_info(int account_id, int content_type, int last_session_time, int only_from_client, sync_result_s * client_sync_result, sync_result_s * server_sync_result) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + err = write_sync_resource_info(account_id, content_type, last_session_time, only_from_client, client_sync_result, server_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in write_sync_resource_info"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __init_datastore_info_array(int account_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, TYPE_CONTACT); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in set_datastoreinfo_array"); + goto error; + } + + err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, TYPE_CALENDAR); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in set_datastoreinfo_array"); + goto error; + } + + err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, TYPE_MEMO); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in set_datastoreinfo_array"); + goto error; + } + + err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, TYPE_CALLLOG); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in set_datastoreinfo_array"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e ___set_datastore_info_array(int account_id, char *config_key, service_type_e content_type) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + datastore_s *datastore_info = NULL; + bool result; + char *value = NULL; + + result = get_config(account_id, config_key, &value); + if (result == true) { + if (strcmp(value, "1") == 0) { + err = ___generate_datastore_info(account_id, content_type, &datastore_info); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed to create or init datastore_info"); + datastoreinfo_per_content_type[content_type] = NULL; + goto error; + } + datastoreinfo_per_content_type[content_type] = datastore_info; + } + + } else + datastoreinfo_per_content_type[content_type] = NULL; + + error: + + if (value != NULL) + free(value); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e ___generate_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore) +{ + _INNER_FUNC_ENTER; + _DEBUG_VERBOSE("content_type=[%d]\n", content_type); + + se_error_type_e err = SE_INTERNAL_OK; + char *clientLastAnchor = NULL; + char *clientNextAnchor = NULL; + char *serverLastAnchor = NULL; + + sync_agent_da_return_e da_err = SYNC_AGENT_DA_ERRORS; + + sync_result_s *pSyncResult = NULL; + datastore_s *temp_datastore = NULL; + err = ___set_datastore_config(account_id, content_type, &temp_datastore); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in set_datastore_config"); + goto error; + } + + GList *anchor_list = NULL; + sync_agent_da_get_last_anchor_list_query_s query; + query.option = SYNC_AGENT_DA_GET_LAST_ANCHOR_LIST_OPTION_ITEM_TYPE_ID; + query.account_id = account_id; + query.item_type_id = temp_datastore->datastore_id; + + da_err = sync_agent_get_last_anchor_list(&query, &anchor_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_last_anchor_list !!"); + goto error; + } + + if (g_list_length(anchor_list) > 0) { + GList *last_anchor_info = g_list_nth(anchor_list, g_list_length(anchor_list) - 1); + clientLastAnchor = ((sync_agent_da_last_anchor_s *) (last_anchor_info->data))->last_anchor_client; + serverLastAnchor = ((sync_agent_da_last_anchor_s *) (last_anchor_info->data))->last_anchor_server; + } else { + sync_agent_da_last_anchor_s lastAnchor_daci; + + lastAnchor_daci.account_id = account_id; + lastAnchor_daci.data_store_id = temp_datastore->datastore_id; + lastAnchor_daci.last_anchor_server = NULL; + lastAnchor_daci.last_anchor_client = NULL; + lastAnchor_daci.access_name = "Engine"; + + da_err = sync_agent_add_last_anchor(&lastAnchor_daci); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("da_add_last_anchor_internal is failed %d", da_err); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + + clientNextAnchor = g_strdup_printf("%ld", time(NULL)); + + _DEBUG_VERBOSE("clientLastAnchor = %s\n", clientLastAnchor); + _DEBUG_VERBOSE("serverLastAnchor = %s\n", serverLastAnchor); + _DEBUG_VERBOSE("clientNextAnchor = %s\n", clientNextAnchor); + + /*slow sync if managed anchor info not exist.. (first sync) */ + if (clientLastAnchor == NULL) + set_datastore_client_anchor(temp_datastore, clientNextAnchor, clientNextAnchor); + else + set_datastore_client_anchor(temp_datastore, clientLastAnchor, clientNextAnchor); + + set_datastore_server_anchor(temp_datastore, serverLastAnchor, NULL); + + pSyncResult = create_sync_result(); + if (pSyncResult == NULL) { + _DEBUG_ERROR("create_syncResult is failed"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + temp_datastore->client_sync_result = pSyncResult; + + pSyncResult = create_sync_result(); + if (pSyncResult == NULL) { + _DEBUG_ERROR("create_syncResult is failed"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + temp_datastore->server_sync_result = pSyncResult; + + *datastore = temp_datastore; + temp_datastore = NULL; + + error: + + if (clientNextAnchor != NULL) + free(clientNextAnchor); + + if (temp_datastore != NULL) + free_datastore(temp_datastore); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __init_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + char *sourceDatastore = NULL; + char key[128]; + datastore_s *temp_datastore = NULL; + sync_result_s *pSyncResult = NULL; + + bool result; + switch (content_type) { + case TYPE_CONTACT: + snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + break; + case TYPE_CALENDAR: + snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + break; + case TYPE_MEMO: + snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + break; + case TYPE_CALLLOG: + snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + break; + default: + _DEBUG_VERBOSE("unknown content type = %d", content_type); + goto error; + } + + _DEBUG_ERROR("content_type = %d", content_type); + _DEBUG_ERROR("key = %s", key); + result = get_config(account_id, key, &sourceDatastore); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + temp_datastore = create_datastore(NULL, sourceDatastore); + if (temp_datastore == NULL) { + _DEBUG_ERROR("failed to create_Datastore"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + pSyncResult = create_sync_result(); + if (pSyncResult == NULL) { + _DEBUG_ERROR("create_syncResult is failed"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + temp_datastore->client_sync_result = pSyncResult; + + pSyncResult = create_sync_result(); + if (pSyncResult == NULL) { + _DEBUG_ERROR("create_syncResult is failed"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + temp_datastore->server_sync_result = pSyncResult; + + *datastore = temp_datastore; + temp_datastore = NULL; + + error: + if (sourceDatastore != NULL) + free(sourceDatastore); + + if (temp_datastore != NULL) + free_datastore(temp_datastore); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e ___set_datastore_config(int account_id, int content_type, datastore_s ** datastore) +{ + _INNER_FUNC_ENTER; + + datastore_s *temp_datastore = NULL; + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + + char *contentType = NULL; + + char sourceDatastore_key[128]; + char targetDatastore_key[128]; + char id_key[128]; + char pw_key[128]; + + char *sourceDatastore = NULL; + char *targetDatastore = NULL; + char *sync_type = NULL; + char *id = NULL; + char *pw = NULL; + + alert_type_e alert_type = ALERT_UNKNOWN; + + GList *config_list = NULL; + GList *iter = NULL; + sync_agent_da_config_s *config_data = NULL; + + int folder_type_id = 0; + switch (content_type) { + case TYPE_CONTACT: + { + folder_type_id = 0; + contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS; + } + break; + case TYPE_CALENDAR: + { + folder_type_id = 0; + contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR; + } + break; + case TYPE_MEMO: + { + folder_type_id = 0; + contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO; + } + break; + case TYPE_CALLLOG: + { + folder_type_id = 0; + contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG; + } + break; + default: + _DEBUG_VERBOSE("unknown content type = %d", content_type); + err = SE_INTERNAL_ERROR; + goto error; + } + + snprintf(sourceDatastore_key, sizeof(sourceDatastore_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE); + snprintf(targetDatastore_key, sizeof(targetDatastore_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET); + snprintf(id_key, sizeof(id_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID); + snprintf(pw_key, sizeof(id_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD); + + da_err = sync_agent_get_config_list(account_id, &config_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + for (iter = config_list; iter != NULL; iter = g_list_next(iter)) { + config_data = (sync_agent_da_config_s *) iter->data; + + if (config_data != NULL) { + if (config_data->key != NULL) { + if (strcmp(config_data->key, sourceDatastore_key) == 0) { + if (config_data->value != NULL) + sourceDatastore = strdup(config_data->value); + } + + if (strcmp(config_data->key, targetDatastore_key) == 0) { + if (config_data->value != NULL) + targetDatastore = strdup(config_data->value); + } + + if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE) == 0) { + if (config_data->value != NULL) + sync_type = strdup(config_data->value); + } + + if (strcmp(config_data->key, id_key) == 0) { + if (config_data->value != NULL) + id = strdup(config_data->value); + } + + if (strcmp(config_data->key, pw_key) == 0) { + if (config_data->value != NULL) + pw = strdup(config_data->value); + } + } + } + } + + if (sync_type != NULL) { + alert_type = ___convert_sync_type_value(sync_type); + } else { + _DEBUG_ERROR("sync_type is NULL !"); + err = SE_INTERNAL_ERROR; + goto error; + } + + _DEBUG_VERBOSE("get_Config result sourceDatastore= %s ", sourceDatastore); + _DEBUG_VERBOSE("get_Config result targetDatastore= %s ", targetDatastore); + _DEBUG_VERBOSE("get_Config result alert_type= %s ", sync_type); + _DEBUG_VERBOSE("get_Config result id= %s ", id); + _DEBUG_VERBOSE("get_Config result pw= %s ", pw); + + temp_datastore = create_datastore(targetDatastore, sourceDatastore); + if (temp_datastore == NULL) { + _DEBUG_ERROR("temp_datastore is NULL"); + err = SE_INTERNAL_ERROR; + goto error; + } + + set_datastore_content_type_info(temp_datastore, content_type, folder_type_id); + set_datastore_account_info(temp_datastore, id, pw); + set_datastore_client_sync_type(temp_datastore, alert_type); + + *datastore = temp_datastore; + temp_datastore = NULL; + + error: + + if (sourceDatastore != NULL) + free(sourceDatastore); + + if (targetDatastore != NULL) + free(targetDatastore); + + if (sync_type != NULL) + free(sync_type); + + if (id != NULL) + free(id); + + if (pw != NULL) + free(pw); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __get_sync_type(int account_id, alert_type_e * alert_type) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + char *sync_type = NULL; + + bool result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE, &sync_type); + if (result == false) { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + *alert_type = ___convert_sync_type_value(sync_type); + + error: + + if (sync_type != NULL) + free(sync_type); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __on_synchronising_account(int account_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "1", "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _off_synchronising_account(int account_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "0", "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __on_resume_flag(int account_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, "1", "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _off_resume_flag(int account_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, "0", "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _assemble_changed_datastores(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + changed_datastore_s *pChangedDatastore = NULL; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (!datastoreinfo_per_content_type[content_type]->client_sync_type) + continue; + + if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_SLOW_SYNC + || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_CLIENT || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_CLIENT_BY_SERVER) { + + if (content_type == TYPE_CALLLOG) { + _DEBUG_TRACE("sync_agent_refresh_item_tbl_from_service for call log"); + sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->datastore_id); + } + + GList *item_list = NULL; + sync_agent_da_get_item_list_query_s query; + query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID; + query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id; + + da_err = sync_agent_get_item_list(account_id, &query, &item_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_item_list !!"); + } + + pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, g_list_length(item_list)); + + if (pChangedDatastore == NULL) { + sync_agent_free_item_list(item_list); + _DEBUG_ERROR("failed in create_ChangedDatastore"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + if (g_list_length(item_list) > 0) { + GList *iter = NULL; + sync_agent_da_item_s *iter_data; + for (iter = item_list; iter != NULL; iter = g_list_next(iter)) { + iter_data = NULL; + iter_data = (sync_agent_da_item_s *) (iter->data); + if (iter_data != NULL) { + char *cttype = __convert_cttype_str(iter_data->data_store_id); + changed_item_s *pChanged = create_changed_item(CHANGE_ADD, iter_data->item_id); + if (pChanged == NULL) { + sync_agent_free_item_list(item_list); + _DEBUG_ERROR("failed in create_ChangedDatastore"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + set_changed_item_content_type(pChanged, cttype); + set_changed_item_index_of_datastore(pChanged, content_type); + + add_changed_datastore_changed_item(pChangedDatastore, pChanged); + } else { + _DEBUG_ERROR("iter_data is NULL !!"); + sync_agent_free_item_list(item_list); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + } + + sync_agent_free_item_list(item_list); + } + + GList *item_id_list = NULL; + + sync_agent_da_get_item_id_list_query_s item_id_query = {0,}; + item_id_query.option = SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID_N_ITEM_TYPE_ID; + item_id_query.account_id = account_id; + item_id_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id; + item_id_query.operation_id = SYNC_AGENT_DA_CHANGE_OPERATION_DELETE; + + da_err = sync_agent_get_item_id_list(&item_id_query, &item_id_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_item_id_list !!"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + GList *item_id_iter = NULL; + char *item_id; + + if (g_list_length(item_id_list) > 0) { + for (item_id_iter = item_id_list; item_id_iter != NULL; item_id_iter = g_list_next(item_id_iter)) { + item_id = NULL; + item_id = (char *)(item_id_iter->data); + + da_err = sync_agent_delete_item(item_id, 1); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_delete_item !!"); + sync_agent_free_item_id_list(item_id_list); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + } + + sync_agent_free_item_id_list(item_id_list); + + /*delete changelog data by account id and datastore id */ + sync_agent_da_delete_changelog_query_s delete_ch_query = {0,}; + delete_ch_query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_TYPE_ID; + delete_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id; + + da_err = sync_agent_delete_changelog(account_id, &delete_ch_query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_delete_changelog !!"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } else if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_TWO_WAY + || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_TWO_WAY_BY_SERVER + || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_CLIENT || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) { + _DEBUG_TRACE("server sync type is ALERT_TYPE [%d]", datastoreinfo_per_content_type[content_type]->server_sync_type); + + /*on resume flag into agent DB */ + err = __on_resume_flag(account_id); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __on_resume_flag"); + goto error; + } + + if (content_type == TYPE_CALLLOG) { + _DEBUG_TRACE("sync_agent_construct_item_tbl_from_service for call log"); + sync_agent_construct_item_tbl_from_service(account_id, content_type); + } + + /*from changelog */ + GList *item_info_list = NULL; + sync_agent_da_get_changelog_list_query_s get_ch_query; + get_ch_query.option = SYNC_AGENT_DA_GET_CHANGELOG_LIST_OPTION_ITEM_TYPE_ID; + get_ch_query.account_id = account_id; + get_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id; + + da_err = sync_agent_get_changelog_list(&get_ch_query, &item_info_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_changelog_list !!"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, g_list_length(item_info_list)); + + if (pChangedDatastore == NULL) { + sync_agent_free_changelog_list(item_info_list); + _DEBUG_ERROR("failed in create_ChangedDatastore"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + /*gathering changelog info */ + if (g_list_length(item_info_list) > 0) { + char *cttype = __convert_cttype_str(datastoreinfo_per_content_type[content_type]->datastore_id); + + GList *item_info_iter = NULL; + sync_agent_da_item_changelog_s *iter_data; + + for (item_info_iter = item_info_list; item_info_iter != NULL; item_info_iter = g_list_next(item_info_iter)) { + iter_data = NULL; + iter_data = (sync_agent_da_item_changelog_s *) (item_info_iter->data); + + if (iter_data != NULL) { + changed_item_s *pChanged = create_changed_item(iter_data->operation_id - 300, iter_data->item_id); + if (pChanged == NULL) { + sync_agent_free_changelog_list(item_info_list); + _DEBUG_ERROR("failed in create_ChangedDatastore"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + set_changed_item_content_type(pChanged, cttype); + set_changed_item_index_of_datastore(pChanged, content_type); + add_changed_datastore_changed_item(pChangedDatastore, pChanged); + } else { + sync_agent_free_changelog_list(item_info_list); + _DEBUG_ERROR("iter_data is NULL !!"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + } + + int *itemTypeIdList = (int *)calloc(1, sizeof(int)); + if (itemTypeIdList == NULL) { + _DEBUG_ERROR("failed in da_set_item_changelog_wait_status_internal"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + itemTypeIdList[0] = datastoreinfo_per_content_type[content_type]->datastore_id; + + sync_agent_da_update_changelog_query_s query; + query.option = SYNC_AGENT_DA_UPDATE_CHANGELOG_OPTION_WAIT_STATUS; + query.folder_id_list = NULL; + query.folder_id_count = 0; + query.item_type_id_list = itemTypeIdList; + query.item_type_id_count = 1; + + da_err = sync_agent_update_changelog(account_id, &query); + + sync_agent_free_changelog_list(item_info_list); + + if (itemTypeIdList != NULL) + free(itemTypeIdList); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_update_changelog !!"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + } else if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER + || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER_BY_SERVER + || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER_BY_SERVER) { + + if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER) { + /*on resume flag into agent DB */ + err = __on_resume_flag(account_id); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __on_resume_flag"); + goto error; + } + } + + /*MUST create empty changedDatastore.. */ + pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, 0); + if (pChangedDatastore == NULL) { + _DEBUG_ERROR("pChangedDatastore is NULL"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER_BY_SERVER) { + + /*_DEBUG_TRACE("sync_agent_refresh_item_tbl_from_service"); + sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->plugin_type);*/ + + /* Delete All item (include changelog), before adapting server item data… */ + _DEBUG_TRACE("sync_agent_begin_service = %d", datastoreinfo_per_content_type[content_type]->datastore_id); + sync_agent_begin_service(datastoreinfo_per_content_type[content_type]->datastore_id); + sync_agent_begin_transaction(); + + /*delete service item data */ + sync_agent_da_delete_service_item_query_s query; + query.content_type = datastoreinfo_per_content_type[content_type]->datastore_id; + query.account_id = account_id; + + da_err = sync_agent_query_delete_service_items(&query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + + sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0); + sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK); + + _DEBUG_ERROR("failed in sync_agent_query_delete_service_items()"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + _DEBUG_TRACE("da_get_item_by_account_id_internal"); + /*get all item by account id */ + GList *item_list = NULL; + sync_agent_da_get_item_list_query_s get_item_query; + get_item_query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID; + get_item_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id; + + da_err = sync_agent_get_item_list(account_id, &get_item_query, &item_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_item_list !!"); + } + _DEBUG_TRACE("item_cnt = %d", g_list_length(item_list)); + + GList *item_iter = NULL; + sync_agent_da_item_s *item_iter_data; + for (item_iter = item_list; item_iter != NULL; item_iter = g_list_next(item_iter)) { + item_iter_data = NULL; + item_iter_data = (sync_agent_da_item_s *) (item_iter->data); + + GList *mapping_list = NULL; + sync_agent_da_get_item_list_query_s mapping_query; + mapping_query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING; + mapping_query.item_id = item_iter_data->item_id; + + da_err = sync_agent_get_item_list(account_id, &mapping_query, &mapping_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_item_list !!"); + } + + _DEBUG_TRACE("acc_cnt = %d", g_list_length(mapping_list)); + + GList *mapping_iter = NULL; + sync_agent_da_item_s *mapping_iter_data; + + for (mapping_iter = mapping_list; mapping_iter != NULL; mapping_iter = g_list_next(mapping_iter)) { + mapping_iter_data = NULL; + mapping_iter_data = (sync_agent_da_item_s *) (mapping_iter->data); + da_err = sync_agent_delete_item(mapping_iter_data->item_id, 1); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_delete_item !!"); + } + } + + sync_agent_free_item_list(mapping_list); + } + + /*delete changelog data by account id and datastore id */ + _DEBUG_TRACE("da_delete_item_changelog_by_item_type_id_internal = %d", datastoreinfo_per_content_type[content_type]->datastore_id); + + sync_agent_da_delete_changelog_query_s delete_ch_query; + delete_ch_query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_TYPE_ID; + delete_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id; + + da_err = sync_agent_delete_changelog(account_id, &delete_ch_query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + + sync_agent_free_item_list(item_list); + + sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0); + sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK); + + _DEBUG_ERROR("failed in sync_agent_delete_changelog !!"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + /*delete item from item_tbl */ + _DEBUG_TRACE("da_delete_item_by_item_type_id_internal = %d", datastoreinfo_per_content_type[content_type]->datastore_id); + sync_agent_da_delete_item_query_s delete_item_query; + delete_item_query.option = SYNC_AGENT_DA_DELETE_ITEM_OPTION_ITEM_TYPE_ID; + delete_item_query.account_id = account_id; + delete_item_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id; + + da_err = sync_agent_query_delete_item(&delete_item_query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + sync_agent_free_item_list(item_list); + + sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0); + sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK); + + _DEBUG_ERROR("failed in sync_agent_query_delete_item !!"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + _DEBUG_TRACE("sync_agent_end_service = %d", datastoreinfo_per_content_type[content_type]->datastore_id); + sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 1); + sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT); + sync_agent_free_item_list(item_list); + } + } + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + (*sync_obj)->changed_datastore = g_list_append((*sync_obj)->changed_datastore, pChangedDatastore); + + /* for prevent */ + if (pChangedDatastore != NULL) { + set_number_of_change(datastoreinfo_per_content_type[content_type]->client_sync_result, pChangedDatastore->number_of_changes); + } else { + _DEBUG_ERROR("pChangedDatastore is NULL !"); + } + + operation_type_e operation_type; + if (datastoreinfo_per_content_type[content_type]->client_sync_result->number_of_change == 0) + operation_type = OPERATION_NOOP; + else + operation_type = OPERATION_ADD; + err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, content_type, false, false, datastoreinfo_per_content_type[content_type]->client_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("Failed in process_update"); + goto error; + } + } + } + } + + _INNER_FUNC_EXIT; + return err; + + error: + + if (pChangedDatastore != NULL) { + free_changed_datastore(pChangedDatastore); + pChangedDatastore = NULL; + } + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _prepare_pre_sync(int account_id, char *sync_mode, san_package_s * san_package, alert_type_e * sync_type) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + /*set synchronising flag into agent DB */ + err = __on_synchronising_account(account_id); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __on_synchronising_account"); + goto error; + } + + /*get sync type */ + err = __get_sync_type(account_id, sync_type); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __get_sync_type"); + goto error; + } + + /*init datastore_info_array */ + err = __init_datastore_info_array(account_id); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __init_datastore_info_array"); + goto error; + } + + /*set config based on sync mode */ + err = __set_config_based_on_sync_mode(account_id, sync_mode, san_package); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __set_config_based_on_sync_mode"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __set_config_based_on_sync_mode(int account_id, char *sync_mode, san_package_s * san_package) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + int content_type; + + if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) { + if (san_package != NULL) { + int count = san_package->cnt_sync_alerts; + _DEBUG_TRACE("count = %d", count); + int i; + for (i = 0; i < count; i++) { + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + + if (datastoreinfo_per_content_type[content_type] != NULL) { + _DEBUG_TRACE("datastoreinfo_per_content_type[content_type]->target = %s", datastoreinfo_per_content_type[content_type]->target); + if (datastoreinfo_per_content_type[content_type]->target == NULL) + continue; + + if (strcmp(san_package->sync_alerts[i].server_uri, datastoreinfo_per_content_type[content_type]->target) == 0) { + datastoreinfo_per_content_type[content_type]->client_sync_type = san_package->sync_alerts[i].sync_type; + _DEBUG_TRACE("san_package->syncAlerts[i].sync_type = %d", san_package->sync_alerts[i].sync_type); + } else + datastoreinfo_per_content_type[content_type]->client_sync_type = ALERT_UNKNOWN; + } + } + } + } + } + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _execute_pre_sync(int account_id, int session_time, pre_sync_return_obj_s * pre_sync_return_obj, alert_type_e * server_sync_type) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + /*if there is no alert command from server check it and goto fail_part */ + if (g_list_length(pre_sync_return_obj->datastore_info) == 0) { + _DEBUG_TRACE("alert command list from server is empty"); + err = SE_INTERNAL_MISCONFIGURATION; + goto error; + } + + err = __execute_pre_sync_set_server_id(account_id, pre_sync_return_obj->dev_id); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __set_server_id"); + goto error; + } + + err = __execute_pre_sync_datastore(account_id, session_time, pre_sync_return_obj->datastore_info, server_sync_type); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __process_datastore"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; + +} + +static se_error_type_e __execute_pre_sync_set_server_id(int account_id, char *dev_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + bool result; + char *value = NULL; + + if (dev_id != NULL) { + result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &value); + if (result == true) { + if (value != NULL) { + if (strcmp(value, dev_id) == 0) { + /* OK */ + } else { + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, dev_id, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + } else { + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, dev_id, "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + } else { + _DEBUG_ERROR("failed in get_Config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + error: + + if (value != NULL) { + free(value); + value = NULL; + } + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __execute_pre_sync_datastore(int account_id, int session_time, GList * datastore_info, alert_type_e * server_sync_type) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + int result; + + bool changeSyncType = false; + alert_type_e sync_type = ALERT_UNKNOWN; + + int content_type; + GList *serverDatastore_iter = NULL; + datastore_info_s *serverDatastoreInfo = NULL; + bool existInServerDatastore; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + existInServerDatastore = false; + + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->client_sync_type = %d", content_type, datastoreinfo_per_content_type[content_type]->client_sync_type); + + for (serverDatastore_iter = datastore_info; serverDatastore_iter != NULL; serverDatastore_iter = g_list_next(serverDatastore_iter)) { + serverDatastoreInfo = serverDatastore_iter->data; + + if (strcmp(serverDatastoreInfo->source, datastoreinfo_per_content_type[content_type]->target) == 0) { + existInServerDatastore = true; + + if (serverDatastoreInfo->next_anchor != NULL) + datastoreinfo_per_content_type[content_type]->next_anchor_server = strdup(serverDatastoreInfo->next_anchor); + + datastoreinfo_per_content_type[content_type]->server_sync_type = serverDatastoreInfo->sync_type; + + if (datastoreinfo_per_content_type[content_type]->client_sync_type != datastoreinfo_per_content_type[content_type]->server_sync_type) { + changeSyncType = true; + sync_type = datastoreinfo_per_content_type[content_type]->server_sync_type; + } + + _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->server_sync_type = %d", content_type, datastoreinfo_per_content_type[content_type]->server_sync_type); + + if (datastoreinfo_per_content_type[content_type]->last_anchor_server) { + /*if (strcmp(datastoreinfo_per_content_type[content_type]->lastAnchorServer, serverDatastoreInfo->lastAnchor) != 0) { + free(datastoreinfo_per_content_type[content_type]->lastAnchorServer); + datastoreinfo_per_content_type[content_type]->lastAnchorServer = NULL; + datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_SLOW_SYNC; + } */ + } else { + /* When first synchronize, lastAnchorServer is NULL... + if (datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_SLOW_SYNC + && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_CLIENT + && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_CLIENT_BY_SERVER + && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_SERVER + && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_SERVER_BY_SERVER) { + _DEBUG_VERBOSE("sync_agent_refresh_item_tbl_from_service"); + sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->plugin_type); + } */ + } + break; + } + } + + if (!existInServerDatastore) { + /* datastore config is wrong this datastore is not going to contain sync process from now */ + err = __process_update(account_id, ALERT_UNKNOWN, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, false, true, datastoreinfo_per_content_type[content_type]->client_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed to process_update"); + goto error; + } + + err = __process_update(account_id, ALERT_UNKNOWN, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, true, true, datastoreinfo_per_content_type[content_type]->server_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed to process_update"); + goto error; + } + + /* set fail into result (both client and server) */ + if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL) + datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_FAILED; + if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL) + datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_FAILED; + + err = ___write_sync_resource_info(account_id, content_type, session_time, 0, datastoreinfo_per_content_type[content_type]->client_sync_result, datastoreinfo_per_content_type[content_type]->server_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed to write_SyncResourceInfo"); + goto error; + } + + free_datastore(datastoreinfo_per_content_type[content_type]); + datastoreinfo_per_content_type[content_type] = NULL; + + datastore_s *datastore = NULL; + err = __init_datastore_info(account_id, content_type, &datastore); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed to init_datastore_info"); + goto error; + } + + datastoreinfo_per_content_type[content_type] = datastore; + } + } + } + } + + if (changeSyncType) { + *server_sync_type = sync_type; + result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_SYNC_TYPE, __convert_sync_type_str(sync_type), "string", "SE"); + if (result == false) { + _DEBUG_ERROR("failed in set_config"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _execute_sync(int account_id, alert_type_e client_sync_type, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + if (client_sync_type == ALERT_TWO_WAY || client_sync_type == ALERT_TWO_WAY_BY_SERVER || client_sync_type == ALERT_ONE_WAY_FROM_CLIENT || client_sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) { + + err = __execute_sync_arrange_changelog(account_id, client_sync_type, (*sync_return_obj)->status); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed to process_update"); + goto error; + } + } + + err = __execute_sync_status(account_id, server_sync_type, sync_obj, sync_return_obj); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __execute_sync_status"); + goto error; + } + + err = __execute_sync_change(account_id, server_sync_type, sync_obj, sync_return_obj); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __execute_sync_change"); + goto error; + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __execute_sync_arrange_changelog(int account_id, alert_type_e sync_type, GList * status) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_da_return_e da_err; + + int i = 0; + int count = 0; + int list_length = g_list_length(status); + char **itemIdList = (char **)calloc(list_length, sizeof(char *)); + if (itemIdList == NULL) { + _DEBUG_ERROR("Failed to alloc memory"); + err = SE_INTERNAL_NO_MEMORY; + goto error; + } + + GList *appliedStatus_iter = NULL; + applied_status_s *pAppliedStatus = NULL; + for (appliedStatus_iter = status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) { + pAppliedStatus = appliedStatus_iter->data; + + switch (pAppliedStatus->status) { + case 200: + case 201: + case 202: + case 203: + case 204: + case 205: + case 207: + case 208: + case 209: + case 210: + case 211: + { + /*clean up change log */ + if (pAppliedStatus->luid != NULL) + itemIdList[count++] = strdup(pAppliedStatus->luid); + + if (sync_type == ALERT_TWO_WAY || sync_type == ALERT_TWO_WAY_BY_SERVER || sync_type == ALERT_ONE_WAY_FROM_CLIENT || sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) { + + if (pAppliedStatus->change_type == CHANGE_DELETE) { + da_err = sync_agent_delete_item(pAppliedStatus->luid, 0); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in da_delete_item_by_item_id_internal"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + } + break; + } + case 206: + case 420: + break; + case 400: + case 500: + { + sync_agent_da_item_changelog_s *get_changelog = NULL; + da_err = sync_agent_create_changelog(&get_changelog); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_changelog !!"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + da_err = sync_agent_get_changelog(account_id, pAppliedStatus->luid, &get_changelog); + if (da_err == SYNC_AGENT_DA_ERR_NO_DATA) { + _DEBUG_VERBOSE("%s item does not exist in ChangeLog tbl", pAppliedStatus->luid); + sync_agent_free_changelog(get_changelog); + break; + } else if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_changelog !!"); + sync_agent_free_changelog(get_changelog); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + if (get_changelog->status != NULL) { + if (strcmp(get_changelog->status, "SYNC_ERROR") == 0) { + if (pAppliedStatus->luid != NULL) + itemIdList[count++] = strdup(pAppliedStatus->luid); + } else { + sync_agent_da_item_changelog_s *set_changelog = NULL; + + da_err = sync_agent_create_changelog(&set_changelog); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_changelog !!"); + sync_agent_free_changelog(get_changelog); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + set_changelog->item_id = strdup(pAppliedStatus->luid); + set_changelog->status = strdup("SYNC_ERROR"); + set_changelog->access_name = strdup("SE"); + + sync_agent_da_update_changelog_query_s update_ch_query; + update_ch_query.option = SYNC_AGENT_DA_UPDATE_CHANGELOG_OPTION_SYNC_STATUS; + update_ch_query.update_item = set_changelog; + + da_err = sync_agent_update_changelog(account_id, &update_ch_query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_update_changelog !!"); + sync_agent_free_changelog(get_changelog); + sync_agent_free_changelog(set_changelog); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_free_changelog(set_changelog); + } + } + + sync_agent_free_changelog(get_changelog); + break; + } + } + } + + if (list_length >= 1) { + _DEBUG_TRACE("before da_delete_item_changelog_by_item_id_list_internal"); + _DEBUG_TRACE("count = %d", count); + + sync_agent_da_delete_changelog_query_s query; + query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_ID_LIST; + query.item_id_list = itemIdList; + query.count = count; + + da_err = sync_agent_delete_changelog(account_id, &query); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_delete_changelog !!"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + } + + error: + + /*free itemIdList */ + if (itemIdList != NULL) { + for (i = 0; i < list_length; i++) { + if (itemIdList[i] != NULL) + free(itemIdList[i]); + } + free(itemIdList); + } + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __execute_sync_status(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + int content_type; + bool existEqualItem; + bool needToSave; + sync_result_s *pClientSyncResult; + GList *changedDatastore_iter = NULL; + changed_datastore_s *pSyncChangedDatastore = NULL; + for (changedDatastore_iter = (*sync_obj)->changed_datastore; changedDatastore_iter != NULL;) { + pSyncChangedDatastore = (changed_datastore_s *) changedDatastore_iter->data; + _DEBUG_VERBOSE("pChangedDatastore = %p", pSyncChangedDatastore); + + int datastoreContentType = 0; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (strcmp(pSyncChangedDatastore->source, datastoreinfo_per_content_type[content_type]->source) == 0) { + datastoreContentType = content_type; + break; + } + } + } + + GList *changedItem_iter = NULL; + changed_item_s *changedItem = NULL; + for (changedItem_iter = pSyncChangedDatastore->sent_item; changedItem_iter != NULL;) { + changedItem = changedItem_iter->data; + + existEqualItem = false; + GList *appliedStatus_iter = NULL; + applied_status_s *pAppliedStatus = NULL; + for (appliedStatus_iter = (*sync_return_obj)->status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) { + pAppliedStatus = appliedStatus_iter->data; + + if (strcmp(pAppliedStatus->luid, changedItem->luid) == 0) { + add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1); + + if (pAppliedStatus->status >= 200 && pAppliedStatus->status <= 210) { + switch (changedItem->change_type) { + case CHANGE_ADD: + add_add_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1); + break; + case CHANGE_REPLACE: + add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1); + break; + case CHANGE_DELETE: + add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1); + break; + default: + break; + } + } else { + /* if status is not success count it is failed */ + } + + (*sync_return_obj)->status = g_list_remove((*sync_return_obj)->status, pAppliedStatus); + free_applied_status(pAppliedStatus); + pAppliedStatus = NULL; + + changedItem_iter = g_list_next(changedItem_iter); + + pSyncChangedDatastore->sent_item = g_list_remove(pSyncChangedDatastore->sent_item, changedItem); + free_changed_item(changedItem); + changedItem = NULL; + + existEqualItem = true; + break; + } + } + if (existEqualItem == false) + changedItem_iter = g_list_next(changedItem_iter); + } + + changedDatastore_iter = g_list_next(changedDatastore_iter); + + needToSave = false; + pClientSyncResult = datastoreinfo_per_content_type[datastoreContentType]->client_sync_result; + if (pClientSyncResult->number_of_change == pClientSyncResult->received_count) { + needToSave = true; + + if (datastoreinfo_per_content_type[datastoreContentType]->client_sync_result != NULL) + datastoreinfo_per_content_type[datastoreContentType]->client_sync_result->session_result = SYNC_SESSION_SUCCEEDED; + + /*remove datastore from list and + free current ChangedDatastore it does not need anymore because all item in datastore has been sent and receive status */ + (*sync_obj)->changed_datastore = g_list_remove((*sync_obj)->changed_datastore, pSyncChangedDatastore); + free_changed_datastore(pSyncChangedDatastore); + pSyncChangedDatastore = NULL; + } + + operation_type_e operation_type; + if (datastoreinfo_per_content_type[datastoreContentType]->client_sync_result->number_of_change == 0) + operation_type = OPERATION_NOOP; + else + operation_type = OPERATION_ADD; + err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, false, needToSave, datastoreinfo_per_content_type[datastoreContentType]->client_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in process_update"); + goto error; + } + } + + error: + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e __execute_sync_change(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + sync_agent_da_service_item_s *service_item = NULL; + sync_agent_da_item_s *fw_item = NULL; + int content_type; + GList *f_id_list = NULL; + sync_result_s *tempServerSyncResult = NULL; + + /* process command that from server sended to client(sync, add, replace etc...) */ + changed_datastore_s *pSyncReturnChangedDatastore = NULL; + sending_status_s *sendingStatus = NULL; + GList *iter = NULL; + for (iter = (*sync_return_obj)->changed_datastore; iter != NULL;) { + pSyncReturnChangedDatastore = (changed_datastore_s *) iter->data; + + int datastoreContentType = 0; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (strcmp(pSyncReturnChangedDatastore->target, datastoreinfo_per_content_type[content_type]->source) == 0) { + datastoreContentType = content_type; + break; + } + } + } + + /*noti to UI numberOfChanges from server */ + if (pSyncReturnChangedDatastore->has_number_of_changes) { + set_number_of_change(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, pSyncReturnChangedDatastore->number_of_changes); + + operation_type_e operation_type; + if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->number_of_change == 0) + operation_type = OPERATION_NOOP; + else + operation_type = OPERATION_ADD; + err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, true, false, datastoreinfo_per_content_type[datastoreContentType]->server_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in process_update"); + goto error; + } + } + + sendingStatus = create_sending_status(pSyncReturnChangedDatastore->target, pSyncReturnChangedDatastore->source); + if (sendingStatus == NULL) { + err = SE_INTERNAL_NO_MEMORY; + _DEBUG_ERROR("failed to alloc memory"); + goto error; + } + + /*get folderId */ + char *folderId = NULL; + sync_agent_da_get_folder_id_list_query_s query; + query.option = SYNC_AGENT_DA_GET_FOLDER_ID_OPTION_FOLDER_TYPE_ID; + query.account_id = account_id; + query.item_type_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id; + query.folder_type_id = datastoreinfo_per_content_type[datastoreContentType]->folder_type_id; + + da_err = sync_agent_get_folder_id_list(&query, &f_id_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_get_folder_id_list_inferface() failed !!"); + goto error; + } + + if (g_list_length(f_id_list) > 0) { + GList *iter = g_list_nth(f_id_list, 0); + folderId = (char *)(iter->data); + } else { + _DEBUG_ERROR("failed to get folderId"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + int changeItemCount = g_list_length(pSyncReturnChangedDatastore->change_item); + if (changeItemCount > 0) { + /*begin transaction */ + _DEBUG_VERBOSE("sync_agent_begin_service with datastoreContentType = %d", datastoreContentType); +// sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id); + sync_agent_begin_transaction(); + + /*back up syncresult structure for rollback case */ + tempServerSyncResult = dup_sync_result(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result); + } + + bool needToSave = false; + sync_result_s *pServerSyncResult = NULL; + GList *changedIter = NULL; + changed_item_s *changedItem = NULL; + applied_status_s *appliedStatus = NULL; + + for (changedIter = pSyncReturnChangedDatastore->change_item; changedIter != NULL; changedIter = g_list_next(changedIter)) { + changedItem = (changedIter->data); + + _DEBUG_VERBOSE("changedItem->changeType : %d", changedItem->change_type); + _DEBUG_VERBOSE("changedItem->luid : %s", changedItem->luid); + _DEBUG_VERBOSE("changedItem->content_type : %s", changedItem->content_type); + _DEBUG_VERBOSE("changedItem->indexOfDatastore : %d", changedItem->index_of_datastore); + _DEBUG_VERBOSE("changedItem->data : %s", changedItem->data); + + add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1); + + bool da_fail = false; + command_result_e returnResult = COMMAND_RESULT_INIT; + switch (changedItem->change_type) { + case CHANGE_ADD: + { + /* add to service DB */ + if (changedItem->data != NULL) { + char *fw_id = NULL; + + da_err = sync_agent_create_service_item(&service_item); + if ((da_err != SYNC_AGENT_DA_SUCCESS) || (service_item == NULL)) { + _DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err); + returnResult = ___convert_return_status(da_err); + da_fail = true; + break; + } + + service_item->item_id = g_strdup(changedItem->luid); + service_item->content_type = datastoreinfo_per_content_type[datastoreContentType]->datastore_id; + service_item->account_id = account_id; + service_item->folder_id = g_strdup(folderId); + service_item->access_name = g_strdup("Engine"); + service_item->data = g_strdup(changedItem->data); + sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id); + + da_err = sync_agent_add_service_item(service_item, &fw_id, false); + + sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1); + + _DEBUG_VERBOSE("da_err = %d", da_err); + + if (da_err == SYNC_AGENT_DA_SUCCESS) { + int acc_cnt; + bool success = true; + char *service_id = sync_agent_get_service_item_id(fw_id); + + GList *folder_list = NULL; + sync_agent_da_get_folder_list_query_s query; + query.option = SYNC_AGENT_DA_GET_FOLDER_LIST_OPTION_SERVICE_ID_MAPPING; + query.account_id = account_id; + query.folder_id = folderId; + da_err = sync_agent_get_folder_list(&query, &folder_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_get_folder_list() failed !!"); + success = false; + } + + acc_cnt = g_list_length(folder_list); + + _DEBUG_VERBOSE("acc_cnt = %d", acc_cnt); + if (acc_cnt > 0) { + char **fw_item_id_list = sync_agent_generate_item_luid(1, acc_cnt); + char **id_list = (char **)calloc(acc_cnt, sizeof(char *)); + if (id_list == NULL) { + _DEBUG_ERROR("CALLOC failed !!!"); + success = false; + break; + } + + if (fw_item_id_list == NULL) { + _DEBUG_ERROR("failed in sync_agent_generate_item_luid"); + da_err = SYNC_AGENT_DA_ERRORS; + success = false; + } else { + int i = 0; + GList *iter = NULL; + sync_agent_da_folder_s *iter_data; + + for (iter = folder_list; iter != NULL; iter = g_list_next(iter)) { + iter_data = NULL; + iter_data = (sync_agent_da_folder_s *) (iter->data); + + da_err = sync_agent_create_item(&fw_item); + if ((da_err != SYNC_AGENT_DA_SUCCESS) || (fw_item == NULL)) { + _DEBUG_ERROR("failed in sync_agent_create_item() = %d", da_err); + success = false; + break; + } + + fw_item->item_id = g_strdup(fw_item_id_list[i]); + fw_item->data_store_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id; + fw_item->account_id = iter_data->account_id; + fw_item->folder_id = g_strdup(iter_data->folder_id); + fw_item->service_id = g_strdup(service_id); + fw_item->access_name = g_strdup("DACI_ChangeLog"); + + da_err = sync_agent_add_item(fw_item, &(id_list[i]), true); + if (fw_item != NULL) + sync_agent_free_item(fw_item); + free(fw_item_id_list[i]); + free(id_list[i]); + + i++; + + _DEBUG_VERBOSE("da_err = %d", da_err); + + if (da_err != SYNC_AGENT_DA_SUCCESS) { + success = false; + break; + } + } + if (fw_item_id_list != NULL) { + free(fw_item_id_list); + } + if (id_list != NULL) { + free(id_list); + } + } + } + + if (success == true) { + returnResult = COMMAND_RESULT_ADDED; + add_add_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1); + } else { + /*this case just fail do rollback and goto fail */ + returnResult = ___convert_return_status(da_err); + da_fail = true; + } + sync_agent_free_folder_list(folder_list); + + if (service_id != NULL) + free(service_id); + + } else if (da_err == SYNC_AGENT_DA_ERR_NOT_SUPPORTED || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT || da_err == SYNC_AGENT_DA_ERR_ALREADY_EXIST) { + /*just return error to server */ + returnResult = ___convert_return_status(da_err); + } else { + /*this case just fail do rollback and goto fail */ + returnResult = ___convert_return_status(da_err); + da_fail = true; + } + + if (service_item != NULL) + sync_agent_free_service_item(service_item); + + if (fw_id != NULL) + free(fw_id); + } else + returnResult = COMMAND_RESULT_COMMAND_FAIL; + + break; + } + case CHANGE_REPLACE: + { + if (changedItem->data != NULL) { + /* update from service DB */ + da_err = sync_agent_create_service_item(&service_item); + if (da_err != SYNC_AGENT_DA_SUCCESS || (service_item == NULL)) { + _DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err); + returnResult = ___convert_return_status(da_err); + da_fail = true; + break; + } + + service_item->item_id = g_strdup(changedItem->luid); + service_item->content_type = datastoreinfo_per_content_type[datastoreContentType]->datastore_id; + service_item->account_id = account_id; + service_item->folder_id = g_strdup(folderId); + service_item->access_name = g_strdup("Engine"); + service_item->data = (const void *)g_strdup(changedItem->data); + + sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id); + + da_err = sync_agent_update_service_item(service_item, changedItem->luid, false); + + sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1); + + _DEBUG_VERBOSE("da_err = %d", da_err); + if (da_err == SYNC_AGENT_DA_SUCCESS) { + + GList *item_list = NULL; + sync_agent_da_get_item_list_query_s query; + query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING; + query.item_id = changedItem->luid; + + da_err = sync_agent_get_item_list(account_id, &query, &item_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_item_list !!"); + } + + bool success = true; + GList *iter = NULL; + sync_agent_da_item_s *iter_data; + + for (iter = item_list; iter != NULL; iter = g_list_next(iter)) { + iter_data = NULL; + iter_data = (sync_agent_da_item_s *) (iter->data); + + da_err = sync_agent_update_item(iter_data->item_id, 1); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + success = false; + break; + } + } + + if (success == true) { + returnResult = COMMAND_RESULT_OK; + add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1); + } else { + /*this case just fail do rollback and goto fail */ + returnResult = ___convert_return_status(da_err); + da_fail = true; + } + + sync_agent_free_item_list(item_list); + } else if (da_err == SYNC_AGENT_DA_ERR_NOT_SUPPORTED || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT) { + /*just return error to server */ + returnResult = ___convert_return_status(da_err); + } else { + /*this case just fail do rollback and goto fail */ + returnResult = ___convert_return_status(da_err); + da_fail = true; + } + + if (service_item != NULL) + sync_agent_free_service_item(service_item); + } else + returnResult = COMMAND_RESULT_COMMAND_FAIL; + + break; + } + case CHANGE_DELETE: + { + /* delete from service DB */ + GList *item_list = NULL; + sync_agent_da_get_item_list_query_s query; + query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING; + query.item_id = changedItem->luid; + + da_err = sync_agent_get_item_list(account_id, &query, &item_list); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_item_list !!"); + } + + sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id); + + da_err = sync_agent_delete_service_item(changedItem->luid, false); + + sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1); + + _DEBUG_VERBOSE("[sync_agent_delete_service_item] result : %d", da_err); + if (da_err == SYNC_AGENT_DA_SUCCESS) { + bool success = true; + GList *iter = NULL; + sync_agent_da_item_s *iter_data; + + for (iter = item_list; iter != NULL; iter = g_list_next(iter)) { + iter_data = NULL; + iter_data = (sync_agent_da_item_s *) (iter->data); + + da_err = sync_agent_delete_item(iter_data->item_id, true); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + success = false; + break; + } + } + + if (success == true) { + returnResult = COMMAND_RESULT_DELETE_WITHOUT_ARCHIVE; + add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1); + } else { + /*this case just fail do rollback and goto fail */ + returnResult = ___convert_return_status(da_err); + da_fail = true; + } + } else if (da_err == SYNC_AGENT_DA_ERR_NO_DATA || da_err == SYNC_AGENT_DA_ERR_SUB_DATA_EXIST || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT) { + /*just return error to server */ + returnResult = ___convert_return_status(da_err); + } else { + /*this case just fail do rollback and goto fail */ + returnResult = ___convert_return_status(da_err); + da_fail = true; + } + sync_agent_free_item_list(item_list); + break; + } + default: + break; + } + + if (da_fail == true) { + /*replace syncresult when rollback happened */ + if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result != NULL) + free(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result); + + datastoreinfo_per_content_type[datastoreContentType]->server_sync_result = tempServerSyncResult; + tempServerSyncResult = NULL; + + _DEBUG_VERBOSE("Transaction_Rollback"); + _DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType); +// sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0); + sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK); + + err = SE_INTERNAL_DA_ERROR; + _DEBUG_ERROR("failed in DA"); + goto error; + } + + appliedStatus = create_applied_status(changedItem->luid, changedItem->change_type, returnResult); + if (appliedStatus == NULL) { + _DEBUG_VERBOSE("Transaction_Rollback"); + _DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType); +// sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0); + sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK); + + err = SE_INTERNAL_NO_MEMORY; + _DEBUG_ERROR("failed to alloc memory"); + goto error; + } + add_sending_status_applied_status(sendingStatus, appliedStatus); + } + + if (changeItemCount > 0) { + /*end transaction */ + _DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType); +// sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1); + sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT); + } + + /* Do not need to send update event to ui when changeItemCount less than 0 */ + /* send update event about add, delete, replace count during this sync command */ + if (changeItemCount >= 0) { + needToSave = false; + pServerSyncResult = datastoreinfo_per_content_type[datastoreContentType]->server_sync_result; + if (pServerSyncResult->number_of_change == pServerSyncResult->received_count) { + needToSave = true; + + if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result != NULL) + datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->session_result = SYNC_SESSION_SUCCEEDED; + } + + operation_type_e operation_type; + if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->number_of_change == 0) + operation_type = OPERATION_NOOP; + else + operation_type = OPERATION_ADD; + err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, true, needToSave, datastoreinfo_per_content_type[datastoreContentType]->server_sync_result); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in process_update"); + goto error; + } + } + + (*sync_obj)->sending_status = g_list_append((*sync_obj)->sending_status, sendingStatus); + sendingStatus = NULL; + + iter = g_list_next(iter); + + (*sync_return_obj)->changed_datastore = g_list_remove((*sync_return_obj)->changed_datastore, pSyncReturnChangedDatastore); + free_changed_datastore(pSyncReturnChangedDatastore); + pSyncReturnChangedDatastore = NULL; + + if (tempServerSyncResult != NULL) { + free(tempServerSyncResult); + tempServerSyncResult = NULL; + } + } + + error: + sync_agent_free_folder_id_list(f_id_list); + + if (tempServerSyncResult != NULL) + free(tempServerSyncResult); + + /* for prevent */ + free_sending_status(sendingStatus); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _update_sync_result(int account_id) +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + int content_type; + + sync_agent_da_last_anchor_s next_anchor_info; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (!datastoreinfo_per_content_type[content_type]->client_sync_type) + continue; + + next_anchor_info.access_name = "Engine"; + next_anchor_info.account_id = account_id; + next_anchor_info.data_store_id = datastoreinfo_per_content_type[content_type]->datastore_id; + next_anchor_info.last_anchor_client = datastoreinfo_per_content_type[content_type]->next_anchor_client; + next_anchor_info.last_anchor_server = datastoreinfo_per_content_type[content_type]->next_anchor_server; + + da_err = sync_agent_update_last_anchor(&next_anchor_info); + if (da_err != SYNC_AGENT_DA_SUCCESS) + _DEBUG_ERROR("failed in da_set_last_anchor_internal"); + + da_err = sync_agent_set_service_change_point(account_id, datastoreinfo_per_content_type[content_type]->datastore_id); + if (da_err != SYNC_AGENT_DA_SUCCESS) + _DEBUG_ERROR("failed in sync_agent_set_service_change_point"); + } + } + + _INNER_FUNC_EXIT; + return err; +} + +static command_result_e ___convert_return_status(sync_agent_da_return_e da_err) +{ + _INNER_FUNC_ENTER; + + command_result_e returnResult = COMMAND_RESULT_INIT; + switch (da_err) { + case 1: /*SYNC_AGENT_DA_SUCCESS */ + /*never comes this case + break; */ + case -813: /*SYNC_AGENT_DA_ERR_ALREADY_EXIST */ + returnResult = COMMAND_RESULT_ALREADY_EXIST; /*[A] Already exists exception */ + break; + case -815: /*SYNC_AGENT_DA_ERR_MEMORY_FULL *//* oma : 420 (device full exception) */ + returnResult = COMMAND_RESULT_DEVICE_FULL; /*[AR] Device full exception */ + break; + case -820: /*SYNC_AGENT_DA_ERR_NOT_SUPPORTED *//* oma : 415 (unsupported media type or format exception) */ + returnResult = COMMAND_RESULT_UNSUPPORTED_TYPE; /*[AR] Unsupported media type or format exception */ + break; + case -819: /*SYNC_AGENT_DA_ERR_NO_DATA *//* kies : not found, oma : 211 (item not deleted exception) */ + returnResult = COMMAND_RESULT_NOT_EXIST; /*[D] Item not deleted exception */ + break; + case -800: /*SYNC_AGENT_DA_ERRORS */ + case -801: /*SYNC_AGENT_DA_NOT_FOUND_PLUG_IN */ + case -802: /*SYNC_AGENT_DA_ERR_OPEN_FAILED */ + case -803: /*SYNC_AGENT_DA_ERR_CLOSE_FAILED */ + case -804: /*SYNC_AGENT_DA_ERR_TRANSACTION_FAILED */ + case -805: /*SYNC_AGENT_DA_ERR_CREATE_TABLE_FAILED */ + case -806: /*SYNC_AGENT_DA_ERR_DROP_TABLE_FAILED */ + case -807: /*SYNC_AGENT_DA_ERR_QUERY_FAILED */ + case -808: /*SYNC_AGENT_DA_ERR_NOT_OPENED */ + case -809: /*SYNC_AGENT_DA_ERR_ACCOUNT_FULL */ + case -810: /*SYNC_AGENT_DA_ERR_DELETE_LAST_ACCOUNT */ + case -811: /*SYNC_AGENT_DA_ERR_PRIMARY_KEY_NOT_UNIQUE */ + case -812: /*SYNC_AGENT_DA_ERR_DB_HANDLER_MGR */ + case -814: /*SYNC_AGENT_DA_ERR_INVALID_CONTENT */ + case -816: /*SYNC_AGENT_DA_ERR_SUB_DATA_EXIST *//* oma : 427 (item not empty) */ + case -817: /*SYNC_AGENT_DA_ERR_LOCKED *//* kies : cannot access */ + case -818: /*SYNC_AGENT_DA_ERR_MORE_DATA */ + case -821: /*SYNC_AGENT_DA_ERR_NOT_EXECUTE *//* kies : ex) sms send command, no network service */ + default: + returnResult = COMMAND_RESULT_COMMAND_FAIL; /*[ARD] Command failed exception */ + break; + } + + _INNER_FUNC_EXIT; + return returnResult; +} + +static char *__convert_cttype_str(int datastore_id) +{ + _INNER_FUNC_ENTER; + + char *cttype = NULL; + /*FIXME : check type and version (contact : vCard2.1 , calendar : vCalendar 1.0 , memo : plain text) of real item data.. */ + switch (datastore_id) { + case TYPE_CONTACT: /*contact (vCard2.1) */ + cttype = ELEMENT_TEXT_VCARD; + break; + case TYPE_CALENDAR: /*calendar (vCalendar2.0) */ + cttype = ELEMENT_TEXT_VCAL; + break; + case TYPE_MEMO: /*note(Memo) */ + cttype = ELEMENT_TEXT_PLAIN; + break; + case TYPE_CALLLOG: + cttype = ELEMENT_TEXT_XCALLLOG; + break; + default: + break; + } + + _INNER_FUNC_EXIT; + return cttype; +} + +static int ___convert_sync_type_value(char *sync_type_str) +{ + _INNER_FUNC_ENTER; + + int sync_type_value; + + if (strcmp(sync_type_str, DEFINE_ALERT_SLOW_SYNC_STR) == 0) + sync_type_value = ALERT_SLOW_SYNC; + else if (strcmp(sync_type_str, DEFINE_ALERT_TWO_WAY_STR) == 0) + sync_type_value = ALERT_TWO_WAY; + else if (strcmp(sync_type_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) == 0) + sync_type_value = ALERT_ONE_WAY_FROM_CLIENT; + else if (strcmp(sync_type_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) == 0) + sync_type_value = ALERT_ONE_WAY_FROM_SERVER; + else if (strcmp(sync_type_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) == 0) + sync_type_value = ALERT_REFRESH_FROM_SERVER; + else if (strcmp(sync_type_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) == 0) + sync_type_value = ALERT_REFRESH_FROM_CLIENT; + else + sync_type_value = ALERT_UNKNOWN; + + _INNER_FUNC_EXIT; + return sync_type_value; +} + +static char *__convert_sync_type_str(alert_type_e sync_type) +{ + _INNER_FUNC_ENTER; + + char *sync_Type = NULL; + + switch (sync_type) { + case ALERT_TWO_WAY: + sync_Type = DEFINE_ALERT_TWO_WAY_STR; + break; + case ALERT_SLOW_SYNC: + sync_Type = DEFINE_ALERT_SLOW_SYNC_STR; + break; + case ALERT_ONE_WAY_FROM_CLIENT: + sync_Type = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR; + break; + case ALERT_REFRESH_FROM_CLIENT: + sync_Type = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR; + break; + case ALERT_ONE_WAY_FROM_SERVER: + sync_Type = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR; + break; + case ALERT_REFRESH_FROM_SERVER: + sync_Type = DEFINE_ALERT_REFRESH_FROM_SERVER_STR; + break; + default: + sync_Type = DEFINE_ALERT_UNKNOWN_STR; + break; + } + + _INNER_FUNC_EXIT; + return sync_Type; +} + +static char *___convert_sync_progress_status_str(sync_progress_status_e progress_status) +{ + _INNER_FUNC_ENTER; + + char *progress = NULL; + + switch (progress_status) { + case SYNC_PROGRESS_NONE: + progress = DEFINE_PROGRESS_NONE; + break; + case SYNC_PROGRESS_SUCCESS: + progress = DEFINE_PROGRESS_SUCCESS; + break; + case SYNC_FAILED_DB: + progress = DEFINE_FAILED_DB; + break; + case SYNC_FAILED_DB_FORBIDDEN: + progress = DEFINE_FAILED_DB_FORBIDDEN; + break; + case SYNC_FAILED_DB_ITEM: + progress = DEFINE_FAILED_DB_ITEM; + break; + case SYNC_FAILED_DB_CONFIG: + progress = DEFINE_FAILED_DB_CONFIG; + break; + case SYNC_FAILED_DB_DEVICEFULL: + progress = DEFINE_FAILED_DB_DEVICEFULL; + break; + default: + break; + } + + _INNER_FUNC_EXIT; + return progress; +} + +static char *___convert_operation_type_str(operation_type_e operation_type) +{ + _INNER_FUNC_ENTER; + + char *operation = NULL; + + switch (operation_type) { + case OPERATION_NOOP: + operation = DEFINE_NOOP; + break; + case OPERATION_ADD: + operation = DEFINE_ADD; + break; + case OPERATION_DELETE: + operation = DEFINE_DELETE; + break; + case OPERATION_MOVE: + operation = DEFINE_MOVE; + break; + case OPERATION_COPY: + operation = DEFINE_COPY; + break; + case OPERATION_REPLACE: + operation = DEFINE_REPLACE; + break; + default: + break; + } + + _INNER_FUNC_EXIT; + return operation; +} + +static char *_convert_sync_progress_str(sync_progress_e process) +{ + _INNER_FUNC_ENTER; + + char *syncProcess = NULL; + + switch (process) { + case PROGRESS_NONE: + syncProcess = DEFINE_SYNC_PROGRESS_NONE; + break; + case PROGRESS_INIT: + syncProcess = DEFINE_SYNC_INIT; + break; + case PROGRESS_CONNECTING: + syncProcess = DEFINE_SYNC_CONNECTING; + break; + case PROGRESS_AUTHENTICATED: + syncProcess = DEFINE_SYNC_AUTHENTICATED; + break; + case PROGRESS_DONE: + syncProcess = DEFINE_SYNC_DONE; + break; + case PROGRESS_ERROR: + syncProcess = DEFINE_SYNC_ERROR; + break; + default: + break; + } + + _INNER_FUNC_EXIT; + return syncProcess; +} + +static char *_convert_sync_error_str(sync_error_e error) +{ + _INNER_FUNC_ENTER; + + char *syncError = NULL; + + switch (error) { + case ERROR_NONE: + syncError = DEFINE_ERROR_NONE; + break; + case ERROR_CONNECTION: + syncError = DEFINE_ERROR_CONNECTION; + break; + case ERROR_SYNCHDR: + syncError = DEFINE_ERROR_SYNCHDR; + break; + case ERROR_INTERNAL: + syncError = DEFINE_ERROR_INTERNAL; + break; + case ERROR_SUSPENDED: + syncError = DEFINE_ERROR_SUSPENDED; + break; + case ERROR_DB: + syncError = DEFINE_ERROR_DB; + break; + case ERROR_ABORT: + syncError = DEFINE_ERROR_ABORT; + break; + case ERROR_SERVER: + syncError = DEFINE_ERROR_SERVER; + break; + case ERROR_MEMORY_FULL: + syncError = DEFINE_ERROR_MEMORY_FULL; + break; + case ERROR_AUTHENTICATE: + syncError = DEFINE_ERROR_AUTHENTICATE; + break; + case ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER: + syncError = DEFINE_ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER; + break; + case ERROR_LOW_BATTERY: + syncError = DEFINE_ERROR_LOW_BATTERY; + break; + default: + break; + } + + _INNER_FUNC_EXIT; + return syncError; +} + +static se_error_type_e _check_low_battery() +{ + _INNER_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_dev_return_e dci_err = SYNC_AGENT_DEV_RETURN_SUCCESS; + char *battery_level = NULL; + int int_battery_level = 0; + + dci_err = sync_agent_get_devinfo(1, "Battery", &battery_level); + if (dci_err != SYNC_AGENT_DEV_RETURN_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_get_devinfo = %d", dci_err); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + _DEBUG_TRACE("battery_level =%s", battery_level); + + if (battery_level != NULL) { + int_battery_level = atoi(battery_level); + + if (int_battery_level < LOW_BATTERY_LEVEL) { + _DEBUG_ERROR("LOW Battery = %d", int_battery_level); + err = SE_INTERNAL_LOW_BATTERY; + goto error; + } + } + + error: + + if (battery_level != NULL) + free(battery_level); + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _open_services() +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (!datastoreinfo_per_content_type[content_type]->client_sync_type) + continue; + + da_err = sync_agent_open_service(content_type); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_open_service(%d) is failed", content_type); + err = SE_INTERNAL_DA_ERROR; + break; + } + } + } + + _INNER_FUNC_EXIT; + return err; +} + +static se_error_type_e _close_services() +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (!datastoreinfo_per_content_type[content_type]->client_sync_type) + continue; + + da_err = sync_agent_close_service(content_type); + if (da_err != SYNC_AGENT_DA_SUCCESS) + _DEBUG_ERROR("sync_agent_close_service(%d) is failed", content_type); + } + } + + _INNER_FUNC_EXIT; + return err; +} + +bool synchronize(int account_id, char *sync_mode, san_package_s * san_package) +{ + _EXTERN_FUNC_ENTER; + + _DEBUG_INFO("accountID = %d", account_id); + _DEBUG_INFO("sync_mode = %s", sync_mode); + + /*FIXME remove msg file */ + int ret_val = remove(OMA_DS_MSG_PATH); + if (ret_val < 0) { + _DEBUG_ERROR("failed in synchronize - remove()"); + } + + /*FIXME time check */ + long t, dt; + t = myclock(); + + se_error_type_e err = SE_INTERNAL_OK; + sync_agent_dev_return_e dci_result = SYNC_AGENT_DEV_RETURN_SUCCESS; + sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; + + common_error_type_e errorCode = COMMON_OK; + sync_progress_e process = PROGRESS_NONE; + sync_error_e error = ERROR_NONE; + + alert_type_e client_sync_type = ALERT_UNKNOWN; + alert_type_e server_sync_type = ALERT_UNKNOWN; + int only_from_client = 0; + + int session_time = sync_agent_convert_seconds_to_utc(time(NULL)); /*lastSessionTime for resultView; */ + _DEBUG_INFO("session_time = %d", session_time); + + pre_sync_return_obj_s *pre_sync_return_obj = NULL; + sync_obj_s *sync_obj = NULL; + sync_return_obj_s *sync_return_obj = NULL; + + bool cancel_flag = false; + int content_type; + char *session_id = NULL; + + char *access_name = NULL; + bool server_flag = false; + + char *msg = NULL; + unsigned int msg_size; + char *recvMsg = NULL; + unsigned int recvMsg_size; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + + dci_result = sync_agent_execute_dev_function(DEFINE_PLATFORM, "pm_lock", 3, LCD_OFF, STAY_CUR_STATE, 0); + if (dci_result != SYNC_AGENT_DEV_RETURN_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_execute_dev_function lock = %d", dci_result); + } + + /* low battery check */ + err = _check_low_battery(); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("check_low_battery = %d", err); + goto fail_part; + } + + da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_agent = %d", da_err); + err = SE_INTERNAL_DA_ERROR; + goto fail_part; + } + + /* prepare for sending pre sync */ + err = _prepare_pre_sync(account_id, sync_mode, san_package, &client_sync_type); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __prepare_pre_sync = %d", err); + goto fail_part; + } + + /* check cancel flag */ + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + err = SE_INTERNAL_CANCEL; + _DEBUG_INFO("cancle flag is on"); + goto cancel_part; + } + + pre_sync_return_obj = (pre_sync_return_obj_s *) calloc(1, sizeof(pre_sync_return_obj_s)); + if (pre_sync_return_obj == NULL) { + _DEBUG_ERROR("failed to alloc memory"); + err = SE_INTERNAL_NO_MEMORY; + goto fail_part; + } + + acc_err = sync_agent_create_fw_account(&fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_create_fw_account"); + err = SE_INTERNAL_NO_MEMORY; + goto fail_part; + } + + acc_err = sync_agent_get_fw_account(account_id, &fw_account); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_update_fw_account"); + err = SE_INTERNAL_ERROR; + goto fail_part; + } + + access_name = strdup(fw_account->access_name); + if (strcmp(access_name, "DIVE") == 0) { + server_flag = true; + } + + /*pkg 1 */ + if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) { + _DEBUG_INFO("sessionID = %d", san_package->session_id); + session_id = g_strdup_printf("%u", san_package->session_id); /*freed in pre_sync */ + errorCode = pre_sync(TRANSPORT_TYPE, account_id, session_id, server_flag, (void **)&pre_sync_return_obj); + } else + errorCode = pre_sync(TRANSPORT_TYPE, account_id, NULL, server_flag, (void **)&pre_sync_return_obj); + + /* check cancel flag */ + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + err = SE_INTERNAL_CANCEL; + _DEBUG_INFO("cancle flag is on"); + goto cancel_part; + } + + _DEBUG_INFO("pre_sync errorCode =[%d]", errorCode); + if (errorCode != COMMON_OK) { + err = SE_INTERNAL_SA_ERROR; + goto fail_part; + } + + /*execute pre_sync return */ + server_sync_type = client_sync_type; + err = _execute_pre_sync(account_id, session_time, pre_sync_return_obj, &server_sync_type); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __process_pre_sync = %d", err); + goto fail_part; + } + + if (server_sync_type == ALERT_REFRESH_FROM_CLIENT || server_sync_type == ALERT_ONE_WAY_FROM_CLIENT) + only_from_client = 1; + + _DEBUG_INFO("client_sync_type = %d", client_sync_type); + _DEBUG_INFO("server_sync_type = %d", server_sync_type); + _DEBUG_INFO("only_from_client = %d", only_from_client); + + _session_process(account_id, server_sync_type, PROGRESS_AUTHENTICATED, ERROR_NONE); + + /* check cancel flag */ + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + err = SE_INTERNAL_CANCEL; + _DEBUG_INFO("cancle flag is on"); + goto cancel_part; + } + + err = _open_services(); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __prepare_pre_sync = %d", err); + goto fail_part; + } + + /* pkg 3 */ + _DEBUG_INFO("pre_sync end pkg3 start"); + sync_obj = (sync_obj_s *) calloc(1, sizeof(sync_obj_s)); + if (sync_obj == NULL) { + _DEBUG_ERROR("failed to alloc memory"); + err = SE_INTERNAL_NO_MEMORY; + goto fail_part; + } + + sync_return_obj = (sync_return_obj_s *) calloc(1, sizeof(sync_return_obj_s)); + if (sync_return_obj == NULL) { + _DEBUG_ERROR("failed to alloc memory"); + err = SE_INTERNAL_NO_MEMORY; + goto fail_part; + } + + err = _assemble_changed_datastores(account_id, server_sync_type, &sync_obj); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __assemble_changeddatastore = %d", err); + goto fail_part; + } + + /* check cancel flag */ + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + err = SE_INTERNAL_CANCEL; + _DEBUG_INFO("cancle flag is on"); + goto cancel_part; + } + + int isFinish = 0; + while (!isFinish) { + + errorCode = generate_msg((void **)&sync_obj, server_flag, &msg, &msg_size); + if (errorCode != COMMON_OK) { + _DEBUG_ERROR("Failed in generate_Msg = %d", errorCode); + err = SE_INTERNAL_SA_ERROR; + goto fail_part; + } + + errorCode = exchange_msg(TRANSPORT_TYPE, msg, msg_size, &recvMsg, &recvMsg_size); + if (errorCode != COMMON_OK) { + _DEBUG_ERROR("Failed in exchange_Msg = %d", errorCode); + if (errorCode == COMMON_CANCEL) { + bool cancel_status = check_cancel_status(); + if (cancel_status) { + err = SE_INTERNAL_SUSPEND; + _DEBUG_INFO("cancle flag is on"); + goto suspend_part; + } + } else { + err = SE_INTERNAL_SA_ERROR; + goto fail_part; + } + } + + /* check cancel flag */ + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + bool cancel_status = check_cancel_status(); + if (cancel_status) { + err = SE_INTERNAL_SUSPEND; + _DEBUG_INFO("cancle flag is on"); + goto suspend_part; + } + } + + errorCode = process_recv_msg(recvMsg, recvMsg_size, only_from_client, (void **)&sync_return_obj, &isFinish); + if (errorCode != COMMON_OK) { + _DEBUG_ERROR("Failed in processRecv_Msg = %d", errorCode); + err = SE_INTERNAL_SA_ERROR; + goto fail_part; + } + + err = _execute_sync(account_id, client_sync_type, server_sync_type, &sync_obj, &sync_return_obj); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __execute_sync = %d", err); + goto fail_part; + } + + free_changed_datastores(sync_return_obj->changed_datastore); + sync_return_obj->changed_datastore = NULL; + + free_applied_statuses(sync_return_obj->status); + sync_return_obj->status = NULL; + + if (msg != NULL) { + free(msg); + msg = NULL; + } + + if (recvMsg != NULL) { + free(recvMsg); + recvMsg = NULL; + } + } + + err = _update_sync_result(account_id); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in __update_anchor = %d", err); + goto fail_part; + } + + _write_sync_data(account_id, client_sync_type, SYNC_SESSION_SUCCEEDED, session_time, only_from_client); + + goto return_part; + + suspend_part: + + errorCode = suspend_sync(TRANSPORT_TYPE, account_id, server_flag); + if (errorCode != COMMON_OK) { + _DEBUG_ERROR("Failed in suspend_sync = %d", errorCode); + err = SE_INTERNAL_SA_ERROR; + if (errorCode == COMMON_SUSPEND_FAIL) + _off_resume_flag(account_id); + + goto fail_part; + } + + cancel_part: + + /*clean up for SA unusual end sync process case */ + clean_up_sa(); + + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL) + datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_STOPPED; + + if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL) + datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_STOPPED; + + } + } + } + + _write_sync_data(account_id, client_sync_type, SYNC_SESSION_STOPPED, session_time, only_from_client); + + goto return_part; + + fail_part: + + /*clean up for SA unusual end sync process case */ + clean_up_sa(); + + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + if (datastoreinfo_per_content_type[content_type]->client_sync_type) { + if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL) + datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_FAILED; + + if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL) + datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_FAILED; + } + } + } + + _write_sync_data(account_id, client_sync_type, SYNC_SESSION_FAILED, session_time, only_from_client); + + return_part: + + if (err == SE_INTERNAL_SA_ERROR) + convert_common_errorcode(errorCode, &process, &error); + else + convert_engine_errorcode(err, &process, &error); + + /* off when session finish normal or cancel case(not suspend) */ + if (err != SE_INTERNAL_SUSPEND) + _off_resume_flag(account_id); + + _off_synchronising_account(account_id); + + _session_process(account_id, server_sync_type, process, error); + _DEBUG_INFO("server_sync_type = %d", server_sync_type); + _DEBUG_INFO("process = %d", process); + _DEBUG_INFO("error = %d", error); + + sync_agent_close_agent(); + _close_services(); + + dci_result = sync_agent_execute_dev_function(DEFINE_PLATFORM, "pm_unlock", 2, LCD_OFF, RESET_TIMER); + if (dci_result != SYNC_AGENT_DEV_RETURN_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_execute_dev_function unlock"); + } + + sync_agent_free_fw_account(fw_account); + + if (access_name != NULL) { + free(access_name); + access_name = NULL; + } + + if (msg != NULL) { + free(msg); + msg = NULL; + } + + if (recvMsg != NULL) { + free(recvMsg); + recvMsg = NULL; + } + + if (pre_sync_return_obj != NULL) { + free_pre_sync_return_obj(pre_sync_return_obj); + pre_sync_return_obj = NULL; + } + + if (sync_obj != NULL) { + free_sync_obj(sync_obj); + sync_obj = NULL; + } + + if (sync_return_obj != NULL) { + free_sync_return_obj(sync_return_obj); + sync_return_obj = NULL; + } + + for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) { + if (datastoreinfo_per_content_type[content_type] != NULL) { + free_datastore(datastoreinfo_per_content_type[content_type]); + datastoreinfo_per_content_type[content_type] = NULL; + } + } + + /*FIXME*/ dt = myclock() - t; + _DEBUG_ERROR("\n=============TOTAL---- % d . % d sec\n", dt / 1000, dt % 1000); + + _EXTERN_FUNC_EXIT; + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +void convert_common_errorcode(common_error_type_e errorCode, sync_progress_e * process, sync_error_e * error) +{ + _EXTERN_FUNC_ENTER; + + switch (errorCode) { + case COMMON_CANCEL: + case COMMON_SUSPEND_FAIL: + case COMMON_OK: /*ERROR_INTERNAL_OK */ + { + /* Do nothing : Error None + * pre_sync : PROGRESS_AUTHENTICATED + * sync end : PROGRESS_DONE + process = PROGRESS_AUTHENTICATED; + process = PROGRESS_DONE; + */ + *process = PROGRESS_DONE; + *error = ERROR_NONE; + } + break; + case COMMON_MISCONFIGURATION: /*ERROR_INTERNAL_MISCONFIGURATION : need configure infomation (account_id, id, pw, server_url...) */ + { + *process = PROGRESS_ERROR; + *error = ERROR_SYNCHDR; + } + break; + case COMMON_AUTHENTICATION_ERROR: /*ERROR_AUTH_REQUIRED, ERROR_AUTH_REJECTED */ + { + *process = PROGRESS_ERROR; + *error = ERROR_AUTHENTICATE; + } + break; + case COMMON_NOT_FOUND: /*ERROR_NOT_FOUND (ERROR_INTERNAL ??) */ + { + *process = PROGRESS_ERROR; + *error = ERROR_SYNCHDR; + } + break; + case COMMON_NO_MEMORY: /*ERROR_INTERNAL_NO_MEMORY */ + { + *process = PROGRESS_ERROR; + *error = ERROR_MEMORY_FULL; + } + break; + case COMMON_INTERNAL_ERROR: /*ERROR_INTERNAL_NOT_DEFINED || ERROR_INTERNAL_BINDER_ERROR */ + { + *process = PROGRESS_ERROR; + *error = ERROR_INTERNAL; + } + break; + case COMMON_SERVER_ERROR: /*ERROR_GENERIC || ERROR_SERVER_FAILURE */ + { + *process = PROGRESS_ERROR; + *error = ERROR_SERVER; + } + break; + case COMMON_CONNECTION_ERROR: /*ERROR_INTERNAL_CONNECTION_ERROR */ + { + *process = PROGRESS_ERROR; + *error = ERROR_CONNECTION; + } + break; + case COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER: /*ERROR_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER */ + { + *process = PROGRESS_ERROR; + *error = ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER; + } + break; + default: + { + *process = PROGRESS_ERROR; + *error = ERROR_INTERNAL; /*?? unknown error */ + } + break; + } + + _EXTERN_FUNC_EXIT; +} + +void convert_engine_errorcode(se_error_type_e err, sync_progress_e * process, sync_error_e * error) +{ + _EXTERN_FUNC_ENTER; + + switch (err) { + case SE_INTERNAL_SUSPEND: + case SE_INTERNAL_CANCEL: + case SE_INTERNAL_OK: + *process = PROGRESS_DONE; + *error = ERROR_NONE; + break; + case ERROR_UNKNOWN: + case SE_INTERNAL_SA_ERROR: + case SE_INTERNAL_SCHEDULER_ERROR: + case SE_INTERNAL_ENGINE_CONTROLER_ERROR: + case SE_INTERNAL_EVENT_ERROR: + case SE_INTERNAL_NOT_DEFINED: + case SE_INTERNAL_ERROR: + *process = PROGRESS_ERROR; + *error = ERROR_INTERNAL; + break; + case SE_INTERNAL_NO_MEMORY: + *process = PROGRESS_ERROR; + *error = ERROR_MEMORY_FULL; + break; + case SE_INTERNAL_DA_ERROR: + *process = PROGRESS_ERROR; + *error = ERROR_DB; + break; + case SE_INTERNAL_MISCONFIGURATION: + *process = PROGRESS_ERROR; + *error = ERROR_SYNCHDR; + break; + case SE_INTERNAL_LOW_BATTERY: + *process = PROGRESS_ERROR; + *error = ERROR_LOW_BATTERY; + break; + } + + _EXTERN_FUNC_EXIT; +} + +se_error_type_e session_process(char *profileDirName, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error) +{ + _EXTERN_FUNC_ENTER; + + _DEBUG_INFO("profileDirName = %s", profileDirName); + _DEBUG_INFO("process = %d", process); + _DEBUG_INFO("error = %d", error); + + se_error_type_e err = SE_INTERNAL_OK; + + char *sync_type = NULL; + char *syncProcess = NULL; + char *syncError = NULL; + + sync_type = __convert_sync_type_str(server_sync_type); + + syncProcess = _convert_sync_progress_str(process); + + syncError = _convert_sync_error_str(error); + + if (sync_type == NULL || syncProcess == NULL || syncError == NULL) { + err = SE_INTERNAL_NOT_DEFINED; + goto error; + } + + err = send_noti_session_process(profileDirName, sync_type, syncProcess, syncError); + if (err != SE_INTERNAL_OK) { + _DEBUG_ERROR("failed in send_noti_session_process"); + goto error; + } + + _EXTERN_FUNC_EXIT; + + error: + + return err; +} + +se_error_type_e reset_synchronizing_profiles() +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; + sync_agent_fw_account_s *fw_account = NULL; + GList *account_info_list = NULL; + GList *iter = NULL; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("failed in sync_agent_open_agent"); + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + sync_agent_fw_account_query_s query; + query.query = ACCOUNT_QUERY_BY_NONE; + + acc_err = sync_agent_query_fw_account(&query, &account_info_list); + if (acc_err != SYNC_AGENT_ACC_SUCCESS) { + _DEBUG_ERROR("sync_agent_query_fw_account is failed"); + goto error; + } + + for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { + fw_account = (sync_agent_fw_account_s *) iter->data; + + _DEBUG_INFO("account = %d", fw_account->account_id); + _off_synchronising_account(fw_account->account_id); + + _DEBUG_INFO("construct_itemTbl From service start"); + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT - 1; content_type++) { + + da_err = sync_agent_open_service(content_type); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_open_service(%d) is failed ", content_type); + continue; + } + + _DEBUG_INFO("sync_agent_construct_item_tbl_from_service(%d, %d);", fw_account->account_id, content_type); + sync_agent_construct_item_tbl_from_service(fw_account->account_id, content_type); + + da_err = sync_agent_close_service(content_type); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_close_service(%d) is failed ", content_type); + continue; + } + } + + _DEBUG_INFO("construct_itemTbl From service end"); + } + + error: + + sync_agent_close_agent(); + + sync_agent_free_fw_account_list(account_info_list); + + _EXTERN_FUNC_EXIT; + + return err; +} + +bool refresh_from_service_all(int account_id) +{ + _EXTERN_FUNC_ENTER; + + se_error_type_e err = SE_INTERNAL_OK; + + sync_agent_da_return_e da_err = sync_agent_open_agent(); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + err = SE_INTERNAL_DA_ERROR; + goto error; + } + + int content_type; + for (content_type = 0; content_type < TYPE_SERVICE_COUNT - 1; content_type++) { + + da_err = sync_agent_open_service(content_type); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_open_service(%d) is failed ", content_type); + continue; + } + + _DEBUG_INFO("sync_agent_refresh_item_tbl_from_service(%d, %d);", account_id, content_type); + sync_agent_refresh_item_tbl_from_service(account_id, content_type); + + da_err = sync_agent_close_service(content_type); + if (da_err != SYNC_AGENT_DA_SUCCESS) { + _DEBUG_ERROR("sync_agent_close_service(%d) is failed ", content_type); + continue; + } + } + + error: + + sync_agent_close_agent(); + + _EXTERN_FUNC_EXIT; + + if (err != SE_INTERNAL_OK) + return false; + else + return true; +} + +se_error_type_e cancel_sync_request() +{ + _EXTERN_FUNC_ENTER; + se_error_type_e err = SE_INTERNAL_OK; + + cancel_connection_sync_request(TRANSPORT_TYPE); + + _EXTERN_FUNC_EXIT; + + return err; +} |