---
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