1 /* main_reconfigure.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2022 Nordic Semiconductor
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include "common.h"
10 
11 #include <zephyr/bluetooth/gatt.h>
12 
13 #define NEW_MTU 100
14 
15 CREATE_FLAG(flag_reconfigured);
16 
att_mtu_updated(struct bt_conn * conn,uint16_t tx,uint16_t rx)17 void att_mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
18 {
19 	printk("MTU Updated: tx %d, rx %d\n", tx, rx);
20 
21 	if (rx == NEW_MTU || tx == NEW_MTU) {
22 		SET_FLAG(flag_reconfigured);
23 	}
24 }
25 
26 static struct bt_gatt_cb cb = {
27 	.att_mtu_updated = att_mtu_updated,
28 };
29 
test_peripheral_main(void)30 static void test_peripheral_main(void)
31 {
32 	backchannel_init();
33 
34 	peripheral_setup_and_connect();
35 
36 	bt_gatt_cb_register(&cb);
37 
38 	/* Wait until all channels are established on both sides */
39 	while (bt_eatt_count(default_conn) < CONFIG_BT_EATT_MAX) {
40 		k_sleep(K_MSEC(10));
41 	}
42 	backchannel_sync_send();
43 	backchannel_sync_wait();
44 
45 	WAIT_FOR_FLAG(flag_reconfigured);
46 	backchannel_sync_send();
47 
48 	/* Wait for the reconfigured flag on the other end */
49 	backchannel_sync_wait();
50 
51 	disconnect();
52 
53 	PASS("EATT Peripheral tests Passed\n");
54 }
55 
test_central_main(void)56 static void test_central_main(void)
57 {
58 	int err;
59 
60 	backchannel_init();
61 
62 	central_setup_and_connect();
63 
64 	bt_gatt_cb_register(&cb);
65 
66 	while (bt_eatt_count(default_conn) < CONFIG_BT_EATT_MAX) {
67 		k_sleep(K_MSEC(10));
68 	}
69 
70 	err = bt_eatt_reconfigure(default_conn, NEW_MTU);
71 	if (err < 0) {
72 		FAIL("Reconfigure failed (%d)\n", err);
73 	}
74 	backchannel_sync_send();
75 	backchannel_sync_wait();
76 
77 	WAIT_FOR_FLAG(flag_reconfigured);
78 	backchannel_sync_send();
79 
80 	/* Wait for the reconfigured flag on the other end */
81 	backchannel_sync_wait();
82 
83 	wait_for_disconnect();
84 
85 	PASS("EATT Central tests Passed\n");
86 }
87 
88 static const struct bst_test_instance test_def[] = {
89 	{
90 		.test_id = "peripheral_reconfigure",
91 		.test_descr = "Peripheral reconfigure",
92 		.test_pre_init_f = test_init,
93 		.test_tick_f = test_tick,
94 		.test_main_f = test_peripheral_main,
95 	},
96 	{
97 		.test_id = "central_reconfigure",
98 		.test_descr = "Central reconfigure",
99 		.test_pre_init_f = test_init,
100 		.test_tick_f = test_tick,
101 		.test_main_f = test_central_main,
102 	},
103 	BSTEST_END_MARKER,
104 };
105 
test_main_reconfigure_install(struct bst_test_list * tests)106 struct bst_test_list *test_main_reconfigure_install(struct bst_test_list *tests)
107 {
108 	return bst_add_tests(tests, test_def);
109 }
110