keep current bluetooth state when starting bt-service 70/30970/2
authorCorentin Lecouvey <corentin.lecouvey@open.eurogiciel.org>
Thu, 27 Nov 2014 16:10:04 +0000 (17:10 +0100)
committerCorentin Lecouvey <corentin.lecouvey@open.eurogiciel.org>
Tue, 9 Dec 2014 09:15:52 +0000 (01:15 -0800)
Previsouly when bt-service was started, it always tries to enable the bluetooth.
Therefore when the bluetooth is already enabled when starting bt-service,
it was not clean.

This patch allows to keep consistency between bluez and tizen bluetooth states.
By this way, if bluetooth is enabled when starting bt-service, bt-service just set vconf
and other variables. Following same logic, if bluetooth is down when starting
bt-service, we keep it down and align vconf status.

Change-Id: Iaf9170a739aa4a4c0ac37f1d98caafea1df50643
Signed-off-by: Corentin Lecouvey <corentin.lecouvey@open.eurogiciel.org>
bt-service/bt-service-adapter.c
bt-service/bt-service-event-receiver.c
bt-service/bt-service-main.c
bt-service/include/bt-service-adapter.h

index 3fa6644..e2a028a 100644 (file)
@@ -655,16 +655,16 @@ static void __bt_set_local_name(void)
        free(phone_name);
 }
 
-static int __bt_set_enabled(void)
+void _bt_set_enabled(void)
 {
-       int enabled = FALSE;
        int result = BLUETOOTH_ERROR_NONE;
 
-       _bt_check_adapter(&enabled);
+       _bt_adapter_set_status(BT_ACTIVATED);
 
-       if (enabled == FALSE) {
-               BT_ERR("Bluetoothd is not running");
-               return BLUETOOTH_ERROR_INTERNAL;
+       // register agent if it was not previously done.
+       if (!adapter_agent) {
+               BT_DBG("");
+               _bt_handle_adapter_added();
        }
 
        __bt_set_visible_mode();
@@ -681,12 +681,14 @@ static int __bt_set_enabled(void)
        /* Send enabled event to API */
        _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
                                DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
-
-       return BLUETOOTH_ERROR_NONE;
 }
 
-static void __bt_set_disabled(int result)
+void _bt_set_disabled(void)
 {
+       int result = BLUETOOTH_ERROR_NONE;
+
+       _bt_adapter_set_status(BT_DEACTIVATED);
+
        /* Update Bluetooth Status to notify other modules */
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
                BT_ERR("Set vconf failed\n");
@@ -714,11 +716,13 @@ void _bt_handle_flight_mode_noti(void)
 
 void _bt_handle_adapter_added(void)
 {
-        adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
-        if (!adapter_agent) {
+       BT_DBG("");
+
+       adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
+       if (!adapter_agent) {
                BT_ERR("Fail to register agent");
                return;
-        }
+       }
 
 #ifdef __TIZEN_MOBILE__
        if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
@@ -738,15 +742,11 @@ void _bt_handle_adapter_added(void)
 
        vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
                        __bt_flight_mode_cb, NULL);
-
-       __bt_set_enabled();
-
-       _bt_adapter_set_status(BT_ACTIVATED);
 }
 
 void _bt_handle_adapter_removed(void)
 {
-       _bt_adapter_set_status(BT_DEACTIVATED);
+       BT_DBG("");
 
        vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
                                (vconf_callback_fn)__bt_phone_name_changed_cb);
@@ -754,33 +754,31 @@ void _bt_handle_adapter_removed(void)
        _bt_destroy_agent(adapter_agent);
        adapter_agent = NULL;
 
-       __bt_set_disabled(BLUETOOTH_ERROR_NONE);
-
        _bt_terminate_service(NULL);
 }
 
 DBusGProxy *_bt_init_core_proxy(void)
 {
-       DBusGProxy *proxy;
+       DBusGProxy *proxy;
        DBusGConnection *conn;
 
        conn = _bt_get_system_gconn();
        if (!conn)
                return NULL;
 
-       proxy = dbus_g_proxy_new_for_name(conn, BT_CORE_NAME,
+       proxy = dbus_g_proxy_new_for_name(conn, BT_CORE_NAME,
                        BT_CORE_PATH, BT_CORE_INTERFACE);
        if (!proxy)
                return NULL;
 
-       core_proxy = proxy;
+       core_proxy = proxy;
 
-       return proxy;
+       return proxy;
 }
 
 static DBusGProxy *__bt_get_core_proxy(void)
 {
-       return (core_proxy) ? core_proxy : _bt_init_core_proxy();
+       return (core_proxy) ? core_proxy : _bt_init_core_proxy();
 }
 
 int _bt_enable_adapter(void)
@@ -825,7 +823,7 @@ int _bt_enable_adapter(void)
                }
 
                /* Display notification */
-               status_message_post(BT_STR_NOT_SUPPORT);
+               notification_status_message_post(BT_STR_NOT_SUPPORT);
 
                /* Terminate myself */
                g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
@@ -862,7 +860,7 @@ int _bt_disable_adapter(void)
                BT_ERR("Bt core call failed");
                _bt_adapter_set_status(BT_ACTIVATED);
                return BLUETOOTH_ERROR_INTERNAL;
-       }
+       }
 
        return BLUETOOTH_ERROR_NONE;
 }
index e175a40..86d33ba 100644 (file)
@@ -704,25 +704,17 @@ void __bt_adapter_property_changed_event(DBusMessageIter *msg_iter, const char *
                                DBUS_TYPE_INVALID);
        } else if (strcasecmp(property, "Powered") == 0) {
                gboolean power = FALSE;
-               int power_event;
 
                dbus_message_iter_recurse(&dict_iter, &value_iter);
                dbus_message_iter_get_basic(&value_iter, &power);
 
-               if (power == TRUE) {
-                       power_event = BLUETOOTH_EVENT_ENABLED;
-                       _bt_adapter_set_status(BT_ACTIVATED);
-               } else {
-                       power_event = BLUETOOTH_EVENT_DISABLED;
-                       _bt_adapter_set_status(BT_DEACTIVATED);
-               }
+               BT_DBG("power state changed: %d", power);
+
+               if (power == TRUE)
+                       _bt_set_enabled();
+               else
+                       _bt_set_disabled();
 
-               BT_ERR("send power state: %d", power);
-               /* Send event to application */
-               _bt_send_event(BT_ADAPTER_EVENT,
-                                       power_event,
-                                       DBUS_TYPE_INT32, &result,
-                                       DBUS_TYPE_INVALID);
        } else if (strcasecmp(property, "Discoverable") == 0) {
                gboolean discoverable = FALSE;
 
index 821d96d..5841c39 100644 (file)
@@ -83,43 +83,44 @@ gboolean _bt_terminate_service(gpointer user_data)
        return FALSE;
 }
 
+/* align bt-service state (adapter_status and vconf keys) on BlueZ state */
 static gboolean __bt_check_bt_service(void *data)
 {
        int bt_status = VCONFKEY_BT_STATUS_OFF;
        int flight_mode_deactivation = 0;
 
+       int adapter_enabled = 0;
+
        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(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
-                       BT_ERR("Fail to get the flight_mode_deactivated value");
+               BT_ERR("Fail to get the flight_mode_deactivated value");
 
-       if (bt_status != VCONFKEY_BT_STATUS_OFF) {
-               BT_DBG("Previous session was enabled.");
+       _bt_check_adapter(&adapter_enabled);
 
-               /* Enable the BT */
-               _bt_enable_adapter();
-       } else if (bt_status == VCONFKEY_BT_STATUS_OFF &&
-                                       flight_mode_deactivation == 1) {
-               _bt_handle_flight_mode_noti();
-       } else {
-               bt_status_t status = _bt_adapter_get_status();
-               int adapter_enabled = 0;
+       BT_DBG("get bt adapter status: %d when starting bt-service ", adapter_enabled);
+
+       _bt_adapter_set_status(adapter_enabled);
 
-               _bt_check_adapter(&adapter_enabled);
+       if (adapter_enabled != bt_status) {
+               BT_DBG("align vconf bt status key with real bluetooth status");
+               if (vconf_set_int(VCONFKEY_BT_STATUS, adapter_enabled) != 0)
+                       BT_ERR("Set vconf key %s failed", VCONFKEY_BT_STATUS);
 
-               BT_DBG("State: %d", status);
-               BT_DBG("Adapter enabled: %d", adapter_enabled);
+               bt_status = adapter_enabled;
+       }
 
-               if (adapter_enabled == 1) {
-                       _bt_handle_adapter_added();
-                       return FALSE;
-               }
+       if (bt_status == VCONFKEY_BT_STATUS_OFF && flight_mode_deactivation == 1) {
+               BT_ERR("call _bt_handle_flight_mode_noti()");
+               _bt_handle_flight_mode_noti();
+               return FALSE;
+       }
 
-               if (status != BT_ACTIVATING && status != BT_ACTIVATED) {
-                       _bt_terminate_service(NULL);
-               }
+       if (adapter_enabled == TRUE) {
+               BT_DBG("");
+               _bt_handle_adapter_added();
        }
 
        return FALSE;
@@ -135,8 +136,6 @@ int main(void)
        sigaction(SIGINT, &sa, NULL);
        sigaction(SIGTERM, &sa, NULL);
 
-       g_type_init();
-
        if (perm_app_set_privilege("bluetooth-frwk-service", NULL, NULL) !=
                                                                PC_OPERATION_SUCCESS)
                BT_ERR("Failed to set app privilege.\n");
index 079e238..5d1275d 100644 (file)
@@ -47,6 +47,10 @@ void _bt_handle_adapter_removed(void);
 
 int _bt_check_adapter(int *status);
 
+void _bt_set_enabled(void);
+
+void _bt_set_disabled(void);
+
 void *_bt_get_adapter_agent(void);
 
 void _bt_set_discovery_status(gboolean mode);