diff --git a/example/embedded/gap_dedicated_bonding.c b/example/embedded/gap_dedicated_bonding.c index b9a98cf51..61c59f359 100644 --- a/example/embedded/gap_dedicated_bonding.c +++ b/example/embedded/gap_dedicated_bonding.c @@ -52,12 +52,11 @@ #include "btstack_memory.h" #include "hci_dump.h" -// static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15}; -static void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){ - printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); +static btstack_packet_callback_registration_t hci_event_callback_registration; +static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ if (packet_type != HCI_EVENT_PACKET) return; switch (packet[0]) { @@ -77,7 +76,9 @@ static void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size) int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]){ - hci_register_packet_handler(packet_handler); + hci_event_callback_registration.callback = &packet_handler; + hci_add_event_handler(&hci_event_callback_registration); + // turn on! hci_power_control(HCI_POWER_ON); diff --git a/example/embedded/gap_inquiry.c b/example/embedded/gap_inquiry.c index 639a9af88..0d7b1832a 100644 --- a/example/embedded/gap_inquiry.c +++ b/example/embedded/gap_inquiry.c @@ -83,6 +83,7 @@ int deviceCount = 0; enum STATE {INIT, ACTIVE} ; enum STATE state = INIT; +static btstack_packet_callback_registration_t hci_event_callback_registration; static int getDeviceIndexForAddress( bd_addr_t addr){ int j; @@ -136,7 +137,7 @@ static void continue_remote_names(void){ * INIT, and ACTIVE. */ -static void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){ +static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ bd_addr_t addr; int i; int numResponses; @@ -245,7 +246,9 @@ static void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size) /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */ int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]) { - hci_register_packet_handler(packet_handler); + + hci_event_callback_registration.callback = &packet_handler; + hci_add_event_handler(&hci_event_callback_registration); // turn on! hci_power_control(HCI_POWER_ON); diff --git a/example/embedded/gap_inquiry_and_bond.c b/example/embedded/gap_inquiry_and_bond.c index 8e6cdec72..469cd35ae 100644 --- a/example/embedded/gap_inquiry_and_bond.c +++ b/example/embedded/gap_inquiry_and_bond.c @@ -78,6 +78,7 @@ int deviceCount = 0; enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ; enum STATE state = INIT; +static btstack_packet_callback_registration_t hci_event_callback_registration; static int getDeviceIndexForAddress( bd_addr_t addr){ int j; @@ -221,7 +222,8 @@ static void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size) int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]) { - hci_register_packet_handler(packet_handler); + hci_event_callback_registration.callback = &packet_handler; + hci_add_event_handler(&hci_event_callback_registration); // turn on! hci_power_control(HCI_POWER_ON); diff --git a/example/embedded/gap_le_advertisements.c b/example/embedded/gap_le_advertisements.c index d44933ee0..c41b73fad 100644 --- a/example/embedded/gap_le_advertisements.c +++ b/example/embedded/gap_le_advertisements.c @@ -56,6 +56,8 @@ #include "hci.h" #include "ble/ad_parser.h" +static btstack_packet_callback_registration_t hci_event_callback_registration; + /* @section GAP LE setup for receiving advertisements * * @text GAP LE advertisements are received as custom HCI events of the @@ -64,10 +66,11 @@ */ /* LISTING_START(GAPLEAdvSetup): Setting up GAP LE client for receiving advertisements */ -static void handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size); +static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); static void gap_le_advertisements_setup(void){ - hci_register_packet_handler(handle_hci_event); + hci_event_callback_registration.callback = &packet_handler; + hci_add_event_handler(&hci_event_callback_registration); } /* LISTING_END */ @@ -224,7 +227,7 @@ static void dump_advertisement_data(uint8_t * adv_data, uint8_t adv_size){ /* LISTING_START(GAPLEAdvPacketHandler): Scanning and receiving advertisements */ -static void handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){ +static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ if (packet_type != HCI_EVENT_PACKET) return; switch (packet[0]) { diff --git a/src/hci.c b/src/hci.c index 838256d79..e35fb25bd 100644 --- a/src/hci.c +++ b/src/hci.c @@ -284,12 +284,6 @@ static int nr_hci_connections(void){ return count; } -/** - * Dummy handler called by HCI - */ -static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ -} - uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ hci_connection_t * connection = hci_connection_for_handle(handle); if (!connection) { @@ -1814,8 +1808,8 @@ void hci_add_event_handler(btstack_packet_callback_registration_t * callback_han /** Register HCI packet handlers */ -void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ - hci_stack->packet_handler = handler; +void hci_register_acl_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ + hci_stack->acl_packet_handler = handler; } /** @@ -1877,9 +1871,6 @@ void hci_init(const hci_transport_t *transport, void *config, btstack_link_key_d // init used hardware control with NULL // init used chipset with NULL - // higher level handler - hci_stack->packet_handler = dummy_handler; - // store and open remote device db hci_stack->link_key_db = link_key_db; if (hci_stack->link_key_db) { @@ -2879,11 +2870,12 @@ int hci_send_cmd(const hci_cmd_t *cmd, ...){ // TODO: generalize, use table similar to hci_create_command static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ + // dump packet if (dump) { hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); } - hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); + // dispatch to all event handlers btstack_linked_list_iterator_t it; btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); while (btstack_linked_list_iterator_has_next(&it)){ @@ -2893,7 +2885,8 @@ static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ } static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ - hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, size); + if (!hci_stack->acl_packet_handler) return; + hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, packet, size); } void hci_emit_state(void){ diff --git a/src/hci.h b/src/hci.h index f85dc7537..daf743676 100644 --- a/src/hci.h +++ b/src/hci.h @@ -573,7 +573,7 @@ typedef struct { uint16_t packet_types; /* callback to L2CAP layer */ - void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); + void (*acl_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); /* callback for SCO data */ void (*sco_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); @@ -791,9 +791,9 @@ void hci_set_bd_addr(bd_addr_t addr); void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler); /** - * @brief Registers a packet handler. Used if L2CAP is not used (rarely). + * @brief Registers a packet handler for ACL data. Used by L2CAP */ -void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); +void hci_register_acl_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); /** * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. diff --git a/src/l2cap.c b/src/l2cap.c index fb8c78fd9..0d6bc6100 100644 --- a/src/l2cap.c +++ b/src/l2cap.c @@ -73,6 +73,8 @@ static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; static int signaling_responses_pending; +static btstack_packet_callback_registration_t hci_event_callback_registration; + static btstack_linked_list_t l2cap_channels; static btstack_linked_list_t l2cap_services; static btstack_linked_list_t l2cap_le_channels; @@ -111,7 +113,11 @@ void l2cap_init(void){ // // register callback with HCI // - hci_register_packet_handler(&l2cap_packet_handler); + hci_event_callback_registration.callback = &l2cap_packet_handler; + hci_add_event_handler(&hci_event_callback_registration); + + hci_register_acl_packet_handler(&l2cap_packet_handler); + hci_connectable_control(0); // no services yet }