diff --git a/example/hfp_ag_demo.c b/example/hfp_ag_demo.c index f628f5eb7..1707e7edd 100644 --- a/example/hfp_ag_demo.c +++ b/example/hfp_ag_demo.c @@ -595,7 +595,7 @@ static void send_sco_data(void){ if ((count & SCO_REPORT_PERIOD) == 0) printf("Sent %u\n", count); } -static void sco_packet_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){ +static void sco_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){ switch (packet_type){ case HCI_EVENT_PACKET: if (packet[0] == HCI_EVENT_SCO_CAN_SEND_NOW){ diff --git a/example/hsp_ag_demo.c b/example/hsp_ag_demo.c index 79df9d4b1..32782dc95 100644 --- a/example/hsp_ag_demo.c +++ b/example/hsp_ag_demo.c @@ -215,16 +215,26 @@ static void send_sco_data(void){ hci_request_sco_can_send_now_event(); static int count = 0; - count++; - if ((count & SCO_REPORT_PERIOD) == 0) printf("Sent %u\n", count); + if ((count & SCO_REPORT_PERIOD)) return; + printf("SCO packets sent: %u\n", count); } -static void sco_packet_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){ - return; +static void sco_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){ static int count = 0; - count++; - if ((count & SCO_REPORT_PERIOD)) return; - printf("SCO packets %u\n", count); + switch (packet_type){ + case HCI_EVENT_PACKET: + if (packet[0] == HCI_EVENT_SCO_CAN_SEND_NOW){ + send_sco_data(); + } + break; + case HCI_SCO_DATA_PACKET: + count++; + if ((count & SCO_REPORT_PERIOD)) return; + printf("SCO packets received: %u\n", count); + break; + default: + break; + } } static void packet_handler(uint8_t * event, uint16_t event_size){ @@ -233,9 +243,6 @@ static void packet_handler(uint8_t * event, uint16_t event_size){ if (event[2] != HCI_STATE_WORKING) break; show_usage(); break; - case HCI_EVENT_SCO_CAN_SEND_NOW: - send_sco_data(); - break; case HCI_EVENT_HSP_META: switch (event[2]) { case HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE: diff --git a/example/hsp_hs_demo.c b/example/hsp_hs_demo.c index f1240b943..ffa509db0 100644 --- a/example/hsp_hs_demo.c +++ b/example/hsp_hs_demo.c @@ -215,16 +215,24 @@ static void send_sco_data(void){ hci_request_sco_can_send_now_event(); static int count = 0; - count++; - if ((count & 15) == 0) printf("Sent %u\n", count); + if ((count & SCO_REPORT_PERIOD)) return; + printf("SCO packets sent: %u\n", count); } -static void sco_packet_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){ +static void sco_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){ static int count = 0; - // hexdumpf(packet, size); - count++; - if ((count & SCO_REPORT_PERIOD)) return; - printf("SCO packets %u\n", count); + switch (packet_type){ + case HCI_EVENT_PACKET: + if (packet[0] == HCI_EVENT_SCO_CAN_SEND_NOW){ + send_sco_data(); + } + break; + default: + count++; + if ((count & SCO_REPORT_PERIOD)) return; + printf("SCO packets received: %u\n", count); + break; + } } static void packet_handler(uint8_t * event, uint16_t event_size){ diff --git a/src/hci.c b/src/hci.c index 99e886c4c..a59b7b576 100644 --- a/src/hci.c +++ b/src/hci.c @@ -1816,7 +1816,7 @@ static void event_handler(uint8_t *packet, int size){ static void sco_handler(uint8_t * packet, uint16_t size){ if (!hci_stack->sco_packet_handler) return; - hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, packet, size); + hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); } static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ @@ -1844,14 +1844,14 @@ void hci_add_event_handler(btstack_packet_callback_registration_t * callback_han /** Register HCI packet handlers */ -void hci_register_acl_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ +void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ hci_stack->acl_packet_handler = handler; } /** * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. */ -void hci_register_sco_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ +void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ hci_stack->sco_packet_handler = handler; } @@ -2930,7 +2930,7 @@ 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){ if (!hci_stack->acl_packet_handler) return; - hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, packet, size); + hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); } static void hci_notify_if_sco_can_send_now(void){ @@ -2940,7 +2940,7 @@ static void hci_notify_if_sco_can_send_now(void){ hci_stack->sco_waiting_for_can_send_now = 0; uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); - hci_stack->sco_packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); + hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); } } diff --git a/src/hci.h b/src/hci.h index b5febacad..df5c2fb8b 100644 --- a/src/hci.h +++ b/src/hci.h @@ -519,10 +519,10 @@ typedef struct { btstack_linked_list_t connections; /* callback to L2CAP layer */ - void (*acl_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); + btstack_packet_handler_t acl_packet_handler; /* callback for SCO data */ - void (*sco_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); + btstack_packet_handler_t sco_packet_handler; /* callbacks for events */ btstack_linked_list_t event_handlers; @@ -720,12 +720,12 @@ void hci_add_event_handler(btstack_packet_callback_registration_t * callback_han /** * @brief Registers a packet handler for ACL data. Used by L2CAP */ -void hci_register_acl_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); +void hci_register_acl_packet_handler(btstack_packet_handler_t handler); /** * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. */ -void hci_register_sco_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); +void hci_register_sco_packet_handler(btstack_packet_handler_t handler); // Sending HCI Commands diff --git a/src/l2cap.c b/src/l2cap.c index ddc4f9121..0efef419c 100644 --- a/src/l2cap.c +++ b/src/l2cap.c @@ -82,7 +82,7 @@ static void l2cap_emit_channel_closed(l2cap_channel_t *channel); static void l2cap_emit_connection_request(l2cap_channel_t *channel); static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); -static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ); +static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ); static void l2cap_notify_channel_can_send(void); typedef struct l2cap_fixed_channel { @@ -1381,7 +1381,7 @@ static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * } } -static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ){ +static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ // Get Channel ID uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); @@ -1487,9 +1487,9 @@ static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t siz default: { // Find channel for this channel_id and connection handle - l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); + l2cap_channel_t * l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); if (channel) { - l2cap_dispatch_to_channel(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); + l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); } break; } @@ -1499,7 +1499,7 @@ static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t siz } // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE -void l2cap_finialize_channel_close(l2cap_channel_t *channel){ +void l2cap_finialize_channel_close(l2cap_channel_t * channel){ channel->state = L2CAP_STATE_CLOSED; l2cap_emit_channel_closed(channel); // discard channel