Skip to content

Commit 02df2b0

Browse files
committed
extmod/btstack: Reset pending_value_handle before calling read-done cb.
Similar to the previous commit but for MP_BLUETOOTH_IRQ_GATTC_READ_DONE: the pending_value_handle needs to be reset before calling mp_bluetooth_gattc_on_read_write_status(), which will call the Python IRQ handler, which may in turn call back into BTstack to perform an action like a write. In that case the pending_value_handle will need to be available for the write/read/etc to proceed. Fixes issue #13634. Signed-off-by: Damien George <damien@micropython.org>
1 parent e7ff724 commit 02df2b0

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

extmod/btstack/modbluetooth_btstack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,9 @@ STATIC void btstack_packet_handler_read(uint8_t packet_type, uint16_t channel, u
462462
if (!conn) {
463463
return;
464464
}
465-
mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, conn->pending_value_handle, status);
465+
uint16_t value_handle = conn->pending_value_handle;
466466
conn->pending_value_handle = 0xffff;
467+
mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, value_handle, status);
467468
} else if (event_type == GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) {
468469
DEBUG_printf(" --> gatt characteristic value query result\n");
469470
uint16_t conn_handle = gatt_event_characteristic_value_query_result_get_handle(packet);

tests/multi_bluetooth/ble_irq_calls.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def __init__(self):
6565
self.done = False
6666
self._conn_handle = None
6767
self._service = None
68-
self._characteristic = None
68+
self._characteristic_handle = None
6969
self._cccd_handle = None
70+
self._reads_remaining = None
7071
ble.active(1)
7172
ble.irq(self._ble_event_handler)
7273
ble.gap_connect(*BDADDR)
@@ -101,7 +102,8 @@ def _ble_event_handler(self, event, data):
101102
_, end_handle, value_handle, properties, uuid = data
102103
assert uuid == STATE_UUID
103104
print("characteristic found:", uuid)
104-
self._characteristic = (end_handle, value_handle, properties)
105+
assert self._characteristic_handle is None
106+
self._characteristic_handle = value_handle
105107

106108
elif event == _IRQ_GATTC_CHARACTERISTIC_DONE:
107109
start_handle, end_handle = self._service
@@ -128,17 +130,21 @@ def _ble_event_handler(self, event, data):
128130
elif event == _IRQ_GATTC_WRITE_DONE:
129131
conn_handle, _, result = data
130132
print("CCCD write result:", result)
131-
_, state_handle, _ = self._characteristic
132133
print("issue gattc_read")
133-
ble.gattc_read(self._conn_handle, state_handle)
134+
self._reads_remaining = 2
135+
ble.gattc_read(self._conn_handle, self._characteristic_handle)
134136

135137
elif event == _IRQ_GATTC_READ_RESULT:
136138
_, _, char_data = data
137139
print("gattc_read result:", bytes(char_data))
138140

139141
elif event == _IRQ_GATTC_READ_DONE:
140-
self.done = True
141-
ble.gap_disconnect(self._conn_handle)
142+
self._reads_remaining -= 1
143+
if self._reads_remaining > 0:
144+
ble.gattc_read(self._conn_handle, self._characteristic_handle)
145+
else:
146+
self.done = True
147+
ble.gap_disconnect(self._conn_handle)
142148

143149

144150
class Peripheral:

tests/multi_bluetooth/ble_irq_calls.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ peripheral start
33
_IRQ_CENTRAL_CONNECT
44
_IRQ_MTU_EXCHANGED
55
_IRQ_GATTS_READ_REQUEST
6+
_IRQ_GATTS_READ_REQUEST
67
_IRQ_CENTRAL_DISCONNECT
78
--- instance1 ---
89
central start
@@ -27,5 +28,8 @@ issue gattc_read
2728
_IRQ_GATTC_READ_RESULT
2829
gattc_read result: b''
2930
_IRQ_GATTC_READ_DONE
31+
_IRQ_GATTC_READ_RESULT
32+
gattc_read result: b''
33+
_IRQ_GATTC_READ_DONE
3034
_IRQ_PERIPHERAL_DISCONNECT
3135
connection closed

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