2013-11-14 15:24:17

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v3 0/3] Fixes for incoming connection and bonding

V3:
Removed adapter bonding state
Added device list in order to track bonding state per device.
Fix for incoming legacy paring. We act here same as Bluedroid.

V2:
Bonding state added.

V1:
With those patches it is possible to bond from remote device and in addition
you will see the name of remote device on Android pairing request pop up.

Lukasz Rymanowski (3):
android: Update bond state on incoming bonding
android: Update bond state on auth and connect failed
android: Change TODO with explaining comment

android/bluetooth.c | 168 ++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 130 insertions(+), 38 deletions(-)

--
1.8.4



2013-11-14 15:24:20

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v3 3/3] android: Change TODO with explaining comment

---
android/bluetooth.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index b4e74e5..dfeb54b 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -598,7 +598,10 @@ static void send_ssp_request(const bdaddr_t *addr, uint8_t variant,
{
struct hal_ev_ssp_request ev;

- /* TODO name and CoD of remote devices should probably be cached */
+ /* It is ok to have empty name and CoD of remote devices here since
+ * those information has been already provided on device_connected event
+ * or during device scaning. Android will use that instead.
+ */
memset(&ev, 0, sizeof(ev));
bdaddr2android(addr, ev.bdaddr);
ev.pairing_variant = variant;
--
1.8.4


2013-11-14 15:24:19

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v3 2/3] android: Update bond state on auth and connect failed

---
android/bluetooth.c | 57 ++++++++++++++++++++++++++++++++---------------------
1 file changed, 35 insertions(+), 22 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index 3ecd300..b4e74e5 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -939,16 +939,51 @@ static void mgmt_device_disconnected_event(uint16_t index, uint16_t length,
HAL_EV_ACL_STATE_CHANGED, sizeof(hal_ev), &hal_ev, -1);
}

+static uint8_t status_mgmt2hal(uint8_t mgmt)
+{
+ switch (mgmt) {
+ case MGMT_STATUS_SUCCESS:
+ return HAL_STATUS_SUCCESS;
+ case MGMT_STATUS_NO_RESOURCES:
+ return HAL_STATUS_NOMEM;
+ case MGMT_STATUS_BUSY:
+ return HAL_STATUS_BUSY;
+ case MGMT_STATUS_NOT_SUPPORTED:
+ return HAL_STATUS_UNSUPPORTED;
+ case MGMT_STATUS_INVALID_PARAMS:
+ return HAL_STATUS_INVALID;
+ case MGMT_STATUS_AUTH_FAILED:
+ return HAL_STATUS_AUTH_FAILURE;
+ case MGMT_STATUS_NOT_CONNECTED:
+ return HAL_STATUS_REMOTE_DEVICE_DOWN;
+ default:
+ return HAL_STATUS_FAILED;
+ }
+}
+
static void mgmt_connect_failed_event(uint16_t index, uint16_t length,
const void *param, void *user_data)
{
+ const struct mgmt_ev_connect_failed *ev = param;
+
DBG("");
+
+ /* In case legacy pairing, we will get connect failed event
+ * in case e.g wrong PIN code entered. Let's check if device is
+ * bonding, if so update bond state */
+ set_device_bond_state(&ev->addr.bdaddr, status_mgmt2hal(ev->status),
+ HAL_BOND_STATE_NONE);
}

static void mgmt_auth_failed_event(uint16_t index, uint16_t length,
const void *param, void *user_data)
{
+ const struct mgmt_ev_auth_failed *ev = param;
+
DBG("");
+
+ set_device_bond_state(&ev->addr.bdaddr, status_mgmt2hal(ev->status),
+ HAL_BOND_STATE_NONE);
}

static void mgmt_device_unpaired_event(uint16_t index, uint16_t length,
@@ -1874,28 +1909,6 @@ static uint8_t set_property(void *buf, uint16_t len)
}
}

-static uint8_t status_mgmt2hal(uint8_t mgmt)
-{
- switch (mgmt) {
- case MGMT_STATUS_SUCCESS:
- return HAL_STATUS_SUCCESS;
- case MGMT_STATUS_NO_RESOURCES:
- return HAL_STATUS_NOMEM;
- case MGMT_STATUS_BUSY:
- return HAL_STATUS_BUSY;
- case MGMT_STATUS_NOT_SUPPORTED:
- return HAL_STATUS_UNSUPPORTED;
- case MGMT_STATUS_INVALID_PARAMS:
- return HAL_STATUS_INVALID;
- case MGMT_STATUS_AUTH_FAILED:
- return HAL_STATUS_AUTH_FAILURE;
- case MGMT_STATUS_NOT_CONNECTED:
- return HAL_STATUS_REMOTE_DEVICE_DOWN;
- default:
- return HAL_STATUS_FAILED;
- }
-}
-
static void pair_device_complete(uint8_t status, uint16_t length,
const void *param, void *user_data)
{
--
1.8.4


2013-11-14 15:24:18

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v3 1/3] android: Update bond state on incoming bonding

Before sending any ssp request or pin code request up to HAL library we
need to send bond state change with bonding state. Otherwise incoming
bonding is not correctly handled by Bluetooth.apk.
In this patch also device list has been added in order to e.g track
bonding state.

Note: For incoming legacy paring there is need to send
HAL_EVE_REMOTE_DEVICE_PROPS before HAL_EV_PIN_REQUEST.
It is because Android will crash due to bug in pinRequestCallback
function in java. Android checks if device is already in HashMap and if
not then creates device, but forget to use that one, but instead do
operations on NULL. By sending HAL_BOND_STATE_BONDING event it works
better but we have race issue. It is because new device is added to
HashMap not in callback context but later after BONDING msg will be
received by BondStateMachine. If it happens before pin_request_cb hits
java then we are fine, otherwise not. So for that reason we send
HAL_EV_REMOTE_DEVICE_PROPS so in the java handler class new device will
be added to HashMap in the callback context.

In ssp case we don't have this problem as we send device found once acl
is created.
---
android/bluetooth.c | 106 ++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 91 insertions(+), 15 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index 39589fa..3ecd300 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -62,6 +62,8 @@ static uint16_t option_index = MGMT_INDEX_NONE;

static int notification_sk = -1;

+#define BASELEN_REMOTE_DEV_PROP (sizeof(struct hal_ev_remote_device_props) \
+ + sizeof(struct hal_property))
/* This list contains addresses which are asked for records */
static GSList *browse_reqs;

@@ -91,6 +93,11 @@ static struct {
.uuids = NULL,
};

+struct device {
+ bdaddr_t bdaddr;
+ int bond_state;
+};
+
struct browse_req {
bdaddr_t bdaddr;
GSList *uuids;
@@ -106,6 +113,7 @@ static const uint16_t uuid_list[] = {
};

static GSList *found_devices = NULL;
+static GSList *devices = NULL;

static void adapter_name_changed(const uint8_t *name)
{
@@ -301,6 +309,14 @@ static void store_link_key(const bdaddr_t *dst, const uint8_t *key,

}

+static int bdaddr_cmp(gconstpointer a, gconstpointer b)
+{
+ const bdaddr_t *bda = a;
+ const bdaddr_t *bdb = b;
+
+ return bacmp(bdb, bda);
+}
+
static void send_bond_state_change(const bdaddr_t *addr, uint8_t status,
uint8_t state)
{
@@ -314,6 +330,29 @@ static void send_bond_state_change(const bdaddr_t *addr, uint8_t status,
HAL_EV_BOND_STATE_CHANGED, sizeof(ev), &ev, -1);
}

+static void set_device_bond_state(const bdaddr_t *addr, uint8_t status,
+ int state) {
+
+ struct device *dev = NULL;
+ GSList *l;
+
+ l = g_slist_find_custom(devices, addr, bdaddr_cmp);
+ if (l)
+ dev = l->data;
+
+ if (!dev) {
+ dev = g_new0( struct device, 1);
+ bacpy(&dev->bdaddr, addr);
+ dev->bond_state = HAL_BOND_STATE_NONE;
+ devices = g_slist_prepend(devices, dev);
+ }
+
+ if (dev->bond_state != state) {
+ dev->bond_state = state;
+ send_bond_state_change(&dev->bdaddr, status, state);
+ }
+}
+
static void browse_req_free(struct browse_req *req)
{
g_slist_free_full(req->uuids, g_free);
@@ -497,12 +536,32 @@ static void new_link_key_callback(uint16_t index, uint16_t length,
key->pin_len);
}

- send_bond_state_change(&addr->bdaddr, HAL_STATUS_SUCCESS,
+ set_device_bond_state(&addr->bdaddr, HAL_STATUS_SUCCESS,
HAL_BOND_STATE_BONDED);

browse_remote_sdp(&addr->bdaddr);
}

+static void send_remote_device_name_prop(const bdaddr_t *bdaddr, char *name)
+{
+ struct hal_ev_remote_device_props *ev;
+ uint8_t buf[BASELEN_REMOTE_DEV_PROP + strlen(name)];
+
+ ev = (void *) buf;
+ memset(buf, 0, sizeof(buf));
+
+ ev->status = HAL_STATUS_SUCCESS;
+ bdaddr2android(bdaddr, ev->bdaddr);
+ ev->num_props = 1;
+ ev->props[0].type = HAL_PROP_DEVICE_NAME;
+ ev->props[0].len = strlen(name);
+ memcpy(&ev->props[0].val, name, strlen(name));
+
+ ipc_send(notification_sk, HAL_SERVICE_ID_BLUETOOTH,
+ HAL_EV_REMOTE_DEVICE_PROPS, sizeof(buf), ev, -1);
+}
+
static void pin_code_request_callback(uint16_t index, uint16_t length,
const void *param, void *user_data)
{
@@ -517,6 +576,13 @@ static void pin_code_request_callback(uint16_t index, uint16_t length,

ba2str(&ev->addr.bdaddr, dst);

+ /* Workaround for Android Bluetooth.apk issue: send remote
+ * device property. Lets use address as a name for now */
+ send_remote_device_name_prop(&ev->addr.bdaddr, dst);
+
+ set_device_bond_state(&ev->addr.bdaddr, HAL_STATUS_SUCCESS,
+ HAL_BOND_STATE_BONDING);
+
DBG("%s type %u secure %u", dst, ev->addr.type, ev->secure);

/* TODO name and CoD of remote devices should probably be cached */
@@ -556,6 +622,9 @@ static void user_confirm_request_callback(uint16_t index, uint16_t length,
ba2str(&ev->addr.bdaddr, dst);
DBG("%s confirm_hint %u", dst, ev->confirm_hint);

+ set_device_bond_state(&ev->addr.bdaddr, HAL_STATUS_SUCCESS,
+ HAL_BOND_STATE_BONDING);
+
if (ev->confirm_hint)
send_ssp_request(&ev->addr.bdaddr, HAL_SSP_VARIANT_CONSENT, 0);
else
@@ -577,6 +646,9 @@ static void user_passkey_request_callback(uint16_t index, uint16_t length,
ba2str(&ev->addr.bdaddr, dst);
DBG("%s", dst);

+ set_device_bond_state(&ev->addr.bdaddr, HAL_STATUS_SUCCESS,
+ HAL_BOND_STATE_BONDING);
+
send_ssp_request(&ev->addr.bdaddr, HAL_SSP_VARIANT_ENTRY, 0);
}

@@ -595,9 +667,15 @@ static void user_passkey_notify_callback(uint16_t index, uint16_t length,
DBG("%s entered %u", dst, ev->entered);

/* HAL seems to not support entered characters */
- if (!ev->entered)
- send_ssp_request(&ev->addr.bdaddr, HAL_SSP_VARIANT_NOTIF,
+ if (ev->entered)
+ return;
+
+ set_device_bond_state(&ev->addr.bdaddr, HAL_STATUS_SUCCESS,
+ HAL_BOND_STATE_BONDING);
+
+ send_ssp_request(&ev->addr.bdaddr, HAL_SSP_VARIANT_NOTIF,
ev->passkey);
}

static void mgmt_discovering_event(uint16_t index, uint16_t length,
@@ -647,14 +725,6 @@ static void confirm_device_name(const bdaddr_t *addr, uint8_t addr_type)
error("Failed to send confirm name request");
}

-static int bdaddr_cmp(gconstpointer a, gconstpointer b)
-{
- const bdaddr_t *bda = a;
- const bdaddr_t *bdb = b;
-
- return bacmp(bdb, bda);
-}
-
static int fill_device_props(struct hal_property *prop, bdaddr_t *addr,
uint32_t cod, int8_t rssi, char *name)
{
@@ -1838,7 +1908,7 @@ static void pair_device_complete(uint8_t status, uint16_t length,
if (status == MGMT_STATUS_SUCCESS)
return;

- send_bond_state_change(&rp->addr.bdaddr, status_mgmt2hal(status),
+ set_device_bond_state(&rp->addr.bdaddr, status_mgmt2hal(status),
HAL_BOND_STATE_NONE);
}

@@ -1855,7 +1925,7 @@ static bool create_bond(void *buf, uint16_t len)
&cp, pair_device_complete, NULL, NULL) == 0)
return false;

- send_bond_state_change(&cp.addr.bdaddr, HAL_STATUS_SUCCESS,
+ set_device_bond_state(&cp.addr.bdaddr, HAL_STATUS_SUCCESS,
HAL_BOND_STATE_BONDING);

return true;
@@ -1865,12 +1935,18 @@ static bool cancel_bond(void *buf, uint16_t len)
{
struct hal_cmd_cancel_bond *cmd = buf;
struct mgmt_addr_info cp;
+ bool result;

cp.type = BDADDR_BREDR;
android2bdaddr(cmd->bdaddr, &cp.bdaddr);

- return mgmt_reply(mgmt_if, MGMT_OP_CANCEL_PAIR_DEVICE, adapter.index,
+ result = mgmt_reply(mgmt_if, MGMT_OP_CANCEL_PAIR_DEVICE, adapter.index,
sizeof(cp), &cp, NULL, NULL, NULL) > 0;
+ if (result)
+ set_device_bond_state(&cp.bdaddr, HAL_STATUS_SUCCESS,
+ HAL_BOND_STATE_NONE);
+
+ return result;
}

static void unpair_device_complete(uint8_t status, uint16_t length,
@@ -1883,7 +1959,7 @@ static void unpair_device_complete(uint8_t status, uint16_t length,
if (status != MGMT_STATUS_SUCCESS)
return;

- send_bond_state_change(&rp->addr.bdaddr, HAL_STATUS_SUCCESS,
+ set_device_bond_state(&rp->addr.bdaddr, HAL_STATUS_SUCCESS,
HAL_BOND_STATE_NONE);
}

--
1.8.4