avrcp: move browsing code to avrcp_browsing.c

This commit is contained in:
Milanka Ringwald 2020-06-16 14:59:19 +02:00 committed by Matthias Ringwald
parent 5634a0d9ba
commit 665a00cb2a
9 changed files with 737 additions and 528 deletions

View File

@ -96,6 +96,7 @@ MBEDTLS = \
memory_buffer_alloc.c \
platform.c \
LWIP_CORE_SRC = init.c mem.c memp.c netif.c udp.c ip.c pbuf.c inet_chksum.c def.c tcp.c tcp_in.c tcp_out.c timeouts.c sys_arch.c
LWIP_IPV4_SRC = acd.c dhcp.c etharp.c icmp.c ip4.c ip4_frag.c ip4_addr.c
LWIP_NETIF_SRC = ethernet.c
@ -128,6 +129,15 @@ AVDTP += \
a2dp_sink.c \
btstack_ring_buffer.c \
AVRCP += \
avrcp.c \
avrcp_controller.c \
avrcp_target.c \
avrcp_browsing.c \
avrcp_browsing_controller.c \
avrcp_browsing_target.c \
avrcp_media_item_iterator.c \
HXCMOD_PLAYER = \
hxcmod.c \
nao-deceased_by_disease.c \
@ -269,6 +279,7 @@ SBC_DECODER_OBJ = $(SBC_DECODER:.c=.o)
SBC_ENCODER_OBJ = $(SBC_ENCODER:.c=.o)
CVSD_PLC_OBJ = $(CVSD_PLC:.c=.o)
AVDTP_OBJ = $(AVDTP:.c=.o)
AVRCP_OBJ = $(AVRCP:.c=.o)
HXCMOD_PLAYER_OBJ = $(HXCMOD_PLAYER:.c=.o)
LWIP_OBJ = $(LWIP_SRC:.c=.o)
MESH_OBJ = $(MESH:.c=.o)
@ -403,7 +414,7 @@ a2dp_source_demo: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} ${SBC_E
a2dp_sink_demo: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} ${SBC_DECODER_OBJ} ${AVDTP_OBJ} avrcp.o avrcp_controller.o avrcp_target.o btstack_resample.o a2dp_sink_demo.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
avrcp_browsing_client: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} avrcp.o avrcp_controller.o avrcp_target.o avrcp_browsing_controller.o avrcp_browsing_target.o avrcp_media_item_iterator.o avrcp_browsing_client.c
avrcp_browsing_client: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} ${AVRCP_OBJ} avrcp_browsing_client.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
dut_mode_classic: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} dut_mode_classic.c

View File

@ -106,6 +106,7 @@
#include "classic/avdtp_source.h"
#include "classic/avdtp_util.h"
#include "classic/avrcp.h"
#include "classic/avrcp_browsing.h"
#include "classic/avrcp_browsing_controller.h"
#include "classic/avrcp_browsing_target.h"
#include "classic/avrcp_controller.h"

View File

@ -48,12 +48,8 @@
#include "classic/sdp_client.h"
#include "classic/sdp_util.h"
#include "classic/avrcp.h"
#include "classic/avrcp_controller.h"
#define PSM_AVCTP_BROWSING 0x001b
static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static const char * default_avrcp_controller_service_name = "BTstack AVRCP Controller Service";
static const char * default_avrcp_controller_service_provider_name = "BTstack AVRCP Controller Service Provider";
@ -66,7 +62,6 @@ static avrcp_context_t * sdp_query_context;
static avrcp_context_t avrcp_context;
static btstack_packet_handler_t avrcp_callback;
static btstack_packet_handler_t avrcp_browsing_callback;
static uint8_t attribute_value[45];
static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
@ -76,10 +71,6 @@ static btstack_packet_handler_t avrcp_controller_packet_handler;
static btstack_packet_handler_t avrcp_target_packet_handler;
static bool l2cap_service_registered = false;
static bool l2cap_browsing_service_registered = false;
static btstack_packet_handler_t avrcp_browsing_controller_packet_handler;
static btstack_packet_handler_t avrcp_browsing_target_packet_handler;
static const char * avrcp_subunit_type_name[] = {
"MONITOR", "AUDIO", "PRINTER", "DISC", "TAPE_RECORDER_PLAYER", "TUNER",
"CA", "CAMERA", "RESERVED", "PANEL", "BULLETIN_BOARD", "CAMERA_STORAGE",
@ -342,52 +333,11 @@ avrcp_connection_t * get_avrcp_connection_for_avrcp_cid_for_role(avrcp_role_t ro
return NULL;
}
avrcp_connection_t * get_avrcp_connection_for_browsing_cid_for_role(avrcp_role_t role, uint16_t browsing_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
while (btstack_linked_list_iterator_has_next(&it)){
avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
if (connection->role != role) continue;
if (connection->avrcp_browsing_cid != browsing_cid) continue;
return connection;
}
return NULL;
}
avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid_for_role(avrcp_role_t role, uint16_t browsing_l2cap_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
while (btstack_linked_list_iterator_has_next(&it)){
avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
if (connection->role != role) continue;
if (connection->browsing_connection && (connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid)) continue;
return connection;
}
return NULL;
}
avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
while (btstack_linked_list_iterator_has_next(&it)){
avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
if (connection->role != role) continue;
if (connection->browsing_connection && (connection->browsing_connection->l2cap_browsing_cid != l2cap_cid)) continue;
return connection->browsing_connection;
}
return NULL;
}
void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid){
connection->wait_to_send = true;
l2cap_request_can_send_now_event(l2cap_cid);
}
void avrcp_browsing_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid){
connection->wait_to_send = true;
l2cap_request_can_send_now_event(l2cap_cid);
}
uint16_t avrcp_get_next_cid(avrcp_role_t role){
do {
if (avrcp_cid_counter == 0xffff) {
@ -652,20 +602,6 @@ static void avrcp_handle_open_connection_for_role( avrcp_connection_t * connecti
log_info("L2CAP_EVENT_CHANNEL_OPENED avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x, role %d", connection->avrcp_cid, connection->l2cap_signaling_cid, connection->role);
}
static void avrcp_browsing_reconnect_handler(avrcp_connection_t * connection_controller, avrcp_connection_t * connection_target){
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) return;
if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W2_L2CAP_RECONNECT){
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
l2cap_create_ertm_channel(avrcp_browsing_packet_handler, connection_controller->remote_addr, connection_controller->browsing_l2cap_psm,
&connection_controller->browsing_connection->ertm_config,
connection_controller->browsing_connection->ertm_buffer,
connection_controller->browsing_connection->ertm_buffer_size, NULL);
}
}
static void avrcp_reconnect_timer_timeout_handler(btstack_timer_source_t * timer){
uint16_t avrcp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
avrcp_connection_t * connection_controller = get_avrcp_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
@ -677,9 +613,7 @@ static void avrcp_reconnect_timer_timeout_handler(btstack_timer_source_t * timer
connection_controller->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
connection_target->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
l2cap_create_channel(&avrcp_packet_handler, connection_controller->remote_addr, connection_controller->avrcp_l2cap_psm, l2cap_max_mtu(), NULL);
} else {
avrcp_browsing_reconnect_handler(connection_controller, connection_target);
}
}
}
static void avrcp_reconnect_timer_start(avrcp_connection_t * connection){
@ -948,430 +882,3 @@ void avrcp_register_packet_handler(btstack_packet_handler_t callback){
btstack_assert(callback != NULL);
avrcp_callback = callback;
}
// AVRCP Browsing Service functions
static void avrcp_browsing_finalize_connection(avrcp_connection_t * connection){
btstack_run_loop_remove_timer(&connection->reconnect_timer);
btstack_memory_avrcp_browsing_connection_free(connection->browsing_connection);
connection->browsing_connection = NULL;
}
static void avrcp_browsing_emit_connection_established(uint16_t browsing_cid, bd_addr_t addr, uint8_t status){
btstack_assert(avrcp_browsing_callback != NULL);
uint8_t event[12];
int pos = 0;
event[pos++] = HCI_EVENT_AVRCP_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED;
event[pos++] = status;
reverse_bd_addr(addr,&event[pos]);
pos += 6;
little_endian_store_16(event, pos, browsing_cid);
pos += 2;
(*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}
static void avrcp_browsing_emit_incoming_connection(uint16_t browsing_cid, bd_addr_t addr){
btstack_assert(avrcp_browsing_callback != NULL);
uint8_t event[11];
int pos = 0;
event[pos++] = HCI_EVENT_AVRCP_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION;
reverse_bd_addr(addr,&event[pos]);
pos += 6;
little_endian_store_16(event, pos, browsing_cid);
pos += 2;
(*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}
static void avrcp_browsing_emit_connection_closed(uint16_t browsing_cid){
btstack_assert(avrcp_browsing_callback != NULL);
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, browsing_cid);
pos += 2;
(*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}
static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection, uint16_t avrcp_browsing_cid){
avrcp_browsing_connection_t * browsing_connection = btstack_memory_avrcp_browsing_connection_get();
if (!browsing_connection){
log_error("Not enough memory to create browsing connection");
return NULL;
}
browsing_connection->state = AVCTP_CONNECTION_IDLE;
browsing_connection->transaction_label = 0xFF;
avrcp_connection->avrcp_browsing_cid = avrcp_browsing_cid;
avrcp_connection->browsing_connection = browsing_connection;
log_info("avrcp_browsing_create_connection, avrcp cid 0x%02x", avrcp_connection->avrcp_browsing_cid);
return browsing_connection;
}
static void avrcp_browsing_configure_ertm(avrcp_browsing_connection_t * browsing_connection, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){
browsing_connection->ertm_buffer = ertm_buffer;
browsing_connection->ertm_buffer_size = ertm_buffer_size;
if (ertm_buffer_size > 0) {
(void)memcpy(&browsing_connection->ertm_config, ertm_config,
sizeof(l2cap_ertm_config_t));
log_info("avrcp_browsing_configure_ertm");
}
}
static avrcp_browsing_connection_t * avrcp_browsing_handle_incoming_connection(avrcp_connection_t * connection, uint16_t local_cid, uint16_t avrcp_browsing_cid){
if (connection->browsing_connection == NULL){
avrcp_browsing_create_connection(connection, avrcp_browsing_cid);
}
if (connection->browsing_connection) {
connection->browsing_connection->l2cap_browsing_cid = local_cid;
connection->browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION;
btstack_run_loop_remove_timer(&connection->reconnect_timer);
}
return connection->browsing_connection;
}
static void avrcp_browsing_handle_open_connection_for_role(avrcp_connection_t * connection, uint16_t local_cid){
connection->browsing_connection->l2cap_browsing_cid = local_cid;
connection->browsing_connection->incoming_declined = false;
connection->browsing_connection->state = AVCTP_CONNECTION_OPENED;
log_info("L2CAP_EVENT_CHANNEL_OPENED browsing_avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x, role %d", connection->avrcp_cid, connection->l2cap_signaling_cid, connection->role);
}
static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
bd_addr_t event_addr;
uint16_t local_cid;
uint8_t status;
bool decline_connection;
bool outoing_active;
avrcp_connection_t * connection_controller;
avrcp_connection_t * connection_target;
switch (packet_type){
case L2CAP_DATA_PACKET:
switch (avrcp_get_frame_type(packet[0])){
case AVRCP_RESPONSE_FRAME:
(*avrcp_browsing_controller_packet_handler)(packet_type, channel, packet, size);
break;
case AVRCP_COMMAND_FRAME:
default: // make compiler happy
(*avrcp_browsing_target_packet_handler)(packet_type, channel, packet, size);
break;
}
break;
case HCI_EVENT_PACKET:
btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
btstack_assert(avrcp_browsing_target_packet_handler != NULL);
switch (hci_event_packet_get_type(packet)) {
case L2CAP_EVENT_INCOMING_CONNECTION:
btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
btstack_assert(avrcp_browsing_target_packet_handler != NULL);
l2cap_event_incoming_connection_get_address(packet, event_addr);
local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
outoing_active = false;
connection_target = get_avrcp_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
connection_controller = get_avrcp_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
if (connection_target == NULL || connection_controller == NULL) {
l2cap_decline_connection(local_cid);
return;
}
if (connection_target->browsing_connection != NULL){
if (connection_target->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED){
outoing_active = true;
connection_target->browsing_connection->incoming_declined = true;
}
}
if (connection_controller->browsing_connection != NULL){
if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED) {
outoing_active = true;
connection_controller->browsing_connection->incoming_declined = true;
}
}
decline_connection = outoing_active;
if (decline_connection == false){
uint16_t avrcp_browsing_cid;
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)){
avrcp_browsing_cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
} else {
avrcp_browsing_cid = connection_controller->avrcp_browsing_cid;
}
// create two connection objects (both)
connection_target->browsing_connection = avrcp_browsing_handle_incoming_connection(connection_target, local_cid, avrcp_browsing_cid);
connection_controller->browsing_connection = avrcp_browsing_handle_incoming_connection(connection_controller, local_cid, avrcp_browsing_cid);
if ((connection_target->browsing_connection == NULL) || (connection_controller->browsing_connection == NULL)){
decline_connection = true;
if (connection_target->browsing_connection) {
avrcp_browsing_finalize_connection(connection_target);
}
if (connection_controller->browsing_connection) {
avrcp_browsing_finalize_connection(connection_controller);
}
}
}
if (decline_connection){
l2cap_decline_connection(local_cid);
} else {
log_info("AVRCP: L2CAP_EVENT_INCOMING_CONNECTION browsing_avrcp_cid 0x%02x", connection_controller->avrcp_browsing_cid);
avrcp_browsing_emit_incoming_connection(connection_controller->avrcp_browsing_cid, event_addr);
}
break;
case L2CAP_EVENT_CHANNEL_OPENED:
l2cap_event_channel_opened_get_address(packet, event_addr);
status = l2cap_event_channel_opened_get_status(packet);
local_cid = l2cap_event_channel_opened_get_local_cid(packet);
connection_controller = get_avrcp_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
connection_target = get_avrcp_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
// incoming: structs are already created in L2CAP_EVENT_INCOMING_CONNECTION
// outgoing: structs are cteated in avrcp_connect() and avrcp_browsing_connect()
if ((connection_controller == NULL) || (connection_target == NULL)) {
break;
}
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) {
break;
}
switch (status){
case ERROR_CODE_SUCCESS:
avrcp_browsing_handle_open_connection_for_role(connection_target, local_cid);
avrcp_browsing_handle_open_connection_for_role(connection_controller, local_cid);
avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status);
return;
case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES:
if (connection_controller->browsing_connection->incoming_declined == true){
log_info("Incoming browsing connection was declined, and the outgoing failed");
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RECONNECT;
connection_controller->browsing_connection->incoming_declined = false;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RECONNECT;
connection_target->browsing_connection->incoming_declined = false;
avrcp_reconnect_timer_start(connection_controller);
return;
}
break;
default:
break;
}
log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status);
avrcp_browsing_finalize_connection(connection_controller);
avrcp_browsing_finalize_connection(connection_target);
break;
case L2CAP_EVENT_CHANNEL_CLOSED:
local_cid = l2cap_event_channel_closed_get_local_cid(packet);
connection_controller = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid);
connection_target = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid);
if ((connection_controller == NULL) || (connection_target == NULL)) {
break;
}
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) {
break;
}
avrcp_browsing_emit_connection_closed(connection_controller->avrcp_browsing_cid);
avrcp_browsing_finalize_connection(connection_controller);
avrcp_browsing_finalize_connection(connection_target);
break;
case L2CAP_EVENT_CAN_SEND_NOW:
local_cid = l2cap_event_can_send_now_get_local_cid(packet);
connection_target = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid);
if ((connection_target != NULL) && (connection_target->browsing_connection != NULL) && connection_target->browsing_connection->wait_to_send) {
connection_target->browsing_connection->wait_to_send = false;
(*avrcp_browsing_target_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
break;
}
connection_controller = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid);
if ((connection_controller != NULL) && (connection_controller->browsing_connection != NULL) && connection_controller->browsing_connection->wait_to_send) {
connection_controller->browsing_connection->wait_to_send = false;
(*avrcp_browsing_controller_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
break;
}
break;
default:
break;
}
break;
default:
break;
}
}
void avrcp_browsing_init(void){
if (l2cap_browsing_service_registered) return;
int status = l2cap_register_service(&avrcp_browsing_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_2);
if (status != ERROR_CODE_SUCCESS) return;
l2cap_browsing_service_registered = true;
}
uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){
btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
btstack_assert(avrcp_browsing_target_packet_handler != NULL);
avrcp_connection_t * connection_controller = get_avrcp_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, remote_addr);
if (!connection_controller){
return ERROR_CODE_COMMAND_DISALLOWED;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_bd_addr_for_role(AVRCP_TARGET, remote_addr);
if (!connection_target){
return ERROR_CODE_COMMAND_DISALLOWED;
}
if (connection_controller->browsing_connection){
return ERROR_CODE_COMMAND_DISALLOWED;
}
if (connection_target->browsing_connection){
return ERROR_CODE_COMMAND_DISALLOWED;
}
uint16_t cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
connection_controller->browsing_connection = avrcp_browsing_create_connection(connection_controller, cid);
if (!connection_controller->browsing_connection) return BTSTACK_MEMORY_ALLOC_FAILED;
connection_target->browsing_connection = avrcp_browsing_create_connection(connection_target, cid);
if (!connection_target->browsing_connection){
avrcp_browsing_finalize_connection(connection_controller);
return BTSTACK_MEMORY_ALLOC_FAILED;
}
avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
if (avrcp_browsing_cid != NULL){
*avrcp_browsing_cid = cid;
}
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
return l2cap_create_ertm_channel(avrcp_browsing_packet_handler, remote_addr, connection_controller->browsing_l2cap_psm,
&connection_controller->browsing_connection->ertm_config,
connection_controller->browsing_connection->ertm_buffer,
connection_controller->browsing_connection->ertm_buffer_size, NULL);
}
uint8_t avrcp_browsing_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){
avrcp_connection_t * connection_controller = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
if (!connection_controller){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
if (!connection_target){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_controller->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_target->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
return ERROR_CODE_COMMAND_DISALLOWED;
}
avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
l2cap_accept_ertm_connection(connection_controller->browsing_connection->l2cap_browsing_cid,
&connection_controller->browsing_connection->ertm_config,
connection_controller->browsing_connection->ertm_buffer,
connection_controller->browsing_connection->ertm_buffer_size);
return ERROR_CODE_SUCCESS;
}
uint8_t avrcp_browsing_decline_incoming_connection(uint16_t avrcp_browsing_cid){
avrcp_connection_t * connection_controller = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
if (!connection_controller){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
if (!connection_target){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_controller->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_target->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
return ERROR_CODE_COMMAND_DISALLOWED;
}
l2cap_decline_connection(connection_controller->browsing_connection->l2cap_browsing_cid);
avrcp_browsing_finalize_connection(connection_controller);
avrcp_browsing_finalize_connection(connection_target);
return ERROR_CODE_SUCCESS;
}
uint8_t avrcp_browsing_disconnect(uint16_t avrcp_browsing_cid){
avrcp_connection_t * connection_controller = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
if (!connection_controller){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
if (!connection_target){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_controller->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_target->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
l2cap_disconnect(connection_controller->browsing_connection->l2cap_browsing_cid, 0);
return ERROR_CODE_SUCCESS;
}
void avrcp_browsing_register_controller_packet_handler(btstack_packet_handler_t callback){
avrcp_browsing_controller_packet_handler = callback;
}
void avrcp_browsing_register_target_packet_handler(btstack_packet_handler_t callback){
avrcp_browsing_target_packet_handler = callback;
}
void avrcp_browsing_register_packet_handler(btstack_packet_handler_t callback){
btstack_assert(callback != NULL);
avrcp_browsing_callback = callback;
}

View File

@ -46,6 +46,7 @@
#define AVRCP_H
#include <stdint.h>
#include "btstack_run_loop.h"
#include "btstack_linked_list.h"
#include "l2cap.h"
@ -54,6 +55,11 @@
extern "C" {
#endif
#define PSM_AVCTP_BROWSING 0x001b
#define AVRCP_BROWSING_ITEM_HEADER_LEN 3
#define AVRCP_BROWSING_MAX_NUM_ATTR_IDS 8
#define BT_SIG_COMPANY_ID 0x001958
#define AVRCP_MEDIA_ATTR_COUNT 7
#define AVRCP_MAX_ATTRIBUTTE_SIZE 100
@ -61,9 +67,6 @@ extern "C" {
#define AVRCP_MAX_FOLDER_NAME_SIZE 20
#define AVRCP_NO_TRACK_SELECTED_PLAYBACK_POSITION_CHANGED 0xFFFFFFFF
// #define AVRCP_NO_TRACK_SELECTED_TRACK_CHANGED 0xFFFFFFFFFFFFFFFF
#define AVRCP_BROWSING_ITEM_HEADER_LEN 3
typedef enum {
AVRCP_STATUS_INVALID_COMMAND = 0, // sent if TG received a PDU that it did not understand.
@ -110,7 +113,6 @@ typedef enum {
AVRCP_CAPABILITY_ID_EVENT = 0x03
} avrcp_capability_id_t;
#define AVRCP_BROWSING_MAX_NUM_ATTR_IDS 8
typedef enum {
AVRCP_MEDIA_ATTR_ALL = 0x0000,
AVRCP_MEDIA_ATTR_TITLE,
@ -298,13 +300,6 @@ typedef enum {
AVCTP_W2_SEND_FRAGMENTED_COMMAND,
} avctp_connection_state_t;
typedef enum {
AVRCP_BROWSING_MEDIA_PLAYER_LIST = 0x00,
AVRCP_BROWSING_MEDIA_PLAYER_VIRTUAL_FILESYSTEM,
AVRCP_BROWSING_SEARCH,
AVRCP_BROWSING_NOW_PLAYING
} avrcp_browsing_scope_t;
typedef struct {
uint16_t len;
uint8_t * value;
@ -333,6 +328,35 @@ typedef enum{
AVRCP_TARGET
} avrcp_role_t;
typedef enum {
AVRCP_SHUFFLE_MODE_INVALID,
AVRCP_SHUFFLE_MODE_OFF,
AVRCP_SHUFFLE_MODE_ALL_TRACKS,
AVRCP_SHUFFLE_MODE_GROUP
} avrcp_shuffle_mode_t;
typedef enum {
AVRCP_REPEAT_MODE_INVALID,
AVRCP_REPEAT_MODE_OFF,
AVRCP_REPEAT_MODE_SINGLE_TRACK,
AVRCP_REPEAT_MODE_ALL_TRACKS,
AVRCP_REPEAT_MODE_GROUP
} avrcp_repeat_mode_t;
typedef enum {
RFC2978_CHARSET_MIB_UTF8 = 106
} rfc2978_charset_mib_enumid_t;
typedef enum {
AVRCP_BROWSING_MEDIA_PLAYER_LIST = 0x00,
AVRCP_BROWSING_MEDIA_PLAYER_VIRTUAL_FILESYSTEM,
AVRCP_BROWSING_SEARCH,
AVRCP_BROWSING_NOW_PLAYING
} avrcp_browsing_scope_t;
// BROWSING
typedef struct {
uint16_t l2cap_browsing_cid;
@ -405,7 +429,6 @@ typedef struct {
bool incoming_declined;
} avrcp_browsing_connection_t;
// BROWSING END
typedef struct {
btstack_linked_item_t item;
@ -514,25 +537,6 @@ typedef struct {
uint8_t accept_response;
} avrcp_connection_t;
typedef enum {
AVRCP_SHUFFLE_MODE_INVALID,
AVRCP_SHUFFLE_MODE_OFF,
AVRCP_SHUFFLE_MODE_ALL_TRACKS,
AVRCP_SHUFFLE_MODE_GROUP
} avrcp_shuffle_mode_t;
typedef enum {
AVRCP_REPEAT_MODE_INVALID,
AVRCP_REPEAT_MODE_OFF,
AVRCP_REPEAT_MODE_SINGLE_TRACK,
AVRCP_REPEAT_MODE_ALL_TRACKS,
AVRCP_REPEAT_MODE_GROUP
} avrcp_repeat_mode_t;
typedef enum {
RFC2978_CHARSET_MIB_UTF8 = 106
} rfc2978_charset_mib_enumid_t;
typedef struct {
avrcp_role_t role;
btstack_packet_handler_t avrcp_callback;

View File

@ -0,0 +1,565 @@
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "avrcp_browsing.c"
#include <stdint.h>
#include <string.h>
#include "bluetooth_psm.h"
#include "bluetooth_sdp.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "classic/sdp_client.h"
#include "classic/sdp_util.h"
#include "classic/avrcp_browsing.h"
static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static btstack_packet_handler_t avrcp_browsing_callback;
static btstack_linked_list_t connections;
static bool l2cap_browsing_service_registered = false;
static btstack_packet_handler_t avrcp_browsing_controller_packet_handler;
static btstack_packet_handler_t avrcp_browsing_target_packet_handler;
avrcp_connection_t * get_avrcp_connection_for_browsing_cid_for_role(avrcp_role_t role, uint16_t browsing_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
while (btstack_linked_list_iterator_has_next(&it)){
avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
if (connection->role != role) continue;
if (connection->avrcp_browsing_cid != browsing_cid) continue;
return connection;
}
return NULL;
}
avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid_for_role(avrcp_role_t role, uint16_t browsing_l2cap_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
while (btstack_linked_list_iterator_has_next(&it)){
avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
if (connection->role != role) continue;
if (connection->browsing_connection && (connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid)) continue;
return connection;
}
return NULL;
}
avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
while (btstack_linked_list_iterator_has_next(&it)){
avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
if (connection->role != role) continue;
if (connection->browsing_connection && (connection->browsing_connection->l2cap_browsing_cid != l2cap_cid)) continue;
return connection->browsing_connection;
}
return NULL;
}
void avrcp_browsing_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid){
connection->wait_to_send = true;
l2cap_request_can_send_now_event(l2cap_cid);
}
static void avrcp_reconnect_timer_timeout_handler(btstack_timer_source_t * timer){
uint16_t avrcp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
avrcp_connection_t * connection_controller = get_avrcp_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
if (connection_controller == NULL) return;
avrcp_connection_t * connection_target = get_avrcp_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
if (connection_target == NULL) return;
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) return;
if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W2_L2CAP_RECONNECT){
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
l2cap_create_ertm_channel(avrcp_browsing_packet_handler, connection_controller->remote_addr, connection_controller->browsing_l2cap_psm,
&connection_controller->browsing_connection->ertm_config,
connection_controller->browsing_connection->ertm_buffer,
connection_controller->browsing_connection->ertm_buffer_size, NULL);
}
}
static void avrcp_reconnect_timer_start(avrcp_connection_t * connection){
btstack_run_loop_set_timer_handler(&connection->reconnect_timer, avrcp_reconnect_timer_timeout_handler);
btstack_run_loop_set_timer_context(&connection->reconnect_timer, (void *)(uintptr_t)connection->avrcp_cid);
// add some jitter/randomness to reconnect delay
uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F);
btstack_run_loop_set_timer(&connection->reconnect_timer, timeout);
btstack_run_loop_add_timer(&connection->reconnect_timer);
}
// AVRCP Browsing Service functions
static void avrcp_browsing_finalize_connection(avrcp_connection_t * connection){
btstack_run_loop_remove_timer(&connection->reconnect_timer);
btstack_memory_avrcp_browsing_connection_free(connection->browsing_connection);
connection->browsing_connection = NULL;
}
static void avrcp_browsing_emit_connection_established(uint16_t browsing_cid, bd_addr_t addr, uint8_t status){
btstack_assert(avrcp_browsing_callback != NULL);
uint8_t event[12];
int pos = 0;
event[pos++] = HCI_EVENT_AVRCP_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED;
event[pos++] = status;
reverse_bd_addr(addr,&event[pos]);
pos += 6;
little_endian_store_16(event, pos, browsing_cid);
pos += 2;
(*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}
static void avrcp_browsing_emit_incoming_connection(uint16_t browsing_cid, bd_addr_t addr){
btstack_assert(avrcp_browsing_callback != NULL);
uint8_t event[11];
int pos = 0;
event[pos++] = HCI_EVENT_AVRCP_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION;
reverse_bd_addr(addr,&event[pos]);
pos += 6;
little_endian_store_16(event, pos, browsing_cid);
pos += 2;
(*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}
static void avrcp_browsing_emit_connection_closed(uint16_t browsing_cid){
btstack_assert(avrcp_browsing_callback != NULL);
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, browsing_cid);
pos += 2;
(*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}
static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection, uint16_t avrcp_browsing_cid){
avrcp_browsing_connection_t * browsing_connection = btstack_memory_avrcp_browsing_connection_get();
if (!browsing_connection){
log_error("Not enough memory to create browsing connection");
return NULL;
}
browsing_connection->state = AVCTP_CONNECTION_IDLE;
browsing_connection->transaction_label = 0xFF;
avrcp_connection->avrcp_browsing_cid = avrcp_browsing_cid;
avrcp_connection->browsing_connection = browsing_connection;
log_info("avrcp_browsing_create_connection, avrcp cid 0x%02x", avrcp_connection->avrcp_browsing_cid);
return browsing_connection;
}
static void avrcp_browsing_configure_ertm(avrcp_browsing_connection_t * browsing_connection, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){
browsing_connection->ertm_buffer = ertm_buffer;
browsing_connection->ertm_buffer_size = ertm_buffer_size;
if (ertm_buffer_size > 0) {
(void)memcpy(&browsing_connection->ertm_config, ertm_config,
sizeof(l2cap_ertm_config_t));
log_info("avrcp_browsing_configure_ertm");
}
}
static avrcp_browsing_connection_t * avrcp_browsing_handle_incoming_connection(avrcp_connection_t * connection, uint16_t local_cid, uint16_t avrcp_browsing_cid){
if (connection->browsing_connection == NULL){
avrcp_browsing_create_connection(connection, avrcp_browsing_cid);
}
if (connection->browsing_connection) {
connection->browsing_connection->l2cap_browsing_cid = local_cid;
connection->browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION;
btstack_run_loop_remove_timer(&connection->reconnect_timer);
}
return connection->browsing_connection;
}
static void avrcp_browsing_handle_open_connection_for_role(avrcp_connection_t * connection, uint16_t local_cid){
connection->browsing_connection->l2cap_browsing_cid = local_cid;
connection->browsing_connection->incoming_declined = false;
connection->browsing_connection->state = AVCTP_CONNECTION_OPENED;
log_info("L2CAP_EVENT_CHANNEL_OPENED browsing_avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x, role %d", connection->avrcp_cid, connection->l2cap_signaling_cid, connection->role);
}
static avrcp_frame_type_t avrcp_get_frame_type(uint8_t header){
return (avrcp_frame_type_t)((header & 0x02) >> 1);
}
static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
bd_addr_t event_addr;
uint16_t local_cid;
uint8_t status;
bool decline_connection;
bool outoing_active;
avrcp_connection_t * connection_controller;
avrcp_connection_t * connection_target;
switch (packet_type){
case L2CAP_DATA_PACKET:
switch (avrcp_get_frame_type(packet[0])){
case AVRCP_RESPONSE_FRAME:
(*avrcp_browsing_controller_packet_handler)(packet_type, channel, packet, size);
break;
case AVRCP_COMMAND_FRAME:
default: // make compiler happy
(*avrcp_browsing_target_packet_handler)(packet_type, channel, packet, size);
break;
}
break;
case HCI_EVENT_PACKET:
btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
btstack_assert(avrcp_browsing_target_packet_handler != NULL);
switch (hci_event_packet_get_type(packet)) {
case L2CAP_EVENT_INCOMING_CONNECTION:
btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
btstack_assert(avrcp_browsing_target_packet_handler != NULL);
l2cap_event_incoming_connection_get_address(packet, event_addr);
local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
outoing_active = false;
connection_target = get_avrcp_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
connection_controller = get_avrcp_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
if (connection_target == NULL || connection_controller == NULL) {
l2cap_decline_connection(local_cid);
return;
}
if (connection_target->browsing_connection != NULL){
if (connection_target->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED){
outoing_active = true;
connection_target->browsing_connection->incoming_declined = true;
}
}
if (connection_controller->browsing_connection != NULL){
if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED) {
outoing_active = true;
connection_controller->browsing_connection->incoming_declined = true;
}
}
decline_connection = outoing_active;
if (decline_connection == false){
uint16_t avrcp_browsing_cid;
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)){
avrcp_browsing_cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
} else {
avrcp_browsing_cid = connection_controller->avrcp_browsing_cid;
}
// create two connection objects (both)
connection_target->browsing_connection = avrcp_browsing_handle_incoming_connection(connection_target, local_cid, avrcp_browsing_cid);
connection_controller->browsing_connection = avrcp_browsing_handle_incoming_connection(connection_controller, local_cid, avrcp_browsing_cid);
if ((connection_target->browsing_connection == NULL) || (connection_controller->browsing_connection == NULL)){
decline_connection = true;
if (connection_target->browsing_connection) {
avrcp_browsing_finalize_connection(connection_target);
}
if (connection_controller->browsing_connection) {
avrcp_browsing_finalize_connection(connection_controller);
}
}
}
if (decline_connection){
l2cap_decline_connection(local_cid);
} else {
log_info("AVRCP: L2CAP_EVENT_INCOMING_CONNECTION browsing_avrcp_cid 0x%02x", connection_controller->avrcp_browsing_cid);
avrcp_browsing_emit_incoming_connection(connection_controller->avrcp_browsing_cid, event_addr);
}
break;
case L2CAP_EVENT_CHANNEL_OPENED:
l2cap_event_channel_opened_get_address(packet, event_addr);
status = l2cap_event_channel_opened_get_status(packet);
local_cid = l2cap_event_channel_opened_get_local_cid(packet);
connection_controller = get_avrcp_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
connection_target = get_avrcp_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
// incoming: structs are already created in L2CAP_EVENT_INCOMING_CONNECTION
// outgoing: structs are cteated in avrcp_connect() and avrcp_browsing_connect()
if ((connection_controller == NULL) || (connection_target == NULL)) {
break;
}
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) {
break;
}
switch (status){
case ERROR_CODE_SUCCESS:
avrcp_browsing_handle_open_connection_for_role(connection_target, local_cid);
avrcp_browsing_handle_open_connection_for_role(connection_controller, local_cid);
avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status);
return;
case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES:
if (connection_controller->browsing_connection->incoming_declined == true){
log_info("Incoming browsing connection was declined, and the outgoing failed");
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RECONNECT;
connection_controller->browsing_connection->incoming_declined = false;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RECONNECT;
connection_target->browsing_connection->incoming_declined = false;
avrcp_reconnect_timer_start(connection_controller);
return;
}
break;
default:
break;
}
log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status);
avrcp_browsing_finalize_connection(connection_controller);
avrcp_browsing_finalize_connection(connection_target);
break;
case L2CAP_EVENT_CHANNEL_CLOSED:
local_cid = l2cap_event_channel_closed_get_local_cid(packet);
connection_controller = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid);
connection_target = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid);
if ((connection_controller == NULL) || (connection_target == NULL)) {
break;
}
if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) {
break;
}
avrcp_browsing_emit_connection_closed(connection_controller->avrcp_browsing_cid);
avrcp_browsing_finalize_connection(connection_controller);
avrcp_browsing_finalize_connection(connection_target);
break;
case L2CAP_EVENT_CAN_SEND_NOW:
local_cid = l2cap_event_can_send_now_get_local_cid(packet);
connection_target = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid);
if ((connection_target != NULL) && (connection_target->browsing_connection != NULL) && connection_target->browsing_connection->wait_to_send) {
connection_target->browsing_connection->wait_to_send = false;
(*avrcp_browsing_target_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
break;
}
connection_controller = get_avrcp_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid);
if ((connection_controller != NULL) && (connection_controller->browsing_connection != NULL) && connection_controller->browsing_connection->wait_to_send) {
connection_controller->browsing_connection->wait_to_send = false;
(*avrcp_browsing_controller_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
break;
}
break;
default:
break;
}
break;
default:
break;
}
}
void avrcp_browsing_init(void){
if (l2cap_browsing_service_registered) return;
int status = l2cap_register_service(&avrcp_browsing_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_2);
if (status != ERROR_CODE_SUCCESS) return;
l2cap_browsing_service_registered = true;
}
uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){
btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
btstack_assert(avrcp_browsing_target_packet_handler != NULL);
avrcp_connection_t * connection_controller = get_avrcp_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, remote_addr);
if (!connection_controller){
return ERROR_CODE_COMMAND_DISALLOWED;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_bd_addr_for_role(AVRCP_TARGET, remote_addr);
if (!connection_target){
return ERROR_CODE_COMMAND_DISALLOWED;
}
if (connection_controller->browsing_connection){
return ERROR_CODE_COMMAND_DISALLOWED;
}
if (connection_target->browsing_connection){
return ERROR_CODE_COMMAND_DISALLOWED;
}
uint16_t cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
connection_controller->browsing_connection = avrcp_browsing_create_connection(connection_controller, cid);
if (!connection_controller->browsing_connection) return BTSTACK_MEMORY_ALLOC_FAILED;
connection_target->browsing_connection = avrcp_browsing_create_connection(connection_target, cid);
if (!connection_target->browsing_connection){
avrcp_browsing_finalize_connection(connection_controller);
return BTSTACK_MEMORY_ALLOC_FAILED;
}
avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
if (avrcp_browsing_cid != NULL){
*avrcp_browsing_cid = cid;
}
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
return l2cap_create_ertm_channel(avrcp_browsing_packet_handler, remote_addr, connection_controller->browsing_l2cap_psm,
&connection_controller->browsing_connection->ertm_config,
connection_controller->browsing_connection->ertm_buffer,
connection_controller->browsing_connection->ertm_buffer_size, NULL);
}
uint8_t avrcp_browsing_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){
avrcp_connection_t * connection_controller = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
if (!connection_controller){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
if (!connection_target){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_controller->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_target->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
return ERROR_CODE_COMMAND_DISALLOWED;
}
avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
l2cap_accept_ertm_connection(connection_controller->browsing_connection->l2cap_browsing_cid,
&connection_controller->browsing_connection->ertm_config,
connection_controller->browsing_connection->ertm_buffer,
connection_controller->browsing_connection->ertm_buffer_size);
return ERROR_CODE_SUCCESS;
}
uint8_t avrcp_browsing_decline_incoming_connection(uint16_t avrcp_browsing_cid){
avrcp_connection_t * connection_controller = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
if (!connection_controller){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
if (!connection_target){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_controller->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_target->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
return ERROR_CODE_COMMAND_DISALLOWED;
}
l2cap_decline_connection(connection_controller->browsing_connection->l2cap_browsing_cid);
avrcp_browsing_finalize_connection(connection_controller);
avrcp_browsing_finalize_connection(connection_target);
return ERROR_CODE_SUCCESS;
}
uint8_t avrcp_browsing_disconnect(uint16_t avrcp_browsing_cid){
avrcp_connection_t * connection_controller = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
if (!connection_controller){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
avrcp_connection_t * connection_target = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
if (!connection_target){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_controller->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
if (!connection_target->browsing_connection){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
l2cap_disconnect(connection_controller->browsing_connection->l2cap_browsing_cid, 0);
return ERROR_CODE_SUCCESS;
}
void avrcp_browsing_register_controller_packet_handler(btstack_packet_handler_t callback){
avrcp_browsing_controller_packet_handler = callback;
}
void avrcp_browsing_register_target_packet_handler(btstack_packet_handler_t callback){
avrcp_browsing_target_packet_handler = callback;
}
void avrcp_browsing_register_packet_handler(btstack_packet_handler_t callback){
btstack_assert(callback != NULL);
avrcp_browsing_callback = callback;
}

View File

@ -0,0 +1,120 @@
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* avrcp_browsing.h
*
* Audio/Video Remote Control Profile (Browsing)
*
*/
#ifndef AVRCP_BROWSING_H
#define AVRCP_BROWSING_H
#include <stdint.h>
#include "avrcp.h"
#include "btstack_run_loop.h"
#include "btstack_linked_list.h"
#include "l2cap.h"
#if defined __cplusplus
extern "C" {
#endif
avrcp_connection_t * get_avrcp_connection_for_browsing_cid_for_role(avrcp_role_t role, uint16_t browsing_cid);
avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid_for_role(avrcp_role_t role, uint16_t browsing_l2cap_cid);
avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid);
void avrcp_browsing_register_controller_packet_handler(btstack_packet_handler_t callback);
void avrcp_browsing_register_target_packet_handler(btstack_packet_handler_t callback);
void avrcp_browsing_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid);
/* API_START */
/**
* @brief Set up AVRCP Browsing service
*/
void avrcp_browsing_init(void);
/**
* @brief Register callback for the AVRCP Browsing Controller client.
* @param callback
*/
void avrcp_browsing_register_packet_handler(btstack_packet_handler_t callback);
/**
* @brief Connect to AVRCP Browsing service on a remote device, emits AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED with status
* @param remote_addr
* @param ertm_buffer
* @param ertm_buffer_size
* @param ertm_config
* @param avrcp_browsing_cid outgoing parameter, valid if status == ERROR_CODE_SUCCESS
* @returns status
*/
uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid);
/**
* @brief Configure incoming connection for Browsing Service.
* @param avrcp_browsing_cid
* @param ertm_buffer
* @param ertm_buffer_size
* @param ertm_config
* @returns status
*/
uint8_t avrcp_browsing_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config);
/**
* @brief Decline incoming connection Browsing Service.
* @param avrcp_browsing_cid
* @returns status
*/
uint8_t avrcp_browsing_decline_incoming_connection(uint16_t avrcp_browsing_cid);
/**
* @brief Disconnect from AVRCP Browsing service
* @param avrcp_browsing_cid
* @returns status
*/
uint8_t avrcp_browsing_disconnect(uint16_t avrcp_browsing_cid);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // AVRCP_BROWSING_H

View File

@ -43,7 +43,7 @@
#include <string.h>
#include <inttypes.h>
#include "btstack.h"
#include "classic/avrcp.h"
#include "classic/avrcp_browsing.h"
#include "classic/avrcp_browsing_controller.h"

View File

@ -43,7 +43,7 @@
#include <string.h>
#include <inttypes.h>
#include "btstack.h"
#include "classic/avrcp.h"
#include "classic/avrcp_browsing.h"
#include "classic/avrcp_browsing_target.h"
static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);

View File

@ -75,6 +75,7 @@ AVRCP += \
avrcp.c \
avrcp_target.c \
avrcp_controller.c \
avrcp_browsing.c \
avrcp_browsing_target.c \
avrcp_browsing_controller.c \