1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "bs_bt_utils.h"
8
9 #include <stdint.h>
10
11 #include <zephyr/bluetooth/addr.h>
12 #include <zephyr/bluetooth/conn.h>
13 #include <zephyr/bluetooth/bluetooth.h>
14
15 #include <babblekit/testcase.h>
16 #include <testlib/conn.h>
17 #include <testlib/scan.h>
18
start_scanning(void)19 void start_scanning(void)
20 {
21 int err;
22 struct bt_le_scan_param param;
23
24 /* Enable bluetooth */
25 err = bt_enable(NULL);
26 if (err) {
27 FAIL("Failed to enable bluetooth (err %d\n)", err);
28 }
29
30 /* Start active scanning */
31 param.type = BT_LE_SCAN_TYPE_ACTIVE;
32 param.options = BT_LE_SCAN_OPT_FILTER_DUPLICATE;
33 param.interval = BT_GAP_SCAN_FAST_INTERVAL;
34 param.window = BT_GAP_SCAN_FAST_WINDOW;
35 param.timeout = 0;
36 param.interval_coded = 0;
37 param.window_coded = 0;
38
39 err = bt_le_scan_start(¶m, NULL);
40 if (err) {
41 FAIL("Failed to start scanning");
42 }
43 }
44
dut_procedure(void)45 void dut_procedure(void)
46 {
47 start_scanning();
48
49 /* Nothing to do */
50
51 PASS("PASS\n");
52 }
53
dut_procedure_connect_short_rpa_timeout(void)54 void dut_procedure_connect_short_rpa_timeout(void)
55 {
56 backchannel_init(1);
57
58 const uint16_t rpa_timeout_s = 1;
59
60 int err;
61 bt_addr_le_t peer = {};
62 struct bt_conn *conn = NULL;
63
64 /* Enable bluetooth */
65 err = bt_enable(NULL);
66 TEST_ASSERT(!err, "Failed to enable bluetooth (err %d)");
67
68 /* Central to use a short RPA timeout */
69 err = bt_le_set_rpa_timeout(rpa_timeout_s);
70 TEST_ASSERT(!err, "Failed to set RPA timeout (err %d)", err);
71
72 err = bt_testlib_scan_find_name(&peer, CONFIG_BT_DEVICE_NAME);
73 TEST_ASSERT(!err, "Failed to start scan (err %d)", err);
74
75 /* Indicate to the peer device that we have found the advertiser. */
76 backchannel_sync_send();
77
78 /* Create a connection using that address */
79 err = bt_testlib_connect(&peer, &conn);
80 TEST_ASSERT(!err, "Failed to initiate connection (err %d)", err);
81
82 PASS("PASS\n");
83 }
84
dut_procedure_connect_timeout(void)85 void dut_procedure_connect_timeout(void)
86 {
87 const uint16_t rpa_timeout_s = 1;
88
89 int err;
90 bt_addr_le_t peer = {.type = BT_ADDR_LE_RANDOM, .a = {.val = {1, 2, 3, 4, 5, 6}}};
91 struct bt_conn *conn = NULL;
92
93 /* Enable bluetooth */
94 err = bt_enable(NULL);
95 TEST_ASSERT(!err, "Failed to enable bluetooth (err %d)", err);
96
97 /* Central to use a short RPA timeout */
98 err = bt_le_set_rpa_timeout(rpa_timeout_s);
99 TEST_ASSERT(!err, "Failed to set RPA timeout (err %d)", err);
100
101 int64_t old_time = k_uptime_get();
102
103 /* Create a connection using that address */
104 err = bt_testlib_connect(&peer, &conn);
105 TEST_ASSERT(err == BT_HCI_ERR_UNKNOWN_CONN_ID,
106 "Expected connection establishment to time out (err %d)", err);
107
108 int64_t new_time = k_uptime_get();
109 int64_t time_diff_ms = new_time - old_time;
110 int64_t expected_conn_timeout_ms = CONFIG_BT_CREATE_CONN_TIMEOUT * 1000;
111 int64_t diff_to_expected_ms = abs(time_diff_ms - expected_conn_timeout_ms);
112
113 printk("Connection creation timed out after %d ms\n", time_diff_ms);
114 TEST_ASSERT(diff_to_expected_ms < 0.1 * expected_conn_timeout_ms,
115 "Connection timeout not within 10 \%% of expected timeout");
116
117 PASS("PASS\n");
118 }
119