7
7
#include "bstests.h"
8
8
#include "babblekit/testcase.h"
9
9
#include <zephyr/bluetooth/conn.h>
10
+ #include <zephyr/bluetooth/hci.h>
10
11
11
12
/* Include conn_internal for the purpose of checking reference counts. */
12
13
#include "host/conn_internal.h"
@@ -42,7 +43,7 @@ static struct bt_conn_cb conn_cb = {
42
43
.connected = connected_cb ,
43
44
};
44
45
45
- static void test_central_connect_timeout_with_timeout (uint32_t timeout_ms )
46
+ static void test_central_connect_timeout_with_timeout (uint32_t timeout_ms , bool stack_load )
46
47
{
47
48
int err ;
48
49
struct bt_conn * conn ;
@@ -62,6 +63,7 @@ static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms)
62
63
.window_coded = 0 ,
63
64
.timeout = timeout_ms / 10 ,
64
65
};
66
+ struct net_buf * bufs [CONFIG_BT_BUF_CMD_TX_COUNT ];
65
67
66
68
k_sem_reset (& sem_failed_to_connect );
67
69
@@ -70,6 +72,21 @@ static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms)
70
72
err = bt_conn_le_create (& peer , & create_param , BT_LE_CONN_PARAM_DEFAULT , & conn );
71
73
TEST_ASSERT (err == 0 , "Failed starting initiator (err %d)" , err );
72
74
75
+ if (stack_load ) {
76
+ /* Claim all the buffers so that the stack cannot handle the timeout */
77
+ for (int i = 0 ; i < CONFIG_BT_BUF_CMD_TX_COUNT ; i ++ ) {
78
+ bufs [i ] = bt_hci_cmd_create (BT_HCI_LE_ADV_ENABLE , 0 );
79
+ TEST_ASSERT (bufs [i ] != NULL , "Failed to claim all command buffers" );
80
+ }
81
+ /* Hold all the buffers until after we expect the connection to timeout */
82
+ err = k_sem_take (& sem_failed_to_connect , K_MSEC (expected_conn_timeout_ms + 50 ));
83
+ TEST_ASSERT (err == - EAGAIN , "Callback ran with no buffers available" , err );
84
+ /* Release all the buffers back to the stack */
85
+ for (int i = 0 ; i < CONFIG_BT_BUF_CMD_TX_COUNT ; i ++ ) {
86
+ net_buf_unref (bufs [i ]);
87
+ }
88
+ }
89
+
73
90
err = k_sem_take (& sem_failed_to_connect , K_MSEC (2 * expected_conn_timeout_ms ));
74
91
TEST_ASSERT (err == 0 , "Failed getting connected timeout within %d s (err %d)" ,
75
92
2 * expected_conn_timeout_ms , err );
@@ -96,8 +113,9 @@ static void test_central_connect_timeout(void)
96
113
err = bt_enable (NULL );
97
114
TEST_ASSERT (err == 0 , "Can't enable Bluetooth (err %d)" , err );
98
115
99
- test_central_connect_timeout_with_timeout (0 );
100
- test_central_connect_timeout_with_timeout (1000 );
116
+ test_central_connect_timeout_with_timeout (0 , false);
117
+ test_central_connect_timeout_with_timeout (1000 , false);
118
+ test_central_connect_timeout_with_timeout (2000 , true);
101
119
102
120
TEST_PASS ("Correct timeout" );
103
121
}
0 commit comments