|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "bstests.h" |
| 8 | +#include "babblekit/testcase.h" |
| 9 | +#include <zephyr/bluetooth/conn.h> |
| 10 | + |
| 11 | +static K_SEM_DEFINE(sem_connected, 0, 1); |
| 12 | + |
| 13 | +static void connected_cb(struct bt_conn *conn, uint8_t err) |
| 14 | +{ |
| 15 | + TEST_ASSERT(conn); |
| 16 | + TEST_ASSERT(err == BT_HCI_ERR_UNKNOWN_CONN_ID, "Expected connection timeout"); |
| 17 | + |
| 18 | + k_sem_give(&sem_connected); |
| 19 | + bt_conn_unref(conn); |
| 20 | +} |
| 21 | + |
| 22 | +static struct bt_conn_cb conn_cb = { |
| 23 | + .connected = connected_cb, |
| 24 | +}; |
| 25 | + |
| 26 | +static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms) |
| 27 | +{ |
| 28 | + int err; |
| 29 | + struct bt_conn *conn; |
| 30 | + |
| 31 | + /* A zero value for `bt_conn_le_create_param.timeout` shall be |
| 32 | + * interpreted as `CONFIG_BT_CREATE_CONN_TIMEOUT`. |
| 33 | + */ |
| 34 | + uint32_t expected_conn_timeout_ms = |
| 35 | + timeout_ms ? timeout_ms : CONFIG_BT_CREATE_CONN_TIMEOUT * MSEC_PER_SEC; |
| 36 | + |
| 37 | + bt_addr_le_t peer = {.a.val = {0x01}}; |
| 38 | + const struct bt_conn_le_create_param create_param = { |
| 39 | + .options = BT_CONN_LE_OPT_NONE, |
| 40 | + .interval = BT_GAP_SCAN_FAST_INTERVAL, |
| 41 | + .window = BT_GAP_SCAN_FAST_WINDOW, |
| 42 | + .interval_coded = 0, |
| 43 | + .window_coded = 0, |
| 44 | + .timeout = timeout_ms / 10, |
| 45 | + }; |
| 46 | + |
| 47 | + k_sem_reset(&sem_connected); |
| 48 | + |
| 49 | + const uint64_t conn_create_start = k_uptime_get(); |
| 50 | + |
| 51 | + err = bt_conn_le_create(&peer, &create_param, BT_LE_CONN_PARAM_DEFAULT, &conn); |
| 52 | + TEST_ASSERT(err == 0, "Failed starting initiator (err %d)", err); |
| 53 | + |
| 54 | + err = k_sem_take(&sem_connected, K_MSEC(2 * expected_conn_timeout_ms)); |
| 55 | + TEST_ASSERT(err == 0, "Failed getting connected timeout within %d s (err %d)", |
| 56 | + 2 * expected_conn_timeout_ms, err); |
| 57 | + |
| 58 | + const uint64_t conn_create_end = k_uptime_get(); |
| 59 | + |
| 60 | + const int64_t time_diff_ms = conn_create_end - conn_create_start; |
| 61 | + const int64_t diff_to_expected_ms = abs(time_diff_ms - expected_conn_timeout_ms); |
| 62 | + |
| 63 | + TEST_PRINT("Connection timeout after %d ms", time_diff_ms); |
| 64 | + TEST_ASSERT(diff_to_expected_ms < 0.1 * expected_conn_timeout_ms, |
| 65 | + "Connection timeout not within 10%% of expected timeout. " |
| 66 | + "Actual timeout: %d", time_diff_ms); |
| 67 | +} |
| 68 | + |
| 69 | +static void test_central_connect_timeout(void) |
| 70 | +{ |
| 71 | + int err; |
| 72 | + |
| 73 | + bt_conn_cb_register(&conn_cb); |
| 74 | + |
| 75 | + /* Initialize Bluetooth */ |
| 76 | + err = bt_enable(NULL); |
| 77 | + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); |
| 78 | + |
| 79 | + test_central_connect_timeout_with_timeout(0); |
| 80 | + test_central_connect_timeout_with_timeout(1000); |
| 81 | + |
| 82 | + TEST_PASS("Correct timeout"); |
| 83 | +} |
| 84 | + |
| 85 | +static const struct bst_test_instance test_def[] = { |
| 86 | + { |
| 87 | + .test_id = "central_connect_timeout", |
| 88 | + .test_descr = "Verifies that the default connection timeout is used correctly", |
| 89 | + .test_tick_f = bst_tick, |
| 90 | + .test_main_f = test_central_connect_timeout |
| 91 | + }, |
| 92 | + BSTEST_END_MARKER |
| 93 | +}; |
| 94 | + |
| 95 | +static struct bst_test_list *test_central_install(struct bst_test_list *tests) |
| 96 | +{ |
| 97 | + return bst_add_tests(tests, test_def); |
| 98 | +} |
| 99 | + |
| 100 | +bst_test_install_t test_installers[] = { |
| 101 | + test_central_install, |
| 102 | + NULL |
| 103 | +}; |
| 104 | + |
| 105 | +int main(void) |
| 106 | +{ |
| 107 | + bst_main(); |
| 108 | + return 0; |
| 109 | +} |
0 commit comments