gnome-bluetooth r547 - trunk/wizard



Author: hadess
Date: Thu Mar 26 15:31:24 2009
New Revision: 547
URL: http://svn.gnome.org/viewvc/gnome-bluetooth?rev=547&view=rev

Log:
Move the pin getting code to a separate file, and convert to using the XML DB.


Added:
   trunk/wizard/pin.c
   trunk/wizard/pin.h
Modified:
   trunk/wizard/Makefile.am
   trunk/wizard/main.c

Modified: trunk/wizard/Makefile.am
==============================================================================
--- trunk/wizard/Makefile.am	(original)
+++ trunk/wizard/Makefile.am	Thu Mar 26 15:31:24 2009
@@ -1,7 +1,7 @@
 
 bin_PROGRAMS = bluetooth-wizard
 
-bluetooth_wizard_SOURCES = main.c
+bluetooth_wizard_SOURCES = main.c pin.c pin.h
 
 bluetooth_wizard_LDADD = $(top_builddir)/common/libgnome-bluetooth.la $(top_builddir)/common/libcommon.la $(WIZARD_LIBS)
 

Modified: trunk/wizard/main.c
==============================================================================
--- trunk/wizard/main.c	(original)
+++ trunk/wizard/main.c	Thu Mar 26 15:31:24 2009
@@ -36,8 +36,9 @@
 #include <bluetooth-chooser.h>
 #include <bluetooth-agent.h>
 
+#include "pin.h"
+
 #define AGENT_PATH "/org/bluez/agent/wizard"
-#define PIN_CODE_DB "pin-code-database.txt"
 
 static gboolean set_page_search_complete(void);
 
@@ -66,94 +67,6 @@
 
 static BluetoothChooser *selector = NULL;
 
-#define TYPE_IS(x, r) {				\
-	if (g_str_equal(type, x)) return r;	\
-}
-
-static guint string_to_type(const char *type)
-{
-	TYPE_IS ("any", BLUETOOTH_TYPE_ANY);
-	TYPE_IS ("mouse", BLUETOOTH_TYPE_MOUSE);
-	TYPE_IS ("keyboard", BLUETOOTH_TYPE_KEYBOARD);
-	TYPE_IS ("headset", BLUETOOTH_TYPE_HEADSET);
-	TYPE_IS ("headphones", BLUETOOTH_TYPE_HEADPHONES);
-	TYPE_IS ("audio", BLUETOOTH_TYPE_OTHER_AUDIO);
-	TYPE_IS ("printer", BLUETOOTH_TYPE_PRINTER);
-	TYPE_IS ("network", BLUETOOTH_TYPE_NETWORK);
-
-	g_warning ("unhandled type '%s'", type);
-	return BLUETOOTH_TYPE_ANY;
-}
-
-static char *get_pincode_for_device(guint type, const char *address, const char *name)
-{
-	char *contents, **lines;
-	char *ret_pin = NULL;
-	guint i;
-
-	/* Load the PIN database and split it in lines */
-	if (!g_file_get_contents(PIN_CODE_DB, &contents, NULL, NULL)) {
-		char *filename;
-
-		filename = g_build_filename(PKGDATADIR, PIN_CODE_DB, NULL);
-		if (!g_file_get_contents(filename, &contents, NULL, NULL)) {
-			g_warning("Could not load "PIN_CODE_DB);
-			g_free (filename);
-			return NULL;
-		}
-		g_free (filename);
-	}
-
-	lines = g_strsplit(contents, "\n", -1);
-	g_free (contents);
-	if (lines == NULL) {
-		g_warning("Could not parse "PIN_CODE_DB);
-		return NULL;
-	}
-
-	/* And now process each line */
-	for (i = 0; lines[i] != NULL; i++) {
-		char **items;
-		guint ltype;
-		const char *laddress, *lname, *lpin;
-
-		/* Ignore comments and empty lines */
-		if (lines[i][0] == '#' || lines[i][0] == '\0')
-			continue;
-
-		items = g_strsplit(lines[i], "\t", 4);
-		ltype = string_to_type(items[0]);
-
-		if (ltype != BLUETOOTH_TYPE_ANY && ltype != type) {
-			g_strfreev (items);
-			continue;
-		}
-		laddress = items[1];
-		lname = items[2];
-		lpin = items[3];
-
-		/* If we have an address, does the OUI prefix match? */
-		if (strlen(laddress) > 0 && g_str_has_prefix(address, laddress) == FALSE) {
-			g_strfreev (items);
-			continue;
-		}
-
-		/* If we have a name, does it match? */
-		if (strlen(lname) > 0 && g_str_equal(name, lname) == FALSE) {
-			g_strfreev (items);
-			continue;
-		}
-
-		/* Everything matches, we have a pincode */
-		ret_pin = g_strdup(lpin);
-		g_strfreev(items);
-		break;
-	}
-
-	g_strfreev(lines);
-	return ret_pin;
-}
-
 static void
 set_large_label (GtkLabel *label, const char *text)
 {

Added: trunk/wizard/pin.c
==============================================================================
--- (empty file)
+++ trunk/wizard/pin.c	Thu Mar 26 15:31:24 2009
@@ -0,0 +1,138 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2009  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+
+#include <bluetooth-enums.h>
+
+#define PIN_CODE_DB "pin-code-database.xml"
+
+#define TYPE_IS(x, r) {				\
+	if (g_str_equal(type, x)) return r;	\
+}
+
+static guint string_to_type(const char *type)
+{
+	TYPE_IS ("any", BLUETOOTH_TYPE_ANY);
+	TYPE_IS ("mouse", BLUETOOTH_TYPE_MOUSE);
+	TYPE_IS ("keyboard", BLUETOOTH_TYPE_KEYBOARD);
+	TYPE_IS ("headset", BLUETOOTH_TYPE_HEADSET);
+	TYPE_IS ("headphones", BLUETOOTH_TYPE_HEADPHONES);
+	TYPE_IS ("audio", BLUETOOTH_TYPE_OTHER_AUDIO);
+	TYPE_IS ("printer", BLUETOOTH_TYPE_PRINTER);
+	TYPE_IS ("network", BLUETOOTH_TYPE_NETWORK);
+
+	g_warning ("unhandled type '%s'", type);
+	return BLUETOOTH_TYPE_ANY;
+}
+
+typedef struct {
+	char *ret_pin;
+	guint type;
+	const char *address;
+	const char *name;
+} PinParseData;
+
+static void
+pin_db_parse_start_tag (GMarkupParseContext *ctx,
+			const gchar         *element_name,
+			const gchar        **attr_names,
+			const gchar        **attr_values,
+			gpointer             data,
+			GError             **error)
+{
+	PinParseData *pdata = (PinParseData *) data;
+
+	if (pdata->ret_pin != NULL)
+		return;
+	if (g_str_equal (element_name, "device") == FALSE)
+		return;
+
+	while (*attr_names && *attr_values) {
+		if (g_str_equal (*attr_names, "type")) {
+			guint type;
+
+			type = string_to_type (*attr_values);
+			if (type != BLUETOOTH_TYPE_ANY && type != pdata->type)
+				return;
+		} else if (g_str_equal (*attr_names, "oui")) {
+			if (g_str_has_prefix (*attr_values, pdata->address) == FALSE)
+				return;
+		} else if (g_str_equal (*attr_names, "name")) {
+			if (g_str_equal (*attr_values, pdata->name) == FALSE)
+				return;
+		} else if (g_str_equal (*attr_names, "pin")) {
+			pdata->ret_pin = g_strdup (*attr_values);
+			return;
+		}
+
+		++attr_names;
+		++attr_values;
+	}
+}
+
+char *
+get_pincode_for_device (guint type, const char *address, const char *name)
+{
+	GMarkupParseContext *ctx;
+	GMarkupParser parser = { pin_db_parse_start_tag, NULL, NULL, NULL, NULL };
+	PinParseData data;
+	char *buf;
+	gsize buf_len;
+	GError *err = NULL;
+
+	/* Load the PIN database and split it in lines */
+	if (!g_file_get_contents(PIN_CODE_DB, &buf, &buf_len, NULL)) {
+		char *filename;
+
+		filename = g_build_filename(PKGDATADIR, PIN_CODE_DB, NULL);
+		if (!g_file_get_contents(filename, &buf, &buf_len, NULL)) {
+			g_warning("Could not load "PIN_CODE_DB);
+			g_free (filename);
+			return NULL;
+		}
+		g_free (filename);
+	}
+
+	data.ret_pin = NULL;
+	data.type = type;
+	data.address = address;
+	data.name = name;
+
+	ctx = g_markup_parse_context_new (&parser, 0, &data, NULL);
+
+	if (!g_markup_parse_context_parse (ctx, buf, buf_len, &err)) {
+		g_warning ("Failed to parse '%s': %s\n", PIN_CODE_DB, err->message);
+		g_error_free (err);
+	}
+
+	g_markup_parse_context_free (ctx);
+	g_free (buf);
+
+	return data.ret_pin;
+}
+

Added: trunk/wizard/pin.h
==============================================================================
--- (empty file)
+++ trunk/wizard/pin.h	Thu Mar 26 15:31:24 2009
@@ -0,0 +1,27 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2009  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <glib.h>
+
+char *get_pincode_for_device (guint type, const char *address, const char *name);
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]