Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / src / agent / main / main.c
1 /*
2  * oma-ds-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *   @main.c
20  *   @version                                                                   0.1
21  *   @brief                                                                             This file is the source file of implementation of main function
22  */
23
24 typedef enum {
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
37 } oma_ds_event_e;
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <signal.h>
43
44 #include <dbus/dbus-glib.h>
45 #include <dbus/dbus.h>
46
47 #include <sync_agent.h>
48
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"
56
57 #ifndef OMADS_AGENT_LOG
58 #undef LOG_TAG
59 #define LOG_TAG "OMA_DS_MAIN"
60 #endif
61
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"
65
66 static DBusConnection *connection = NULL;
67
68 static void _unregistered_path(DBusConnection * connection, void *user_data)
69 {
70         /* connection was finalized */
71         _INNER_FUNC_ENTER;
72
73         _INNER_FUNC_EXIT;
74 }
75
76 static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data)
77 {
78         _INNER_FUNC_ENTER;
79         if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) {
80                 DBusMessage *reply;
81                 _DEBUG_INFO("Recived the HelloFromSelf message\n");
82
83                 reply = dbus_message_new_method_return(message);
84                 if (reply == NULL)
85                         _DEBUG_ERROR("no memory\n");
86
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");
93                 _INNER_FUNC_EXIT;
94                 exit(1);
95         } else {
96                 _DEBUG_INFO("can't match message");
97         }
98
99         _INNER_FUNC_EXIT;
100         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
101 }
102
103 static DBusObjectPathVTable echo_vtable = {
104         _unregistered_path,
105         _message_path,
106         NULL,
107 };
108
109 static int _register_dbus_service(void)
110 {
111         _INNER_FUNC_ENTER;
112         int result;
113         DBusError error;
114         void *d;
115
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);
121                 return 1;
122         }
123
124         if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) {
125                 _DEBUG_ERROR("no memory\n");
126                 goto failed;
127         }
128
129         if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) {
130                 _DEBUG_ERROR("no memory\n");
131                 goto failed;
132         }
133
134         if (d != (void *)0xdeadbeef) {
135                 _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n");
136                 goto failed;
137         }
138
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);
144                 _INNER_FUNC_EXIT;
145                 exit(1);
146         }
147
148         _INNER_FUNC_EXIT;
149         return 0;
150
151  failed:
152         dbus_connection_unref(connection);
153         _INNER_FUNC_EXIT;
154         return 1;
155 }
156
157 //static void __update_csc()
158 //{
159 //      _INNER_FUNC_ENTER;
160 //
161 //      char profile[128];
162 //      int numberOfProfiles = 0;
163 //      char *contents = "check";
164 //
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");
168 //              goto error;
169 //      }
170 //
171 //      int i;
172 //      for (i = 1; i <= numberOfProfiles; i++) {
173 //              snprintf(profile, sizeof(profile), "Sync%d", i);
174 //              add_profile_csc(i);
175 //      }
176 //
177 //      sync_agent_write_whole_file(OMA_DS_CSC_CHECK_PATH, contents, strlen(contents), false);
178 //
179 //error:
180 //
181 //      _INNER_FUNC_EXIT;
182 //}
183
184 static void _request_reset_synchronizing_profiles()
185 {
186         _INNER_FUNC_ENTER;
187
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);
190         _INNER_FUNC_EXIT;
191 }
192
193 static void _check_csc()
194 {
195         _INNER_FUNC_ENTER;
196
197         bool exist = false;
198
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 */
203
204         /*exist = sync_agent_is_existing_fs(OMA_DS_CSC_CHECK_PATH); */
205         exist = true;
206         _DEBUG_TRACE("update = %d", exist);
207
208         /* for prevent */
209 //      if (exist == false)
210 //              __update_csc();
211
212         _INNER_FUNC_EXIT;
213 }
214
215 static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context)
216 {
217         _INNER_FUNC_ENTER;
218
219         switch (signo) {
220         case SIGTERM:
221                 _DEBUG_TRACE("Got SIGTERM");
222
223                 /* stop gmain loop */
224                 sync_agent_stop_main_loop(0);
225
226                 break;
227
228         default:
229                 break;
230         }
231
232         _INNER_FUNC_EXIT;
233 }
234
235 int main()
236 {
237         _EXTERN_FUNC_ENTER;
238
239         struct sigaction sig_act;
240
241         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
242         sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
243
244         if (sync_agent_daemonize() < 0) {
245                 _DEBUG_ERROR("daemonize error");
246                 return -1;
247         }
248
249         _register_dbus_service();
250
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);
254         } else {
255                 _DEBUG_INFO("done init_Framework");
256         }
257
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();
271
272         sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_REQUEST, EC_MSG_TYPE_NAME_SYNC_TASK_REQUEST, pSyncTaskSpec, NULL);
273
274         sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_ADD_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_ADD_PROFILE, pAddProfileTaskSpec, NULL);
275
276         sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_EDIT_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_EDIT_PROFILE, pEditProfileTaskSpec, NULL);
277
278         sync_agent_register_task_spec(EC_MSG_TYPE_SYNC_TASK_DELETE_PROFILE, EC_MSG_TYPE_NAME_SYNC_TASK_DELETE_PROFILE, pDeleteProfileTaskSpec, NULL);
279
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);
281
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);
283
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);
285
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);
287
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);
289
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);
291
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);
293
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);
295
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);
297
298         sync_agent_ec_queuing_rule_spec_s *pQueuing_rule = sync_agent_create_queuing_rule_spec_outline("sync_queueing_rule");
299
300         sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_REFRESH_FROM_SERVICE, pSyncTaskSpec, 0);
301
302         sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_REQUEST, pSyncTaskSpec, 0);
303
304         sync_agent_add_progress_blocking_element(pQueuing_rule, EC_MSG_TYPE_SYNC_TASK_RESET_ALL_DATA, pSyncTaskSpec, 0);
305
306         sync_agent_register_async_queuing_rule_spec(pQueuing_rule, NULL, NULL);
307
308         sync_agent_unref_queuing_rule_spec(pQueuing_rule);
309
310         _request_reset_synchronizing_profiles();        /*reset synchronizing flag 0 every account & construct item tbl */
311
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()");
315
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()");
319
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()");
323
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()");
327
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()");
331
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()");
335
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()");
339
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()");
343
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()");
347
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()");
351
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()");
355
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()");
359
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");
363
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");
367
368         /*register profiles from csc */
369         _check_csc();
370
371         /* Block SIGPIPE signal (client may close socket abnormally) */
372         signal(SIGPIPE, SIG_IGN);
373
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);
380
381         sync_agent_run_main_loop(0);
382
383         if (connection) {
384                 dbus_connection_unref(connection);
385                 connection = NULL;
386         }
387
388         deinit_error = sync_agent_deinit();
389         if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS)
390                 _DEBUG_ERROR("failed in sync_agent_deinit");
391
392         _EXTERN_FUNC_EXIT;
393
394         return 0;
395 }