1 /* common.c - Common test code */
2 
3 /*
4  * Copyright (c) 2022 Nordic Semiconductor
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include "common.h"
10 #include "argparse.h"
11 
12 struct bt_conn *default_conn;
13 
14 static const struct bt_data ad[] = {
15 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
16 };
17 
18 static volatile bool is_connected;
19 static volatile bool is_encrypted;
20 
connected(struct bt_conn * conn,uint8_t conn_err)21 static void connected(struct bt_conn *conn, uint8_t conn_err)
22 {
23 	char addr[BT_ADDR_LE_STR_LEN];
24 
25 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
26 
27 	if (conn_err) {
28 		if (default_conn) {
29 			bt_conn_unref(default_conn);
30 			default_conn = NULL;
31 		}
32 
33 		FAIL("Failed to connect to %s (%u)\n", addr, conn_err);
34 		return;
35 	}
36 
37 	if (!default_conn) {
38 		default_conn = bt_conn_ref(conn);
39 	}
40 
41 	printk("Connected: %s\n", addr);
42 	is_connected = true;
43 }
44 
disconnected(struct bt_conn * conn,uint8_t reason)45 static void disconnected(struct bt_conn *conn, uint8_t reason)
46 {
47 	char addr[BT_ADDR_LE_STR_LEN];
48 
49 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
50 
51 	printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
52 
53 	bt_conn_unref(default_conn);
54 	default_conn = NULL;
55 	is_connected = false;
56 	is_encrypted = false;
57 }
58 
security_changed(struct bt_conn * conn,bt_security_t level,enum bt_security_err security_err)59 static void security_changed(struct bt_conn *conn, bt_security_t level,
60 			     enum bt_security_err security_err)
61 {
62 	if (security_err == BT_SECURITY_ERR_SUCCESS && level > BT_SECURITY_L1) {
63 		is_encrypted = true;
64 	}
65 }
66 
67 BT_CONN_CB_DEFINE(conn_callbacks) = {
68 	.connected = connected,
69 	.disconnected = disconnected,
70 	.security_changed = security_changed,
71 };
72 
device_found(const bt_addr_le_t * addr,int8_t rssi,uint8_t type,struct net_buf_simple * ad)73 static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
74 			 struct net_buf_simple *ad)
75 {
76 	int err;
77 
78 	err = bt_le_scan_stop();
79 	if (err) {
80 		FAIL("Stop LE scan failed (err %d)\n", err);
81 	}
82 
83 	err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT,
84 				&default_conn);
85 	if (err) {
86 		FAIL("Create conn failed (err %d)\n", err);
87 	}
88 
89 	printk("Device connected\n");
90 }
91 
test_init(void)92 void test_init(void)
93 {
94 	bst_ticker_set_next_tick_absolute(60e6); /* 60 seconds */
95 	bst_result = In_progress;
96 }
97 
test_tick(bs_time_t HW_device_time)98 void test_tick(bs_time_t HW_device_time)
99 {
100 	if (bst_result != Passed) {
101 		bst_result = Failed;
102 		bs_trace_error_time_line("Test eatt finished.\n");
103 	}
104 }
105 
central_setup_and_connect(void)106 void central_setup_and_connect(void)
107 {
108 	int err;
109 
110 	err = bt_enable(NULL);
111 	if (err) {
112 		FAIL("Can't enable Bluetooth (err %d)\n", err);
113 	}
114 
115 	err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, device_found);
116 	if (err) {
117 		FAIL("Scanning failed to start (err %d)\n", err);
118 	}
119 
120 	while (!is_connected) {
121 		k_sleep(K_MSEC(100));
122 	}
123 
124 	err = bt_conn_set_security(default_conn, BT_SECURITY_L2);
125 	if (err) {
126 		FAIL("Failed to start encryption procedure\n");
127 	}
128 
129 	while (!is_encrypted) {
130 		k_sleep(K_MSEC(100));
131 	}
132 }
133 
peripheral_setup_and_connect(void)134 void peripheral_setup_and_connect(void)
135 {
136 	int err;
137 
138 	err = bt_enable(NULL);
139 	if (err) {
140 		FAIL("Can't enable Bluetooth (err %d)\n", err);
141 	}
142 
143 	err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
144 	if (err) {
145 		FAIL("Advertising failed to start (err %d)\n", err);
146 	}
147 
148 	while (!is_connected) {
149 		k_sleep(K_MSEC(100));
150 	}
151 
152 	/* Wait for central to start encryption */
153 	while (!is_encrypted) {
154 		k_sleep(K_MSEC(100));
155 	}
156 }
157 
wait_for_disconnect(void)158 void wait_for_disconnect(void)
159 {
160 	while (is_connected) {
161 		k_sleep(K_MSEC(100));
162 	}
163 }
164 
disconnect(void)165 void disconnect(void)
166 {
167 	int err;
168 
169 	err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
170 	if (err) {
171 		FAIL("Disconnection failed (err %d)\n", err);
172 	}
173 
174 	while (is_connected) {
175 		k_sleep(K_MSEC(100));
176 	}
177 }
178 
179 
180 #define CHANNEL_ID 0
181 #define MSG_SIZE 1
182 
backchannel_init(void)183 void backchannel_init(void)
184 {
185 	uint device_number = get_device_nbr();
186 	uint peer_number = device_number ^ 1;
187 	uint device_numbers[] = { peer_number };
188 	uint channel_numbers[] = { CHANNEL_ID };
189 	uint *ch;
190 
191 	ch = bs_open_back_channel(device_number, device_numbers, channel_numbers,
192 				  ARRAY_SIZE(channel_numbers));
193 	if (!ch) {
194 		FAIL("Unable to open backchannel\n");
195 	}
196 }
197 
backchannel_sync_send(void)198 void backchannel_sync_send(void)
199 {
200 	uint8_t sync_msg[MSG_SIZE] = { get_device_nbr() };
201 
202 	printk("Sending sync\n");
203 	bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
204 }
205 
backchannel_sync_wait(void)206 void backchannel_sync_wait(void)
207 {
208 	uint8_t sync_msg[MSG_SIZE];
209 
210 	while (true) {
211 		if (bs_bc_is_msg_received(CHANNEL_ID) > 0) {
212 			bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
213 			if (sync_msg[0] != get_device_nbr()) {
214 				/* Received a message from another device, exit */
215 				break;
216 			}
217 		}
218 
219 		k_sleep(K_MSEC(1));
220 	}
221 
222 	printk("Sync received\n");
223 }
224