Skip to content

Commit 43cad17

Browse files
jimmodpgeorge
authored andcommitted
aioble/multitests/ble_write_capture.py: Add multitest for write capture.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 23b3c7f commit 43cad17

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--- instance0 ---
2+
advertise
3+
connected
4+
written b'central0'
5+
written b'central2'
6+
written b'central0' True
7+
written b'central1' True
8+
written b'central2' True
9+
written b'central3' True
10+
written b'central4' True
11+
--- instance1 ---
12+
connect
13+
service UUID('a5a5a5a5-ffff-9999-1111-5a5a5a5a5a5a')
14+
characteristic UUID('00000000-1111-2222-3333-444444444444') UUID('00000000-1111-2222-3333-555555555555')
15+
write
16+
write
17+
write
18+
write
19+
write
20+
write
21+
write
22+
write
23+
write
24+
write

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy