aboutsummaryrefslogtreecommitdiff
path: root/tests/multi_bluetooth/ble_gap_device_name.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/multi_bluetooth/ble_gap_device_name.py')
-rw-r--r--tests/multi_bluetooth/ble_gap_device_name.py53
1 files changed, 24 insertions, 29 deletions
diff --git a/tests/multi_bluetooth/ble_gap_device_name.py b/tests/multi_bluetooth/ble_gap_device_name.py
index fbc9d80ba..e7202170b 100644
--- a/tests/multi_bluetooth/ble_gap_device_name.py
+++ b/tests/multi_bluetooth/ble_gap_device_name.py
@@ -16,45 +16,44 @@ _IRQ_GATTC_READ_DONE = const(16)
GAP_DEVICE_NAME_UUID = bluetooth.UUID(0x2A00)
-waiting_event = None
-waiting_data = None
-value_handle = 0
+waiting_events = {}
def irq(event, data):
- global waiting_event, waiting_data, value_handle
if event == _IRQ_CENTRAL_CONNECT:
print("_IRQ_CENTRAL_CONNECT")
+ waiting_events[event] = data[0]
elif event == _IRQ_CENTRAL_DISCONNECT:
print("_IRQ_CENTRAL_DISCONNECT")
elif event == _IRQ_PERIPHERAL_CONNECT:
print("_IRQ_PERIPHERAL_CONNECT")
+ waiting_events[event] = data[0]
elif event == _IRQ_PERIPHERAL_DISCONNECT:
print("_IRQ_PERIPHERAL_DISCONNECT")
elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT:
if data[-1] == GAP_DEVICE_NAME_UUID:
print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1])
- value_handle = data[2]
+ waiting_events[event] = data[2]
+ else:
+ return
+ elif event == _IRQ_GATTC_CHARACTERISTIC_DONE:
+ print("_IRQ_GATTC_CHARACTERISTIC_DONE")
elif event == _IRQ_GATTC_READ_RESULT:
print("_IRQ_GATTC_READ_RESULT", bytes(data[-1]))
- if waiting_event is not None:
- if isinstance(waiting_event, int) and event == waiting_event:
- waiting_event = None
- waiting_data = data
+ if event not in waiting_events:
+ waiting_events[event] = None
def wait_for_event(event, timeout_ms):
- global waiting_event, waiting_data
- waiting_event = event
- waiting_data = None
-
t0 = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms:
- if waiting_data:
- return True
+ if event in waiting_events:
+ result = waiting_events[event]
+ del waiting_events[event]
+ return result
machine.idle()
- return False
+ raise ValueError("Timeout waiting for {}".format(event))
# Acting in peripheral role.
@@ -79,10 +78,8 @@ def instance0():
ble.gap_advertise(20_000)
# Wait for central to connect, then wait for it to disconnect.
- if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS):
- return
- if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, 4 * TIMEOUT_MS):
- return
+ wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS)
+ wait_for_event(_IRQ_CENTRAL_DISCONNECT, 4 * TIMEOUT_MS)
finally:
ble.active(0)
@@ -91,6 +88,7 @@ def instance0():
def instance1():
multitest.next()
try:
+ value_handle = None
for iteration in range(2):
# Wait for peripheral to start advertising.
time.sleep_ms(500)
@@ -98,17 +96,15 @@ def instance1():
# Connect to peripheral.
print("gap_connect")
ble.gap_connect(*BDADDR)
- if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS):
- return
- conn_handle, _, _ = waiting_data
+ conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS)
if iteration == 0:
+ # Only do characteristic discovery on the first iteration,
+ # assume value_handle is unchanged on the second.
print("gattc_discover_characteristics")
ble.gattc_discover_characteristics(conn_handle, 1, 65535)
- wait_for_event(lambda: value_handle, TIMEOUT_MS)
-
- # Wait for all characteristic results to come in (there should be an event for it).
- time.sleep_ms(500)
+ value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS)
+ wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS)
# Read the peripheral's GAP device name.
print("gattc_read")
@@ -117,8 +113,7 @@ def instance1():
# Disconnect from peripheral.
print("gap_disconnect:", ble.gap_disconnect(conn_handle))
- if not wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS):
- return
+ wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS)
finally:
ble.active(0)