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 avdtp_context_t a2dp_sink_context; 56 57 static a2dp_state_t app_state = A2DP_IDLE; 58 static int send_stream_established_for_outgoing_connection; 59 60 static avdtp_stream_endpoint_context_t sc; 61 62 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 63 64 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){ 65 uint8_t* attribute; 66 de_create_sequence(service); 67 68 // 0x0000 "Service Record Handle" 69 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 70 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 71 72 // 0x0001 "Service Class ID List" 73 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 74 attribute = de_push_sequence(service); 75 { 76 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SINK); 77 } 78 de_pop_sequence(service, attribute); 79 80 // 0x0004 "Protocol Descriptor List" 81 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 82 attribute = de_push_sequence(service); 83 { 84 uint8_t* l2cpProtocol = de_push_sequence(attribute); 85 { 86 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 87 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVDTP); 88 } 89 de_pop_sequence(attribute, l2cpProtocol); 90 91 uint8_t* avProtocol = de_push_sequence(attribute); 92 { 93 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); // avProtocol_service 94 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 95 } 96 de_pop_sequence(attribute, avProtocol); 97 } 98 de_pop_sequence(service, attribute); 99 100 // 0x0005 "Public Browse Group" 101 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 102 attribute = de_push_sequence(service); 103 { 104 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 105 } 106 de_pop_sequence(service, attribute); 107 108 // 0x0009 "Bluetooth Profile Descriptor List" 109 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 110 attribute = de_push_sequence(service); 111 { 112 uint8_t *a2dProfile = de_push_sequence(attribute); 113 { 114 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION); 115 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 116 } 117 de_pop_sequence(attribute, a2dProfile); 118 } 119 de_pop_sequence(service, attribute); 120 121 122 // 0x0100 "Service Name" 123 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 124 if (service_name){ 125 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 126 } else { 127 de_add_data(service, DE_STRING, strlen(default_a2dp_sink_service_name), (uint8_t *) default_a2dp_sink_service_name); 128 } 129 130 // 0x0100 "Provider Name" 131 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 132 if (service_provider_name){ 133 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 134 } else { 135 de_add_data(service, DE_STRING, strlen(default_a2dp_sink_service_provider_name), (uint8_t *) default_a2dp_sink_service_provider_name); 136 } 137 138 // 0x0311 "Supported Features" 139 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 140 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 141 } 142 143 void a2dp_sink_register_packet_handler(btstack_packet_handler_t callback){ 144 // avdtp_sink_register_packet_handler(callback); 145 // return; 146 if (callback == NULL){ 147 log_error("a2dp_sink_register_packet_handler called with NULL callback"); 148 return; 149 } 150 avdtp_sink_register_packet_handler(&packet_handler); 151 a2dp_sink_context.a2dp_callback = callback; 152 } 153 154 void a2dp_sink_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){ 155 if (callback == NULL){ 156 log_error("a2dp_sink_register_media_handler called with NULL callback"); 157 return; 158 } 159 avdtp_sink_register_media_handler(callback); 160 } 161 162 void a2dp_sink_init(void){ 163 avdtp_sink_init(&a2dp_sink_context); 164 } 165 166 avdtp_stream_endpoint_t * a2dp_sink_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, 167 uint8_t * codec_capabilities, uint16_t codec_capabilities_len, 168 uint8_t * media_codec_info, uint16_t media_codec_info_len){ 169 avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_sink_create_stream_endpoint(AVDTP_SINK, media_type); 170 if (!local_stream_endpoint){ 171 return NULL; 172 } 173 avdtp_sink_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint)); 174 avdtp_sink_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type, 175 codec_capabilities, codec_capabilities_len); 176 local_stream_endpoint->remote_configuration.media_codec.media_codec_information = media_codec_info; 177 local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_info_len; 178 avdtp_sink_register_delay_reporting_category(avdtp_stream_endpoint_seid(local_stream_endpoint)); 179 return local_stream_endpoint; 180 } 181 182 uint8_t a2dp_sink_establish_stream(bd_addr_t bd_addr, uint8_t local_seid, uint16_t * avdtp_cid){ 183 sc.local_stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_sink_context); 184 if (!sc.local_stream_endpoint){ 185 log_info("No local_stream_endpoint for seid %d", local_seid); 186 return AVDTP_SEID_DOES_NOT_EXIST; 187 } 188 // remember to tell client 189 send_stream_established_for_outgoing_connection = 1; 190 return avdtp_sink_connect(bd_addr, avdtp_cid); 191 } 192 193 void a2dp_sink_disconnect(uint16_t a2dp_cid){ 194 avdtp_disconnect(a2dp_cid, &a2dp_sink_context); 195 } 196 197 static inline void a2dp_signaling_emit_media_codec_sbc(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){ 198 if (!callback) return; 199 if (event_size < 18) return; 200 event[0] = HCI_EVENT_A2DP_META; 201 event[2] = A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION; 202 (*callback)(HCI_EVENT_PACKET, 0, event, event_size); 203 } 204 205 static inline void avdtp_signaling_emit_media_codec_other(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){ 206 if (!callback) return; 207 if (event_size < 113) return; 208 event[0] = HCI_EVENT_A2DP_META; 209 event[2] = A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION; 210 } 211 212 static inline void a2dp_emit_stream_event(btstack_packet_handler_t callback, uint16_t a2dp_cid, uint8_t eventID, uint8_t local_seid){ 213 if (!callback) return; 214 uint8_t event[6]; 215 int pos = 0; 216 event[pos++] = HCI_EVENT_A2DP_META; 217 event[pos++] = sizeof(event) - 2; 218 event[pos++] = eventID; 219 little_endian_store_16(event, pos, a2dp_cid); 220 pos += 2; 221 event[pos++] = local_seid; 222 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 223 } 224 225 static inline void a2dp_emit_signaling_connection_released(btstack_packet_handler_t callback, uint16_t a2dp_cid){ 226 if (!callback) return; 227 uint8_t event[5]; 228 int pos = 0; 229 event[pos++] = HCI_EVENT_A2DP_META; 230 event[pos++] = sizeof(event) - 2; 231 event[pos++] = A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED; 232 little_endian_store_16(event, pos, a2dp_cid); 233 pos += 2; 234 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 235 } 236 237 // static inline void a2dp_emit_cmd_accepted(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){ 238 // if (!callback) return; 239 // UNUSED(size); 240 // packet[0] = HCI_EVENT_A2DP_META; 241 // packet[2] = A2DP_SUBEVENT_COMMAND_ACCEPTED; 242 // (*callback)(HCI_EVENT_PACKET, 0, packet, size); 243 // } 244 245 static inline void a2dp_emit_cmd_rejected(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){ 246 if (!callback) return; 247 UNUSED(size); 248 packet[0] = HCI_EVENT_A2DP_META; 249 packet[2] = A2DP_SUBEVENT_COMMAND_REJECTED; 250 (*callback)(HCI_EVENT_PACKET, 0, packet, size); 251 } 252 253 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 254 UNUSED(channel); 255 UNUSED(size); 256 bd_addr_t address; 257 uint8_t status; 258 uint8_t signal_identifier; 259 uint16_t cid; 260 uint8_t loc_seid; 261 uint8_t rem_seid; 262 263 if (packet_type != HCI_EVENT_PACKET) return; 264 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return; 265 266 switch (packet[2]){ 267 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED: 268 avdtp_subevent_signaling_connection_established_get_bd_addr(packet, address); 269 cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet); 270 status = avdtp_subevent_signaling_connection_established_get_status(packet); 271 if (status != 0){ 272 app_state = A2DP_IDLE; 273 274 // only care for outgoing connections 275 if (send_stream_established_for_outgoing_connection == 0) break; 276 send_stream_established_for_outgoing_connection = 0; 277 278 log_info("AVDTP_SUBEVENT_SIGNALING_CONNECTION failed status %d ---", status); 279 a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, 0, 0, status); 280 break; 281 } 282 app_state = A2DP_CONNECTED; 283 log_info("AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED, avdtp cid 0x%02x ---", cid); 284 break; 285 286 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 287 log_info("received non SBC codec. not implemented"); 288 avdtp_signaling_emit_media_codec_other(a2dp_sink_context.a2dp_callback, packet, size); 289 break; 290 291 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION: 292 if (app_state < A2DP_CONNECTED) return; 293 a2dp_signaling_emit_media_codec_sbc(a2dp_sink_context.a2dp_callback, packet, size); 294 break; 295 296 case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: 297 avdtp_subevent_streaming_connection_established_get_bd_addr(packet, address); 298 status = avdtp_subevent_streaming_connection_established_get_status(packet); 299 cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet); 300 loc_seid = avdtp_subevent_streaming_connection_established_get_local_seid(packet); 301 rem_seid = avdtp_subevent_streaming_connection_established_get_remote_seid(packet); 302 log_info("AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED, avdtp cid 0x%02x, status %d ---", cid, status); 303 304 // about to notify client 305 send_stream_established_for_outgoing_connection = 0; 306 307 if (status){ 308 a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, loc_seid, rem_seid, status); 309 break; 310 } 311 app_state = A2DP_STREAMING_OPENED; 312 a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, loc_seid, rem_seid, 0); 313 break; 314 315 case AVDTP_SUBEVENT_SIGNALING_ACCEPT: 316 signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet); 317 cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet); 318 loc_seid = avdtp_subevent_signaling_accept_get_local_seid(packet); 319 320 switch (signal_identifier){ 321 case AVDTP_SI_START: 322 a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_STARTED, loc_seid); 323 break; 324 case AVDTP_SI_SUSPEND: 325 a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_SUSPENDED, loc_seid); 326 break; 327 case AVDTP_SI_ABORT: 328 case AVDTP_SI_CLOSE: 329 a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_STOPPED, loc_seid); 330 break; 331 default: 332 // a2dp_emit_cmd_accepted(a2dp_sink_context.a2dp_callback, packet, size); 333 break; 334 } 335 break; 336 337 case AVDTP_SUBEVENT_SIGNALING_REJECT: 338 case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT: 339 signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet); 340 cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet); 341 loc_seid = avdtp_subevent_signaling_accept_get_local_seid(packet); 342 a2dp_emit_cmd_rejected(a2dp_sink_context.a2dp_callback, packet, size); 343 app_state = A2DP_IDLE; 344 break; 345 case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED: 346 cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet); 347 loc_seid = avdtp_subevent_streaming_connection_released_get_local_seid(packet); 348 app_state = A2DP_IDLE; 349 a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_RELEASED, loc_seid); 350 break; 351 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 352 cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet); 353 354 // for outgoing connections, suppress release event and report stream established failed 355 if (send_stream_established_for_outgoing_connection){ 356 send_stream_established_for_outgoing_connection = 0; 357 log_info("A2DP sink outgoing connnection failed - disconnect"); 358 a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, 0, 0, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 359 break; 360 } 361 362 a2dp_emit_signaling_connection_released(a2dp_sink_context.a2dp_callback, cid); 363 app_state = A2DP_IDLE; 364 break; 365 default: 366 app_state = A2DP_IDLE; 367 log_info("not implemented"); 368 break; 369 } 370 371 } 372 373