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 #include "babblekit/testcase.h"
17
18 LOG_MODULE_REGISTER(test_peripheral, LOG_LEVEL_DBG);
19
20 BUILD_ASSERT(CONFIG_BT_BONDABLE, "CONFIG_BT_BONDABLE must be enabled by default.");
21
pairing_complete_unpair(struct bt_conn * conn,bool bonded)22 static void pairing_complete_unpair(struct bt_conn *conn, bool bonded)
23 {
24 TEST_FAIL("Pairing succeed");
25 }
26
peripheral_security_changed_unpair(struct bt_conn * conn,bt_security_t level,enum bt_security_err err)27 static void peripheral_security_changed_unpair(struct bt_conn *conn,
28 bt_security_t level,
29 enum bt_security_err err)
30 {
31 /* Try to trigger fault here */
32 k_msleep(2000);
33 LOG_INF("remove pairing...");
34 bt_unpair(BT_ID_DEFAULT, bt_conn_get_dst(conn));
35 LOG_DBG("unpaired");
36 }
37
peripheral_unpair_in_sec_cb(void)38 void peripheral_unpair_in_sec_cb(void)
39 {
40 LOG_DBG("===== Peripheral (will trigger unpair in sec changed cb) =====");
41
42 int err;
43 struct bt_conn_cb peripheral_cb = {};
44 struct bt_conn_auth_info_cb peripheral_auth_info_cb = {};
45
46 /* Call `bt_unpair` in security changed callback */
47
48 peripheral_cb.security_changed = peripheral_security_changed_unpair;
49 peripheral_auth_info_cb.pairing_complete = pairing_complete_unpair;
50
51 bs_bt_utils_setup();
52
53 bt_conn_cb_register(&peripheral_cb);
54 err = bt_conn_auth_info_cb_register(&peripheral_auth_info_cb);
55 TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed.");
56
57 advertise_connectable(BT_ID_DEFAULT, NULL);
58 wait_connected();
59
60 wait_disconnected();
61
62 clear_g_conn();
63
64 TEST_PASS("PASS");
65 }
66
pairing_failed_disconnect(struct bt_conn * conn,enum bt_security_err err)67 static void pairing_failed_disconnect(struct bt_conn *conn, enum bt_security_err err)
68 {
69 TEST_FAIL("Pairing failed");
70 }
71
peripheral_security_changed_disconnect(struct bt_conn * conn,bt_security_t level,enum bt_security_err err)72 static void peripheral_security_changed_disconnect(struct bt_conn *conn,
73 bt_security_t level,
74 enum bt_security_err err)
75 {
76 /* Try to trigger fault here */
77 k_msleep(2000);
78 LOG_INF("disconnecting...");
79 bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
80 }
81
peripheral_disconnect_in_sec_cb(void)82 void peripheral_disconnect_in_sec_cb(void)
83 {
84 LOG_DBG("===== Peripheral (will trigger unpair in sec changed cb) =====");
85
86 int err;
87 struct bt_conn_cb peripheral_cb = {};
88 struct bt_conn_auth_info_cb peripheral_auth_info_cb = {};
89
90 /* Disconnect in security changed callback */
91
92 peripheral_cb.security_changed = peripheral_security_changed_disconnect;
93 peripheral_auth_info_cb.pairing_failed = pairing_failed_disconnect;
94
95 bs_bt_utils_setup();
96
97 bt_conn_cb_register(&peripheral_cb);
98 err = bt_conn_auth_info_cb_register(&peripheral_auth_info_cb);
99 TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed.");
100
101 advertise_connectable(BT_ID_DEFAULT, NULL);
102 wait_connected();
103
104 wait_disconnected();
105
106 clear_g_conn();
107
108 TEST_PASS("PASS");
109 }
110