2011-02-24 20:34:26

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH 1/3] Fix gattrib.c coding style

Just remove an extra empty line.
---
attrib/gattrib.c | 1 -
1 files changed, 0 insertions(+), 1 deletions(-)

diff --git a/attrib/gattrib.c b/attrib/gattrib.c
index 779a471..5b8b590 100644
--- a/attrib/gattrib.c
+++ b/attrib/gattrib.c
@@ -195,7 +195,6 @@ void g_attrib_unref(GAttrib *attrib)
g_io_channel_unref(attrib->io);
}

-
if (attrib->destroy)
attrib->destroy(attrib->destroy_user_data);

--
1.7.4.1



2011-02-24 20:47:20

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH 3/3] gatttool: Remove extra reference to the connection IO Channel

Hi Vinicius,

On Thu, Feb 24, 2011, Vinicius Costa Gomes wrote:
> As we want the connection to be closed when the last GAttrib
> reference is dropped, we don't need to keep this reference.
> ---
> attrib/gatttool.c | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)

All three patches have been pushed upstream. Thanks.

Johan

2011-02-24 20:34:28

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH 3/3] gatttool: Remove extra reference to the connection IO Channel

As we want the connection to be closed when the last GAttrib
reference is dropped, we don't need to keep this reference.
---
attrib/gatttool.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/attrib/gatttool.c b/attrib/gatttool.c
index 7478043..547757d 100644
--- a/attrib/gatttool.c
+++ b/attrib/gatttool.c
@@ -620,6 +620,7 @@ int main(int argc, char *argv[])
}

attrib = g_attrib_new(chan);
+ g_io_channel_unref(chan);

event_loop = g_main_loop_new(NULL, FALSE);

@@ -634,7 +635,6 @@ int main(int argc, char *argv[])

g_main_loop_unref(event_loop);

- g_io_channel_unref(chan);
g_attrib_unref(attrib);

done:
--
1.7.4.1


2011-02-24 20:34:27

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [PATCH 2/3] Add support for GATT client timeouts

This patch adds support to destroying the GATT connection
when a GATT server doesn't respond for more than 30 seconds.

A function to destroy the GAttrib is introduced and it is used
in the timeout case and when the last GAttrib reference is
dropped.
---
attrib/gattrib.c | 43 ++++++++++++++++++++++++++++++++++++-------
1 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/attrib/gattrib.c b/attrib/gattrib.c
index 5b8b590..cf49ede 100644
--- a/attrib/gattrib.c
+++ b/attrib/gattrib.c
@@ -36,12 +36,15 @@
#include "btio.h"
#include "gattrib.h"

+#define GATT_TIMEOUT 30
+
struct _GAttrib {
GIOChannel *io;
gint refs;
gint mtu;
guint read_watch;
guint write_watch;
+ guint timeout_watch;
GQueue *queue;
GSList *events;
guint next_cmd_id;
@@ -164,17 +167,11 @@ static void event_destroy(struct event *evt)
g_free(evt);
}

-void g_attrib_unref(GAttrib *attrib)
+static void attrib_destroy(GAttrib *attrib)
{
GSList *l;
struct command *c;

- if (!attrib)
- return;
-
- if (g_atomic_int_dec_and_test(&attrib->refs) == FALSE)
- return;
-
while ((c = g_queue_pop_head(attrib->queue)))
command_destroy(c);

@@ -187,6 +184,9 @@ void g_attrib_unref(GAttrib *attrib)
g_slist_free(attrib->events);
attrib->events = NULL;

+ if (attrib->timeout_watch> 0)
+ g_source_remove(attrib->timeout_watch);
+
if (attrib->write_watch > 0)
g_source_remove(attrib->write_watch);

@@ -201,6 +201,17 @@ void g_attrib_unref(GAttrib *attrib)
g_free(attrib);
}

+void g_attrib_unref(GAttrib *attrib)
+{
+ if (!attrib)
+ return;
+
+ if (g_atomic_int_dec_and_test(&attrib->refs) == FALSE)
+ return;
+
+ attrib_destroy(attrib);
+}
+
GIOChannel *g_attrib_get_channel(GAttrib *attrib)
{
if (!attrib)
@@ -233,6 +244,15 @@ gboolean g_attrib_set_destroy_function(GAttrib *attrib,
return TRUE;
}

+static gboolean disconnect_timeout(gpointer data)
+{
+ struct _GAttrib *attrib = data;
+
+ attrib_destroy(attrib);
+
+ return FALSE;
+}
+
static gboolean can_write_data(GIOChannel *io, GIOCondition cond,
gpointer data)
{
@@ -267,6 +287,10 @@ static gboolean can_write_data(GIOChannel *io, GIOCondition cond,

cmd->sent = TRUE;

+ if (attrib->timeout_watch == 0)
+ attrib->timeout_watch = g_timeout_add_seconds(GATT_TIMEOUT,
+ disconnect_timeout, attrib);
+
return FALSE;
}

@@ -295,6 +319,11 @@ static gboolean received_data(GIOChannel *io, GIOCondition cond, gpointer data)
GIOStatus iostat;
gboolean qempty;

+ if (attrib->timeout_watch > 0) {
+ g_source_remove(attrib->timeout_watch);
+ attrib->timeout_watch = 0;
+ }
+
if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
attrib->read_watch = 0;
if (attrib->disconnect)
--
1.7.4.1