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 #include "utils.h"
9 #include <zephyr/bluetooth/addr.h>
10 #include <zephyr/bluetooth/conn.h>
11 
12 #include <stdint.h>
13 
14 #include <zephyr/bluetooth/bluetooth.h>
15 
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(central, LOG_LEVEL_INF);
18 
central(void)19 void central(void)
20 {
21 	bs_bt_utils_setup();
22 
23 	struct bt_conn *conn_a;
24 	struct bt_conn *conn_b;
25 
26 	/* Connect to the first identity of the peripheral. */
27 	LOG_INF("conn first");
28 	scan_connect_to_first_result();
29 	LOG_INF("wait conn");
30 	wait_connected(&conn_a);
31 
32 	/* Subscribe to battery notifications and wait on the first one. */
33 	LOG_INF("subscribe first");
34 	bas_subscribe(conn_a);
35 	wait_bas_notification();
36 
37 	/* Connect to the second identity of the peripheral. */
38 	LOG_INF("scan 2nd id");
39 	scan_connect_to_first_result();
40 	wait_connected(&conn_b);
41 
42 	/* Establish security with the second identity and resolve identity address. */
43 	LOG_INF("set sec");
44 	set_security(conn_b, BT_SECURITY_L2);
45 	wait_pairing_completed();
46 
47 	/* Wait for notification from the first connection after identity address resolution. */
48 	LOG_INF("wait notif");
49 	wait_bas_notification();
50 
51 	/* Disconnect the first identity of the peripheral. */
52 	LOG_INF("discon id first");
53 	disconnect(conn_a);
54 	wait_disconnected();
55 	clear_conn(conn_a);
56 
57 	/* Disconnect the second identity of the peripheral. */
58 	LOG_INF("discon id second");
59 	disconnect(conn_b);
60 	wait_disconnected();
61 	clear_conn(conn_b);
62 
63 	PASS("PASS\n");
64 }
65 
66 static const struct bst_test_instance test_to_add[] = {
67 	{
68 		.test_id = "central",
69 		.test_pre_init_f = test_init,
70 		.test_tick_f = test_tick,
71 		.test_main_f = central,
72 	},
73 	BSTEST_END_MARKER,
74 };
75 
install(struct bst_test_list * tests)76 static struct bst_test_list *install(struct bst_test_list *tests)
77 {
78 	return bst_add_tests(tests, test_to_add);
79 };
80 
81 bst_test_install_t test_installers[] = {install, NULL};
82 
main(void)83 int main(void)
84 {
85 	bst_main();
86 	return 0;
87 }
88