Commit ed2ea2c7 authored by Asbjørn Sæbø's avatar Asbjørn Sæbø Committed by Carles Cufi
Browse files

Bluetooth: Audio: Media - truncate strings for notification



Truncate strings being notified to fit the ATT_MTU, to prevent
notifications from not being delivered due to being too long.

For now uses the minimum ATT_MTU, as the MCS does not yet track
connections.

Signed-off-by: default avatarAsbjørn Sæbø <asbjorn.sabo@nordicsemi.no>
parent 2ec32224
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
config BT_MCS
	bool "Media Control Service Support"
	depends on MCTL_LOCAL_PLAYER_REMOTE_CONTROL
	depends on UTF8
	select BT_CCID
	select EXPERIMENTAL
	help
+34 −2
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#include <init.h>
#include <stdio.h>
#include <zephyr/types.h>
#include <sys/util.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/conn.h>
@@ -794,7 +795,7 @@ struct bt_ots *bt_mcs_get_ots(void)
/* Callback functions from the media player, notifying attributes */
/* Placed here, after the service definition, because they reference it. */

/* Helper function to shorten functions that notify */
/* Helper function to notify non-string values */
static void notify(const struct bt_uuid *uuid, const void *data, uint16_t len)
{
	int err = bt_gatt_notify_uuid(NULL, uuid, mcs.attrs, data, len);
@@ -808,6 +809,37 @@ static void notify(const struct bt_uuid *uuid, const void *data, uint16_t len)
	}
}

/* Helper function to notify UTF8 string values
 * Will truncate string to fit within notification if required.
 * The string must be null-terminated.
 */
static void notify_string(const struct bt_uuid *uuid, const char *str)
{
	/* TODO:
	 * This function will need to get the ATT_MTU to know what length to
	 * truncate the string to.  But the ATT_MTU is per connection, and MCS
	 * is not connection-aware yet.
	 * For now: Truncate according to the default ATT_MTU, so that
	 * notifications will go through
	 */

	/* TODO: Use bt_gatt_get_mtu() to find the ATT_MTU */
	const uint16_t att_mtu = 23;
	const uint16_t maxlen = att_mtu - 1 - 2; /* Subtract opcode and handle */
	const uint16_t len = strlen(str);

	if (len > maxlen) {
		/* Truncation requires, and gives, a null-terminated string. */
		char trunc_str[maxlen + 1];

		utf8_lcpy(trunc_str, str, sizeof(trunc_str));
		/* Null-termination is not sent on air */
		notify(uuid, (void *)trunc_str, strlen(trunc_str));
	} else {
		notify(uuid, (void *)str, len);
	}
}

void media_proxy_sctrl_track_changed_cb(void)
{
	BT_DBG("Notifying track change");
@@ -817,7 +849,7 @@ void media_proxy_sctrl_track_changed_cb(void)
void media_proxy_sctrl_track_title_cb(const char *title)
{
	BT_DBG("Notifying track title: %s", log_strdup(title));
	notify(BT_UUID_MCS_TRACK_TITLE, title, strlen(title));
	notify_string(BT_UUID_MCS_TRACK_TITLE, title);
}

void media_proxy_sctrl_track_position_cb(int32_t position)
+1 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ CONFIG_MCTL_LOCAL_PLAYER_LOCAL_CONTROL=y
CONFIG_MCTL_LOCAL_PLAYER_REMOTE_CONTROL=y
CONFIG_MCTL_REMOTE_PLAYER_CONTROL=y
CONFIG_MCTL_REMOTE_PLAYER_CONTROL_OBJECTS=y
CONFIG_UTF8=y

# Media player
CONFIG_BT_MPL=y
+1 −0
Original line number Diff line number Diff line
@@ -87,6 +87,7 @@ CONFIG_BT_MCS=y
CONFIG_BT_MCC=y
CONFIG_BT_MCC_OTS=y
CONFIG_BT_MCC_TOTAL_OBJ_CONTENT_MEM=4096
CONFIG_UTF8=y

# Object Transfer
CONFIG_BT_OTS=y