1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <string.h>
7
8 #include <zephyr/bluetooth/conn.h>
9 #include <zephyr/bluetooth/addr.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/sys/util.h>
12
13 #include "common.h"
14
15 extern enum bst_result_t bst_result;
16 struct bt_conn *default_conn;
17 atomic_t flag_connected;
18 atomic_t flag_conn_updated;
19
connected(struct bt_conn * conn,uint8_t err)20 static void connected(struct bt_conn *conn, uint8_t err)
21 {
22 char addr[BT_ADDR_LE_STR_LEN];
23
24 (void)bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
25
26 if (default_conn == NULL) {
27 default_conn = bt_conn_ref(conn);
28 }
29
30 if (err != 0) {
31 bt_conn_unref(default_conn);
32 default_conn = NULL;
33
34 FAIL("Failed to connect to %s (0x%02x)\n", addr, err);
35
36 return;
37 }
38
39 printk("Connected to %s\n", addr);
40 SET_FLAG(flag_connected);
41 }
42
disconnected(struct bt_conn * conn,uint8_t reason)43 static void disconnected(struct bt_conn *conn, uint8_t reason)
44 {
45 char addr[BT_ADDR_LE_STR_LEN];
46
47 if (conn != default_conn) {
48 return;
49 }
50
51 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
52
53 printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
54
55 bt_conn_unref(default_conn);
56 default_conn = NULL;
57 UNSET_FLAG(flag_connected);
58 UNSET_FLAG(flag_conn_updated);
59 }
60
conn_param_updated_cb(struct bt_conn * conn,uint16_t interval,uint16_t latency,uint16_t timeout)61 static void conn_param_updated_cb(struct bt_conn *conn, uint16_t interval, uint16_t latency,
62 uint16_t timeout)
63 {
64 printk("Connection parameter updated: %p 0x%04X (%u us), 0x%04X, 0x%04X\n", conn, interval,
65 BT_CONN_INTERVAL_TO_US(interval), latency, timeout);
66
67 SET_FLAG(flag_conn_updated);
68 }
69
70 BT_CONN_CB_DEFINE(conn_callbacks) = {
71 .connected = connected,
72 .disconnected = disconnected,
73 .le_param_updated = conn_param_updated_cb,
74 };
75
test_init(void)76 void test_init(void)
77 {
78 bst_result = In_progress;
79 bst_ticker_set_next_tick_absolute(WAIT_TIME);
80 }
81
test_tick(bs_time_t HW_device_time)82 void test_tick(bs_time_t HW_device_time)
83 {
84 if (bst_result != Passed) {
85 FAIL("Test failed (not passed after %i us)\n", WAIT_TIME);
86 }
87 }
88