Apply tizen 3.0 based product patchsets
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-main.c
old mode 100755 (executable)
new mode 100644 (file)
index 7a18ca9..b9d9fb5
 
 static GMainLoop *main_loop;
 static gboolean terminated = FALSE;
+static gboolean is_initialized = FALSE;
+
+static void __on_log_glib(const gchar *log_domain, GLogLevelFlags log_level,
+               const gchar *msg, gpointer user_data)
+{
+       BT_ERR_C("%s", msg);
+}
 
 static void __bt_release_service(void)
 {
@@ -50,9 +57,7 @@ static void __bt_release_service(void)
 
        _bt_clear_request_list();
 
-#ifndef GATT_NO_RELAY
-       _bt_clear_gatt_client_senders();
-#endif
+       is_initialized = FALSE;
        _bt_service_cynara_deinit();
 
        BT_DBG("Terminating the bt-service daemon");
@@ -60,55 +65,29 @@ static void __bt_release_service(void)
 
 static void __bt_sigterm_handler(int signo, siginfo_t *info, void *data)
 {
-       int ret;
-
        BT_INFO("signal [%d] is sent by [%d]", signo, info->si_pid);
 
-#ifndef TIZEN_TV
-       ret = _bt_recover_adapter();
-       if (ret != BLUETOOTH_ERROR_NONE)
-               BT_ERR("_bt_recover_adapter is failed : %d", ret);
-#endif
+       if (!TIZEN_PROFILE_TV) {
+               int ret;
+               ret = _bt_recover_adapter();
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       BT_ERR("_bt_recover_adapter is failed : %d", ret);
+       }
        return;
 }
 
 gboolean _bt_terminate_service(gpointer user_data)
 {
-       int bt_status = VCONFKEY_BT_STATUS_OFF;
+       __bt_release_service();
 
-       if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
-               BT_ERR("no bluetooth device info, so BT was disabled at previous session");
-       } else {
-               if (bt_status != VCONFKEY_BT_STATUS_OFF) {
-                       if(vconf_set_int(VCONFKEY_BT_STATUS,
-                                       VCONFKEY_BT_STATUS_OFF) != 0)
-                               BT_ERR("Set vconf failed\n");
-
-                       if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
-                                                       EVT_VAL_BT_OFF) != ES_R_OK)
-                               BT_ERR("Fail to set value");
-               }
-       }
+       terminated = TRUE;
 
-       if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_status) < 0) {
-               BT_ERR("no bluetooth device info, so BT was disabled at previous session");
-       } else {
-               if (bt_status != VCONFKEY_BT_LE_STATUS_OFF) {
-                       if(vconf_set_int(VCONFKEY_BT_LE_STATUS,
-                                       VCONFKEY_BT_LE_STATUS_OFF) != 0)
-                               BT_ERR("Set vconf failed\n");
-                       if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
-                                                       EVT_VAL_BT_LE_OFF) != ES_R_OK)
-                               BT_ERR("Fail to set value");
-               }
-       }
+       BT_INFO_C("Terminating the bt-service daemon");
 
        if (main_loop != NULL) {
                g_main_loop_quit(main_loop);
        } else {
                BT_ERR("main_loop == NULL");
-               __bt_release_service();
-               terminated = TRUE;
                exit(0);
        }
 
@@ -117,111 +96,115 @@ gboolean _bt_terminate_service(gpointer user_data)
 
 gboolean _bt_reliable_terminate_service(gpointer user_data)
 {
-       _bt_deinit_service_event_receiver();
-
        _bt_deinit_proxys();
 
        _bt_clear_request_list();
 
        _bt_set_disabled(BLUETOOTH_ERROR_NONE);
 
-       _bt_service_adapter_le_deinit();
        _bt_deinit_service_event_sender();
-       _bt_deinit_hf_local_term_event_sender();
 
        _bt_service_unregister();
 
        terminated = TRUE;
 
-       BT_INFO_C("Terminating the bt-service daemon");
+       BT_INFO_C("### Terminating the bt-service daemon");
 
-       if (main_loop != NULL) {
+       if (main_loop != NULL)
                g_main_loop_quit(main_loop);
-       } else {
+       else
                exit(0);
-       }
 
        return FALSE;
 }
 
 static gboolean __bt_check_bt_service(void *data)
 {
-       int bt_status = VCONFKEY_BT_STATUS_OFF;
-       int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
        bt_status_t status = BT_DEACTIVATED;
        bt_le_status_t le_status = BT_LE_DEACTIVATED;
+       int bt_status = VCONFKEY_BT_STATUS_OFF;
+       int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
        int flight_mode_deactivation = 0;
        int bt_off_due_to_timeout = 0;
 #if 0
        int ps_mode_deactivation = 0;
 #endif
 
+       if (_is_name_acquired() == FALSE) {
+               BT_ERR("dbus name is NOT acquired yet");
+               return TRUE;
+       }
+
        status = _bt_adapter_get_status();
        le_status = _bt_adapter_get_le_status();
        BT_DBG("State: %d, LE State: %d", status, le_status);
 
-#ifdef TIZEN_TV
-       if (_bt_get_enable_timer_id() == 0)
-               _bt_enable_adapter();
-#else
-       if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
-               BT_DBG("no bluetooth device info, so BT was disabled at previous session");
-       }
+       if (TIZEN_PROFILE_TV) {
+               if (_bt_get_enable_timer_id() == 0)
+                       _bt_enable_adapter();
+       } else {
+               if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
+                       BT_DBG("no bluetooth device info, so BT was disabled at previous session");
 
-       if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0) {
-               BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
-       }
+               if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
+                       BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
 
-       if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
-               BT_ERR("Fail to get the flight_mode_deactivation value");
+               if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
+                       BT_ERR("Fail to get the flight_mode_deactivation value");
 
 #if 0
        if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
                BT_ERR("Fail to get the ps_mode_deactivation value");
+       }
 #endif
 
-       if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
-               BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
+               if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
+                       BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
 
-       if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
-               (status == BT_DEACTIVATED)) {
-               BT_DBG("Previous session was enabled.");
+               if (bt_off_due_to_timeout) {
+                       /* Set the vconf flag to 0 here, if BT need to be enabled
+                       bt_off_due_to_timeout variable already hold the old value */
 
-               /* Enable the BT */
-               _bt_enable_adapter();
-       } else if (bt_status == VCONFKEY_BT_STATUS_OFF && flight_mode_deactivation == 1) {
-               _bt_enable_core();
-       }
+                       if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 0) != 0)
+                               BT_ERR("Set vconf failed");
+               }
 
-       if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
-               BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
 
-               /* Enable the BT LE */
-               _bt_enable_adapter_le();
-       } else {
-               status = _bt_adapter_get_status();
-               le_status = _bt_adapter_get_le_status();
-               BT_DBG("State: %d, LE State: %d", status, le_status);
+               if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
+                       (status == BT_DEACTIVATED)) {
+                       BT_DBG("Previous session was enabled.");
 
-               if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
-                               (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)){
-                       _bt_terminate_service(NULL);
+                       /* Enable the BT */
+                       _bt_enable_adapter();
+               } else if (bt_status == VCONFKEY_BT_STATUS_OFF && flight_mode_deactivation == 1) {
+                       _bt_enable_core();
+               }
+
+               if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
+                       BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
+
+                       /* Enable the BT LE */
+                       _bt_enable_adapter_le();
+               } else {
+                       status = _bt_adapter_get_status();
+                       le_status = _bt_adapter_get_le_status();
+                       BT_DBG("State: %d, LE State: %d", status, le_status);
+
+                       if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
+                                       (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)) {
+                               _bt_terminate_service(NULL);
+                       }
                }
        }
-#endif
        return FALSE;
 }
 
-int main(void)
+int _bt_service_initialize(void)
 {
-       struct sigaction sa;
-       BT_INFO_C("Starting the bt-service daemon");
+       int ret;
 
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = __bt_sigterm_handler;
-       sa.sa_flags = SA_SIGINFO;
-       sigaction(SIGINT, &sa, NULL);
-       sigaction(SIGTERM, &sa, NULL);
+       if (is_initialized == TRUE)
+               return BLUETOOTH_ERROR_NONE;
 
        /* Security Initialization */
        if (_bt_service_cynara_init() != BLUETOOTH_ERROR_NONE) {
@@ -229,40 +212,62 @@ int main(void)
                return EXIT_FAILURE;
        }
 
+       _bt_service_register_vconf_handler();
+
        /* Event reciever Init */
-       if (_bt_init_service_event_receiver() != BLUETOOTH_ERROR_NONE) {
+       ret = _bt_init_service_event_receiver();
+       if (ret != BLUETOOTH_ERROR_NONE) {
                BT_ERR("Fail to init event reciever");
-               return 0;
+               return ret;
        }
 
        /* Event sender Init */
-       if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
+       ret = _bt_init_service_event_sender();
+       if (ret != BLUETOOTH_ERROR_NONE) {
                BT_ERR("Fail to init event sender");
-               return 0;
+               return ret;
        }
 
-       if (_bt_init_hf_local_term_event_sender() != BLUETOOTH_ERROR_NONE) {
+       ret = _bt_init_hf_local_term_event_sender();
+       if (ret != BLUETOOTH_ERROR_NONE) {
                BT_ERR("Fail to init core event sender");
-               return 0;
+               return ret;
        }
 
-       if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
+       ret = _bt_service_register();
+       if (ret != BLUETOOTH_ERROR_NONE) {
                BT_ERR("Fail to register service");
-               return 0;
+               return ret;
        }
 
-       if (_bt_service_adapter_le_init() != BLUETOOTH_ERROR_NONE) {
+       ret = _bt_service_adapter_le_init();
+       if (ret != BLUETOOTH_ERROR_NONE) {
                BT_ERR("Fail to init le");
-               return 0;
+               return ret;
        }
 
        _bt_init_request_id();
 
        _bt_init_request_list();
 
-#ifndef GATT_NO_RELAY
-       _bt_init_gatt_client_senders();
-#endif
+       is_initialized = TRUE;
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int main(void)
+{
+       struct sigaction sa;
+       BT_INFO_C("### Starting the bt-service daemon");
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_sigaction = __bt_sigterm_handler;
+       sa.sa_flags = SA_SIGINFO;
+       sigaction(SIGINT, &sa, NULL);
+       sigaction(SIGTERM, &sa, NULL);
+
+       if (_bt_service_initialize() != BLUETOOTH_ERROR_NONE)
+               return 0;
 
        g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
 
@@ -271,14 +276,19 @@ int main(void)
                return 0;
        }
 
+       bluetooth_plugin_init();
+
+       g_log_set_default_handler(__on_log_glib, NULL);
+
        main_loop = g_main_loop_new(NULL, FALSE);
 
        g_main_loop_run(main_loop);
        BT_DBG("g_main_loop_quit called!");
 
-       if (main_loop != NULL) {
+       bluetooth_plugin_deinit();
+
+       if (main_loop != NULL)
                g_main_loop_unref(main_loop);
-       }
 
        if (terminated == FALSE)
                __bt_release_service();