Skip to contentSkip to conversation

Deduplication of avdtp

PATCH Superseded Andrei Emeltchenko 1 participant 10 messages Feb 13, 2015 Bluetooth drivers

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Deduplication of avdtp

From: Andrei Emeltchenko <[email protected]>

Second iteration of AV deduplication.

Changes:
* v2: Fix fixup patch
* v1: Add SEID fixes with unit tests
* RFC: initial version

Andrei Emeltchenko (8):
unit/avdtp: Add SEP register/unregister tests unit/avdtp: Add duplicate SEID test shared/utils: Add helpers for bitfield fast uid generation android/avdtp: Fix SEID generation
unit/avdtp: Add check for too many SEP creation android/avdtp: Fix registering SEP with wrong SEID android/avdtp: Refactor local SEP list handling unit/avdtp: Refactor context destroy

 android/Makefile.am |   1 +
 android/a2dp.c      |  19 +++++--
 android/avdtp.c     |  65 ++++++++++++++++--------
 android/avdtp.h     |   2 +
 android/avdtptest.c |   6 +++
 src/shared/util.c   |  24 +++++++++
 src/shared/util.h   |   3 ++
 unit/test-avdtp.c   | 139 ++++++++++++++++++++++++++++++++++++++++++++++++----
 8 files changed, 227 insertions(+), 32 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH v2 1/8 unit/avdtp: Add SEP register/unregister tests

From: Andrei Emeltchenko <[email protected]>

Add register_sep and unregister_sep testing registering maximum amount of SEPs.

---
 unit/test-avdtp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
unit/test-avdtp.c +34 -0
index 8fe5ce3..c9ff014 100644
@@ -39,6 +39,10 @@
 #include "src/log.h"
 #include "android/avdtp.h"
 
+#define MAX_SEID 0x3E
+
+GSList *lseps = NULL;
+
 struct test_pdu {
 	bool valid;
 	bool fragmented;
@@ -551,6 +555,34 @@ static void test_server_0_sep(gconstpointer data)
 	execute_context(context);
 }
 
+static void unregister_sep(void *data)
+{
+	struct avdtp_local_sep *sep = data;
+
+	avdtp_unregister_sep(sep);
+}
+
+static void test_server_seid(gconstpointer data)
+{
+	struct context *context = create_context(0x0103, data);
+	struct avdtp_local_sep *sep;
+	int i;
+
+	for (i = 0; i < MAX_SEID; i++) {
+		sep = avdtp_register_sep(AVDTP_SEP_TYPE_SINK,
+						AVDTP_MEDIA_TYPE_AUDIO,
+						0x00, TRUE, &sep_ind, NULL,
+						context);
+		g_assert(sep);
+
+		lseps = g_slist_append(lseps, sep);
+	}
+
+	/* Remove all SEPs */
+	g_slist_free_full(lseps, unregister_sep);
+	lseps = NULL;
+}
+
 static gboolean sep_getcap_ind_frg(struct avdtp *session,
 					struct avdtp_local_sep *sep,
 					GSList **caps, uint8_t *err,
@@ -738,6 +770,8 @@ int main(int argc, char *argv[])
 	 * To verify that the following procedures are implemented according to
 	 * their specification in AVDTP.
 	 */
+	define_test("/TP/SIG/SMG/BV-06-C-SEID-1", test_server_seid,
+			raw_pdu(0x00));
 	define_test("/TP/SIG/SMG/BV-05-C", test_client,
 			raw_pdu(0x00, 0x01));
 	define_test("/TP/SIG/SMG/BV-06-C", test_server,
Signature · 1 line
2.1.0
#2 lore raw

PATCH v2 2/8 unit/avdtp: Add duplicate SEID test

In reply to Andrei Emeltchenko

From: Andrei Emeltchenko <[email protected]>

Add test adding two SEP, then remove first and create another one. With the current code new SEP has the same SEID (2!). Following patches fixing the bug.

---
 unit/test-avdtp.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
unit/test-avdtp.c +43 -0
index c9ff014..60eea7a 100644
@@ -583,6 +583,46 @@ static void test_server_seid(gconstpointer data)
 	lseps = NULL;
 }
 
+static void test_server_seid_duplicate(gconstpointer data)
+{
+	struct context *context = create_context(0x0103, data);
+	struct avdtp_local_sep *sep;
+	int i;
+
+	for (i = 0; i < 2; i++) {
+		sep = avdtp_register_sep(AVDTP_SEP_TYPE_SINK,
+						AVDTP_MEDIA_TYPE_AUDIO,
+						0x00, TRUE, &sep_ind, NULL,
+						context);
+		g_assert(sep);
+
+		lseps = g_slist_append(lseps, sep);
+	}
+
+	/* Remove 1st element */
+	sep = g_slist_nth_data(lseps, 0);
+	lseps = g_slist_remove(lseps, sep);
+	avdtp_unregister_sep(sep);
+
+	/* Now register new element */
+	sep = avdtp_register_sep(AVDTP_SEP_TYPE_SINK,
+						AVDTP_MEDIA_TYPE_AUDIO,
+						0x00, TRUE, &sep_ind, NULL,
+						context);
+	g_assert(sep);
+	lseps = g_slist_append(lseps, sep);
+
+	/* Check SEID ids */
+
+	g_idle_add(send_pdu, context);
+
+	execute_context(context);
+
+	/* Remove all SEPs */
+	g_slist_free_full(lseps, unregister_sep);
+	lseps = NULL;
+}
+
 static gboolean sep_getcap_ind_frg(struct avdtp *session,
 					struct avdtp_local_sep *sep,
 					GSList **caps, uint8_t *err,
@@ -772,6 +812,9 @@ int main(int argc, char *argv[])
 	 */
 	define_test("/TP/SIG/SMG/BV-06-C-SEID-1", test_server_seid,
 			raw_pdu(0x00));
+	define_test("/TP/SIG/SMG/BV-06-C-SEID-2", test_server_seid_duplicate,
+			raw_pdu(0x00, 0x01),
+			raw_pdu(0x02, 0x01, 0x08, 0x08, 0x0c, 0x08));
 	define_test("/TP/SIG/SMG/BV-05-C", test_client,
 			raw_pdu(0x00, 0x01));
 	define_test("/TP/SIG/SMG/BV-06-C", test_server,
Signature · 1 line
2.1.0
#3 lore raw

PATCH v2 3/8 shared/utils: Add helpers for bitfield fast uid generation

In reply to Andrei Emeltchenko

From: Andrei Emeltchenko <[email protected]>

For small unique id generation we may use bitfield since it is very fast and memory efficient. If we need to generate id from 1 to 64 helper functions are added to utils.

---
 src/shared/util.c | 24 ++++++++++++++++++++++++
 src/shared/util.h |  3 +++
 2 files changed, 27 insertions(+)
src/shared/util.c +24 -0
index 74ffb08..484031f 100644
@@ -106,3 +106,27 @@ unsigned char util_get_dt(const char *parent, const char *name)
 
 	return DT_UNKNOWN;
 }
+
+/* Helpers for bitfield operations */
+/* Find unique id from 1 to max */
+uint8_t util_get_bitmap64_uid(uint64_t *bitmap, uint8_t max)
+{
+	uint64_t temp = *bitmap;
+	uint64_t id = 1;
+
+	while (temp >>= 1)
+		id++;
+
+	if (id > max)
+		return 0;
+
+	*bitmap |= 0x01l << id;
+
+	return id;
+}
+
+/* Clear id bit in bitmap */
+void util_clear_bitmap64(uint64_t *bitmap, uint8_t id)
+{
+	*bitmap &= ~(0x01l << id);
+}
src/shared/util.h +3 -0
index 8437662..8f28c24 100644
@@ -93,6 +93,9 @@ void util_hexdump(const char dir, const unsigned char *buf, size_t len,
 
 unsigned char util_get_dt(const char *parent, const char *name);
 
+uint8_t util_get_bitmap64_uid(uint64_t *bitmap, uint8_t max);
+void util_clear_bitmap64(uint64_t *bitmap, uint8_t id);
+
 static inline void bswap_128(const void *src, void *dst)
 {
 	const uint8_t *s = src;
Signature · 1 line
2.1.0
#4 lore raw

PATCH 1/1 shared/utils: Add helpers for bitfield fast uid generation

From: Andrei Emeltchenko <[email protected]>

For small unique id generation we may use bitfield since it is very fast and memory efficient. If we need to generate id from 1 to 64 helper functions are added to utils.

---
 src/shared/util.c | 24 ++++++++++++++++++++++++
 src/shared/util.h |  3 +++
 2 files changed, 27 insertions(+)
src/shared/util.c +24 -0
index 74ffb08..3ae6b9e 100644
@@ -106,3 +106,27 @@ unsigned char util_get_dt(const char *parent, const char *name)
 
 	return DT_UNKNOWN;
 }
+
+/* Helpers for bitfield operations */
+/* Find unique id from 1 to max */
+uint8_t util_get_bitmap64_uid(uint64_t *bitmap, uint8_t max)
+{
+	uint64_t temp = *bitmap >> 1;
+	uint64_t id = 1;
+
+	for (; temp & 0x01; temp >>= 1)
+		id++;
+
+	if (id > max)
+		return 0;
+
+	*bitmap |= 0x01l << id;
+
+	return id;
+}
+
+/* Clear id bit in bitmap */
+void util_clear_bitmap64(uint64_t *bitmap, uint8_t id)
+{
+	*bitmap &= ~(0x01l << id);
+}
src/shared/util.h +3 -0
index 8437662..8f28c24 100644
@@ -93,6 +93,9 @@ void util_hexdump(const char dir, const unsigned char *buf, size_t len,
 
 unsigned char util_get_dt(const char *parent, const char *name);
 
+uint8_t util_get_bitmap64_uid(uint64_t *bitmap, uint8_t max);
+void util_clear_bitmap64(uint64_t *bitmap, uint8_t id);
+
 static inline void bswap_128(const void *src, void *dst)
 {
 	const uint8_t *s = src;
Signature · 1 line
2.1.0
#5 lore raw

PATCH v2 4/8 android/avdtp: Fix SEID generation

In reply to Andrei Emeltchenko

From: Andrei Emeltchenko <[email protected]>

Fixes SEID generation for avdtp. Currently SEIDs were assigned assuming that SEID are registered and unregistered in the same order like:
seid = g_slist_length(seps) + 1
which makes it possible to reuse similar SEIDs

---
 android/Makefile.am |  1 +
 android/avdtp.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
android/Makefile.am +1 -0
index 9e73be4..ebbb28e 100644
@@ -102,6 +102,7 @@ noinst_PROGRAMS += android/avdtptest
 android_avdtptest_SOURCES = android/avdtptest.c \
 				src/log.h src/log.c \
 				btio/btio.h btio/btio.c \
+				src/shared/util.h src/shared/util.c \
 				android/avdtp.h android/avdtp.c
 android_avdtptest_CFLAGS = $(AM_CFLAGS)
 android_avdtptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@
android/avdtp.c +20 -1
index 853fdf3..728f3fd 100644
@@ -41,10 +41,12 @@
 
 #include "lib/bluetooth.h"
 #include "src/log.h"
+#include "src/shared/util.h"
 #include "avdtp.h"
 #include "../profiles/audio/a2dp-codecs.h"
 
 #define MAX_SEID 0x3E
+static uint64_t seids;
 
 #ifndef MAX
 # define MAX(x, y) ((x) > (y) ? (x) : (y))
@@ -3342,6 +3344,22 @@ int avdtp_delay_report(struct avdtp *session, struct avdtp_stream *stream,
 							&req, sizeof(req));
 }
 
+static uint8_t avdtp_get_seid(void)
+{
+	uint8_t id = util_get_bitmap64_uid(&seids, MAX_SEID);
+
+	DBG("seids 0x%04lx id %d", seids, id);
+
+	return id;
+}
+
+static void avdtp_clear_seid(uint8_t seid)
+{
+	util_clear_bitmap64(&seids, seid);
+
+	DBG("seids 0x%04lx cleared bit %d ", seids, seid);
+}
+
 struct avdtp_local_sep *avdtp_register_sep(uint8_t type, uint8_t media_type,
 						uint8_t codec_type,
 						gboolean delay_reporting,
@@ -3357,7 +3375,7 @@ struct avdtp_local_sep *avdtp_register_sep(uint8_t type, uint8_t media_type,
 	sep = g_new0(struct avdtp_local_sep, 1);
 
 	sep->state = AVDTP_STATE_IDLE;
-	sep->info.seid = g_slist_length(lseps) + 1;
+	sep->info.seid = avdtp_get_seid();
 	sep->info.type = type;
 	sep->info.media_type = media_type;
 	sep->codec = codec_type;
@@ -3393,6 +3411,7 @@ int avdtp_unregister_sep(struct avdtp_local_sep *sep)
 	DBG("SEP %p unregistered: type:%d codec:%d seid:%d", sep,
 			sep->info.type, sep->codec, sep->info.seid);
 
+	avdtp_clear_seid(sep->info.seid);
 	g_free(sep);
 
 	return 0;
Signature · 1 line
2.1.0
#6 lore raw

PATCH v2 5/8 unit/avdtp: Add check for too many SEP creation

In reply to Andrei Emeltchenko

From: Andrei Emeltchenko <[email protected]>

Test checks that maximum numbers of SEPs registered is MAX_SEID.

---
 unit/test-avdtp.c | 7 +++++++
 1 file changed, 7 insertions(+)
unit/test-avdtp.c +7 -0
index 60eea7a..471cdde 100644
@@ -578,6 +578,13 @@ static void test_server_seid(gconstpointer data)
 		lseps = g_slist_append(lseps, sep);
 	}
 
+	/* Now add (MAX_SEID + 1) SEP -> it shall fail */
+	sep = avdtp_register_sep(AVDTP_SEP_TYPE_SINK,
+						AVDTP_MEDIA_TYPE_AUDIO,
+						0x00, TRUE, &sep_ind, NULL,
+						context);
+	g_assert(!sep);
+
 	/* Remove all SEPs */
 	g_slist_free_full(lseps, unregister_sep);
 	lseps = NULL;
Signature · 1 line
2.1.0
#7 lore raw

PATCH v2 6/8 android/avdtp: Fix registering SEP with wrong SEID

In reply to Andrei Emeltchenko

From: Andrei Emeltchenko <[email protected]>

Add seid check fixing registering too many SEPs

---
 android/avdtp.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
android/avdtp.c +5 -1
index 728f3fd..f4b2d45 100644
@@ -3368,6 +3368,10 @@ struct avdtp_local_sep *avdtp_register_sep(uint8_t type, uint8_t media_type,
 						void *user_data)
 {
 	struct avdtp_local_sep *sep;
+	uint8_t seid = avdtp_get_seid();
+
+	if (!seid)
+		return NULL;
 
 	if (g_slist_length(lseps) > MAX_SEID)
 		return NULL;
@@ -3375,7 +3379,7 @@ struct avdtp_local_sep *avdtp_register_sep(uint8_t type, uint8_t media_type,
 	sep = g_new0(struct avdtp_local_sep, 1);
 
 	sep->state = AVDTP_STATE_IDLE;
-	sep->info.seid = avdtp_get_seid();
+	sep->info.seid = seid;
 	sep->info.type = type;
 	sep->info.media_type = media_type;
 	sep->codec = codec_type;
Signature · 1 line
2.1.0
#8 lore raw

PATCH v2 7/8 android/avdtp: Refactor local SEP list handling

In reply to Andrei Emeltchenko

From: Andrei Emeltchenko <[email protected]>

In order to deduplicate similar code in android/ and profiles/ we need to handle local SEP list outside of avdtp.c.

---
 android/a2dp.c      | 19 ++++++++++++++++---
 android/avdtp.c     | 44 +++++++++++++++++++++++---------------------
 android/avdtp.h     |  2 ++
 android/avdtptest.c |  6 ++++++
 unit/test-avdtp.c   | 44 +++++++++++++++++++++++++++++++++++++-------
 5 files changed, 84 insertions(+), 31 deletions(-)
android/a2dp.c +16 -3
index 10f5523..f8f418f 100644
@@ -52,6 +52,7 @@
 #define SVC_HINT_CAPTURING 0x08
 #define IDLE_TIMEOUT 1
 #define AUDIO_RETRY_TIMEOUT 2
+#define MAX_SEID 0x3E
 
 static GIOChannel *server = NULL;
 static GSList *devices = NULL;
@@ -65,6 +66,8 @@ static bool audio_retrying = false;
 static struct ipc *hal_ipc = NULL;
 static struct ipc *audio_ipc = NULL;
 
+static GSList *lseps = NULL;
+
 struct a2dp_preset {
 	void *data;
 	int8_t len;
@@ -114,8 +117,10 @@ static void unregister_endpoint(void *data)
 {
 	struct a2dp_endpoint *endpoint = data;
 
-	if (endpoint->sep)
+	if (endpoint->sep) {
+		lseps = g_slist_remove(lseps, endpoint->sep);
 		avdtp_unregister_sep(endpoint->sep);
+	}
 
 	if (endpoint->caps)
 		preset_free(endpoint->caps);
@@ -620,6 +625,7 @@ static void signaling_connect_cb(GIOChannel *chan, GError *err,
 							gpointer user_data)
 {
 	struct a2dp_device *dev = user_data;
+	struct avdtp *session;
 	uint16_t imtu, omtu;
 	GError *gerr = NULL;
 	int fd;
@@ -643,10 +649,13 @@ static void signaling_connect_cb(GIOChannel *chan, GError *err,
 	fd = g_io_channel_unix_get_fd(chan);
 
 	/* FIXME: Add proper version */
-	dev->session = avdtp_new(fd, imtu, omtu, 0x0100);
-	if (!dev->session)
+	session = avdtp_new(fd, imtu, omtu, 0x0100);
+	if (!session)
 		goto failed;
 
+	avdtp_set_lseps(session, lseps);
+	dev->session = session;
+
 	avdtp_add_disconnect_cb(dev->session, disconnect_cb, dev);
 
 	/* Proceed to stream setup if initiator */
@@ -1324,6 +1333,9 @@ static uint8_t register_endpoint(const uint8_t *uuid, uint8_t codec,
 
 	/* FIXME: Add proper check for uuid */
 
+	if (g_slist_length(lseps) > MAX_SEID)
+		return 0;
+
 	endpoint = g_new0(struct a2dp_endpoint, 1);
 	endpoint->id = g_slist_length(endpoints) + 1;
 	endpoint->codec = codec;
@@ -1342,6 +1354,7 @@ static uint8_t register_endpoint(const uint8_t *uuid, uint8_t codec,
 						btohs(vndcodec->codec_id));
 	}
 
+	lseps = g_slist_append(lseps, endpoint->sep);
 	endpoints = g_slist_append(endpoints, endpoint);
 
 	return endpoint->id;
android/avdtp.c +23 -21

161 lines not on this page · raw message

android/avdtp.h +2 -0
index d5335e4..68786fe 100644
@@ -207,6 +207,8 @@ typedef void (*avdtp_disconnect_cb_t) (void *user_data);
 
 struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version);
 
+void avdtp_set_lseps(struct avdtp *session, GSList *lseps);
+
 unsigned int avdtp_add_disconnect_cb(struct avdtp *session,
 						avdtp_disconnect_cb_t cb,
 						void *user_data);
android/avdtptest.c +6 -0
index ce39519..f7f4749 100644
@@ -57,6 +57,7 @@ static uint16_t version = 0x0103;
 static guint media_player = 0;
 static guint media_recorder = 0;
 static guint idle_id = 0;
+static GSList *lseps = NULL;
 
 static bool fragment = false;
 
@@ -418,6 +419,7 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
 		return;
 	}
 
+	avdtp_set_lseps(avdtp, lseps);
 	avdtp_add_disconnect_cb(avdtp, disconnect_cb, NULL);
 
 	if (preconf) {
@@ -871,6 +873,8 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
+	lseps = g_slist_append(lseps, local_sep);
+
 	if (!bacmp(&dst, BDADDR_ANY)) {
 		printf("Listening...\n");
 		io = do_listen(&err);
@@ -889,6 +893,8 @@ int main(int argc, char *argv[])
 
 	printf("Done\n");
 
+	g_slist_free(lseps);
+
 	avdtp_unref(avdtp);
 	avdtp = NULL;
 
unit/test-avdtp.c +37 -7

126 lines not on this page · raw message

Signature · 1 line
2.1.0
#9 lore raw

PATCH v2 8/8 unit/avdtp: Refactor context destroy

In reply to Andrei Emeltchenko

From: Andrei Emeltchenko <[email protected]>

destroy_context() function will be used for dummy_tests without context_execute()

---
 unit/test-avdtp.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
unit/test-avdtp.c +10 -3
index a76198a..afbe875 100644
@@ -260,10 +260,8 @@ static struct context *create_context(uint16_t version, gconstpointer data)
 	return context_new(version, 672, 672, data);
 }
 
-static void execute_context(struct context *context)
+static void destroy_context(struct context *context)
 {
-	g_main_loop_run(context->main_loop);
-
 	if (context->source > 0)
 		g_source_remove(context->source);
 	avdtp_unref(context->session);
@@ -274,6 +272,13 @@ static void execute_context(struct context *context)
 	g_free(context);
 }
 
+static void execute_context(struct context *context)
+{
+	g_main_loop_run(context->main_loop);
+
+	destroy_context(context);
+}
+
 static gboolean sep_getcap_ind(struct avdtp *session,
 					struct avdtp_local_sep *sep,
 					GSList **caps, uint8_t *err,
@@ -600,6 +605,8 @@ static void test_server_seid(gconstpointer data)
 	/* Remove all SEPs */
 	g_slist_free_full(lseps, unregister_sep);
 	lseps = NULL;
+
+	destroy_context(context);
 }
 
 static void test_server_seid_duplicate(gconstpointer data)
Signature · 1 line
2.1.0