|
| 1 | +# Test for sending notifications to subscribed clients. |
| 2 | + |
| 3 | +from micropython import const |
| 4 | +import time, machine, bluetooth |
| 5 | + |
| 6 | +TIMEOUT_MS = 5000 |
| 7 | + |
| 8 | +_IRQ_CENTRAL_CONNECT = const(1) |
| 9 | +_IRQ_CENTRAL_DISCONNECT = const(2) |
| 10 | +_IRQ_GATTS_WRITE = const(3) |
| 11 | +_IRQ_PERIPHERAL_CONNECT = const(7) |
| 12 | +_IRQ_PERIPHERAL_DISCONNECT = const(8) |
| 13 | +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) |
| 14 | +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) |
| 15 | +_IRQ_GATTC_DESCRIPTOR_RESULT = const(13) |
| 16 | +_IRQ_GATTC_DESCRIPTOR_DONE = const(14) |
| 17 | +_IRQ_GATTC_READ_RESULT = const(15) |
| 18 | +_IRQ_GATTC_READ_DONE = const(16) |
| 19 | +_IRQ_GATTC_WRITE_DONE = const(17) |
| 20 | +_IRQ_GATTC_NOTIFY = const(18) |
| 21 | +_IRQ_GATTC_INDICATE = const(19) |
| 22 | + |
| 23 | +_CCCD_UUID = bluetooth.UUID(const(0x2902)) |
| 24 | + |
| 25 | +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") |
| 26 | +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") |
| 27 | +CHAR = ( |
| 28 | + CHAR_UUID, |
| 29 | + bluetooth.FLAG_READ | bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE, |
| 30 | +) |
| 31 | +SERVICE = ( |
| 32 | + SERVICE_UUID, |
| 33 | + (CHAR,), |
| 34 | +) |
| 35 | +SERVICES = (SERVICE,) |
| 36 | + |
| 37 | +waiting_events = {} |
| 38 | + |
| 39 | + |
| 40 | +def irq(event, data): |
| 41 | + if event == _IRQ_CENTRAL_CONNECT: |
| 42 | + print("_IRQ_CENTRAL_CONNECT") |
| 43 | + waiting_events[event] = data[0] |
| 44 | + elif event == _IRQ_CENTRAL_DISCONNECT: |
| 45 | + print("_IRQ_CENTRAL_DISCONNECT") |
| 46 | + elif event == _IRQ_GATTS_WRITE: |
| 47 | + print("_IRQ_GATTS_WRITE", ble.gatts_read(data[-1])) |
| 48 | + elif event == _IRQ_PERIPHERAL_CONNECT: |
| 49 | + print("_IRQ_PERIPHERAL_CONNECT") |
| 50 | + waiting_events[event] = data[0] |
| 51 | + elif event == _IRQ_PERIPHERAL_DISCONNECT: |
| 52 | + print("_IRQ_PERIPHERAL_DISCONNECT") |
| 53 | + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: |
| 54 | + # conn_handle, def_handle, value_handle, properties, uuid = data |
| 55 | + if data[-1] == CHAR_UUID: |
| 56 | + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) |
| 57 | + waiting_events[event] = data[2] |
| 58 | + else: |
| 59 | + return |
| 60 | + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: |
| 61 | + print("_IRQ_GATTC_CHARACTERISTIC_DONE") |
| 62 | + elif event == _IRQ_GATTC_DESCRIPTOR_RESULT: |
| 63 | + # conn_handle, dsc_handle, uuid = data |
| 64 | + if data[-1] == _CCCD_UUID: |
| 65 | + print("_IRQ_GATTC_DESCRIPTOR_RESULT", data[-1]) |
| 66 | + waiting_events[event] = data[1] |
| 67 | + else: |
| 68 | + return |
| 69 | + elif event == _IRQ_GATTC_DESCRIPTOR_DONE: |
| 70 | + print("_IRQ_GATTC_DESCRIPTOR_DONE") |
| 71 | + elif event == _IRQ_GATTC_READ_RESULT: |
| 72 | + print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) |
| 73 | + elif event == _IRQ_GATTC_READ_DONE: |
| 74 | + print("_IRQ_GATTC_READ_DONE", data[-1]) |
| 75 | + elif event == _IRQ_GATTC_WRITE_DONE: |
| 76 | + print("_IRQ_GATTC_WRITE_DONE", data[-1]) |
| 77 | + elif event == _IRQ_GATTC_NOTIFY: |
| 78 | + print("_IRQ_GATTC_NOTIFY", bytes(data[-1])) |
| 79 | + elif event == _IRQ_GATTC_INDICATE: |
| 80 | + print("_IRQ_GATTC_NOTIFY", bytes(data[-1])) |
| 81 | + |
| 82 | + if event not in waiting_events: |
| 83 | + waiting_events[event] = None |
| 84 | + |
| 85 | + |
| 86 | +def wait_for_event(event, timeout_ms): |
| 87 | + t0 = time.ticks_ms() |
| 88 | + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: |
| 89 | + if event in waiting_events: |
| 90 | + return waiting_events.pop(event) |
| 91 | + machine.idle() |
| 92 | + raise ValueError("Timeout waiting for {}".format(event)) |
| 93 | + |
| 94 | + |
| 95 | +# Acting in peripheral role. |
| 96 | +def instance0(): |
| 97 | + multitest.globals(BDADDR=ble.config("mac")) |
| 98 | + ((char_handle,),) = ble.gatts_register_services(SERVICES) |
| 99 | + print("gap_advertise") |
| 100 | + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") # \x04\x09MPY |
| 101 | + multitest.next() |
| 102 | + try: |
| 103 | + # Write initial characteristic value (will be read by client). |
| 104 | + ble.gatts_write(char_handle, "periph0") ### |
| 105 | + |
| 106 | + # Wait for central to connect to us. |
| 107 | + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS * 10) |
| 108 | + |
| 109 | + # A |
| 110 | + # Wait for a write to the characteristic from the central (to synchronise). |
| 111 | + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) |
| 112 | + print("sync A") |
| 113 | + # This should be local-only. |
| 114 | + ble.gatts_write(char_handle, "periph1") |
| 115 | + time.sleep_ms(100) |
| 116 | + # Update local-only, then force notify. |
| 117 | + ble.gatts_write(char_handle, "periph2") |
| 118 | + ble.gatts_notify(conn_handle, char_handle) ### |
| 119 | + time.sleep_ms(100) |
| 120 | + # Update local and notify subscribers. No notification should be sent. |
| 121 | + ble.gatts_write(char_handle, "periph3", True) |
| 122 | + time.sleep_ms(100) |
| 123 | + multitest.broadcast("A") |
| 124 | + |
| 125 | + # B |
| 126 | + # Synchronise with the client (which should now be subscribed for notify). |
| 127 | + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) |
| 128 | + print("sync B") |
| 129 | + # This should be local-only (send_update=False). |
| 130 | + ble.gatts_write(char_handle, "periph4", False) |
| 131 | + time.sleep_ms(100) |
| 132 | + # This should notify the subscribed client. |
| 133 | + ble.gatts_write(char_handle, "periph5", True) ### |
| 134 | + time.sleep_ms(100) |
| 135 | + multitest.broadcast("B") |
| 136 | + |
| 137 | + # C |
| 138 | + # Synchronise with the client (which should now be subscribed for indicate). |
| 139 | + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) |
| 140 | + print("sync C") |
| 141 | + # This should be local-only (send_update=False). |
| 142 | + ble.gatts_write(char_handle, "periph6", False) |
| 143 | + time.sleep_ms(100) |
| 144 | + # This should indicate the subscribed client. |
| 145 | + ble.gatts_write(char_handle, "periph7", True) ### |
| 146 | + time.sleep_ms(100) |
| 147 | + multitest.broadcast("C") |
| 148 | + |
| 149 | + # D |
| 150 | + # Synchronise with the client (which should now be unsubscribed). |
| 151 | + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) |
| 152 | + print("sync D") |
| 153 | + # This should be local-only (send_update=False). |
| 154 | + ble.gatts_write(char_handle, "periph8", False) |
| 155 | + time.sleep_ms(100) |
| 156 | + # This should be local-only (no more subscribers). |
| 157 | + ble.gatts_write(char_handle, "periph9", True) |
| 158 | + time.sleep_ms(100) |
| 159 | + # Update local-only, then another force notify. |
| 160 | + ble.gatts_write(char_handle, "periph10") |
| 161 | + ble.gatts_notify(conn_handle, char_handle) ### |
| 162 | + time.sleep_ms(100) |
| 163 | + multitest.broadcast("D") |
| 164 | + |
| 165 | + # Wait for the central to disconnect. |
| 166 | + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) |
| 167 | + finally: |
| 168 | + ble.active(0) |
| 169 | + |
| 170 | + |
| 171 | +# Acting in central role. |
| 172 | +def instance1(): |
| 173 | + multitest.next() |
| 174 | + try: |
| 175 | + # Connect to peripheral and then disconnect. |
| 176 | + print("gap_connect") |
| 177 | + ble.gap_connect(*BDADDR) |
| 178 | + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) |
| 179 | + |
| 180 | + # Discover characteristics. |
| 181 | + ble.gattc_discover_characteristics(conn_handle, 1, 65535) |
| 182 | + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) |
| 183 | + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) |
| 184 | + |
| 185 | + # Discover CCCD. |
| 186 | + ble.gattc_discover_descriptors(conn_handle, value_handle, value_handle + 5) |
| 187 | + cccd_handle = wait_for_event(_IRQ_GATTC_DESCRIPTOR_RESULT, TIMEOUT_MS) |
| 188 | + wait_for_event(_IRQ_GATTC_DESCRIPTOR_DONE, TIMEOUT_MS) |
| 189 | + |
| 190 | + # Issue read of characteristic, should get initial value. |
| 191 | + print("gattc_read") |
| 192 | + ble.gattc_read(conn_handle, value_handle) |
| 193 | + wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) |
| 194 | + |
| 195 | + # While the four states are active, all incoming notifications |
| 196 | + # and indications will be printed by the event handler. We |
| 197 | + # should only expect to see certain ones. |
| 198 | + |
| 199 | + # Start unsubscribed. |
| 200 | + # Write to the characteristic (triggers A). |
| 201 | + print("gattc_write") |
| 202 | + ble.gattc_write(conn_handle, value_handle, "central0", 1) |
| 203 | + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) |
| 204 | + # Wait for A to complete. |
| 205 | + multitest.wait("A") |
| 206 | + |
| 207 | + # Subscribe for notify. |
| 208 | + ble.gattc_write(conn_handle, cccd_handle, b"\x01\x00", 1) |
| 209 | + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) |
| 210 | + # Write to the characteristic (triggers B). |
| 211 | + print("gattc_write") |
| 212 | + ble.gattc_write(conn_handle, value_handle, "central1", 1) |
| 213 | + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) |
| 214 | + # Wait for B to complete. |
| 215 | + multitest.wait("B") |
| 216 | + |
| 217 | + # Subscribe for indicate. |
| 218 | + ble.gattc_write(conn_handle, cccd_handle, b"\x02\x00", 1) |
| 219 | + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) |
| 220 | + # Write to the characteristic (triggers C). |
| 221 | + print("gattc_write") |
| 222 | + ble.gattc_write(conn_handle, value_handle, "central2", 1) |
| 223 | + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) |
| 224 | + # Wait for C to complete. |
| 225 | + multitest.wait("C") |
| 226 | + |
| 227 | + # Unsubscribe. |
| 228 | + ble.gattc_write(conn_handle, cccd_handle, b"\x00\x00", 1) |
| 229 | + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) |
| 230 | + # Write to the characteristic (triggers D). |
| 231 | + print("gattc_write") |
| 232 | + ble.gattc_write(conn_handle, value_handle, "central3", 1) |
| 233 | + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) |
| 234 | + # Wait for D to complete. |
| 235 | + multitest.wait("D") |
| 236 | + |
| 237 | + # Disconnect from peripheral. |
| 238 | + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) |
| 239 | + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) |
| 240 | + finally: |
| 241 | + ble.active(0) |
| 242 | + |
| 243 | + |
| 244 | +ble = bluetooth.BLE() |
| 245 | +ble.active(1) |
| 246 | +ble.irq(irq) |
0 commit comments