1 /*
2 * Copyright (c) 2019 Bose Corporation
3 * Copyright (c) 2020-2021 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "common.h"
9
10 extern enum bst_result_t bst_result;
11 struct bt_conn *default_conn;
12 atomic_t flag_connected;
13 atomic_t flag_conn_updated;
14
15 const struct bt_data ad[AD_SIZE] = {
16 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR))
17 };
18
device_found(const struct bt_le_scan_recv_info * info,struct net_buf_simple * ad)19 static void device_found(const struct bt_le_scan_recv_info *info, struct net_buf_simple *ad)
20 {
21 char addr_str[BT_ADDR_LE_STR_LEN];
22 int err;
23
24 if (default_conn) {
25 return;
26 }
27
28 /* We're only interested in connectable events */
29 if ((info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) == 0) {
30 return;
31 }
32
33 bt_addr_le_to_str(info->addr, addr_str, sizeof(addr_str));
34 printk("Device found: %s (RSSI %d)\n", addr_str, info->rssi);
35
36 /* connect only to devices in close proximity */
37 if (info->rssi < -70) {
38 FAIL("RSSI too low");
39 return;
40 }
41
42 printk("Stopping scan\n");
43 if (bt_le_scan_stop()) {
44 FAIL("Could not stop scan");
45 return;
46 }
47
48 err = bt_conn_le_create(info->addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT,
49 &default_conn);
50 if (err) {
51 FAIL("Could not connect to peer: %d", err);
52 }
53 }
54
55 struct bt_le_scan_cb common_scan_cb = {
56 .recv = device_found,
57 };
58
connected(struct bt_conn * conn,uint8_t err)59 static void connected(struct bt_conn *conn, uint8_t err)
60 {
61 char addr[BT_ADDR_LE_STR_LEN];
62
63 (void)bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
64
65 if (default_conn == NULL) {
66 default_conn = bt_conn_ref(conn);
67 }
68
69 if (err != 0) {
70 bt_conn_unref(default_conn);
71 default_conn = NULL;
72
73 FAIL("Failed to connect to %s (0x%02x)\n", addr, err);
74 return;
75 }
76
77 printk("Connected to %s\n", addr);
78 SET_FLAG(flag_connected);
79 }
80
disconnected(struct bt_conn * conn,uint8_t reason)81 void disconnected(struct bt_conn *conn, uint8_t reason)
82 {
83 char addr[BT_ADDR_LE_STR_LEN];
84
85 if (conn != default_conn) {
86 return;
87 }
88
89 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
90
91 printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
92
93 bt_conn_unref(default_conn);
94 default_conn = NULL;
95 UNSET_FLAG(flag_connected);
96 UNSET_FLAG(flag_conn_updated);
97 }
98
conn_param_updated_cb(struct bt_conn * conn,uint16_t interval,uint16_t latency,uint16_t timeout)99 static void conn_param_updated_cb(struct bt_conn *conn, uint16_t interval, uint16_t latency,
100 uint16_t timeout)
101 {
102 printk("Connection parameter updated: %p 0x%04X (%u us), 0x%04X, 0x%04X\n", conn, interval,
103 BT_CONN_INTERVAL_TO_US(interval), latency, timeout);
104
105 SET_FLAG(flag_conn_updated);
106 }
107
108 BT_CONN_CB_DEFINE(conn_callbacks) = {
109 .connected = connected,
110 .disconnected = disconnected,
111 .le_param_updated = conn_param_updated_cb,
112 };
113
test_tick(bs_time_t HW_device_time)114 void test_tick(bs_time_t HW_device_time)
115 {
116 if (bst_result != Passed) {
117 FAIL("test failed (not passed after %i seconds)\n", WAIT_SECONDS);
118 }
119 }
120
test_init(void)121 void test_init(void)
122 {
123 bst_ticker_set_next_tick_absolute(WAIT_TIME);
124 bst_result = In_progress;
125 }
126