@@ -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
0 commit comments