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 BLUEKITCHEN 24 * GMBH 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_util.c" 39 40 #include <stdint.h> 41 #include <string.h> 42 43 #include "classic/avdtp.h" 44 #include "classic/avdtp_util.h" 45 46 #include "btstack_debug.h" 47 #include "btstack_util.h" 48 #include "l2cap.h" 49 50 /* 51 52 List of AVDTP_SUBEVENTs sorted by packet handler 53 54 55 Sink + Source: 56 - AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED 57 - AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED 58 - AVDTP_SUBEVENT_SIGNALING_SEP_FOUND 59 - AVDTP_SUBEVENT_SIGNALING_ACCEPT 60 - AVDTP_SUBEVENT_SIGNALING_REJECT 61 - AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT 62 - AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY 63 - AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY 64 - AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY 65 - AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY 66 - AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY 67 - AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY 68 - AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY 69 - AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY 70 - AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY 71 - AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE 72 - AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE 73 74 Source: 75 - AVDTP_SUBEVENT_SIGNALING_DELAY_REPORT 76 77 Sink or Source based on SEP Type: 78 - AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED 79 - AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED 80 - AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW 81 - AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION 82 - AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION 83 84 */ 85 86 static const char * avdtp_si_name[] = { 87 "ERROR", 88 "AVDTP_SI_DISCOVER", 89 "AVDTP_SI_GET_CAPABILITIES", 90 "AVDTP_SI_SET_CONFIGURATION", 91 "AVDTP_SI_GET_CONFIGURATION", 92 "AVDTP_SI_RECONFIGURE", 93 "AVDTP_SI_OPEN", 94 "AVDTP_SI_START", 95 "AVDTP_SI_CLOSE", 96 "AVDTP_SI_SUSPEND", 97 "AVDTP_SI_ABORT", 98 "AVDTP_SI_SECURITY_CONTROL", 99 "AVDTP_SI_GET_ALL_CAPABILITIES", 100 "AVDTP_SI_DELAY_REPORT" 101 }; 102 const char * avdtp_si2str(uint16_t index){ 103 if ((index <= 0) || (index >= sizeof(avdtp_si_name)/sizeof(avdtp_si_name[0]) )) return avdtp_si_name[0]; 104 return avdtp_si_name[index]; 105 } 106 107 void avdtp_reset_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){ 108 stream_endpoint->media_con_handle = 0; 109 stream_endpoint->l2cap_media_cid = 0; 110 stream_endpoint->l2cap_reporting_cid = 0; 111 stream_endpoint->l2cap_recovery_cid = 0; 112 113 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE; 114 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; 115 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE; 116 117 stream_endpoint->connection = NULL; 118 119 stream_endpoint->sep.in_use = 0; 120 memset(&stream_endpoint->remote_sep, 0, sizeof(avdtp_sep_t)); 121 122 stream_endpoint->remote_capabilities_bitmap = 0; 123 memset(&stream_endpoint->remote_capabilities, 0, sizeof(avdtp_capabilities_t)); 124 stream_endpoint->remote_configuration_bitmap = 0; 125 memset(&stream_endpoint->remote_configuration, 0, sizeof(avdtp_capabilities_t)); 126 127 // temporary SBC config used by A2DP Source 128 memset(stream_endpoint->media_codec_info, 0, 8); 129 130 stream_endpoint->media_disconnect = 0; 131 stream_endpoint->media_connect = 0; 132 stream_endpoint->start_stream = 0; 133 stream_endpoint->close_stream = 0; 134 stream_endpoint->request_can_send_now = false; 135 stream_endpoint->abort_stream = 0; 136 stream_endpoint->suspend_stream = 0; 137 stream_endpoint->sequence_number = 0; 138 } 139 140 int get_bit16(uint16_t bitmap, int position){ 141 return (bitmap >> position) & 1; 142 } 143 144 uint16_t store_bit16(uint16_t bitmap, int position, uint8_t value){ 145 if (value){ 146 bitmap |= 1 << position; 147 } else { 148 bitmap &= ~ (1 << position); 149 } 150 return bitmap; 151 } 152 153 avdtp_message_type_t avdtp_get_signaling_packet_type(uint8_t * packet){ 154 return (avdtp_message_type_t) (packet[0] & 0x03); 155 } 156 157 int avdtp_read_signaling_header(avdtp_signaling_packet_t * signaling_header, uint8_t * packet, uint16_t size){ 158 int pos = 0; 159 if (size < 2) return pos; 160 signaling_header->transaction_label = packet[pos] >> 4; 161 signaling_header->packet_type = (avdtp_packet_type_t)((packet[pos] >> 2) & 0x03); 162 signaling_header->message_type = (avdtp_message_type_t) (packet[pos] & 0x03); 163 pos++; 164 memset(signaling_header->command, 0, sizeof(signaling_header->command)); 165 switch (signaling_header->packet_type){ 166 case AVDTP_SINGLE_PACKET: 167 signaling_header->num_packets = 0; 168 signaling_header->offset = 0; 169 signaling_header->size = 0; 170 break; 171 case AVDTP_END_PACKET: 172 signaling_header->num_packets = 0; 173 break; 174 case AVDTP_START_PACKET: 175 signaling_header->num_packets = packet[pos++]; 176 signaling_header->size = 0; 177 signaling_header->offset = 0; 178 break; 179 case AVDTP_CONTINUE_PACKET: 180 if (signaling_header->num_packets <= 0) { 181 log_info(" ERROR: wrong num fragmented packets\n"); 182 break; 183 } 184 signaling_header->num_packets--; 185 break; 186 default: 187 btstack_assert(false); 188 break; 189 } 190 signaling_header->signal_identifier = (avdtp_signal_identifier_t)(packet[pos++] & 0x3f); 191 return pos; 192 } 193 194 static bool avdtp_is_basic_capability(int service_category){ 195 return (AVDTP_MEDIA_TRANSPORT <= service_category) && (service_category <= AVDTP_MEDIA_CODEC); 196 } 197 198 int avdtp_pack_service_capabilities(uint8_t *buffer, int size, avdtp_capabilities_t caps, avdtp_service_category_t category) { 199 UNUSED(size); 200 201 int i; 202 // pos = 0 reserved for length 203 int pos = 1; 204 switch(category){ 205 case AVDTP_MEDIA_TRANSPORT: 206 case AVDTP_REPORTING: 207 case AVDTP_DELAY_REPORTING: 208 break; 209 case AVDTP_RECOVERY: 210 buffer[pos++] = caps.recovery.recovery_type; // 0x01=RFC2733 211 buffer[pos++] = caps.recovery.maximum_recovery_window_size; 212 buffer[pos++] = caps.recovery.maximum_number_media_packets; 213 break; 214 case AVDTP_CONTENT_PROTECTION: 215 buffer[pos++] = caps.content_protection.cp_type_value_len + 2; 216 big_endian_store_16(buffer, pos, caps.content_protection.cp_type); 217 pos += 2; 218 (void)memcpy(buffer + pos, caps.content_protection.cp_type_value, 219 caps.content_protection.cp_type_value_len); 220 pos += caps.content_protection.cp_type_value_len; 221 break; 222 case AVDTP_HEADER_COMPRESSION: 223 buffer[pos++] = (caps.header_compression.back_ch << 7) | (caps.header_compression.media << 6) | (caps.header_compression.recovery << 5); 224 break; 225 case AVDTP_MULTIPLEXING: 226 buffer[pos++] = caps.multiplexing_mode.fragmentation << 7; 227 for (i=0; i<caps.multiplexing_mode.transport_identifiers_num; i++){ 228 buffer[pos++] = caps.multiplexing_mode.transport_session_identifiers[i] << 7; 229 buffer[pos++] = caps.multiplexing_mode.tcid[i] << 7; 230 // media, reporting. recovery 231 } 232 break; 233 case AVDTP_MEDIA_CODEC: 234 buffer[pos++] = ((uint8_t)caps.media_codec.media_type) << 4; 235 buffer[pos++] = (uint8_t)caps.media_codec.media_codec_type; 236 for (i = 0; i<caps.media_codec.media_codec_information_len; i++){ 237 buffer[pos++] = caps.media_codec.media_codec_information[i]; 238 } 239 break; 240 default: 241 break; 242 } 243 buffer[0] = pos - 1; // length 244 return pos; 245 } 246 247 static int avdtp_unpack_service_capabilities_has_errors(avdtp_connection_t * connection, avdtp_signal_identifier_t signal_identifier, avdtp_service_category_t category, uint8_t cap_len){ 248 connection->error_code = 0; 249 250 if ((category == AVDTP_SERVICE_CATEGORY_INVALID_0) || 251 ((category == AVDTP_SERVICE_CATEGORY_INVALID_FF) && (signal_identifier == AVDTP_SI_RECONFIGURE))){ 252 log_info(" ERROR: BAD SERVICE CATEGORY %d\n", category); 253 connection->reject_service_category = category; 254 connection->error_code = AVDTP_ERROR_CODE_BAD_SERV_CATEGORY; 255 return 1; 256 } 257 258 if (signal_identifier == AVDTP_SI_RECONFIGURE){ 259 if ( (category != AVDTP_CONTENT_PROTECTION) && (category != AVDTP_MEDIA_CODEC)){ 260 log_info(" ERROR: REJECT CATEGORY, INVALID_CAPABILITIES\n"); 261 connection->reject_service_category = category; 262 connection->error_code = AVDTP_ERROR_CODE_INVALID_CAPABILITIES; 263 return 1; 264 } 265 } 266 267 switch(category){ 268 case AVDTP_MEDIA_TRANSPORT: 269 if (cap_len != 0){ 270 log_info(" ERROR: REJECT CATEGORY, BAD_MEDIA_TRANSPORT\n"); 271 connection->reject_service_category = category; 272 connection->error_code = AVDTP_ERROR_CODE_BAD_MEDIA_TRANSPORT_FORMAT; 273 return 1; 274 } 275 break; 276 case AVDTP_REPORTING: 277 case AVDTP_DELAY_REPORTING: 278 if (cap_len != 0){ 279 log_info(" ERROR: REJECT CATEGORY, BAD_LENGTH\n"); 280 connection->reject_service_category = category; 281 connection->error_code = AVDTP_ERROR_CODE_BAD_LENGTH; 282 return 1; 283 } 284 break; 285 case AVDTP_RECOVERY: 286 if (cap_len != 3){ 287 log_info(" ERROR: REJECT CATEGORY, BAD_MEDIA_TRANSPORT\n"); 288 connection->reject_service_category = category; 289 connection->error_code = AVDTP_ERROR_CODE_BAD_RECOVERY_FORMAT; 290 return 1; 291 } 292 break; 293 case AVDTP_CONTENT_PROTECTION: 294 if (cap_len < 2){ 295 log_info(" ERROR: REJECT CATEGORY, BAD_CP_FORMAT\n"); 296 connection->reject_service_category = category; 297 connection->error_code = AVDTP_ERROR_CODE_BAD_CP_FORMAT; 298 return 1; 299 } 300 break; 301 case AVDTP_HEADER_COMPRESSION: 302 // TODO: find error code for bad header compression 303 if (cap_len != 1){ 304 log_info(" ERROR: REJECT CATEGORY, BAD_HEADER_COMPRESSION\n"); 305 connection->reject_service_category = category; 306 connection->error_code = AVDTP_ERROR_CODE_BAD_RECOVERY_FORMAT; 307 return 1; 308 } 309 break; 310 case AVDTP_MULTIPLEXING: 311 break; 312 case AVDTP_MEDIA_CODEC: 313 break; 314 default: 315 break; 316 } 317 return 0; 318 } 319 320 uint16_t avdtp_unpack_service_capabilities(avdtp_connection_t * connection, avdtp_signal_identifier_t signal_identifier, avdtp_capabilities_t * caps, uint8_t * packet, uint16_t size){ 321 322 int i; 323 324 uint16_t registered_service_categories = 0; 325 uint16_t to_process = size; 326 327 while (to_process >= 2){ 328 329 avdtp_service_category_t category = (avdtp_service_category_t) packet[0]; 330 uint8_t cap_len = packet[1]; 331 packet += 2; 332 to_process -= 2; 333 334 if (cap_len > to_process){ 335 connection->reject_service_category = category; 336 connection->error_code = AVDTP_ERROR_CODE_BAD_LENGTH; 337 return 0; 338 } 339 340 if (avdtp_unpack_service_capabilities_has_errors(connection, signal_identifier, category, cap_len)) return 0; 341 342 int category_valid = 1; 343 344 uint8_t * data = packet; 345 uint16_t pos = 0; 346 347 switch(category){ 348 case AVDTP_RECOVERY: 349 caps->recovery.recovery_type = data[pos++]; 350 caps->recovery.maximum_recovery_window_size = data[pos++]; 351 caps->recovery.maximum_number_media_packets = data[pos++]; 352 break; 353 case AVDTP_CONTENT_PROTECTION: 354 caps->content_protection.cp_type = big_endian_read_16(data, 0); 355 caps->content_protection.cp_type_value_len = cap_len - 2; 356 // connection->reject_service_category = category; 357 // connection->error_code = UNSUPPORTED_CONFIGURATION; 358 // support for content protection goes here 359 break; 360 case AVDTP_HEADER_COMPRESSION: 361 caps->header_compression.back_ch = (data[0] >> 7) & 1; 362 caps->header_compression.media = (data[0] >> 6) & 1; 363 caps->header_compression.recovery = (data[0] >> 5) & 1; 364 break; 365 case AVDTP_MULTIPLEXING: 366 caps->multiplexing_mode.fragmentation = (data[pos++] >> 7) & 1; 367 // read [tsid, tcid] for media, reporting. recovery respectively 368 caps->multiplexing_mode.transport_identifiers_num = 3; 369 for (i=0; i<caps->multiplexing_mode.transport_identifiers_num; i++){ 370 caps->multiplexing_mode.transport_session_identifiers[i] = (data[pos++] >> 7) & 1; 371 caps->multiplexing_mode.tcid[i] = (data[pos++] >> 7) & 1; 372 } 373 break; 374 case AVDTP_MEDIA_CODEC: 375 caps->media_codec.media_type = (avdtp_media_type_t)(data[pos++] >> 4); 376 caps->media_codec.media_codec_type = (avdtp_media_codec_type_t)(data[pos++]); 377 caps->media_codec.media_codec_information_len = cap_len - 2; 378 caps->media_codec.media_codec_information = &data[pos++]; 379 break; 380 case AVDTP_MEDIA_TRANSPORT: 381 case AVDTP_REPORTING: 382 case AVDTP_DELAY_REPORTING: 383 break; 384 default: 385 category_valid = 0; 386 break; 387 } 388 389 if (category_valid) { 390 registered_service_categories = store_bit16(registered_service_categories, category, 1); 391 } 392 393 packet += cap_len; 394 to_process -= cap_len; 395 } 396 397 return registered_service_categories; 398 } 399 400 void avdtp_prepare_capabilities(avdtp_signaling_packet_t * signaling_packet, uint8_t transaction_label, uint16_t service_categories, avdtp_capabilities_t capabilities, uint8_t identifier){ 401 if (signaling_packet->offset) return; 402 bool basic_capabilities_only = false; 403 signaling_packet->message_type = AVDTP_RESPONSE_ACCEPT_MSG; 404 int i; 405 406 signaling_packet->size = 0; 407 memset(signaling_packet->command, 0 , sizeof(signaling_packet->command)); 408 409 switch (identifier) { 410 case AVDTP_SI_GET_CAPABILITIES: 411 basic_capabilities_only = true; 412 break; 413 case AVDTP_SI_GET_ALL_CAPABILITIES: 414 break; 415 case AVDTP_SI_SET_CONFIGURATION: 416 signaling_packet->command[signaling_packet->size++] = signaling_packet->acp_seid << 2; 417 signaling_packet->command[signaling_packet->size++] = signaling_packet->int_seid << 2; 418 signaling_packet->message_type = AVDTP_CMD_MSG; 419 break; 420 case AVDTP_SI_RECONFIGURE: 421 signaling_packet->command[signaling_packet->size++] = signaling_packet->acp_seid << 2; 422 signaling_packet->message_type = AVDTP_CMD_MSG; 423 break; 424 default: 425 log_error("avdtp_prepare_capabilities wrong identifier %d", identifier); 426 break; 427 } 428 429 for (i = AVDTP_MEDIA_TRANSPORT; i <= AVDTP_DELAY_REPORTING; i++){ 430 int registered_category = get_bit16(service_categories, i); 431 if (!registered_category && (identifier == AVDTP_SI_SET_CONFIGURATION)){ 432 // TODO: introduce bitmap of mandatory categories 433 if (i == AVDTP_MEDIA_TRANSPORT){ 434 registered_category = true; 435 } 436 } 437 // AVDTP_SI_GET_CAPABILITIES reports only basic capabilities (i.e., it skips non-basic categories) 438 if (basic_capabilities_only && !avdtp_is_basic_capability(i)){ 439 registered_category = false; 440 } 441 442 if (registered_category){ 443 // service category 444 signaling_packet->command[signaling_packet->size++] = i; 445 signaling_packet->size += avdtp_pack_service_capabilities(signaling_packet->command + signaling_packet->size, 446 sizeof(signaling_packet->command) - signaling_packet->size, capabilities, (avdtp_service_category_t) i); 447 } 448 } 449 signaling_packet->signal_identifier = (avdtp_signal_identifier_t)identifier; 450 signaling_packet->transaction_label = transaction_label; 451 } 452 453 int avdtp_signaling_create_fragment(uint16_t cid, avdtp_signaling_packet_t * signaling_packet, uint8_t * out_buffer) { 454 int mtu = l2cap_get_remote_mtu_for_local_cid(cid); 455 int data_len = 0; 456 457 uint16_t offset = signaling_packet->offset; 458 uint16_t pos = 1; 459 460 if (offset == 0){ 461 if (signaling_packet->size <= (mtu - 2)){ 462 signaling_packet->packet_type = AVDTP_SINGLE_PACKET; 463 out_buffer[pos++] = signaling_packet->signal_identifier; 464 data_len = signaling_packet->size; 465 } else { 466 signaling_packet->packet_type = AVDTP_START_PACKET; 467 out_buffer[pos++] = (mtu + signaling_packet->size)/ (mtu-1); 468 out_buffer[pos++] = signaling_packet->signal_identifier; 469 data_len = mtu - 3; 470 signaling_packet->offset = data_len; 471 } 472 } else { 473 int remaining_bytes = signaling_packet->size - offset; 474 if (remaining_bytes <= (mtu - 1)){ 475 signaling_packet->packet_type = AVDTP_END_PACKET; 476 data_len = remaining_bytes; 477 signaling_packet->offset = 0; 478 } else{ 479 signaling_packet->packet_type = AVDTP_CONTINUE_PACKET; 480 data_len = mtu - 1; 481 signaling_packet->offset += data_len; 482 } 483 } 484 out_buffer[0] = avdtp_header(signaling_packet->transaction_label, signaling_packet->packet_type, signaling_packet->message_type); 485 (void)memcpy(out_buffer + pos, signaling_packet->command + offset, 486 data_len); 487 pos += data_len; 488 return pos; 489 } 490 491 492 void avdtp_signaling_emit_connection_established(uint16_t avdtp_cid, bd_addr_t addr, hci_con_handle_t con_handle, uint8_t status) { 493 uint8_t event[14]; 494 int pos = 0; 495 event[pos++] = HCI_EVENT_AVDTP_META; 496 event[pos++] = sizeof(event) - 2; 497 event[pos++] = AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED; 498 little_endian_store_16(event, pos, avdtp_cid); 499 pos += 2; 500 reverse_bd_addr(addr,&event[pos]); 501 pos += 6; 502 little_endian_store_16(event, pos, con_handle); 503 pos += 2; 504 event[pos++] = status; 505 avdtp_emit_sink_and_source(event, pos); 506 } 507 508 void avdtp_signaling_emit_connection_released(uint16_t avdtp_cid) { 509 uint8_t event[5]; 510 int pos = 0; 511 event[pos++] = HCI_EVENT_AVDTP_META; 512 event[pos++] = sizeof(event) - 2; 513 event[pos++] = AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED; 514 little_endian_store_16(event, pos, avdtp_cid); 515 pos += 2; 516 avdtp_emit_sink_and_source(event, pos); 517 } 518 519 void avdtp_signaling_emit_sep(uint16_t avdtp_cid, avdtp_sep_t sep) { 520 uint8_t event[9]; 521 int pos = 0; 522 event[pos++] = HCI_EVENT_AVDTP_META; 523 event[pos++] = sizeof(event) - 2; 524 event[pos++] = AVDTP_SUBEVENT_SIGNALING_SEP_FOUND; 525 little_endian_store_16(event, pos, avdtp_cid); 526 pos += 2; 527 event[pos++] = sep.seid; 528 event[pos++] = sep.in_use; 529 event[pos++] = sep.media_type; 530 event[pos++] = sep.type; 531 avdtp_emit_sink_and_source(event, pos); 532 } 533 534 void avdtp_signaling_emit_sep_done(uint16_t avdtp_cid) { 535 uint8_t event[5]; 536 int pos = 0; 537 event[pos++] = HCI_EVENT_AVDTP_META; 538 event[pos++] = sizeof(event) - 2; 539 event[pos++] = AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE; 540 little_endian_store_16(event, pos, avdtp_cid); 541 pos += 2; 542 avdtp_emit_sink_and_source(event, pos); 543 } 544 545 void avdtp_signaling_emit_accept(uint16_t avdtp_cid, uint8_t local_seid, avdtp_signal_identifier_t identifier, bool is_initiator) { 546 uint8_t event[8]; 547 int pos = 0; 548 event[pos++] = HCI_EVENT_AVDTP_META; 549 event[pos++] = sizeof(event) - 2; 550 event[pos++] = AVDTP_SUBEVENT_SIGNALING_ACCEPT; 551 little_endian_store_16(event, pos, avdtp_cid); 552 pos += 2; 553 event[pos++] = local_seid; 554 event[pos++] = is_initiator ? 1 : 0; 555 event[pos++] = identifier; 556 avdtp_emit_sink_and_source(event, pos); 557 } 558 559 void avdtp_signaling_emit_accept_for_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint, uint8_t local_seid, avdtp_signal_identifier_t identifier, bool is_initiator){ 560 uint8_t event[8]; 561 int pos = 0; 562 event[pos++] = HCI_EVENT_AVDTP_META; 563 event[pos++] = sizeof(event) - 2; 564 event[pos++] = AVDTP_SUBEVENT_SIGNALING_ACCEPT; 565 little_endian_store_16(event, pos, stream_endpoint->connection->avdtp_cid); 566 pos += 2; 567 event[pos++] = local_seid; 568 event[pos++] = is_initiator ? 1 : 0; 569 event[pos++] = identifier; 570 571 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint); 572 (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 573 } 574 575 void avdtp_signaling_emit_reject(uint16_t avdtp_cid, uint8_t local_seid, avdtp_signal_identifier_t identifier, bool is_initiator) { 576 uint8_t event[8]; 577 int pos = 0; 578 event[pos++] = HCI_EVENT_AVDTP_META; 579 event[pos++] = sizeof(event) - 2; 580 event[pos++] = AVDTP_SUBEVENT_SIGNALING_REJECT; 581 little_endian_store_16(event, pos, avdtp_cid); 582 pos += 2; 583 event[pos++] = local_seid; 584 event[pos++] = is_initiator ? 1 : 0; 585 event[pos++] = identifier; 586 avdtp_emit_sink_and_source(event, pos); 587 } 588 589 void avdtp_signaling_emit_general_reject(uint16_t avdtp_cid, uint8_t local_seid, avdtp_signal_identifier_t identifier, bool is_initiator) { 590 uint8_t event[8]; 591 int pos = 0; 592 event[pos++] = HCI_EVENT_AVDTP_META; 593 event[pos++] = sizeof(event) - 2; 594 event[pos++] = AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT; 595 little_endian_store_16(event, pos, avdtp_cid); 596 pos += 2; 597 event[pos++] = local_seid; 598 event[pos++] = is_initiator ? 1 : 0; 599 event[pos++] = identifier; 600 avdtp_emit_sink_and_source(event, pos); 601 } 602 603 static inline void 604 avdtp_signaling_emit_capability(uint8_t capability_subevent_id, uint16_t avdtp_cid, uint8_t remote_seid) { 605 uint8_t event[6]; 606 int pos = 0; 607 event[pos++] = HCI_EVENT_AVDTP_META; 608 event[pos++] = sizeof(event) - 2; 609 event[pos++] = capability_subevent_id; 610 little_endian_store_16(event, pos, avdtp_cid); 611 pos += 2; 612 event[pos++] = remote_seid; 613 avdtp_emit_sink_and_source(event, pos); 614 } 615 616 static void avdtp_signaling_emit_media_codec_sbc_capability(uint16_t avdtp_cid, uint8_t remote_seid, adtvp_media_codec_capabilities_t media_codec) { 617 const uint8_t * media_codec_information = media_codec.media_codec_information; 618 uint8_t event[14]; 619 int pos = 0; 620 event[pos++] = HCI_EVENT_AVDTP_META; 621 event[pos++] = sizeof(event) - 2; 622 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY; 623 little_endian_store_16(event, pos, avdtp_cid); 624 pos += 2; 625 event[pos++] = remote_seid; 626 event[pos++] = media_codec.media_type; 627 event[pos++] = media_codec_information[0] >> 4; 628 event[pos++] = media_codec_information[0] & 0x0F; 629 event[pos++] = media_codec_information[1] >> 4; 630 event[pos++] = (media_codec_information[1] & 0x0F) >> 2; 631 event[pos++] = media_codec_information[1] & 0x03; 632 event[pos++] = media_codec_information[2]; 633 event[pos++] = media_codec_information[3]; 634 avdtp_emit_sink_and_source(event, pos); 635 } 636 637 static void avdtp_signaling_emit_media_codec_mpeg_audio_capability(uint16_t avdtp_cid, uint8_t remote_seid, adtvp_media_codec_capabilities_t media_codec) { 638 const uint8_t * media_codec_information = media_codec.media_codec_information; 639 uint8_t event[15]; 640 int pos = 0; 641 event[pos++] = HCI_EVENT_AVDTP_META; 642 event[pos++] = sizeof(event) - 2; 643 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY; 644 little_endian_store_16(event, pos, avdtp_cid); 645 pos += 2; 646 event[pos++] = remote_seid; 647 event[pos++] = media_codec.media_type; 648 649 uint8_t layer_bitmap = media_codec_information[0] >> 5; 650 uint8_t crc = (media_codec_information[0] >> 4) & 0x01; 651 uint8_t channel_mode_bitmap = media_codec_information[0] & 0x07; 652 uint8_t mpf = (media_codec_information[1] >> 6) & 0x01; 653 uint8_t sampling_frequency_bitmap = media_codec_information[1] & 0x3F; 654 uint8_t vbr = (media_codec_information[2] >> 7) & 0x01; 655 uint16_t bit_rate_index_bitmap = ((media_codec_information[3] & 0x3f) << 8) | media_codec.media_codec_information[4]; 656 657 event[pos++] = layer_bitmap; 658 event[pos++] = crc; 659 event[pos++] = channel_mode_bitmap; 660 event[pos++] = mpf; 661 event[pos++] = sampling_frequency_bitmap; 662 event[pos++] = vbr; 663 little_endian_store_16(event, pos, bit_rate_index_bitmap); // bit rate index 664 pos += 2; 665 avdtp_emit_sink_and_source(event, pos); 666 } 667 668 static void avdtp_signaling_emit_media_codec_mpeg_aac_capability(uint16_t avdtp_cid, uint8_t remote_seid, adtvp_media_codec_capabilities_t media_codec) { 669 const uint8_t * media_codec_information = media_codec.media_codec_information; 670 uint8_t event[15]; 671 int pos = 0; 672 event[pos++] = HCI_EVENT_AVDTP_META; 673 event[pos++] = sizeof(event) - 2; 674 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY; 675 little_endian_store_16(event, pos, avdtp_cid); 676 pos += 2; 677 event[pos++] = remote_seid; 678 event[pos++] = media_codec.media_type; 679 680 uint8_t object_type_bitmap = media_codec_information[0]; 681 uint16_t sampling_frequency_bitmap = (media_codec_information[1] << 4) | (media_codec_information[2] >> 4); 682 uint8_t channels_bitmap = (media_codec_information[2] >> 2) & 0x03; 683 uint32_t bit_rate_bitmap = ((media_codec_information[3] & 0x7f) << 16) | (media_codec_information[4] << 8) | media_codec_information[5]; 684 uint8_t vbr = media_codec_information[3] >> 7; 685 686 event[pos++] = object_type_bitmap; 687 little_endian_store_16(event, pos, sampling_frequency_bitmap); 688 pos += 2; 689 event[pos++] = channels_bitmap; 690 little_endian_store_24(event, pos, bit_rate_bitmap); 691 pos += 3; 692 event[pos++] = vbr; 693 avdtp_emit_sink_and_source(event, pos); 694 } 695 696 static void avdtp_signaling_emit_media_codec_atrac_capability(uint16_t avdtp_cid, uint8_t remote_seid, adtvp_media_codec_capabilities_t media_codec) { 697 const uint8_t * media_codec_information = media_codec.media_codec_information; 698 uint8_t event[16]; 699 int pos = 0; 700 event[pos++] = HCI_EVENT_AVDTP_META; 701 pos++; // set later 702 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY; 703 little_endian_store_16(event, pos, avdtp_cid); 704 pos += 2; 705 event[pos++] = remote_seid; 706 event[pos++] = media_codec.media_type; 707 708 uint8_t version = media_codec_information[0] >> 5; 709 uint8_t channel_mode_bitmap = (media_codec_information[0] >> 2) & 0x07; 710 uint8_t sampling_frequency_bitmap = (media_codec_information[1] >> 4) & 0x03; 711 uint8_t vbr = (media_codec_information[1] >> 3) & 0x01; 712 uint16_t bit_rate_index_bitmap = ((media_codec_information[1]) & 0x07) << 16 | (media_codec_information[2] << 8) | media_codec_information[3]; 713 uint16_t maximum_sul = (media_codec_information[4] << 8) | media_codec_information[5]; 714 715 event[pos++] = version; 716 event[pos++] = channel_mode_bitmap; 717 event[pos++] = sampling_frequency_bitmap; 718 event[pos++] = vbr; 719 little_endian_store_24(event, pos, bit_rate_index_bitmap); 720 pos += 3; 721 little_endian_store_16(event, pos, maximum_sul); 722 pos += 2; 723 event[1] = pos - 2; 724 avdtp_emit_sink_and_source(event, pos); 725 } 726 727 static void avdtp_signaling_emit_media_codec_other_capability(uint16_t avdtp_cid, uint8_t remote_seid, adtvp_media_codec_capabilities_t media_codec) { 728 uint8_t event[AVDTP_MAX_MEDIA_CODEC_INFORMATION_LENGTH + 11]; 729 int pos = 0; 730 event[pos++] = HCI_EVENT_AVDTP_META; 731 pos++; // set later 732 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY; 733 little_endian_store_16(event, pos, avdtp_cid); 734 pos += 2; 735 event[pos++] = remote_seid; 736 event[pos++] = media_codec.media_type; 737 little_endian_store_16(event, pos, media_codec.media_codec_type); 738 pos += 2; 739 little_endian_store_16(event, pos, media_codec.media_codec_information_len); 740 pos += 2; 741 uint32_t media_codec_info_len = btstack_min(media_codec.media_codec_information_len, AVDTP_MAX_MEDIA_CODEC_INFORMATION_LENGTH); 742 (void)memcpy(event + pos, media_codec.media_codec_information, media_codec_info_len); 743 pos += media_codec_info_len; 744 event[1] = pos - 2; 745 avdtp_emit_sink_and_source(event, pos); 746 } 747 748 static void 749 avdtp_signaling_emit_media_transport_capability(uint16_t avdtp_cid, uint8_t remote_seid) { 750 avdtp_signaling_emit_capability(AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY, avdtp_cid, 751 remote_seid); 752 } 753 754 static void avdtp_signaling_emit_reporting_capability(uint16_t avdtp_cid, uint8_t remote_seid) { 755 avdtp_signaling_emit_capability(AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY, avdtp_cid, remote_seid); 756 } 757 758 static void 759 avdtp_signaling_emit_delay_reporting_capability(uint16_t avdtp_cid, uint8_t remote_seid) { 760 avdtp_signaling_emit_capability(AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY, avdtp_cid, 761 remote_seid); 762 } 763 764 static void avdtp_signaling_emit_recovery_capability(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_recovery_capabilities_t *recovery) { 765 uint8_t event[9]; 766 int pos = 0; 767 event[pos++] = HCI_EVENT_AVDTP_META; 768 event[pos++] = sizeof(event) - 2; 769 event[pos++] = AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY; 770 little_endian_store_16(event, pos, avdtp_cid); 771 pos += 2; 772 event[pos++] = remote_seid; 773 event[pos++] = recovery->recovery_type; 774 event[pos++] = recovery->maximum_recovery_window_size; 775 event[pos++] = recovery->maximum_number_media_packets; 776 avdtp_emit_sink_and_source(event, pos); 777 } 778 779 #define MAX_CONTENT_PROTECTION_VALUE_LEN 32 780 static void 781 avdtp_signaling_emit_content_protection_capability(uint16_t avdtp_cid, uint8_t remote_seid, adtvp_content_protection_t *content_protection) { 782 uint8_t event[10 + MAX_CONTENT_PROTECTION_VALUE_LEN]; 783 int pos = 0; 784 event[pos++] = HCI_EVENT_AVDTP_META; 785 pos++; // set later 786 event[pos++] = AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY; 787 little_endian_store_16(event, pos, avdtp_cid); 788 pos += 2; 789 event[pos++] = remote_seid; 790 791 little_endian_store_16(event, pos, content_protection->cp_type); 792 pos += 2; 793 794 // drop cp protection value if longer than expected 795 if (content_protection->cp_type_value_len <= MAX_CONTENT_PROTECTION_VALUE_LEN){ 796 little_endian_store_16(event, pos, content_protection->cp_type_value_len); 797 pos += 2; 798 (void)memcpy(event + pos, content_protection->cp_type_value, content_protection->cp_type_value_len); 799 pos += content_protection->cp_type_value_len; 800 } else { 801 little_endian_store_16(event, pos, 0); 802 pos += 2; 803 } 804 event[1] = pos - 2; 805 avdtp_emit_sink_and_source(event, pos); 806 } 807 808 809 static void 810 avdtp_signaling_emit_header_compression_capability(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_header_compression_capabilities_t *header_compression) { 811 uint8_t event[9]; 812 int pos = 0; 813 event[pos++] = HCI_EVENT_AVDTP_META; 814 event[pos++] = sizeof(event) - 2; 815 event[pos++] = AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY; 816 little_endian_store_16(event, pos, avdtp_cid); 817 pos += 2; 818 event[pos++] = remote_seid; 819 event[pos++] = header_compression->back_ch; 820 event[pos++] = header_compression->media; 821 event[pos++] = header_compression->recovery; 822 avdtp_emit_sink_and_source(event, pos); 823 } 824 825 static void 826 avdtp_signaling_emit_content_multiplexing_capability(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_multiplexing_mode_capabilities_t *multiplexing_mode) { 827 uint8_t event[14]; 828 int pos = 0; 829 event[pos++] = HCI_EVENT_AVDTP_META; 830 event[pos++] = sizeof(event) - 2; 831 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY; 832 little_endian_store_16(event, pos, avdtp_cid); 833 pos += 2; 834 event[pos++] = remote_seid; 835 836 event[pos++] = multiplexing_mode->fragmentation; 837 event[pos++] = multiplexing_mode->transport_identifiers_num; 838 839 int i; 840 for (i = 0; i < 3; i++){ 841 event[pos++] = multiplexing_mode->transport_session_identifiers[i]; 842 } 843 for (i = 0; i < 3; i++){ 844 event[pos++] = multiplexing_mode->tcid[i]; 845 } 846 avdtp_emit_sink_and_source(event, pos); 847 } 848 849 static void avdtp_signaling_emit_capability_done(uint16_t avdtp_cid, uint8_t remote_seid) { 850 uint8_t event[6]; 851 int pos = 0; 852 event[pos++] = HCI_EVENT_AVDTP_META; 853 event[pos++] = sizeof(event) - 2; 854 event[pos++] = AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE; 855 little_endian_store_16(event, pos, avdtp_cid); 856 pos += 2; 857 event[pos++] = remote_seid; 858 avdtp_emit_sink_and_source(event, pos); 859 } 860 861 static void avdtp_signaling_emit_media_codec_capability(uint16_t avdtp_cid, uint8_t remote_seid, adtvp_media_codec_capabilities_t media_codec){ 862 switch (media_codec.media_codec_type){ 863 case AVDTP_CODEC_SBC: 864 avdtp_signaling_emit_media_codec_sbc_capability(avdtp_cid, remote_seid, media_codec); 865 break; 866 case AVDTP_CODEC_MPEG_1_2_AUDIO: 867 avdtp_signaling_emit_media_codec_mpeg_audio_capability(avdtp_cid, remote_seid, media_codec); 868 break; 869 case AVDTP_CODEC_MPEG_2_4_AAC: 870 avdtp_signaling_emit_media_codec_mpeg_aac_capability(avdtp_cid, remote_seid, media_codec); 871 break; 872 case AVDTP_CODEC_ATRAC_FAMILY: 873 avdtp_signaling_emit_media_codec_atrac_capability(avdtp_cid, remote_seid, media_codec); 874 break; 875 default: 876 avdtp_signaling_emit_media_codec_other_capability(avdtp_cid, remote_seid, media_codec); 877 break; 878 } 879 } 880 881 // emit events for all capabilities incl. final done event 882 void avdtp_signaling_emit_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_capabilities_t *capabilities, 883 uint16_t registered_service_categories) { 884 if (get_bit16(registered_service_categories, AVDTP_MEDIA_CODEC)){ 885 avdtp_signaling_emit_media_codec_capability(avdtp_cid, remote_seid, capabilities->media_codec); 886 } 887 888 if (get_bit16(registered_service_categories, AVDTP_MEDIA_TRANSPORT)){ 889 avdtp_signaling_emit_media_transport_capability(avdtp_cid, remote_seid); 890 } 891 if (get_bit16(registered_service_categories, AVDTP_REPORTING)){ 892 avdtp_signaling_emit_reporting_capability(avdtp_cid, remote_seid); 893 } 894 if (get_bit16(registered_service_categories, AVDTP_RECOVERY)){ 895 avdtp_signaling_emit_recovery_capability(avdtp_cid, remote_seid, &capabilities->recovery); 896 } 897 if (get_bit16(registered_service_categories, AVDTP_CONTENT_PROTECTION)){ 898 avdtp_signaling_emit_content_protection_capability(avdtp_cid, remote_seid, 899 &capabilities->content_protection); 900 } 901 if (get_bit16(registered_service_categories, AVDTP_HEADER_COMPRESSION)){ 902 avdtp_signaling_emit_header_compression_capability(avdtp_cid, remote_seid, 903 &capabilities->header_compression); 904 } 905 if (get_bit16(registered_service_categories, AVDTP_MULTIPLEXING)){ 906 avdtp_signaling_emit_content_multiplexing_capability(avdtp_cid, remote_seid, 907 &capabilities->multiplexing_mode); 908 } 909 if (get_bit16(registered_service_categories, AVDTP_DELAY_REPORTING)){ 910 avdtp_signaling_emit_delay_reporting_capability(avdtp_cid, remote_seid); 911 } 912 avdtp_signaling_emit_capability_done(avdtp_cid, remote_seid); 913 } 914 915 static uint16_t 916 avdtp_signaling_setup_media_codec_sbc_config_event(uint8_t *event, uint16_t size, 917 const avdtp_stream_endpoint_t *stream_endpoint, 918 uint16_t avdtp_cid, uint8_t reconfigure, 919 const uint8_t *media_codec_information) { 920 921 btstack_assert(size >= AVDTP_MEDIA_CONFIG_SBC_EVENT_LEN); 922 923 uint8_t local_seid = avdtp_local_seid(stream_endpoint); 924 uint8_t remote_seid = avdtp_remote_seid(stream_endpoint); 925 926 int pos = 0; 927 event[pos++] = HCI_EVENT_AVDTP_META; 928 event[pos++] = AVDTP_MEDIA_CONFIG_SBC_EVENT_LEN - 2; 929 930 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION; 931 little_endian_store_16(event, pos, avdtp_cid); 932 pos += 2; 933 event[pos++] = local_seid; 934 event[pos++] = remote_seid; 935 event[pos++] = reconfigure; 936 event[pos++] = AVDTP_CODEC_SBC; 937 938 uint8_t sampling_frequency_bitmap = media_codec_information[0] >> 4; 939 uint8_t channel_mode_bitmap = media_codec_information[0] & 0x0F; 940 uint8_t block_length_bitmap = media_codec_information[1] >> 4; 941 uint8_t subbands_bitmap = (media_codec_information[1] & 0x0F) >> 2; 942 943 uint8_t num_channels = 0; 944 avdtp_channel_mode_t channel_mode; 945 946 if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){ 947 channel_mode = AVDTP_CHANNEL_MODE_JOINT_STEREO; 948 num_channels = 2; 949 } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){ 950 channel_mode = AVDTP_CHANNEL_MODE_STEREO; 951 num_channels = 2; 952 } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){ 953 channel_mode = AVDTP_CHANNEL_MODE_DUAL_CHANNEL; 954 num_channels = 2; 955 } else { 956 channel_mode = AVDTP_CHANNEL_MODE_MONO; 957 num_channels = 1; 958 } 959 960 uint16_t sampling_frequency = 0; 961 if (sampling_frequency_bitmap & AVDTP_SBC_48000) { 962 sampling_frequency = 48000; 963 } else if (sampling_frequency_bitmap & AVDTP_SBC_44100) { 964 sampling_frequency = 44100; 965 } else if (sampling_frequency_bitmap & AVDTP_SBC_32000) { 966 sampling_frequency = 32000; 967 } else if (sampling_frequency_bitmap & AVDTP_SBC_16000) { 968 sampling_frequency = 16000; 969 } 970 971 uint8_t subbands = 0; 972 if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){ 973 subbands = 8; 974 } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){ 975 subbands = 4; 976 } 977 978 uint8_t block_length = 0; 979 if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){ 980 block_length = 16; 981 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){ 982 block_length = 12; 983 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){ 984 block_length = 8; 985 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){ 986 block_length = 4; 987 } 988 989 little_endian_store_16(event, pos, sampling_frequency); 990 pos += 2; 991 992 event[pos++] = (uint8_t) channel_mode; 993 event[pos++] = num_channels; 994 event[pos++] = block_length; 995 event[pos++] = subbands; 996 event[pos++] = media_codec_information[1] & 0x03; 997 event[pos++] = media_codec_information[2]; 998 event[pos++] = media_codec_information[3]; 999 1000 btstack_assert(pos == AVDTP_MEDIA_CONFIG_SBC_EVENT_LEN); 1001 1002 return pos; 1003 } 1004 1005 static uint16_t 1006 avdtp_signaling_setup_media_codec_mpeg_audio_config_event(uint8_t *event, uint16_t size, 1007 const avdtp_stream_endpoint_t *stream_endpoint, 1008 uint16_t avdtp_cid, uint8_t reconfigure, 1009 const uint8_t *media_codec_information) { 1010 1011 btstack_assert(size >= AVDTP_MEDIA_CONFIG_MPEG_AUDIO_EVENT_LEN); 1012 1013 uint8_t local_seid = avdtp_local_seid(stream_endpoint); 1014 uint8_t remote_seid = avdtp_remote_seid(stream_endpoint); 1015 1016 uint16_t pos = 0; 1017 event[pos++] = HCI_EVENT_AVDTP_META; 1018 event[pos++] = AVDTP_MEDIA_CONFIG_MPEG_AUDIO_EVENT_LEN - 2; 1019 1020 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION; 1021 little_endian_store_16(event, pos, avdtp_cid); 1022 pos += 2; 1023 event[pos++] = local_seid; 1024 event[pos++] = remote_seid; 1025 event[pos++] = reconfigure; 1026 event[pos++] = AVDTP_CODEC_MPEG_1_2_AUDIO; 1027 1028 uint8_t layer_bitmap = media_codec_information[0] >> 5; 1029 uint8_t crc = (media_codec_information[0] >> 4) & 0x01; 1030 uint8_t channel_mode_bitmap = (media_codec_information[0] & 0x07); 1031 uint8_t mpf = (media_codec_information[1] >> 6) & 0x01; 1032 uint8_t sampling_frequency_bitmap = (media_codec_information[1] & 0x3F); 1033 uint8_t vbr = (media_codec_information[2] >> 7) & 0x01; 1034 uint16_t bit_rate_index_bitmap = ((media_codec_information[2] & 0x3f) << 8) | media_codec_information[3]; 1035 1036 uint8_t layer = 0; 1037 if (layer_bitmap & 0x04){ 1038 layer = AVDTP_MPEG_LAYER_1; 1039 } else if (layer_bitmap & 0x02){ 1040 layer = AVDTP_MPEG_LAYER_2; 1041 } else if (layer_bitmap & 0x01){ 1042 layer = AVDTP_MPEG_LAYER_3; 1043 } 1044 1045 uint8_t num_channels = 0; 1046 avdtp_channel_mode_t channel_mode = AVDTP_CHANNEL_MODE_JOINT_STEREO; 1047 if (channel_mode_bitmap & 0x08){ 1048 num_channels = 1; 1049 channel_mode = AVDTP_CHANNEL_MODE_MONO; 1050 } else if (channel_mode_bitmap & 0x04){ 1051 num_channels = 2; 1052 channel_mode = AVDTP_CHANNEL_MODE_DUAL_CHANNEL; 1053 } else if (channel_mode_bitmap & 0x02){ 1054 num_channels = 2; 1055 channel_mode = AVDTP_CHANNEL_MODE_STEREO; 1056 } else if (channel_mode_bitmap & 0x02){ 1057 num_channels = 2; 1058 channel_mode = AVDTP_CHANNEL_MODE_JOINT_STEREO; 1059 } 1060 1061 uint16_t sampling_frequency = 0; 1062 if (sampling_frequency_bitmap & 0x01) { 1063 sampling_frequency = 48000; 1064 } else if (sampling_frequency_bitmap & 0x02) { 1065 sampling_frequency = 44100; 1066 } else if (sampling_frequency_bitmap & 0x04) { 1067 sampling_frequency = 32000; 1068 } else if (sampling_frequency_bitmap & 0x08) { 1069 sampling_frequency = 24000; 1070 } else if (sampling_frequency_bitmap & 0x10) { 1071 sampling_frequency = 22050; 1072 } else if (sampling_frequency_bitmap & 0x20) { 1073 sampling_frequency = 16000; 1074 } 1075 1076 uint8_t bitrate_index = 0; 1077 uint8_t i; 1078 for (i=0;i<14;i++){ 1079 if (bit_rate_index_bitmap & (1U << i)) { 1080 bitrate_index = i; 1081 } 1082 } 1083 1084 event[pos++] = (uint8_t) layer; 1085 event[pos++] = crc; 1086 event[pos++] = (uint8_t) channel_mode; 1087 event[pos++] = num_channels; 1088 event[pos++] = mpf; 1089 little_endian_store_16(event, pos, sampling_frequency); 1090 pos += 2; 1091 event[pos++] = vbr; 1092 event[pos++] = bitrate_index; 1093 1094 btstack_assert(pos == AVDTP_MEDIA_CONFIG_MPEG_AUDIO_EVENT_LEN); 1095 1096 return pos; 1097 } 1098 1099 static uint16_t 1100 avdtp_signaling_setup_media_codec_mpec_aac_config_event(uint8_t *event, uint16_t size, 1101 const avdtp_stream_endpoint_t *stream_endpoint, 1102 uint16_t avdtp_cid, uint8_t reconfigure, 1103 const uint8_t *media_codec_information) { 1104 1105 btstack_assert(size >= AVDTP_MEDIA_CONFIG_MPEG_AUDIO_EVENT_LEN); 1106 1107 uint8_t local_seid = avdtp_local_seid(stream_endpoint); 1108 uint8_t remote_seid = avdtp_remote_seid(stream_endpoint); 1109 1110 uint16_t pos = 0; 1111 event[pos++] = HCI_EVENT_AVDTP_META; 1112 event[pos++] = AVDTP_MEDIA_CONFIG_MPEG_AAC_EVENT_LEN - 2; 1113 1114 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION; 1115 little_endian_store_16(event, pos, avdtp_cid); 1116 pos += 2; 1117 event[pos++] = local_seid; 1118 event[pos++] = remote_seid; 1119 event[pos++] = reconfigure; 1120 event[pos++] =AVDTP_CODEC_MPEG_2_4_AAC; 1121 1122 uint8_t object_type_bitmap = media_codec_information[0]; 1123 uint16_t sampling_frequency_bitmap = (media_codec_information[1] << 4) | (media_codec_information[2] >> 4); 1124 uint8_t channels_bitmap = (media_codec_information[2] >> 2) & 0x03; 1125 uint8_t vbr = media_codec_information[3] >> 7; 1126 uint32_t bit_rate = ((media_codec_information[3] & 0x7f) << 16) | (media_codec_information[4] << 8) | media_codec_information[5]; 1127 1128 uint8_t object_type = 0; 1129 if (object_type_bitmap & 0x80){ 1130 object_type = AVDTP_AAC_MPEG2_LC; 1131 } else if (object_type_bitmap & 0x40){ 1132 object_type = AVDTP_AAC_MPEG4_LC; 1133 } else if (object_type_bitmap & 0x020){ 1134 object_type = AVDTP_AAC_MPEG4_LTP; 1135 } else if (object_type_bitmap & 0x010){ 1136 object_type = AVDTP_AAC_MPEG4_SCALABLE; 1137 } 1138 1139 uint32_t sampling_frequency = 0; 1140 uint8_t i; 1141 const uint32_t aac_sampling_frequency_table[] = { 1142 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000 1143 }; 1144 for (i=0;i<12;i++){ 1145 if (sampling_frequency_bitmap & (1U << i)) { 1146 sampling_frequency = aac_sampling_frequency_table[i]; 1147 } 1148 } 1149 1150 uint8_t num_channels = 0; 1151 if (channels_bitmap & 0x02){ 1152 num_channels = 1; 1153 } else if (channels_bitmap & 0x01){ 1154 num_channels = 2; 1155 } 1156 1157 event[pos++] = object_type; 1158 little_endian_store_24(event, pos, sampling_frequency); 1159 pos += 3; 1160 event[pos++] = num_channels; 1161 little_endian_store_24(event, pos, bit_rate); 1162 pos += 3; 1163 event[pos++] = vbr; 1164 1165 btstack_assert(AVDTP_MEDIA_CONFIG_MPEG_AAC_EVENT_LEN == pos); 1166 1167 return pos; 1168 } 1169 1170 static uint16_t avdtp_signaling_setup_media_codec_atrac_config_event(uint8_t *event, uint16_t size, 1171 const avdtp_stream_endpoint_t *stream_endpoint, 1172 uint16_t avdtp_cid, uint8_t reconfigure, 1173 const uint8_t *media_codec_information) { 1174 btstack_assert(size >= AVDTP_MEDIA_CONFIG_ATRAC_EVENT_LEN); 1175 1176 uint8_t local_seid = avdtp_local_seid(stream_endpoint); 1177 uint8_t remote_seid = avdtp_remote_seid(stream_endpoint); 1178 1179 uint16_t pos = 0; 1180 event[pos++] = HCI_EVENT_AVDTP_META; 1181 event[pos++] = AVDTP_MEDIA_CONFIG_ATRAC_EVENT_LEN - 2; 1182 1183 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION; 1184 little_endian_store_16(event, pos, avdtp_cid); 1185 pos += 2; 1186 event[pos++] = local_seid; 1187 event[pos++] = remote_seid; 1188 event[pos++] = reconfigure; 1189 event[pos++] = AVDTP_CODEC_ATRAC_FAMILY; 1190 1191 avdtp_atrac_version_t version = (avdtp_atrac_version_t) (media_codec_information[0] >> 5); 1192 uint8_t channel_mode_bitmap = (media_codec_information[0] >> 2) & 0x07; 1193 uint16_t sampling_frequency_bitmap = (media_codec_information[1] >> 4) & 0x03; 1194 uint8_t vbr = (media_codec_information[1] >> 3) & 0x01; 1195 uint16_t bit_rate_index_bitmap = ((media_codec_information[1]) & 0x07) << 16 | (media_codec_information[2] << 8) | media_codec_information[3]; 1196 uint16_t maximum_sul = (media_codec_information[4] << 8) | media_codec_information[5]; 1197 1198 uint8_t num_channels = 0; 1199 avdtp_channel_mode_t channel_mode = AVDTP_CHANNEL_MODE_JOINT_STEREO; 1200 if (channel_mode_bitmap & 0x04){ 1201 num_channels = 1; 1202 channel_mode = AVDTP_CHANNEL_MODE_MONO; 1203 } else if (channel_mode_bitmap & 0x02){ 1204 num_channels = 2; 1205 channel_mode = AVDTP_CHANNEL_MODE_DUAL_CHANNEL; 1206 } else if (channel_mode_bitmap & 0x01){ 1207 num_channels = 2; 1208 channel_mode = AVDTP_CHANNEL_MODE_JOINT_STEREO; 1209 } 1210 1211 uint16_t sampling_frequency = 0; 1212 if (sampling_frequency_bitmap & 0x02){ 1213 sampling_frequency = 44100; 1214 } else if (sampling_frequency_bitmap & 0x01){ 1215 sampling_frequency = 48000; 1216 } 1217 1218 // bit 0 = index 0x18, bit 19 = index 0 1219 uint8_t bit_rate_index = 0; 1220 uint8_t i; 1221 for (i=0;i <= 19;i++){ 1222 if (bit_rate_index_bitmap & (1U << i)) { 1223 bit_rate_index = 18 - i; 1224 } 1225 } 1226 1227 event[pos++] = (uint8_t) version; 1228 event[pos++] = (uint8_t) channel_mode; 1229 event[pos++] = num_channels; 1230 little_endian_store_16(event, pos, sampling_frequency); 1231 pos += 2; 1232 event[pos++] = vbr; 1233 event[pos++] = bit_rate_index; 1234 little_endian_store_16(event, pos, maximum_sul); 1235 pos += 2; 1236 1237 btstack_assert(pos == AVDTP_MEDIA_CONFIG_ATRAC_EVENT_LEN); 1238 return pos; 1239 } 1240 1241 static uint16_t avdtp_signaling_setup_media_codec_other_config_event(uint8_t *event, uint16_t size, 1242 const avdtp_stream_endpoint_t *stream_endpoint, 1243 uint16_t avdtp_cid, uint8_t reconfigure, 1244 const adtvp_media_codec_capabilities_t *media_codec) { 1245 btstack_assert(size >= AVDTP_MEDIA_CONFIG_OTHER_EVENT_LEN); 1246 1247 uint8_t local_seid = avdtp_local_seid(stream_endpoint); 1248 uint8_t remote_seid = avdtp_remote_seid(stream_endpoint); 1249 1250 uint16_t pos = 0; 1251 event[pos++] = HCI_EVENT_AVDTP_META; 1252 pos++; // set later 1253 event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION; 1254 little_endian_store_16(event, pos, avdtp_cid); 1255 pos += 2; 1256 event[pos++] = local_seid; 1257 event[pos++] = remote_seid; 1258 event[pos++] = reconfigure; 1259 event[pos++] = media_codec->media_type; 1260 little_endian_store_16(event, pos, media_codec->media_codec_type); 1261 pos += 2; 1262 little_endian_store_16(event, pos, media_codec->media_codec_information_len); 1263 pos += 2; 1264 1265 btstack_assert(pos == 13); 1266 1267 uint16_t media_codec_len = btstack_min(AVDTP_MAX_MEDIA_CODEC_INFORMATION_LENGTH, media_codec->media_codec_information_len); 1268 (void)memcpy(event + pos, media_codec->media_codec_information, media_codec_len); 1269 pos += media_codec_len; 1270 event[1] = pos - 2; 1271 return pos; 1272 } 1273 1274 void avdtp_signaling_emit_delay(uint16_t avdtp_cid, uint8_t local_seid, uint16_t delay) { 1275 uint8_t event[8]; 1276 int pos = 0; 1277 event[pos++] = HCI_EVENT_AVDTP_META; 1278 event[pos++] = sizeof(event) - 2; 1279 event[pos++] = AVDTP_SUBEVENT_SIGNALING_DELAY_REPORT; 1280 little_endian_store_16(event, pos, avdtp_cid); 1281 pos += 2; 1282 event[pos++] = local_seid; 1283 little_endian_store_16(event, pos, delay); 1284 pos += 2; 1285 avdtp_emit_source(event, pos); 1286 } 1287 1288 uint16_t avdtp_setup_media_codec_config_event(uint8_t *event, uint16_t size, const avdtp_stream_endpoint_t *stream_endpoint, 1289 uint16_t avdtp_cid, uint8_t reconfigure, 1290 const adtvp_media_codec_capabilities_t * media_codec) { 1291 switch (media_codec->media_codec_type){ 1292 case AVDTP_CODEC_SBC: 1293 return avdtp_signaling_setup_media_codec_sbc_config_event(event, size, stream_endpoint, avdtp_cid, reconfigure, 1294 media_codec->media_codec_information); 1295 case AVDTP_CODEC_MPEG_1_2_AUDIO: 1296 return avdtp_signaling_setup_media_codec_mpeg_audio_config_event(event, size, stream_endpoint, avdtp_cid, reconfigure, 1297 media_codec->media_codec_information); 1298 case AVDTP_CODEC_MPEG_2_4_AAC: 1299 return avdtp_signaling_setup_media_codec_mpec_aac_config_event(event, size, stream_endpoint, avdtp_cid, reconfigure, 1300 media_codec->media_codec_information); 1301 case AVDTP_CODEC_ATRAC_FAMILY: 1302 return avdtp_signaling_setup_media_codec_atrac_config_event(event, size, stream_endpoint, avdtp_cid, reconfigure, 1303 media_codec->media_codec_information); 1304 default: 1305 return avdtp_signaling_setup_media_codec_other_config_event(event, size, stream_endpoint, avdtp_cid, reconfigure, 1306 media_codec); 1307 } 1308 } 1309 1310 void avdtp_signaling_emit_configuration(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid, uint8_t reconfigure, 1311 avdtp_capabilities_t *configuration, uint16_t configured_service_categories) { 1312 1313 if (get_bit16(configured_service_categories, AVDTP_MEDIA_CODEC)){ 1314 uint16_t pos = 0; 1315 // assume MEDIA_CONFIG_OTHER_EVENT_LEN is larger than all other events 1316 uint8_t event[AVDTP_MEDIA_CONFIG_OTHER_EVENT_LEN]; 1317 pos = avdtp_setup_media_codec_config_event(event, sizeof(event), stream_endpoint, avdtp_cid, reconfigure, 1318 &configuration->media_codec); 1319 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint); 1320 (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 1321 } 1322 } 1323 1324 void avdtp_streaming_emit_connection_established(avdtp_stream_endpoint_t *stream_endpoint, uint8_t status) { 1325 uint8_t event[14]; 1326 int pos = 0; 1327 event[pos++] = HCI_EVENT_AVDTP_META; 1328 event[pos++] = sizeof(event) - 2; 1329 event[pos++] = AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED; 1330 little_endian_store_16(event, pos, stream_endpoint->connection->avdtp_cid); 1331 pos += 2; 1332 reverse_bd_addr(stream_endpoint->connection->remote_addr, &event[pos]); 1333 pos += 6; 1334 event[pos++] = avdtp_local_seid(stream_endpoint); 1335 event[pos++] = avdtp_remote_seid(stream_endpoint); 1336 event[pos++] = status; 1337 1338 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint); 1339 (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 1340 } 1341 1342 void avdtp_streaming_emit_connection_released(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid, uint8_t local_seid) { 1343 uint8_t event[6]; 1344 int pos = 0; 1345 event[pos++] = HCI_EVENT_AVDTP_META; 1346 event[pos++] = sizeof(event) - 2; 1347 event[pos++] = AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED; 1348 little_endian_store_16(event, pos, avdtp_cid); 1349 pos += 2; 1350 event[pos++] = local_seid; 1351 1352 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint); 1353 (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 1354 } 1355 1356 void avdtp_streaming_emit_can_send_media_packet_now(avdtp_stream_endpoint_t *stream_endpoint, uint16_t sequence_number) { 1357 uint8_t event[8]; 1358 int pos = 0; 1359 event[pos++] = HCI_EVENT_AVDTP_META; 1360 event[pos++] = sizeof(event) - 2; 1361 event[pos++] = AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW; 1362 little_endian_store_16(event, pos, stream_endpoint->connection->avdtp_cid); 1363 pos += 2; 1364 event[pos++] = avdtp_local_seid(stream_endpoint); 1365 little_endian_store_16(event, pos, sequence_number); 1366 pos += 2; 1367 event[1] = pos - 2; 1368 1369 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint); 1370 (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 1371 } 1372 1373 uint8_t avdtp_request_can_send_now_acceptor(avdtp_connection_t *connection) { 1374 if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1375 connection->wait_to_send_acceptor = true; 1376 l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); 1377 return ERROR_CODE_SUCCESS; 1378 } 1379 1380 uint8_t avdtp_request_can_send_now_initiator(avdtp_connection_t *connection) { 1381 if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1382 connection->wait_to_send_initiator = true; 1383 l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); 1384 return ERROR_CODE_SUCCESS; 1385 } 1386 1387 uint8_t avdtp_local_seid(const avdtp_stream_endpoint_t * stream_endpoint){ 1388 if (!stream_endpoint) return 0; 1389 return stream_endpoint->sep.seid; 1390 1391 } 1392 1393 uint8_t avdtp_remote_seid(const avdtp_stream_endpoint_t * stream_endpoint){ 1394 if (!stream_endpoint) return AVDTP_INVALID_SEP_SEID; 1395 return stream_endpoint->remote_sep.seid; 1396 } 1397 1398 // helper to set/get configuration 1399 void avdtp_config_sbc_set_sampling_frequency(uint8_t * config, uint16_t sampling_frequency_hz){ 1400 avdtp_sbc_sampling_frequency_t sampling_frequency; 1401 switch (sampling_frequency_hz){ 1402 case 16000: 1403 sampling_frequency = AVDTP_SBC_16000; 1404 break; 1405 case 32000: 1406 sampling_frequency = AVDTP_SBC_32000; 1407 break; 1408 case 48000: 1409 sampling_frequency = AVDTP_SBC_48000; 1410 break; 1411 default: 1412 sampling_frequency = AVDTP_SBC_44100; 1413 break; 1414 } 1415 config[0] = (((uint8_t) sampling_frequency) << 4) | (config[0] & 0x0f); 1416 } 1417 1418 void avdtp_config_sbc_store(uint8_t * config, const avdtp_configuration_sbc_t * configuration){ 1419 avdtp_sbc_channel_mode_t sbc_channel_mode; 1420 switch (configuration->channel_mode){ 1421 case AVDTP_CHANNEL_MODE_MONO: 1422 sbc_channel_mode = AVDTP_SBC_MONO; 1423 break; 1424 case AVDTP_CHANNEL_MODE_DUAL_CHANNEL: 1425 sbc_channel_mode = AVDTP_SBC_DUAL_CHANNEL; 1426 break; 1427 case AVDTP_CHANNEL_MODE_STEREO: 1428 sbc_channel_mode = AVDTP_SBC_STEREO; 1429 break; 1430 default: 1431 sbc_channel_mode = AVDTP_SBC_JOINT_STEREO; 1432 break; 1433 } 1434 config[0] = (uint8_t) sbc_channel_mode; 1435 config[1] = (configuration->block_length << 4) | (configuration->subbands << 2) | configuration->allocation_method; 1436 config[2] = configuration-> min_bitpool_value; 1437 config[3] = configuration->max_bitpool_value; 1438 avdtp_config_sbc_set_sampling_frequency(config, configuration->sampling_frequency); 1439 } 1440 1441 void avdtp_config_mpeg_audio_set_sampling_frequency(uint8_t * config, uint16_t sampling_frequency_hz) { 1442 uint8_t sampling_frequency_index = 0; 1443 switch (sampling_frequency_hz){ 1444 case 16000: 1445 sampling_frequency_index = 5; 1446 break; 1447 case 22040: 1448 sampling_frequency_index = 4; 1449 break; 1450 case 24000: 1451 sampling_frequency_index = 3; 1452 break; 1453 case 32000: 1454 sampling_frequency_index = 2; 1455 break; 1456 case 44100: 1457 sampling_frequency_index = 1; 1458 break; 1459 case 48000: 1460 sampling_frequency_index = 0; 1461 break; 1462 default: 1463 btstack_assert(false); 1464 break; 1465 } 1466 config[1] = (config[1] & 0xC0) | (1 << sampling_frequency_index); 1467 } 1468 1469 void avdtp_config_mpeg_audio_store(uint8_t * config, const avdtp_configuration_mpeg_audio_t * configuration){ 1470 1471 config[0] = (1 << (7 - (configuration->layer - AVDTP_MPEG_LAYER_1))) | ((configuration->crc & 0x01) << 4) | (1 << (configuration->channel_mode - AVDTP_CHANNEL_MODE_MONO)); 1472 config[1] = ((configuration->media_payload_format & 0x01) << 6) ; 1473 uint16_t bit_rate_mask = 1 << configuration->bit_rate_index; 1474 config[2] = ((configuration->vbr & 0x01) << 7) | ((bit_rate_mask >> 8) & 0x3f); 1475 config[3] = bit_rate_mask & 0xff; 1476 avdtp_config_mpeg_audio_set_sampling_frequency(config, configuration->sampling_frequency); 1477 } 1478 1479 1480 void avdtp_config_mpeg_aac_set_sampling_frequency(uint8_t * config, uint16_t sampling_frequency_hz) { 1481 uint16_t sampling_frequency_bitmap = 0; 1482 uint8_t i; 1483 const uint32_t aac_sampling_frequency_table[] = { 1484 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000 1485 }; 1486 for (i=0;i<12;i++){ 1487 if (sampling_frequency_hz == aac_sampling_frequency_table[i]){ 1488 sampling_frequency_bitmap = 1 << i; 1489 break; 1490 } 1491 } 1492 config[1] = sampling_frequency_bitmap >> 4; 1493 config[2] = ((sampling_frequency_bitmap & 0x0f) << 4) | (config[2] & 0x0f); 1494 } 1495 1496 void avdtp_config_mpeg_aac_store(uint8_t * config, const avdtp_configuration_mpeg_aac_t * configuration) { 1497 config[0] = 1 << (7 -(configuration->object_type - AVDTP_AAC_MPEG2_LC)); 1498 uint8_t channels_bitmap = 0; 1499 switch (configuration->channels){ 1500 case 1: 1501 channels_bitmap = 0x02; 1502 break; 1503 case 2: 1504 channels_bitmap = 0x01; 1505 break; 1506 default: 1507 break; 1508 } 1509 config[2] = channels_bitmap << 2; 1510 config[3] = ((configuration->vbr & 0x01) << 7) | ((configuration->bit_rate >> 16) & 0x7f); 1511 config[4] = (configuration->bit_rate >> 8) & 0xff; 1512 config[5] = configuration->bit_rate & 0xff; 1513 avdtp_config_mpeg_aac_set_sampling_frequency(config, configuration->sampling_frequency); 1514 } 1515 1516 void avdtp_config_atrac_set_sampling_frequency(uint8_t * config, uint16_t sampling_frequency_hz) { 1517 uint8_t fs_bitmap = 0; 1518 switch (sampling_frequency_hz){ 1519 case 44100: 1520 fs_bitmap = 2; 1521 break; 1522 case 48000: 1523 fs_bitmap = 1; 1524 break; 1525 default: 1526 break; 1527 } 1528 config[1] = (fs_bitmap << 4) | (config[1] & 0x0F); 1529 } 1530 1531 void avdtp_config_atrac_store(uint8_t * config, const avdtp_configuration_atrac_t * configuration){ 1532 uint8_t channel_mode_bitmap = 0; 1533 switch (configuration->channel_mode){ 1534 case AVDTP_CHANNEL_MODE_MONO: 1535 channel_mode_bitmap = 4; 1536 break; 1537 case AVDTP_CHANNEL_MODE_DUAL_CHANNEL: 1538 channel_mode_bitmap = 2; 1539 break; 1540 case AVDTP_CHANNEL_MODE_JOINT_STEREO: 1541 channel_mode_bitmap = 1; 1542 break; 1543 default: 1544 break; 1545 } 1546 config[0] = ((configuration->version - AVDTP_ATRAC_VERSION_1 + 1) << 5) | (channel_mode_bitmap << 2); 1547 uint32_t bit_rate_bitmap = 1 << (0x18 - configuration->bit_rate_index); 1548 config[1] = ((configuration->vbr & 0x01) << 3) | ((bit_rate_bitmap >> 16) & 0x07); 1549 config[2] = (bit_rate_bitmap >> 8) & 0xff; 1550 config[3] = bit_rate_bitmap & 0xff; 1551 config[4] = configuration->maximum_sul >> 8; 1552 config[5] = configuration->maximum_sul & 0xff; 1553 config[6] = 0; 1554 avdtp_config_atrac_set_sampling_frequency(config, configuration->sampling_frequency); 1555 } 1556