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/bluetooth.h>
11 #include <zephyr/bluetooth/conn.h>
12 #include <zephyr/toolchain.h>
13 
14 #include <stdint.h>
15 #include <string.h>
16 
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(peripheral, LOG_LEVEL_INF);
19 
verify_equal_address(struct bt_conn * conn_a,struct bt_conn * conn_b)20 static void verify_equal_address(struct bt_conn *conn_a, struct bt_conn *conn_b)
21 {
22 	int err;
23 	struct bt_conn_info info_a;
24 	struct bt_conn_info info_b;
25 
26 	err = bt_conn_get_info(conn_a, &info_a);
27 	ASSERT(!err, "Unexpected info_a result.");
28 
29 	err = bt_conn_get_info(conn_b, &info_b);
30 	ASSERT(!err, "Unexpected info_b result.");
31 
32 	ASSERT(bt_addr_le_eq(info_a.le.dst, info_b.le.dst),
33 	       "Conn A address is not equal with the conn B address");
34 }
35 
peripheral(void)36 void peripheral(void)
37 {
38 	bs_bt_utils_setup();
39 
40 	int id_a;
41 	int id_b;
42 
43 	struct bt_conn *conn_a;
44 	struct bt_conn *conn_b;
45 
46 	/* Create two identities that will simultaneously connect with the same central peer. */
47 	id_a = bt_id_create(NULL, NULL);
48 	ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)\n", id_a);
49 
50 	id_b = bt_id_create(NULL, NULL);
51 	ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)\n", id_b);
52 
53 	/* Connect with the first identity. */
54 	LOG_INF("adv");
55 	advertise_connectable(id_a);
56 	LOG_INF("wait conn");
57 	wait_connected(&conn_a);
58 
59 	/* Send battery notification on the first connection. */
60 	wait_bas_ccc_subscription();
61 	bas_notify(conn_a);
62 
63 	/* Connect with the second identity. */
64 	LOG_INF("adv id 2");
65 	advertise_connectable(id_b);
66 	wait_connected(&conn_b);
67 
68 	/* Wait for the pairing completed callback on the second identity. */
69 	wait_pairing_completed();
70 
71 	/* Both connections should relate to the identity address of the same Central peer. */
72 	verify_equal_address(conn_a, conn_b);
73 
74 	/* Send notification after identity address resolution to the first connection object. */
75 	bas_notify(conn_a);
76 
77 	/* Disconnect the first identity. */
78 	wait_disconnected();
79 	clear_conn(conn_a);
80 
81 	/* Disconnect the second identity. */
82 	wait_disconnected();
83 	clear_conn(conn_b);
84 
85 	PASS("PASS\n");
86 }
87 
88 static const struct bst_test_instance test_to_add[] = {
89 	{
90 		.test_id = "peripheral",
91 		.test_pre_init_f = test_init,
92 		.test_tick_f = test_tick,
93 		.test_main_f = peripheral,
94 	},
95 	BSTEST_END_MARKER,
96 };
97 
install(struct bst_test_list * tests)98 static struct bst_test_list *install(struct bst_test_list *tests)
99 {
100 	return bst_add_tests(tests, test_to_add);
101 };
102 
103 bst_test_install_t test_installers[] = {install, NULL};
104 
main(void)105 int main(void)
106 {
107 	bst_main();
108 	return 0;
109 }
110