|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "utils.h" |
| 8 | +#include "gatt_utils.h" |
| 9 | + |
| 10 | +#include <zephyr/bluetooth/addr.h> |
| 11 | +#include <zephyr/bluetooth/bluetooth.h> |
| 12 | +#include <zephyr/bluetooth/conn.h> |
| 13 | +#include <zephyr/settings/settings.h> |
| 14 | +#include <zephyr/toolchain/gcc.h> |
| 15 | + |
| 16 | +#include <stdint.h> |
| 17 | +#include <string.h> |
| 18 | + |
| 19 | +void client_round_0(void) |
| 20 | +{ |
| 21 | + struct bt_conn *conn; |
| 22 | + |
| 23 | + printk("start round 0...........\n"); |
| 24 | + |
| 25 | + conn = connect_as_peripheral(); |
| 26 | + printk("connected: conn %p\n", conn); |
| 27 | + wait_bonded(); |
| 28 | + |
| 29 | + gatt_discover(); |
| 30 | + activate_robust_caching(); |
| 31 | + /* subscribe to the SC indication, so we don't have to ATT read to |
| 32 | + * become change-aware. |
| 33 | + */ |
| 34 | + gatt_subscribe_to_service_changed(true); |
| 35 | + |
| 36 | + read_test_char(true); |
| 37 | + |
| 38 | + disconnect(conn); |
| 39 | +} |
| 40 | + |
| 41 | +void client_round_1(void) |
| 42 | +{ |
| 43 | + struct bt_conn *conn; |
| 44 | + |
| 45 | + printk("start round 1...........\n"); |
| 46 | + |
| 47 | + conn = connect_as_peripheral(); |
| 48 | + printk("connected: conn %p\n", conn); |
| 49 | + wait_secured(); |
| 50 | + |
| 51 | + /* server should remember we are change-aware */ |
| 52 | + read_test_char(true); |
| 53 | + |
| 54 | + disconnect(conn); |
| 55 | +} |
| 56 | + |
| 57 | +void client_round_2(void) |
| 58 | +{ |
| 59 | + struct bt_conn *conn; |
| 60 | + |
| 61 | + printk("start round 2...........\n"); |
| 62 | + |
| 63 | + conn = connect_as_peripheral(); |
| 64 | + printk("connected: conn %p\n", conn); |
| 65 | + wait_secured(); |
| 66 | + |
| 67 | + /* We are change-unaware. wait until the Service Changed indication is |
| 68 | + * received, that should then make us change-aware. |
| 69 | + */ |
| 70 | + wait_for_sc_indication(); |
| 71 | + read_test_char(true); |
| 72 | + |
| 73 | + /* We sleep just enough so that the server's `delayed store` work item |
| 74 | + * is executed. We still trigger a disconnect, even though the server |
| 75 | + * device will be unresponsive for this round. |
| 76 | + */ |
| 77 | + k_sleep(K_MSEC(CONFIG_BT_SETTINGS_DELAYED_STORE_MS)); |
| 78 | + |
| 79 | + disconnect(conn); |
| 80 | +} |
| 81 | + |
| 82 | +void client_round_3(void) |
| 83 | +{ |
| 84 | + struct bt_conn *conn; |
| 85 | + |
| 86 | + printk("start round 3...........\n"); |
| 87 | + |
| 88 | + conn = connect_as_peripheral(); |
| 89 | + printk("connected: conn %p\n", conn); |
| 90 | + wait_secured(); |
| 91 | + |
| 92 | + /* server should remember we are change-aware */ |
| 93 | + read_test_char(true); |
| 94 | + |
| 95 | + /* Unsubscribe from the SC indication. |
| 96 | + * |
| 97 | + * In the next round, we will be change-unaware, so the first ATT read |
| 98 | + * will fail, but the second one will succeed and we will be marked as |
| 99 | + * change-aware again. |
| 100 | + */ |
| 101 | + gatt_subscribe_to_service_changed(false); |
| 102 | + |
| 103 | + disconnect(conn); |
| 104 | +} |
| 105 | + |
| 106 | +void client_round_4(void) |
| 107 | +{ |
| 108 | + struct bt_conn *conn; |
| 109 | + |
| 110 | + printk("start round 4...........\n"); |
| 111 | + |
| 112 | + conn = connect_as_peripheral(); |
| 113 | + printk("connected: conn %p\n", conn); |
| 114 | + wait_secured(); |
| 115 | + |
| 116 | + /* GATT DB has changed again. |
| 117 | + * Before disc: svc1 |
| 118 | + * After disc: svc1 + svc2 |
| 119 | + * At boot: svc1 |
| 120 | + * Expect a failure on the first read of the same GATT handle. |
| 121 | + */ |
| 122 | + read_test_char(false); |
| 123 | + read_test_char(true); |
| 124 | + |
| 125 | + disconnect(conn); |
| 126 | +} |
| 127 | + |
| 128 | +void client_round_5(void) |
| 129 | +{ |
| 130 | + printk("start round 5...........\n"); |
| 131 | + printk("don't need to do anything, central will " |
| 132 | + "not connect to us\n"); |
| 133 | +} |
| 134 | + |
| 135 | +void client_round_6(void) |
| 136 | +{ |
| 137 | + struct bt_conn *conn; |
| 138 | + |
| 139 | + printk("start round 6...........\n"); |
| 140 | + conn = connect_as_peripheral(); |
| 141 | + printk("connected: conn %p\n", conn); |
| 142 | + wait_secured(); |
| 143 | + |
| 144 | + /* GATT DB has changed again. |
| 145 | + * Expect a failure on the first read of the same GATT handle. |
| 146 | + */ |
| 147 | + read_test_char(false); |
| 148 | + read_test_char(true); |
| 149 | + |
| 150 | + disconnect(conn); |
| 151 | +} |
| 152 | + |
| 153 | +void client_procedure(void) |
| 154 | +{ |
| 155 | + bt_enable(NULL); |
| 156 | + settings_load(); |
| 157 | + |
| 158 | + client_round_0(); |
| 159 | + client_round_1(); |
| 160 | + client_round_2(); |
| 161 | + client_round_3(); |
| 162 | + client_round_4(); |
| 163 | + client_round_5(); |
| 164 | + client_round_6(); |
| 165 | + |
| 166 | + PASS("PASS\n"); |
| 167 | +} |
0 commit comments