1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <string.h>
9 
10 #include <zephyr/logging/log.h>
11 #include <zephyr/bluetooth/addr.h>
12 #include <zephyr/bluetooth/conn.h>
13 #include <zephyr/bluetooth/bluetooth.h>
14 
15 #include "bs_bt_utils.h"
16 
17 LOG_MODULE_REGISTER(test_peripheral, LOG_LEVEL_DBG);
18 
19 BUILD_ASSERT(CONFIG_BT_BONDABLE, "CONFIG_BT_BONDABLE must be enabled by default.");
20 
pairing_complete_unpair(struct bt_conn * conn,bool bonded)21 static void pairing_complete_unpair(struct bt_conn *conn, bool bonded)
22 {
23 	FAIL("Pairing succeed\n");
24 }
25 
peripheral_security_changed_unpair(struct bt_conn * conn,bt_security_t level,enum bt_security_err err)26 static void peripheral_security_changed_unpair(struct bt_conn *conn,
27 					bt_security_t level,
28 					enum bt_security_err err)
29 {
30 	/* Try to trigger fault here */
31 	k_msleep(2000);
32 	LOG_INF("remove pairing...");
33 	bt_unpair(BT_ID_DEFAULT, bt_conn_get_dst(conn));
34 	LOG_DBG("unpaired");
35 }
36 
peripheral_unpair_in_sec_cb(void)37 void peripheral_unpair_in_sec_cb(void)
38 {
39 	LOG_DBG("===== Peripheral (will trigger unpair in sec changed cb) =====");
40 
41 	int err;
42 	struct bt_conn_cb peripheral_cb = {};
43 	struct bt_conn_auth_info_cb peripheral_auth_info_cb = {};
44 
45 	/* Call `bt_unpair` in security changed callback */
46 
47 	peripheral_cb.security_changed = peripheral_security_changed_unpair;
48 	peripheral_auth_info_cb.pairing_complete = pairing_complete_unpair;
49 
50 	bs_bt_utils_setup();
51 
52 	bt_conn_cb_register(&peripheral_cb);
53 	err = bt_conn_auth_info_cb_register(&peripheral_auth_info_cb);
54 	ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n");
55 
56 	advertise_connectable(BT_ID_DEFAULT, NULL);
57 	wait_connected();
58 
59 	wait_disconnected();
60 
61 	clear_g_conn();
62 
63 	PASS("PASS\n");
64 }
65 
pairing_failed_disconnect(struct bt_conn * conn,enum bt_security_err err)66 static void pairing_failed_disconnect(struct bt_conn *conn, enum bt_security_err err)
67 {
68 	FAIL("Pairing failed\n");
69 }
70 
peripheral_security_changed_disconnect(struct bt_conn * conn,bt_security_t level,enum bt_security_err err)71 static void peripheral_security_changed_disconnect(struct bt_conn *conn,
72 						   bt_security_t level,
73 						   enum bt_security_err err)
74 {
75 	/* Try to trigger fault here */
76 	k_msleep(2000);
77 	LOG_INF("disconnecting...");
78 	bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
79 }
80 
peripheral_disconnect_in_sec_cb(void)81 void peripheral_disconnect_in_sec_cb(void)
82 {
83 	LOG_DBG("===== Peripheral (will trigger unpair in sec changed cb) =====");
84 
85 	int err;
86 	struct bt_conn_cb peripheral_cb = {};
87 	struct bt_conn_auth_info_cb peripheral_auth_info_cb = {};
88 
89 	/* Disconnect in security changed callback */
90 
91 	peripheral_cb.security_changed = peripheral_security_changed_disconnect;
92 	peripheral_auth_info_cb.pairing_failed = pairing_failed_disconnect;
93 
94 	bs_bt_utils_setup();
95 
96 	bt_conn_cb_register(&peripheral_cb);
97 	err = bt_conn_auth_info_cb_register(&peripheral_auth_info_cb);
98 	ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n");
99 
100 	advertise_connectable(BT_ID_DEFAULT, NULL);
101 	wait_connected();
102 
103 	wait_disconnected();
104 
105 	clear_g_conn();
106 
107 	PASS("PASS\n");
108 }
109