2012-01-31 17:10:48

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH BlueZ 1/6] event: Add type information to the mgmt "Device Connected" event

In the case of incomming connections we have to know the type of
the device that connected to us.

Through hciops we have the LE Connection Complete event, that
information was lost when Management interface was being used.
---
plugins/hciops.c | 30 +++++++++++++++++-------------
plugins/mgmtops.c | 29 +++++++++++++++--------------
src/device.c | 8 ++++++++
src/device.h | 1 +
src/event.c | 4 +++-
src/event.h | 2 +-
6 files changed, 45 insertions(+), 29 deletions(-)

diff --git a/plugins/hciops.c b/plugins/hciops.c
index f4af637..be8b412 100644
--- a/plugins/hciops.c
+++ b/plugins/hciops.c
@@ -2160,7 +2160,8 @@ static inline void conn_complete(int index, void *ptr)
conn = get_connection(dev, &evt->bdaddr);
conn->handle = btohs(evt->handle);

- btd_event_conn_complete(&dev->bdaddr, &evt->bdaddr, NULL, NULL);
+ btd_event_conn_complete(&dev->bdaddr, &evt->bdaddr, ADDR_TYPE_BREDR,
+ NULL, NULL);

if (conn->secmode3)
bonding_complete(dev, conn, 0);
@@ -2179,6 +2180,17 @@ static inline void conn_complete(int index, void *ptr)
free(str);
}

+static inline addr_type_t le_addr_type(uint8_t bdaddr_type)
+{
+ switch (bdaddr_type) {
+ case LE_RANDOM_ADDRESS:
+ return ADDR_TYPE_LE_RANDOM;
+ case LE_PUBLIC_ADDRESS:
+ default:
+ return ADDR_TYPE_LE_PUBLIC;
+ }
+}
+
static inline void le_conn_complete(int index, void *ptr)
{
struct dev_info *dev = &devs[index];
@@ -2186,6 +2198,7 @@ static inline void le_conn_complete(int index, void *ptr)
char filename[PATH_MAX];
char local_addr[18], peer_addr[18], *str;
struct bt_conn *conn;
+ addr_type_t type;

if (evt->status) {
btd_event_conn_failed(&dev->bdaddr, &evt->peer_bdaddr,
@@ -2196,7 +2209,9 @@ static inline void le_conn_complete(int index, void *ptr)
conn = get_connection(dev, &evt->peer_bdaddr);
conn->handle = btohs(evt->handle);

- btd_event_conn_complete(&dev->bdaddr, &evt->peer_bdaddr, NULL, NULL);
+ type = le_addr_type(evt->peer_bdaddr_type);
+ btd_event_conn_complete(&dev->bdaddr, &evt->peer_bdaddr, type,
+ NULL, NULL);

/* check if the remote version needs be requested */
ba2str(&dev->bdaddr, local_addr);
@@ -2270,17 +2285,6 @@ static inline void conn_request(int index, void *ptr)
btd_event_remote_class(&dev->bdaddr, &evt->bdaddr, class);
}

-static inline addr_type_t le_addr_type(uint8_t bdaddr_type)
-{
- switch (bdaddr_type) {
- case LE_RANDOM_ADDRESS:
- return ADDR_TYPE_LE_RANDOM;
- case LE_PUBLIC_ADDRESS:
- default:
- return ADDR_TYPE_LE_PUBLIC;
- }
-}
-
static inline void le_advertising_report(int index, evt_le_meta_event *meta)
{
struct dev_info *dev = &devs[index];
diff --git a/plugins/mgmtops.c b/plugins/mgmtops.c
index b409569..63af363 100644
--- a/plugins/mgmtops.c
+++ b/plugins/mgmtops.c
@@ -396,6 +396,20 @@ static void mgmt_new_link_key(int sk, uint16_t index, void *buf, size_t len)
bonding_complete(info, &ev->key.bdaddr, 0);
}

+static inline addr_type_t mgmt_addr_type(uint8_t mgmt_addr_type)
+{
+ switch (mgmt_addr_type) {
+ case MGMT_ADDR_BREDR:
+ return ADDR_TYPE_BREDR;
+ case MGMT_ADDR_LE_PUBLIC:
+ return ADDR_TYPE_LE_PUBLIC;
+ case MGMT_ADDR_LE_RANDOM:
+ return ADDR_TYPE_LE_RANDOM;
+ default:
+ return ADDR_TYPE_BREDR;
+ }
+}
+
static void mgmt_device_connected(int sk, uint16_t index, void *buf, size_t len)
{
struct mgmt_ev_device_connected *ev = buf;
@@ -431,6 +445,7 @@ static void mgmt_device_connected(int sk, uint16_t index, void *buf, size_t len)
eir_parse(&eir_data, ev->eir, eir_len);

btd_event_conn_complete(&info->bdaddr, &ev->addr.bdaddr,
+ mgmt_addr_type(ev->addr.type),
eir_data.name, eir_data.dev_class);
}

@@ -1257,20 +1272,6 @@ static void mgmt_local_name_changed(int sk, uint16_t index, void *buf, size_t le
adapter_name_changed(adapter, (char *) ev->name);
}

-static inline addr_type_t mgmt_addr_type(uint8_t mgmt_addr_type)
-{
- switch (mgmt_addr_type) {
- case MGMT_ADDR_BREDR:
- return ADDR_TYPE_BREDR;
- case MGMT_ADDR_LE_PUBLIC:
- return ADDR_TYPE_LE_PUBLIC;
- case MGMT_ADDR_LE_RANDOM:
- return ADDR_TYPE_LE_RANDOM;
- default:
- return ADDR_TYPE_BREDR;
- }
-}
-
static void mgmt_device_found(int sk, uint16_t index, void *buf, size_t len)
{
struct mgmt_ev_device_found *ev = buf;
diff --git a/src/device.c b/src/device.c
index c19acd4..d39ca53 100644
--- a/src/device.c
+++ b/src/device.c
@@ -2005,6 +2005,14 @@ void device_get_address(struct btd_device *device, bdaddr_t *bdaddr,
*type = device->type;
}

+void device_set_addr_type(struct btd_device *device, addr_type_t type)
+{
+ if (device == NULL)
+ return;
+
+ device->type = type;
+}
+
const gchar *device_get_path(struct btd_device *device)
{
if (!device)
diff --git a/src/device.h b/src/device.h
index 13005ae..7cb9bb2 100644
--- a/src/device.h
+++ b/src/device.h
@@ -59,6 +59,7 @@ void btd_device_add_uuid(struct btd_device *device, const char *uuid);
struct btd_adapter *device_get_adapter(struct btd_device *device);
void device_get_address(struct btd_device *device, bdaddr_t *bdaddr,
addr_type_t *type);
+void device_set_addr_type(struct btd_device *device, addr_type_t type);
const gchar *device_get_path(struct btd_device *device);
struct agent *device_get_agent(struct btd_device *device);
gboolean device_is_bredr(struct btd_device *device);
diff --git a/src/event.c b/src/event.c
index 0783b47..ff1896a 100644
--- a/src/event.c
+++ b/src/event.c
@@ -442,7 +442,7 @@ int btd_event_ltk_notify(bdaddr_t *local, bdaddr_t *peer, addr_type_t addr_type,
return ret;
}

-void btd_event_conn_complete(bdaddr_t *local, bdaddr_t *peer,
+void btd_event_conn_complete(bdaddr_t *local, bdaddr_t *peer, addr_type_t type,
char *name, uint8_t *dev_class)
{
struct btd_adapter *adapter;
@@ -453,6 +453,8 @@ void btd_event_conn_complete(bdaddr_t *local, bdaddr_t *peer,

update_lastused(local, peer);

+ device_set_addr_type(device, type);
+
adapter_add_connection(adapter, device);

if (name != NULL)
diff --git a/src/event.h b/src/event.h
index ccdd47c..c09350d 100644
--- a/src/event.h
+++ b/src/event.h
@@ -29,7 +29,7 @@ void btd_event_device_found(bdaddr_t *local, bdaddr_t *peer, addr_type_t type,
void btd_event_set_legacy_pairing(bdaddr_t *local, bdaddr_t *peer, gboolean legacy);
void btd_event_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class);
void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name);
-void btd_event_conn_complete(bdaddr_t *local, bdaddr_t *peer,
+void btd_event_conn_complete(bdaddr_t *local, bdaddr_t *peer, addr_type_t type,
char *name, uint8_t *dev_class);
void btd_event_conn_failed(bdaddr_t *local, bdaddr_t *peer, uint8_t status);
void btd_event_disconn_complete(bdaddr_t *local, bdaddr_t *peer);
--
1.7.8.1



2012-01-31 17:37:02

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ 1/6] event: Add type information to the mgmt "Device Connected" event

Hi Vinicius,

On Tue, Jan 31, 2012, Vinicius Costa Gomes wrote:
> In the case of incomming connections we have to know the type of
> the device that connected to us.
>
> Through hciops we have the LE Connection Complete event, that
> information was lost when Management interface was being used.
> ---
> plugins/hciops.c | 30 +++++++++++++++++-------------
> plugins/mgmtops.c | 29 +++++++++++++++--------------
> src/device.c | 8 ++++++++
> src/device.h | 1 +
> src/event.c | 4 +++-
> src/event.h | 2 +-
> 6 files changed, 45 insertions(+), 29 deletions(-)

All six patches have been applied. Thanks.

Please pay attention to your summary line lengths in the future.
Remember that git shortlog indents them by 6 characters so anything over
74 will look strange. I fixed it manually this time.

Johan

2012-01-31 17:10:53

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH BlueZ 6/6] adapter_ops: Add address type information to the pair_device command

Now that we have address type information we have to inform it
when pairing with an device.
---
plugins/hciops.c | 3 ++-
plugins/mgmtops.c | 3 ++-
src/adapter.c | 5 +++--
src/adapter.h | 5 +++--
src/device.c | 3 ++-
5 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/plugins/hciops.c b/plugins/hciops.c
index be8b412..90106df 100644
--- a/plugins/hciops.c
+++ b/plugins/hciops.c
@@ -3649,7 +3649,8 @@ failed:
bonding_complete(dev, conn, HCI_UNSPECIFIED_ERROR);
}

-static int hciops_create_bonding(int index, bdaddr_t *bdaddr, uint8_t io_cap)
+static int hciops_create_bonding(int index, bdaddr_t *bdaddr,
+ uint8_t addr_type, uint8_t io_cap)
{
struct dev_info *dev = &devs[index];
BtIOSecLevel sec_level;
diff --git a/plugins/mgmtops.c b/plugins/mgmtops.c
index 63af363..8f6788e 100644
--- a/plugins/mgmtops.c
+++ b/plugins/mgmtops.c
@@ -1952,7 +1952,7 @@ static int mgmt_set_io_capability(int index, uint8_t io_capability)
return 0;
}

-static int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t io_cap)
+static int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t addr_type, uint8_t io_cap)
{
char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_pair_device)];
struct mgmt_hdr *hdr = (void *) buf;
@@ -1968,6 +1968,7 @@ static int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t io_cap)
hdr->index = htobs(index);

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

if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
diff --git a/src/adapter.c b/src/adapter.c
index 25f70ff..89f6ca7 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -3551,10 +3551,11 @@ int btd_adapter_set_did(struct btd_adapter *adapter, uint16_t vendor,
}

int adapter_create_bonding(struct btd_adapter *adapter, bdaddr_t *bdaddr,
- uint8_t io_cap)
+ uint8_t addr_type, uint8_t io_cap)
{
suspend_discovery(adapter);
- return adapter_ops->create_bonding(adapter->dev_id, bdaddr, io_cap);
+ return adapter_ops->create_bonding(adapter->dev_id, bdaddr,
+ addr_type, io_cap);
}

int adapter_cancel_bonding(struct btd_adapter *adapter, bdaddr_t *bdaddr)
diff --git a/src/adapter.h b/src/adapter.h
index c1f981a..7265d8b 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -212,7 +212,8 @@ struct btd_adapter_ops {
int (*restore_powered) (int index);
int (*load_keys) (int index, GSList *keys, gboolean debug_keys);
int (*set_io_capability) (int index, uint8_t io_capability);
- int (*create_bonding) (int index, bdaddr_t *bdaddr, uint8_t io_cap);
+ int (*create_bonding) (int index, bdaddr_t *bdaddr, uint8_t addr_type,
+ uint8_t io_cap);
int (*cancel_bonding) (int index, bdaddr_t *bdaddr);
int (*read_local_oob_data) (int index);
int (*add_remote_oob_data) (int index, bdaddr_t *bdaddr, uint8_t *hash,
@@ -265,7 +266,7 @@ int btd_adapter_set_did(struct btd_adapter *adapter, uint16_t vendor,
uint16_t product, uint16_t version);

int adapter_create_bonding(struct btd_adapter *adapter, bdaddr_t *bdaddr,
- uint8_t io_cap);
+ uint8_t addr_type, uint8_t io_cap);

int adapter_cancel_bonding(struct btd_adapter *adapter, bdaddr_t *bdaddr);

diff --git a/src/device.c b/src/device.c
index 0133e07..b2c8379 100644
--- a/src/device.c
+++ b/src/device.c
@@ -2297,7 +2297,8 @@ DBusMessage *device_create_bonding(struct btd_device *device,
if (device_is_bonded(device))
return btd_error_already_exists(msg);

- err = adapter_create_bonding(adapter, &device->bdaddr, capability);
+ err = adapter_create_bonding(adapter, &device->bdaddr,
+ device->type, capability);
if (err < 0)
return btd_error_failed(msg, strerror(-err));

--
1.7.8.1


2012-01-31 17:10:50

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH BlueZ 3/6] device: Fix doing SDP discovery for LE devices after the bonding is complete

---
src/device.c | 8 ++++++--
1 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/device.c b/src/device.c
index 596ec0b..88cd4cc 100644
--- a/src/device.c
+++ b/src/device.c
@@ -2380,8 +2380,12 @@ void device_bonding_complete(struct btd_device *device, uint8_t status)
device->discov_timer = 0;
}

- device_browse_sdp(device, bonding->conn, bonding->msg,
- NULL, FALSE);
+ if (device_is_bredr(device))
+ device_browse_sdp(device, bonding->conn, bonding->msg,
+ NULL, FALSE);
+ else
+ device_browse_primary(device, bonding->conn,
+ bonding->msg, FALSE);

bonding_request_free(bonding);
} else {
--
1.7.8.1


2012-01-31 17:10:51

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH BlueZ 4/6] device: Fix not using the same way for pairing LE devices

Now that both backends (hciops and mgmtops) are capable
of pairing LE devices, we don't have to force the procedure.
---
src/adapter.c | 10 +---------
1 files changed, 1 insertions(+), 9 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index d5075db..25f70ff 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1578,15 +1578,7 @@ static DBusMessage *create_paired_device(DBusConnection *conn,
return btd_error_failed(msg, strerror(-err));
}

- if (device_is_bredr(device))
- return device_create_bonding(device, conn, msg,
- agent_path, cap);
-
- err = device_browse_primary(device, conn, msg, TRUE);
- if (err < 0)
- return btd_error_failed(msg, strerror(-err));
-
- return NULL;
+ return device_create_bonding(device, conn, msg, agent_path, cap);
}

static gint device_path_cmp(struct btd_device *device, const gchar *path)
--
1.7.8.1


2012-01-31 17:10:52

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH BlueZ 5/6] device: Fix not using the "bonded" property for new bondings

When checking if a device was already bonded, we should
use the paired property instead of reading the link key
from storage. This method will work for LE links, also.
---
src/device.c | 16 +---------------
1 files changed, 1 insertions(+), 15 deletions(-)

diff --git a/src/device.c b/src/device.c
index 88cd4cc..0133e07 100644
--- a/src/device.c
+++ b/src/device.c
@@ -2287,29 +2287,15 @@ DBusMessage *device_create_bonding(struct btd_device *device,
const char *agent_path,
uint8_t capability)
{
- char filename[PATH_MAX + 1];
- char *str, srcaddr[18], dstaddr[18];
struct btd_adapter *adapter = device->adapter;
struct bonding_req *bonding;
- bdaddr_t src;
int err;

- adapter_get_address(adapter, &src);
- ba2str(&src, srcaddr);
- ba2str(&device->bdaddr, dstaddr);
-
if (device->bonding)
return btd_error_in_progress(msg);

- /* check if a link key already exists */
- create_name(filename, PATH_MAX, STORAGEDIR, srcaddr,
- "linkkeys");
-
- str = textfile_caseget(filename, dstaddr);
- if (str) {
- free(str);
+ if (device_is_bonded(device))
return btd_error_already_exists(msg);
- }

err = adapter_create_bonding(adapter, &device->bdaddr, capability);
if (err < 0)
--
1.7.8.1


2012-01-31 17:10:49

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH BlueZ 2/6] device: Fix not setting the device as bonded when restoring it from the LTK

---
src/device.c | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/src/device.c b/src/device.c
index d39ca53..596ec0b 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1025,6 +1025,11 @@ struct btd_device *device_create(DBusConnection *conn,
device_set_bonded(device, TRUE);
}

+ if (device_is_le(device) && has_longtermkeys(&src, &device->bdaddr)) {
+ device_set_paired(device, TRUE);
+ device_set_bonded(device, TRUE);
+ }
+
if (read_device_id(srcaddr, address, NULL, &vendor, &product, &version)
== 0) {
device_set_vendor(device, vendor);
--
1.7.8.1