+static pa_bluetooth_transport *transport_new(pa_bluetooth_discovery *y, const char *path, enum profile p, const uint8_t *config, int size) {
+ pa_bluetooth_transport *t;
+
+ t = pa_xnew0(pa_bluetooth_transport, 1);
+ t->y = y;
+ t->path = pa_xstrdup(path);
+ t->profile = p;
+ t->config_size = size;
+
+ if (size > 0) {
+ t->config = pa_xnew(uint8_t, size);
+ memcpy(t->config, config, size);
+ }
+
+ return t;
+}
+
+static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
+ pa_bluetooth_discovery *y = userdata;
+ pa_bluetooth_device *d;
+ pa_bluetooth_transport *t;
+ const char *path, *dev_path = NULL, *uuid = NULL;
+ uint8_t *config = NULL;
+ int size = 0;
+ enum profile p;
+ DBusMessageIter args, props;
+ DBusMessage *r;
+
+ dbus_message_iter_init(m, &args);
+
+ dbus_message_iter_get_basic(&args, &path);
+ if (!dbus_message_iter_next(&args))
+ goto fail;
+
+ dbus_message_iter_recurse(&args, &props);
+ if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
+ goto fail;
+
+ /* Read transport properties */
+ while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
+ const char *key;
+ DBusMessageIter value, entry;
+ int var;
+
+ dbus_message_iter_recurse(&props, &entry);
+ dbus_message_iter_get_basic(&entry, &key);
+
+ dbus_message_iter_next(&entry);
+ dbus_message_iter_recurse(&entry, &value);
+
+ var = dbus_message_iter_get_arg_type(&value);
+ if (strcasecmp(key, "UUID") == 0) {
+ if (var != DBUS_TYPE_STRING)
+ goto fail;
+ dbus_message_iter_get_basic(&value, &uuid);
+ } else if (strcasecmp(key, "Device") == 0) {
+ if (var != DBUS_TYPE_OBJECT_PATH)
+ goto fail;
+ dbus_message_iter_get_basic(&value, &dev_path);
+ } else if (strcasecmp(key, "Configuration") == 0) {
+ DBusMessageIter array;
+ if (var != DBUS_TYPE_ARRAY)
+ goto fail;
+ dbus_message_iter_recurse(&value, &array);
+ dbus_message_iter_get_fixed_array(&array, &config, &size);
+ }
+
+ dbus_message_iter_next(&props);
+ }
+
+ d = found_device(y, dev_path);
+ if (!d)
+ goto fail;
+
+ if (dbus_message_has_path(m, HFP_AG_ENDPOINT))
+ p = PROFILE_HSP;
+ else if (dbus_message_has_path(m, A2DP_SOURCE_ENDPOINT))
+ p = PROFILE_A2DP;
+ else
+ p = PROFILE_A2DP_SOURCE;
+
+ t = transport_new(y, path, p, config, size);
+ pa_hashmap_put(d->transports, t->path, t);
+
+ pa_log_debug("Transport %s profile %d available", t->path, t->profile);
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ return r;
+
+fail:
+ pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
+ pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
+ "Unable to set configuration")));
+ return r;
+}
+
+static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
+ pa_bluetooth_discovery *y = userdata;
+ pa_bluetooth_device *d;
+ pa_bluetooth_transport *t;
+ void *state = NULL;
+ DBusMessage *r;
+ DBusError e;
+ const char *path;
+
+ dbus_error_init(&e);
+
+ if (!dbus_message_get_args(m, &e, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e.message);
+ dbus_error_free(&e);
+ goto fail;
+ }
+
+ while ((d = pa_hashmap_iterate(y->devices, &state, NULL))) {
+ if ((t = pa_hashmap_get(d->transports, path))) {
+ pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
+ pa_hashmap_remove(d->transports, t->path);
+ transport_free(t);
+ break;
+ }
+ }
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ return r;
+
+fail:
+ pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
+ "Unable to clear configuration")));
+ return r;
+}
+
+static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
+
+ switch (freq) {
+ case BT_SBC_SAMPLING_FREQ_16000:
+ case BT_SBC_SAMPLING_FREQ_32000:
+ return 53;
+
+ case BT_SBC_SAMPLING_FREQ_44100:
+
+ switch (mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ return 31;
+
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ return 53;
+
+ default:
+ pa_log_warn("Invalid channel mode %u", mode);
+ return 53;
+ }
+
+ case BT_SBC_SAMPLING_FREQ_48000:
+
+ switch (mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ return 29;
+
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ return 51;
+
+ default:
+ pa_log_warn("Invalid channel mode %u", mode);
+ return 51;
+ }
+
+ default:
+ pa_log_warn("Invalid sampling freq %u", freq);
+ return 53;
+ }
+}
+
+static DBusMessage *endpoint_select_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
+ pa_bluetooth_discovery *y = userdata;
+ sbc_capabilities_raw_t *cap, config;
+ uint8_t *pconf = (uint8_t *) &config;
+ int i, size;
+ DBusMessage *r;
+ DBusError e;
+
+ static const struct {
+ uint32_t rate;
+ uint8_t cap;
+ } freq_table[] = {
+ { 16000U, BT_SBC_SAMPLING_FREQ_16000 },
+ { 32000U, BT_SBC_SAMPLING_FREQ_32000 },
+ { 44100U, BT_SBC_SAMPLING_FREQ_44100 },
+ { 48000U, BT_SBC_SAMPLING_FREQ_48000 }
+ };
+
+ dbus_error_init(&e);
+
+ if (!dbus_message_get_args(m, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e.message);
+ dbus_error_free(&e);
+ goto fail;
+ }
+
+ if (dbus_message_has_path(m, HFP_AG_ENDPOINT))
+ goto done;
+
+ pa_assert(size == sizeof(config));
+
+ memset(&config, 0, sizeof(config));
+
+ /* Find the lowest freq that is at least as high as the requested
+ * sampling rate */
+ for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
+ if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
+ config.frequency = freq_table[i].cap;
+ break;
+ }
+
+ if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
+ for (--i; i >= 0; i--) {
+ if (cap->frequency & freq_table[i].cap) {
+ config.frequency = freq_table[i].cap;
+ break;
+ }
+ }
+
+ if (i < 0) {
+ pa_log("Not suitable sample rate");
+ goto fail;
+ }
+ }
+
+ pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
+
+ if (y->core->default_sample_spec.channels <= 1) {
+ if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
+ }
+
+ if (y->core->default_sample_spec.channels >= 2) {
+ if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
+ } else {
+ pa_log("No supported channel modes");
+ goto fail;
+ }
+ }
+
+ if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_16;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_12;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_8;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_4;
+ else {
+ pa_log_error("No supported block lengths");
+ goto fail;
+ }
+
+ if (cap->subbands & BT_A2DP_SUBBANDS_8)
+ config.subbands = BT_A2DP_SUBBANDS_8;
+ else if (cap->subbands & BT_A2DP_SUBBANDS_4)
+ config.subbands = BT_A2DP_SUBBANDS_4;
+ else {
+ pa_log_error("No supported subbands");
+ goto fail;
+ }
+
+ if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
+ config.allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
+ else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
+ config.allocation_method = BT_A2DP_ALLOCATION_SNR;
+
+ config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
+ config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
+
+done:
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ pa_assert_se(dbus_message_append_args(
+ r,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size,
+ DBUS_TYPE_INVALID));
+
+ return r;
+
+fail:
+ pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
+ "Unable to select configuration")));
+ return r;
+}
+
+static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
+ struct pa_bluetooth_discovery *y = userdata;
+ DBusMessage *r = NULL;
+ DBusError e;
+ const char *path;
+
+ pa_assert(y);
+
+ pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface(m),
+ dbus_message_get_path(m),
+ dbus_message_get_member(m));
+
+ path = dbus_message_get_path(m);
+ dbus_error_init(&e);
+
+ if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT) && !pa_streq(path, HFP_AG_ENDPOINT))
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
+ const char *xml = ENDPOINT_INTROSPECT_XML;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ pa_assert_se(dbus_message_append_args(
+ r,
+ DBUS_TYPE_STRING, &xml,
+ DBUS_TYPE_INVALID));
+
+ } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SetConfiguration")) {
+ r = endpoint_set_configuration(c, m, userdata);
+ } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
+ r = endpoint_select_configuration(c, m, userdata);
+ } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "ClearConfiguration"))
+ r = endpoint_clear_configuration(c, m, userdata);
+ else
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (r) {
+ pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
+ dbus_message_unref(r);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+