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