* [PATCH 2/3] Add support for GATT client timeouts
2011-02-24 20:34 [PATCH 1/3] Fix gattrib.c coding style Vinicius Costa Gomes
@ 2011-02-24 20:34 ` Vinicius Costa Gomes
2011-02-24 20:34 ` [PATCH 3/3] gatttool: Remove extra reference to the connection IO Channel Vinicius Costa Gomes
1 sibling, 0 replies; 4+ messages in thread
From: Vinicius Costa Gomes @ 2011-02-24 20:34 UTC (permalink / raw)
To: linux-bluetooth; +Cc: Vinicius Costa Gomes
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 3/3] gatttool: Remove extra reference to the connection IO Channel
2011-02-24 20:34 [PATCH 1/3] Fix gattrib.c coding style Vinicius Costa Gomes
2011-02-24 20:34 ` [PATCH 2/3] Add support for GATT client timeouts Vinicius Costa Gomes
@ 2011-02-24 20:34 ` Vinicius Costa Gomes
2011-02-24 20:47 ` Johan Hedberg
1 sibling, 1 reply; 4+ messages in thread
From: Vinicius Costa Gomes @ 2011-02-24 20:34 UTC (permalink / raw)
To: linux-bluetooth; +Cc: Vinicius Costa Gomes
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
^ permalink raw reply related [flat|nested] 4+ messages in thread