From: Lennart Poettering Date: Mon, 29 Sep 2008 19:45:00 +0000 (+0200) Subject: make module-bluetooth-discover actually load modules and smaller other fixes X-Git-Tag: v0.9.13~56 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f5c301d9f475ca62b32f48514d703266bf9b6445;p=platform%2Fupstream%2Fpulseaudio.git make module-bluetooth-discover actually load modules and smaller other fixes --- diff --git a/src/modules/bluetooth/module-bluetooth-discover.c b/src/modules/bluetooth/module-bluetooth-discover.c index 3e9763b..e0a760f 100644 --- a/src/modules/bluetooth/module-bluetooth-discover.c +++ b/src/modules/bluetooth/module-bluetooth-discover.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "dbus-util.h" #include "module-bluetooth-discover-symdef.h" @@ -62,11 +63,14 @@ struct device { char *address; int class; int trusted; + const char *audio_profile; + uint32_t module_index; PA_LLIST_FIELDS(struct device); }; struct adapter { char *object_path; + char *name; char *mode; char *address; PA_LLIST_HEAD(struct device, device_list); @@ -80,20 +84,28 @@ struct userdata { }; static struct uuid *uuid_new(const char *uuid) { - struct uuid *node = pa_xnew(struct uuid, 1); + struct uuid *node; + + node = pa_xnew(struct uuid, 1); node->uuid = pa_xstrdup(uuid); PA_LLIST_INIT(struct uuid, node); + return node; } static void uuid_free(struct uuid *uuid) { + pa_assert(uuid); + + pa_xfree(uuid->uuid); pa_xfree(uuid); } -static struct device *device_new(const char *device, struct adapter *adapter) { - struct device *node = pa_xnew(struct device, 1); +static struct device *device_new(struct adapter *adapter, const char *object_path) { + struct device *node; + + node = pa_xnew(struct device, 1); node->name = NULL; - node->object_path = pa_xstrdup(device); + node->object_path = pa_xstrdup(object_path); node->paired = -1; node->adapter = adapter; node->alias = NULL; @@ -102,84 +114,293 @@ static struct device *device_new(const char *device, struct adapter *adapter) { node->address = NULL; node->class = -1; node->trusted = -1; + node->audio_profile = NULL; + node->module_index = PA_INVALID_INDEX; PA_LLIST_INIT(struct device, node); + return node; } static void device_free(struct device *device) { - struct uuid *uuid_list_i; - while (device->uuid_list) { - uuid_list_i = device->uuid_list; - PA_LLIST_REMOVE(struct uuid, device->uuid_list, uuid_list_i); - uuid_free(uuid_list_i); + struct uuid *i; + + pa_assert(device); + + while ((i = device->uuid_list)) { + PA_LLIST_REMOVE(struct uuid, device->uuid_list, i); + uuid_free(i); } + + pa_xfree(device->name); + pa_xfree(device->object_path); + pa_xfree(device->alias); + pa_xfree(device->address); pa_xfree(device); } -static struct adapter *adapter_new(const char *adapter) { - struct adapter *node = pa_xnew(struct adapter, 1); - node->object_path = pa_xstrdup(adapter); +static struct adapter *adapter_new(const char *object_path) { + struct adapter *node; + + node = pa_xnew(struct adapter, 1); + node->object_path = pa_xstrdup(object_path); node->mode = NULL; node->address = NULL; + node->name = NULL; + PA_LLIST_HEAD_INIT(struct device, node->device_list); PA_LLIST_INIT(struct adapter, node); + return node; } static void adapter_free(struct adapter *adapter) { - struct device *device_list_i; - while (adapter->device_list) { - device_list_i = adapter->device_list; - PA_LLIST_REMOVE(struct device, adapter->device_list, device_list_i); - device_free(device_list_i); + struct device *i; + + pa_assert(adapter); + + while ((i = adapter->device_list)) { + PA_LLIST_REMOVE(struct device, adapter->device_list, i); + device_free(i); } + + pa_xfree(adapter->object_path); + pa_xfree(adapter->mode); + pa_xfree(adapter->address); + pa_xfree(adapter->name); pa_xfree(adapter); } -static void print_devices(struct device *device_list) { - struct device *device_list_i = device_list; - while (device_list_i != NULL) { - struct uuid *uuid_list_i = device_list_i->uuid_list; - if (strcmp(device_list_i->object_path, "/DEVICE_HEAD") != 0) { - pa_log_debug(" [ %s ]", device_list_i->object_path); - pa_log_debug(" Name = %s", device_list_i->name); - pa_log_debug(" Paired = %d", device_list_i->paired); - pa_log_debug(" Adapter = %s", device_list_i->adapter->object_path); - pa_log_debug(" Alias = %s", device_list_i->alias); - pa_log_debug(" Connected = %d", device_list_i->connected); - pa_log_debug(" UUIDs = "); - while (uuid_list_i != NULL) { - if (strcmp(uuid_list_i->uuid, "UUID_HEAD") != 0) - pa_log(" %s", uuid_list_i->uuid); - uuid_list_i = uuid_list_i->next; - } - pa_log_debug(" Address = %s", device_list_i->address); - pa_log_debug(" Class = 0x%x", device_list_i->class); - pa_log_debug(" Trusted = %d", device_list_i->trusted); +static struct adapter* adapter_find(struct userdata *u, const char *path) { + struct adapter *i; + + for (i = u->adapter_list; i; i = i->next) + if (pa_streq(i->object_path, path)) + return i; + + return NULL; +} + +static struct device* device_find(struct userdata *u, const char *adapter, const char *path) { + struct adapter *a; + struct device *i; + + if (!(a = adapter_find(u, adapter))) + return NULL; + + for (i = a->device_list; i; i = i->next) + if (pa_streq(i->object_path, path)) + return i; + + return NULL; +} + +static const char *yes_no_na(int b) { + if (b < 0) + return "n/a"; + + return pa_yes_no(b); +} + +static void print_devices(struct adapter *a) { + struct device *i; + + pa_assert(a); + + for (i = a->device_list; i; i = i->next) { + struct uuid *j; + + if (pa_streq(i->object_path, "/DEVICE_HEAD")) + continue; + + pa_log_debug("\t[ %s ]\n" + "\t\tName = %s\n" + "\t\tPaired = %s\n" + "\t\tAdapter = %s\n" + "\t\tAlias = %s\n" + "\t\tConnected = %s\n" + "\t\tAudio = %s\n", + i->object_path, + pa_strnull(i->name), + yes_no_na(i->paired), + i->adapter->object_path, + pa_strnull(i->alias), + yes_no_na(i->connected), + pa_strnull(i->audio_profile)); + + pa_log_debug("\t\tUUIDs = "); + for (j = i->uuid_list; j; j = j->next) { + + if (pa_streq(j->uuid, "UUID_HEAD")) + continue; + + pa_log_debug("\t\t %s", j->uuid); } - device_list_i = device_list_i->next; + + pa_log_debug("\t\tAddress = %s\n" + "\t\tClass = 0x%x\n" + "\t\tTrusted = %s", + i->address, + i->class, + yes_no_na(i->trusted)); + } +} + +static void print_adapters(struct userdata *u) { + struct adapter *i; + + pa_assert(u); + + for (i = u->adapter_list; i; i = i->next) { + + if (pa_streq(i->object_path, "/ADAPTER_HEAD")) + continue; + + pa_log_debug( + "[ %s ]\n" + "\tName = %s\n" + "\tMode = %s\n" + "\tAddress = %s\n", + i->object_path, + pa_strnull(i->name), + pa_strnull(i->mode), + pa_strnull(i->address)); + + print_devices(i); + } +} + +static const char *strip_object_path(const char *op) { + const char *slash; + + if ((slash = strrchr(op, '/'))) + return slash+1; + + return op; +} + +static void load_module_for_device(struct userdata *u, struct device *d) { + char *args; + pa_module *m; + + pa_assert(u); + pa_assert(d); + + if (!d->audio_profile) { + pa_log_debug("Ignoring %s since it is not an audio device.", d->object_path); + return; } + + args = pa_sprintf_malloc("sink_name=%s address=%s profile=%s", strip_object_path(d->object_path), d->address, d->audio_profile); + m = pa_module_load(u->module->core, "module-bluetooth-device", args); + pa_xfree(args); + + if (!m) { + pa_log_debug("Failed to load module for device %s", d->object_path); + return; + } + + d->module_index = m->index; } -static void print_adapters(struct adapter *adapter_list) { - struct adapter *adapter_list_i = adapter_list; - while (adapter_list_i != NULL) { - if (strcmp(adapter_list_i->object_path, "/ADAPTER_HEAD") != 0) { - pa_log_debug("[ %s ]", adapter_list_i->object_path); - pa_log_debug(" Mode = %s", adapter_list_i->mode); - pa_log_debug(" Address = %s", adapter_list_i->address); - print_devices(adapter_list_i->device_list); +static void load_modules(struct userdata *u) { + struct device *d; + struct adapter *a; + + pa_assert(u); + + for (a = u->adapter_list; a; a = a->next) + for (d = a->device_list; d; d = d->next) + load_module_for_device(u, d); +} + +static int get_adapter_properties(struct userdata *u, struct adapter *a) { + DBusError e; + DBusMessage *m = NULL, *r = NULL; + DBusMessageIter arg_i, element_i; + int ret = -1; + + pa_assert(u); + pa_assert(a); + dbus_error_init(&e); + + pa_assert_se(m = dbus_message_new_method_call("org.bluez", a->object_path, "org.bluez.Adapter", "GetProperties")); + + r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); + + if (!r) { + pa_log("org.bluez.Adapter.GetProperties failed: %s", e.message); + goto finish; + } + + if (!dbus_message_iter_init(r, &arg_i)) { + pa_log("org.bluez.Adapter.GetProperties reply has no arguments"); + goto finish; + } + + if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) { + pa_log("org.bluez.Adapter.GetProperties argument is not an array"); + goto finish; + } + + dbus_message_iter_recurse(&arg_i, &element_i); + while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) { + + if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) { + const char *key; + DBusMessageIter dict_i, variant_i; + + dbus_message_iter_recurse(&element_i, &dict_i); + + if (dbus_message_iter_get_arg_type(&dict_i) != DBUS_TYPE_STRING) { + pa_log("Property name not a string."); + goto finish; + } + + dbus_message_iter_get_basic(&dict_i, &key); + + if (!dbus_message_iter_next(&dict_i)) { + pa_log("Dictionary item missing"); + goto finish; + } + + dbus_message_iter_recurse(&dict_i, &variant_i); + + if (dbus_message_iter_get_arg_type(&variant_i) == DBUS_TYPE_STRING) { + const char *value; + dbus_message_iter_get_basic(&variant_i, &value); + + if (pa_streq(key, "Mode")) + a->mode = pa_xstrdup(value); + else if (pa_streq(key, "Address")) + a->address = pa_xstrdup(value); + else if (pa_streq(key, "Name")) + a->name = pa_xstrdup(value); + } } - adapter_list_i = adapter_list_i->next; + + if (!dbus_message_iter_next(&element_i)) + break; } + + ret = 0; + +finish: + if (m) + dbus_message_unref(m); + if (r) + dbus_message_unref(r); + + dbus_error_free(&e); + + return ret; } -static void detect_adapters(struct userdata *u) { +static int detect_adapters(struct userdata *u) { DBusError e; DBusMessage *m = NULL, *r = NULL; - DBusMessageIter arg_i, element_i, dict_i, variant_i; + DBusMessageIter arg_i, element_i; struct adapter *adapter_list_i; - const char *key, *value; + int ret = -1; pa_assert(u); dbus_error_init(&e); @@ -187,203 +408,222 @@ static void detect_adapters(struct userdata *u) { /* get adapters */ pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters")); r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); + if (!r) { pa_log("org.bluez.Manager.ListAdapters failed: %s", e.message); - goto fail; + goto finish; } + if (!dbus_message_iter_init(r, &arg_i)) { pa_log("org.bluez.Manager.ListAdapters reply has no arguments"); - goto fail; + goto finish; } + if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) { pa_log("org.bluez.Manager.ListAdapters argument is not an array"); - goto fail; + goto finish; } + dbus_message_iter_recurse(&arg_i, &element_i); while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) { if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) { + struct adapter *node; + const char *value; + dbus_message_iter_get_basic(&element_i, &value); node = adapter_new(value); PA_LLIST_PREPEND(struct adapter, u->adapter_list, node); } - dbus_message_iter_next(&element_i); + + if (!dbus_message_iter_next(&element_i)) + break; } + ret = 0; + /* get adapter properties */ - adapter_list_i = u->adapter_list; - while (adapter_list_i != NULL) { - pa_assert_se(m = dbus_message_new_method_call("org.bluez", adapter_list_i->object_path, "org.bluez.Adapter", "GetProperties")); - r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); - if (!r) { - pa_log("org.bluez.Adapter.GetProperties failed: %s", e.message); - goto fail; - } - if (!dbus_message_iter_init(r, &arg_i)) { - pa_log("org.bluez.Adapter.GetProperties reply has no arguments"); - goto fail; - } - if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) { - pa_log("org.bluez.Adapter.GetProperties argument is not an array"); - goto fail; - } - dbus_message_iter_recurse(&arg_i, &element_i); - while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) { - if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) { - dbus_message_iter_recurse(&element_i, &dict_i); - dbus_message_iter_get_basic(&dict_i, &key); - dbus_message_iter_next(&dict_i); - dbus_message_iter_recurse(&dict_i, &variant_i); + for (adapter_list_i = u->adapter_list; adapter_list_i; adapter_list_i = adapter_list_i->next) + get_adapter_properties(u, adapter_list_i); + +finish: + if (m) + dbus_message_unref(m); + if (r) + dbus_message_unref(r); + + dbus_error_free(&e); + return ret; +} + +static int get_device_properties(struct userdata *u, struct device *d) { + DBusError e; + DBusMessage *m = NULL, *r = NULL; + DBusMessageIter arg_i, element_i, dict_i, variant_i; + int ret = -1; + + pa_assert(u); + pa_assert(d); + + dbus_error_init(&e); + + pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->object_path, "org.bluez.Device", "GetProperties")); + + r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); + + if (!r) { + pa_log("org.bluez.Device.GetProperties failed: %s", e.message); + goto finish; + } + + if (!dbus_message_iter_init(r, &arg_i)) { + pa_log("org.bluez.Device.GetProperties reply has no arguments"); + goto finish; + } + + if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) { + pa_log("org.bluez.Device.GetProperties argument is not an array"); + goto finish; + } + + dbus_message_iter_recurse(&arg_i, &element_i); + while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) { + if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) { + const char *value, *key; + int32_t ivalue; + + dbus_message_iter_recurse(&element_i, &dict_i); + dbus_message_iter_get_basic(&dict_i, &key); + dbus_message_iter_next(&dict_i); + + dbus_message_iter_recurse(&dict_i, &variant_i); + if (pa_streq(key, "Name")) { + dbus_message_iter_get_basic(&variant_i, &value); + d->name = pa_xstrdup(value); + } else if (pa_streq(key, "Paired")) { + dbus_message_iter_get_basic(&variant_i, &ivalue); + d->paired = ivalue; + } else if (pa_streq(key, "Alias")) { + dbus_message_iter_get_basic(&variant_i, &value); + d->alias = pa_xstrdup(value); + } else if (pa_streq(key, "Connected")) { + dbus_message_iter_get_basic(&variant_i, &ivalue); + d->connected = ivalue; + } else if (pa_streq(key, "UUIDs")) { + DBusMessageIter uuid_i; + + dbus_message_iter_recurse(&variant_i, &uuid_i); + while (dbus_message_iter_get_arg_type(&uuid_i) != DBUS_TYPE_INVALID) { + + struct uuid *node; + dbus_message_iter_get_basic(&uuid_i, &value); + node = uuid_new(value); + PA_LLIST_PREPEND(struct uuid, d->uuid_list, node); + + if ((strcasecmp(value, A2DP_SOURCE_UUID) == 0) || + (strcasecmp(value, A2DP_SINK_UUID) == 0)) + d->audio_profile = "a2dp"; + else if (((strcasecmp(value, HSP_HS_UUID) == 0) || + (strcasecmp(value, HFP_HS_UUID) == 0)) && + !d->audio_profile) + d->audio_profile = "hsp"; + + if (!dbus_message_iter_next(&uuid_i)) + break; + } + + } else if (pa_streq(key, "Address")) { dbus_message_iter_get_basic(&variant_i, &value); - if (strcmp(key, "Mode") == 0) - adapter_list_i->mode = pa_xstrdup(value); - else if (strcmp(key, "Address") == 0) - adapter_list_i->address = pa_xstrdup(value); + d->address = pa_xstrdup(value); + } else if (pa_streq(key, "Class")) { + dbus_message_iter_get_basic(&variant_i, &ivalue); + d->class = ivalue; + } else if (pa_streq(key, "Trusted")) { + dbus_message_iter_get_basic(&variant_i, &ivalue); + d->trusted = ivalue; } - dbus_message_iter_next(&element_i); } - adapter_list_i = adapter_list_i->next; + + if (!dbus_message_iter_next(&element_i)) + break; } -fail: + ret = 0; + +finish: if (m) dbus_message_unref(m); if (r) dbus_message_unref(r); + dbus_error_free(&e); + return ret; } -static void detect_devices(struct userdata *u) { +static int detect_devices(struct userdata *u) { DBusError e; DBusMessage *m = NULL, *r = NULL; - DBusMessageIter arg_i, element_i, dict_i, variant_i; + DBusMessageIter arg_i, element_i; struct adapter *adapter_list_i; struct device *device_list_i; - const char *key, *value; - int32_t ivalue; + const char *value; + int ret = -1; pa_assert(u); dbus_error_init(&e); /* get devices of each adapter */ - adapter_list_i = u->adapter_list; - while (adapter_list_i != NULL) { + for (adapter_list_i = u->adapter_list; adapter_list_i; adapter_list_i = adapter_list_i->next) { + pa_assert_se(m = dbus_message_new_method_call("org.bluez", adapter_list_i->object_path, "org.bluez.Adapter", "ListDevices")); + r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); + if (!r) { pa_log("org.bluez.Adapter.ListDevices failed: %s", e.message); - goto fail; + goto finish; } + if (!dbus_message_iter_init(r, &arg_i)) { pa_log("org.bluez.Adapter.ListDevices reply has no arguments"); - goto fail; + goto finish; } + if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) { pa_log("org.bluez.Adapter.ListDevices argument is not an array"); - goto fail; + goto finish; } + dbus_message_iter_recurse(&arg_i, &element_i); while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) { if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) { struct device *node; dbus_message_iter_get_basic(&element_i, &value); - node = device_new(value, adapter_list_i); + node = device_new(adapter_list_i, value); PA_LLIST_PREPEND(struct device, adapter_list_i->device_list, node); } - dbus_message_iter_next(&element_i); + + if (!dbus_message_iter_next(&element_i)) + break; } - adapter_list_i = adapter_list_i->next; } /* get device properties */ - adapter_list_i = u->adapter_list; - while (adapter_list_i != NULL) { - device_list_i = adapter_list_i->device_list; - while (device_list_i != NULL) { - pa_assert_se(m = dbus_message_new_method_call("org.bluez", device_list_i->object_path, "org.bluez.Device", "GetProperties")); - r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); - if (!r) { - pa_log("org.bluez.Device.GetProperties failed: %s", e.message); - goto fail; - } - if (!dbus_message_iter_init(r, &arg_i)) { - pa_log("org.bluez.Device.GetProperties reply has no arguments"); - goto fail; - } - if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) { - pa_log("org.bluez.Device.GetProperties argument is not an array"); - goto fail; - } - dbus_message_iter_recurse(&arg_i, &element_i); - while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) { - if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) { - dbus_message_iter_recurse(&element_i, &dict_i); - dbus_message_iter_get_basic(&dict_i, &key); - dbus_message_iter_next(&dict_i); - dbus_message_iter_recurse(&dict_i, &variant_i); - if (strcmp(key, "Name") == 0) { - dbus_message_iter_get_basic(&variant_i, &value); - device_list_i->name = pa_xstrdup(value); - } - else if (strcmp(key, "Paired") == 0) { - dbus_message_iter_get_basic(&variant_i, &ivalue); - device_list_i->paired = ivalue; - } - else if (strcmp(key, "Alias") == 0) { - dbus_message_iter_get_basic(&variant_i, &value); - device_list_i->alias = pa_xstrdup(value); - } - else if (strcmp(key, "Connected") == 0) { - dbus_message_iter_get_basic(&variant_i, &ivalue); - device_list_i->connected = ivalue; - } - else if (strcmp(key, "UUIDs") == 0) { - DBusMessageIter uuid_i; - pa_bool_t is_audio_device = FALSE; - dbus_message_iter_recurse(&variant_i, &uuid_i); - while (dbus_message_iter_get_arg_type(&uuid_i) != DBUS_TYPE_INVALID) { - struct uuid *node; - dbus_message_iter_get_basic(&uuid_i, &value); - node = uuid_new(value); - PA_LLIST_PREPEND(struct uuid, device_list_i->uuid_list, node); - if ( (strcasecmp(value, HSP_HS_UUID) == 0) || (strcasecmp(value, HFP_HS_UUID) == 0) || - (strcasecmp(value, A2DP_SOURCE_UUID) == 0) || (strcasecmp(value, A2DP_SINK_UUID) == 0) ) - is_audio_device = TRUE; - dbus_message_iter_next(&uuid_i); - } - if (!is_audio_device) { - /* remove the current device from the list */ - PA_LLIST_REMOVE(struct device, adapter_list_i->device_list, device_list_i); - pa_xfree(device_list_i); - break; - } - } - else if (strcmp(key, "Address") == 0) { - dbus_message_iter_get_basic(&variant_i, &value); - device_list_i->address = pa_xstrdup(value); - } - else if (strcmp(key, "Class") == 0) { - dbus_message_iter_get_basic(&variant_i, &ivalue); - device_list_i->class = ivalue; - } - else if (strcmp(key, "Trusted") == 0) { - dbus_message_iter_get_basic(&variant_i, &ivalue); - device_list_i->trusted = ivalue; - } - } - dbus_message_iter_next(&element_i); - } - device_list_i = device_list_i->next; - } - adapter_list_i = adapter_list_i->next; - } + for (adapter_list_i = u->adapter_list; adapter_list_i; adapter_list_i = adapter_list_i->next) + for (device_list_i = adapter_list_i->device_list; device_list_i; device_list_i = device_list_i->next) + get_device_properties(u, device_list_i); -fail: + ret = 0; + +finish: if (m) dbus_message_unref(m); if (r) dbus_message_unref(r); + dbus_error_free(&e); + + return ret; } static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) { @@ -404,43 +644,81 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void * dbus_message_get_member(msg)); if (dbus_message_is_signal(msg, "org.bluez.Manager", "AdapterAdded")) { + if (!dbus_message_iter_init(msg, &arg_i)) pa_log("dbus: message has no parameters"); else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH) pa_log("dbus: argument is not object path"); else { + struct adapter *node; + dbus_message_iter_get_basic(&arg_i, &value); - pa_log("hcid: adapter %s added", value); + pa_log_debug("hcid: adapter %s added", value); + + node = adapter_new(value); + PA_LLIST_PREPEND(struct adapter, u->adapter_list, node); + + get_adapter_properties(u, node); } - } - else if (dbus_message_is_signal(msg, "org.bluez.Manager", "AdapterRemoved")) { + + } else if (dbus_message_is_signal(msg, "org.bluez.Manager", "AdapterRemoved")) { if (!dbus_message_iter_init(msg, &arg_i)) pa_log("dbus: message has no parameters"); else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH) pa_log("dbus: argument is not object path"); else { + struct adapter *a; + dbus_message_iter_get_basic(&arg_i, &value); - pa_log("hcid: adapter %s removed", value); + pa_log_debug("hcid: adapter %s removed", value); + + if ((a = adapter_find(u, value))) { + PA_LLIST_REMOVE(struct adapter, u->adapter_list, a); + adapter_free(a); + } } - } - else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceCreated")) { + + } else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceCreated")) { + if (!dbus_message_iter_init(msg, &arg_i)) pa_log("dbus: message has no parameters"); else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH) pa_log("dbus: argument is not object path"); else { - dbus_message_iter_get_basic(&arg_i, &value); - pa_log("hcid: device %s created", value); + struct adapter *adapter; + + if (!(adapter = adapter_find(u, dbus_message_get_path(msg)))) + pa_log("dbus: failed to find adapter for object path"); + else { + struct device *node; + + dbus_message_iter_get_basic(&arg_i, &value); + pa_log_debug("hcid: device %s created", value); + + node = device_new(adapter, value); + PA_LLIST_PREPEND(struct device, adapter->device_list, node); + + get_device_properties(u, node); + load_module_for_device(u, node); + } } - } - else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceRemoved")) { + + } else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceRemoved")) { + if (!dbus_message_iter_init(msg, &arg_i)) pa_log("dbus: message has no parameters"); else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH) pa_log("dbus: argument is not object path"); else { + struct device *d; + dbus_message_iter_get_basic(&arg_i, &value); - pa_log("hcid: device %s removed", value); + pa_log_debug("hcid: device %s removed", value); + + if ((d = device_find(u, dbus_message_get_path(msg), value))) { + PA_LLIST_REMOVE(struct device, d->adapter->device_list, d); + device_free(d); + } } } @@ -450,39 +728,38 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void * void pa__done(pa_module* m) { struct userdata *u; - struct adapter *adapter_list_i; + struct adapter *i; pa_assert(m); if (!(u = m->userdata)) return; - while (u->adapter_list) { - adapter_list_i = u->adapter_list; - PA_LLIST_REMOVE(struct adapter, u->adapter_list, adapter_list_i); - adapter_free(adapter_list_i); + while ((i = u->adapter_list)) { + PA_LLIST_REMOVE(struct adapter, u->adapter_list, i); + adapter_free(i); } - pa_dbus_connection_unref(u->conn); + if (u->conn) + pa_dbus_connection_unref(u->conn); + pa_xfree(u); - return; } int pa__init(pa_module* m) { DBusError err; - struct adapter *adapter_list_i; - struct device *device_list_i; struct userdata *u; pa_assert(m); dbus_error_init(&err); - m->userdata = u = pa_xnew0(struct userdata, 1); + + m->userdata = u = pa_xnew(struct userdata, 1); u->module = m; PA_LLIST_HEAD_INIT(struct adapter, u->adapter_list); /* connect to the bus */ u->conn = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &err); - if ( dbus_error_is_set(&err) || (u->conn == NULL) ) { + if (dbus_error_is_set(&err) || (u->conn == NULL) ) { pa_log("Failed to get D-Bus connection: %s", err.message); goto fail; } @@ -491,30 +768,21 @@ int pa__init(pa_module* m) { detect_adapters(u); detect_devices(u); - print_adapters(u->adapter_list); - - /* load device modules */ - adapter_list_i = u->adapter_list; - while (adapter_list_i != NULL) { - device_list_i = adapter_list_i->device_list; - while (device_list_i != NULL) { - pa_log_debug("Loading module-bt-device for %s", device_list_i->name); - /* TODO: call module */ - device_list_i = device_list_i->next; - } - adapter_list_i = adapter_list_i->next; - } + print_adapters(u); + load_modules(u); /* dynamic detection of bluetooth audio devices */ if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) { pa_log_error("Failed to add filter function"); goto fail; } + dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Manager'", &err); if (dbus_error_is_set(&err)) { pa_log_error("Unable to subscribe to org.bluez.Manager signals: %s: %s", err.name, err.message); goto fail; } + dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Adapter'", &err); if (dbus_error_is_set(&err)) { pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err.name, err.message); @@ -526,5 +794,6 @@ int pa__init(pa_module* m) { fail: dbus_error_free(&err); pa__done(m); + return -1; }