|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * EATT notification reliability test: |
| 7 | + * A central acting as a GATT client scans and connects |
| 8 | + * to a peripheral acting as a GATT server. |
| 9 | + * The GATT client will then attempt to connect a number of CONFIG_BT_EATT_MAX bearers |
| 10 | + * over EATT, send notifications, disconnect all bearers and reconnect EATT_BEARERS_TEST |
| 11 | + * and send start a transaction with a request, then send a lot of notifications |
| 12 | + * before the response is received. |
| 13 | + * The test might be expanded by checking that all the notifications all transmitted |
| 14 | + * on EATT channels. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <bluetooth/bluetooth.h> |
| 18 | +#include <bluetooth/gatt.h> |
| 19 | +#include <bluetooth/conn.h> |
| 20 | +#include <bluetooth/att.h> |
| 21 | + |
| 22 | +#include "common.h" |
| 23 | + |
| 24 | +CREATE_FLAG(flag_is_connected); |
| 25 | +CREATE_FLAG(flag_discover_complete); |
| 26 | + |
| 27 | +static struct bt_conn *g_conn; |
| 28 | +static const struct bt_gatt_attr *local_attr; |
| 29 | +static struct bt_uuid *test_svc_uuid = TEST_SERVICE_UUID; |
| 30 | + |
| 31 | +#define NUM_NOTIF 100 |
| 32 | +#define SAMPLE_DATA 1 |
| 33 | +#define EATT_BEARERS_TEST 1 |
| 34 | + |
| 35 | +volatile int num_eatt_channels; |
| 36 | + |
| 37 | +static void connected(struct bt_conn *conn, uint8_t err) |
| 38 | +{ |
| 39 | + char addr[BT_ADDR_LE_STR_LEN]; |
| 40 | + |
| 41 | + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); |
| 42 | + |
| 43 | + if (err != 0) { |
| 44 | + FAIL("Failed to connect to %s (%u)\n", addr, err); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + printk("Connected to %s\n", addr); |
| 49 | + SET_FLAG(flag_is_connected); |
| 50 | +} |
| 51 | + |
| 52 | +static void disconnected(struct bt_conn *conn, uint8_t reason) |
| 53 | +{ |
| 54 | + char addr[BT_ADDR_LE_STR_LEN]; |
| 55 | + |
| 56 | + if (conn != g_conn) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); |
| 61 | + |
| 62 | + printk("Disconnected: %s (reason 0x%02x)\n", addr, reason); |
| 63 | + |
| 64 | + bt_conn_unref(g_conn); |
| 65 | + |
| 66 | + g_conn = NULL; |
| 67 | + UNSET_FLAG(flag_is_connected); |
| 68 | +} |
| 69 | + |
| 70 | +BT_CONN_CB_DEFINE(conn_callbacks) = { |
| 71 | + .connected = connected, |
| 72 | + .disconnected = disconnected, |
| 73 | +}; |
| 74 | + |
| 75 | +void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, |
| 76 | + struct net_buf_simple *ad) |
| 77 | +{ |
| 78 | + char addr_str[BT_ADDR_LE_STR_LEN]; |
| 79 | + int err; |
| 80 | + |
| 81 | + if (g_conn != NULL) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + /* We're only interested in connectable events */ |
| 86 | + if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); |
| 91 | + printk("Device found: %s (RSSI %d)\n", addr_str, rssi); |
| 92 | + |
| 93 | + printk("Stopping scan\n"); |
| 94 | + err = bt_le_scan_stop(); |
| 95 | + if (err != 0) { |
| 96 | + FAIL("Could not stop scan: %d"); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, |
| 101 | + BT_LE_CONN_PARAM_DEFAULT, &g_conn); |
| 102 | + if (err != 0) { |
| 103 | + FAIL("Could not connect to peer: %d", err); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void send_notification(void) |
| 108 | +{ |
| 109 | + const uint8_t sample_dat = SAMPLE_DATA; |
| 110 | + int err; |
| 111 | + |
| 112 | + do { |
| 113 | + err = bt_gatt_notify(g_conn, local_attr, &sample_dat, sizeof(sample_dat)); |
| 114 | + if (!err) { |
| 115 | + return; |
| 116 | + } else if (err != -ENOMEM) { |
| 117 | + printk("GATT notify failed (err %d)\n", err); |
| 118 | + return; |
| 119 | + } |
| 120 | + k_sleep(K_TICKS(1)); |
| 121 | + } while (err == -ENOMEM); |
| 122 | +} |
| 123 | + |
| 124 | +static uint8_t discover_func(struct bt_conn *conn, |
| 125 | + const struct bt_gatt_attr *attr, |
| 126 | + struct bt_gatt_discover_params *params) |
| 127 | +{ |
| 128 | + SET_FLAG(flag_discover_complete); |
| 129 | + printk("Discover complete\n"); |
| 130 | + |
| 131 | + return BT_GATT_ITER_STOP; |
| 132 | +} |
| 133 | + |
| 134 | +static void gatt_discover(void) |
| 135 | +{ |
| 136 | + static struct bt_gatt_discover_params discover_params; |
| 137 | + int err; |
| 138 | + |
| 139 | + printk("Discovering services and characteristics\n"); |
| 140 | + |
| 141 | + discover_params.uuid = test_svc_uuid; |
| 142 | + discover_params.func = discover_func; |
| 143 | + discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE; |
| 144 | + discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE; |
| 145 | + discover_params.type = BT_GATT_DISCOVER_PRIMARY; |
| 146 | + |
| 147 | + err = bt_gatt_discover(g_conn, &discover_params); |
| 148 | + if (err != 0) { |
| 149 | + FAIL("Discover failed(err %d)\n", err); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +BT_GATT_SERVICE_DEFINE(g_svc, |
| 154 | + BT_GATT_PRIMARY_SERVICE(TEST_SERVICE_UUID), |
| 155 | + BT_GATT_CHARACTERISTIC(TEST_CHRC_UUID, BT_GATT_CHRC_NOTIFY, |
| 156 | + 0x00, NULL, NULL, NULL)); |
| 157 | + |
| 158 | +static void test_main(void) |
| 159 | +{ |
| 160 | + int err; |
| 161 | + |
| 162 | + device_sync_init(PERIPHERAL_ID); |
| 163 | + |
| 164 | + err = bt_enable(NULL); |
| 165 | + if (err != 0) { |
| 166 | + FAIL("Bluetooth enable failed (err %d)\n", err); |
| 167 | + } |
| 168 | + |
| 169 | + err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); |
| 170 | + if (err != 0) { |
| 171 | + FAIL("Scanning failed to start (err %d)\n", err); |
| 172 | + } |
| 173 | + |
| 174 | + printk("Scanning successfully started\n"); |
| 175 | + |
| 176 | + WAIT_FOR_FLAG(flag_is_connected); |
| 177 | + |
| 178 | + err = bt_eatt_connect(g_conn, CONFIG_BT_EATT_MAX); |
| 179 | + if (err) { |
| 180 | + FAIL("Sending credit based connection request failed (err %d)\n", err); |
| 181 | + } |
| 182 | + |
| 183 | + /* Wait for the channels to be connected */ |
| 184 | + while (bt_eatt_count(g_conn) < CONFIG_BT_EATT_MAX) { |
| 185 | + k_sleep(K_TICKS(1)); |
| 186 | + } |
| 187 | + |
| 188 | + local_attr = &g_svc.attrs[1]; |
| 189 | + printk("############# Notification test\n"); |
| 190 | + for (int idx = 0; idx < NUM_NOTIF; idx++) { |
| 191 | + printk("Notification %d\n", idx); |
| 192 | + send_notification(); |
| 193 | + } |
| 194 | + |
| 195 | + printk("############# Disconnect and reconnect\n"); |
| 196 | + for (int idx = 0; idx < CONFIG_BT_EATT_MAX; idx++) { |
| 197 | + bt_eatt_disconnect_one(g_conn); |
| 198 | + while (bt_eatt_count(g_conn) != (CONFIG_BT_EATT_MAX - idx)) { |
| 199 | + k_sleep(K_TICKS(1)); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + printk("Connecting %d bearers\n", EATT_BEARERS_TEST); |
| 204 | + err = bt_eatt_connect(g_conn, EATT_BEARERS_TEST); |
| 205 | + if (err) { |
| 206 | + FAIL("Sending credit based connection request failed (err %d)\n", err); |
| 207 | + } |
| 208 | + |
| 209 | + /* Wait for the channels to be connected */ |
| 210 | + while (bt_eatt_count(g_conn) < EATT_BEARERS_TEST) { |
| 211 | + k_sleep(K_TICKS(1)); |
| 212 | + } |
| 213 | + |
| 214 | + printk("############# Send notifications during discovery request\n"); |
| 215 | + gatt_discover(); |
| 216 | + while (!TEST_FLAG(flag_discover_complete)) { |
| 217 | + printk("Notifying...\n"); |
| 218 | + send_notification(); |
| 219 | + } |
| 220 | + |
| 221 | + printk("Send sync to contine\n"); |
| 222 | + device_sync_send(); |
| 223 | + |
| 224 | + PASS("Client Passed\n"); |
| 225 | +} |
| 226 | + |
| 227 | +static const struct bst_test_instance test_vcs[] = { |
| 228 | + { |
| 229 | + .test_id = "client", |
| 230 | + .test_post_init_f = test_init, |
| 231 | + .test_tick_f = test_tick, |
| 232 | + .test_main_f = test_main |
| 233 | + }, |
| 234 | + BSTEST_END_MARKER |
| 235 | +}; |
| 236 | + |
| 237 | +struct bst_test_list *test_client_install(struct bst_test_list *tests) |
| 238 | +{ |
| 239 | + return bst_add_tests(tests, test_vcs); |
| 240 | +} |
0 commit comments