diff options
Diffstat (limited to 'src/agent/main/main.c')
-rwxr-xr-x | src/agent/main/main.c | 395 |
1 files changed, 395 insertions, 0 deletions
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; +} |