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 "babblekit/testcase.h"
10 #include "babblekit/flags.h"
11 #include "babblekit/sync.h"
12 #include "common.h"
13 
14 #include <zephyr/bluetooth/gatt.h>
15 
16 #define NEW_MTU 100
17 
18 DEFINE_FLAG_STATIC(flag_reconfigured);
19 
att_mtu_updated(struct bt_conn * conn,uint16_t tx,uint16_t rx)20 void att_mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
21 {
22 	printk("MTU Updated: tx %d, rx %d\n", tx, rx);
23 
24 	if (rx == NEW_MTU || tx == NEW_MTU) {
25 		SET_FLAG(flag_reconfigured);
26 	}
27 }
28 
29 static struct bt_gatt_cb cb = {
30 	.att_mtu_updated = att_mtu_updated,
31 };
32 
test_peripheral_main(void)33 static void test_peripheral_main(void)
34 {
35 	TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel");
36 
37 	peripheral_setup_and_connect();
38 
39 	bt_gatt_cb_register(&cb);
40 
41 	/* Wait until all channels are established on both sides */
42 	while (bt_eatt_count(default_conn) < CONFIG_BT_EATT_MAX) {
43 		k_sleep(K_MSEC(10));
44 	}
45 	bk_sync_send();
46 	bk_sync_wait();
47 
48 	WAIT_FOR_FLAG(flag_reconfigured);
49 	bk_sync_send();
50 
51 	/* Wait for the reconfigured flag on the other end */
52 	bk_sync_wait();
53 
54 	disconnect();
55 
56 	TEST_PASS("EATT Peripheral tests Passed");
57 }
58 
test_central_main(void)59 static void test_central_main(void)
60 {
61 	int err;
62 
63 	TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel");
64 
65 	central_setup_and_connect();
66 
67 	bt_gatt_cb_register(&cb);
68 
69 	while (bt_eatt_count(default_conn) < CONFIG_BT_EATT_MAX) {
70 		k_sleep(K_MSEC(10));
71 	}
72 
73 	err = bt_eatt_reconfigure(default_conn, NEW_MTU);
74 	if (err < 0) {
75 		TEST_FAIL("Reconfigure failed (%d)", err);
76 	}
77 	bk_sync_send();
78 	bk_sync_wait();
79 
80 	WAIT_FOR_FLAG(flag_reconfigured);
81 	bk_sync_send();
82 
83 	/* Wait for the reconfigured flag on the other end */
84 	bk_sync_wait();
85 
86 	wait_for_disconnect();
87 
88 	TEST_PASS("EATT Central tests Passed");
89 }
90 
91 static const struct bst_test_instance test_def[] = {
92 	{
93 		.test_id = "peripheral_reconfigure",
94 		.test_descr = "Peripheral reconfigure",
95 		.test_main_f = test_peripheral_main,
96 	},
97 	{
98 		.test_id = "central_reconfigure",
99 		.test_descr = "Central reconfigure",
100 		.test_main_f = test_central_main,
101 	},
102 	BSTEST_END_MARKER,
103 };
104 
test_main_reconfigure_install(struct bst_test_list * tests)105 struct bst_test_list *test_main_reconfigure_install(struct bst_test_list *tests)
106 {
107 	return bst_add_tests(tests, test_def);
108 }
109