summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2020-06-02 14:22:47 +1000
committerDamien George <damien.p.george@gmail.com>2020-06-05 14:08:15 +1000
commitc07ea3e4c2cfd9fe75a8b207a39d8bb0295fcc3b (patch)
tree0732d6941d18a4ecd9a1d36c9c7419234883ac17
parent919d640aec637004110d9f4df9814523a3bfd847 (diff)
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.
-rw-r--r--examples/bluetooth/ble_temperature_central.py4
-rw-r--r--extmod/btstack/modbluetooth_btstack.c11
-rw-r--r--extmod/modbluetooth.c6
-rw-r--r--extmod/modbluetooth.h4
-rw-r--r--extmod/nimble/modbluetooth_nimble.c3
5 files changed, 19 insertions, 9 deletions
diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py
index 19dc93409..619578462 100644
--- a/examples/bluetooth/ble_temperature_central.py
+++ b/examples/bluetooth/ble_temperature_central.py
@@ -161,6 +161,10 @@ class BLETemperatureCentral:
self._read_callback(self._value)
self._read_callback = None
+ elif event == _IRQ_GATTC_READ_DONE:
+ # Read completed.
+ conn_handle, value_handle, status = data
+
elif event == _IRQ_GATTC_NOTIFY:
# The ble_temperature.py demo periodically notifies its value.
conn_handle, value_handle, notify_data = data
diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c
index 112c11a65..31b282575 100644
--- a/extmod/btstack/modbluetooth_btstack.c
+++ b/extmod/btstack/modbluetooth_btstack.c
@@ -171,8 +171,6 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
irq == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE ||
irq == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE) {
mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status);
- } else {
- // Note that query complete is fired for other operations like query value too.
}
} else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) {
DEBUG_EVENT_printf(" --> gatt service query result\n");
@@ -262,6 +260,13 @@ STATIC void btstack_packet_handler_discover_descriptors(uint8_t packet_type, uin
btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE);
}
+// For when the handler is being used for a read query.
+STATIC void btstack_packet_handler_read(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
+ (void)channel;
+ (void)size;
+ btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_READ_DONE);
+}
+
// For when the handler is being used for write-with-response.
STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
(void)channel;
@@ -740,7 +745,7 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start
int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) {
DEBUG_EVENT_printf("mp_bluetooth_gattc_read\n");
- return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_generic, conn_handle, value_handle));
+ return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_read, conn_handle, value_handle));
}
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) {
diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c
index 3d66bc84c..34d18b0b7 100644
--- a/extmod/modbluetooth.c
+++ b/extmod/modbluetooth.c
@@ -898,7 +898,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
} else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE) {
// conn_handle, value_handle, data
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, &o->irq_data_data);
- } else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
+ } else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
// conn_handle, value_handle, status
ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, NULL, NULL);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
@@ -1090,10 +1090,10 @@ void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state) {
schedule_ringbuf(atomic_state);
}
-void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_handle, uint16_t status) {
+void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status) {
MICROPY_PY_BLUETOOTH_ENTER
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
- if (enqueue_irq(o, 2 + 2 + 2, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE)) {
+ if (enqueue_irq(o, 2 + 2 + 2, event)) {
ringbuf_put16(&o->ringbuf, conn_handle);
ringbuf_put16(&o->ringbuf, value_handle);
ringbuf_put16(&o->ringbuf, status);
diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h
index 5d02d96e3..c8b8dc63f 100644
--- a/extmod/modbluetooth.h
+++ b/extmod/modbluetooth.h
@@ -276,8 +276,8 @@ size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_h
void mp_bluetooth_gattc_on_data_available_chunk(const uint8_t *data, size_t data_len);
void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state);
-// Notify modbluetooth that a write has completed.
-void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_handle, uint16_t status);
+// Notify modbluetooth that a read or write operation has completed.
+void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status);
#endif
// For stacks that don't manage attribute value data (currently all of them), helpers
diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c
index 4b0f25848..9b95d05f3 100644
--- a/extmod/nimble/modbluetooth_nimble.c
+++ b/extmod/nimble/modbluetooth_nimble.c
@@ -844,6 +844,7 @@ STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_err
if (error->status == 0) {
gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, attr->handle, attr->om);
}
+ mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, attr->handle, error->status);
return 0;
}
@@ -861,7 +862,7 @@ STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_er
if (!mp_bluetooth_is_active()) {
return 0;
}
- mp_bluetooth_gattc_on_write_status(conn_handle, attr->handle, error->status);
+ mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE, conn_handle, attr->handle, error->status);
return 0;
}