Skip to content

Commit c07ea3e

Browse files
jimmodpgeorge
authored andcommitted
extmod/modbluetooth: Implement read done event.
On btstack there's no status associated with the read result, it comes through as a separate event. This allows you to detect read failures or timeouts.
1 parent 919d640 commit c07ea3e

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

examples/bluetooth/ble_temperature_central.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ def _irq(self, event, data):
161161
self._read_callback(self._value)
162162
self._read_callback = None
163163

164+
elif event == _IRQ_GATTC_READ_DONE:
165+
# Read completed.
166+
conn_handle, value_handle, status = data
167+
164168
elif event == _IRQ_GATTC_NOTIFY:
165169
# The ble_temperature.py demo periodically notifies its value.
166170
conn_handle, value_handle, notify_data = data

extmod/btstack/modbluetooth_btstack.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
171171
irq == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE ||
172172
irq == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE) {
173173
mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status);
174-
} else {
175-
// Note that query complete is fired for other operations like query value too.
176174
}
177175
} else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) {
178176
DEBUG_EVENT_printf(" --> gatt service query result\n");
@@ -262,6 +260,13 @@ STATIC void btstack_packet_handler_discover_descriptors(uint8_t packet_type, uin
262260
btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE);
263261
}
264262

263+
// For when the handler is being used for a read query.
264+
STATIC void btstack_packet_handler_read(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
265+
(void)channel;
266+
(void)size;
267+
btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_READ_DONE);
268+
}
269+
265270
// For when the handler is being used for write-with-response.
266271
STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
267272
(void)channel;
@@ -740,7 +745,7 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start
740745

741746
int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) {
742747
DEBUG_EVENT_printf("mp_bluetooth_gattc_read\n");
743-
return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_generic, conn_handle, value_handle));
748+
return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_read, conn_handle, value_handle));
744749
}
745750

746751
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) {

extmod/modbluetooth.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
898898
} else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE) {
899899
// conn_handle, value_handle, data
900900
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, &o->irq_data_data);
901-
} else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
901+
} else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
902902
// conn_handle, value_handle, status
903903
ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, NULL, NULL);
904904
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
@@ -1090,10 +1090,10 @@ void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state) {
10901090
schedule_ringbuf(atomic_state);
10911091
}
10921092

1093-
void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_handle, uint16_t status) {
1093+
void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status) {
10941094
MICROPY_PY_BLUETOOTH_ENTER
10951095
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
1096-
if (enqueue_irq(o, 2 + 2 + 2, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE)) {
1096+
if (enqueue_irq(o, 2 + 2 + 2, event)) {
10971097
ringbuf_put16(&o->ringbuf, conn_handle);
10981098
ringbuf_put16(&o->ringbuf, value_handle);
10991099
ringbuf_put16(&o->ringbuf, status);

extmod/modbluetooth.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_h
276276
void mp_bluetooth_gattc_on_data_available_chunk(const uint8_t *data, size_t data_len);
277277
void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state);
278278

279-
// Notify modbluetooth that a write has completed.
280-
void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_handle, uint16_t status);
279+
// Notify modbluetooth that a read or write operation has completed.
280+
void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status);
281281
#endif
282282

283283
// For stacks that don't manage attribute value data (currently all of them), helpers

extmod/nimble/modbluetooth_nimble.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_err
844844
if (error->status == 0) {
845845
gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, attr->handle, attr->om);
846846
}
847+
mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, attr->handle, error->status);
847848
return 0;
848849
}
849850

@@ -861,7 +862,7 @@ STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_er
861862
if (!mp_bluetooth_is_active()) {
862863
return 0;
863864
}
864-
mp_bluetooth_gattc_on_write_status(conn_handle, attr->handle, error->status);
865+
mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE, conn_handle, attr->handle, error->status);
865866
return 0;
866867
}
867868

0 commit comments

Comments
 (0)