Skip to content

Commit 9d823a5

Browse files
jimmodpgeorge
authored andcommitted
extmod/modbluetooth: Add event for "indicate acknowledgement".
This commit adds the IRQ_GATTS_INDICATE_DONE BLE event which will be raised with the status of gatts_indicate (unlike notify, indications require acknowledgement). An example of its use is added to ble_temperature.py, and to the multitests in ble_characteristic.py. Implemented for btstack and nimble bindings, tested in both directions between unix/btstack and pybd/nimble.
1 parent 3c7ca20 commit 9d823a5

File tree

7 files changed

+92
-14
lines changed

7 files changed

+92
-14
lines changed

examples/bluetooth/ble_temperature.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
_IRQ_CENTRAL_CONNECT = const(1)
1515
_IRQ_CENTRAL_DISCONNECT = const(2)
16+
_IRQ_GATTS_INDICATE_DONE = const(20)
1617

1718
# org.bluetooth.service.environmental_sensing
1819
_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
1920
# org.bluetooth.characteristic.temperature
2021
_TEMP_CHAR = (
2122
bluetooth.UUID(0x2A6E),
22-
bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
23+
bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE,
2324
)
2425
_ENV_SENSE_SERVICE = (
2526
_ENV_SENSE_UUID,
@@ -52,15 +53,21 @@ def _irq(self, event, data):
5253
self._connections.remove(conn_handle)
5354
# Start advertising again to allow a new connection.
5455
self._advertise()
56+
elif event == _IRQ_GATTS_INDICATE_DONE:
57+
conn_handle, value_handle, status, = data
5558

56-
def set_temperature(self, temp_deg_c, notify=False):
59+
def set_temperature(self, temp_deg_c, notify=False, indicate=False):
5760
# Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius.
5861
# Write the local value, ready for a central to read.
5962
self._ble.gatts_write(self._handle, struct.pack("<h", int(temp_deg_c * 100)))
60-
if notify:
63+
if notify or indicate:
6164
for conn_handle in self._connections:
62-
# Notify connected centrals to issue a read.
63-
self._ble.gatts_notify(conn_handle, self._handle)
65+
if notify:
66+
# Notify connected centrals.
67+
self._ble.gatts_notify(conn_handle, self._handle)
68+
if indicate:
69+
# Indicate connected centrals.
70+
self._ble.gatts_indicate(conn_handle, self._handle)
6471

6572
def _advertise(self, interval_us=500000):
6673
self._ble.gap_advertise(interval_us, adv_data=self._payload)
@@ -76,7 +83,7 @@ def demo():
7683
while True:
7784
# Write every second, notify every 10 seconds.
7885
i = (i + 1) % 10
79-
temp.set_temperature(t, notify=i == 0)
86+
temp.set_temperature(t, notify=i == 0, indicate=False)
8087
# Random walk the temperature.
8188
t += random.uniform(-0.5, 0.5)
8289
time.sleep_ms(1000)

extmod/btstack/modbluetooth_btstack.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,46 @@ STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_typ
238238
}
239239
#endif
240240

241-
STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) {
242-
DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet);
241+
// This needs to be separate to btstack_packet_handler otherwise we get
242+
// dual-delivery of the HCI_EVENT_LE_META event.
243+
STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
244+
(void)channel;
245+
(void)size;
246+
DEBUG_EVENT_printf("btstack_packet_handler_att_server(packet_type=%u, packet=%p)\n", packet_type, packet);
243247
if (packet_type != HCI_EVENT_PACKET) {
244248
return;
245249
}
246250

247251
uint8_t event_type = hci_event_packet_get_type(packet);
252+
248253
if (event_type == ATT_EVENT_CONNECTED) {
249254
DEBUG_EVENT_printf(" --> att connected\n");
255+
// The ATT_EVENT_*CONNECTED events are fired for both peripheral and central role, with no way to tell which.
256+
// So we use the HCI_EVENT_LE_META event directly in the main packet handler.
250257
} else if (event_type == ATT_EVENT_DISCONNECTED) {
251258
DEBUG_EVENT_printf(" --> att disconnected\n");
252-
} else if (event_type == HCI_EVENT_LE_META) {
259+
} else if (event_type == ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE) {
260+
DEBUG_EVENT_printf(" --> att indication complete\n");
261+
uint16_t conn_handle = att_event_handle_value_indication_complete_get_conn_handle(packet);
262+
uint16_t value_handle = att_event_handle_value_indication_complete_get_attribute_handle(packet);
263+
uint8_t status = att_event_handle_value_indication_complete_get_status(packet);
264+
mp_bluetooth_gatts_on_indicate_complete(conn_handle, value_handle, status);
265+
} else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
266+
// Ignore, duplicated by att_server.c.
267+
} else {
268+
DEBUG_EVENT_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type);
269+
}
270+
}
271+
272+
STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) {
273+
DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet);
274+
if (packet_type != HCI_EVENT_PACKET) {
275+
return;
276+
}
277+
278+
uint8_t event_type = hci_event_packet_get_type(packet);
279+
280+
if (event_type == HCI_EVENT_LE_META) {
253281
DEBUG_EVENT_printf(" --> hci le meta\n");
254282
if (hci_event_le_meta_get_subevent_code(packet) == HCI_SUBEVENT_LE_CONNECTION_COMPLETE) {
255283
uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
@@ -277,7 +305,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
277305
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
278306
}
279307
} else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) {
280-
DEBUG_EVENT_printf(" --> hci transport packet set\n");
308+
DEBUG_EVENT_printf(" --> hci transport packet sent\n");
281309
} else if (event_type == HCI_EVENT_COMMAND_COMPLETE) {
282310
DEBUG_EVENT_printf(" --> hci command complete\n");
283311
} else if (event_type == HCI_EVENT_COMMAND_STATUS) {
@@ -288,6 +316,8 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
288316
DEBUG_EVENT_printf(" --> btstack # conns changed\n");
289317
} else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) {
290318
DEBUG_EVENT_printf(" --> hci vendor specific\n");
319+
} else if (event_type == GATT_EVENT_MTU) {
320+
DEBUG_EVENT_printf(" --> hci MTU\n");
291321
} else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
292322
DEBUG_EVENT_printf(" --> hci disconnect complete\n");
293323
uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet);
@@ -500,6 +530,9 @@ int mp_bluetooth_init(void) {
500530
// Register for HCI events.
501531
hci_add_event_handler(&hci_event_callback_registration);
502532

533+
// Register for ATT server events.
534+
att_server_register_packet_handler(&btstack_packet_handler_att_server);
535+
503536
// Set a timeout for HCI initialisation.
504537
btstack_run_loop_set_timer(&btstack_init_deinit_timeout, BTSTACK_INIT_DEINIT_TIMEOUT_MS);
505538
btstack_run_loop_set_timer_handler(&btstack_init_deinit_timeout, btstack_init_deinit_timeout_handler);
@@ -816,7 +849,8 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
816849
size_t len = 0;
817850
mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &data, &len);
818851

819-
// TODO: Handle ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE to generate acknowledgment event.
852+
// Indicate will raise ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE when
853+
// acknowledged (or timeout/error).
820854

821855
// Attempt to send immediately, will copy buffer.
822856
MICROPY_PY_BLUETOOTH_ENTER

extmod/modbluetooth.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ STATIC const mp_rom_map_elem_t mp_module_bluetooth_globals_table[] = {
800800
{ MP_ROM_QSTR(MP_QSTR_FLAG_READ), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ) },
801801
{ MP_ROM_QSTR(MP_QSTR_FLAG_WRITE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE) },
802802
{ MP_ROM_QSTR(MP_QSTR_FLAG_NOTIFY), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY) },
803+
{ MP_ROM_QSTR(MP_QSTR_FLAG_INDICATE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE) },
803804
{ MP_ROM_QSTR(MP_QSTR_FLAG_WRITE_NO_RESPONSE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_NO_RESPONSE) },
804805
};
805806

@@ -887,6 +888,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
887888
} else if (event == MP_BLUETOOTH_IRQ_GATTS_WRITE) {
888889
// conn_handle, value_handle
889890
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL);
891+
} else if (event == MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE) {
892+
// conn_handle, value_handle, status
893+
ringbuf_extract(&o->ringbuf, data_tuple, 2, 1, NULL, 0, NULL, NULL);
890894
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
891895
} else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) {
892896
// addr_type, addr, adv_type, rssi, adv_data
@@ -999,6 +1003,17 @@ void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) {
9991003
schedule_ringbuf(atomic_state);
10001004
}
10011005

1006+
void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status) {
1007+
MICROPY_PY_BLUETOOTH_ENTER
1008+
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
1009+
if (enqueue_irq(o, 2 + 2 + 1, MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE)) {
1010+
ringbuf_put16(&o->ringbuf, conn_handle);
1011+
ringbuf_put16(&o->ringbuf, value_handle);
1012+
ringbuf_put(&o->ringbuf, status);
1013+
}
1014+
schedule_ringbuf(atomic_state);
1015+
}
1016+
10021017
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
10031018
void mp_bluetooth_gap_on_scan_complete(void) {
10041019
MICROPY_PY_BLUETOOTH_ENTER

extmod/modbluetooth.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_NO_RESPONSE (1 << 2)
6969
#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE (1 << 3)
7070
#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY (1 << 4)
71+
#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE (1 << 5)
7172

7273
// For mp_bluetooth_gattc_write, the mode parameter
7374
#define MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE (0)
@@ -107,6 +108,7 @@
107108
#define MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE (17)
108109
#define MP_BLUETOOTH_IRQ_GATTC_NOTIFY (18)
109110
#define MP_BLUETOOTH_IRQ_GATTC_INDICATE (19)
111+
#define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20)
110112

111113
/*
112114
These aren't included in the module for space reasons, but can be used
@@ -132,6 +134,7 @@ _IRQ_GATTC_READ_DONE = const(16)
132134
_IRQ_GATTC_WRITE_DONE = const(17)
133135
_IRQ_GATTC_NOTIFY = const(18)
134136
_IRQ_GATTC_INDICATE = const(19)
137+
_IRQ_GATTS_INDICATE_DONE = const(20)
135138
*/
136139

137140
// Common UUID type.
@@ -245,6 +248,9 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han
245248
// Call this when a characteristic is written to.
246249
void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle);
247250

251+
// Call this when an acknowledgment is received for an indication.
252+
void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status);
253+
248254
#if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK
249255
// Call this when a characteristic is read from. Return false to deny the read.
250256
bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle);

extmod/nimble/modbluetooth_nimble.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) {
248248
reverse_addr_byte_order(addr, event->disconnect.conn.peer_id_addr.val);
249249
mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT, event->disconnect.conn.conn_handle, event->disconnect.conn.peer_id_addr.type, addr);
250250
break;
251+
252+
case BLE_GAP_EVENT_NOTIFY_TX: {
253+
DEBUG_EVENT_printf("gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status);
254+
// This event corresponds to either a sent notify/indicate (status == 0), or an indication confirmation (status != 0).
255+
if (event->notify_tx.indication && event->notify_tx.status != 0) {
256+
// Map "done/ack" to 0, otherwise pass the status directly.
257+
mp_bluetooth_gatts_on_indicate_complete(event->notify_tx.conn_handle, event->notify_tx.attr_handle, event->notify_tx.status == BLE_HS_EDONE ? 0 : event->notify_tx.status);
258+
}
259+
}
251260
}
252261

253262
return 0;
@@ -603,8 +612,8 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
603612
if (!mp_bluetooth_is_active()) {
604613
return ERRNO_BLUETOOTH_NOT_ACTIVE;
605614
}
606-
// TODO: catch BLE_GAP_EVENT_NOTIFY_TX to raise an event for completed
607-
// indication transaction.
615+
// This will raise BLE_GAP_EVENT_NOTIFY_TX with a status when it is
616+
// acknowledged (or timeout/error).
608617
return ble_hs_err_to_errno(ble_gattc_indicate(conn_handle, value_handle));
609618
}
610619

tests/multi_bluetooth/ble_characteristic.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
_IRQ_GATTC_WRITE_DONE = const(17)
1818
_IRQ_GATTC_NOTIFY = const(18)
1919
_IRQ_GATTC_INDICATE = const(19)
20+
_IRQ_GATTS_INDICATE_DONE = const(20)
2021

2122
SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A")
2223
CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444")
2324
CHAR = (
2425
CHAR_UUID,
25-
bluetooth.FLAG_READ | bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY,
26+
bluetooth.FLAG_READ | bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE,
2627
)
2728
SERVICE = (
2829
SERVICE_UUID,
@@ -62,6 +63,8 @@ def irq(event, data):
6263
print("_IRQ_GATTC_NOTIFY", data[-1])
6364
elif event == _IRQ_GATTC_INDICATE:
6465
print("_IRQ_GATTC_INDICATE", data[-1])
66+
elif event == _IRQ_GATTS_INDICATE_DONE:
67+
print("_IRQ_GATTS_INDICATE_DONE", data[-1])
6568

6669
if waiting_event is not None:
6770
if (isinstance(waiting_event, int) and event == waiting_event) or (
@@ -128,6 +131,9 @@ def instance0():
128131
print("gatts_indicate")
129132
ble.gatts_indicate(conn_handle, char_handle)
130133

134+
# Wait for the indicate ack.
135+
wait_for_event(_IRQ_GATTS_INDICATE_DONE, TIMEOUT_MS)
136+
131137
# Wait for the central to disconnect.
132138
wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS)
133139
finally:

tests/multi_bluetooth/ble_characteristic.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ gatts_notify
99
_IRQ_GATTS_WRITE b'central2'
1010
gatts_write
1111
gatts_indicate
12+
_IRQ_GATTS_INDICATE_DONE 0
1213
_IRQ_CENTRAL_DISCONNECT
1314
--- instance1 ---
1415
gap_connect

0 commit comments

Comments
 (0)