1 /* 2 * Copyright (C) 2016 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "avdtp_sink.c" 39 40 41 #include <stdint.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #include "btstack.h" 48 #include "avdtp.h" 49 #include "avdtp_sink.h" 50 #include "avdtp_util.h" 51 #include "avdtp_initiator.h" 52 #include "avdtp_acceptor.h" 53 54 static const char * default_avdtp_sink_service_name = "BTstack AVDTP Sink Service"; 55 static const char * default_avdtp_sink_service_provider_name = "BTstack AVDTP Sink Service Provider"; 56 57 static avdtp_context_t avdtp_sink_context; 58 59 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 60 61 //static void (*handle_media_data)(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size); 62 63 void a2dp_sink_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 64 uint8_t* attribute; 65 de_create_sequence(service); 66 67 // 0x0000 "Service Record Handle" 68 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 69 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 70 71 // 0x0001 "Service Class ID List" 72 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 73 attribute = de_push_sequence(service); 74 { 75 de_add_number(attribute, DE_UUID, DE_SIZE_16, AUDIO_SINK_GROUP); 76 } 77 de_pop_sequence(service, attribute); 78 79 // 0x0004 "Protocol Descriptor List" 80 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 81 attribute = de_push_sequence(service); 82 { 83 uint8_t* l2cpProtocol = de_push_sequence(attribute); 84 { 85 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 86 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); 87 } 88 de_pop_sequence(attribute, l2cpProtocol); 89 90 uint8_t* avProtocol = de_push_sequence(attribute); 91 { 92 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); // avProtocol_service 93 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 94 } 95 de_pop_sequence(attribute, avProtocol); 96 } 97 de_pop_sequence(service, attribute); 98 99 // 0x0005 "Public Browse Group" 100 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 101 attribute = de_push_sequence(service); 102 { 103 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 104 } 105 de_pop_sequence(service, attribute); 106 107 // 0x0009 "Bluetooth Profile Descriptor List" 108 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 109 attribute = de_push_sequence(service); 110 { 111 uint8_t *a2dProfile = de_push_sequence(attribute); 112 { 113 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, ADVANCED_AUDIO_DISTRIBUTION); 114 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 115 } 116 de_pop_sequence(attribute, a2dProfile); 117 } 118 de_pop_sequence(service, attribute); 119 120 121 // 0x0100 "Service Name" 122 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 123 if (service_name){ 124 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 125 } else { 126 de_add_data(service, DE_STRING, strlen(default_avdtp_sink_service_name), (uint8_t *) default_avdtp_sink_service_name); 127 } 128 129 // 0x0100 "Provider Name" 130 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 131 if (service_provider_name){ 132 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 133 } else { 134 de_add_data(service, DE_STRING, strlen(default_avdtp_sink_service_provider_name), (uint8_t *) default_avdtp_sink_service_provider_name); 135 } 136 137 // 0x0311 "Supported Features" 138 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 139 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 140 } 141 142 143 void avdtp_sink_register_media_transport_category(uint8_t seid){ 144 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 145 avdtp_register_media_transport_category(stream_endpoint); 146 } 147 148 void avdtp_sink_register_reporting_category(uint8_t seid){ 149 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 150 avdtp_register_reporting_category(stream_endpoint); 151 } 152 153 void avdtp_sink_register_delay_reporting_category(uint8_t seid){ 154 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 155 avdtp_register_delay_reporting_category(stream_endpoint); 156 } 157 158 void avdtp_sink_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){ 159 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 160 avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets); 161 } 162 163 void avdtp_sink_register_content_protection_category(uint8_t seid, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){ 164 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 165 avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len); 166 } 167 168 void avdtp_sink_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){ 169 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 170 avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery); 171 } 172 173 void avdtp_sink_register_media_codec_category(uint8_t seid, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, const uint8_t * media_codec_info, uint16_t media_codec_info_len){ 174 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 175 avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len); 176 } 177 178 void avdtp_sink_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){ 179 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context); 180 avdtp_register_multiplexing_category(stream_endpoint, fragmentation); 181 } 182 183 // // media, reporting. recovery 184 // void avdtp_sink_register_media_transport_identifier_for_multiplexing_category(uint8_t seid, uint8_t fragmentation){ 185 186 // } 187 188 189 /* END: tracking can send now requests pro l2cap cid */ 190 // TODO remove 191 192 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 193 avdtp_packet_handler(packet_type, channel, packet, size, &avdtp_sink_context); 194 } 195 196 // TODO: find out which security level is needed, and replace LEVEL_0 in avdtp_sink_init 197 void avdtp_sink_init(void){ 198 avdtp_sink_context.stream_endpoints = NULL; 199 avdtp_sink_context.connections = NULL; 200 avdtp_sink_context.stream_endpoints_id_counter = 0; 201 // TODO: assign dummy handlers; 202 203 l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0); 204 } 205 206 avdtp_stream_endpoint_t * avdtp_sink_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){ 207 return avdtp_create_stream_endpoint(sep_type, media_type, &avdtp_sink_context); 208 } 209 210 void avdtp_sink_register_media_handler(void (*callback)(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size)){ 211 if (callback == NULL){ 212 log_error("avdtp_sink_register_media_handler called with NULL callback"); 213 return; 214 } 215 avdtp_sink_context.handle_media_data = callback; 216 } 217 218 void avdtp_sink_register_packet_handler(btstack_packet_handler_t callback){ 219 if (callback == NULL){ 220 log_error("avdtp_sink_register_packet_handler called with NULL callback"); 221 return; 222 } 223 avdtp_sink_context.avdtp_callback = callback; 224 } 225 226 void avdtp_sink_connect(bd_addr_t bd_addr){ 227 avdtp_connection_t * connection = avdtp_connection_for_bd_addr(bd_addr, &avdtp_sink_context); 228 if (!connection){ 229 connection = avdtp_create_connection(bd_addr, &avdtp_sink_context); 230 } 231 if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return; 232 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 233 l2cap_create_channel(packet_handler, connection->remote_addr, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, NULL); 234 } 235 236 void avdtp_sink_disconnect(uint16_t con_handle){ 237 avdtp_disconnect(con_handle, &avdtp_sink_context); 238 } 239 240 void avdtp_sink_open_stream(uint16_t con_handle, uint8_t acp_seid){ 241 avdtp_open_stream(con_handle, acp_seid, &avdtp_sink_context); 242 } 243 244 void avdtp_sink_start_stream(uint16_t con_handle, uint8_t acp_seid){ 245 avdtp_start_stream(con_handle, acp_seid, &avdtp_sink_context); 246 } 247 248 void avdtp_sink_stop_stream(uint16_t con_handle, uint8_t acp_seid){ 249 avdtp_stop_stream(con_handle, acp_seid, &avdtp_sink_context); 250 } 251 252 void avdtp_sink_abort_stream(uint16_t con_handle, uint8_t acp_seid){ 253 avdtp_abort_stream(con_handle, acp_seid, &avdtp_sink_context); 254 } 255 256 void avdtp_sink_discover_stream_endpoints(uint16_t con_handle){ 257 avdtp_discover_stream_endpoints(con_handle, &avdtp_sink_context); 258 } 259 260 void avdtp_sink_get_capabilities(uint16_t con_handle, uint8_t acp_seid){ 261 avdtp_get_capabilities(con_handle, acp_seid, &avdtp_sink_context); 262 } 263 264 void avdtp_sink_get_all_capabilities(uint16_t con_handle, uint8_t acp_seid){ 265 avdtp_get_all_capabilities(con_handle, acp_seid, &avdtp_sink_context); 266 } 267 268 void avdtp_sink_get_configuration(uint16_t con_handle, uint8_t acp_seid){ 269 avdtp_get_configuration(con_handle, acp_seid, &avdtp_sink_context); 270 } 271 272 void avdtp_sink_set_configuration(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){ 273 avdtp_set_configuration(con_handle, int_seid, acp_seid, configured_services_bitmap, configuration, &avdtp_sink_context); 274 } 275 276 void avdtp_sink_reconfigure(uint16_t con_handle, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){ 277 avdtp_reconfigure(con_handle, acp_seid, configured_services_bitmap, configuration, &avdtp_sink_context); 278 } 279 280 void avdtp_sink_suspend(uint16_t con_handle, uint8_t acp_seid){ 281 avdtp_suspend(con_handle, acp_seid, &avdtp_sink_context); 282 }