|
| 1 | +# Test characteristic write capture. |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +sys.path.append("") |
| 6 | + |
| 7 | +from micropython import const |
| 8 | +import time, machine |
| 9 | + |
| 10 | +import uasyncio as asyncio |
| 11 | +import aioble |
| 12 | +import bluetooth |
| 13 | + |
| 14 | +TIMEOUT_MS = 5000 |
| 15 | + |
| 16 | +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") |
| 17 | +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") |
| 18 | +CHAR_CAPTURE_UUID = bluetooth.UUID("00000000-1111-2222-3333-555555555555") |
| 19 | + |
| 20 | + |
| 21 | +# Acting in peripheral role. |
| 22 | +async def instance0_task(): |
| 23 | + service = aioble.Service(SERVICE_UUID) |
| 24 | + characteristic = aioble.Characteristic( |
| 25 | + service, |
| 26 | + CHAR_UUID, |
| 27 | + write=True, |
| 28 | + ) |
| 29 | + # Second characteristic enabled write capture. |
| 30 | + characteristic_capture = aioble.Characteristic( |
| 31 | + service, |
| 32 | + CHAR_CAPTURE_UUID, |
| 33 | + write=True, |
| 34 | + capture=True, |
| 35 | + ) |
| 36 | + aioble.register_services(service) |
| 37 | + |
| 38 | + multitest.globals(BDADDR=aioble.config("mac")) |
| 39 | + multitest.next() |
| 40 | + |
| 41 | + # Wait for central to connect to us. |
| 42 | + print("advertise") |
| 43 | + async with await aioble.advertise( |
| 44 | + 20_000, adv_data=b"\x02\x01\x06\x04\xffMPY", timeout_ms=TIMEOUT_MS |
| 45 | + ) as connection: |
| 46 | + print("connected") |
| 47 | + |
| 48 | + # We should miss writes while we're sleeping. |
| 49 | + for i in range(2): |
| 50 | + await characteristic.written(timeout_ms=TIMEOUT_MS) |
| 51 | + print("written", characteristic.read()) |
| 52 | + await asyncio.sleep_ms(500) |
| 53 | + |
| 54 | + # Shouldn't miss any writes as they will be captured and queued. |
| 55 | + for i in range(5): |
| 56 | + write_connection, value = await characteristic_capture.written(timeout_ms=TIMEOUT_MS) |
| 57 | + print("written", value, write_connection == connection) |
| 58 | + await asyncio.sleep_ms(500) |
| 59 | + |
| 60 | + |
| 61 | +def instance0(): |
| 62 | + try: |
| 63 | + asyncio.run(instance0_task()) |
| 64 | + finally: |
| 65 | + aioble.stop() |
| 66 | + |
| 67 | + |
| 68 | +# Acting in central role. |
| 69 | +async def instance1_task(): |
| 70 | + multitest.next() |
| 71 | + |
| 72 | + # Connect to peripheral and then disconnect. |
| 73 | + print("connect") |
| 74 | + device = aioble.Device(*BDADDR) |
| 75 | + async with await device.connect(timeout_ms=TIMEOUT_MS) as connection: |
| 76 | + # Discover characteristics. |
| 77 | + service = await connection.service(SERVICE_UUID) |
| 78 | + print("service", service.uuid) |
| 79 | + characteristic = await service.characteristic(CHAR_UUID) |
| 80 | + characteristic_capture = await service.characteristic(CHAR_CAPTURE_UUID) |
| 81 | + print("characteristic", characteristic.uuid, characteristic_capture.uuid) |
| 82 | + |
| 83 | + # Write to the characteristic five times, but faster than the remote side is waiting. |
| 84 | + # Some writes will be lost. |
| 85 | + for i in range(5): |
| 86 | + print("write") |
| 87 | + await characteristic.write("central" + str(i), timeout_ms=TIMEOUT_MS) |
| 88 | + await asyncio.sleep_ms(200) |
| 89 | + |
| 90 | + # Write to the capture characteristic five times, but faster than the remote side is waiting. |
| 91 | + # The writes should be captured and queued. |
| 92 | + for i in range(5): |
| 93 | + print("write") |
| 94 | + await characteristic_capture.write("central" + str(i), timeout_ms=TIMEOUT_MS) |
| 95 | + await asyncio.sleep_ms(200) |
| 96 | + |
| 97 | + |
| 98 | +def instance1(): |
| 99 | + try: |
| 100 | + asyncio.run(instance1_task()) |
| 101 | + finally: |
| 102 | + aioble.stop() |
0 commit comments