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__ "a2dp_sink.c" 39 40 #include <stdint.h> 41 #include <string.h> 42 43 #include "bluetooth_psm.h" 44 #include "bluetooth_sdp.h" 45 #include "btstack_debug.h" 46 #include "btstack_event.h" 47 #include "classic/a2dp_sink.h" 48 #include "classic/avdtp_sink.h" 49 #include "classic/avdtp_util.h" 50 #include "classic/sdp_util.h" 51 52 static const char * default_a2dp_sink_service_name = "BTstack A2DP Sink Service"; 53 static const char * default_a2dp_sink_service_provider_name = "BTstack A2DP Sink Service Provider"; 54 55 static bool outgoing_active = false; 56 static uint16_t a2dp_sink_cid; 57 static bool stream_endpoint_configured = false; 58 59 static btstack_packet_handler_t a2dp_sink_packet_handler_user; 60 61 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, 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, BLUETOOTH_SERVICE_CLASS_AUDIO_SINK); 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_PSM_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, BLUETOOTH_SERVICE_CLASS_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_a2dp_sink_service_name), (uint8_t *) default_a2dp_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_a2dp_sink_service_provider_name), (uint8_t *) default_a2dp_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 void a2dp_sink_register_packet_handler(btstack_packet_handler_t callback){ 143 // avdtp_sink_register_packet_handler(callback); 144 // return; 145 if (callback == NULL){ 146 log_error("a2dp_sink_register_packet_handler called with NULL callback"); 147 return; 148 } 149 avdtp_sink_register_packet_handler(&a2dp_sink_packet_handler_internal); 150 a2dp_sink_packet_handler_user = callback; 151 } 152 153 void a2dp_sink_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){ 154 if (callback == NULL){ 155 log_error("a2dp_sink_register_media_handler called with NULL callback"); 156 return; 157 } 158 avdtp_sink_register_media_handler(callback); 159 } 160 161 void a2dp_sink_init(void){ 162 avdtp_sink_init(); 163 } 164 165 avdtp_stream_endpoint_t * a2dp_sink_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, 166 uint8_t * codec_capabilities, uint16_t codec_capabilities_len, 167 uint8_t * codec_configuration, uint16_t codec_configuration_len){ 168 avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_sink_create_stream_endpoint(AVDTP_SINK, media_type); 169 if (!local_stream_endpoint){ 170 return NULL; 171 } 172 avdtp_sink_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint)); 173 avdtp_sink_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type, 174 codec_capabilities, codec_capabilities_len); 175 avdtp_sink_register_delay_reporting_category(avdtp_stream_endpoint_seid(local_stream_endpoint)); 176 177 // store user codec configuration buffer 178 local_stream_endpoint->media_codec_configuration_info = codec_configuration; 179 local_stream_endpoint->media_codec_configuration_len = codec_configuration_len; 180 181 return local_stream_endpoint; 182 } 183 184 void a2dp_sink_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){ 185 avdtp_sink_finalize_stream_endpoint(stream_endpoint); 186 } 187 188 uint8_t a2dp_sink_establish_stream(bd_addr_t bd_addr, uint8_t local_seid, uint16_t * avdtp_cid){ 189 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 190 if (stream_endpoint == NULL){ 191 log_info("No local_stream_endpoint for seid %d", local_seid); 192 return ERROR_CODE_COMMAND_DISALLOWED; 193 } 194 // remember to tell client 195 outgoing_active = true; 196 return avdtp_sink_connect(bd_addr, avdtp_cid); 197 } 198 199 void a2dp_sink_disconnect(uint16_t a2dp_cid){ 200 avdtp_disconnect(a2dp_cid); 201 } 202 203 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 204 UNUSED(channel); 205 UNUSED(size); 206 207 uint8_t status; 208 uint8_t local_seid; 209 uint8_t signal_identifier; 210 211 if (packet_type != HCI_EVENT_PACKET) return; 212 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return; 213 214 switch (packet[2]){ 215 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED: 216 if (stream_endpoint_configured) return; 217 218 status = avdtp_subevent_signaling_connection_established_get_status(packet); 219 if (status != ERROR_CODE_SUCCESS){ 220 // only care for outgoing connections 221 if (!outgoing_active) break; 222 outgoing_active = false; 223 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 224 log_info("A2DP sink signaling connection failed status %d", status); 225 break; 226 } 227 228 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 229 log_info("A2DP sink signaling connection established avdtp_cid 0x%02x", avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet)); 230 break; 231 232 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 233 if (stream_endpoint_configured) break; 234 stream_endpoint_configured = true; 235 a2dp_sink_cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet); 236 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION); 237 break; 238 239 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION: 240 if (stream_endpoint_configured) break; 241 stream_endpoint_configured = true; 242 a2dp_sink_cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet); 243 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION); 244 break; 245 246 case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: 247 if (stream_endpoint_configured == false) break; 248 if (a2dp_sink_cid != avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet)) break; 249 250 // about to notify client 251 outgoing_active = false; 252 status = avdtp_subevent_streaming_connection_established_get_status(packet); 253 if (status != ERROR_CODE_SUCCESS){ 254 log_info("A2DP sink streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", a2dp_sink_cid, status); 255 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 256 break; 257 } 258 259 log_info("A2DP streaming connection established --- avdtp_cid 0x%02x, local seid %d, remote seid %d", a2dp_sink_cid, 260 avdtp_subevent_streaming_connection_established_get_local_seid(packet), 261 avdtp_subevent_streaming_connection_established_get_remote_seid(packet)); 262 263 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 264 break; 265 266 case AVDTP_SUBEVENT_SIGNALING_ACCEPT: 267 if (stream_endpoint_configured == false) break; 268 if (a2dp_sink_cid != avdtp_subevent_signaling_accept_get_avdtp_cid(packet)) break; 269 270 signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet); 271 local_seid = avdtp_subevent_signaling_accept_get_local_seid(packet); 272 273 switch (signal_identifier){ 274 case AVDTP_SI_START: 275 a2dp_emit_stream_event(a2dp_sink_packet_handler_user, a2dp_sink_cid, local_seid, A2DP_SUBEVENT_STREAM_STARTED); 276 break; 277 case AVDTP_SI_SUSPEND: 278 a2dp_emit_stream_event(a2dp_sink_packet_handler_user, a2dp_sink_cid, local_seid, A2DP_SUBEVENT_STREAM_SUSPENDED); 279 break; 280 case AVDTP_SI_ABORT: 281 case AVDTP_SI_CLOSE: 282 a2dp_emit_stream_event(a2dp_sink_packet_handler_user, a2dp_sink_cid, local_seid, A2DP_SUBEVENT_STREAM_STOPPED); 283 break; 284 default: 285 break; 286 } 287 break; 288 289 case AVDTP_SUBEVENT_SIGNALING_REJECT: 290 if (stream_endpoint_configured == false) break; 291 if (a2dp_sink_cid != avdtp_subevent_signaling_reject_get_avdtp_cid(packet)) break; 292 293 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 294 break; 295 296 case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT: 297 if (stream_endpoint_configured == false) break; 298 if (a2dp_sink_cid != avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet)) break; 299 300 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 301 break; 302 303 case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED: 304 if (stream_endpoint_configured == false) break; 305 if (a2dp_sink_cid != avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet)) break; 306 307 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_RELEASED); 308 break; 309 310 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 311 if (a2dp_sink_cid != avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet)) break; 312 313 stream_endpoint_configured = false; 314 outgoing_active = false; 315 316 a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED); 317 break; 318 default: 319 break; 320 } 321 322 } 323 324