Skip to content

Commit 59dd898

Browse files
CarbuneanuAlexandrucarlescufi
authored andcommitted
test: bsim: Notification EATT test
Increasing the test coverage of notification on EATT bearers. - test implementation Signed-off-by: Alexandru Carbuneanu <[email protected]>
1 parent 8ff873e commit 59dd898

File tree

9 files changed

+582
-0
lines changed

9 files changed

+582
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
if (NOT DEFINED ENV{BSIM_COMPONENTS_PATH})
6+
message(FATAL_ERROR "This test requires the BabbleSim simulator. Please set\
7+
the environment variable BSIM_COMPONENTS_PATH to point to its components \
8+
folder. More information can be found in\
9+
https://babblesim.github.io/folder_structure_and_env.html")
10+
endif()
11+
12+
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
13+
project(bsim_test_eatt_notif)
14+
15+
FILE(GLOB app_sources src/*.c)
16+
target_sources(app PRIVATE ${app_sources} )
17+
18+
zephyr_include_directories(
19+
$ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
20+
$ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
21+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CONFIG_BT=y
2+
CONFIG_BT_CENTRAL=y
3+
CONFIG_BT_PERIPHERAL=y
4+
CONFIG_BT_SMP=y
5+
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
6+
CONFIG_BT_DEVICE_NAME="EATT test"
7+
CONFIG_BT_EATT=y
8+
CONFIG_BT_TESTING=y
9+
CONFIG_BT_EATT_AUTO_CONNECT=n
10+
CONFIG_BT_L2CAP_ECRED=y
11+
CONFIG_BT_EATT_MAX=16
12+
CONFIG_BT_MAX_CONN=1
13+
CONFIG_BT_GATT_CLIENT=y
14+
CONFIG_BT_ATT_PREPARE_COUNT=3
15+
CONFIG_ASSERT=y
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* EATT notification reliability test:
7+
* A central acting as a GATT client scans and connects
8+
* to a peripheral acting as a GATT server.
9+
* The GATT client will then attempt to connect a number of CONFIG_BT_EATT_MAX bearers
10+
* over EATT, send notifications, disconnect all bearers and reconnect EATT_BEARERS_TEST
11+
* and send start a transaction with a request, then send a lot of notifications
12+
* before the response is received.
13+
* The test might be expanded by checking that all the notifications all transmitted
14+
* on EATT channels.
15+
*/
16+
17+
#include <bluetooth/bluetooth.h>
18+
#include <bluetooth/gatt.h>
19+
#include <bluetooth/conn.h>
20+
#include <bluetooth/att.h>
21+
22+
#include "common.h"
23+
24+
CREATE_FLAG(flag_is_connected);
25+
CREATE_FLAG(flag_discover_complete);
26+
27+
static struct bt_conn *g_conn;
28+
static const struct bt_gatt_attr *local_attr;
29+
static struct bt_uuid *test_svc_uuid = TEST_SERVICE_UUID;
30+
31+
#define NUM_NOTIF 100
32+
#define SAMPLE_DATA 1
33+
#define EATT_BEARERS_TEST 1
34+
35+
volatile int num_eatt_channels;
36+
37+
static void connected(struct bt_conn *conn, uint8_t err)
38+
{
39+
char addr[BT_ADDR_LE_STR_LEN];
40+
41+
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
42+
43+
if (err != 0) {
44+
FAIL("Failed to connect to %s (%u)\n", addr, err);
45+
return;
46+
}
47+
48+
printk("Connected to %s\n", addr);
49+
SET_FLAG(flag_is_connected);
50+
}
51+
52+
static void disconnected(struct bt_conn *conn, uint8_t reason)
53+
{
54+
char addr[BT_ADDR_LE_STR_LEN];
55+
56+
if (conn != g_conn) {
57+
return;
58+
}
59+
60+
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
61+
62+
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
63+
64+
bt_conn_unref(g_conn);
65+
66+
g_conn = NULL;
67+
UNSET_FLAG(flag_is_connected);
68+
}
69+
70+
BT_CONN_CB_DEFINE(conn_callbacks) = {
71+
.connected = connected,
72+
.disconnected = disconnected,
73+
};
74+
75+
void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
76+
struct net_buf_simple *ad)
77+
{
78+
char addr_str[BT_ADDR_LE_STR_LEN];
79+
int err;
80+
81+
if (g_conn != NULL) {
82+
return;
83+
}
84+
85+
/* We're only interested in connectable events */
86+
if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) {
87+
return;
88+
}
89+
90+
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
91+
printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
92+
93+
printk("Stopping scan\n");
94+
err = bt_le_scan_stop();
95+
if (err != 0) {
96+
FAIL("Could not stop scan: %d");
97+
return;
98+
}
99+
100+
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
101+
BT_LE_CONN_PARAM_DEFAULT, &g_conn);
102+
if (err != 0) {
103+
FAIL("Could not connect to peer: %d", err);
104+
}
105+
}
106+
107+
void send_notification(void)
108+
{
109+
const uint8_t sample_dat = SAMPLE_DATA;
110+
int err;
111+
112+
do {
113+
err = bt_gatt_notify(g_conn, local_attr, &sample_dat, sizeof(sample_dat));
114+
if (!err) {
115+
return;
116+
} else if (err != -ENOMEM) {
117+
printk("GATT notify failed (err %d)\n", err);
118+
return;
119+
}
120+
k_sleep(K_TICKS(1));
121+
} while (err == -ENOMEM);
122+
}
123+
124+
static uint8_t discover_func(struct bt_conn *conn,
125+
const struct bt_gatt_attr *attr,
126+
struct bt_gatt_discover_params *params)
127+
{
128+
SET_FLAG(flag_discover_complete);
129+
printk("Discover complete\n");
130+
131+
return BT_GATT_ITER_STOP;
132+
}
133+
134+
static void gatt_discover(void)
135+
{
136+
static struct bt_gatt_discover_params discover_params;
137+
int err;
138+
139+
printk("Discovering services and characteristics\n");
140+
141+
discover_params.uuid = test_svc_uuid;
142+
discover_params.func = discover_func;
143+
discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
144+
discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
145+
discover_params.type = BT_GATT_DISCOVER_PRIMARY;
146+
147+
err = bt_gatt_discover(g_conn, &discover_params);
148+
if (err != 0) {
149+
FAIL("Discover failed(err %d)\n", err);
150+
}
151+
}
152+
153+
BT_GATT_SERVICE_DEFINE(g_svc,
154+
BT_GATT_PRIMARY_SERVICE(TEST_SERVICE_UUID),
155+
BT_GATT_CHARACTERISTIC(TEST_CHRC_UUID, BT_GATT_CHRC_NOTIFY,
156+
0x00, NULL, NULL, NULL));
157+
158+
static void test_main(void)
159+
{
160+
int err;
161+
162+
device_sync_init(PERIPHERAL_ID);
163+
164+
err = bt_enable(NULL);
165+
if (err != 0) {
166+
FAIL("Bluetooth enable failed (err %d)\n", err);
167+
}
168+
169+
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
170+
if (err != 0) {
171+
FAIL("Scanning failed to start (err %d)\n", err);
172+
}
173+
174+
printk("Scanning successfully started\n");
175+
176+
WAIT_FOR_FLAG(flag_is_connected);
177+
178+
err = bt_eatt_connect(g_conn, CONFIG_BT_EATT_MAX);
179+
if (err) {
180+
FAIL("Sending credit based connection request failed (err %d)\n", err);
181+
}
182+
183+
/* Wait for the channels to be connected */
184+
while (bt_eatt_count(g_conn) < CONFIG_BT_EATT_MAX) {
185+
k_sleep(K_TICKS(1));
186+
}
187+
188+
local_attr = &g_svc.attrs[1];
189+
printk("############# Notification test\n");
190+
for (int idx = 0; idx < NUM_NOTIF; idx++) {
191+
printk("Notification %d\n", idx);
192+
send_notification();
193+
}
194+
195+
printk("############# Disconnect and reconnect\n");
196+
for (int idx = 0; idx < CONFIG_BT_EATT_MAX; idx++) {
197+
bt_eatt_disconnect_one(g_conn);
198+
while (bt_eatt_count(g_conn) != (CONFIG_BT_EATT_MAX - idx)) {
199+
k_sleep(K_TICKS(1));
200+
}
201+
}
202+
203+
printk("Connecting %d bearers\n", EATT_BEARERS_TEST);
204+
err = bt_eatt_connect(g_conn, EATT_BEARERS_TEST);
205+
if (err) {
206+
FAIL("Sending credit based connection request failed (err %d)\n", err);
207+
}
208+
209+
/* Wait for the channels to be connected */
210+
while (bt_eatt_count(g_conn) < EATT_BEARERS_TEST) {
211+
k_sleep(K_TICKS(1));
212+
}
213+
214+
printk("############# Send notifications during discovery request\n");
215+
gatt_discover();
216+
while (!TEST_FLAG(flag_discover_complete)) {
217+
printk("Notifying...\n");
218+
send_notification();
219+
}
220+
221+
printk("Send sync to contine\n");
222+
device_sync_send();
223+
224+
PASS("Client Passed\n");
225+
}
226+
227+
static const struct bst_test_instance test_vcs[] = {
228+
{
229+
.test_id = "client",
230+
.test_post_init_f = test_init,
231+
.test_tick_f = test_tick,
232+
.test_main_f = test_main
233+
},
234+
BSTEST_END_MARKER
235+
};
236+
237+
struct bst_test_list *test_client_install(struct bst_test_list *tests)
238+
{
239+
return bst_add_tests(tests, test_vcs);
240+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "common.h"
8+
#include <logging/log.h>
9+
10+
#define LOG_MODULE_NAME common
11+
12+
LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG);
13+
14+
void test_tick(bs_time_t HW_device_time)
15+
{
16+
if (bst_result != Passed) {
17+
FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME);
18+
}
19+
}
20+
21+
void test_init(void)
22+
{
23+
bst_ticker_set_next_tick_absolute(WAIT_TIME);
24+
bst_result = In_progress;
25+
}
26+
27+
/* Call in init functions*/
28+
void device_sync_init(uint device_nbr)
29+
{
30+
uint peer;
31+
32+
if (device_nbr == CENTRAL_ID) {
33+
peer = PERIPHERAL_ID;
34+
} else {
35+
peer = CENTRAL_ID;
36+
}
37+
38+
uint dev_nbrs[BACK_CHANNELS] = { peer };
39+
uint channel_nbrs[BACK_CHANNELS] = { 0 };
40+
const uint *ch = bs_open_back_channel(device_nbr, dev_nbrs, channel_nbrs, BACK_CHANNELS);
41+
42+
if (!ch) {
43+
LOG_ERR("bs_open_back_channel failed!");
44+
}
45+
}
46+
47+
/* Call it to make peer to proceed.*/
48+
void device_sync_send(void)
49+
{
50+
uint8_t msg[1] = "S";
51+
52+
bs_bc_send_msg(0, msg, sizeof(msg));
53+
}
54+
55+
/* Wait until peer send sync*/
56+
void device_sync_wait(void)
57+
{
58+
int size_msg_received = 0;
59+
uint8_t msg;
60+
61+
while (!size_msg_received) {
62+
size_msg_received = bs_bc_is_msg_received(0);
63+
k_sleep(K_MSEC(1));
64+
}
65+
66+
bs_bc_receive_msg(0, &msg, size_msg_received);
67+
}

0 commit comments

Comments
 (0)