From 323d300063a7e0193cf61fbe114062cf000a3fe5 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 6 Apr 2018 18:05:12 +0200 Subject: [PATCH] hfp: add local_role field to hfp_connection --- src/classic/hfp.c | 13 +++++++------ src/classic/hfp.h | 14 +++++++++++--- src/classic/hfp_ag.c | 4 ++-- src/classic/hfp_hf.c | 4 ++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/classic/hfp.c b/src/classic/hfp.c index 9c4e70973..ecc79f4ca 100644 --- a/src/classic/hfp.c +++ b/src/classic/hfp.c @@ -362,11 +362,12 @@ static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){ btstack_memory_hfp_connection_free(hfp_connection); } -static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ +static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){ hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); if (hfp_connection) return hfp_connection; hfp_connection = create_hfp_connection_context(); memcpy(hfp_connection->remote_addr, bd_addr, 6); + hfp_connection->local_role = local_role; return hfp_connection; } @@ -535,7 +536,7 @@ static void hfp_handle_failed_sco_connection(uint8_t status){ } -void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ +void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ UNUSED(channel); // ok: no channel bd_addr_t event_addr; @@ -564,7 +565,7 @@ void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet case RFCOMM_EVENT_INCOMING_CONNECTION: // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); - hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr); + hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role); if (!hfp_connection){ log_info("hfp: no memory to accept incoming connection - decline"); rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); @@ -691,7 +692,7 @@ void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet if (!hfp_connection) break; if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ hfp_connection->state = HFP_IDLE; - hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid); + hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); break; } @@ -1361,8 +1362,8 @@ static void parse_sequence(hfp_connection_t * hfp_connection){ } } -void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){ - hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr); +void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ + hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role); log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection); if (!hfp_connection) { diff --git a/src/classic/hfp.h b/src/classic/hfp.h index b1045dd63..93b7f45cc 100644 --- a/src/classic/hfp.h +++ b/src/classic/hfp.h @@ -154,6 +154,11 @@ extern "C" { #define HFP_CODEC_CVSD 0x01 #define HFP_CODEC_MSBC 0x02 +typedef enum { + HFP_ROLE_AG = 0, + HFP_ROLE_HF, +} hfp_role_t; + typedef enum { HFP_CMD_NONE = 0, HFP_CMD_ERROR, @@ -474,6 +479,9 @@ typedef struct{ typedef struct hfp_connection { btstack_linked_item_t item; + // local role: HF or AG + hfp_role_t local_role; + bd_addr_t remote_addr; hci_con_handle_t acl_handle; hci_con_handle_t sco_handle; @@ -485,7 +493,7 @@ typedef struct hfp_connection { hfp_state_t state; hfp_codecs_state_t codecs_state; - // needed for reestablishing connection + // needed for reestablishing connection - service uuid of the remote uint16_t service_uuid; // used during service level connection establishment @@ -638,7 +646,7 @@ void hfp_set_callback(btstack_packet_handler_t callback); void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler); void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name); -void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); +void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role); void hfp_emit_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t value); void hfp_emit_simple_event(btstack_packet_handler_t callback, uint8_t event_subtype); void hfp_emit_string_event(btstack_packet_handler_t callback, uint8_t event_subtype, const char * value); @@ -652,7 +660,7 @@ hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle); btstack_linked_list_t * hfp_get_connections(void); void hfp_parse(hfp_connection_t * connection, uint8_t byte, int isHandsFree); -void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid); +void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role); void hfp_release_service_level_connection(hfp_connection_t * connection); void hfp_reset_context_flags(hfp_connection_t * connection); diff --git a/src/classic/hfp_ag.c b/src/classic/hfp_ag.c index 3ba410e1e..7fb07b993 100644 --- a/src/classic/hfp_ag.c +++ b/src/classic/hfp_ag.c @@ -2008,7 +2008,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); return; } - hfp_handle_hci_event(packet_type, channel, packet, size); + hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); break; default: break; @@ -2068,7 +2068,7 @@ void hfp_ag_init(uint16_t rfcomm_channel_nr){ } void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ - hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE); + hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); } void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ diff --git a/src/classic/hfp_hf.c b/src/classic/hfp_hf.c index b9057ce86..c35e26636 100644 --- a/src/classic/hfp_hf.c +++ b/src/classic/hfp_hf.c @@ -1078,7 +1078,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe hfp_handle_rfcomm_event(packet_type, channel, packet, size); break; case HCI_EVENT_PACKET: - hfp_handle_hci_event(packet_type, channel, packet, size); + hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); break; default: break; @@ -1128,7 +1128,7 @@ void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ } void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ - hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY); + hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); } void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){