From ca7f8243b0d02a799100889530c5b9f4dbde3709 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Wed, 3 Feb 2016 18:31:40 +0100 Subject: [PATCH] att_dispatcher: register directly for HCI Events --- src/ble/att_dispatch.c | 45 ++++++++++++++++++++++++++---------------- src/l2cap.c | 6 ------ 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/ble/att_dispatch.c b/src/ble/att_dispatch.c index f9bc1f81d..3f0151f09 100644 --- a/src/ble/att_dispatch.c +++ b/src/ble/att_dispatch.c @@ -50,27 +50,36 @@ static void dummy_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t * static btstack_packet_handler_t att_client_handler = &dummy_packet_handler; static btstack_packet_handler_t att_server_handler = &dummy_packet_handler; +static btstack_packet_callback_registration_t hci_event_callback_registration; + +static int registered_for_hci_events = 0; + static void dummy_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ } +static void att_hci_event_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){ + att_client_handler(packet_type, 0, packet, size); + att_server_handler(packet_type, 0, packet, size); +} + +static void att_dispatch_register_for_hci_events(void){ + if (registered_for_hci_events) return; + registered_for_hci_events = 1; + hci_event_callback_registration.callback = &att_hci_event_handler; + hci_add_event_handler(&hci_event_callback_registration); +} + static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ - switch (packet_type){ - case ATT_DATA_PACKET: { - // log_info("att_data_packet with opcode 0x%x", packet[0]); - uint8_t att_pdu_odd = packet[0] & 1; - if (att_pdu_odd){ - // odd PDUs are sent from server to client - att_client_handler(packet_type, handle, packet, size); - } else { - // even PDUs are sent from client to server - att_server_handler(packet_type, handle, packet, size); - } - break; - } - default: - att_client_handler(packet_type, handle, packet, size); - att_server_handler(packet_type, handle, packet, size); - break; + if (packet_type != ATT_DATA_PACKET) return; + + // log_info("att_data_packet with opcode 0x%x", packet[0]); + uint8_t att_pdu_odd = packet[0] & 1; + if (att_pdu_odd){ + // odd PDUs are sent from server to client + att_client_handler(packet_type, handle, packet, size); + } else { + // even PDUs are sent from client to server + att_server_handler(packet_type, handle, packet, size); } } @@ -79,6 +88,7 @@ static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pa * @param packet_hander for ATT client packets */ void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ + att_dispatch_register_for_hci_events(); if (packet_handler == NULL){ packet_handler = dummy_packet_handler; } @@ -91,6 +101,7 @@ void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ * @param packet_hander for ATT server packets */ void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ + att_dispatch_register_for_hci_events(); if (packet_handler == NULL){ packet_handler = dummy_packet_handler; } diff --git a/src/l2cap.c b/src/l2cap.c index 7b3b5a422..48582d14e 100644 --- a/src/l2cap.c +++ b/src/l2cap.c @@ -905,9 +905,6 @@ static void l2cap_event_handler(uint8_t *packet, uint16_t size){ if (!channel->packet_handler) continue; (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); } - if (attribute_protocol_packet_handler) { - (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); - } if (connectionless_channel_packet_handler) { (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); } @@ -969,9 +966,6 @@ static void l2cap_event_handler(uint8_t *packet, uint16_t size){ // pass on: main packet handler, att and sm packet handlers (*packet_handler)(HCI_EVENT_PACKET, 0, packet, size); - if (attribute_protocol_packet_handler){ - (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); - } if (connectionless_channel_packet_handler) { (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); }