2014-06-23 14:28:28

by Szymon Janc

[permalink] [raw]
Subject: [PATCH v2 1/7] android/bluetooth: Add helper for finding device with android address

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

diff --git a/android/bluetooth.c b/android/bluetooth.c
index fc5d529..d42fe8c 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -477,6 +477,24 @@ static struct device *get_device(const bdaddr_t *bdaddr, uint8_t type)
return dev;
}

+static struct device *find_device_android(const uint8_t *addr)
+{
+ bdaddr_t bdaddr;
+
+ android2bdaddr(addr, &bdaddr);
+
+ return find_device(&bdaddr);
+}
+
+static struct device *get_device_android(const uint8_t *addr)
+{
+ bdaddr_t bdaddr;
+
+ android2bdaddr(addr, &bdaddr);
+
+ return get_device(&bdaddr, BDADDR_BREDR);
+}
+
static void send_adapter_property(uint8_t type, uint16_t len, const void *val)
{
uint8_t buf[BASELEN_PROP_CHANGED + len];
@@ -3865,13 +3883,11 @@ static void handle_create_bond_cmd(const void *buf, uint16_t len)
uint8_t status;
struct mgmt_cp_pair_device cp;

- cp.io_cap = DEFAULT_IO_CAPABILITY;
- android2bdaddr(cmd->bdaddr, &cp.addr.bdaddr);
-
- /* type is used only as fallback when device is not in cache */
- dev = get_device(&cp.addr.bdaddr, BDADDR_BREDR);
+ dev = get_device_android(cmd->bdaddr);

+ cp.io_cap = DEFAULT_IO_CAPABILITY;
cp.addr.type = select_device_bearer(dev);
+ bacpy(&cp.addr.bdaddr, &dev->bdaddr);

if (device_is_paired(dev, cp.addr.type)) {
status = HAL_STATUS_FAILED;
@@ -3901,15 +3917,14 @@ static void handle_cancel_bond_cmd(const void *buf, uint16_t len)
struct device *dev;
uint8_t status;

- android2bdaddr(cmd->bdaddr, &cp.bdaddr);
-
- dev = find_device(&cp.bdaddr);
+ dev = find_device_android(cmd->bdaddr);
if (!dev) {
status = HAL_STATUS_FAILED;
goto failed;
}

cp.type = select_device_bearer(dev);
+ bacpy(&cp.bdaddr, &dev->bdaddr);

if (mgmt_reply(mgmt_if, MGMT_OP_CANCEL_PAIR_DEVICE,
adapter.index, sizeof(cp), &cp,
@@ -3951,15 +3966,15 @@ static void handle_remove_bond_cmd(const void *buf, uint16_t len)
struct device *dev;
uint8_t status;

- cp.disconnect = 1;
- android2bdaddr(cmd->bdaddr, &cp.addr.bdaddr);
-
- dev = find_device(&cp.addr.bdaddr);
+ dev = find_device_android(cmd->bdaddr);
if (!dev) {
status = HAL_STATUS_FAILED;
goto failed;
}

+ cp.disconnect = 1;
+ bacpy(&cp.addr.bdaddr, &dev->bdaddr);
+
if (dev->le_paired) {
cp.addr.type = dev->bdaddr_type;

@@ -4098,24 +4113,27 @@ static uint8_t user_passkey_reply(const bdaddr_t *bdaddr, bool accept,
static void handle_ssp_reply_cmd(const void *buf, uint16_t len)
{
const struct hal_cmd_ssp_reply *cmd = buf;
- bdaddr_t bdaddr;
+ struct device *dev;
uint8_t status;
char addr[18];

/* TODO should parameters sanity be verified here? */

- android2bdaddr(cmd->bdaddr, &bdaddr);
- ba2str(&bdaddr, addr);
+ dev = find_device_android(cmd->bdaddr);
+ if (!dev)
+ return;
+
+ ba2str(&dev->bdaddr, addr);

DBG("%s variant %u accept %u", addr, cmd->ssp_variant, cmd->accept);

switch (cmd->ssp_variant) {
case HAL_SSP_VARIANT_CONFIRM:
case HAL_SSP_VARIANT_CONSENT:
- status = user_confirm_reply(&bdaddr, cmd->accept);
+ status = user_confirm_reply(&dev->bdaddr, cmd->accept);
break;
case HAL_SSP_VARIANT_ENTRY:
- status = user_passkey_reply(&bdaddr, cmd->accept,
+ status = user_passkey_reply(&dev->bdaddr, cmd->accept,
cmd->passkey);
break;
case HAL_SSP_VARIANT_NOTIF:
@@ -4355,11 +4373,8 @@ static void handle_get_remote_device_props_cmd(const void *buf, uint16_t len)
const struct hal_cmd_get_remote_device_props *cmd = buf;
struct device *dev;
uint8_t status;
- bdaddr_t addr;
-
- android2bdaddr(cmd->bdaddr, &addr);

- dev = find_device(&addr);
+ dev = find_device_android(cmd->bdaddr);
if (!dev) {
status = HAL_STATUS_INVALID;
goto failed;
@@ -4379,11 +4394,8 @@ static void handle_get_remote_device_prop_cmd(const void *buf, uint16_t len)
const struct hal_cmd_get_remote_device_prop *cmd = buf;
struct device *dev;
uint8_t status;
- bdaddr_t addr;
-
- android2bdaddr(cmd->bdaddr, &addr);

- dev = find_device(&addr);
+ dev = find_device_android(cmd->bdaddr);
if (!dev) {
status = HAL_STATUS_INVALID;
goto failed;
@@ -4461,7 +4473,6 @@ static void handle_set_remote_device_prop_cmd(const void *buf, uint16_t len)
const struct hal_cmd_set_remote_device_prop *cmd = buf;
struct device *dev;
uint8_t status;
- bdaddr_t addr;

if (len != sizeof(*cmd) + cmd->len) {
error("Invalid set remote device prop cmd (0x%x), terminating",
@@ -4470,9 +4481,7 @@ static void handle_set_remote_device_prop_cmd(const void *buf, uint16_t len)
return;
}

- android2bdaddr(cmd->bdaddr, &addr);
-
- dev = find_device(&addr);
+ dev = find_device_android(cmd->bdaddr);
if (!dev) {
status = HAL_STATUS_INVALID;
goto failed;
--
1.9.3



2014-06-24 20:45:29

by Szymon Janc

[permalink] [raw]
Subject: Re: [PATCH v2 1/7] android/bluetooth: Add helper for finding device with android address

On Monday 23 June 2014 16:28:28 Szymon Janc wrote:
> ---
> android/bluetooth.c | 67
> ++++++++++++++++++++++++++++++----------------------- 1 file changed, 38
> insertions(+), 29 deletions(-)
>
> diff --git a/android/bluetooth.c b/android/bluetooth.c
> index fc5d529..d42fe8c 100644
> --- a/android/bluetooth.c
> +++ b/android/bluetooth.c
> @@ -477,6 +477,24 @@ static struct device *get_device(const bdaddr_t
> *bdaddr, uint8_t type) return dev;
> }
>
> +static struct device *find_device_android(const uint8_t *addr)
> +{
> + bdaddr_t bdaddr;
> +
> + android2bdaddr(addr, &bdaddr);
> +
> + return find_device(&bdaddr);
> +}
> +
> +static struct device *get_device_android(const uint8_t *addr)
> +{
> + bdaddr_t bdaddr;
> +
> + android2bdaddr(addr, &bdaddr);
> +
> + return get_device(&bdaddr, BDADDR_BREDR);
> +}
> +
> static void send_adapter_property(uint8_t type, uint16_t len, const void
> *val) {
> uint8_t buf[BASELEN_PROP_CHANGED + len];
> @@ -3865,13 +3883,11 @@ static void handle_create_bond_cmd(const void *buf,
> uint16_t len) uint8_t status;
> struct mgmt_cp_pair_device cp;
>
> - cp.io_cap = DEFAULT_IO_CAPABILITY;
> - android2bdaddr(cmd->bdaddr, &cp.addr.bdaddr);
> -
> - /* type is used only as fallback when device is not in cache */
> - dev = get_device(&cp.addr.bdaddr, BDADDR_BREDR);
> + dev = get_device_android(cmd->bdaddr);
>
> + cp.io_cap = DEFAULT_IO_CAPABILITY;
> cp.addr.type = select_device_bearer(dev);
> + bacpy(&cp.addr.bdaddr, &dev->bdaddr);
>
> if (device_is_paired(dev, cp.addr.type)) {
> status = HAL_STATUS_FAILED;
> @@ -3901,15 +3917,14 @@ static void handle_cancel_bond_cmd(const void *buf,
> uint16_t len) struct device *dev;
> uint8_t status;
>
> - android2bdaddr(cmd->bdaddr, &cp.bdaddr);
> -
> - dev = find_device(&cp.bdaddr);
> + dev = find_device_android(cmd->bdaddr);
> if (!dev) {
> status = HAL_STATUS_FAILED;
> goto failed;
> }
>
> cp.type = select_device_bearer(dev);
> + bacpy(&cp.bdaddr, &dev->bdaddr);
>
> if (mgmt_reply(mgmt_if, MGMT_OP_CANCEL_PAIR_DEVICE,
> adapter.index, sizeof(cp), &cp,
> @@ -3951,15 +3966,15 @@ static void handle_remove_bond_cmd(const void *buf,
> uint16_t len) struct device *dev;
> uint8_t status;
>
> - cp.disconnect = 1;
> - android2bdaddr(cmd->bdaddr, &cp.addr.bdaddr);
> -
> - dev = find_device(&cp.addr.bdaddr);
> + dev = find_device_android(cmd->bdaddr);
> if (!dev) {
> status = HAL_STATUS_FAILED;
> goto failed;
> }
>
> + cp.disconnect = 1;
> + bacpy(&cp.addr.bdaddr, &dev->bdaddr);
> +
> if (dev->le_paired) {
> cp.addr.type = dev->bdaddr_type;
>
> @@ -4098,24 +4113,27 @@ static uint8_t user_passkey_reply(const bdaddr_t
> *bdaddr, bool accept, static void handle_ssp_reply_cmd(const void *buf,
> uint16_t len)
> {
> const struct hal_cmd_ssp_reply *cmd = buf;
> - bdaddr_t bdaddr;
> + struct device *dev;
> uint8_t status;
> char addr[18];
>
> /* TODO should parameters sanity be verified here? */
>
> - android2bdaddr(cmd->bdaddr, &bdaddr);
> - ba2str(&bdaddr, addr);
> + dev = find_device_android(cmd->bdaddr);
> + if (!dev)
> + return;
> +
> + ba2str(&dev->bdaddr, addr);
>
> DBG("%s variant %u accept %u", addr, cmd->ssp_variant, cmd->accept);
>
> switch (cmd->ssp_variant) {
> case HAL_SSP_VARIANT_CONFIRM:
> case HAL_SSP_VARIANT_CONSENT:
> - status = user_confirm_reply(&bdaddr, cmd->accept);
> + status = user_confirm_reply(&dev->bdaddr, cmd->accept);
> break;
> case HAL_SSP_VARIANT_ENTRY:
> - status = user_passkey_reply(&bdaddr, cmd->accept,
> + status = user_passkey_reply(&dev->bdaddr, cmd->accept,
> cmd->passkey);
> break;
> case HAL_SSP_VARIANT_NOTIF:
> @@ -4355,11 +4373,8 @@ static void handle_get_remote_device_props_cmd(const
> void *buf, uint16_t len) const struct hal_cmd_get_remote_device_props *cmd
> = buf;
> struct device *dev;
> uint8_t status;
> - bdaddr_t addr;
> -
> - android2bdaddr(cmd->bdaddr, &addr);
>
> - dev = find_device(&addr);
> + dev = find_device_android(cmd->bdaddr);
> if (!dev) {
> status = HAL_STATUS_INVALID;
> goto failed;
> @@ -4379,11 +4394,8 @@ static void handle_get_remote_device_prop_cmd(const
> void *buf, uint16_t len) const struct hal_cmd_get_remote_device_prop *cmd =
> buf;
> struct device *dev;
> uint8_t status;
> - bdaddr_t addr;
> -
> - android2bdaddr(cmd->bdaddr, &addr);
>
> - dev = find_device(&addr);
> + dev = find_device_android(cmd->bdaddr);
> if (!dev) {
> status = HAL_STATUS_INVALID;
> goto failed;
> @@ -4461,7 +4473,6 @@ static void handle_set_remote_device_prop_cmd(const
> void *buf, uint16_t len) const struct hal_cmd_set_remote_device_prop *cmd =
> buf;
> struct device *dev;
> uint8_t status;
> - bdaddr_t addr;
>
> if (len != sizeof(*cmd) + cmd->len) {
> error("Invalid set remote device prop cmd (0x%x), terminating",
> @@ -4470,9 +4481,7 @@ static void handle_set_remote_device_prop_cmd(const
> void *buf, uint16_t len) return;
> }
>
> - android2bdaddr(cmd->bdaddr, &addr);
> -
> - dev = find_device(&addr);
> + dev = find_device_android(cmd->bdaddr);
> if (!dev) {
> status = HAL_STATUS_INVALID;
> goto failed;

Applied.

--
Szymon K. Janc
[email protected]

2014-06-23 14:28:32

by Szymon Janc

[permalink] [raw]
Subject: [PATCH v2 5/7] android/bluetooth: Map RPA to Identity address on New IRK event

This patch allows to pair with devices using RPA. There is no API
to notify HAL about new Identity Address. When New IRK event is
received ID address is stored in device and map to RPA is done.
This allows to use RPA after address was resolved making this
transparent to Android Framework. When daemon is restarted device
is advertised to Framework with ID address.
---
android/bluetooth.c | 62 +++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 53 insertions(+), 9 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index c664461..61bee90 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -127,6 +127,9 @@ struct device {
bdaddr_t bdaddr;
uint8_t bdaddr_type;

+ bdaddr_t rpa;
+ uint8_t rpa_type;
+
bool le;
bool bredr;

@@ -220,7 +223,10 @@ static struct ipc *hal_ipc = NULL;

static void get_device_android_addr(struct device *dev, uint8_t *addr)
{
- bdaddr2android(&dev->bdaddr, addr);
+ if (bacmp(&dev->rpa, BDADDR_ANY))
+ bdaddr2android(&dev->rpa, addr);
+ else
+ bdaddr2android(&dev->bdaddr, addr);
}

static void mgmt_debug(const char *str, void *user_data)
@@ -382,6 +388,9 @@ static int device_match(gconstpointer a, gconstpointer b)
const struct device *dev = a;
const bdaddr_t *bdaddr = b;

+ if (!bacmp(&dev->rpa, bdaddr))
+ return 0;
+
return bacmp(&dev->bdaddr, bdaddr);
}

@@ -689,7 +698,7 @@ void bt_store_gatt_ccc(const bdaddr_t *dst, uint16_t value)
return;
}

- ba2str(dst, addr);
+ ba2str(&dev->bdaddr, addr);

DBG("%s Gatt CCC %d", addr, value);

@@ -1642,12 +1651,21 @@ static void update_found_device(const bdaddr_t *bdaddr, uint8_t bdaddr_type,
/* Notify Gatt if its registered for LE events */
if (bdaddr_type != BDADDR_BREDR && gatt_device_found_cb) {
bool discoverable;
+ bdaddr_t *addr;
+ uint8_t addr_type;

discoverable = eir.flags & (EIR_LIM_DISC | EIR_GEN_DISC);

- gatt_device_found_cb(bdaddr, bdaddr_type, rssi, data_len, data,
- discoverable,
- dev->le_bonded);
+ if (bacmp(&dev->rpa, BDADDR_ANY)) {
+ addr = &dev->rpa;
+ addr_type = dev->rpa_type;
+ } else {
+ addr = &dev->bdaddr;
+ addr_type = dev->bdaddr_type;
+ }
+
+ gatt_device_found_cb(addr, addr_type, rssi, data_len, data,
+ discoverable, dev->le_bonded);
}

if (!dev->bredr_paired && !dev->le_paired)
@@ -2078,10 +2096,36 @@ static void new_irk_callback(uint16_t index, uint16_t length,

DBG("new IRK for %s, RPA %s", dst, rpa);

- /* TODO: handle new Identity to RPA mapping */
- dev = find_device(&addr->bdaddr);
- if (!dev)
- return;
+ if (!bacmp(&ev->rpa, BDADDR_ANY)) {
+ dev = find_device(&addr->bdaddr);
+ if (!dev)
+ return;
+ } else {
+ dev = find_device(&addr->bdaddr);
+
+ if (dev && dev->bredr_paired) {
+ bacpy(&dev->rpa, &addr->bdaddr);
+ dev->rpa_type = addr->type;
+
+ /* TODO merge properties ie. UUIDs */
+ } else {
+ dev = find_device(&ev->rpa);
+ if (!dev)
+ return;
+
+ /* don't leave garbage in cache file */
+ remove_device_info(dev, CACHE_FILE);
+
+ bacpy(&dev->rpa, &dev->bdaddr);
+ dev->rpa_type = dev->bdaddr_type;
+
+ bacpy(&dev->bdaddr, &addr->bdaddr);
+ dev->bdaddr_type = addr->type;
+ }
+ }
+
+ update_device_state(dev, ev->key.addr.type, HAL_STATUS_SUCCESS, false,
+ true, !!ev->store_hint);

if (ev->store_hint)
store_irk(dev, ev->key.val);
--
1.9.3


2014-06-23 14:28:34

by Szymon Janc

[permalink] [raw]
Subject: [PATCH v2 7/7] android/bluetooth: Log error if loading LTKs or IRKs failed

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

diff --git a/android/bluetooth.c b/android/bluetooth.c
index f8c2688..28fc04a 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -2260,6 +2260,15 @@ static void load_link_keys(GSList *keys, bt_bluetooth_ready cb)
}
}

+static void load_ltk_complete(uint8_t status, uint16_t length,
+ const void *param, void *user_data)
+{
+ if (status == MGMT_STATUS_SUCCESS)
+ return;
+
+ info("Failed to load LTKs: %s (0x%02x)", mgmt_errstr(status), status);
+}
+
static void load_ltks(GSList *ltks)
{
struct mgmt_cp_load_long_term_keys *cp;
@@ -2286,12 +2295,21 @@ static void load_ltks(GSList *ltks)
memcpy(ltk, ltks->data, sizeof(*ltk));

if (mgmt_send(mgmt_if, MGMT_OP_LOAD_LONG_TERM_KEYS, adapter.index,
- cp_size, cp, NULL, NULL, NULL) == 0)
+ cp_size, cp, load_ltk_complete, NULL, NULL) == 0)
error("Failed to load LTKs");

g_free(cp);
}

+static void load_irk_complete(uint8_t status, uint16_t length,
+ const void *param, void *user_data)
+{
+ if (status == MGMT_STATUS_SUCCESS)
+ return;
+
+ info("Failed to load IRKs: %s (0x%02x)", mgmt_errstr(status), status);
+}
+
static void load_irks(GSList *irks)
{
struct mgmt_cp_load_irks *cp;
@@ -2313,7 +2331,7 @@ static void load_irks(GSList *irks)
memcpy(irk, irks->data, sizeof(*irk));

if (mgmt_send(mgmt_if, MGMT_OP_LOAD_IRKS, adapter.index, cp_size, cp,
- NULL, NULL, NULL) == 0)
+ load_irk_complete, NULL, NULL) == 0)
error("Failed to load IRKs");

g_free(cp);
--
1.9.3


2014-06-23 14:28:33

by Szymon Janc

[permalink] [raw]
Subject: [PATCH v2 6/7] android/gatt: Use proper address when connecting

If device was using RPA but address was resolved IDA must be used.
---
android/bluetooth.c | 14 ++++++++++++++
android/bluetooth.h | 2 ++
android/gatt.c | 15 +++++++++++++--
3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index 61bee90..f8c2688 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -1436,6 +1436,20 @@ bool bt_is_device_le(const bdaddr_t *addr)
return dev->le;
}

+const bdaddr_t *bt_get_id_addr(const bdaddr_t *addr, uint8_t *type)
+{
+ struct device *dev;
+
+ dev = find_device(addr);
+ if (!dev)
+ return NULL;
+
+ if (type)
+ *type = dev->bdaddr_type;
+
+ return &dev->bdaddr;
+}
+
const char *bt_get_adapter_name(void)
{
return adapter.name;
diff --git a/android/bluetooth.h b/android/bluetooth.h
index eb50fe1..dde477d 100644
--- a/android/bluetooth.h
+++ b/android/bluetooth.h
@@ -76,3 +76,5 @@ void bt_update_sign_counter(const bdaddr_t *addr, enum bt_csrk_type type);
void bt_store_gatt_ccc(const bdaddr_t *addr, uint16_t value);

uint16_t bt_get_gatt_ccc(const bdaddr_t *addr);
+
+const bdaddr_t *bt_get_id_addr(const bdaddr_t *addr, uint8_t *type);
diff --git a/android/gatt.c b/android/gatt.c
index 32853fa..af83d96 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -1423,6 +1423,8 @@ static int connect_le(struct gatt_device *dev)
GIOChannel *io;
GError *gerr = NULL;
char addr[18];
+ const bdaddr_t *bdaddr;
+ uint8_t bdaddr_type;

ba2str(&dev->bdaddr, addr);

@@ -1437,6 +1439,15 @@ static int connect_le(struct gatt_device *dev)
sec_level = bt_device_is_bonded(&dev->bdaddr) ? BT_IO_SEC_MEDIUM :
BT_IO_SEC_LOW;

+ if (dev->bdaddr_type == BDADDR_LE_RANDOM) {
+ bdaddr = bt_get_id_addr(&dev->bdaddr, &bdaddr_type);
+ if (!bdaddr)
+ return -EINVAL;
+ } else {
+ bdaddr = &dev->bdaddr;
+ bdaddr_type = dev->bdaddr_type;
+ }
+
/*
* This connection will help us catch any PDUs that comes before
* pairing finishes
@@ -1445,8 +1456,8 @@ static int connect_le(struct gatt_device *dev)
BT_IO_OPT_SOURCE_BDADDR,
&adapter_addr,
BT_IO_OPT_SOURCE_TYPE, BDADDR_LE_PUBLIC,
- BT_IO_OPT_DEST_BDADDR, &dev->bdaddr,
- BT_IO_OPT_DEST_TYPE, dev->bdaddr_type,
+ BT_IO_OPT_DEST_BDADDR, bdaddr,
+ BT_IO_OPT_DEST_TYPE, bdaddr_type,
BT_IO_OPT_CID, ATT_CID,
BT_IO_OPT_SEC_LEVEL, sec_level,
BT_IO_OPT_INVALID);
--
1.9.3


2014-06-23 14:28:31

by Szymon Janc

[permalink] [raw]
Subject: [PATCH v2 4/7] android/bluetooth: Add get_device_android_addr helper

This will be used to fake address for Android Framework.
---
android/bluetooth.c | 43 +++++++++++++++++++++++++++++--------------
1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index be50a1a..c664461 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -218,6 +218,11 @@ static GSList *browse_reqs;

static struct ipc *hal_ipc = NULL;

+static void get_device_android_addr(struct device *dev, uint8_t *addr)
+{
+ bdaddr2android(&dev->bdaddr, addr);
+}
+
static void mgmt_debug(const char *str, void *user_data)
{
const char *prefix = user_data;
@@ -752,7 +757,7 @@ static void send_bond_state_change(struct device *dev, uint8_t status,

ev.status = status;
ev.state = state;
- bdaddr2android(addr, ev.bdaddr);
+ get_device_android_addr(dev, ev.bdaddr);

ipc_send_notif(hal_ipc, HAL_SERVICE_ID_BLUETOOTH,
HAL_EV_BOND_STATE_CHANGED, sizeof(ev), &ev);
@@ -871,7 +876,7 @@ static void send_device_property(struct device *dev, uint8_t type,
struct hal_ev_remote_device_props *ev = (void *) buf;

ev->status = HAL_STATUS_SUCCESS;
- bdaddr2android(&dev->bdaddr, ev->bdaddr);
+ get_device_android_addr(dev, ev->bdaddr);
ev->num_props = 1;
ev->props[0].type = type;
ev->props[0].len = len;
@@ -1118,7 +1123,7 @@ static void pin_code_request_callback(uint16_t index, uint16_t length,

/* Name already sent in remote device prop */
memset(&hal_ev, 0, sizeof(hal_ev));
- bdaddr2android(&ev->addr.bdaddr, hal_ev.bdaddr);
+ get_device_android_addr(dev, hal_ev.bdaddr);
hal_ev.class_of_dev = dev->class;

ipc_send_notif(hal_ipc, HAL_SERVICE_ID_BLUETOOTH, HAL_EV_PIN_REQUEST,
@@ -1132,7 +1137,7 @@ static void send_ssp_request(struct device *dev, uint8_t variant,

memset(&ev, 0, sizeof(ev));

- bdaddr2android(&dev->bdaddr, ev.bdaddr);
+ get_device_android_addr(dev, ev.bdaddr);
memcpy(ev.name, dev->name, strlen(dev->name));
ev.class_of_dev = dev->class;

@@ -1459,7 +1464,7 @@ static void update_new_device(struct device *dev, int8_t rssi,
{
uint8_t buf[IPC_MTU];
struct hal_ev_device_found *ev = (void *) buf;
- bdaddr_t android_bdaddr;
+ uint8_t android_bdaddr[6];
uint8_t android_type;
int size;

@@ -1470,10 +1475,9 @@ static void update_new_device(struct device *dev, int8_t rssi,

size = sizeof(*ev);

- bdaddr2android(&dev->bdaddr, &android_bdaddr);
+ get_device_android_addr(dev, android_bdaddr);
size += fill_hal_prop(buf + size, HAL_PROP_DEVICE_ADDR,
- sizeof(android_bdaddr),
- &android_bdaddr);
+ sizeof(android_bdaddr), android_bdaddr);
ev->num_props++;

android_type = get_device_android_type(dev);
@@ -1536,7 +1540,7 @@ static void update_device(struct device *dev, int8_t rssi,
size = sizeof(*ev);

ev->status = HAL_STATUS_SUCCESS;
- bdaddr2android(&dev->bdaddr, ev->bdaddr);
+ get_device_android_addr(dev, ev->bdaddr);

old_type = get_device_android_type(dev);

@@ -1715,6 +1719,7 @@ static void mgmt_device_connected_event(uint16_t index, uint16_t length,
{
const struct mgmt_ev_device_connected *ev = param;
struct hal_ev_acl_state_changed hal_ev;
+ struct device *dev;

if (length < sizeof(*ev)) {
error("Too short device connected event (%u bytes)", length);
@@ -1726,7 +1731,12 @@ static void mgmt_device_connected_event(uint16_t index, uint16_t length,

hal_ev.status = HAL_STATUS_SUCCESS;
hal_ev.state = HAL_ACL_STATE_CONNECTED;
- bdaddr2android(&ev->addr.bdaddr, hal_ev.bdaddr);
+
+ dev = find_device(&ev->addr.bdaddr);
+ if (!dev)
+ return;
+
+ get_device_android_addr(dev, hal_ev.bdaddr);

ipc_send_notif(hal_ipc, HAL_SERVICE_ID_BLUETOOTH,
HAL_EV_ACL_STATE_CHANGED, sizeof(hal_ev), &hal_ev);
@@ -1738,15 +1748,20 @@ static void mgmt_device_disconnected_event(uint16_t index, uint16_t length,
{
const struct mgmt_ev_device_disconnected *ev = param;
struct hal_ev_acl_state_changed hal_ev;
+ struct device *dev;

if (length < sizeof(*ev)) {
error("Too short device disconnected event (%u bytes)", length);
return;
}

+ dev = find_device(&ev->addr.bdaddr);
+ if (!dev)
+ return;
+
hal_ev.status = HAL_STATUS_SUCCESS;
hal_ev.state = HAL_ACL_STATE_DISCONNECTED;
- bdaddr2android(&ev->addr.bdaddr, hal_ev.bdaddr);
+ get_device_android_addr(dev, hal_ev.bdaddr);

ipc_send_notif(hal_ipc, HAL_SERVICE_ID_BLUETOOTH,
HAL_EV_ACL_STATE_CHANGED, sizeof(hal_ev), &hal_ev);
@@ -3345,7 +3360,7 @@ static uint8_t get_adapter_bonded_devices(void)
for (l = bonded_devices; l; l = g_slist_next(l)) {
struct device *dev = l->data;

- bdaddr2android(&dev->bdaddr, buf + (i * sizeof(bdaddr_t)));
+ get_device_android_addr(dev, buf + (i * sizeof(bdaddr_t)));
i++;
}

@@ -3461,7 +3476,7 @@ static void get_adapter_properties(void)
for (i = 0, l = bonded_devices; l; l = g_slist_next(l), i++) {
struct device *dev = l->data;

- bdaddr2android(&dev->bdaddr, bonded + (i * sizeof(bdaddr_t)));
+ get_device_android_addr(dev, bonded + (i * sizeof(bdaddr_t)));
}

size += fill_hal_prop(buf + size, HAL_PROP_ADAPTER_BONDED_DEVICES,
@@ -4255,7 +4270,7 @@ static void get_remote_device_props(struct device *dev)
size = sizeof(*ev);

ev->status = HAL_STATUS_SUCCESS;
- bdaddr2android(&dev->bdaddr, ev->bdaddr);
+ get_device_android_addr(dev, ev->bdaddr);

android_type = get_device_android_type(dev);
size += fill_hal_prop(buf + size, HAL_PROP_DEVICE_TYPE,
--
1.9.3


2014-06-23 14:28:30

by Szymon Janc

[permalink] [raw]
Subject: [PATCH v2 3/7] android/bluetooth: Fix passing address type to SSP replies

---
android/bluetooth.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index 4031b17..be50a1a 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -4056,7 +4056,8 @@ failed:
status);
}

-static uint8_t user_confirm_reply(const bdaddr_t *bdaddr, bool accept)
+static uint8_t user_confirm_reply(const bdaddr_t *bdaddr, uint8_t type,
+ bool accept)
{
struct mgmt_addr_info cp;
uint16_t opcode;
@@ -4067,7 +4068,7 @@ static uint8_t user_confirm_reply(const bdaddr_t *bdaddr, bool accept)
opcode = MGMT_OP_USER_CONFIRM_NEG_REPLY;

bacpy(&cp.bdaddr, bdaddr);
- cp.type = BDADDR_BREDR;
+ cp.type = type;

if (mgmt_reply(mgmt_if, opcode, adapter.index, sizeof(cp), &cp,
NULL, NULL, NULL) > 0)
@@ -4076,8 +4077,8 @@ static uint8_t user_confirm_reply(const bdaddr_t *bdaddr, bool accept)
return HAL_STATUS_FAILED;
}

-static uint8_t user_passkey_reply(const bdaddr_t *bdaddr, bool accept,
- uint32_t passkey)
+static uint8_t user_passkey_reply(const bdaddr_t *bdaddr, uint8_t type,
+ bool accept, uint32_t passkey)
{
unsigned int id;

@@ -4086,7 +4087,7 @@ static uint8_t user_passkey_reply(const bdaddr_t *bdaddr, bool accept,

memset(&cp, 0, sizeof(cp));
bacpy(&cp.addr.bdaddr, bdaddr);
- cp.addr.type = BDADDR_BREDR;
+ cp.addr.type = type;
cp.passkey = htobl(passkey);

id = mgmt_reply(mgmt_if, MGMT_OP_USER_PASSKEY_REPLY,
@@ -4097,7 +4098,7 @@ static uint8_t user_passkey_reply(const bdaddr_t *bdaddr, bool accept,

memset(&cp, 0, sizeof(cp));
bacpy(&cp.addr.bdaddr, bdaddr);
- cp.addr.type = BDADDR_BREDR;
+ cp.addr.type = type;

id = mgmt_reply(mgmt_if, MGMT_OP_USER_PASSKEY_NEG_REPLY,
adapter.index, sizeof(cp), &cp,
@@ -4130,11 +4131,12 @@ static void handle_ssp_reply_cmd(const void *buf, uint16_t len)
switch (cmd->ssp_variant) {
case HAL_SSP_VARIANT_CONFIRM:
case HAL_SSP_VARIANT_CONSENT:
- status = user_confirm_reply(&dev->bdaddr, cmd->accept);
+ status = user_confirm_reply(&dev->bdaddr, dev->bdaddr_type,
+ cmd->accept);
break;
case HAL_SSP_VARIANT_ENTRY:
- status = user_passkey_reply(&dev->bdaddr, cmd->accept,
- cmd->passkey);
+ status = user_passkey_reply(&dev->bdaddr, dev->bdaddr_type,
+ cmd->accept, cmd->passkey);
break;
case HAL_SSP_VARIANT_NOTIF:
status = HAL_STATUS_SUCCESS;
--
1.9.3


2014-06-23 14:28:29

by Szymon Janc

[permalink] [raw]
Subject: [PATCH v2 2/7] android/bluetooth: Pass device to send_bond_state_change function

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

diff --git a/android/bluetooth.c b/android/bluetooth.c
index d42fe8c..4031b17 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -745,7 +745,7 @@ static void store_link_key(const bdaddr_t *dst, const uint8_t *key,
g_key_file_free(key_file);
}

-static void send_bond_state_change(const bdaddr_t *addr, uint8_t status,
+static void send_bond_state_change(struct device *dev, uint8_t status,
uint8_t state)
{
struct hal_ev_bond_state_changed ev;
@@ -861,7 +861,7 @@ static void update_device_state(struct device *dev, uint8_t addr_type,
new_bond = device_bond_state(dev);

if (old_bond != new_bond)
- send_bond_state_change(&dev->bdaddr, status, new_bond);
+ send_bond_state_change(dev, status, new_bond);
}

static void send_device_property(struct device *dev, uint8_t type,
--
1.9.3