From: =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis <frederic.danis@linux.intel.com>
To: ofono@ofono.org
Subject: [PATCH 2/4] bluetooth: use GSList instead of GHashTable to store adapter path/address
Date: Thu, 27 Jan 2011 16:05:38 +0100 [thread overview]
Message-ID: <1296140740-11486-3-git-send-email-frederic.danis@linux.intel.com> (raw)
In-Reply-To: <1296140740-11486-1-git-send-email-frederic.danis@linux.intel.com>
[-- Attachment #1: Type: text/plain, Size: 4530 bytes --]
---
plugins/bluetooth.c | 86 +++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 74 insertions(+), 12 deletions(-)
diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index e59bd31..4da662a 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -39,9 +39,14 @@
static DBusConnection *connection;
static GHashTable *uuid_hash = NULL;
-static GHashTable *adapter_address_hash = NULL;
+static GSList *adapter_list = NULL;
static gint bluetooth_refcount;
+struct adapter_address {
+ char *adapter;
+ char *address;
+};
+
void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
char *buf, int size)
{
@@ -235,6 +240,52 @@ static void parse_string(DBusMessageIter *iter, gpointer user_data)
dbus_message_iter_get_basic(iter, str);
}
+static gint adapter_compare(gconstpointer a, gconstpointer b)
+{
+ const struct adapter_address *entry = a;
+ const char *key = b;
+
+ return g_strcmp0(entry->adapter, key);
+}
+
+static GSList* adapter_address_add(GSList *list, const char *path,
+ const char *address)
+{
+ GSList *l;
+ struct adapter_address *entry;
+
+ l = g_slist_find_custom(adapter_list, path, adapter_compare);
+ if (l != NULL) {
+ entry = l->data;
+
+ g_free(entry->address);
+
+ entry->address = g_strdup(address);
+
+ return list;
+ }
+
+ entry = g_try_new0(struct adapter_address, 1);
+ if (entry == NULL) {
+ ofono_error("Unable to allocate adapter_address structure");
+ return list;
+ }
+
+ entry->adapter = g_strdup(path);
+ entry->address = g_strdup(address);
+
+ return g_slist_prepend(list, entry);
+}
+
+static GSList* adapter_address_remove(GSList *l, struct adapter_address *entry)
+{
+ g_free(entry->adapter);
+ g_free(entry->address);
+ g_free(entry);
+
+ return g_slist_remove(l, entry);
+}
+
static void device_properties_cb(DBusPendingCall *call, gpointer user_data)
{
DBusMessage *reply;
@@ -245,6 +296,7 @@ static void device_properties_cb(DBusPendingCall *call, gpointer user_data)
const char *device_addr = NULL;
const char *alias = NULL;
struct bluetooth_profile *profile;
+ GSList *entry;
reply = dbus_pending_call_steal_reply(call);
@@ -266,9 +318,15 @@ static void device_properties_cb(DBusPendingCall *call, gpointer user_data)
"Address", parse_string, &device_addr,
"Alias", parse_string, &alias, NULL);
- if (adapter)
- adapter_addr = g_hash_table_lookup(adapter_address_hash,
- adapter);
+ if (adapter) {
+ entry = g_slist_find_custom(adapter_list, adapter,
+ adapter_compare);
+ if (entry != NULL) {
+ struct adapter_address *a = entry->data;
+
+ adapter_addr = a->address;
+ }
+ }
if ((have_uuid & HFP_AG) && device_addr && adapter_addr) {
profile = g_hash_table_lookup(uuid_hash, HFP_AG_UUID);
@@ -392,8 +450,7 @@ static void adapter_properties_cb(DBusPendingCall *call, gpointer user_data)
NULL);
DBG("Adapter Address: %s, Path: %s", addr, path);
- g_hash_table_insert(adapter_address_hash,
- g_strdup(path), g_strdup(addr));
+ adapter_list = adapter_address_add(adapter_list, path, addr);
for (l = device_list; l; l = l->next) {
const char *device = l->data;
@@ -429,10 +486,16 @@ static gboolean adapter_removed(DBusConnection *connection,
DBusMessage *message, void *user_data)
{
const char *path;
+ GSList *l;
if (dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
- DBUS_TYPE_INVALID) == TRUE)
- g_hash_table_remove(adapter_address_hash, path);
+ DBUS_TYPE_INVALID) == TRUE) {
+ l = g_slist_find_custom(adapter_list, path, adapter_compare);
+
+ if (l != NULL)
+ adapter_list = adapter_address_remove(adapter_list,
+ l->data);
+ }
return TRUE;
}
@@ -538,9 +601,6 @@ static int bluetooth_ref(void)
uuid_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
- adapter_address_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
- g_free, g_free);
-
increment:
g_atomic_int_inc(&bluetooth_refcount);
@@ -566,7 +626,9 @@ static void bluetooth_unref(void)
g_dbus_remove_watch(connection, property_watch);
g_hash_table_destroy(uuid_hash);
- g_hash_table_destroy(adapter_address_hash);
+ while (adapter_list)
+ adapter_list = adapter_address_remove(adapter_list,
+ adapter_list->data);
}
int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
--
1.7.1
next prev parent reply other threads:[~2011-01-27 15:05 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-01-27 15:05 [PATCH v2 0/4] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 15:05 ` [PATCH 1/4] bluetooth: only use bluetooth_refcount in bluetooth_ref/bluetooth_unref =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 15:12 ` Marcel Holtmann
2011-01-27 15:25 ` Frederic Danis
2011-01-27 16:17 ` Gustavo F. Padovan
2011-01-27 16:18 ` Marcel Holtmann
2011-01-27 15:05 ` =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis [this message]
2011-01-27 15:13 ` [PATCH 2/4] bluetooth: use GSList instead of GHashTable to store adapter path/address Marcel Holtmann
2011-01-27 15:22 ` Frederic Danis
2011-01-27 15:24 ` Marcel Holtmann
2011-01-27 15:35 ` Frederic Danis
2011-01-27 16:19 ` Gustavo F. Padovan
2011-01-27 15:05 ` [PATCH 3/4] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 16:26 ` Gustavo F. Padovan
2011-01-27 16:32 ` Frederic Danis
2011-01-27 15:05 ` [PATCH 4/4] bluetooth: add Bluetooth service authorization support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
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=1296140740-11486-3-git-send-email-frederic.danis@linux.intel.com \
--to=frederic.danis@linux.intel.com \
--cc=ofono@ofono.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