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