btstack/test/avrcp/avrcp.c
2017-02-10 11:07:03 +01:00

258 lines
11 KiB
C

/*
* 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
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "btstack.h"
#include "avrcp.h"
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";
static btstack_linked_list_t avrcp_connections;
static btstack_packet_handler_t avrcp_callback;
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
void avrcp_controller_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
uint8_t* attribute;
de_create_sequence(service);
// 0x0000 "Service Record Handle"
de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
// 0x0001 "Service Class ID List"
de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
attribute = de_push_sequence(service);
{
de_add_number(attribute, DE_UUID, DE_SIZE_16, AUDIO_SINK_GROUP);
}
de_pop_sequence(service, attribute);
// 0x0004 "Protocol Descriptor List"
de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
attribute = de_push_sequence(service);
{
uint8_t* l2cpProtocol = de_push_sequence(attribute);
{
de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_AVCTP);
}
de_pop_sequence(attribute, l2cpProtocol);
uint8_t* avProtocol = de_push_sequence(attribute);
{
de_add_number(avProtocol, DE_UUID, DE_SIZE_16, PSM_AVCTP); // avProtocol_service
de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version
}
de_pop_sequence(attribute, avProtocol);
}
de_pop_sequence(service, attribute);
// 0x0005 "Public Browse Group"
de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
attribute = de_push_sequence(service);
{
de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
}
de_pop_sequence(service, attribute);
// 0x0009 "Bluetooth Profile Descriptor List"
de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
attribute = de_push_sequence(service);
{
uint8_t *a2dProfile = de_push_sequence(attribute);
{
de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, ADVANCED_AUDIO_DISTRIBUTION);
de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103);
}
de_pop_sequence(attribute, a2dProfile);
}
de_pop_sequence(service, attribute);
// 0x0100 "Service Name"
de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100);
if (service_name){
de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name);
} else {
de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_name), (uint8_t *) default_avrcp_controller_service_name);
}
// 0x0100 "Provider Name"
de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102);
if (service_provider_name){
de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
} else {
de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_provider_name), (uint8_t *) default_avrcp_controller_service_provider_name);
}
// 0x0311 "Supported Features"
de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
}
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t event_addr;
hci_con_handle_t con_handle;
uint16_t psm;
uint16_t local_cid;
// avrcp_connection_t * connection = NULL;
switch (packet_type) {
// case L2CAP_DATA_PACKET:
// connection = get_avdtp_connection_for_l2cap_signaling_cid(channel);
// if (connection){
// handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
// break;
// }
// stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(channel);
// if (!stream_endpoint){
// if (!connection) break;
// handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
// break;
// }
// if (channel == stream_endpoint->connection->l2cap_signaling_cid){
// stream_endpoint_state_machine(stream_endpoint->connection, stream_endpoint, L2CAP_DATA_PACKET, 0, packet, size);
// break;
// }
// if (channel == stream_endpoint->l2cap_media_cid){
// (*handle_media_data)(stream_endpoint, packet, size);
// break;
// }
// if (channel == stream_endpoint->l2cap_reporting_cid){
// // TODO
// printf("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED\n");
// } else if (channel == stream_endpoint->l2cap_recovery_cid){
// // TODO
// printf("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED\n");
// } else {
// log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
// }
// break;
case HCI_EVENT_PACKET:
switch (hci_event_packet_get_type(packet)) {
case L2CAP_EVENT_INCOMING_CONNECTION:
l2cap_event_incoming_connection_get_address(packet, event_addr);
local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
break;
case L2CAP_EVENT_CHANNEL_OPENED:
l2cap_event_channel_opened_get_address(packet, event_addr);
if (l2cap_event_channel_opened_get_status(packet)){
log_error("L2CAP connection to connection %s failed. status code 0x%02x",
bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet));
break;
}
psm = l2cap_event_channel_opened_get_psm(packet);
if (psm != PSM_AVCTP){
log_error("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED");
return;
}
con_handle = l2cap_event_channel_opened_get_handle(packet);
local_cid = l2cap_event_channel_opened_get_local_cid(packet);
printf("L2CAP_EVENT_CHANNEL_OPENED: Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
bd_addr_to_str(event_addr), con_handle, psm, local_cid, l2cap_event_channel_opened_get_remote_cid(packet));
printf("channel %d, size %d\n", channel, size);
break;
default:
break;
}
break;
default:
break;
}
}
void avrcp_init(void){
avrcp_connections = NULL;
l2cap_register_service(&packet_handler, PSM_AVCTP, 0xffff, LEVEL_0);
}
void avrcp_register_packet_handler(btstack_packet_handler_t callback){
if (callback == NULL){
log_error("avrcp_register_packet_handler called with NULL callback");
return;
}
avrcp_callback = callback;
}
static avrcp_connection_t * get_avrcp_connection_for_bd_addr(bd_addr_t addr){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &avrcp_connections);
while (btstack_linked_list_iterator_has_next(&it)){
avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
if (memcmp(addr, connection->remote_addr, 6) != 0) continue;
return connection;
}
return NULL;
}
static avrcp_connection_t * avrcp_create_connection(bd_addr_t remote_addr){
avrcp_connection_t * connection = btstack_memory_avrcp_connection_get();
memset(connection, 0, sizeof(avrcp_connection_t));
connection->state = AVCTP_CONNECTION_IDLE;
connection->transaction_label++;
memcpy(connection->remote_addr, remote_addr, 6);
btstack_linked_list_add(&avrcp_connections, (btstack_linked_item_t *) connection);
return connection;
}
void avrcp_connect(bd_addr_t bd_addr){
avrcp_connection_t * connection = get_avrcp_connection_for_bd_addr(bd_addr);
if (!connection){
connection = avrcp_create_connection(bd_addr);
}
if (connection->state != AVCTP_CONNECTION_IDLE) return;
connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
l2cap_create_channel(packet_handler, connection->remote_addr, PSM_AVCTP, 0xffff, NULL);
}