Fix the instance id's miss-matching issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / bt-service-main.c
index fb1d227..34394c1 100644 (file)
@@ -19,6 +19,9 @@
 #include <dlog.h>
 #include <string.h>
 #include <vconf.h>
+#ifdef TIZEN_FEATURE_BT_FACTORY_MODE
+#include <bincfg.h>
+#endif
 
 #include <bundle.h>
 #include <eventsystem.h>
@@ -54,9 +57,6 @@ static void __on_log_glib(const gchar *log_domain, GLogLevelFlags log_level,
 
 static void __bt_release_service(void)
 {
-#if TODO_40
-       _bt_service_unregister_vconf_handler();
-#endif
        _bt_service_le_deinit();
        _bt_deinit_hf_local_term_event_sender();
        _bt_deinit_service_event_sender();
@@ -107,7 +107,7 @@ gboolean _bt_terminate_service(gpointer user_data)
        return FALSE;
 }
 
-gboolean _bt_reliable_terminate_service(gpointer user_data)
+static gboolean __bt_main_loop_quit_idle_cb(gpointer user_data)
 {
        _bt_deinit_proxys();
 
@@ -117,20 +117,48 @@ gboolean _bt_reliable_terminate_service(gpointer user_data)
 
        _bt_deinit_service_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 {
+               BT_ERR("main_loop == NULL");
                exit(0);
+       }
+
+       return FALSE;
+}
+
+gboolean _bt_reliable_terminate_service(gpointer user_data)
+{
+       _bt_service_unregister();
+
+       /* Handle remaining pending requests finally by using g_idle_add()
+          again without well-known name. */
+       g_idle_add((GSourceFunc)__bt_main_loop_quit_idle_cb, NULL);
 
        return FALSE;
 }
 
+static void __bt_check_factory_mode(void)
+{
+#ifdef TIZEN_FEATURE_BT_FACTORY_MODE
+       int factorymode_status = 0;
+       /* BT should not be activated automatically in factory binary */
+       /* factorymode_status :: 0 - normal binary, 1 - factory binary */
+       factorymode_status = bincfg_is_factory_binary();
+       if (factorymode_status != 0) {
+               BT_INFO("factory mode [%d]", factorymode_status);
+               if(vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
+                       BT_ERR("Set vconf failed");
+               if(vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
+                       BT_ERR("Set vconf failed");
+       }
+#endif
+}
+
 static gboolean __bt_check_bt_service(void *data)
 {
        bt_status_t status = BT_DEACTIVATED;
@@ -157,6 +185,9 @@ static gboolean __bt_check_bt_service(void *data)
                if (_bt_get_enable_timer_id() == 0)
 #endif
                        _bt_enable_adapter();
+       } else if (!headed_plugin_info->plugin_headed_enabled) {
+               BT_DBG("Enable adapter if headless device");
+               _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");
@@ -175,6 +206,9 @@ static gboolean __bt_check_bt_service(void *data)
                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");
 
+               BT_DBG("headed VCONF BT_STATUS:[%d] LE_STATUS:[%d] BT_OFF_DUE_TO_TIMEOUT:[%d]",
+               bt_status, bt_le_status, bt_off_due_to_timeout);
+
                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 */
@@ -197,10 +231,10 @@ static gboolean __bt_check_bt_service(void *data)
                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.");
 
-#if TODO_40 /* Need to enable this function */
+
                        /* Enable the BT LE */
                        _bt_enable_adapter_le();
-#endif
+
                } else {
                        status = _bt_adapter_get_status();
                        le_status = _bt_adapter_get_le_status();
@@ -228,9 +262,8 @@ int _bt_service_initialize(void)
                return EXIT_FAILURE;
        }
 
-#if TODO_40 /* Need to add this function */
-       _bt_service_register_vconf_handler();
-#endif
+       /* Flight mode handler implemented in HAL */
+
        /* Event sender Init */
        ret = _bt_init_service_event_sender();
        if (ret != BLUETOOTH_ERROR_NONE) {
@@ -296,12 +329,19 @@ int main(void)
        struct sigaction sa;
        BT_INFO_C("### Starting the bt-service daemon");
 
+       if (!TIZEN_FEATURE_BT_SUPPORTED) {
+               BT_INFO_C("BT feature is not supported, terminate bt-service");
+               return 0;
+       }
+
        memset(&sa, 0, sizeof(sa));
        sa.sa_sigaction = __bt_sigterm_handler;
        sa.sa_flags = SA_SIGINFO;
        sigaction(SIGINT, &sa, NULL);
        sigaction(SIGTERM, &sa, NULL);
 
+       __bt_check_factory_mode();
+
        if (_bt_service_initialize() != BLUETOOTH_ERROR_NONE)
                return 0;
 
@@ -323,6 +363,8 @@ int main(void)
 
        bluetooth_plugin_deinit();
 
+       _bt_service_unref_connection();
+
        if (main_loop != NULL)
                g_main_loop_unref(main_loop);