OSDN Git Service

android: Convert bt_adapter_new into a bt_adapter_init function
authorJohan Hedberg <johan.hedberg@intel.com>
Fri, 18 Oct 2013 11:11:11 +0000 (14:11 +0300)
committerJohan Hedberg <johan.hedberg@intel.com>
Fri, 18 Oct 2013 11:30:14 +0000 (14:30 +0300)
android/adapter.c
android/adapter.h
android/main.c

index c3bfa21..4009f4d 100644 (file)
@@ -21,6 +21,8 @@
  *
  */
 
+#include <errno.h>
+
 #include "lib/bluetooth.h"
 #include "src/shared/mgmt.h"
 #include "lib/mgmt.h"
 #include "adapter.h"
 
 struct bt_adapter {
+       uint16_t index;
        struct mgmt *mgmt;
+
+       bt_adapter_ready ready;
+
        bdaddr_t bdaddr;
        uint32_t dev_class;
 
@@ -38,12 +44,29 @@ struct bt_adapter {
        uint32_t current_settings;
 };
 
-extern struct bt_adapter *default_adapter;
+static struct bt_adapter *default_adapter;
 
 static void load_link_keys_complete(uint8_t status, uint16_t length,
                                        const void *param, void *user_data)
 {
+       struct bt_adapter *adapter = user_data;
+       int err;
+
+       if (status) {
+               error("Failed to load link keys for index %u: %s (0x%02x)",
+                       adapter->index, mgmt_errstr(status), status);
+               err = -EIO;
+               goto failed;
+       }
+
        DBG("status %u", status);
+
+       default_adapter = adapter;
+       adapter->ready(adapter, 0);
+       return;
+
+failed:
+       adapter->ready(NULL, err);
 }
 
 static void load_link_keys(struct bt_adapter *adapter, GSList *keys)
@@ -72,22 +95,26 @@ static void read_info_complete(uint8_t status, uint16_t length, const void *para
 {
        struct bt_adapter *adapter = user_data;
        const struct mgmt_rp_read_info *rp = param;
+       int err;
 
        DBG("");
 
        if (status) {
                error("Failed to read info for index %u: %s (0x%02x)",
-                       0, mgmt_errstr(status), status);
+                       adapter->index, mgmt_errstr(status), status);
+               err = -EIO;
                goto failed;
        }
 
        if (length < sizeof(*rp)) {
                error("Too small read info complete response");
+               err = -EIO;
                goto failed;
        }
 
        if (!bacmp(&rp->bdaddr, BDADDR_ANY)) {
                error("No Bluetooth address");
+               err = -ENODEV;
                goto failed;
        }
 
@@ -107,21 +134,24 @@ static void read_info_complete(uint8_t status, uint16_t length, const void *para
        return;
 
 failed:
-       default_adapter = NULL;
+       adapter->ready(NULL, err);
 }
 
-struct bt_adapter *bt_adapter_new(uint16_t index, struct mgmt *mgmt_if)
+bool bt_adapter_init(uint16_t index, struct mgmt *mgmt_if,
+                                               bt_adapter_ready ready)
 {
        struct bt_adapter *adapter;
 
        adapter = g_new0(struct bt_adapter, 1);
 
        adapter->mgmt = mgmt_ref(mgmt_if);
+       adapter->index = index;
+       adapter->ready = ready;
 
        if (mgmt_send(mgmt_if, MGMT_OP_READ_INFO, index, 0, NULL,
                                read_info_complete, adapter, NULL) > 0) {
                mgmt_unref(mgmt_if);
-               return NULL;
+               return false;
        }
 
        return adapter;
index 6ab0cd6..d8ad191 100644 (file)
@@ -30,4 +30,7 @@
 
 struct bt_adapter;
 
-struct bt_adapter *bt_adapter_new(uint16_t index, struct mgmt *mgmt_if);
+typedef void (*bt_adapter_ready)(struct bt_adapter *adapter, int err);
+
+bool bt_adapter_init(uint16_t index, struct mgmt *mgmt_if,
+                                               bt_adapter_ready func);
index 87e1036..256b83b 100644 (file)
@@ -52,7 +52,7 @@ static struct mgmt *mgmt_if = NULL;
 static uint8_t mgmt_version = 0;
 static uint8_t mgmt_revision = 0;
 
-struct bt_adapter *default_adapter = NULL;
+static uint16_t adapter_index = MGMT_INDEX_NONE;
 
 static gboolean quit_eventloop(gpointer user_data)
 {
@@ -81,17 +81,28 @@ static GOptionEntry options[] = {
        { NULL }
 };
 
+static void adapter_ready(struct bt_adapter *adapter, int err)
+{
+       if (err) {
+               error("Adapter initialization failed: %s", strerror(err));
+               exit(EXIT_FAILURE);
+       }
+
+       info("Adapter initialized");
+}
+
 static void mgmt_index_added_event(uint16_t index, uint16_t length,
                                        const void *param, void *user_data)
 {
        DBG("index %u", index);
 
-       if (default_adapter) {
+       if (adapter_index != MGMT_INDEX_NONE) {
                DBG("skip event for index %u", index);
                return;
        }
 
-       default_adapter = bt_adapter_new(index, mgmt_if);
+       adapter_index = index;
+        bt_adapter_init(index, mgmt_if, adapter_ready);
 
        error("Failed to read adapter info for index %u", index);
 }