Using connman enable/disable BT to replace old enable/disable 39/29639/1
authorwu zheng <wu.zheng@intel.com>
Fri, 31 Oct 2014 05:28:49 +0000 (13:28 +0800)
committerwu zheng <wu.zheng@intel.com>
Fri, 31 Oct 2014 05:28:49 +0000 (13:28 +0800)
According to the requirement of Tizen,
connman need to control the BT enable/disable.

Added the return value too.

Change-Id: I956029c1a56a2e6f5b25d19c153af1144824bfd1
Signed-off-by: Wu Zheng <wu.zheng@intel.com>
include/bluez.h
lib/bluez.c
src/manager.c

index 6953a51..f9e7296 100644 (file)
@@ -154,7 +154,7 @@ void bluez_adapter_remove_device(
 int bluez_adapter_get_property_powered(
                                struct _bluez_adapter *adapter,
                                gboolean *powered);
-void bluez_adapter_set_powered(struct _bluez_adapter *adapter,
+int bluez_adapter_set_powered(struct _bluez_adapter *adapter,
                                gboolean powered);
 typedef void (*bluez_adapter_powered_cb_t)(
                                bluez_adapter_t *adapter,
index 9b0d625..7d07424 100644 (file)
@@ -20,6 +20,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <gio/gio.h>
+#ifdef TIZEN
+#include <tizen_error.h>
+#endif
 #include "common.h"
 #include "bluez.h"
 
 #define GATT_CHR_IFACE "org.bluez.GattCharacteristic1"
 #define GATT_DESCRIPTOR_IFACE "org.bluez.GattDescriptor1"
 
+#define CONNMAN_DBUS_NAME "net.connman"
+#define CONNMAN_BLUETOOTH_TECHNOLOGY_PATH "/net/connman/technology/bluetooth"
+#define CONNMAN_BLUETOTOH_TECHNOLOGY_INTERFACE "net.connman.Technology"
+
 #define BT_MEDIA_OBJECT_PATH "/Musicplayer"
 #define MEDIA_PLAYER_INTERFACE  "org.mpris.MediaPlayer2.Player"
 
+typedef enum {
+#ifdef TIZEN
+       BT_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful*/
+       BT_ERROR_CANCELLED = TIZEN_ERROR_CANCELED, /**< Operation cancelled */
+       BT_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
+       BT_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
+       BT_ERROR_RESOURCE_BUSY = TIZEN_ERROR_RESOURCE_BUSY, /**< Device or resource busy */
+       BT_ERROR_TIMED_OUT = TIZEN_ERROR_TIMED_OUT, /**< Timeout error */
+       BT_ERROR_NOW_IN_PROGRESS = TIZEN_ERROR_NOW_IN_PROGRESS, /**< Operation now in progress */
+       BT_ERROR_NOT_INITIALIZED = TIZEN_ERROR_NETWORK_CLASS|0x0101, /**< Local adapter not initialized */
+       BT_ERROR_NOT_ENABLED = TIZEN_ERROR_NETWORK_CLASS|0x0102, /**< Local adapter not enabled */
+       BT_ERROR_ALREADY_DONE = TIZEN_ERROR_NETWORK_CLASS|0x0103, /**< Operation already done  */
+       BT_ERROR_OPERATION_FAILED = TIZEN_ERROR_NETWORK_CLASS|0x0104, /**< Operation failed */
+       BT_ERROR_NOT_IN_PROGRESS = TIZEN_ERROR_NETWORK_CLASS|0x0105, /**< Operation not in progress */
+       BT_ERROR_REMOTE_DEVICE_NOT_BONDED = TIZEN_ERROR_NETWORK_CLASS|0x0106, /**< Remote device not bonded */
+       BT_ERROR_AUTH_REJECTED = TIZEN_ERROR_NETWORK_CLASS|0x0107, /**< Authentication rejected */
+       BT_ERROR_AUTH_FAILED = TIZEN_ERROR_NETWORK_CLASS|0x0108, /**< Authentication failed */
+       BT_ERROR_REMOTE_DEVICE_NOT_FOUND = TIZEN_ERROR_NETWORK_CLASS|0x0109, /**< Remote device not found */
+       BT_ERROR_SERVICE_SEARCH_FAILED = TIZEN_ERROR_NETWORK_CLASS|0x010A, /**< Service search failed */
+       BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED = TIZEN_ERROR_NETWORK_CLASS|0x010B, /**< Remote device is not connected */
+       BT_ERROR_ADAPTER_NOT_FOUND = TIZEN_ERROR_NETWORK_CLASS|0x010C, /**< Adapter not found */
+#else
+       BT_ERROR_NONE = 0x00, /**< Successful*/
+       BT_ERROR_CANCELLED, /**< Operation cancelled */
+       BT_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
+       BT_ERROR_OUT_OF_MEMORY, /**< Out of memory */
+       BT_ERROR_RESOURCE_BUSY, /**< Device or resource busy */
+       BT_ERROR_TIMED_OUT, /**< Timeout error */
+       BT_ERROR_NOW_IN_PROGRESS, /**< Operation now in progress */
+       BT_ERROR_NOT_INITIALIZED, /**< Local adapter not initialized */
+       BT_ERROR_NOT_ENABLED, /**< Local adapter not enabled */
+       BT_ERROR_ALREADY_DONE, /**< Operation already done  */
+       BT_ERROR_OPERATION_FAILED, /**< Operation failed */
+       BT_ERROR_NOT_IN_PROGRESS, /**< Operation not in progress */
+       BT_ERROR_REMOTE_DEVICE_NOT_BONDED, /**< Remote device not bonded */
+       BT_ERROR_AUTH_REJECTED, /**< Authentication rejected */
+       BT_ERROR_AUTH_FAILED, /**< Authentication failed */
+       BT_ERROR_REMOTE_DEVICE_NOT_FOUND, /**< Remote device not found */
+       BT_ERROR_SERVICE_SEARCH_FAILED, /**< Service search failed */
+       BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED, /**< Remote device is not connected */
+       BT_ERROR_ADAPTER_NOT_FOUND, /**< Adapter not found */
+#endif
+} bt_error_e;
+
+#define BT_SUCCESS BT_ERROR_NONE
+
 GDBusObjectManager *object_manager = NULL;
 
 struct player_settinngs_t {
@@ -3173,19 +3226,36 @@ void bluez_adapter_set_alias(struct _bluez_adapter *adapter,
                                        simple_reply_callback, NULL);
 }
 
-void bluez_adapter_set_powered(struct _bluez_adapter *adapter,
+int bluez_adapter_set_powered(struct _bluez_adapter *adapter,
                                gboolean powered)
 {
-
+       GError *error = NULL;
+       GDBusConnection *connection;
        GVariant *val = g_variant_new("b", powered);
-       GVariant *parameters = g_variant_new("(ssv)",
-                       ADAPTER_INTERFACE, "Powered", val);
+       GVariant *parameters = g_variant_new("(sv)",
+                                       "Powered", val);
 
        DBG("powered %d", powered);
 
-       g_dbus_proxy_call(adapter->parent->properties_proxy,
-                                       "Set", parameters, 0,
-                                       -1, NULL, simple_reply_callback, NULL);
+       connection = get_system_lib_dbus_connect();
+
+       if (connection == NULL)
+               return BT_ERROR_OPERATION_FAILED;
+
+       g_dbus_connection_call_sync(connection, CONNMAN_DBUS_NAME,
+                               CONNMAN_BLUETOOTH_TECHNOLOGY_PATH,
+                               CONNMAN_BLUETOTOH_TECHNOLOGY_INTERFACE,
+                               "SetProperty",
+                               parameters,
+                               NULL, 0, -1, NULL, &error);
+
+       if (error) {
+               DBG("error %s", error->message);
+               g_error_free(error);
+               return BT_ERROR_OPERATION_FAILED;
+       }
+
+       return BT_SUCCESS;
 }
 
 void bluez_adapter_set_discoverable(struct _bluez_adapter *adapter,
index 5aa2667..d5c988f 100644 (file)
@@ -36,6 +36,8 @@
 static GDBusObjectManagerServer *manager_server;
 GDBusObjectSkeleton *bt_object;
 
+static gboolean bt_activate_timeout(gpointer user_data);
+
 G_DEFINE_TYPE(CommsManagerSkeleton, comms_manager_skeleton,
                                G_TYPE_DBUS_INTERFACE_SKELETON);
 
@@ -325,6 +327,7 @@ static void bt_adapter_set_enable(bluez_adapter_t *adapter, void *user_data)
 {
        struct bt_activate_data *adapter_activate_data = user_data;
        gboolean powered;
+       int ret = 0;
 
        bluez_adapter_set_powered_changed_cb(default_adapter,
                                                adapter_powered_changed,
@@ -332,14 +335,20 @@ static void bt_adapter_set_enable(bluez_adapter_t *adapter, void *user_data)
 
        bluez_adapter_get_property_powered(default_adapter, &powered);
        if (powered == FALSE)
-               bluez_adapter_set_powered(default_adapter, TRUE);
+               ret = bluez_adapter_set_powered(default_adapter, TRUE);
 
-       g_dbus_method_invocation_return_value(
+       if (ret) {
+               bt_activate_timeout(adapter_activate_data);
+               adapter_activate_data = NULL;
+       } else {
+               g_dbus_method_invocation_return_value(
                        adapter_activate_data->invocation, NULL);
-       adapter_activate_data->invocation = NULL;
+               adapter_activate_data->invocation = NULL;
 
-       if (powered == TRUE)
-               adapter_powered_on(adapter_activate_data->skeleton);
+               if (powered == TRUE)
+                       adapter_powered_on
+                               (adapter_activate_data->skeleton);
+       }
 }
 
 static void discoverable_changed(bluez_adapter_t *adapter,
@@ -418,15 +427,18 @@ static void adapter_added_cb(bluez_adapter_t *adapter, void *user_data)
                g_source_remove(bt_activate_timeout_id);
                bt_activate_timeout_id = 0;
        }
+
+       if (data)
+               g_free(data);
 }
 
-gboolean bt_activate_timeout(gpointer user_data)
+static gboolean bt_activate_timeout(gpointer user_data)
 {
        struct bt_activate_data *data = user_data;
 
        DBG("");
 
-       comms_error_failed(data->invocation, "Time out");
+       comms_error_failed(data->invocation, "Activate failed");
 
        bluez_adapter_unset_adapter_added();
 
@@ -527,6 +539,7 @@ static void handle_disable_bluetooth_service(GDBusConnection *connection,
 {
        CommsManagerSkeleton *skeleton = user_data;
        gboolean state;
+       int ret;
 
        DBG("");
        state = get_bluetooth_activating(skeleton);
@@ -542,11 +555,15 @@ static void handle_disable_bluetooth_service(GDBusConnection *connection,
                return;
        }
 
-       bluez_adapter_set_powered(default_adapter, FALSE);
+       ret = bluez_adapter_set_powered(default_adapter, FALSE);
 
        default_adapter = NULL;
 
-       g_dbus_method_invocation_return_value(invocation, NULL);
+       if (ret)
+               comms_error_failed(invocation,
+                                               "set powered off");
+       else
+               g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
 static void handle_set_default_adapter(GDBusConnection *connection,