1
1
/*
2
- * Copyright (c) 2022 Nordic Semiconductor
2
+ * Copyright (c) 2022-2025 Nordic Semiconductor
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
10
10
11
11
#include "babblekit/testcase.h"
12
12
#include "babblekit/flags.h"
13
+ #include "bsim_args_runner.h"
14
+ #include "argparse.h"
13
15
14
16
extern enum bst_result_t bst_result ;
15
17
@@ -20,25 +22,63 @@ static struct bt_conn *default_conn;
20
22
DEFINE_FLAG_STATIC (is_connected );
21
23
DEFINE_FLAG_STATIC (chan_connected );
22
24
DEFINE_FLAG_STATIC (data_received );
25
+ DEFINE_FLAG_STATIC (data_sent );
23
26
24
- #define DATA_BYTE_VAL 0xBB
27
+ static bool fixed ;
28
+ static const char * l2cap_data = "Hello" ;
25
29
26
- /* L2CAP channel buffer pool */
27
- NET_BUF_POOL_DEFINE (buf_pool , 1 , BT_L2CAP_SDU_BUF_SIZE (16 ), CONFIG_BT_CONN_TX_USER_DATA_SIZE , NULL );
30
+ /* L2CAP dynamic channel buffer pool */
31
+ NET_BUF_POOL_DEFINE (sdu_buf_pool , 1 , BT_L2CAP_SDU_BUF_SIZE (16 ), CONFIG_BT_CONN_TX_USER_DATA_SIZE ,
32
+ NULL );
33
+
34
+ #define FIXED_CID 0x21
35
+
36
+ /* L2CAP fixed channel buffer pool */
37
+ NET_BUF_POOL_DEFINE (pdu_buf_pool , 1 , BT_L2CAP_BUF_SIZE (CONFIG_BT_L2CAP_TX_MTU ),
38
+ CONFIG_BT_CONN_TX_USER_DATA_SIZE , NULL );
39
+
40
+ static void test_args (int argc , char * argv [])
41
+ {
42
+ bs_args_struct_t args_struct [] = {
43
+ {
44
+ .dest = & fixed ,
45
+ .type = 'b' ,
46
+ .name = "{0, 1}" ,
47
+ .option = "fixed" ,
48
+ .descript = "Use fixed (true) or dynamic (false) channel."
49
+ },
50
+ };
51
+
52
+ bs_args_parse_all_cmd_line (argc , argv , args_struct );
53
+ }
28
54
29
55
static void chan_connected_cb (struct bt_l2cap_chan * l2cap_chan )
30
56
{
31
57
struct net_buf * buf ;
32
58
int err ;
59
+ struct bt_l2cap_le_chan * le_chan = BT_L2CAP_LE_CHAN (l2cap_chan );
60
+
61
+ /* If we're testing dynamic channels, skip sending data on the fixed channel. */
62
+ if (!fixed && le_chan -> tx .cid == FIXED_CID ) {
63
+ return ;
64
+ }
33
65
34
66
/* Send data immediately on L2CAP connection */
35
- buf = net_buf_alloc (& buf_pool , K_NO_WAIT );
67
+ if (fixed ) {
68
+ buf = net_buf_alloc (& pdu_buf_pool , K_NO_WAIT );
69
+ } else {
70
+ buf = net_buf_alloc (& sdu_buf_pool , K_NO_WAIT );
71
+ }
36
72
if (!buf ) {
37
73
TEST_FAIL ("Buffer allocation failed" );
38
74
}
39
75
40
- (void )net_buf_reserve (buf , BT_L2CAP_SDU_CHAN_SEND_RESERVE );
41
- (void )net_buf_add_u8 (buf , DATA_BYTE_VAL );
76
+ if (fixed ) {
77
+ (void )net_buf_reserve (buf , BT_L2CAP_CHAN_SEND_RESERVE );
78
+ } else {
79
+ (void )net_buf_reserve (buf , BT_L2CAP_SDU_CHAN_SEND_RESERVE );
80
+ }
81
+ (void )net_buf_add_mem (buf , l2cap_data , strlen (l2cap_data ) + 1 );
42
82
43
83
/* Try to send data */
44
84
err = bt_l2cap_chan_send (l2cap_chan , buf );
@@ -60,7 +100,7 @@ static int chan_recv_cb(struct bt_l2cap_chan *chan, struct net_buf *buf)
60
100
{
61
101
(void )chan ;
62
102
63
- if ((buf -> len != 1 ) || ( buf -> data [ 0 ] != DATA_BYTE_VAL ) ) {
103
+ if (strcmp (buf -> data , l2cap_data ) != 0 ) {
64
104
TEST_FAIL ("Unexpected data received" );
65
105
}
66
106
@@ -69,19 +109,27 @@ static int chan_recv_cb(struct bt_l2cap_chan *chan, struct net_buf *buf)
69
109
return 0 ;
70
110
}
71
111
112
+ static void chan_sent_cb (struct bt_l2cap_chan * chan )
113
+ {
114
+ (void )chan ;
115
+ SET_FLAG (data_sent );
116
+ }
117
+
72
118
static const struct bt_l2cap_chan_ops l2cap_ops = {
73
119
.connected = chan_connected_cb ,
74
120
.disconnected = chan_disconnected_cb ,
75
121
.recv = chan_recv_cb ,
122
+ .sent = chan_sent_cb ,
76
123
};
77
124
78
- static struct bt_l2cap_le_chan channel ;
125
+ static struct bt_l2cap_le_chan dyn_chan ;
126
+ static struct bt_l2cap_chan fixed_chan ;
79
127
80
128
static int accept (struct bt_conn * conn , struct bt_l2cap_server * server ,
81
129
struct bt_l2cap_chan * * l2cap_chan )
82
130
{
83
- channel .chan .ops = & l2cap_ops ;
84
- * l2cap_chan = & channel .chan ;
131
+ dyn_chan .chan .ops = & l2cap_ops ;
132
+ * l2cap_chan = & dyn_chan .chan ;
85
133
86
134
return 0 ;
87
135
}
@@ -92,20 +140,36 @@ static struct bt_l2cap_server server = {
92
140
.psm = PSM ,
93
141
};
94
142
143
+ static int l2cap_fixed_accept (struct bt_conn * conn , struct bt_l2cap_chan * * chan )
144
+ {
145
+ * chan = & fixed_chan ;
146
+
147
+ * * chan = (struct bt_l2cap_chan ){
148
+ .ops = & l2cap_ops ,
149
+ };
150
+
151
+ return 0 ;
152
+ }
153
+
154
+ BT_L2CAP_FIXED_CHANNEL_DEFINE (fixed_chan_1 ) = {
155
+ .cid = FIXED_CID ,
156
+ .accept = l2cap_fixed_accept ,
157
+ };
158
+
95
159
static void connect_l2cap_channel (void )
96
160
{
97
- struct bt_l2cap_chan * chans [] = {& channel .chan , NULL };
161
+ struct bt_l2cap_chan * chans [] = {& dyn_chan .chan , NULL };
98
162
int err ;
99
163
100
- channel .chan .ops = & l2cap_ops ;
164
+ dyn_chan .chan .ops = & l2cap_ops ;
101
165
102
166
if (IS_ENABLED (CONFIG_BT_L2CAP_ECRED )) {
103
167
err = bt_l2cap_ecred_chan_connect (default_conn , chans , server .psm );
104
168
if (err ) {
105
169
TEST_FAIL ("Failed to send ecred connection request (err %d)" , err );
106
170
}
107
171
} else {
108
- err = bt_l2cap_chan_connect (default_conn , & channel .chan , server .psm );
172
+ err = bt_l2cap_chan_connect (default_conn , & dyn_chan .chan , server .psm );
109
173
if (err ) {
110
174
TEST_FAIL ("Failed to send connection request (err %d)" , err );
111
175
}
@@ -187,7 +251,9 @@ static void test_peripheral_main(void)
187
251
return ;
188
252
}
189
253
190
- register_l2cap_server ();
254
+ if (!fixed ) {
255
+ register_l2cap_server ();
256
+ }
191
257
192
258
err = bt_le_adv_start (BT_LE_ADV_CONN_FAST_1 , ad , ARRAY_SIZE (ad ), NULL , 0 );
193
259
if (err != 0 ) {
@@ -199,11 +265,13 @@ static void test_peripheral_main(void)
199
265
200
266
WAIT_FOR_FLAG (chan_connected );
201
267
268
+ WAIT_FOR_FLAG (data_sent );
269
+
202
270
WAIT_FOR_FLAG (data_received );
203
271
204
272
WAIT_FOR_FLAG_UNSET (is_connected );
205
273
206
- TEST_PASS ("Test passed" );
274
+ TEST_PASS ("Test passed" );
207
275
}
208
276
209
277
static void test_central_main (void )
@@ -222,9 +290,14 @@ static void test_central_main(void)
222
290
223
291
WAIT_FOR_FLAG (is_connected );
224
292
225
- connect_l2cap_channel ();
293
+ if (!fixed ) {
294
+ connect_l2cap_channel ();
295
+ }
296
+
226
297
WAIT_FOR_FLAG (chan_connected );
227
298
299
+ WAIT_FOR_FLAG (data_sent );
300
+
228
301
WAIT_FOR_FLAG (data_received );
229
302
230
303
err = bt_conn_disconnect (default_conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN );
@@ -242,11 +315,13 @@ static const struct bst_test_instance test_def[] = {
242
315
{
243
316
.test_id = "peripheral" ,
244
317
.test_descr = "Peripheral" ,
318
+ .test_args_f = test_args ,
245
319
.test_main_f = test_peripheral_main ,
246
320
},
247
321
{
248
322
.test_id = "central" ,
249
323
.test_descr = "Central" ,
324
+ .test_args_f = test_args ,
250
325
.test_main_f = test_central_main ,
251
326
},
252
327
BSTEST_END_MARKER ,
0 commit comments