2012-09-04 19:04:28

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 00/14] LE General Connection Establishment procedure

This series implement the LE General Connection Establishment procedure
for LE connections.

If there are LE bonded devices marked for auto connection they are added
to a connect_list on the adapter. When there is any device on this list
scan is performed continuously. When a device is found the connect_list
is checked. If that device is on the list scan is stopped and a
connection attempt is made to that device.

If any client tries to perform discovery and the scan for the General
Connection Establishment procedure is active, the discovery request is
queued and performed right after the GCEP scan session finishes.

This series changes quite a lot the LE connection logic, but we've been
testing and using this code at INdT for about 4 weeks. Any comments and
more testing are appreciated.

Claudio Takahasi (6):
core: Control connections based on adapter state
mgmt: Add LE scanning callback
core: Replace interleaved by LE scanning
core: Start LE scanning when a device requests
core: Queue discovery if scanning is active
core: Re-connect for ECONNRESET or ECONNABORTED

João Paulo Rechi Vita (7):
mgmt: Print error message when start_discovery fails
core: Add compare function for bdaddr in a struct btd_device
core: Add a list of LE devices to connect
core: Use adapter connect list for LE connections
core: Mutually exclude concurrent connections
mgmt: Add address type to bonding debug message
core: Suspend scanning before connect on pairing

Paulo Alcantara (1):
core: Disable unnecessary auto connections

src/adapter.c | 193 ++++++++++++++++++++++++++++++++++++++++++++-------
src/adapter.h | 5 ++
src/device.c | 217 +++++++++++++++++++++++++++++-----------------------------
src/device.h | 2 +
src/mgmt.c | 44 +++++++++++-
src/mgmt.h | 1 +
6 files changed, 325 insertions(+), 137 deletions(-)

--
1.7.11.4



2012-09-11 13:34:13

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: Re: [PATCH BlueZ v5 01/14] core: Control connections based on adapter state

On Fri, Sep 7, 2012 at 6:59 AM, Johan Hedberg <[email protected]> wrote:
> Hi,
>
> On Tue, Sep 04, 2012, João Paulo Rechi Vita wrote:
>> +static void set_auto_connect(gpointer data, gpointer user_data)
>> +{
>> + struct btd_device *device = data;
>> + gboolean enable = GPOINTER_TO_INT(user_data);
>> +
>> + device_set_auto_connect(device, enable);
>> +}
>> +
>> static void call_adapter_powered_callbacks(struct btd_adapter *adapter,
>> gboolean powered)
>> {
>> @@ -2125,7 +2133,10 @@ static void call_adapter_powered_callbacks(struct btd_adapter *adapter,
>> btd_adapter_powered_cb cb = l->data;
>>
>> cb(adapter, powered);
>> - }
>> + }
>> +
>> + g_slist_foreach(adapter->devices, set_auto_connect,
>> + GINT_TO_POINTER(powered));
>
> Instead of this GINT_TO_POINTER magic on a variable that isn't even a
> gint couldn't you just pass &powered to g_slist_foreach and then in the
> callback do:
>
> gboolean enable = *(gboolean *) user_data;
>
> or
>
> gboolean *enable = user_data;
>
> device_set_auto_connect(device, *enable);
>

I'm sending an updated version now.

--
João Paulo Rechi Vita
Openbossa Labs - INdT

2012-09-07 10:23:14

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ v5 04/14] core: Add a list of LE devices to connect

Hi,

On Tue, Sep 04, 2012, Jo?o Paulo Rechi Vita wrote:
> This commit creates a per-adapter list of LE devices to connect when a
> advertising from them is seen during a scan.
> ---
> src/adapter.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
> src/adapter.h | 5 +++++
> 2 files changed, 63 insertions(+), 1 deletion(-)

I applied patches 2-4 as they didn't seem to directly depend on the
first one. So no need to resend those when resending this patch set.

Johan

2012-09-07 09:59:32

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ v5 01/14] core: Control connections based on adapter state

Hi,

On Tue, Sep 04, 2012, Jo?o Paulo Rechi Vita wrote:
> +static void set_auto_connect(gpointer data, gpointer user_data)
> +{
> + struct btd_device *device = data;
> + gboolean enable = GPOINTER_TO_INT(user_data);
> +
> + device_set_auto_connect(device, enable);
> +}
> +
> static void call_adapter_powered_callbacks(struct btd_adapter *adapter,
> gboolean powered)
> {
> @@ -2125,7 +2133,10 @@ static void call_adapter_powered_callbacks(struct btd_adapter *adapter,
> btd_adapter_powered_cb cb = l->data;
>
> cb(adapter, powered);
> - }
> + }
> +
> + g_slist_foreach(adapter->devices, set_auto_connect,
> + GINT_TO_POINTER(powered));

Instead of this GINT_TO_POINTER magic on a variable that isn't even a
gint couldn't you just pass &powered to g_slist_foreach and then in the
callback do:

gboolean enable = *(gboolean *) user_data;

or

gboolean *enable = user_data;

device_set_auto_connect(device, *enable);

Johan

2012-09-05 17:15:15

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: Re: [PATCH BlueZ v5 00/14] LE General Connection Establishment procedure

On Wed, Sep 5, 2012 at 2:00 AM, Chen Ganir <[email protected]> wrote:
> João,
>
>
> On 09/04/2012 10:04 PM, João Paulo Rechi Vita wrote:
>>
>> This series implement the LE General Connection Establishment procedure
>> for LE connections.
>>
>> If there are LE bonded devices marked for auto connection they are added
>> to a connect_list on the adapter. When there is any device on this list
>> scan is performed continuously. When a device is found the connect_list
>
> How do we stop this scan ? Do we need to remove all auto-connect settings?
>

The scan stops when there are no devices left on the connect_list. The
connect_list is the new interface to connect to LE devices, and it
will use the GCE procedure. The auto-connect flag here simply states
whether or not we should try to re-connect to a device after
disconnection (adding it back to the connect_list).

>
>> is checked. If that device is on the list scan is stopped and a
>> connection attempt is made to that device.
>
> Do we have a timeout for this ? What happens if we succeed ? Do we start
> scanning again if more devices are set to auto-connect ? What happens when
> we fail a connection ? Do we retry to the same or start scanning again ?
>

Yes, right now there is the L2CAP timeout of 40s for an LE connect. If
the connection fails the device is added back to the connect_list, on
the att_error_cb() on src/device.c. After a connect attempt finishes
(either with sucess or fail) we check if there are any devices on the
connect_list and if so, we restart scanning.

>
>>
>> If any client tries to perform discovery and the scan for the General
>> Connection Establishment procedure is active, the discovery request is
>> queued and performed right after the GCEP scan session finishes.
>>
> I'm am having difficulties understanding the logic here. You say that as
> long as we have bonded devices set for auto-connect, the GCE scan will run
> until a connection is made?. What happens if we do not find any device ? Do
> we scan forever?
>

Scan will be active until a all LE bonded devices are connected or
removed (unpaired).

> This is a bit of a problem - device discovery can not be queued. Discovering
> devices is a user initiated command, and it should run whenever a client
> requires it. When a user requests for a list of devices, he expects to get
> the list. He does not care about background logic. The proper logic here is
> to pause the background scanning, discover devices (if LE scan reveals
> bonded devices that have auto-connect setting they should connect and then
> resume the discovery). After Device discovery is terminated, background
> connect scan should be continued.
>

And that is exactly what is implemented right now :)

Thanks for the reviewing, Chen!

--
João Paulo Rechi Vita
Openbossa Labs - INdT

2012-09-05 05:00:54

by Ganir, Chen

[permalink] [raw]
Subject: Re: [PATCH BlueZ v5 00/14] LE General Connection Establishment procedure

João,

On 09/04/2012 10:04 PM, João Paulo Rechi Vita wrote:
> This series implement the LE General Connection Establishment procedure
> for LE connections.
>
> If there are LE bonded devices marked for auto connection they are added
> to a connect_list on the adapter. When there is any device on this list
> scan is performed continuously. When a device is found the connect_list
How do we stop this scan ? Do we need to remove all auto-connect settings?

> is checked. If that device is on the list scan is stopped and a
> connection attempt is made to that device.
Do we have a timeout for this ? What happens if we succeed ? Do we start
scanning again if more devices are set to auto-connect ? What happens
when we fail a connection ? Do we retry to the same or start scanning
again ?

>
> If any client tries to perform discovery and the scan for the General
> Connection Establishment procedure is active, the discovery request is
> queued and performed right after the GCEP scan session finishes.
>
I'm am having difficulties understanding the logic here. You say that as
long as we have bonded devices set for auto-connect, the GCE scan will
run until a connection is made?. What happens if we do not find any
device ? Do we scan forever?

This is a bit of a problem - device discovery can not be queued.
Discovering devices is a user initiated command, and it should run
whenever a client requires it. When a user requests for a list of
devices, he expects to get the list. He does not care about background
logic. The proper logic here is to pause the background scanning,
discover devices (if LE scan reveals bonded devices that have
auto-connect setting they should connect and then resume the discovery).
After Device discovery is terminated, background connect scan should be
continued.


> This series changes quite a lot the LE connection logic, but we've been
> testing and using this code at INdT for about 4 weeks. Any comments and
> more testing are appreciated.
>
> Claudio Takahasi (6):
> core: Control connections based on adapter state
> mgmt: Add LE scanning callback
> core: Replace interleaved by LE scanning
> core: Start LE scanning when a device requests
> core: Queue discovery if scanning is active
> core: Re-connect for ECONNRESET or ECONNABORTED
>
> João Paulo Rechi Vita (7):
> mgmt: Print error message when start_discovery fails
> core: Add compare function for bdaddr in a struct btd_device
> core: Add a list of LE devices to connect
> core: Use adapter connect list for LE connections
> core: Mutually exclude concurrent connections
> mgmt: Add address type to bonding debug message
> core: Suspend scanning before connect on pairing
>
> Paulo Alcantara (1):
> core: Disable unnecessary auto connections
>
> src/adapter.c | 193 ++++++++++++++++++++++++++++++++++++++++++++-------
> src/adapter.h | 5 ++
> src/device.c | 217 +++++++++++++++++++++++++++++-----------------------------
> src/device.h | 2 +
> src/mgmt.c | 44 +++++++++++-
> src/mgmt.h | 1 +
> 6 files changed, 325 insertions(+), 137 deletions(-)
>


BR,
Chen Ganir


2012-09-04 19:04:42

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 14/14] core: Suspend scanning before connect on pairing

If there is a disconnected bonded device there will be a scanning
procedure active due to the General Connection Establishment Procedure.
This scan have to be suspended before trying to connect to the remote
device for pairing.
---
src/device.c | 150 ++++++++++++++++++++++++++++++-----------------------------
1 file changed, 77 insertions(+), 73 deletions(-)

diff --git a/src/device.c b/src/device.c
index d0cebf4..81cd3b1 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1702,8 +1702,8 @@ send_reply:
else if (dbus_message_is_method_call(req->msg, ADAPTER_INTERFACE,
"CreateDevice")) {
if (err < 0) {
- DBusMessage *reply;
- reply = btd_error_failed(req->msg, strerror(-err));
+ DBusMessage *reply = btd_error_failed(req->msg,
+ strerror(-err));
g_dbus_send_message(req->conn, reply);
goto cleanup;
}
@@ -1915,6 +1915,38 @@ done:
browse_request_free(req);
}

+static void bonding_request_free(struct bonding_req *bonding)
+{
+ struct btd_device *device;
+
+ if (!bonding)
+ return;
+
+ if (bonding->listener_id)
+ g_dbus_remove_watch(bonding->conn, bonding->listener_id);
+
+ if (bonding->msg)
+ dbus_message_unref(bonding->msg);
+
+ if (bonding->conn)
+ dbus_connection_unref(bonding->conn);
+
+ device = bonding->device;
+ g_free(bonding);
+
+ if (!device)
+ return;
+
+ device->bonding = NULL;
+
+ if (!device->agent)
+ return;
+
+ agent_cancel(device->agent);
+ agent_free(device->agent);
+ device->agent = NULL;
+}
+
static void att_connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
{
struct att_callbacks *attcb = user_data;
@@ -1944,6 +1976,21 @@ static void att_connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)

if (attcb->success)
attcb->success(user_data);
+
+ if (device->bonding) {
+ /* this is a LE device during pairing */
+ int err = adapter_create_bonding(device->adapter,
+ &device->bdaddr, device->bdaddr_type,
+ agent_get_io_capability(device->agent));
+ if (err < 0) {
+ DBusMessage *reply = btd_error_failed(
+ device->bonding->msg, strerror(-err));
+ g_dbus_send_message(device->bonding->conn, reply);
+ bonding_request_cancel(device->bonding);
+ bonding_request_free(device->bonding);
+ }
+ }
+
done:
g_free(attcb);
}
@@ -1995,16 +2042,30 @@ GIOChannel *device_att_connect(gpointer user_data)
attcb->user_data = device;

if (device_is_bredr(device)) {
- io = bt_io_connect(att_connect_cb,
- attcb, NULL, &gerr,
+ io = bt_io_connect(att_connect_cb, attcb, NULL, &gerr,
BT_IO_OPT_SOURCE_BDADDR, &sba,
BT_IO_OPT_DEST_BDADDR, &device->bdaddr,
BT_IO_OPT_PSM, ATT_PSM,
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
BT_IO_OPT_INVALID);
+ } else if (device->bonding) {
+ /* this is a LE device during pairing, using low sec level */
+ io = bt_io_connect(att_connect_cb, attcb, NULL, &gerr,
+ BT_IO_OPT_SOURCE_BDADDR, &sba,
+ BT_IO_OPT_DEST_BDADDR, &device->bdaddr,
+ BT_IO_OPT_DEST_TYPE, device->bdaddr_type,
+ BT_IO_OPT_CID, ATT_CID,
+ BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
+ BT_IO_OPT_INVALID);
+ if (io == NULL) {
+ DBusMessage *reply = btd_error_failed(
+ device->bonding->msg, gerr->message);
+ g_dbus_send_message(device->bonding->conn, reply);
+ bonding_request_cancel(device->bonding);
+ bonding_request_free(device->bonding);
+ }
} else {
- io = bt_io_connect(att_connect_cb,
- attcb, NULL, &gerr,
+ io = bt_io_connect(att_connect_cb, attcb, NULL, &gerr,
BT_IO_OPT_SOURCE_BDADDR, &sba,
BT_IO_OPT_DEST_BDADDR, &device->bdaddr,
BT_IO_OPT_DEST_TYPE, device->bdaddr_type,
@@ -2330,38 +2391,6 @@ static DBusMessage *new_authentication_return(DBusMessage *msg, uint8_t status)
}
}

-static void bonding_request_free(struct bonding_req *bonding)
-{
- struct btd_device *device;
-
- if (!bonding)
- return;
-
- if (bonding->listener_id)
- g_dbus_remove_watch(bonding->conn, bonding->listener_id);
-
- if (bonding->msg)
- dbus_message_unref(bonding->msg);
-
- if (bonding->conn)
- dbus_connection_unref(bonding->conn);
-
- device = bonding->device;
- g_free(bonding);
-
- if (!device)
- return;
-
- device->bonding = NULL;
-
- if (!device->agent)
- return;
-
- agent_cancel(device->agent);
- agent_free(device->agent);
- device->agent = NULL;
-}
-
void device_set_paired(struct btd_device *device, gboolean value)
{
DBusConnection *conn = get_dbus_connection();
@@ -2455,41 +2484,6 @@ DBusMessage *device_create_bonding(struct btd_device *device,
if (device_is_bonded(device))
return btd_error_already_exists(msg);

- if (device_is_le(device)) {
- struct att_callbacks *attcb;
- GError *gerr = NULL;
- bdaddr_t sba;
-
- adapter_get_address(adapter, &sba);
-
- attcb = g_new0(struct att_callbacks, 1);
- attcb->user_data = device;
-
- device->att_io = bt_io_connect(att_connect_cb,
- attcb, NULL, &gerr,
- BT_IO_OPT_SOURCE_BDADDR, &sba,
- BT_IO_OPT_DEST_BDADDR, &device->bdaddr,
- BT_IO_OPT_DEST_TYPE, device->bdaddr_type,
- BT_IO_OPT_CID, ATT_CID,
- BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
- BT_IO_OPT_INVALID);
-
- if (device->att_io == NULL) {
- DBusMessage *reply = btd_error_failed(msg,
- gerr->message);
-
- error("Bonding bt_io_connect(): %s", gerr->message);
- g_error_free(gerr);
- g_free(attcb);
- return reply;
- }
- }
-
- err = adapter_create_bonding(adapter, &device->bdaddr,
- device->bdaddr_type, capability);
- if (err < 0)
- return btd_error_failed(msg, strerror(-err));
-
bonding = bonding_request_new(conn, msg, device, agent_path,
capability);

@@ -2501,6 +2495,16 @@ DBusMessage *device_create_bonding(struct btd_device *device,
device->bonding = bonding;
bonding->device = device;

+ if (device_is_le(device)) {
+ adapter_connect_list_add(adapter, device);
+ return NULL;
+ }
+
+ err = adapter_create_bonding(adapter, &device->bdaddr,
+ device->bdaddr_type, capability);
+ if (err < 0)
+ return btd_error_failed(msg, strerror(-err));
+
return NULL;
}

--
1.7.11.4


2012-09-04 19:04:41

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 13/14] mgmt: Add address type to bonding debug message

---
src/mgmt.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/mgmt.c b/src/mgmt.c
index 8e69778..991075c 100644
--- a/src/mgmt.c
+++ b/src/mgmt.c
@@ -2371,7 +2371,8 @@ int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t addr_type, uint8_t
char addr[18];

ba2str(bdaddr, addr);
- DBG("hci%d bdaddr %s io_cap 0x%02x", index, addr, io_cap);
+ DBG("hci%d bdaddr %s type %d io_cap 0x%02x",
+ index, addr, addr_type, io_cap);

memset(buf, 0, sizeof(buf));
hdr->opcode = htobs(MGMT_OP_PAIR_DEVICE);
--
1.7.11.4


2012-09-04 19:04:40

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 12/14] core: Re-connect for ECONNRESET or ECONNABORTED

From: Claudio Takahasi <[email protected]>

This patch keeps scanning and re-connections active if the disconnection
reason is ECONNRESET(Remote Initiated Disconnection).

Re-connection is a behaviour determined by Profiles or by the upper
layer(user actions). For instance, HoG requires re-connection always
active, no matter if the previous disconnection reason was page timeout
or remote initiated disconnection (ECONNRESET). Some devices disconnects
after some idle time, connectable advertises are sent by the peripheral
when commanded by the user(eg: key pressed). Disconnection can be also
triggered by the local host (ECONNABORTED) using command line tools or
Disconnect method in the Device interface.

The peripheral dictates the re-connection controlling the connectable
advertises, BlueZ(central) needs to keep the scanning always active to
able to detect the advertises and trigger the connection.
---
src/device.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/device.c b/src/device.c
index f9a0f3c..d0cebf4 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1845,10 +1845,18 @@ static gboolean attrib_disconnected_cb(GIOChannel *io, GIOCondition cond,

g_slist_foreach(device->attios, attio_disconnected, NULL);

- if (device->auto_connect == FALSE || err != ETIMEDOUT)
+ if (device->auto_connect == FALSE) {
+ DBG("Automatic connection disabled");
goto done;
+ }

- adapter_connect_list_add(device_get_adapter(device), device);
+ /*
+ * Keep scanning/re-connection active if disconnection reason
+ * is page timeout, remote user terminated connection or local
+ * initiated disconnection.
+ */
+ if (err == ETIMEDOUT || err == ECONNRESET || err == ECONNABORTED)
+ adapter_connect_list_add(device_get_adapter(device), device);

done:
attio_cleanup(device);
--
1.7.11.4


2012-09-04 19:04:39

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 11/14] core: Disable unnecessary auto connections

From: Paulo Alcantara <[email protected]>

BlueZ host disconnects the link when encryption fails. ECONNABORTED
error is returned by the kernel when the connection is terminated by the
local host. This scenario commonly happens when authentication fails due
PIN or Key Missing.
---
src/device.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/src/device.c b/src/device.c
index 92bf4ae..f9a0f3c 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1945,6 +1945,9 @@ static void att_error_cb(const GError *gerr, gpointer user_data)
struct att_callbacks *attcb = user_data;
struct btd_device *device = attcb->user_data;

+ if (g_error_matches(gerr, BT_IO_ERROR, ECONNABORTED))
+ return;
+
if (device->auto_connect == FALSE)
return;

--
1.7.11.4


2012-09-04 19:04:38

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 10/14] core: Queue discovery if scanning is active

From: Claudio Takahasi <[email protected]>

This patch manages BR/EDR inquiry and BLE scanning discovery sessions.
A scanning session is added in the discovery session list when there is
a bonded device which requires re-connection.

bluetoothd decides if interleaved or scanning needs to be executed based
on the queued discovery sessions. Interleaved discovery has higher
priority, scanning only is executed when there is only a scanning
session active.
---
src/adapter.c | 67 +++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 47 insertions(+), 20 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 70fbc02..8df97eb 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -133,6 +133,7 @@ struct btd_adapter {
GSList *devices; /* Devices structure pointers */
GSList *mode_sessions; /* Request Mode sessions */
GSList *disc_sessions; /* Discovery sessions */
+ struct session_req *scanning_session;
GSList *connect_list; /* Devices to connect when found */
guint discov_id; /* Discovery timer */
gboolean discovering; /* Discovery active */
@@ -222,18 +223,19 @@ static struct session_req *create_session(struct btd_adapter *adapter,
DBusConnection *conn, DBusMessage *msg,
uint8_t mode, GDBusWatchFunction cb)
{
- const char *sender = dbus_message_get_sender(msg);
+ const char *sender;
struct session_req *req;

req = g_new0(struct session_req, 1);
req->adapter = adapter;
- req->conn = dbus_connection_ref(conn);
- req->msg = dbus_message_ref(msg);
req->mode = mode;

- if (cb == NULL)
+ if (conn == NULL || cb == NULL || msg == NULL)
return session_ref(req);

+ req->conn = dbus_connection_ref(conn);
+ req->msg = dbus_message_ref(msg);
+ sender = dbus_message_get_sender(msg);
req->owner = g_strdup(sender);
req->id = g_dbus_add_disconnect_watch(conn, sender, cb, req, NULL);

@@ -445,7 +447,9 @@ static struct session_req *find_session(GSList *list, const char *sender)
for (; list; list = list->next) {
struct session_req *req = list->data;

- if (g_str_equal(req->owner, sender))
+ /* req->owner may be NULL if the session has been added by the
+ * daemon itself, so we use g_strcmp0 instead of g_str_equal */
+ if (g_strcmp0(req->owner, sender) == 0)
return req;
}

@@ -520,7 +524,7 @@ static void session_remove(struct session_req *req)
struct btd_adapter *adapter = req->adapter;

/* Ignore set_mode session */
- if (req->owner == NULL)
+ if (req->owner == NULL && adapter->pending_mode)
return;

DBG("%s session %p with %s deactivated",
@@ -1009,7 +1013,12 @@ static gboolean discovery_cb(gpointer user_data)
struct btd_adapter *adapter = user_data;

adapter->discov_id = 0;
- mgmt_start_discovery(adapter->dev_id);
+
+ if (adapter->scanning_session &&
+ (g_slist_length(adapter->disc_sessions) == 1))
+ mgmt_start_scanning(adapter->dev_id);
+ else
+ mgmt_start_discovery(adapter->dev_id);

return FALSE;
}
@@ -2205,6 +2214,8 @@ const char *btd_adapter_get_name(struct btd_adapter *adapter)
void adapter_connect_list_add(struct btd_adapter *adapter,
struct btd_device *device)
{
+ struct session_req *req;
+
if (g_slist_find(adapter->connect_list, device)) {
DBG("ignoring already added device %s",
device_get_path(device));
@@ -2216,10 +2227,21 @@ void adapter_connect_list_add(struct btd_adapter *adapter,
DBG("%s added to %s's connect_list", device_get_path(device),
adapter->name);

- if (adapter->disc_sessions)
+ if (!adapter->up)
+ return;
+
+ if (adapter->off_requested)
+ return;
+
+ if (adapter->scanning_session)
return;

- mgmt_start_scanning(adapter->dev_id);
+ if (adapter->disc_sessions == NULL)
+ adapter->discov_id = g_idle_add(discovery_cb, adapter);
+
+ req = create_session(adapter, NULL, NULL, 0, NULL);
+ adapter->disc_sessions = g_slist_append(adapter->disc_sessions, req);
+ adapter->scanning_session = req;
}

void adapter_connect_list_remove(struct btd_adapter *adapter,
@@ -2239,6 +2261,7 @@ void adapter_connect_list_remove(struct btd_adapter *adapter,

void btd_adapter_start(struct btd_adapter *adapter)
{
+ struct session_req *req;
char address[18];
gboolean powered;

@@ -2265,8 +2288,15 @@ void btd_adapter_start(struct btd_adapter *adapter)

info("Adapter %s has been enabled", adapter->path);

- if (g_slist_length(adapter->connect_list))
- mgmt_start_scanning(adapter->dev_id);
+ if (g_slist_length(adapter->connect_list) == 0 ||
+ adapter->disc_sessions)
+ return;
+
+ req = create_session(adapter, NULL, NULL, 0, NULL);
+ adapter->disc_sessions = g_slist_append(adapter->disc_sessions, req);
+ adapter->scanning_session = req;
+
+ adapter->discov_id = g_idle_add(discovery_cb, adapter);
}

static void reply_pending_requests(struct btd_adapter *adapter)
@@ -2610,6 +2640,11 @@ void adapter_set_discovering(struct btd_adapter *adapter,

connect_list_size = g_slist_length(adapter->connect_list);

+ if (connect_list_size == 0 && adapter->scanning_session) {
+ session_unref(adapter->scanning_session);
+ adapter->scanning_session = NULL;
+ }
+
if (adapter_has_discov_sessions(adapter)) {
adapter->discov_id = g_idle_add(discovery_cb, adapter);

@@ -2618,14 +2653,6 @@ void adapter_set_discovering(struct btd_adapter *adapter,
g_slist_length(adapter->disc_sessions));
return;
}
-
- if (connect_list_size) {
- mgmt_start_scanning(adapter->dev_id);
-
- DBG("hci%u restarting scanning connect_list size %u",
- adapter->dev_id, connect_list_size);
- return;
- }
}

static void suspend_discovery(struct btd_adapter *adapter)
@@ -2940,7 +2967,7 @@ static gboolean clean_connecting_state(GIOChannel *io, GIOCondition cond, gpoint

if (adapter->waiting_to_connect == 0 &&
g_slist_length(adapter->connect_list))
- mgmt_start_scanning(adapter->dev_id);
+ adapter->discov_id = g_idle_add(discovery_cb, adapter);

btd_device_unref(device);
return FALSE;
--
1.7.11.4


2012-09-04 19:04:37

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 09/14] core: Start LE scanning when a device requests

From: Claudio Takahasi <[email protected]>

This patch enables the LE scanning when a device requires connection and
there isn't discovery sessions, triggering the General Connection
Establishment Procedure.
---
src/adapter.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/src/adapter.c b/src/adapter.c
index 0355a4c..70fbc02 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2215,6 +2215,11 @@ void adapter_connect_list_add(struct btd_adapter *adapter,
btd_device_ref(device));
DBG("%s added to %s's connect_list", device_get_path(device),
adapter->name);
+
+ if (adapter->disc_sessions)
+ return;
+
+ mgmt_start_scanning(adapter->dev_id);
}

void adapter_connect_list_remove(struct btd_adapter *adapter,
--
1.7.11.4


2012-09-04 19:04:36

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 08/14] core: Replace interleaved by LE scanning

From: Claudio Takahasi <[email protected]>

This patches replaces the interleaved discovery by LE scanning when LE
re-connection is required.
---
src/adapter.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index e1c4fe3..0355a4c 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2261,7 +2261,7 @@ void btd_adapter_start(struct btd_adapter *adapter)
info("Adapter %s has been enabled", adapter->path);

if (g_slist_length(adapter->connect_list))
- mgmt_start_discovery(adapter->dev_id);
+ mgmt_start_scanning(adapter->dev_id);
}

static void reply_pending_requests(struct btd_adapter *adapter)
@@ -2605,14 +2605,22 @@ void adapter_set_discovering(struct btd_adapter *adapter,

connect_list_size = g_slist_length(adapter->connect_list);

- if (!adapter_has_discov_sessions(adapter) && !connect_list_size)
+ if (adapter_has_discov_sessions(adapter)) {
+ adapter->discov_id = g_idle_add(discovery_cb, adapter);
+
+ DBG("hci%u restarting discovery: disc_sessions %u",
+ adapter->dev_id,
+ g_slist_length(adapter->disc_sessions));
return;
+ }

- DBG("hci%u restarting discovery: disc_sessions %u, connect_list size "
- "%u", adapter->dev_id, g_slist_length(adapter->disc_sessions),
- connect_list_size);
+ if (connect_list_size) {
+ mgmt_start_scanning(adapter->dev_id);

- adapter->discov_id = g_idle_add(discovery_cb, adapter);
+ DBG("hci%u restarting scanning connect_list size %u",
+ adapter->dev_id, connect_list_size);
+ return;
+ }
}

static void suspend_discovery(struct btd_adapter *adapter)
@@ -2927,7 +2935,7 @@ static gboolean clean_connecting_state(GIOChannel *io, GIOCondition cond, gpoint

if (adapter->waiting_to_connect == 0 &&
g_slist_length(adapter->connect_list))
- mgmt_start_discovery(adapter->dev_id);
+ mgmt_start_scanning(adapter->dev_id);

btd_device_unref(device);
return FALSE;
--
1.7.11.4


2012-09-04 19:04:35

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 07/14] mgmt: Add LE scanning callback

From: Claudio Takahasi <[email protected]>

This patch adds a new callback to allow the adapter to control LE
scanning. The current approach uses the active scanning with default
windows and intervals defined by the core spec without any filtering.
---
src/mgmt.c | 34 ++++++++++++++++++++++++++++++++++
src/mgmt.h | 1 +
2 files changed, 35 insertions(+)

diff --git a/src/mgmt.c b/src/mgmt.c
index 4643816..8e69778 100644
--- a/src/mgmt.c
+++ b/src/mgmt.c
@@ -2043,6 +2043,40 @@ int mgmt_start_discovery(int index)
return 0;
}

+int mgmt_start_scanning(int index)
+{
+ char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
+ struct mgmt_hdr *hdr = (void *) buf;
+ struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)];
+ struct controller_info *info = &controllers[index];
+
+ DBG("index %d", index);
+
+ if (!mgmt_low_energy(info->current_settings)) {
+ error("scanning failed: Low Energy not enabled/supported");
+ return -ENOTSUP;
+ }
+
+ info->discov_type = 0;
+ hci_set_bit(BDADDR_LE_PUBLIC, &info->discov_type);
+ hci_set_bit(BDADDR_LE_RANDOM, &info->discov_type);
+
+ memset(buf, 0, sizeof(buf));
+ hdr->opcode = htobs(MGMT_OP_START_DISCOVERY);
+ hdr->len = htobs(sizeof(*cp));
+ hdr->index = htobs(index);
+
+ cp->type = info->discov_type;
+
+ if (write(mgmt_sock, buf, sizeof(buf)) < 0) {
+ int err = -errno;
+ error("failed to write to MGMT socket: %s", strerror(-err));
+ return err;
+ }
+
+ return 0;
+}
+
int mgmt_stop_discovery(int index)
{
char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
diff --git a/src/mgmt.h b/src/mgmt.h
index 95245d2..eb7434a 100644
--- a/src/mgmt.h
+++ b/src/mgmt.h
@@ -33,6 +33,7 @@ int mgmt_set_dev_class(int index, uint8_t major, uint8_t minor);
int mgmt_set_fast_connectable(int index, gboolean enable);

int mgmt_start_discovery(int index);
+int mgmt_start_scanning(int index);
int mgmt_stop_discovery(int index);

int mgmt_read_clock(int index, bdaddr_t *bdaddr, int which, int timeout,
--
1.7.11.4


2012-09-04 19:04:34

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 06/14] core: Mutually exclude concurrent connections

Since controllers don't support more than one ongoing connection
procedure at the same time, new connection attempts needs to yield if
there is an ongoing connection procedure already.
---
src/adapter.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++--------
src/device.c | 6 +++---
src/device.h | 2 +-
3 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 298a9cd..e1c4fe3 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -136,6 +136,8 @@ struct btd_adapter {
GSList *connect_list; /* Devices to connect when found */
guint discov_id; /* Discovery timer */
gboolean discovering; /* Discovery active */
+ gboolean connecting; /* Connect active */
+ guint waiting_to_connect; /* # of devices waiting to connect */
gboolean discov_suspended; /* Discovery suspended */
guint auto_timeout_id; /* Automatic connections timeout */
sdp_list_t *services; /* Services associated to adapter */
@@ -2257,6 +2259,9 @@ void btd_adapter_start(struct btd_adapter *adapter)
call_adapter_powered_callbacks(adapter, TRUE);

info("Adapter %s has been enabled", adapter->path);
+
+ if (g_slist_length(adapter->connect_list))
+ mgmt_start_discovery(adapter->dev_id);
}

static void reply_pending_requests(struct btd_adapter *adapter)
@@ -2580,6 +2585,7 @@ void adapter_set_discovering(struct btd_adapter *adapter,
gboolean discovering)
{
const char *path = adapter->path;
+ guint connect_list_size;

adapter->discovering = discovering;

@@ -2594,11 +2600,17 @@ void adapter_set_discovering(struct btd_adapter *adapter,
g_slist_free_full(adapter->oor_devices, dev_info_free);
adapter->oor_devices = g_slist_copy(adapter->found_devices);

- if (!adapter_has_discov_sessions(adapter) || adapter->discov_suspended)
+ if (adapter->discov_suspended)
+ return;
+
+ connect_list_size = g_slist_length(adapter->connect_list);
+
+ if (!adapter_has_discov_sessions(adapter) && !connect_list_size)
return;

- DBG("hci%u restarting discovery, disc_sessions %u", adapter->dev_id,
- g_slist_length(adapter->disc_sessions));
+ DBG("hci%u restarting discovery: disc_sessions %u, connect_list size "
+ "%u", adapter->dev_id, g_slist_length(adapter->disc_sessions),
+ connect_list_size);

adapter->discov_id = g_idle_add(discovery_cb, adapter);
}
@@ -2906,18 +2918,44 @@ static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer,
return textfile_get(filename, key);
}

+static gboolean clean_connecting_state(GIOChannel *io, GIOCondition cond, gpointer user_data)
+{
+ struct btd_device *device = user_data;
+ struct btd_adapter *adapter = device_get_adapter(device);
+
+ adapter->connecting = FALSE;
+
+ if (adapter->waiting_to_connect == 0 &&
+ g_slist_length(adapter->connect_list))
+ mgmt_start_discovery(adapter->dev_id);
+
+ btd_device_unref(device);
+ return FALSE;
+}
+
static gboolean connect_pending_cb(gpointer user_data)
{
struct btd_device *device = user_data;
struct btd_adapter *adapter = device_get_adapter(device);
+ GIOChannel *io;

/* in the future we may want to check here if the controller supports
* scanning and connecting at the same time */
if (adapter->discovering)
return TRUE;

- device_att_connect(device);
+ if (adapter->connecting)
+ return TRUE;
+
+ adapter->connecting = TRUE;
+ adapter->waiting_to_connect--;

+ io = device_att_connect(device);
+ g_io_add_watch(io, G_IO_OUT | G_IO_ERR, clean_connecting_state,
+ btd_device_ref(device));
+ g_io_channel_unref(io);
+
+ btd_device_unref(device);
return FALSE;
}

@@ -3009,12 +3047,19 @@ void adapter_update_found_devices(struct btd_adapter *adapter,

if (bdaddr_type == BDADDR_LE_PUBLIC ||
bdaddr_type == BDADDR_LE_RANDOM) {
+ struct btd_device *device;
+
l = g_slist_find_custom(adapter->connect_list, bdaddr,
(GCompareFunc) device_bdaddr_cmp);
- if (l) {
- g_idle_add(connect_pending_cb, l->data);
- stop_discovery(adapter);
- }
+ if (!l)
+ goto done;
+
+ device = l->data;
+ adapter_connect_list_remove(adapter, device);
+
+ g_idle_add(connect_pending_cb, btd_device_ref(device));
+ stop_discovery(adapter);
+ adapter->waiting_to_connect++;
}

done:
diff --git a/src/device.c b/src/device.c
index cc8a77a..92bf4ae 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1963,7 +1963,7 @@ static void att_success_cb(gpointer user_data)
g_slist_foreach(device->attios, attio_connected, device->attrib);
}

-gboolean device_att_connect(gpointer user_data)
+GIOChannel *device_att_connect(gpointer user_data)
{
struct btd_device *device = user_data;
struct btd_adapter *adapter = device->adapter;
@@ -2006,12 +2006,12 @@ gboolean device_att_connect(gpointer user_data)
error("ATT bt_io_connect(%s): %s", addr, gerr->message);
g_error_free(gerr);
g_free(attcb);
- return FALSE;
+ return NULL;
}

device->att_io = io;

- return FALSE;
+ return g_io_channel_ref(io);
}

static void att_browse_error_cb(const GError *gerr, gpointer user_data)
diff --git a/src/device.h b/src/device.h
index ab70f90..1b5be4e 100644
--- a/src/device.h
+++ b/src/device.h
@@ -158,4 +158,4 @@ int device_unblock(DBusConnection *conn, struct btd_device *device,
void device_set_pnpid(struct btd_device *device, uint8_t vendor_id_src,
uint16_t vendor_id, uint16_t product_id,
uint16_t product_ver);
-gboolean device_att_connect(gpointer user_data);
+GIOChannel *device_att_connect(gpointer user_data);
--
1.7.11.4


2012-09-04 19:04:33

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 05/14] core: Use adapter connect list for LE connections

When a connection is needed for a LE device it is added to the adapter
connect list instead of directly connecting the ATT io channel.
---
src/adapter.c | 2 +-
src/device.c | 45 +++++++++++----------------------------------
src/device.h | 1 +
3 files changed, 13 insertions(+), 35 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index c17f518..298a9cd 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2916,7 +2916,7 @@ static gboolean connect_pending_cb(gpointer user_data)
if (adapter->discovering)
return TRUE;

- /* TODO: call device connect callback */
+ device_att_connect(device);

return FALSE;
}
diff --git a/src/device.c b/src/device.c
index c65f0b7..cc8a77a 100644
--- a/src/device.c
+++ b/src/device.c
@@ -68,8 +68,6 @@
#define DISCONNECT_TIMER 2
#define DISCOVERY_TIMER 2

-#define AUTO_CONNECTION_INTERVAL 5 /* Next connection attempt */
-
struct btd_disconnect_data {
guint id;
disconnect_watch watch;
@@ -1831,15 +1829,6 @@ static void attio_disconnected(gpointer data, gpointer user_data)
attio->dcfunc(attio->user_data);
}

-static void att_connect_dispatched(gpointer user_data)
-{
- struct btd_device *device = user_data;
-
- device->auto_id = 0;
-}
-
-static gboolean att_connect(gpointer user_data);
-
static gboolean attrib_disconnected_cb(GIOChannel *io, GIOCondition cond,
gpointer user_data)
{
@@ -1859,10 +1848,7 @@ static gboolean attrib_disconnected_cb(GIOChannel *io, GIOCondition cond,
if (device->auto_connect == FALSE || err != ETIMEDOUT)
goto done;

- device->auto_id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,
- AUTO_CONNECTION_INTERVAL,
- att_connect, device,
- att_connect_dispatched);
+ adapter_connect_list_add(device_get_adapter(device), device);

done:
attio_cleanup(device);
@@ -1962,11 +1948,7 @@ static void att_error_cb(const GError *gerr, gpointer user_data)
if (device->auto_connect == FALSE)
return;

- device->auto_id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,
- AUTO_CONNECTION_INTERVAL,
- att_connect, device,
- att_connect_dispatched);
-
+ adapter_connect_list_add(device_get_adapter(device), device);
DBG("Enabling automatic connections");
}

@@ -1981,7 +1963,7 @@ static void att_success_cb(gpointer user_data)
g_slist_foreach(device->attios, attio_connected, device->attrib);
}

-static gboolean att_connect(gpointer user_data)
+gboolean device_att_connect(gpointer user_data)
{
struct btd_device *device = user_data;
struct btd_adapter *adapter = device->adapter;
@@ -2243,6 +2225,9 @@ void device_set_temporary(struct btd_device *device, gboolean temporary)

DBG("temporary %d", temporary);

+ if (temporary)
+ adapter_connect_list_remove(device_get_adapter(device), device);
+
device->temporary = temporary;
}

@@ -2258,6 +2243,7 @@ void device_set_bonded(struct btd_device *device, gboolean bonded)

void device_set_auto_connect(struct btd_device *device, gboolean enable)
{
+ struct btd_adapter *adapter = device_get_adapter(device);
char addr[18];

if (!device)
@@ -2271,15 +2257,10 @@ void device_set_auto_connect(struct btd_device *device, gboolean enable)

/* Disabling auto connect */
if (enable == FALSE) {
- if (device->auto_id)
- g_source_remove(device->auto_id);
+ adapter_connect_list_remove(adapter, device);
return;
}

- /* Enabling auto connect */
- if (device->auto_id != 0)
- return;
-
if (device->attrib) {
DBG("Already connected");
return;
@@ -2288,9 +2269,8 @@ void device_set_auto_connect(struct btd_device *device, gboolean enable)
if (device->attios == NULL && device->attios_offline == NULL)
return;

- device->auto_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
- att_connect, device,
- att_connect_dispatched);
+ /* Enabling auto connect */
+ adapter_connect_list_add(adapter, device);
}

static gboolean start_discovery(gpointer user_data)
@@ -3191,10 +3171,7 @@ guint btd_device_add_attio_callback(struct btd_device *device,

device->attios = g_slist_append(device->attios, attio);

- if (device->auto_id == 0)
- device->auto_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
- att_connect, device,
- att_connect_dispatched);
+ adapter_connect_list_add(device_get_adapter(device), device);

return attio->id;
}
diff --git a/src/device.h b/src/device.h
index f1d95c6..ab70f90 100644
--- a/src/device.h
+++ b/src/device.h
@@ -158,3 +158,4 @@ int device_unblock(DBusConnection *conn, struct btd_device *device,
void device_set_pnpid(struct btd_device *device, uint8_t vendor_id_src,
uint16_t vendor_id, uint16_t product_id,
uint16_t product_ver);
+gboolean device_att_connect(gpointer user_data);
--
1.7.11.4


2012-09-04 19:04:32

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 04/14] core: Add a list of LE devices to connect

This commit creates a per-adapter list of LE devices to connect when a
advertising from them is seen during a scan.
---
src/adapter.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
src/adapter.h | 5 +++++
2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/src/adapter.c b/src/adapter.c
index 771a956..c17f518 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -133,6 +133,7 @@ struct btd_adapter {
GSList *devices; /* Devices structure pointers */
GSList *mode_sessions; /* Request Mode sessions */
GSList *disc_sessions; /* Discovery sessions */
+ GSList *connect_list; /* Devices to connect when found */
guint discov_id; /* Discovery timer */
gboolean discovering; /* Discovery active */
gboolean discov_suspended; /* Discovery suspended */
@@ -2199,6 +2200,36 @@ const char *btd_adapter_get_name(struct btd_adapter *adapter)
return adapter->name;
}

+void adapter_connect_list_add(struct btd_adapter *adapter,
+ struct btd_device *device)
+{
+ if (g_slist_find(adapter->connect_list, device)) {
+ DBG("ignoring already added device %s",
+ device_get_path(device));
+ return;
+ }
+
+ adapter->connect_list = g_slist_append(adapter->connect_list,
+ btd_device_ref(device));
+ DBG("%s added to %s's connect_list", device_get_path(device),
+ adapter->name);
+}
+
+void adapter_connect_list_remove(struct btd_adapter *adapter,
+ struct btd_device *device)
+{
+ if (!g_slist_find(adapter->connect_list, device)) {
+ DBG("device %s is not on the list, ignoring",
+ device_get_path(device));
+ return;
+ }
+
+ adapter->connect_list = g_slist_remove(adapter->connect_list, device);
+ DBG("%s removed from %s's connect_list", device_get_path(device),
+ adapter->name);
+ btd_device_unref(device);
+}
+
void btd_adapter_start(struct btd_adapter *adapter)
{
char address[18];
@@ -2875,6 +2906,21 @@ static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer,
return textfile_get(filename, key);
}

+static gboolean connect_pending_cb(gpointer user_data)
+{
+ struct btd_device *device = user_data;
+ struct btd_adapter *adapter = device_get_adapter(device);
+
+ /* in the future we may want to check here if the controller supports
+ * scanning and connecting at the same time */
+ if (adapter->discovering)
+ return TRUE;
+
+ /* TODO: call device connect callback */
+
+ return FALSE;
+}
+
void adapter_update_found_devices(struct btd_adapter *adapter,
bdaddr_t *bdaddr, uint8_t bdaddr_type,
int8_t rssi, uint8_t confirm_name,
@@ -2886,6 +2932,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
gboolean legacy, name_known;
uint32_t dev_class;
int err;
+ GSList *l;

memset(&eir_data, 0, sizeof(eir_data));
err = eir_parse(&eir_data, data, data_len);
@@ -2908,7 +2955,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
eir_data.name);

dev = adapter_search_found_devices(adapter, bdaddr);
- if (dev) {
+ if (dev && dev->bdaddr_type == BDADDR_BREDR) {
adapter->oor_devices = g_slist_remove(adapter->oor_devices,
dev);

@@ -2960,6 +3007,16 @@ void adapter_update_found_devices(struct btd_adapter *adapter,

adapter->found_devices = g_slist_prepend(adapter->found_devices, dev);

+ if (bdaddr_type == BDADDR_LE_PUBLIC ||
+ bdaddr_type == BDADDR_LE_RANDOM) {
+ l = g_slist_find_custom(adapter->connect_list, bdaddr,
+ (GCompareFunc) device_bdaddr_cmp);
+ if (l) {
+ g_idle_add(connect_pending_cb, l->data);
+ stop_discovery(adapter);
+ }
+ }
+
done:
dev->rssi = rssi;

diff --git a/src/adapter.h b/src/adapter.h
index 5a0247e..cd37b15 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -232,3 +232,8 @@ int btd_adapter_gatt_server_start(struct btd_adapter *adapter);
void btd_adapter_gatt_server_stop(struct btd_adapter *adapter);

int btd_adapter_ssp_enabled(struct btd_adapter *adapter);
+
+void adapter_connect_list_add(struct btd_adapter *adapter,
+ struct btd_device *device);
+void adapter_connect_list_remove(struct btd_adapter *adapter,
+ struct btd_device *device);
--
1.7.11.4


2012-09-04 19:04:31

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 03/14] core: Add compare function for bdaddr in a struct btd_device

This is a utility function similar to device_address_cmp but comparing
bdaddr instead of the string representing the address. This way is
possible to avoid allocating two buffers to temporarily hold the
strings, two sprintf() calls to generate the strings from the bdaddr
arrays, and a string comparison, substituting all of it for one memcmp()
call.
---
src/device.c | 5 +++++
src/device.h | 1 +
2 files changed, 6 insertions(+)

diff --git a/src/device.c b/src/device.c
index d2bac97..c65f0b7 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1228,6 +1228,11 @@ gint device_address_cmp(struct btd_device *device, const gchar *address)
return strcasecmp(addr, address);
}

+gint device_bdaddr_cmp(struct btd_device *device, bdaddr_t *bdaddr)
+{
+ return bacmp(&device->bdaddr, bdaddr);
+}
+
static gboolean record_has_uuid(const sdp_record_t *rec,
const char *profile_uuid)
{
diff --git a/src/device.h b/src/device.h
index edc64b9..f1d95c6 100644
--- a/src/device.h
+++ b/src/device.h
@@ -77,6 +77,7 @@ uint16_t btd_device_get_product(struct btd_device *device);
uint16_t btd_device_get_version(struct btd_device *device);
void device_remove(struct btd_device *device, gboolean remove_stored);
gint device_address_cmp(struct btd_device *device, const gchar *address);
+gint device_bdaddr_cmp(struct btd_device *device, bdaddr_t *bdaddr);
int device_browse_primary(struct btd_device *device, DBusConnection *conn,
DBusMessage *msg, gboolean secure);
int device_browse_sdp(struct btd_device *device, DBusConnection *conn,
--
1.7.11.4


2012-09-04 19:04:30

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 02/14] mgmt: Print error message when start_discovery fails

If we fail to communicate with the MGMT socket is better to print the
error message on the mgmtops plugin, where it really happened, instead
of leaving this job to its users.
---
src/adapter.c | 6 +-----
src/mgmt.c | 7 +++++--
2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index d4ba4fc..771a956 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1004,13 +1004,9 @@ struct btd_device *adapter_get_device(DBusConnection *conn,
static gboolean discovery_cb(gpointer user_data)
{
struct btd_adapter *adapter = user_data;
- int err;

adapter->discov_id = 0;
-
- err = mgmt_start_discovery(adapter->dev_id);
- if (err < 0)
- error("start_discovery: %s (%d)", strerror(-err), -err);
+ mgmt_start_discovery(adapter->dev_id);

return FALSE;
}
diff --git a/src/mgmt.c b/src/mgmt.c
index 0ec2912..4643816 100644
--- a/src/mgmt.c
+++ b/src/mgmt.c
@@ -2034,8 +2034,11 @@ int mgmt_start_discovery(int index)

cp->type = info->discov_type;

- if (write(mgmt_sock, buf, sizeof(buf)) < 0)
- return -errno;
+ if (write(mgmt_sock, buf, sizeof(buf)) < 0) {
+ int err = -errno;
+ error("failed to write to MGMT socket: %s", strerror(-err));
+ return err;
+ }

return 0;
}
--
1.7.11.4


2012-09-04 19:04:29

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ v5 01/14] core: Control connections based on adapter state

From: Claudio Takahasi <[email protected]>

This patch disable automatic ATTIO connections when the adapter is
powered down and enable automatic connection when the adapter is powered
on.
---
src/adapter.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index f507aa5..d4ba4fc 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2116,6 +2116,14 @@ static int get_pairable_timeout(const char *src)
return main_opts.pairto;
}

+static void set_auto_connect(gpointer data, gpointer user_data)
+{
+ struct btd_device *device = data;
+ gboolean enable = GPOINTER_TO_INT(user_data);
+
+ device_set_auto_connect(device, enable);
+}
+
static void call_adapter_powered_callbacks(struct btd_adapter *adapter,
gboolean powered)
{
@@ -2125,7 +2133,10 @@ static void call_adapter_powered_callbacks(struct btd_adapter *adapter,
btd_adapter_powered_cb cb = l->data;

cb(adapter, powered);
- }
+ }
+
+ g_slist_foreach(adapter->devices, set_auto_connect,
+ GINT_TO_POINTER(powered));
}

static void emit_device_disappeared(gpointer data, gpointer user_data)
@@ -3348,15 +3359,10 @@ static gboolean disable_auto(gpointer user_data)
return FALSE;
}

-static void set_auto_connect(gpointer data, gpointer user_data)
-{
- struct btd_device *device = data;
-
- device_set_auto_connect(device, TRUE);
-}
-
void btd_adapter_enable_auto_connect(struct btd_adapter *adapter)
{
+ gboolean enable = TRUE;
+
if (!adapter->up)
return;

@@ -3365,7 +3371,8 @@ void btd_adapter_enable_auto_connect(struct btd_adapter *adapter)
if (adapter->auto_timeout_id)
return;

- g_slist_foreach(adapter->devices, set_auto_connect, NULL);
+ g_slist_foreach(adapter->devices, set_auto_connect,
+ GINT_TO_POINTER(enable));

adapter->auto_timeout_id = g_timeout_add_seconds(main_opts.autoto,
disable_auto, adapter);
--
1.7.11.4