summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2020-06-16 21:53:22 +1000
committerDamien George <damien@micropython.org>2020-07-18 14:23:47 +1000
commite152d0c19769f26e5416f86fe81afb9376fb531b (patch)
tree122c1a61eaedc86e4077139e0593d222549a2074 /tests
parent07aec4681f6ea5173315e660c6ba39cd12a2aa58 (diff)
extmod/btstack: Schedule notify/indicate/write ops for bg completion.
The goal of this commit is to allow using ble.gatts_notify() at any time, even if the stack is not ready to send the notification right now. It also addresses the same issue for ble.gatts_indicate() and ble.gattc_write() (without response). In addition this commit fixes the case where the buffer passed to write-with-response wasn't copied, meaning it could be modified by the caller, affecting the in-progress write. The changes are: - gatts_notify/indicate will now run in the background if the ACL buffer is currently full, meaning that notify/indicate can be called at any time. - gattc_write(mode=0) (no response) will now allow for one outstanding write. - gattc_write(mode=1) (with response) will now copy the buffer so that it can't be modified by the caller while the write is in progress. All four paths also now track the buffer while the operation is in progress, which prevents the GC free'ing the buffer while it's still needed.
Diffstat (limited to 'tests')
-rw-r--r--tests/multi_bluetooth/ble_characteristic.py24
-rw-r--r--tests/multi_bluetooth/ble_characteristic.py.exp9
2 files changed, 33 insertions, 0 deletions
diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py
index 33d92b823..fd6fd4672 100644
--- a/tests/multi_bluetooth/ble_characteristic.py
+++ b/tests/multi_bluetooth/ble_characteristic.py
@@ -16,6 +16,7 @@ _IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)
+_IRQ_GATTC_INDICATE = const(19)
SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A")
CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444")
@@ -59,6 +60,8 @@ def irq(event, data):
print("_IRQ_GATTC_WRITE_DONE", data[-1])
elif event == _IRQ_GATTC_NOTIFY:
print("_IRQ_GATTC_NOTIFY", data[-1])
+ elif event == _IRQ_GATTC_INDICATE:
+ print("_IRQ_GATTC_INDICATE", data[-1])
if waiting_event is not None:
if (isinstance(waiting_event, int) and event == waiting_event) or (
@@ -115,6 +118,16 @@ def instance0():
print("gatts_notify")
ble.gatts_notify(conn_handle, char_handle, "periph2")
+ # Wait for a write to the characteristic from the central.
+ wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS)
+
+ # Wait a bit, then notify a new value on the characteristic.
+ time.sleep_ms(1000)
+ print("gatts_write")
+ ble.gatts_write(char_handle, "periph3")
+ print("gatts_indicate")
+ ble.gatts_indicate(conn_handle, char_handle)
+
# Wait for the central to disconnect.
wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS)
finally:
@@ -163,6 +176,17 @@ def instance1():
ble.gattc_read(conn_handle, value_handle)
wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS)
+ # Write to the characteristic, and ask for a response.
+ print("gattc_write")
+ ble.gattc_write(conn_handle, value_handle, "central2", 1)
+ wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS)
+
+ # Wait for a indicate (should have new data), then read new value.
+ wait_for_event(_IRQ_GATTC_INDICATE, TIMEOUT_MS)
+ print("gattc_read")
+ ble.gattc_read(conn_handle, value_handle)
+ wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS)
+
# Disconnect from peripheral.
print("gap_disconnect:", ble.gap_disconnect(conn_handle))
wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS)
diff --git a/tests/multi_bluetooth/ble_characteristic.py.exp b/tests/multi_bluetooth/ble_characteristic.py.exp
index d89842ef7..7f66a4d90 100644
--- a/tests/multi_bluetooth/ble_characteristic.py.exp
+++ b/tests/multi_bluetooth/ble_characteristic.py.exp
@@ -6,6 +6,9 @@ gatts_write
gatts_notify
_IRQ_GATTS_WRITE b'central1'
gatts_notify
+_IRQ_GATTS_WRITE b'central2'
+gatts_write
+gatts_indicate
_IRQ_CENTRAL_DISCONNECT
--- instance1 ---
gap_connect
@@ -26,5 +29,11 @@ _IRQ_GATTC_NOTIFY b'periph2'
gattc_read
_IRQ_GATTC_READ_RESULT b'central1'
_IRQ_GATTC_READ_DONE 0
+gattc_write
+_IRQ_GATTC_WRITE_DONE 0
+_IRQ_GATTC_INDICATE b'periph3'
+gattc_read
+_IRQ_GATTC_READ_RESULT b'periph3'
+_IRQ_GATTC_READ_DONE 0
gap_disconnect: True
_IRQ_PERIPHERAL_DISCONNECT