diff --git a/example/avrcp_browsing_client.c b/example/avrcp_browsing_client.c index c707f2729..2327c4224 100644 --- a/example/avrcp_browsing_client.c +++ b/example/avrcp_browsing_client.c @@ -134,10 +134,8 @@ int btstack_main(int argc, const char * argv[]){ // Register AVRCP for HCI events. avrcp_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler); - // Initialize AVRCP Controller. + // Initialize AVRCP Browsing Controller, HCI events will be sent to the AVRCP Controller callback. avrcp_browsing_controller_init(); - // Register AVRCP for HCI events. - avrcp_browsing_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler); // Initialize SDP. sdp_init(); @@ -180,13 +178,13 @@ static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: { local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet); if (browsing_cid != 0 && browsing_cid != local_cid) { - printf("AVRCP Browsing Client: Connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid); + printf("AVRCP Browsing Client: AVRCP Controller connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid); return; } status = avrcp_subevent_connection_established_get_status(packet); if (status != ERROR_CODE_SUCCESS){ - printf("AVRCP Browsing Client: Connection failed: status 0x%02x\n", status); + printf("AVRCP Browsing Client: AVRCP Controller connection failed: status 0x%02x\n", status); browsing_cid = 0; return; } @@ -194,11 +192,11 @@ static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16 avrcp_cid = local_cid; avrcp_connected = 1; avrcp_subevent_connection_established_get_bd_addr(packet, adress); - printf("AVRCP Browsing Client: Channel successfully opened: %s, browsing_cid 0x%02x\n", bd_addr_to_str(adress), avrcp_cid); + printf("AVRCP Browsing Client: AVRCP Controller Channel successfully opened: %s, avrcp_cid 0x%02x\n", bd_addr_to_str(adress), avrcp_cid); return; } case AVRCP_SUBEVENT_CONNECTION_RELEASED: - printf("AVRCP Browsing Client: Channel released: browsing_cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); + printf("AVRCP Browsing Client: AVRCP Controller Channel released: avrcp_cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); browsing_cid = 0; avrcp_browsing_connected = 0; return; @@ -206,13 +204,13 @@ static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16 case AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED: { local_cid = avrcp_subevent_browsing_connection_established_get_browsing_cid(packet); if (browsing_cid != 0 && browsing_cid != local_cid) { - printf("AVRCP Browsing Client: Connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid); + printf("AVRCP Browsing Client: AVRCP Browsing Controller Connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid); return; } status = avrcp_subevent_browsing_connection_established_get_status(packet); if (status != ERROR_CODE_SUCCESS){ - printf("AVRCP Browsing Client: Connection failed: status 0x%02x\n", status); + printf("AVRCP Browsing Client: AVRCP Browsing Controller Connection failed: status 0x%02x\n", status); browsing_cid = 0; return; } @@ -220,11 +218,11 @@ static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16 browsing_cid = local_cid; avrcp_browsing_connected = 1; avrcp_subevent_browsing_connection_established_get_bd_addr(packet, adress); - printf("AVRCP Browsing Client: Channel successfully opened: %s, browsing_cid 0x%02x\n", bd_addr_to_str(adress), browsing_cid); + printf("AVRCP Browsing Client: AVRCP Browsing Controller Channel successfully opened: %s, browsing_cid 0x%02x\n", bd_addr_to_str(adress), browsing_cid); return; } case AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED: - printf("AVRCP Browsing Client: Channel released: browsing_cid 0x%02x\n", avrcp_subevent_browsing_connection_released_get_browsing_cid(packet)); + printf("AVRCP Browsing Client: AVRCP Browsing Controller Channel released: browsing_cid 0x%02x\n", avrcp_subevent_browsing_connection_released_get_browsing_cid(packet)); browsing_cid = 0; avrcp_browsing_connected = 0; return; @@ -239,8 +237,10 @@ static void show_usage(void){ bd_addr_t iut_address; gap_local_bd_addr(iut_address); printf("\n--- Bluetooth AVRCP Controller Connection Test Console %s ---\n", bd_addr_to_str(iut_address)); - printf("c - AVRCP create connection for browsing to addr %s\n", bd_addr_to_str(device_addr)); - printf("C - AVRCP disconnect\n"); + printf("a - AVRCP Controller create connection to addr %s\n", bd_addr_to_str(device_addr)); + printf("c - AVRCP Browsing Controller create connection to addr %s\n", bd_addr_to_str(device_addr)); + printf("C - AVRCP Browsing Controller disconnect\n"); + printf("A - AVRCP Controller disconnect\n"); printf("---\n"); } #endif @@ -251,20 +251,33 @@ static void stdin_process(char cmd){ switch (cmd){ case 'a': - printf(" - Create AVRCP connection for controll to addr %s.\n", bd_addr_to_str(device_addr)); + printf(" - Create AVRCP connection for control to addr %s.\n", bd_addr_to_str(device_addr)); status = avrcp_controller_connect(device_addr, &avrcp_cid); break; + case 'A': + if (avrcp_connected){ + printf(" - AVRCP Controller disconnect from addr %s.\n", bd_addr_to_str(device_addr)); + status = avrcp_controller_disconnect(avrcp_cid); + break; + } + printf("AVRCP Controller already disconnected\n"); + break; + case 'c': if (!avrcp_connected) { - printf(" You must first create AVRCP connection for controll to addr %s.\n", bd_addr_to_str(device_addr)); + printf(" You must first create AVRCP connection for control to addr %s.\n", bd_addr_to_str(device_addr)); break; } printf(" - Create AVRCP connection for browsing to addr %s.\n", bd_addr_to_str(device_addr)); status = avrcp_browsing_controller_connect(device_addr, ertm_buffer, sizeof(ertm_buffer), &ertm_config, &browsing_cid); break; case 'C': - printf(" - AVRCP disconnect from addr %s.\n", bd_addr_to_str(device_addr)); - status = avrcp_browsing_controller_disconnect(browsing_cid); + if (avrcp_browsing_connected){ + printf(" - AVRCP Browsing Controller disconnect from addr %s.\n", bd_addr_to_str(device_addr)); + status = avrcp_browsing_controller_disconnect(browsing_cid); + break; + } + printf("AVRCP Browsing Controller already disconnected\n"); break; case '\n': case '\r': diff --git a/src/classic/avrcp.c b/src/classic/avrcp.c index 11efb3a98..cc071949d 100644 --- a/src/classic/avrcp.c +++ b/src/classic/avrcp.c @@ -91,7 +91,6 @@ static avrcp_context_t * sdp_query_context; static uint8_t attribute_value[1000]; static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); -static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); static const char * avrcp_subunit_type_name[] = { "MONITOR", "AUDIO", "PRINTER", "DISC", "TAPE_RECORDER_PLAYER", "TUNER", @@ -318,7 +317,7 @@ void avrcp_create_sdp_record(uint8_t controller, uint8_t * service, uint32_t ser de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); } -static avrcp_connection_t * get_avrcp_connection_for_bd_addr(bd_addr_t addr, avrcp_context_t * context){ +avrcp_connection_t * get_avrcp_connection_for_bd_addr(bd_addr_t addr, avrcp_context_t * context){ btstack_linked_list_iterator_t it; btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); while (btstack_linked_list_iterator_has_next(&it)){ @@ -357,7 +356,7 @@ void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_ } -static uint16_t avrcp_get_next_cid(void){ +uint16_t avrcp_get_next_cid(void){ avrcp_cid_counter++; if (avrcp_cid_counter == 0){ avrcp_cid_counter = 1; @@ -408,10 +407,11 @@ void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t av (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); } -static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ +void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(sdp_query_context->avrcp_cid, sdp_query_context); if (!connection) return; - if (connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return; + if (connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return; + UNUSED(packet_type); UNUSED(channel); UNUSED(size); @@ -523,12 +523,12 @@ static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t c case BLUETOOTH_PROTOCOL_L2CAP: if (!des_iterator_has_more(&prot_it)) continue; des_iterator_next(&prot_it); - de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_browsing_l2cap_psm); + de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->browsing_l2cap_psm); break; case BLUETOOTH_PROTOCOL_AVCTP: if (!des_iterator_has_more(&prot_it)) continue; des_iterator_next(&prot_it); - de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_browsing_version); + de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->browsing_version); break; default: break; @@ -545,8 +545,9 @@ static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t c } break; - case SDP_EVENT_QUERY_COMPLETE: + case SDP_EVENT_QUERY_COMPLETE:{ status = sdp_event_query_complete_get_status(packet); + if (status != ERROR_CODE_SUCCESS){ avrcp_emit_connection_established(sdp_query_context->avrcp_callback, connection->avrcp_cid, connection->remote_addr, status); btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection); @@ -555,17 +556,18 @@ static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t c break; } - if (!sdp_query_context->parse_sdp_record || !sdp_query_context->avrcp_l2cap_psm){ + if (!sdp_query_context->parse_sdp_record){ connection->state = AVCTP_CONNECTION_IDLE; avrcp_emit_connection_established(sdp_query_context->avrcp_callback, connection->avrcp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND); btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection); btstack_memory_avrcp_connection_free(connection); break; } - // log_info("AVRCP Control PSM 0x%02x, Browsing PSM 0x%02x", sdp_query_context->avrcp_l2cap_psm, sdp_query_context->avrcp_browsing_l2cap_psm); + // log_info("AVRCP Control PSM 0x%02x, Browsing PSM 0x%02x", sdp_query_context->avrcp_l2cap_psm, sdp_query_context->browsing_l2cap_psm); connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avrcp_l2cap_psm, l2cap_max_mtu(), NULL); break; + } } } @@ -646,29 +648,24 @@ void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid){ avrcp_connection_t * connection = get_avrcp_connection_for_bd_addr(bd_addr, context); - printf("avrcp_connect 1 \n"); if (connection){ return ERROR_CODE_COMMAND_DISALLOWED; } connection = avrcp_create_connection(bd_addr, context); - printf("avrcp_connect 2 \n"); if (!connection){ log_error("avrcp: could not allocate connection struct."); return BTSTACK_MEMORY_ALLOC_FAILED; } - printf("avrcp_connect 3 \n"); if (!avrcp_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; - printf("avrcp_connect 4 \n"); *avrcp_cid = connection->avrcp_cid; connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE; - + context->avrcp_l2cap_psm = 0; context->avrcp_version = 0; context->avrcp_cid = connection->avrcp_cid; - + connection->browsing_l2cap_psm = 0; sdp_query_context = context; - return sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, bd_addr, BLUETOOTH_PROTOCOL_AVCTP); } diff --git a/src/classic/avrcp.h b/src/classic/avrcp.h index d19b2cb4e..e87a7f634 100644 --- a/src/classic/avrcp.h +++ b/src/classic/avrcp.h @@ -273,12 +273,31 @@ typedef enum { AVRCP_PARSER_IGNORE_ATTRIBUTE_VALUE } avrcp_parser_state_t; +// BROWSING +typedef struct { + avctp_connection_state_t state; + uint8_t wait_to_send; + uint8_t transaction_label; + + uint8_t * ertm_buffer; + uint32_t ertm_buffer_size; + l2cap_ertm_config_t ertm_config; +} avrcp_browsing_connection_t; +// BROWSING END + typedef struct { btstack_linked_item_t item; bd_addr_t remote_addr; uint16_t l2cap_signaling_cid; uint16_t avrcp_cid; + uint16_t l2cap_browsing_cid; + uint16_t avrcp_browsing_cid; + uint16_t browsing_l2cap_psm; + uint16_t browsing_version; + + avrcp_browsing_connection_t * browsing_connection; + avctp_connection_state_t state; uint8_t wait_to_send; @@ -375,33 +394,17 @@ typedef struct { btstack_packet_handler_t avrcp_callback; btstack_packet_handler_t packet_handler; + // btstack_packet_handler_t browsing_callback; + btstack_packet_handler_t browsing_packet_handler; + // SDP query uint8_t parse_sdp_record; uint32_t record_id; uint16_t avrcp_cid; uint16_t avrcp_l2cap_psm; uint16_t avrcp_version; - uint16_t avrcp_browsing_l2cap_psm; - uint16_t avrcp_browsing_version; } avrcp_context_t; -// BROWSING -typedef struct { - btstack_linked_item_t item; - bd_addr_t remote_addr; - uint16_t l2cap_browsing_cid; - uint16_t browsing_cid; - - avctp_connection_state_t state; - uint8_t wait_to_send; - uint8_t transaction_label; - - uint8_t * ertm_buffer; - uint32_t ertm_buffer_size; - l2cap_ertm_config_t ertm_config; -} avrcp_browsing_connection_t; - -// BROWSING END const char * avrcp_subunit2str(uint16_t index); const char * avrcp_event2str(uint16_t index); @@ -422,7 +425,12 @@ void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t av uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size); avrcp_connection_t * get_avrcp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid, avrcp_context_t * context); avrcp_connection_t * get_avrcp_connection_for_avrcp_cid(uint16_t avrcp_cid, avrcp_context_t * context); +avrcp_connection_t * get_avrcp_connection_for_bd_addr(bd_addr_t addr, avrcp_context_t * context); void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid); +uint16_t avrcp_get_next_cid(void); + +// SDP query +void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); #if defined __cplusplus } diff --git a/src/classic/avrcp_browsing_controller.c b/src/classic/avrcp_browsing_controller.c index e5629805b..712405265 100644 --- a/src/classic/avrcp_browsing_controller.c +++ b/src/classic/avrcp_browsing_controller.c @@ -46,58 +46,43 @@ #include "classic/avrcp.h" #include "classic/avrcp_browsing_controller.h" -static avrcp_context_t avrcp_browsing_controller_context; -static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); -static uint16_t avrcp_cid_counter = 0; - -static avrcp_context_t * sdp_query_context; -static uint8_t attribute_value[1000]; -static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); - void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context); +static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); -static uint16_t avrcp_get_next_cid(void){ - avrcp_cid_counter++; - if (avrcp_cid_counter == 0){ - avrcp_cid_counter = 1; +static avrcp_connection_t * get_avrcp_connection_for_browsing_cid(uint16_t browsing_cid, avrcp_context_t * context){ + btstack_linked_list_iterator_t it; + btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); + while (btstack_linked_list_iterator_has_next(&it)){ + avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); + if (connection->avrcp_browsing_cid != browsing_cid) continue; + return connection; } - return avrcp_cid_counter; + return NULL; +} + +static avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid(uint16_t browsing_l2cap_cid, avrcp_context_t * context){ + btstack_linked_list_iterator_t it; + btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); + while (btstack_linked_list_iterator_has_next(&it)){ + avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); + if (connection->l2cap_browsing_cid != browsing_l2cap_cid) continue; + return connection; + } + return NULL; } static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid(uint16_t l2cap_cid, avrcp_context_t * context){ btstack_linked_list_iterator_t it; btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); while (btstack_linked_list_iterator_has_next(&it)){ - avrcp_browsing_connection_t * connection = (avrcp_browsing_connection_t *)btstack_linked_list_iterator_next(&it); + avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); if (connection->l2cap_browsing_cid != l2cap_cid) continue; - return connection; + return connection->browsing_connection; } return NULL; } -static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_cid(uint16_t avrcp_cid, avrcp_context_t * context){ - btstack_linked_list_iterator_t it; - btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); - while (btstack_linked_list_iterator_has_next(&it)){ - avrcp_browsing_connection_t * connection = (avrcp_browsing_connection_t *)btstack_linked_list_iterator_next(&it); - if (connection->browsing_cid != avrcp_cid) continue; - return connection; - } - return NULL; -} - -static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_bd_addr(bd_addr_t addr, avrcp_context_t * context){ - btstack_linked_list_iterator_t it; - btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); - while (btstack_linked_list_iterator_has_next(&it)){ - avrcp_browsing_connection_t * connection = (avrcp_browsing_connection_t *)btstack_linked_list_iterator_next(&it); - if (memcmp(addr, connection->remote_addr, 6) != 0) continue; - return connection; - } - return NULL; -} - -static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t avrcp_cid, bd_addr_t addr, uint8_t status){ +static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){ if (!callback) return; uint8_t event[12]; int pos = 0; @@ -107,239 +92,62 @@ static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t event[pos++] = status; reverse_bd_addr(addr,&event[pos]); pos += 6; - little_endian_store_16(event, pos, avrcp_cid); + little_endian_store_16(event, pos, browsing_cid); pos += 2; (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); } -static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t avrcp_cid){ +static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){ if (!callback) return; uint8_t event[5]; int pos = 0; event[pos++] = HCI_EVENT_AVRCP_META; event[pos++] = sizeof(event) - 2; event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED; - little_endian_store_16(event, pos, avrcp_cid); + little_endian_store_16(event, pos, browsing_cid); pos += 2; (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); } -static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ - avrcp_browsing_connection_t * connection = get_avrcp_browsing_connection_for_cid(sdp_query_context->avrcp_cid, sdp_query_context); - if (!connection) return; - if (connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return; - UNUSED(packet_type); - UNUSED(channel); - UNUSED(size); - uint8_t status; - des_iterator_t des_list_it; - des_iterator_t prot_it; - // uint32_t avdtp_remote_uuid = 0; - - switch (hci_event_packet_get_type(packet)){ - case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: - // Handle new SDP record - if (sdp_event_query_attribute_byte_get_record_id(packet) != sdp_query_context->record_id) { - sdp_query_context->record_id = sdp_event_query_attribute_byte_get_record_id(packet); - sdp_query_context->parse_sdp_record = 0; - printf("SDP Record: Nr: %d\n", sdp_query_context->record_id); - } - - if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { - attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); - - if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { - switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { - case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: - if (de_get_element_type(attribute_value) != DE_DES) break; - for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { - uint8_t * element = des_iterator_get_element(&des_list_it); - if (de_get_element_type(element) != DE_UUID) continue; - uint32_t uuid = de_get_uuid32(element); - switch (uuid){ - case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET: - if (sdp_query_context->role == AVRCP_CONTROLLER) { - sdp_query_context->parse_sdp_record = 1; - printf(" Controller \n"); - break; - } - break; - case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL: - case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER: - if (sdp_query_context->role == AVRCP_TARGET) { - printf(" Target \n"); - sdp_query_context->parse_sdp_record = 1; - break; - } - break; - default: - printf(" not found\n"); - break; - } - } - break; - - case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: { - if (!sdp_query_context->parse_sdp_record) break; - // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); - for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { - uint8_t *des_element; - uint8_t *element; - uint32_t uuid; - - if (des_iterator_get_type(&des_list_it) != DE_DES) continue; - - des_element = des_iterator_get_element(&des_list_it); - des_iterator_init(&prot_it, des_element); - element = des_iterator_get_element(&prot_it); - - if (de_get_element_type(element) != DE_UUID) continue; - - uuid = de_get_uuid32(element); - switch (uuid){ - case BLUETOOTH_PROTOCOL_L2CAP: - if (!des_iterator_has_more(&prot_it)) continue; - des_iterator_next(&prot_it); - de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_l2cap_psm); - printf(" found signaling PSM: 0x%02x\n", sdp_query_context->avrcp_l2cap_psm); - break; - case BLUETOOTH_PROTOCOL_AVCTP: - if (!des_iterator_has_more(&prot_it)) continue; - des_iterator_next(&prot_it); - de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_version); - break; - default: - break; - } - } - } - break; - case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: { - // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); - if (!sdp_query_context->parse_sdp_record) break; - if (de_get_element_type(attribute_value) != DE_DES) break; - - des_iterator_t des_list_0_it; - uint8_t *element_0; - - des_iterator_init(&des_list_0_it, attribute_value); - element_0 = des_iterator_get_element(&des_list_0_it); - - for (des_iterator_init(&des_list_it, element_0); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { - uint8_t *des_element; - uint8_t *element; - uint32_t uuid; - - if (des_iterator_get_type(&des_list_it) != DE_DES) continue; - - des_element = des_iterator_get_element(&des_list_it); - des_iterator_init(&prot_it, des_element); - element = des_iterator_get_element(&prot_it); - - if (de_get_element_type(element) != DE_UUID) continue; - - uuid = de_get_uuid32(element); - switch (uuid){ - case BLUETOOTH_PROTOCOL_L2CAP: - if (!des_iterator_has_more(&prot_it)) continue; - des_iterator_next(&prot_it); - de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_browsing_l2cap_psm); - printf(" found browsing PSM: 0x%02x\n", sdp_query_context->avrcp_browsing_l2cap_psm); - break; - case BLUETOOTH_PROTOCOL_AVCTP: - if (!des_iterator_has_more(&prot_it)) continue; - des_iterator_next(&prot_it); - de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_browsing_version); - break; - default: - break; - } - } - } - break; - default: - break; - } - } - } else { - log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); - } - break; - - case SDP_EVENT_QUERY_COMPLETE: - status = sdp_event_query_complete_get_status(packet); - if (status != ERROR_CODE_SUCCESS){ - avrcp_emit_browsing_connection_established(sdp_query_context->avrcp_callback, connection->browsing_cid, connection->remote_addr, status); - btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection); - btstack_memory_avrcp_browsing_connection_free(connection); - log_info("AVRCP: SDP query failed with status 0x%02x.", status); - break; - } - - if (!sdp_query_context->parse_sdp_record || !sdp_query_context->avrcp_browsing_l2cap_psm){ - connection->state = AVCTP_CONNECTION_IDLE; - avrcp_emit_browsing_connection_established(sdp_query_context->avrcp_callback, connection->browsing_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND); - btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection); - btstack_memory_avrcp_browsing_connection_free(connection); - break; - } - // log_info("AVRCP Control PSM 0x%02x, Browsing PSM 0x%02x", sdp_query_context->avrcp_l2cap_psm, sdp_query_context->avrcp_browsing_l2cap_psm); - connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; - - l2cap_create_ertm_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avrcp_browsing_l2cap_psm, - &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL); - - // l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avrcp_l2cap_psm, l2cap_max_mtu(), NULL); - break; - } -} - - -static avrcp_browsing_connection_t * avrcp_browsing_create_connection(bd_addr_t remote_addr, avrcp_context_t * context){ - avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get(); - if (!connection){ - log_error("avrcp: not enough memory to create connection"); - return NULL; - } +static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection){ + avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get(); memset(connection, 0, sizeof(avrcp_browsing_connection_t)); connection->state = AVCTP_CONNECTION_IDLE; connection->transaction_label = 0xFF; - connection->browsing_cid = avrcp_get_next_cid(); - memcpy(connection->remote_addr, remote_addr, 6); - btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection); + avrcp_connection->avrcp_browsing_cid = avrcp_get_next_cid(); + avrcp_connection->browsing_connection = connection; return connection; } -static uint8_t avrcp_browsing_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_cid){ - avrcp_browsing_connection_t * connection = get_avrcp_browsing_connection_for_bd_addr(bd_addr, context); - if (connection){ +static uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, avrcp_context_t * context, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * browsing_cid){ + avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_bd_addr(remote_addr, context); + if (!avrcp_connection){ + log_error("avrcp: there is no previously established AVRCP controller connection."); return ERROR_CODE_COMMAND_DISALLOWED; } - connection = avrcp_browsing_create_connection(bd_addr, context); + avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; + if (connection){ + return ERROR_CODE_SUCCESS; + } + + connection = avrcp_browsing_create_connection(avrcp_connection); if (!connection){ printf("avrcp: could not allocate connection struct."); return BTSTACK_MEMORY_ALLOC_FAILED; } - // if (!avrcp_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; + if (!browsing_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; - *avrcp_cid = connection->browsing_cid; - connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE; + *browsing_cid = avrcp_connection->avrcp_browsing_cid; connection->ertm_buffer = ertm_buffer; connection->ertm_buffer_size = size; + avrcp_connection->browsing_connection = connection; + memcpy(&connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t)); - context->parse_sdp_record = 0; - context->record_id = 0; - context->avrcp_l2cap_psm = 0; - context->avrcp_version = 0; - context->avrcp_browsing_l2cap_psm = 0; - context->avrcp_browsing_version = 0; + return l2cap_create_ertm_channel(avrcp_browsing_controller_packet_handler, remote_addr, avrcp_connection->browsing_l2cap_psm, + &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL); - context->avrcp_cid = connection->browsing_cid; - sdp_query_context = context; - printf(" start SDP query\n"); - return sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, bd_addr, BLUETOOTH_PROTOCOL_AVCTP); } void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){ @@ -349,6 +157,7 @@ void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t uint16_t local_cid; uint8_t status; avrcp_browsing_connection_t * connection = NULL; + avrcp_connection_t * avrcp_connection = NULL; if (packet_type != HCI_EVENT_PACKET) return; @@ -359,15 +168,17 @@ void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t case L2CAP_EVENT_INCOMING_CONNECTION: l2cap_event_incoming_connection_get_address(packet, event_addr); local_cid = l2cap_event_incoming_connection_get_local_cid(packet); - connection = avrcp_browsing_create_connection(event_addr, context); - if (!connection) { - log_error("Failed to alloc connection structure"); + avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); + if (!avrcp_connection) { + log_error("No previously created AVRCP controller connections"); l2cap_decline_connection(local_cid); break; } + connection = avrcp_browsing_create_connection(avrcp_connection); + avrcp_connection->browsing_connection = connection; + avrcp_connection->l2cap_browsing_cid = local_cid; connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; - connection->l2cap_browsing_cid = local_cid; - log_info("L2CAP_EVENT_INCOMING_CONNECTION avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x", connection->browsing_cid, connection->l2cap_browsing_cid); + log_info("L2CAP_EVENT_INCOMING_CONNECTION browsing_cid 0x%02x, l2cap_signaling_cid 0x%02x", avrcp_connection->avrcp_browsing_cid, avrcp_connection->l2cap_browsing_cid); // l2cap_accept_connection(local_cid); printf("L2CAP Accepting incoming connection request in ERTM\n"); l2cap_accept_ertm_connection(local_cid, &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size); @@ -378,36 +189,46 @@ void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t status = l2cap_event_channel_opened_get_status(packet); local_cid = l2cap_event_channel_opened_get_local_cid(packet); - connection = get_avrcp_browsing_connection_for_bd_addr(event_addr, context); - if (!connection){ - log_error("Failed to alloc AVRCP connection structure"); - avrcp_emit_browsing_connection_established(context->avrcp_callback, connection->browsing_cid, event_addr, BTSTACK_MEMORY_ALLOC_FAILED); + avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); + if (!avrcp_connection){ + log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr)); + avrcp_emit_browsing_connection_established(context->avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST); l2cap_disconnect(local_cid, 0); // reason isn't used break; } - if (status != ERROR_CODE_SUCCESS){ - log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); - avrcp_emit_browsing_connection_established(context->avrcp_callback, connection->browsing_cid, event_addr, status); - btstack_linked_list_remove(&context->connections, (btstack_linked_item_t*) connection); - btstack_memory_avrcp_browsing_connection_free(connection); + + connection = avrcp_connection->browsing_connection; + if (!connection){ + log_error("Failed to alloc AVRCP connection structure"); + avrcp_emit_browsing_connection_established(context->avrcp_callback, local_cid, event_addr, BTSTACK_MEMORY_ALLOC_FAILED); + l2cap_disconnect(local_cid, 0); // reason isn't used break; } - connection->l2cap_browsing_cid = local_cid; - log_info("L2CAP_EVENT_CHANNEL_OPENED avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x", connection->browsing_cid, connection->l2cap_browsing_cid); + if (status != ERROR_CODE_SUCCESS){ + log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); + avrcp_emit_browsing_connection_established(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status); + btstack_memory_avrcp_browsing_connection_free(connection); + avrcp_connection->browsing_connection = NULL; + break; + } + avrcp_connection->l2cap_browsing_cid = local_cid; + + log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, avrcp_connection->l2cap_browsing_cid); connection->state = AVCTP_CONNECTION_OPENED; - avrcp_emit_browsing_connection_established(context->avrcp_callback, connection->browsing_cid, event_addr, ERROR_CODE_SUCCESS); + avrcp_emit_browsing_connection_established(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS); break; case L2CAP_EVENT_CHANNEL_CLOSED: // data: event (8), len(8), channel (16) local_cid = l2cap_event_channel_closed_get_local_cid(packet); - connection = get_avrcp_browsing_connection_for_l2cap_cid(local_cid, context); - if (connection){ - avrcp_emit_browsing_connection_closed(context->avrcp_callback, connection->browsing_cid); + avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid(local_cid, context); + + if (avrcp_connection && avrcp_connection->browsing_connection){ + avrcp_emit_browsing_connection_closed(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid); // free connection - btstack_linked_list_remove(&context->connections, (btstack_linked_item_t*) connection); - btstack_memory_avrcp_browsing_connection_free(connection); + btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection); + avrcp_connection->browsing_connection = NULL; break; } break; @@ -435,19 +256,19 @@ static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16 switch (packet_type) { case L2CAP_DATA_PACKET: - connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_browsing_controller_context); + connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context); if (!connection) break; // avrcp_handle_l2cap_data_packet_for_browsing_connection(connection, packet, size); break; case HCI_EVENT_PACKET: switch (hci_event_packet_get_type(packet)){ case L2CAP_EVENT_CAN_SEND_NOW: - connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_browsing_controller_context); + connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context); if (!connection) break; // avrcp_browsing_controller_handle_can_send_now(connection); break; default: - avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_browsing_controller_context); + avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); break; } default: @@ -456,31 +277,22 @@ static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16 } void avrcp_browsing_controller_init(void){ - avrcp_browsing_controller_context.role = AVRCP_CONTROLLER; - avrcp_browsing_controller_context.connections = NULL; - avrcp_browsing_controller_context.packet_handler = avrcp_browsing_controller_packet_handler; + avrcp_controller_context.browsing_packet_handler = avrcp_browsing_controller_packet_handler; l2cap_register_service(&avrcp_browsing_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0); } -void avrcp_browsing_controller_register_packet_handler(btstack_packet_handler_t callback){ - if (callback == NULL){ - log_error("avrcp_browsing_controller_register_packet_handler called with NULL callback"); - return; - } - avrcp_browsing_controller_context.avrcp_callback = callback; -} - uint8_t avrcp_browsing_controller_connect(bd_addr_t bd_addr, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){ - return avrcp_browsing_connect(bd_addr, &avrcp_browsing_controller_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid); + return avrcp_browsing_connect(bd_addr, &avrcp_controller_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid); } uint8_t avrcp_browsing_controller_disconnect(uint16_t avrcp_browsing_cid){ - avrcp_browsing_connection_t * connection = get_avrcp_browsing_connection_for_cid(avrcp_browsing_cid, &avrcp_browsing_controller_context); - if (!connection){ + avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context); + if (!avrcp_connection){ log_error("avrcp_browsing_controller_disconnect: could not find a connection."); return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; } - if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; - l2cap_disconnect(connection->l2cap_browsing_cid, 0); + if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; + + l2cap_disconnect(avrcp_connection->l2cap_browsing_cid, 0); return ERROR_CODE_SUCCESS; } diff --git a/src/classic/avrcp_browsing_controller.h b/src/classic/avrcp_browsing_controller.h index c9210ed72..fd8db2e27 100644 --- a/src/classic/avrcp_browsing_controller.h +++ b/src/classic/avrcp_browsing_controller.h @@ -59,12 +59,6 @@ extern "C" { */ void avrcp_browsing_controller_init(void); -/** - * @brief Register callback for the AVRCP Sink client. - * @param callback - */ -void avrcp_browsing_controller_register_packet_handler(btstack_packet_handler_t callback); - /** * @brief Connect to device with a Bluetooth address. * @param bd_addr diff --git a/src/classic/avrcp_controller.c b/src/classic/avrcp_controller.c index 2b6d88b04..2d5521566 100644 --- a/src/classic/avrcp_controller.c +++ b/src/classic/avrcp_controller.c @@ -47,8 +47,6 @@ #include "classic/avrcp.h" #include "classic/avrcp_controller.h" -static avrcp_context_t avrcp_controller_context; - void avrcp_controller_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name){ avrcp_create_sdp_record(1, service, service_record_handle, browsing, supported_features, service_name, service_provider_name); } diff --git a/src/classic/avrcp_controller.h b/src/classic/avrcp_controller.h index ea7d8610f..b613867d3 100644 --- a/src/classic/avrcp_controller.h +++ b/src/classic/avrcp_controller.h @@ -55,7 +55,6 @@ extern "C" { #endif /* API_START */ - /** * @brief AVRCP Controller service record. * @param service @@ -278,8 +277,11 @@ uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode * @returns status */ uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode); - /* API_END */ + +// Only used by AVRCP controller and AVRCP browsing controller +avrcp_context_t avrcp_controller_context; + #if defined __cplusplus } #endif