summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorFrédéric Danis <frederic.danis@linux.intel.com>2011-01-20 12:10:12 +0100
committerMarcel Holtmann <marcel@holtmann.org>2011-01-20 12:20:47 +0100
commitb7590b08d253dc762a058fbdf2480757a6ff9af1 (patch)
treee4bbbb54a24f4c26cd56a33065926411586b8a84 /plugins
parent63fd483fc912327ab6874ab6bfada0a246152ac5 (diff)
downloadofono-b7590b08d253dc762a058fbdf2480757a6ff9af1.tar.bz2
bluetooth: Add reference count for bluetooth utils
Add bluetooth_ref()/bluetooth_unref() to support reference count in bluetooth utils.
Diffstat (limited to 'plugins')
-rw-r--r--plugins/bluetooth.c51
1 files changed, 32 insertions, 19 deletions
diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index 602c6da5..93dd7a13 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -40,6 +40,7 @@
static DBusConnection *connection;
static GHashTable *uuid_hash = NULL;
static GHashTable *adapter_address_hash = NULL;
+static gint bluetooth_refcount;
void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
char *buf, int size)
@@ -504,12 +505,10 @@ static guint adapter_added_watch;
static guint adapter_removed_watch;
static guint property_watch;
-int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
+static void bluetooth_ref(void)
{
- int err;
-
- if (uuid_hash)
- goto done;
+ if (bluetooth_refcount > 0)
+ goto increment;
connection = ofono_dbus_get_connection();
@@ -533,7 +532,6 @@ int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
if (bluetooth_watch == 0 || adapter_added_watch == 0 ||
adapter_removed_watch == 0 || property_watch == 0) {
- err = -EIO;
goto remove;
}
@@ -543,28 +541,21 @@ int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
adapter_address_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, g_free);
-done:
- g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
-
- bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
- manager_properties_cb, NULL, NULL, -1,
- DBUS_TYPE_INVALID);
+increment:
+ g_atomic_int_inc(&bluetooth_refcount);
- return 0;
+ return;
remove:
g_dbus_remove_watch(connection, bluetooth_watch);
g_dbus_remove_watch(connection, adapter_added_watch);
g_dbus_remove_watch(connection, adapter_removed_watch);
g_dbus_remove_watch(connection, property_watch);
- return err;
}
-void bluetooth_unregister_uuid(const char *uuid)
+static void bluetooth_unref(void)
{
- g_hash_table_remove(uuid_hash, uuid);
-
- if (g_hash_table_size(uuid_hash))
+ if (g_atomic_int_dec_and_test(&bluetooth_refcount) == FALSE)
return;
g_dbus_remove_watch(connection, bluetooth_watch);
@@ -574,7 +565,29 @@ void bluetooth_unregister_uuid(const char *uuid)
g_hash_table_destroy(uuid_hash);
g_hash_table_destroy(adapter_address_hash);
- uuid_hash = NULL;
+}
+
+int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
+{
+ bluetooth_ref();
+
+ if (bluetooth_refcount == 0)
+ return -EIO;
+
+ g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
+
+ bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
+ manager_properties_cb, NULL, NULL, -1,
+ DBUS_TYPE_INVALID);
+
+ return 0;
+}
+
+void bluetooth_unregister_uuid(const char *uuid)
+{
+ g_hash_table_remove(uuid_hash, uuid);
+
+ bluetooth_unref();
}
OFONO_PLUGIN_DEFINE(bluetooth, "Bluetooth Utils Plugins", VERSION,