From ea41a24d3adaeeeffe9dac4413e937da611f02c1 Mon Sep 17 00:00:00 2001 From: Jonathan Rico Date: Sat, 25 May 2024 22:31:00 +0200 Subject: [PATCH] Bluetooth: tests: fix double-registering of callbacks This was the source of assert failures. If only they were enabled in the first place... The issue is that in order to save on build time, these two tests build only one image, from which two devices are instantiated (`gatt_client_test.c` and `gatt_server_test.c`). The double-registration happens as `BT_CONN_CB_DEFINE()` is build-time, and is included in two files of the same image. Then _both_ images will end up with both `connected()` `disconnected()` etc callbacks, which have logic specific to their respective images/devices. The patch uses runtime registration instead so each device only ever registers its own callbacks and not the other device's too. Signed-off-by: Jonathan Rico --- .../bluetooth/host/gatt/authorization/src/gatt_client_test.c | 4 +++- .../bluetooth/host/gatt/authorization/src/gatt_server_test.c | 4 +++- tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c | 4 +++- tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c index 9c60c5b57b2..d276bfcedc3 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c @@ -60,7 +60,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) UNSET_FLAG(flag_is_connected); } -BT_CONN_CB_DEFINE(conn_callbacks) = { +static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, }; @@ -297,6 +297,8 @@ static void test_main(void) { int err; + bt_conn_cb_register(&conn_callbacks); + err = bt_enable(NULL); if (err != 0) { FAIL("Bluetooth discover failed (err %d)\n", err); diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c index 03942821640..a09b94367fb 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c @@ -45,7 +45,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) g_conn = NULL; } -BT_CONN_CB_DEFINE(conn_callbacks) = { +static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, }; @@ -331,6 +331,8 @@ static void test_main(void) return; } + bt_conn_cb_register(&conn_callbacks); + err = bt_enable(NULL); if (err != 0) { FAIL("Bluetooth init failed (err %d)\n", err); diff --git a/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c index 0ae87791f81..cc233510b0b 100644 --- a/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c @@ -74,7 +74,7 @@ static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_ } } -BT_CONN_CB_DEFINE(conn_callbacks) = { +static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, .security_changed = security_changed, @@ -351,6 +351,8 @@ static void test_main(void) { int err; + bt_conn_cb_register(&conn_callbacks); + err = bt_enable(NULL); if (err != 0) { FAIL("Bluetooth discover failed (err %d)\n", err); diff --git a/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c index e41d568dff8..c77fe39c000 100644 --- a/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c @@ -47,7 +47,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) UNSET_FLAG(flag_is_connected); } -BT_CONN_CB_DEFINE(conn_callbacks) = { +static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, }; @@ -151,6 +151,8 @@ static void test_main(void) BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)) }; + bt_conn_cb_register(&conn_callbacks); + err = bt_enable(NULL); if (err != 0) { FAIL("Bluetooth init failed (err %d)\n", err);