use bluez dbus api to enable/disable adapter 69/28869/2
authorCorentin Lecouvey <corentin.lecouvey@open.eurogiciel.org>
Thu, 16 Oct 2014 13:36:24 +0000 (15:36 +0200)
committerCorentin Lecouvey <corentin.lecouvey@open.eurogiciel.org>
Mon, 20 Oct 2014 16:01:17 +0000 (18:01 +0200)
It was previously done invoking bluetooth-tools scripts.
For example, to disable adapter, one of the script kills bluez daemon process.
Now we just modify 'Powered' adapter property.

The patch also removes the timeout callback when enabling adapter since it can
disable adapter if the status is not correct. This adapter status is handled
by bluez4 adapter dbus api code which needs to be adapted.

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

index d1c75a9..d3b518f 100644 (file)
 #include <vconf.h>
 #include <vconf-keys.h>
 
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus.h>
+#include <glib.h>
+
 #include "bt_core.h"
 #include "bt-internal-types.h"
 
 static GMainLoop *main_loop = NULL;
+static DBusGConnection *conn = NULL;
 
 typedef enum {
        BT_DEACTIVATED,
@@ -109,6 +114,52 @@ static GError *bt_core_error(BtCoreError error, const char *err_msg)
        return g_error_new(BT_CORE_ERROR, error, err_msg, NULL);
 }
 
+static DBusGProxy *_bt_get_adapter_proxy(void)
+{
+       DBusGProxy *proxy;
+       char *adapter_path = NULL;
+
+       if (conn == NULL) {
+               conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
+               retv_if(conn == NULL, NULL);
+       }
+
+       proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
+                       BT_BLUEZ_HCI_PATH, BT_PROPERTIES_INTERFACE);
+       retv_if(proxy == NULL, NULL);
+
+       return proxy;
+}
+
+static int _bt_power_adapter(gboolean powered)
+{
+       GValue state = { 0 };
+       GError *error = NULL;
+       DBusGProxy *proxy;
+
+       proxy = _bt_get_adapter_proxy();
+       retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       g_value_init(&state, G_TYPE_BOOLEAN);
+       g_value_set_boolean(&state, powered);
+
+       BT_DBG("send power state: %d to bluez", powered);
+
+       dbus_g_proxy_call(proxy, "Set", &error,
+                               G_TYPE_STRING, BT_ADAPTER_INTERFACE,
+                               G_TYPE_STRING, "Powered",
+                               G_TYPE_VALUE, &state,
+                               G_TYPE_INVALID, G_TYPE_INVALID);
+
+       if (error != NULL) {
+               BT_ERR("Powered set err:[%s]", error->message);
+               g_error_free(error);
+               g_value_unset(&state);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+       return BLUETOOTH_ERROR_NONE;
+}
+
 static int __bt_enable_adapter(void)
 {
        int ret;
@@ -124,13 +175,7 @@ static int __bt_enable_adapter(void)
 
        __bt_core_set_status(BT_ACTIVATING);
 
-       ret = system("/usr/etc/bluetooth/bt-stack-up.sh &");
-       if (ret < 0) {
-               BT_DBG("running script failed");
-               ret = system("/usr/etc/bluetooth/bt-dev-end.sh &");
-               __bt_core_set_status(BT_DEACTIVATED);
-               return -1;
-       }
+       _bt_power_adapter(TRUE);
 
        return 0;
 }
@@ -157,14 +202,10 @@ static int __bt_disable_adapter(void)
 #endif
        __bt_core_set_status(BT_DEACTIVATING);
 
-       if (system("/usr/etc/bluetooth/bt-stack-down.sh &") < 0) {
-                       BT_DBG("running script failed");
-                       __bt_core_set_status(BT_ACTIVATED);
-                       return -1;
-       }
-#ifndef __TIZEN_MOBILE__
+       _bt_power_adapter(FALSE);
+
        __bt_core_terminate();
-#endif
+
        return 0;
 }
 
@@ -500,7 +541,6 @@ static void __bt_core_sigterm_handler(int signo)
 
 int main(void)
 {
-       DBusGConnection *conn = NULL;
        GError *error = NULL;
        BtCore *bt_core;
        DBusGProxy *dbus_proxy = NULL;
@@ -508,8 +548,6 @@ int main(void)
 
        BT_DBG("+");
 
-       g_type_init();
-
        conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
        if (error != NULL) {
                BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
index ae83e63..0aeaf64 100644 (file)
@@ -40,6 +40,19 @@ extern "C" {
 #define BT_ERR(fmt, args...) \
         SLOGE(fmt, ##args)
 
+#define retv_if(expr, val) \
+       do { \
+               if (expr) { \
+                       BT_ERR("(%s) return", #expr); \
+                       return (val); \
+               } \
+       } while (0)
+
+#define BT_BLUEZ_NAME "org.bluez"
+#define BT_BLUEZ_HCI_PATH "/org/bluez/hci0"
+#define BT_ADAPTER_INTERFACE "org.bluez.Adapter1"
+#define BT_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
+
 #define BT_CORE_NAME "org.projectx.bt_core"
 #define BT_CORE_PATH "/org/projectx/bt_core"
 
index dbf967d..523b266 100644 (file)
@@ -783,34 +783,6 @@ static DBusGProxy *__bt_get_core_proxy(void)
        return (core_proxy) ? core_proxy : _bt_init_core_proxy();
 }
 
-gboolean __bt_enable_timeout_cb(gpointer user_data)
-{
-       DBusGProxy *proxy;
-
-       retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
-
-       proxy = __bt_get_core_proxy();
-       if (!proxy)
-               return BLUETOOTH_ERROR_INTERNAL;
-
-       /* Clean up the process */
-       if (dbus_g_proxy_call(proxy, "DisableAdapter", NULL,
-                       G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
-                       BT_ERR("Bt core call failed");
-       }
-
-       __bt_adapter_set_status(BT_DEACTIVATED);
-
-       __bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
-
-       /* Display notification */
-       status_message_post(BT_STR_NOT_SUPPORT);
-
-       _bt_terminate_service(NULL);
-
-       return FALSE;
-}
-
 int _bt_enable_adapter(void)
 {
        DBusGProxy *proxy;
@@ -860,10 +832,6 @@ int _bt_enable_adapter(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       g_timeout_add(BT_ENABLE_TIMEOUT,
-                       (GSourceFunc)__bt_enable_timeout_cb,
-                       NULL);
-
        return BLUETOOTH_ERROR_NONE;
 }
 
index 47e369a..ac8ba56 100644 (file)
@@ -772,6 +772,22 @@ void __bt_adapter_property_changed_event(DBusMessageIter *msg_iter, const char *
                                DBUS_TYPE_INT32, &result,
                                DBUS_TYPE_STRING, &name,
                                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);
+
+               power_event = (power == TRUE) ? BLUETOOTH_EVENT_ENABLED :
+                               BLUETOOTH_EVENT_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;