1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "bs_bt_utils.h"
8 #include <zephyr/bluetooth/addr.h>
9 #include <zephyr/bluetooth/bluetooth.h>
10 #include <zephyr/bluetooth/conn.h>
11 #include <zephyr/toolchain.h>
12 
13 #include <stdint.h>
14 #include <string.h>
15 
16 #include "babblekit/testcase.h"
17 #include "babblekit/flags.h"
18 
peripheral(void)19 void peripheral(void)
20 {
21 	bs_bt_utils_setup();
22 
23 	int id_a;
24 	int id_b;
25 	bt_addr_le_t central;
26 
27 	id_a = bt_id_create(NULL, NULL);
28 	TEST_ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)", id_a);
29 
30 	id_b = bt_id_create(NULL, NULL);
31 	TEST_ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)", id_b);
32 
33 	printk("== Bonding id a ==\n");
34 	advertise_connectable(id_a, NULL);
35 	wait_connected();
36 	/* Central should bond here, and trigger a disconnect. */
37 	wait_disconnected();
38 	central = *bt_conn_get_dst(g_conn);
39 	clear_g_conn();
40 
41 	printk("== Bonding id b ==\n");
42 	advertise_connectable(id_b, NULL);
43 	wait_connected();
44 	/* Central should bond here. */
45 	BUILD_ASSERT(IS_ENABLED(CONFIG_BT_SMP_ALLOW_UNAUTH_OVERWRITE), "");
46 	BUILD_ASSERT(IS_ENABLED(CONFIG_BT_ID_UNPAIR_MATCHING_BONDS), "");
47 	TAKE_FLAG(flag_pairing_complete);
48 	TEST_ASSERT(bt_addr_le_eq(bt_conn_get_dst(g_conn), &central),
49 		    "Test requires that central uses the same identity in both bonds.");
50 	/* Central should disconnect here. */
51 	wait_disconnected();
52 	clear_g_conn();
53 
54 	printk("== Directed connect id b ==\n");
55 	advertise_connectable(id_b, &central);
56 	wait_connected();
57 	/* Central should verify that its bond with id_b works as expected. */
58 
59 	TEST_PASS("PASS");
60 }
61