3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * @brief This file is the source file of implementation of main function
25 OMA_DS_ADD_PROFILE = 1,
26 OMA_DS_EDIT_PROFILE = 2,
27 OMA_DS_DELETE_PROFILE = 3,
28 OMA_DS_REQUEST_SYNC = 4,
29 OMA_DS_CANCEL_SYNC = 5,
30 OMA_DS_GET_PROFILE_DATA = 6,
31 OMA_DS_GET_PROFILE_SYNC_CATEGORY = 7,
32 OMA_DS_GET_PROFILE_SYNC_STATISTICS = 8,
33 OMA_DS_GET_ALL_PROFILES_DATA = 9,
34 OMA_DS_ADD_PROFILE_CP = 10,
35 OMA_DS_REQUEST_CALLLOG_SYNC = 11,
36 OMA_DS_RESET_ALL_DATA = 12
44 #include <dbus/dbus-glib.h>
45 #include <dbus/dbus.h>
47 #include <sync_agent.h>
49 #include "framework/event/oma_ds_platform_event_handler.h"
50 #include "framework/event/oma_ds_event_handler.h"
51 #include "framework/task/oma_ds_engine_controller_task.h"
52 #include "common/csc_keys.h"
53 #include "common/common_define_internal.h"
54 #include "common/common_vconf.h"
55 #include "service-engine/se_account.h"
57 #ifndef OMADS_AGENT_LOG
59 #define LOG_TAG "OMA_DS_MAIN"
62 #define MC_SERVICE_OBJECT_PATH "/com/samsung/omadsagent" /* Don't use special character */
63 #define MC_SERVICE_DBUS "com.samsung.omadsagent"
64 #define MC_SERVICE_INTERFACE "com.samsung.omadsagent"
66 static DBusConnection *connection = NULL;
68 static void _unregistered_path(DBusConnection * connection, void *user_data)
70 /* connection was finalized */
76 static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data)
79 if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) {
81 _DEBUG_INFO("Recived the HelloFromSelf message\n");
83 reply = dbus_message_new_method_return(message);
85 _DEBUG_ERROR("no memory\n");
87 if (!dbus_connection_send(connection, reply, NULL))
88 _DEBUG_ERROR("no memory\n");
89 } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Hello_Agent")) {
90 _DEBUG_INFO("Hello_Agent");
91 } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Goodbye_Agent")) {
92 _DEBUG_INFO("Goodbye_Agent");
96 _DEBUG_INFO("can't match message");
100 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
103 static DBusObjectPathVTable echo_vtable = {
109 static int _register_dbus_service(void)
116 dbus_error_init(&error);
117 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
118 if (connection == NULL) {
119 _DEBUG_ERROR("*** Failed to open connection to activating message bus: %s\n", error.message);
120 dbus_error_free(&error);
124 if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) {
125 _DEBUG_ERROR("no memory\n");
129 if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) {
130 _DEBUG_ERROR("no memory\n");
134 if (d != (void *)0xdeadbeef) {
135 _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n");
139 result = dbus_bus_request_name(connection, MC_SERVICE_DBUS, 0, &error);
140 if (dbus_error_is_set(&error)) {
141 _DEBUG_ERROR("Error %s\n", error.message);
142 dbus_error_free(&error);
143 dbus_connection_unref(connection);
152 dbus_connection_unref(connection);
157 //static void __update_csc()
159 // _INNER_FUNC_ENTER;
161 // char profile[128];
162 // int numberOfProfiles = 0;
163 // char *contents = "check";
165 // bool result = get_vconf_Int_key(CSC_VCONF_KEY_SYNCMLDS_NBDATASYNC, &numberOfProfiles);
166 // if (result == false) {
167 // _DEBUG_ERROR("failed in get_vconf_Int_key");
172 // for (i = 1; i <= numberOfProfiles; i++) {
173 // snprintf(profile, sizeof(profile), "Sync%d", i);
174 // add_profile_csc(i);
177 // sync_agent_write_whole_file(OMA_DS_CSC_CHECK_PATH, contents, strlen(contents), false);
184 static void _request_reset_synchronizing_profiles()
188 unsigned int request_msg_id = 0;
189 sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_RESET_SYNCHRONIZING_PROFILES, 0, 0, NULL, NULL, NULL, NULL, NULL, (int *)&request_msg_id);
193 static void _check_csc()
199 /*FIXME(temporary do not process csc for ui reason)
200 check csc_check file is existed
201 if false do csc update
202 if true check csc xml file has been changed */
204 /*exist = sync_agent_is_existing_fs(OMA_DS_CSC_CHECK_PATH); */
206 _DEBUG_TRACE("update = %d", exist);
209 // if (exist == false)
215 static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context)
221 _DEBUG_TRACE("Got SIGTERM");
223 /* stop gmain loop */
224 sync_agent_stop_main_loop(0);
239 struct sigaction sig_act;
241 sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
242 sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
244 if (sync_agent_daemonize() < 0) {
245 _DEBUG_ERROR("daemonize error");
249 _register_dbus_service();
251 sync_agent_init_error_e init_error = sync_agent_init("/usr/share/oma-ds-cfg/omads_fw_config.xml");
252 if (init_error != SYNC_AGENT_INIT_SUCCESS) {
253 _DEBUG_ERROR("Failed to sync_agent_init() : %d", init_error);
255 _DEBUG_INFO("done init_Framework");
258 sync_agent_ec_task_spec_s *pSyncTaskSpec = make_synchronize_task();
259 sync_agent_ec_task_spec_s *pAddProfileTaskSpec = make_add_profile_task();
260 sync_agent_ec_task_spec_s *pEditProfileTaskSpec = make_edit_profile_task();
261 sync_agent_ec_task_spec_s *pDeleteProfileTaskSpec = make_delete_profile_task();
262 sync_agent_ec_task_spec_s *pGetProfileDataTaskSpec = make_get_profile_data_task();
263 sync_agent_ec_task_spec_s *pGetProfileSyncCategoryTaskSpec = make_get_profile_sync_category_task();
264 sync_agent_ec_task_spec_s *pGetProfileStatisticsTaskSpec = make_get_profile_statistics_task();
265 sync_agent_ec_task_spec_s *pResetSynchronizingProfilesTaskSpec = make_reset_synchronizing_profiles_task();
266 sync_agent_ec_task_spec_s *pAddProfileCPTaskSpec = make_add_profile_cp_task();
267 sync_agent_ec_task_spec_s *pRefreshFromServiceTaskSpec = make_refresh_from_service_task();
268 sync_agent_ec_task_spec_s *pCancelSyncRequestTaskSpec = make_cancel_request_task();
269 sync_agent_ec_task_spec_s *pResetAllDataTaskSpec = make_reset_all_data_task();
270 sync_agent_ec_task_spec_s *pGetAllProfilesData = make_get_all_profiles_data_task();
272 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_REQUEST, EC_MSG_TYPE_NAME_SYNC_TASK_REQUEST, pSyncTaskSpec, NULL);
274 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_ADD_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_ADD_PROFILE, pAddProfileTaskSpec, NULL);
276 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_EDIT_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_EDIT_PROFILE, pEditProfileTaskSpec, NULL);
278 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_DELETE_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_DELETE_PROFILE, pDeleteProfileTaskSpec, NULL);
280 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_DATA, EC_MSG_TYPE_NAME_SYNC_TASK_GET_PROFILE_DATA, pGetProfileDataTaskSpec, NULL);
282 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);
284 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_GET_PROFILE_STATISTICS, EC_MSG_TYPE_NAME_SYNC_TASK_GET_PROFILE_STATISTICS, pGetProfileStatisticsTaskSpec, NULL);
286 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_RESET_SYNCHRONIZING_PROFILES, EC_MSG_TYPE_NAME_SYNC_TASK_RESET_SYNCHRONIZING_PROFILES, pResetSynchronizingProfilesTaskSpec, NULL);
288 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_ADD_PROFILE_CP, EC_MSG_TYPE_NAME_SYNC_TASK_ADD_PROFILE_CP, pAddProfileCPTaskSpec, NULL);
290 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_REFRESH_FROM_SERVICE, EC_MSG_TYPE_NAME_SYNC_TASK_REFRESH_FROM_SERVICE, pRefreshFromServiceTaskSpec, NULL);
292 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_CANCEL_SYNC_REQUEST, EC_MSG_TYPE_NAME_SYNC_TASK_CANCEL_SYNC_REQUEST, pCancelSyncRequestTaskSpec, NULL);
294 sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_RESET_ALL_DATA, EC_MSG_TYPE_NAME_SYNC_TASK_RESET_ALL_DATA, pResetAllDataTaskSpec, NULL);
296 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);
298 sync_agent_ec_queuing_rule_spec_s *pQueuing_rule = sync_agent_create_queuing_rule_spec_outline("sync_queueing_rule");
300 sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_REFRESH_FROM_SERVICE, pSyncTaskSpec, 0);
302 sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_REQUEST, pSyncTaskSpec, 0);
304 sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_RESET_ALL_DATA, pSyncTaskSpec, 0);
306 sync_agent_register_async_queuing_rule_spec(pQueuing_rule, NULL, NULL);
308 sync_agent_unref_queuing_rule_spec(pQueuing_rule);
310 _request_reset_synchronizing_profiles(); /*reset synchronizing flag 0 every account & construct item tbl */
312 error = sync_agent_set_event_callback(OMA_DS_ADD_PROFILE, event_callback_add_profile_sync);
313 if (error != SYNC_AGENT_EVENT_SUCCESS)
314 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
316 error = sync_agent_set_event_callback(OMA_DS_EDIT_PROFILE, event_callback_edit_profile_sync);
317 if (error != SYNC_AGENT_EVENT_SUCCESS)
318 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
320 error = sync_agent_set_event_callback(OMA_DS_DELETE_PROFILE, event_callback_delete_profile_sync);
321 if (error != SYNC_AGENT_EVENT_SUCCESS)
322 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
324 error = sync_agent_set_event_callback(OMA_DS_REQUEST_SYNC, event_callback_request_sync_sync);
325 if (error != SYNC_AGENT_EVENT_SUCCESS)
326 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
328 error = sync_agent_set_event_callback(OMA_DS_CANCEL_SYNC, event_callback_cancel_sync_sync);
329 if (error != SYNC_AGENT_EVENT_SUCCESS)
330 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
332 error = sync_agent_set_event_callback(OMA_DS_GET_PROFILE_DATA, event_callback_get_profile_data_sync);
333 if (error != SYNC_AGENT_EVENT_SUCCESS)
334 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
336 error = sync_agent_set_event_callback(OMA_DS_GET_PROFILE_SYNC_CATEGORY, event_callback_get_profile_sync_category_sync);
337 if (error != SYNC_AGENT_EVENT_SUCCESS)
338 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
340 error = sync_agent_set_event_callback(OMA_DS_GET_PROFILE_SYNC_STATISTICS, event_callback_get_profile_last_statistics_sync);
341 if (error != SYNC_AGENT_EVENT_SUCCESS)
342 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
344 error = sync_agent_set_event_callback(OMA_DS_GET_ALL_PROFILES_DATA, event_callback_request_get_all_profiles_data);
345 if (error != SYNC_AGENT_EVENT_SUCCESS)
346 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
348 error = sync_agent_set_event_callback(OMA_DS_ADD_PROFILE_CP, event_callback_add_profile_cp_sync);
349 if (error != SYNC_AGENT_EVENT_SUCCESS)
350 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
352 error = sync_agent_set_event_callback(OMA_DS_REQUEST_CALLLOG_SYNC, event_callback_request_calllog_sync_async);
353 if (error != SYNC_AGENT_EVENT_SUCCESS)
354 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
356 error = sync_agent_set_event_callback(OMA_DS_RESET_ALL_DATA, event_callback_request_reset_all_data_sync);
357 if (error != SYNC_AGENT_EVENT_SUCCESS)
358 _DEBUG_ERROR("failed sync_agent_set_event_callback()");
360 sync_agent_pm_return_e san_err = sync_agnet_register_user_callback(3, NULL, 1, san_callback_parse);
361 if (san_err != SYNC_AGENT_PM_SUCCESS)
362 _DEBUG_ERROR("failed in sync_agnet_register_user_callback");
364 sync_agent_pm_return_e scheduler_err = sync_agnet_register_user_callback(1, NULL, 1, send_periodic_sync_msg);
365 if (scheduler_err != SYNC_AGENT_PM_SUCCESS)
366 _DEBUG_ERROR("failed in sync_agnet_register_user_callback");
368 /*register profiles from csc */
371 /* Block SIGPIPE signal (client may close socket abnormally) */
372 signal(SIGPIPE, SIG_IGN);
374 /* register signal handler. will be called by kill command */
375 sig_act.sa_handler = NULL;
376 sig_act.sa_sigaction = _agent_daemon_signal_handler;
377 sig_act.sa_flags = SA_SIGINFO;
378 sigemptyset(&sig_act.sa_mask);
379 sigaction(SIGTERM, &sig_act, NULL);
381 sync_agent_run_main_loop(0);
384 dbus_connection_unref(connection);
388 deinit_error = sync_agent_deinit();
389 if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS)
390 _DEBUG_ERROR("failed in sync_agent_deinit");