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