Commit e152d0c1 authored by Jim Mussared's avatar Jim Mussared Committed by Damien George

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.
parent 07aec468
This diff is collapsed.
......@@ -33,6 +33,8 @@
#include "lib/btstack/src/btstack.h"
typedef struct _mp_btstack_pending_op_t mp_btstack_pending_op_t;
typedef struct _mp_bluetooth_btstack_root_pointers_t {
// This stores both the advertising data and the scan response data, concatenated together.
uint8_t *adv_data;
......@@ -45,6 +47,7 @@ typedef struct _mp_bluetooth_btstack_root_pointers_t {
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
// Registration for notify/indicate events.
gatt_client_notification_t notification;
btstack_linked_list_t pending_ops;
#endif
} mp_bluetooth_btstack_root_pointers_t;
......
......@@ -321,8 +321,7 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map
case MP_QSTR_gap_name: {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(e->value, &bufinfo, MP_BUFFER_READ);
int ret = mp_bluetooth_gap_set_device_name(bufinfo.buf, bufinfo.len);
bluetooth_handle_errno(ret);
bluetooth_handle_errno(mp_bluetooth_gap_set_device_name(bufinfo.buf, bufinfo.len));
break;
}
case MP_QSTR_rxbuf: {
......@@ -663,10 +662,9 @@ STATIC mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args)
if (n_args == 4) {
mp_buffer_info_t bufinfo = {0};
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
size_t len = bufinfo.len;
int err = mp_bluetooth_gatts_notify_send(conn_handle, value_handle, bufinfo.buf, &len);
int err = mp_bluetooth_gatts_notify_send(conn_handle, value_handle, bufinfo.buf, bufinfo.len);
bluetooth_handle_errno(err);
return MP_OBJ_NEW_SMALL_INT(len);
return mp_const_none;
} else {
int err = mp_bluetooth_gatts_notify(conn_handle, value_handle);
return bluetooth_handle_errno(err);
......@@ -674,6 +672,15 @@ STATIC mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args)
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_notify_obj, 3, 4, bluetooth_ble_gatts_notify);
STATIC mp_obj_t bluetooth_ble_gatts_indicate(mp_obj_t self_in, mp_obj_t conn_handle_in, mp_obj_t value_handle_in) {
mp_int_t conn_handle = mp_obj_get_int(conn_handle_in);
mp_int_t value_handle = mp_obj_get_int(value_handle_in);
int err = mp_bluetooth_gatts_indicate(conn_handle, value_handle);
return bluetooth_handle_errno(err);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_gatts_indicate_obj, bluetooth_ble_gatts_indicate);
STATIC mp_obj_t bluetooth_ble_gatts_set_buffer(size_t n_args, const mp_obj_t *args) {
mp_int_t value_handle = mp_obj_get_int(args[1]);
mp_int_t len = mp_obj_get_int(args[2]);
......@@ -765,6 +772,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_gatts_read), MP_ROM_PTR(&bluetooth_ble_gatts_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_write), MP_ROM_PTR(&bluetooth_ble_gatts_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_notify), MP_ROM_PTR(&bluetooth_ble_gatts_notify_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_indicate), MP_ROM_PTR(&bluetooth_ble_gatts_indicate_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_set_buffer), MP_ROM_PTR(&bluetooth_ble_gatts_set_buffer_obj) },
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
// GATT Client (i.e. central/scanner role)
......@@ -1147,47 +1155,58 @@ mp_bluetooth_gatts_db_entry_t *mp_bluetooth_gatts_db_lookup(mp_gatts_db_t db, ui
}
int mp_bluetooth_gatts_db_read(mp_gatts_db_t db, uint16_t handle, uint8_t **value, size_t *value_len) {
MICROPY_PY_BLUETOOTH_ENTER
mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(db, handle);
if (!entry) {
return MP_EINVAL;
}
*value = entry->data;
*value_len = entry->data_len;
if (entry->append) {
entry->data_len = 0;
if (entry) {
*value = entry->data;
*value_len = entry->data_len;
if (entry->append) {
entry->data_len = 0;
}
}
return 0;
MICROPY_PY_BLUETOOTH_EXIT
return entry ? 0 : MP_EINVAL;
}
int mp_bluetooth_gatts_db_write(mp_gatts_db_t db, uint16_t handle, const uint8_t *value, size_t value_len) {
MICROPY_PY_BLUETOOTH_ENTER
mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(db, handle);
if (!entry) {
return MP_EINVAL;
}
if (entry) {
if (value_len > entry->data_alloc) {
uint8_t *data = m_new_maybe(uint8_t, value_len);
if (data) {
entry->data = data;
entry->data_alloc = value_len;
} else {
MICROPY_PY_BLUETOOTH_EXIT
return MP_ENOMEM;
}
}
if (value_len > entry->data_alloc) {
entry->data = m_new(uint8_t, value_len);
entry->data_alloc = value_len;
memcpy(entry->data, value, value_len);
entry->data_len = value_len;
}
memcpy(entry->data, value, value_len);
entry->data_len = value_len;
return 0;
MICROPY_PY_BLUETOOTH_EXIT
return entry ? 0 : MP_EINVAL;
}
int mp_bluetooth_gatts_db_resize(mp_gatts_db_t db, uint16_t handle, size_t len, bool append) {
MICROPY_PY_BLUETOOTH_ENTER
mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(db, handle);
if (!entry) {
return MP_EINVAL;
if (entry) {
uint8_t *data = m_renew_maybe(uint8_t, entry->data, entry->data_alloc, len, true);
if (data) {
entry->data = data;
entry->data_alloc = len;
entry->data_len = 0;
entry->append = append;
} else {
MICROPY_PY_BLUETOOTH_EXIT
return MP_ENOMEM;
}
}
entry->data = m_renew(uint8_t, entry->data, entry->data_alloc, len);
entry->data_alloc = len;
entry->data_len = 0;
entry->append = append;
return 0;
MICROPY_PY_BLUETOOTH_EXIT
return entry ? 0 : MP_EINVAL;
}
#endif // MICROPY_PY_BLUETOOTH
......@@ -199,7 +199,7 @@ int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t
// Notify the central that it should do a read.
int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle);
// Notify the central, including a data payload. (Note: does not set the gatts db value).
int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len);
int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len);
// Indicate the central.
int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle);
......
......@@ -587,13 +587,13 @@ int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) {
return ble_hs_err_to_errno(ble_gattc_notify(conn_handle, value_handle));
}
int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len) {
int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) {
if (!mp_bluetooth_is_active()) {
return ERRNO_BLUETOOTH_NOT_ACTIVE;
}
struct os_mbuf *om = ble_hs_mbuf_from_flat(value, *value_len);
struct os_mbuf *om = ble_hs_mbuf_from_flat(value, value_len);
if (om == NULL) {
return -1;
return MP_ENOMEM;
}
// TODO: check that notify_custom takes ownership of om, if not os_mbuf_free_chain(om).
return ble_hs_err_to_errno(ble_gattc_notify_custom(conn_handle, value_handle, om));
......
......@@ -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)
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment