xref: /btstack/src/classic/a2dp.c (revision 72237cc21082e528280bd52e428caefea9387ed6)
1 /*
2  * Copyright (C) 2022 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 #include <stdint.h>
39 #include <string.h>
40 #include "a2dp.h"
41 #include "l2cap.h"
42 #include "classic/sdp_util.h"
43 #include "classic/avdtp_source.h"
44 #include "classic/a2dp_source.h"
45 #include "btstack_event.h"
46 #include "bluetooth_sdp.h"
47 #include "bluetooth_psm.h"
48 
49 #define BTSTACK_FILE__ "a2dp.c"
50 
51 #include <stddef.h>
52 #include "bluetooth.h"
53 #include "classic/a2dp.h"
54 #include "classic/avdtp_util.h"
55 #include "btstack_debug.h"
56 
57 // ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG has been replaced by ENABLE_A2DP_EXPLICIT_CONFIG which is valid for both roles
58 #ifdef  ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
59 #error "Please define ENABLE_A2DP_EXPLICIT_CONFIG instead of ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG"
60 #endif
61 
62 #define AVDTP_MAX_SEP_NUM 10
63 #define A2DP_SET_CONFIG_DELAY_MS 200
64 
65 static void a2dp_config_process_discover_seps_with_next_waiting_connection(void);
66 
67 // higher layer callbacks
68 static btstack_packet_handler_t a2dp_source_callback;
69 static btstack_packet_handler_t a2dp_sink_callback;
70 
71 // config process - singletons using sep_discovery_cid is used as mutex
72 static avdtp_role_t             a2dp_config_process_role;
73 static uint16_t                 a2dp_config_process_sep_discovery_cid;
74 static uint16_t                 a2dp_config_process_sep_discovery_count;
75 static uint16_t                 a2dp_config_process_sep_discovery_index;
76 static avdtp_sep_t              a2dp_config_process_sep_discovery_seps[AVDTP_MAX_SEP_NUM];
77 static btstack_timer_source_t   a2dp_config_process_set_config_timer;
78 static bool                     a2dp_config_process_set_config_timer_active;
79 
80 void a2dp_init(void) {
81 }
82 
83 void a2dp_deinit(void){
84     a2dp_config_process_sep_discovery_cid = 0;
85     a2dp_source_callback = NULL;
86     a2dp_sink_callback = NULL;
87 }
88 
89 void a2dp_create_sdp_record(uint8_t * service,  uint32_t service_record_handle, uint16_t service_class_uuid, uint16_t supported_features, const char * service_name, const char * service_provider_name){
90     uint8_t* attribute;
91     de_create_sequence(service);
92 
93     // 0x0000 "Service Record Handle"
94     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
95     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
96 
97     // 0x0001 "Service Class ID List"
98     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
99     attribute = de_push_sequence(service);
100     {
101         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_class_uuid);
102     }
103     de_pop_sequence(service, attribute);
104 
105     // 0x0004 "Protocol Descriptor List"
106     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
107     attribute = de_push_sequence(service);
108     {
109         uint8_t* l2cpProtocol = de_push_sequence(attribute);
110         {
111             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
112             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVDTP);
113         }
114         de_pop_sequence(attribute, l2cpProtocol);
115 
116         uint8_t* avProtocol = de_push_sequence(attribute);
117         {
118             de_add_number(avProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);  // avProtocol_service
119             de_add_number(avProtocol,  DE_UINT, DE_SIZE_16,  0x0103);  // version
120         }
121         de_pop_sequence(attribute, avProtocol);
122     }
123     de_pop_sequence(service, attribute);
124 
125     // 0x0005 "Public Browse Group"
126     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
127     attribute = de_push_sequence(service);
128     {
129         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
130     }
131     de_pop_sequence(service, attribute);
132 
133     // 0x0009 "Bluetooth Profile Descriptor List"
134     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
135     attribute = de_push_sequence(service);
136     {
137         uint8_t *a2dProfile = de_push_sequence(attribute);
138         {
139             de_add_number(a2dProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION);
140             de_add_number(a2dProfile,  DE_UINT, DE_SIZE_16, 0x0103);
141         }
142         de_pop_sequence(attribute, a2dProfile);
143     }
144     de_pop_sequence(service, attribute);
145 
146 
147     // 0x0100 "Service Name"
148     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
149     de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
150 
151     // 0x0100 "Provider Name"
152     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
153     de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
154 
155     // 0x0311 "Supported Features"
156     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
157     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
158 }
159 
160 uint8_t a2dp_subevent_id_for_avdtp_subevent_id(uint8_t subevent){
161     switch (subevent){
162         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:
163             return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION;
164         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION:
165             return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION;
166         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION:
167             return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION;
168         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION:
169             return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION;
170         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION:
171             return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION;
172         default:
173             btstack_unreachable();
174             return 0;
175     }
176 }
177 
178 static void a2dp_replace_subevent_id_and_emit(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size, uint8_t subevent_id){
179     UNUSED(size);
180     btstack_assert(callback != NULL);
181     // cache orig event and subevent id
182     uint8_t orig_event_id    = packet[0];
183     uint8_t orig_subevent_id = packet[2];
184     // execute callback
185     packet[0] = HCI_EVENT_A2DP_META;
186     packet[2] = subevent_id;
187     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
188     // restore id
189     packet[0] = orig_event_id;
190     packet[2] = orig_subevent_id;
191 }
192 
193 void a2dp_emit_stream_event(btstack_packet_handler_t callback, uint16_t cid, uint8_t local_seid, uint8_t subevent_id){
194     uint8_t event[6];
195     int pos = 0;
196     event[pos++] = HCI_EVENT_A2DP_META;
197     event[pos++] = sizeof(event) - 2;
198     event[pos++] = subevent_id;
199     little_endian_store_16(event, pos, cid);
200     pos += 2;
201     event[pos++] = local_seid;
202     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
203 }
204 
205 void a2dp_register_source_packet_handler(btstack_packet_handler_t callback){
206     btstack_assert(callback != NULL);
207     a2dp_source_callback = callback;
208 }
209 
210 void a2dp_register_sink_packet_handler(btstack_packet_handler_t callback){
211     btstack_assert(callback != NULL);
212     a2dp_sink_callback = callback;
213 }
214 
215 void a2dp_replace_subevent_id_and_emit_source(uint8_t * packet, uint16_t size, uint8_t subevent_id) {
216     a2dp_replace_subevent_id_and_emit(a2dp_source_callback, packet, size, subevent_id);
217 }
218 
219 static void a2dp_replace_subevent_id_and_emit_sink(uint8_t *packet, uint16_t size, uint8_t subevent_id) {
220     a2dp_replace_subevent_id_and_emit(a2dp_sink_callback, packet, size, subevent_id);
221 }
222 
223 static void a2dp_replace_subevent_id_and_emit_for_role(avdtp_role_t role, uint8_t * packet, uint16_t size, uint8_t subevent_id) {
224     if (role == AVDTP_ROLE_SOURCE){
225         a2dp_replace_subevent_id_and_emit_source(packet, size, subevent_id);
226     } else {
227         a2dp_replace_subevent_id_and_emit_sink(packet, size, subevent_id);
228     }
229 }
230 
231 static void a2dp_emit_role(avdtp_role_t role, uint8_t * packet, uint16_t size){
232     if (role == AVDTP_ROLE_SOURCE){
233         (*a2dp_source_callback)(HCI_EVENT_PACKET, 0, packet, size);
234     } else {
235         (*a2dp_sink_callback)(HCI_EVENT_PACKET, 0, packet, size);
236     }
237 }
238 
239 void a2dp_emit_stream_event_for_role(avdtp_role_t role, uint16_t cid, uint8_t local_seid, uint8_t subevent_id) {
240     uint8_t event[6];
241     int pos = 0;
242     event[pos++] = HCI_EVENT_A2DP_META;
243     event[pos++] = sizeof(event) - 2;
244     event[pos++] = subevent_id;
245     little_endian_store_16(event, pos, cid);
246     pos += 2;
247     event[pos++] = local_seid;
248     a2dp_emit_role(role, event, sizeof(event));
249 }
250 
251 void a2dp_emit_stream_reconfigured_role(avdtp_role_t role, uint16_t cid, uint8_t local_seid, uint8_t status){
252     uint8_t event[7];
253     int pos = 0;
254     event[pos++] = HCI_EVENT_A2DP_META;
255     event[pos++] = sizeof(event) - 2;
256     event[pos++] = A2DP_SUBEVENT_STREAM_RECONFIGURED;
257     little_endian_store_16(event, pos, cid);
258     pos += 2;
259     event[pos++] = local_seid;
260     event[pos++] = status;
261     a2dp_emit_role(role, event, sizeof(event));
262 }
263 
264 void a2dp_emit_streaming_connection_failed_for_role(avdtp_role_t role, avdtp_connection_t *connection, uint8_t status) {
265     uint8_t event[14];
266     int pos = 0;
267     event[pos++] = HCI_EVENT_A2DP_META;
268     event[pos++] = sizeof(event) - 2;
269     event[pos++] = A2DP_SUBEVENT_STREAM_ESTABLISHED;
270     little_endian_store_16(event, pos, connection->avdtp_cid);
271     pos += 2;
272     reverse_bd_addr(connection->remote_addr, &event[pos]);
273     pos += 6;
274     event[pos++] = 0;
275     event[pos++] = 0;
276     event[pos++] = status;
277     a2dp_emit_role(role, event, sizeof(event));
278 }
279 
280 static a2dp_config_process_t * a2dp_config_process_for_role(avdtp_role_t role, avdtp_connection_t *connection){
281     return (role == AVDTP_ROLE_SOURCE) ? &connection->a2dp_source_config_process : &connection->a2dp_sink_config_process;
282 }
283 
284 static void a2dp_config_process_timer_handler(btstack_timer_source_t * timer){
285     uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
286     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
287     btstack_run_loop_set_timer_context(&a2dp_config_process_set_config_timer, NULL);
288     a2dp_config_process_set_config_timer_active = false;
289 
290     log_info("Config timer fired, avdtp_cid 0x%02x", avdtp_cid);
291 
292     if (connection == NULL) {
293         a2dp_config_process_discover_seps_with_next_waiting_connection();
294         return;
295     }
296     a2dp_config_process_t * config_process = a2dp_config_process_for_role(a2dp_config_process_role, connection);
297     if (config_process->stream_endpoint_configured) {
298         a2dp_config_process_discover_seps_with_next_waiting_connection();
299         return;
300     }
301 
302     avdtp_discover_stream_endpoints(avdtp_cid);
303 }
304 
305 static void a2dp_config_process_timer_start(uint16_t avdtp_cid){
306     log_info("Config timer start for cid 0%02x", avdtp_cid);
307     a2dp_config_process_set_config_timer_active = true;
308     btstack_run_loop_remove_timer(&a2dp_config_process_set_config_timer);
309     btstack_run_loop_set_timer_handler(&a2dp_config_process_set_config_timer, a2dp_config_process_timer_handler);
310     btstack_run_loop_set_timer(&a2dp_config_process_set_config_timer, A2DP_SET_CONFIG_DELAY_MS);
311     btstack_run_loop_set_timer_context(&a2dp_config_process_set_config_timer, (void *)(uintptr_t)avdtp_cid);
312     btstack_run_loop_add_timer(&a2dp_config_process_set_config_timer);
313 }
314 
315 static void a2dp_config_process_timer_restart(void){
316     log_info("Config timer restart");
317     btstack_run_loop_remove_timer(&a2dp_config_process_set_config_timer);
318     btstack_run_loop_set_timer(&a2dp_config_process_set_config_timer, A2DP_SET_CONFIG_DELAY_MS);
319     btstack_run_loop_add_timer(&a2dp_config_process_set_config_timer);
320 }
321 
322 static void a2dp_config_process_timer_stop(void){
323     if (a2dp_config_process_set_config_timer_active == false) return;
324     log_info("Config timer stop");
325     btstack_run_loop_remove_timer(&a2dp_config_process_set_config_timer);
326     btstack_run_loop_set_timer_context(&a2dp_config_process_set_config_timer, NULL);
327     a2dp_config_process_set_config_timer_active = false;
328 }
329 
330 // Discover seps, both incoming and outgoing
331 static void a2dp_config_process_start_discovering_seps(avdtp_role_t role, avdtp_connection_t *connection) {
332     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
333 
334     config_process->state = A2DP_DISCOVER_SEPS;
335     config_process->discover_seps = false;
336 
337     a2dp_config_process_role = role;
338     a2dp_config_process_sep_discovery_index = 0;
339     a2dp_config_process_sep_discovery_count = 0;
340     memset(a2dp_config_process_sep_discovery_seps, 0, sizeof(avdtp_sep_t) * AVDTP_MAX_SEP_NUM);
341     a2dp_config_process_sep_discovery_cid = connection->avdtp_cid;
342 
343     // if we initiated the connection, start config right away, else wait a bit to give remote a chance to do it first
344     if (config_process->outgoing_active){
345         log_info("discover seps");
346         avdtp_discover_stream_endpoints(connection->avdtp_cid);
347     } else {
348         log_info("wait a bit, then discover seps");
349         a2dp_config_process_timer_start(connection->avdtp_cid);
350     }
351 }
352 
353 static void a2dp_config_process_discover_seps_with_next_waiting_connection(void){
354     btstack_assert(a2dp_config_process_sep_discovery_cid == 0);
355     btstack_linked_list_iterator_t it;
356     btstack_linked_list_iterator_init(&it, avdtp_get_connections());
357     while (btstack_linked_list_iterator_has_next(&it)){
358         avdtp_connection_t * next_connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
359         if (next_connection->a2dp_source_config_process.discover_seps) {
360             a2dp_config_process_start_discovering_seps(AVDTP_ROLE_SOURCE, next_connection);
361         }
362         if (next_connection->a2dp_sink_config_process.discover_seps) {
363             a2dp_config_process_start_discovering_seps(AVDTP_ROLE_SINK, next_connection);
364         }
365     }
366 }
367 
368 void a2dp_config_process_ready_for_sep_discovery(avdtp_role_t role, avdtp_connection_t *connection) {
369     // start discover seps now if:
370     // - outgoing active: signaling for outgoing connection
371     // - outgoing not active: incoming connection and no sep discover ongoing
372 
373     // sep discovery active?
374     if (a2dp_config_process_sep_discovery_cid == 0){
375         a2dp_config_process_start_discovering_seps(role, connection);
376     } else {
377         // post-pone sep discovery
378         a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
379         config_process->discover_seps = true;
380     }
381 }
382 
383 static void a2dp_config_process_handle_media_configuration(avdtp_role_t role, const uint8_t *packet, uint8_t local_seid) {
384     uint16_t cid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_avdtp_cid(packet);
385     avdtp_connection_t *connection = avdtp_get_connection_for_avdtp_cid(cid);
386     btstack_assert(connection != NULL);
387     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
388 
389     config_process->local_stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
390     // bail out if local seid invalid
391     if (config_process->local_stream_endpoint == NULL) return;
392 
393     // stop timer
394     if (a2dp_config_process_sep_discovery_cid == cid) {
395         a2dp_config_process_timer_stop();
396         a2dp_config_process_sep_discovery_cid = 0;
397     }
398 
399     config_process->stream_endpoint_configured = true;
400 
401     switch (config_process->state) {
402         case A2DP_W4_SET_CONFIGURATION:
403             // outgoing: discovery and config of remote sink sep successful, trigger stream open
404             config_process->state = A2DP_W2_OPEN_STREAM_WITH_SEID;
405             break;
406         case A2DP_DISCOVER_SEPS:
407         case A2DP_GET_CAPABILITIES:
408         case A2DP_W2_GET_ALL_CAPABILITIES:
409         case A2DP_DISCOVERY_DONE:
410         case A2DP_W4_GET_CONFIGURATION:
411             // incoming: wait for stream open
412             config_process->state = A2DP_W4_OPEN_STREAM_WITH_SEID;
413             break;
414         default:
415             // wait for configuration after sending reconfigure - keep state
416             break;
417     }
418 }
419 
420 void a2dp_config_process_set_config(avdtp_role_t role, avdtp_connection_t *connection) {
421     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
422     uint8_t remote_seid = config_process->local_stream_endpoint->set_config_remote_seid;
423     log_info("A2DP initiate set configuration locally and wait for response ... local seid 0x%02x, remote seid 0x%02x",
424              avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint), remote_seid);
425     config_process->state = A2DP_W4_SET_CONFIGURATION;
426     avdtp_set_configuration(connection->avdtp_cid,
427                             avdtp_stream_endpoint_seid(config_process->local_stream_endpoint),
428                             remote_seid,
429                             config_process->local_stream_endpoint->remote_configuration_bitmap,
430                             config_process->local_stream_endpoint->remote_configuration);
431 }
432 
433 static void
434 a2dp_config_process_handle_media_capability(avdtp_role_t role, uint16_t cid, uint8_t a2dp_subevent_id, uint8_t *packet, uint16_t size) {
435     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(cid);
436     btstack_assert(connection != NULL);
437     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
438     if (config_process->state != A2DP_GET_CAPABILITIES) return;
439     a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, a2dp_subevent_id);
440 }
441 
442 uint8_t a2dp_config_process_config_init(avdtp_role_t role, avdtp_connection_t *connection, uint8_t local_seid, uint8_t remote_seid,
443                                         avdtp_media_codec_type_t codec_type) {
444 
445     // check state
446     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
447     switch (config_process->state){
448         case A2DP_DISCOVERY_DONE:
449         case A2DP_GET_CAPABILITIES:
450             break;
451         default:
452             return ERROR_CODE_COMMAND_DISALLOWED;
453     }
454 
455     // lookup local stream endpoint
456     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
457     if (stream_endpoint == NULL){
458         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
459     }
460 
461     // lookup remote stream endpoint
462     avdtp_sep_t * remote_sep = NULL;
463     uint8_t i;
464     for (i=0; i < a2dp_config_process_sep_discovery_count; i++){
465         if (a2dp_config_process_sep_discovery_seps[i].seid == remote_seid){
466             remote_sep = &a2dp_config_process_sep_discovery_seps[i];
467         }
468     }
469     if (remote_sep == NULL){
470         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
471     }
472 
473     // set media configuration
474     stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
475     stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
476     stream_endpoint->remote_configuration.media_codec.media_codec_type = codec_type;
477     // remote seid to use
478     stream_endpoint->set_config_remote_seid = remote_seid;
479     // enable delay reporting if supported
480     if (remote_sep->registered_service_categories & (1<<AVDTP_DELAY_REPORTING)){
481         stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_DELAY_REPORTING, 1);
482     }
483 
484     // suitable stream endpoint found, configure it
485     config_process->local_stream_endpoint = stream_endpoint;
486     config_process->have_config = true;
487 
488 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG
489     if (config_process->state == A2DP_DISCOVERY_DONE)
490         config_process->state = A2DP_SET_CONFIGURATION;
491     }
492 #endif
493 
494     return ERROR_CODE_SUCCESS;
495 }
496 void a2dp_config_process_avdtp_event_handler(avdtp_role_t role, uint8_t *packet, uint16_t size) {
497     uint16_t cid;
498     avdtp_connection_t * connection;
499     a2dp_config_process_t * config_process;
500     uint8_t signal_identifier;
501     uint8_t status;
502     uint8_t local_seid;
503     uint8_t remote_seid;
504 
505     switch (hci_event_avdtp_meta_get_subevent_code(packet)){
506         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:
507             cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
508             connection = avdtp_get_connection_for_avdtp_cid(cid);
509             btstack_assert(connection != NULL);
510             config_process = a2dp_config_process_for_role(role, connection);
511 
512             status = avdtp_subevent_signaling_connection_established_get_status(packet);
513             if (status != ERROR_CODE_SUCCESS){
514                 // notify about connection error only if we're initiator
515                 if (config_process->outgoing_active){
516                     log_info("A2DP source signaling connection failed status 0x%02x", status);
517                     config_process->outgoing_active = false;
518                     a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED);
519                 }
520                 break;
521             }
522             log_info("A2DP source signaling connection established avdtp_cid 0x%02x", cid);
523             config_process->state = A2DP_CONNECTED;
524 
525             // notify app
526             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED);
527 
528             // Windows 10 as Source starts SEP discovery after 1500 ms, but only if it did not get a Discover command
529             // If BTstack is configured for both roles, we need to avoid sending Discover command in Source Role for outgoing Sink connections
530 
531             // For this, we trigger SEP discovery if:
532             // a) this is an outgoing connection
533             // b) this connection wasn't caused by an outgoing connection of the other role
534             if (role == AVDTP_ROLE_SOURCE){
535                 if (connection->a2dp_source_config_process.outgoing_active || !connection->a2dp_sink_config_process.outgoing_active){
536                     a2dp_config_process_ready_for_sep_discovery(AVDTP_ROLE_SOURCE, connection);
537                 }
538             } else {
539                 if (connection->a2dp_sink_config_process.outgoing_active || !connection->a2dp_source_config_process.outgoing_active){
540                     a2dp_config_process_ready_for_sep_discovery(AVDTP_ROLE_SINK, connection);
541                 }
542             }
543             break;
544 
545         case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND:
546             cid = avdtp_subevent_signaling_sep_found_get_avdtp_cid(packet);
547             connection = avdtp_get_connection_for_avdtp_cid(cid);
548             btstack_assert(connection != NULL);
549             config_process = a2dp_config_process_for_role(role, connection);
550 
551             if (config_process->state == A2DP_DISCOVER_SEPS) {
552                 avdtp_sep_t sep;
553                 memset(&sep, 0, sizeof(avdtp_sep_t));
554                 sep.seid       = avdtp_subevent_signaling_sep_found_get_remote_seid(packet);;
555                 sep.in_use     = avdtp_subevent_signaling_sep_found_get_in_use(packet);
556                 sep.media_type = (avdtp_media_type_t) avdtp_subevent_signaling_sep_found_get_media_type(packet);
557                 sep.type       = (avdtp_sep_type_t) avdtp_subevent_signaling_sep_found_get_sep_type(packet);
558                 log_info("A2DP Found sep: remote seid 0x%02x, in_use %d, media type %d, sep type %s, index %d",
559                          sep.seid, sep.in_use, sep.media_type, sep.type == AVDTP_SOURCE ? "source" : "sink",
560                          a2dp_config_process_sep_discovery_count);
561                 avdtp_sep_type_t matching_type = (role == AVDTP_ROLE_SOURCE) ? AVDTP_SINK : AVDTP_SOURCE;
562                 if ((sep.type == matching_type) && (sep.in_use == false)) {
563                     a2dp_config_process_sep_discovery_seps[a2dp_config_process_sep_discovery_count++] = sep;
564                 }
565             }
566             break;
567 
568         case AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE:
569             cid = avdtp_subevent_signaling_sep_dicovery_done_get_avdtp_cid(packet);
570             connection = avdtp_get_connection_for_avdtp_cid(cid);
571             btstack_assert(connection != NULL);
572             config_process = a2dp_config_process_for_role(role, connection);
573 
574             if (config_process->state != A2DP_DISCOVER_SEPS) break;
575 
576             if (a2dp_config_process_sep_discovery_count > 0){
577                 config_process->state = A2DP_GET_CAPABILITIES;
578                 a2dp_config_process_sep_discovery_index = 0;
579                 config_process->have_config = false;
580             } else {
581                 if (config_process->outgoing_active){
582                     config_process->outgoing_active = false;
583                     connection = avdtp_get_connection_for_avdtp_cid(cid);
584                     btstack_assert(connection != NULL);
585                     a2dp_emit_streaming_connection_failed_for_role(role, connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND);
586                 }
587 
588                 // continue
589                 config_process->state = A2DP_CONNECTED;
590                 a2dp_config_process_sep_discovery_cid = 0;
591                 a2dp_config_process_discover_seps_with_next_waiting_connection();
592             }
593             break;
594 
595         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:
596             cid = avdtp_subevent_signaling_media_codec_sbc_capability_get_avdtp_cid(packet);
597             connection = avdtp_get_connection_for_avdtp_cid(cid);
598             btstack_assert(connection != NULL);
599             config_process = a2dp_config_process_for_role(role, connection);
600 
601             if (config_process->state != A2DP_GET_CAPABILITIES) break;
602 
603             // forward codec capability
604             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY);
605 
606 #ifndef ENABLE_A2DP_EXPLICIT_CONFIG
607             // select SEP if none configured yet
608             if (config_process->have_config == false){
609                 // find SBC stream endpoint
610                 avdtp_sep_type_t required_sep_type = (role == AVDTP_ROLE_SOURCE) ? AVDTP_SOURCE : AVDTP_SINK;
611                 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_source_stream_endpoint_for_media_codec_and_type(AVDTP_CODEC_SBC, required_sep_type);
612                 if (stream_endpoint != NULL){
613                     // choose SBC config params
614                     avdtp_configuration_sbc_t configuration;
615                     configuration.sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
616                     configuration.channel_mode       = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
617                     configuration.block_length       = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
618                     configuration.subbands           = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
619                     configuration.allocation_method  = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
620                     configuration.max_bitpool_value  = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet));
621                     configuration.min_bitpool_value  = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet));
622 
623                     // and pre-select this endpoint
624                     local_seid = avdtp_stream_endpoint_seid(stream_endpoint);
625                     remote_seid = avdtp_subevent_signaling_media_codec_sbc_capability_get_remote_seid(packet);
626                     a2dp_config_process_set_sbc(role, cid, local_seid, remote_seid, &configuration);
627                 }
628             }
629 #endif
630             break;
631             // forward codec capability
632         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY:
633             cid = avdtp_subevent_signaling_media_codec_mpeg_audio_capability_get_avdtp_cid(packet);
634             a2dp_config_process_handle_media_capability(role, cid,
635                                                         A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY,
636                                                         packet, size);
637             break;
638         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY:
639             cid = avdtp_subevent_signaling_media_codec_mpeg_aac_capability_get_avdtp_cid(packet);
640             a2dp_config_process_handle_media_capability(role, cid,
641                                                         A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY,
642                                                         packet, size);
643             break;
644         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY:
645             cid = avdtp_subevent_signaling_media_codec_atrac_capability_get_avdtp_cid(packet);
646             a2dp_config_process_handle_media_capability(role, cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY,
647                                                         packet,
648                                                         size);
649             break;
650         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY:
651             cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet);
652             a2dp_config_process_handle_media_capability(role, cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY,
653                                                         packet,
654                                                         size);
655             break;
656 
657             // not forwarded
658         case AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY:
659         case AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY:
660         case AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY:
661         case AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY:
662         case AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY:
663         case AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY:
664             break;
665 
666         case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY:
667             cid = avdtp_subevent_signaling_delay_reporting_capability_get_avdtp_cid(packet);
668             connection = avdtp_get_connection_for_avdtp_cid(cid);
669             btstack_assert(connection != NULL);
670             config_process = a2dp_config_process_for_role(role, connection);
671             log_info("received AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY, cid 0x%02x, state %d", cid, config_process->state);
672 
673             if (config_process->state != A2DP_GET_CAPABILITIES) break;
674 
675             // store delay reporting capability
676             a2dp_config_process_sep_discovery_seps[a2dp_config_process_sep_discovery_index].registered_service_categories |= 1 << AVDTP_DELAY_REPORTING;
677 
678             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY);
679             break;
680 
681         case AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE:
682             cid = avdtp_subevent_signaling_capabilities_done_get_avdtp_cid(packet);
683             connection = avdtp_get_connection_for_avdtp_cid(cid);
684             btstack_assert(connection != NULL);
685             config_process = a2dp_config_process_for_role(role, connection);
686 
687             if (config_process->state != A2DP_GET_CAPABILITIES) break;
688 
689             // forward capabilities done for endpoint
690             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CAPABILITIES_DONE);
691 
692             // endpoint was not suitable, check next one if possible
693             a2dp_config_process_sep_discovery_index++;
694 
695             if (a2dp_config_process_sep_discovery_index >= a2dp_config_process_sep_discovery_count){
696 
697                 // emit 'all capabilities for all seps reported'
698                 uint8_t event[6];
699                 uint8_t pos = 0;
700                 event[pos++] = HCI_EVENT_A2DP_META;
701                 event[pos++] = sizeof(event) - 2;
702                 event[pos++] = A2DP_SUBEVENT_SIGNALING_CAPABILITIES_COMPLETE;
703                 little_endian_store_16(event, pos, cid);
704                 a2dp_emit_role(role, event, sizeof(event));
705 
706                 // do we have a valid config?
707                 if (config_process->have_config){
708                     config_process->state = A2DP_SET_CONFIGURATION;
709                     config_process->have_config = false;
710                     break;
711                 }
712 
713 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG
714                 config_process->state = A2DP_DISCOVERY_DONE;
715                 // TODO call a2dp_discover_seps_with_next_waiting_connection?
716                 break;
717 #endif
718 
719                 // we didn't find a suitable SBC stream endpoint, sorry.
720                 if (config_process->outgoing_active){
721                     config_process->outgoing_active = false;
722                     connection = avdtp_get_connection_for_avdtp_cid(cid);
723                     btstack_assert(connection != NULL);
724                     a2dp_emit_streaming_connection_failed_for_role(role, connection,
725                                                                  ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND);
726                 }
727                 config_process->state = A2DP_CONNECTED;
728                 a2dp_config_process_sep_discovery_cid = 0;
729                 a2dp_config_process_discover_seps_with_next_waiting_connection();
730             }
731             break;
732 
733             // forward codec configuration
734         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:
735             local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet);
736             a2dp_config_process_handle_media_configuration(role, packet, local_seid);
737             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size,
738                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION);
739             break;
740         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION:
741             local_seid = avdtp_subevent_signaling_media_codec_mpeg_audio_configuration_get_local_seid(packet);
742             a2dp_config_process_handle_media_configuration(role, packet, local_seid);
743             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size,
744                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION);
745             break;
746         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION:
747             local_seid = avdtp_subevent_signaling_media_codec_mpeg_aac_configuration_get_local_seid(packet);
748             a2dp_config_process_handle_media_configuration(role, packet, local_seid);
749             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size,
750                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION);
751             break;
752         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION:
753             local_seid = avdtp_subevent_signaling_media_codec_atrac_configuration_get_local_seid(packet);
754             a2dp_config_process_handle_media_configuration(role, packet, local_seid);
755             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size,
756                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION);
757             break;
758         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION:
759             local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet);
760             a2dp_config_process_handle_media_configuration(role, packet, local_seid);
761             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size,
762                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION);
763             break;
764         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
765             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
766             connection = avdtp_get_connection_for_avdtp_cid(cid);
767             btstack_assert(connection != NULL);
768             config_process = a2dp_config_process_for_role(role, connection);
769 
770             if (config_process->state != A2DP_W4_OPEN_STREAM_WITH_SEID) break;
771 
772             config_process->outgoing_active = false;
773             status = avdtp_subevent_streaming_connection_established_get_status(packet);
774             if (status != ERROR_CODE_SUCCESS){
775                 log_info("A2DP source streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status);
776                 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED);
777                 break;
778             }
779 
780             log_info("A2DP source streaming connection established --- avdtp_cid 0x%02x, local seid 0x%02x, remote seid 0x%02x", cid,
781                      avdtp_subevent_streaming_connection_established_get_local_seid(packet),
782                      avdtp_subevent_streaming_connection_established_get_remote_seid(packet));
783             config_process->state = A2DP_STREAMING_OPENED;
784             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED);
785             break;
786 
787         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
788             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
789             connection = avdtp_get_connection_for_avdtp_cid(cid);
790             btstack_assert(connection != NULL);
791             config_process = a2dp_config_process_for_role(role, connection);
792 
793             // restart set config timer while remote is active for current cid
794             if (a2dp_config_process_set_config_timer_active &&
795                 (avdtp_subevent_signaling_accept_get_is_initiator(packet) == 0) &&
796                 (cid == a2dp_config_process_sep_discovery_cid)){
797 
798                 a2dp_config_process_timer_restart();
799                 break;
800             }
801 
802             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
803 
804             log_info("A2DP cmd %s accepted, global state %d, cid 0x%02x", avdtp_si2str(signal_identifier), config_process->state, cid);
805 
806             switch (config_process->state){
807                 case A2DP_GET_CAPABILITIES:
808                     remote_seid = a2dp_config_process_sep_discovery_seps[a2dp_config_process_sep_discovery_index].seid;
809                     log_info("A2DP get capabilities for remote seid 0x%02x", remote_seid);
810                     avdtp_get_all_capabilities(cid, remote_seid, role);
811                     return;
812 
813                 case A2DP_SET_CONFIGURATION:
814                     a2dp_config_process_set_config(role, connection);
815                     return;
816 
817                 case A2DP_W2_OPEN_STREAM_WITH_SEID:
818                     log_info("A2DP open stream ... local seid 0x%02x, active remote seid 0x%02x",
819                              avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
820                              config_process->local_stream_endpoint->remote_sep.seid);
821                     config_process->state = A2DP_W4_OPEN_STREAM_WITH_SEID;
822                     avdtp_source_open_stream(cid,
823                                              avdtp_stream_endpoint_seid(config_process->local_stream_endpoint),
824                                              config_process->local_stream_endpoint->remote_sep.seid);
825                     break;
826 
827                 case A2DP_W2_RECONFIGURE_WITH_SEID:
828                     log_info("A2DP reconfigured ... local seid 0x%02x, active remote seid 0x%02x",
829                              avdtp_stream_endpoint_seid(config_process->local_stream_endpoint),
830                              config_process->local_stream_endpoint->remote_sep.seid);
831                     a2dp_emit_stream_reconfigured_role(role, cid, avdtp_stream_endpoint_seid(
832                             config_process->local_stream_endpoint), ERROR_CODE_SUCCESS);
833                     config_process->state = A2DP_STREAMING_OPENED;
834                     break;
835 
836                 case A2DP_STREAMING_OPENED:
837                     switch (signal_identifier){
838                         case  AVDTP_SI_START:
839                             a2dp_emit_stream_event_for_role(role, cid, avdtp_stream_endpoint_seid(config_process->local_stream_endpoint),
840                                                           A2DP_SUBEVENT_STREAM_STARTED);
841                             break;
842                         case AVDTP_SI_SUSPEND:
843                             a2dp_emit_stream_event_for_role(role, cid, avdtp_stream_endpoint_seid(config_process->local_stream_endpoint),
844                                                           A2DP_SUBEVENT_STREAM_SUSPENDED);
845                             break;
846                         case AVDTP_SI_ABORT:
847                         case AVDTP_SI_CLOSE:
848                             a2dp_emit_stream_event_for_role(role, cid, avdtp_stream_endpoint_seid(config_process->local_stream_endpoint),
849                                                           A2DP_SUBEVENT_STREAM_STOPPED);
850                             break;
851                         default:
852                             break;
853                     }
854                     break;
855 
856                 default:
857                     break;
858             }
859             break;
860 
861         case AVDTP_SUBEVENT_SIGNALING_REJECT:
862             cid = avdtp_subevent_signaling_reject_get_avdtp_cid(packet);
863             connection = avdtp_get_connection_for_avdtp_cid(cid);
864             btstack_assert(connection != NULL);
865             config_process = a2dp_config_process_for_role(role, connection);
866 
867             if (avdtp_subevent_signaling_reject_get_is_initiator(packet) == 0) break;
868 
869             switch (config_process->state) {
870                 case A2DP_W2_RECONFIGURE_WITH_SEID:
871                     log_info("A2DP reconfigure failed ... local seid 0x%02x, active remote seid 0x%02x",
872                              avdtp_stream_endpoint_seid(config_process->local_stream_endpoint),
873                              config_process->local_stream_endpoint->remote_sep.seid);
874                     a2dp_emit_stream_reconfigured_role(role, cid, avdtp_stream_endpoint_seid(
875                             config_process->local_stream_endpoint), ERROR_CODE_UNSPECIFIED_ERROR);
876                     config_process->state = A2DP_STREAMING_OPENED;
877                     break;
878                 default:
879                     config_process->state = A2DP_CONNECTED;
880                     break;
881             }
882 
883             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
884             break;
885 
886         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
887             cid = avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet);
888             connection = avdtp_get_connection_for_avdtp_cid(cid);
889             btstack_assert(connection != NULL);
890             config_process = a2dp_config_process_for_role(role, connection);
891 
892             if (avdtp_subevent_signaling_general_reject_get_is_initiator(packet) == 0) break;
893 
894             config_process->state = A2DP_CONNECTED;
895             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
896             break;
897 
898         case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:
899             cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet);
900             connection = avdtp_get_connection_for_avdtp_cid(cid);
901             btstack_assert(connection != NULL);
902             config_process = a2dp_config_process_for_role(role, connection);
903 
904             config_process->state = A2DP_CONFIGURED;
905             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_STREAM_RELEASED);
906             break;
907 
908         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:
909             cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet);
910             connection = avdtp_get_connection_for_avdtp_cid(cid);
911             btstack_assert(connection != NULL);
912             config_process = a2dp_config_process_for_role(role, connection);
913 
914             // connect/release are passed on to app
915             if (a2dp_config_process_sep_discovery_cid == cid){
916                 a2dp_config_process_timer_stop();
917                 config_process->stream_endpoint_configured = false;
918                 config_process->local_stream_endpoint = NULL;
919 
920                 config_process->state = A2DP_IDLE;
921                 a2dp_config_process_sep_discovery_cid = 0;
922             }
923             a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED);
924             break;
925 
926         default:
927             break;
928     }
929 }
930 
931 uint8_t a2dp_config_process_set_sbc(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_sbc_t * configuration){
932     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
933     if (connection == NULL){
934         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
935     }
936     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
937 
938     uint8_t status = a2dp_config_process_config_init(role, connection, local_seid, remote_seid, AVDTP_CODEC_SBC);
939     if (status != 0) {
940         return status;
941     }
942     // set config in reserved buffer
943     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) config_process->local_stream_endpoint->media_codec_info;
944     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4;
945     avdtp_config_sbc_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
946 
947 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG
948     a2dp_config_process_set_config(role, connection);
949 #endif
950 
951     return ERROR_CODE_SUCCESS;
952 }
953 
954 uint8_t a2dp_config_process_set_mpeg_audio(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_audio_t * configuration){
955     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
956     if (connection == NULL){
957         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
958     }
959     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
960 
961     uint8_t status = a2dp_config_process_config_init(role, connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_1_2_AUDIO);
962     if (status != 0) {
963         return status;
964     }
965 
966     // set config in reserved buffer
967     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *)config_process->local_stream_endpoint->media_codec_info;
968     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4;
969     avdtp_config_mpeg_audio_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
970 
971 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG
972     a2dp_config_process_set_config(role, connection);
973 #endif
974 
975     return ERROR_CODE_SUCCESS;
976 }
977 
978 uint8_t a2dp_config_process_set_mpeg_aac(avdtp_role_t role, uint16_t a2dp_cid,  uint8_t local_seid,  uint8_t remote_seid, const avdtp_configuration_mpeg_aac_t * configuration){
979     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
980     if (connection == NULL){
981         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
982     }
983     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
984 
985     uint8_t status = a2dp_config_process_config_init(AVDTP_ROLE_SOURCE, connection, local_seid, remote_seid,
986                                                      AVDTP_CODEC_MPEG_2_4_AAC);
987     if (status != 0) {
988         return status;
989     }
990     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) config_process->local_stream_endpoint->media_codec_info;
991     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 6;
992     avdtp_config_mpeg_aac_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
993 
994 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG
995     a2dp_config_process_set_config(role, connection);
996 #endif
997 
998     return ERROR_CODE_SUCCESS;
999 }
1000 
1001 uint8_t a2dp_config_process_set_atrac(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_atrac_t * configuration){
1002     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
1003     if (connection == NULL){
1004         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1005     }
1006     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
1007 
1008     uint8_t status = a2dp_config_process_config_init(AVDTP_ROLE_SOURCE, connection, local_seid, remote_seid,
1009                                                      AVDTP_CODEC_ATRAC_FAMILY);
1010     if (status != 0) {
1011         return status;
1012     }
1013 
1014     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) config_process->local_stream_endpoint->media_codec_info;
1015     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 7;
1016     avdtp_config_atrac_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
1017 
1018 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG
1019     a2dp_config_process_set_config(role, connection);
1020 #endif
1021 
1022     return ERROR_CODE_SUCCESS;
1023 }
1024 
1025 uint8_t a2dp_config_process_set_other(avdtp_role_t role, uint16_t a2dp_cid,  uint8_t local_seid, uint8_t remote_seid,
1026                                      const uint8_t * media_codec_information, uint8_t media_codec_information_len){
1027     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
1028     if (connection == NULL){
1029         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1030     }
1031     a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection);
1032 
1033     uint8_t status = a2dp_config_process_config_init(AVDTP_ROLE_SOURCE, connection, local_seid, remote_seid,
1034                                                      AVDTP_CODEC_NON_A2DP);
1035     if (status != 0) {
1036         return status;
1037     }
1038 
1039     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) media_codec_information;
1040     config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_information_len;
1041 
1042 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG
1043         a2dp_config_process_set_config(role, connection);
1044 #endif
1045 
1046     return status;
1047 }
1048