Skip to content

Commit 01ba7ec

Browse files
theob-prom-alperen-sener
authored andcommitted
[nrf frtomtree] Tests: Bluetooth: Add MTU exchange procedure in testlib
Add `bt_testlib_att_exchange_mtu` function to simplify the procedure. Signed-off-by: Théo Battrel <[email protected]> (cherry picked from commit e3cf59b)
1 parent 2c2fd96 commit 01ba7ec

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

tests/bluetooth/common/testlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_include_directories(testlib PUBLIC
1515

1616
target_sources(testlib PRIVATE
1717
src/adv.c
18+
src/att.c
1819
src/att_read.c
1920
src/att_write.c
2021
src/conn_ref.c
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ATT_H_
8+
#define ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ATT_H_
9+
10+
#include <stdint.h>
11+
#include <zephyr/bluetooth/conn.h>
12+
13+
int bt_testlib_att_exchange_mtu(struct bt_conn *conn);
14+
15+
#endif /* ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ATT_H_ */
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include <zephyr/bluetooth/bluetooth.h>
10+
#include <zephyr/bluetooth/conn.h>
11+
#include <zephyr/bluetooth/gatt.h>
12+
#include <zephyr/bluetooth/att.h>
13+
14+
#include <zephyr/logging/log.h>
15+
16+
LOG_MODULE_REGISTER(bt_testlib_att, LOG_LEVEL_DBG);
17+
18+
static uint8_t exchange_mtu_err;
19+
static K_MUTEX_DEFINE(exchange_mtu_lock);
20+
static K_CONDVAR_DEFINE(exchange_mtu_done);
21+
22+
static void bt_testlib_att_exchange_mtu_cb(struct bt_conn *conn, uint8_t err,
23+
struct bt_gatt_exchange_params *params)
24+
{
25+
char addr[BT_ADDR_LE_STR_LEN];
26+
27+
k_mutex_lock(&exchange_mtu_lock, K_FOREVER);
28+
29+
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
30+
31+
LOG_DBG("MTU exchange %s (%s)", err == 0U ? "successful" : "failed", addr);
32+
33+
exchange_mtu_err = err;
34+
35+
k_condvar_signal(&exchange_mtu_done);
36+
37+
k_mutex_unlock(&exchange_mtu_lock);
38+
}
39+
40+
int bt_testlib_att_exchange_mtu(struct bt_conn *conn)
41+
{
42+
int err;
43+
struct bt_gatt_exchange_params exchange_mtu_params;
44+
45+
__ASSERT_NO_MSG(conn != NULL);
46+
47+
k_mutex_lock(&exchange_mtu_lock, K_FOREVER);
48+
49+
exchange_mtu_err = 0;
50+
51+
exchange_mtu_params.func = bt_testlib_att_exchange_mtu_cb;
52+
53+
err = bt_gatt_exchange_mtu(conn, &exchange_mtu_params);
54+
if (err != 0) {
55+
LOG_ERR("Failed to exchange MTU (err %d)", err);
56+
57+
k_mutex_unlock(&exchange_mtu_lock);
58+
59+
return err;
60+
}
61+
62+
k_condvar_wait(&exchange_mtu_done, &exchange_mtu_lock, K_FOREVER);
63+
64+
err = exchange_mtu_err;
65+
66+
k_mutex_unlock(&exchange_mtu_lock);
67+
68+
return err;
69+
}

0 commit comments

Comments
 (0)