Linux bluetooth development
 help / color / mirror / Atom feed
From: Anderson Lizardo <anderson.lizardo@openbossa.org>
To: linux-bluetooth@vger.kernel.org
Cc: Anderson Lizardo <anderson.lizardo@openbossa.org>
Subject: [PATCH 06/15] GATT server: parse and resolve service includes
Date: Wed, 16 Mar 2011 21:00:30 -0400	[thread overview]
Message-ID: <1300323639-13296-7-git-send-email-anderson.lizardo@openbossa.org> (raw)
In-Reply-To: <1300323639-13296-1-git-send-email-anderson.lizardo@openbossa.org>

"Resolving" service includes consists of replacing pointers to the ID
strings with actual struct gatt_service pointers, after all services
have been parsed.
---
 plugins/gatt-profile.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/plugins/gatt-profile.c b/plugins/gatt-profile.c
index c0b2a50..b746449 100644
--- a/plugins/gatt-profile.c
+++ b/plugins/gatt-profile.c
@@ -93,6 +93,28 @@ static void parse_service(const gchar **attribute_names,
 	*services = g_slist_prepend(*services, svc);
 }
 
+static void parse_include(const gchar **attribute_names,
+						const gchar **attribute_values,
+						struct gatt_service *cur_svc)
+{
+	const gchar *id;
+	int i;
+
+	for (i = 0, id = NULL; attribute_names[i]; i++) {
+		if (g_strcmp0(attribute_names[i], "id") == 0)
+			id = attribute_values[i];
+		else
+			error("Invalid XML attribute: %s", attribute_names[i]);
+	}
+
+	if (id == NULL || id[0] == '\0') {
+		error("\"id\" attribute required");
+		return;
+	}
+
+	cur_svc->includes = g_slist_append(cur_svc->includes, g_strdup(id));
+}
+
 static void element_start(GMarkupParseContext *ctx, const gchar *element_name,
 		const gchar **attribute_names, const gchar **attribute_values,
 		gpointer user_data, GError **err)
@@ -107,8 +129,18 @@ static void element_start(GMarkupParseContext *ctx, const gchar *element_name,
 	else if (g_strcmp0(element_name, "secondary-service") == 0)
 		parse_service(attribute_names, attribute_values, FALSE,
 								services);
-	else
-		error("Invalid XML tag: %s", element_name);
+	else {
+		struct gatt_service *cur_svc = g_slist_nth_data(*services, 0);
+
+		if (cur_svc == NULL)
+			error("XML tag \"%s\" must be inside a service",
+								element_name);
+		else if (g_strcmp0(element_name, "include") == 0)
+			parse_include(attribute_names, attribute_values,
+								cur_svc);
+		else
+			error("Invalid XML tag: %s", element_name);
+	}
 }
 
 static void element_end(GMarkupParseContext *ctx, const gchar *element_name,
@@ -117,11 +149,45 @@ static void element_end(GMarkupParseContext *ctx, const gchar *element_name,
 	/* TODO: verify tag balance */
 }
 
+static gint find_id(gconstpointer a, gconstpointer b)
+{
+	const struct gatt_service *svc = a;
+	const gchar *id = b;
+
+	return g_strcmp0(svc->id, id);
+}
+
+static void resolve_includes(gpointer data, gpointer user_data)
+{
+	struct gatt_service *svc = data;
+	GSList *services = user_data;
+	GSList *l, *includes = NULL;
+
+	/* TODO: check for circular references */
+
+	for (l = svc->includes; l; l = l->next) {
+		gchar *id = l->data;
+		GSList *l2;
+
+		l2 = g_slist_find_custom(services, id, find_id);
+		if (l2)
+			includes = g_slist_append(includes, l2->data);
+		else
+			error("Could not find service with ID %s", id);
+
+		g_free(id);
+	}
+
+	g_slist_free(svc->includes);
+	svc->includes = includes;
+}
+
 static void free_services(gpointer data, gpointer user_data)
 {
 	struct gatt_service *svc = data;
 
-	/* TODO: free characteristics, includes and descriptors */
+	g_free(svc->id);
+	g_slist_free(svc->includes);
 
 	g_free(svc);
 }
@@ -140,6 +206,7 @@ static int add_xml_profile(DBusConnection *conn, const char *profile)
 		ret = -EINVAL;
 	}
 
+	g_slist_foreach(services, resolve_includes, services);
 	g_slist_foreach(services, free_services, NULL);
 	g_slist_free(services);
 
-- 
1.7.0.4


  parent reply	other threads:[~2011-03-17  1:00 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-17  1:00 [RFC][PATCH 00/15] Attribute server management API Anderson Lizardo
2011-03-17  1:00 ` [PATCH 01/15] Remove built-in example attribute server Anderson Lizardo
2011-03-17  1:00 ` [PATCH 02/15] GATT server: add profile API plugin skeleton Anderson Lizardo
2011-03-17  1:00 ` [PATCH 03/15] GATT server: add initial D-Bus interface Anderson Lizardo
2011-03-17  1:00 ` [PATCH 04/15] GATT server: add initial XML parsing Anderson Lizardo
2011-03-17  1:00 ` [PATCH 05/15] GATT server: parse primary/secondary services Anderson Lizardo
2011-03-17  1:00 ` Anderson Lizardo [this message]
2011-03-17  1:00 ` [PATCH 07/15] GATT server: parse characteristics Anderson Lizardo
2011-03-17  1:00 ` [PATCH 08/15] GATT server: parse characteristic value Anderson Lizardo
2011-03-17  1:00 ` [PATCH 09/15] GATT server: add test/test-gatt-profile example Anderson Lizardo
2011-03-17  1:00 ` [PATCH 10/15] Add attrib_db_find_avail() function to attribute server Anderson Lizardo
2011-03-17  1:00 ` [PATCH 11/15] GATT server: add service registration Anderson Lizardo
2011-03-17  1:00 ` [PATCH 12/15] GATT server: add service includes registration Anderson Lizardo
2011-03-17  1:00 ` [PATCH 13/15] GATT server: add characteristic registration Anderson Lizardo
2011-03-17  1:00 ` [PATCH 14/15] GATT server: add characteristic descriptor registration Anderson Lizardo
2011-03-17  1:00 ` [PATCH 15/15] GATT server: add disconnect watch Anderson Lizardo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1300323639-13296-7-git-send-email-anderson.lizardo@openbossa.org \
    --to=anderson.lizardo@openbossa.org \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox