xref: /btstack/src/classic/a2dp_source.c (revision ffc20ab45ec3772611529054d3c40c13bc3369d7)
1 
2 /*
3  * Copyright (C) 2016 BlueKitchen GmbH
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  * 4. Any redistribution, use, or modification is done solely for
18  *    personal benefit and not for any commercial purpose or for
19  *    monetary gain.
20  *
21  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
25  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Please inquire about commercial licensing options at
35  * [email protected]
36  *
37  */
38 
39 /**
40  * Supported use cases:
41  * - single incoming connection: sep discovery starts and stream will get setup if remote sink sep with SBC is found
42  * - single outgoing connection: see above
43  * - outgoing and incoming connection to same device:
44  *    - if outgoing is triggered first, incoming will get ignored.
45  *    - if incoming starts first, start ougoing will fail, but incoming will succeed.
46  * - outgoing and incoming connections to different devices:
47  *    - if outgoing is first, incoming gets ignored.
48  *    - if incoming starts first SEP discovery will get stopped and outgoing will succeed.
49  */
50 
51 #define BTSTACK_FILE__ "a2dp_source.c"
52 
53 #include <stdint.h>
54 #include <string.h>
55 
56 #include "bluetooth_psm.h"
57 #include "bluetooth_sdp.h"
58 #include "btstack_debug.h"
59 #include "btstack_event.h"
60 #include "classic/a2dp.h"
61 #include "classic/a2dp_source.h"
62 #include "classic/avdtp_source.h"
63 #include "classic/avdtp_util.h"
64 #include "classic/sdp_util.h"
65 #include "l2cap.h"
66 #include "a2dp.h"
67 
68 #define AVDTP_MAX_SEP_NUM 10
69 #define A2DP_SET_CONFIG_DELAY_MS 200
70 
71 static const char * a2dp_source_default_service_name = "BTstack A2DP Source Service";
72 static const char * a2dp_default_source_service_provider_name = "BTstack A2DP Source Service Provider";
73 
74 static uint8_t (*a2dp_source_media_config_validator)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size);
75 
76 // config process - singletons using sep_discovery_cid is used as mutex
77 static uint16_t                 a2dp_source_sep_discovery_cid;
78 static uint16_t                 a2dp_source_sep_discovery_count;
79 static uint16_t                 a2dp_source_sep_discovery_index;
80 static avdtp_sep_t              a2dp_source_sep_discovery_seps[AVDTP_MAX_SEP_NUM];
81 static btstack_timer_source_t   a2dp_source_set_config_timer;
82 static bool                     a2dp_source_set_config_timer_active;
83 
84 static void a2dp_source_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
85 static void a2dp_discover_seps_with_next_waiting_connection(void);
86 
87 void a2dp_source_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
88     if (service_provider_name == NULL){
89         service_provider_name = a2dp_default_source_service_provider_name;
90     }
91     if (service_name == NULL){
92         service_name = a2dp_source_default_service_name;
93     }
94     a2dp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE,
95                            supported_features, service_name, service_provider_name);
96 }
97 
98 static void a2dp_source_set_config_timer_handler(btstack_timer_source_t * timer){
99     uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
100 	avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
101 	btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, NULL);
102 	a2dp_source_set_config_timer_active = false;
103 
104 	log_info("Set Config timer fired, avdtp_cid 0x%02x", avdtp_cid);
105 
106     if (connection == NULL) {
107         a2dp_discover_seps_with_next_waiting_connection();
108         return;
109     }
110 
111     if (connection->a2dp_source_config_process.stream_endpoint_configured) return;
112     avdtp_source_discover_stream_endpoints(avdtp_cid);
113 }
114 
115 static void a2dp_source_set_config_timer_start(uint16_t avdtp_cid){
116     log_info("Set Config timer start for cid 0%02x", avdtp_cid);
117     a2dp_source_set_config_timer_active = true;
118     btstack_run_loop_remove_timer(&a2dp_source_set_config_timer);
119     btstack_run_loop_set_timer_handler(&a2dp_source_set_config_timer,a2dp_source_set_config_timer_handler);
120     btstack_run_loop_set_timer(&a2dp_source_set_config_timer, A2DP_SET_CONFIG_DELAY_MS);
121     btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, (void *)(uintptr_t)avdtp_cid);
122     btstack_run_loop_add_timer(&a2dp_source_set_config_timer);
123 }
124 
125 static void a2dp_source_set_config_timer_restart(void){
126     log_info("Set Config timer restart");
127     btstack_run_loop_remove_timer(&a2dp_source_set_config_timer);
128     btstack_run_loop_set_timer(&a2dp_source_set_config_timer, A2DP_SET_CONFIG_DELAY_MS);
129     btstack_run_loop_add_timer(&a2dp_source_set_config_timer);
130 }
131 
132 static void a2dp_source_set_config_timer_stop(void){
133     if (a2dp_source_set_config_timer_active == false) return;
134     log_info("Set Config timer stop");
135     btstack_run_loop_remove_timer(&a2dp_source_set_config_timer);
136 	btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, NULL);
137 	a2dp_source_set_config_timer_active = false;
138 }
139 
140 // Discover seps, both incoming and outgoing
141 static void a2dp_start_discovering_seps(avdtp_connection_t * connection){
142     connection->a2dp_source_config_process.state = A2DP_DISCOVER_SEPS;
143     connection->a2dp_source_config_process.discover_seps = false;
144 
145     a2dp_source_sep_discovery_index = 0;
146     a2dp_source_sep_discovery_count = 0;
147     memset(a2dp_source_sep_discovery_seps, 0, sizeof(avdtp_sep_t) * AVDTP_MAX_SEP_NUM);
148     a2dp_source_sep_discovery_cid = connection->avdtp_cid;
149 
150     // if we initiated the connection, start config right away, else wait a bit to give remote a chance to do it first
151     if (connection->a2dp_source_config_process.outgoing_active){
152         log_info("discover seps");
153         avdtp_source_discover_stream_endpoints(connection->avdtp_cid);
154     } else {
155         log_info("wait a bit, then discover seps");
156         a2dp_source_set_config_timer_start(connection->avdtp_cid);
157     }
158 }
159 
160 static void a2dp_discover_seps_with_next_waiting_connection(void){
161     btstack_assert(a2dp_source_sep_discovery_cid == 0);
162     btstack_linked_list_iterator_t it;
163     btstack_linked_list_iterator_init(&it, avdtp_get_connections());
164     while (btstack_linked_list_iterator_has_next(&it)){
165         avdtp_connection_t * next_connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
166         if (!next_connection->a2dp_source_config_process.discover_seps) continue;
167         a2dp_start_discovering_seps(next_connection);
168     }
169 }
170 
171 static void a2dp_source_ready_for_sep_discovery(avdtp_connection_t * connection){
172     // start discover seps now if:
173     // - outgoing active: signaling for outgoing connection
174     // - outgoing not active: incoming connection and no sep discover ongoing
175 
176     // sep discovery active?
177     if (a2dp_source_sep_discovery_cid == 0){
178         a2dp_start_discovering_seps(connection);
179     } else {
180         // post-pone sep discovery
181         connection->a2dp_source_config_process.discover_seps = true;
182     }
183 }
184 
185 static void a2dp_handle_received_configuration(const uint8_t *packet, uint8_t local_seid) {
186     uint16_t cid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_avdtp_cid(packet);
187     avdtp_connection_t *avdtp_connection = avdtp_get_connection_for_avdtp_cid(cid);
188     btstack_assert(avdtp_connection != NULL);
189     avdtp_connection->a2dp_source_config_process.local_stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
190     // bail out if local seid invalid
191     if (!avdtp_connection->a2dp_source_config_process.local_stream_endpoint) return;
192 
193     // stop timer
194     if (a2dp_source_sep_discovery_cid == cid) {
195         a2dp_source_set_config_timer_stop();
196         a2dp_source_sep_discovery_cid = 0;
197     }
198 
199     avdtp_connection->a2dp_source_config_process.stream_endpoint_configured = true;
200 
201     switch (avdtp_connection->a2dp_source_config_process.state) {
202         case A2DP_W4_SET_CONFIGURATION:
203             // outgoing: discovery and config of remote sink sep successful, trigger stream open
204             avdtp_connection->a2dp_source_config_process.state = A2DP_W2_OPEN_STREAM_WITH_SEID;
205             break;
206         case A2DP_DISCOVER_SEPS:
207         case A2DP_GET_CAPABILITIES:
208         case A2DP_W2_GET_ALL_CAPABILITIES:
209         case A2DP_DISCOVERY_DONE:
210         case A2DP_W4_GET_CONFIGURATION:
211             // incoming: wait for stream open
212             avdtp_connection->a2dp_source_config_process.state = A2DP_W4_OPEN_STREAM_WITH_SEID;
213             break;
214         default:
215             // wait for configuration after sending reconfigure - keep state
216             break;
217     }
218 }
219 
220 static void a2dp_source_set_config(avdtp_connection_t * connection){
221     uint8_t remote_seid = connection->a2dp_source_config_process.local_stream_endpoint->set_config_remote_seid;
222     log_info("A2DP initiate set configuration locally and wait for response ... local seid 0x%02x, remote seid 0x%02x",
223              avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint), remote_seid);
224     connection->a2dp_source_config_process.state = A2DP_W4_SET_CONFIGURATION;
225     avdtp_source_set_configuration(connection->avdtp_cid,
226                                    avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
227                                    remote_seid,
228                                    connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration_bitmap,
229                                    connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration);
230 }
231 
232 static void a2dp_source_handle_media_capability(uint16_t cid, uint8_t a2dp_subevent_id, uint8_t *packet, uint16_t size){
233     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(cid);
234     btstack_assert(connection != NULL);
235     if (connection->a2dp_source_config_process.state != A2DP_GET_CAPABILITIES) return;
236     a2dp_replace_subevent_id_and_emit_source(packet, size, a2dp_subevent_id);
237 }
238 
239 static void a2dp_source_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
240     UNUSED(channel);
241     UNUSED(size);
242 
243     uint16_t cid;
244     avdtp_connection_t * connection;
245 
246     uint8_t signal_identifier;
247     uint8_t status;
248     uint8_t local_seid;
249     uint8_t remote_seid;
250 
251     if (packet_type != HCI_EVENT_PACKET) return;
252     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return;
253 
254     switch (packet[2]){
255         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:
256             cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
257             connection = avdtp_get_connection_for_avdtp_cid(cid);
258             btstack_assert(connection != NULL);
259 
260             status = avdtp_subevent_signaling_connection_established_get_status(packet);
261             if (status != ERROR_CODE_SUCCESS){
262                 // notify about connection error only if we're initiator
263                 if (connection->a2dp_source_config_process.outgoing_active){
264                     log_info("A2DP source signaling connection failed status 0x%02x", status);
265                     connection->a2dp_source_config_process.outgoing_active = false;
266                     a2dp_replace_subevent_id_and_emit_source(packet, size,
267                                                              A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED);
268                 }
269                 break;
270             }
271             log_info("A2DP source signaling connection established avdtp_cid 0x%02x", cid);
272             connection->a2dp_source_config_process.state = A2DP_CONNECTED;
273 
274             // notify app
275             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED);
276 
277             // Windows 10 as Source starts SEP discovery after 1500 ms, but only if it did not get a Discover command
278             // If BTstack is configured for both roles, we need to avoid sending Discover command in Source Role for outgoing Sink connections
279 
280             // For this, we trigger SDP query only if:
281             // a) this is an outgoing source connection
282             // b) this connection wasn't caused by an outgoing sink request
283             if (connection->a2dp_source_config_process.outgoing_active || !connection->a2dp_sink_config_process.outgoing_active){
284                 a2dp_source_ready_for_sep_discovery(connection);
285             }
286             break;
287 
288         case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND:
289             cid = avdtp_subevent_signaling_sep_found_get_avdtp_cid(packet);
290             connection = avdtp_get_connection_for_avdtp_cid(cid);
291             btstack_assert(connection != NULL);
292 
293             if (connection->a2dp_source_config_process.state == A2DP_DISCOVER_SEPS) {
294                 avdtp_sep_t sep;
295                 memset(&sep, 0, sizeof(avdtp_sep_t));
296                 sep.seid       = avdtp_subevent_signaling_sep_found_get_remote_seid(packet);;
297                 sep.in_use     = avdtp_subevent_signaling_sep_found_get_in_use(packet);
298                 sep.media_type = (avdtp_media_type_t) avdtp_subevent_signaling_sep_found_get_media_type(packet);
299                 sep.type       = (avdtp_sep_type_t) avdtp_subevent_signaling_sep_found_get_sep_type(packet);
300                 log_info("A2DP Found sep: remote seid 0x%02x, in_use %d, media type %d, sep type %s, index %d",
301                          sep.seid, sep.in_use, sep.media_type, sep.type == AVDTP_SOURCE ? "source" : "sink",
302                          a2dp_source_sep_discovery_count);
303                 if ((sep.type == AVDTP_SINK) && (sep.in_use == false)) {
304                     a2dp_source_sep_discovery_seps[a2dp_source_sep_discovery_count++] = sep;
305                 }
306             }
307             break;
308 
309         case AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE:
310             cid = avdtp_subevent_signaling_sep_dicovery_done_get_avdtp_cid(packet);
311             connection = avdtp_get_connection_for_avdtp_cid(cid);
312             btstack_assert(connection != NULL);
313 
314             if (connection->a2dp_source_config_process.state != A2DP_DISCOVER_SEPS) break;
315 
316             if (a2dp_source_sep_discovery_count > 0){
317                 connection->a2dp_source_config_process.state = A2DP_GET_CAPABILITIES;
318                 a2dp_source_sep_discovery_index = 0;
319                 connection->a2dp_source_config_process.have_config = false;
320             } else {
321                 if (connection->a2dp_source_config_process.outgoing_active){
322                     connection->a2dp_source_config_process.outgoing_active = false;
323                     connection = avdtp_get_connection_for_avdtp_cid(cid);
324                     btstack_assert(connection != NULL);
325                     a2dp_emit_source_streaming_connection_failed(connection,
326                                                                  ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND);
327                 }
328 
329                 // continue
330                 connection->a2dp_source_config_process.state = A2DP_CONNECTED;
331                 a2dp_source_sep_discovery_cid = 0;
332                 a2dp_discover_seps_with_next_waiting_connection();
333             }
334             break;
335 
336         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:
337             cid = avdtp_subevent_signaling_media_codec_sbc_capability_get_avdtp_cid(packet);
338             connection = avdtp_get_connection_for_avdtp_cid(cid);
339             btstack_assert(connection != NULL);
340 
341             if (connection->a2dp_source_config_process.state != A2DP_GET_CAPABILITIES) break;
342 
343             // forward codec capability
344             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY);
345 
346 #ifndef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
347             // select SEP if none configured yet
348             if (connection->a2dp_source_config_process.have_config == false){
349                 // find SBC stream endpoint
350                 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_source_stream_endpoint_for_media_codec(AVDTP_CODEC_SBC);
351                 if (stream_endpoint != NULL){
352                     // choose SBC config params
353                     avdtp_configuration_sbc_t configuration;
354                     configuration.sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
355                     configuration.channel_mode       = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
356                     configuration.block_length       = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
357                     configuration.subbands           = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
358                     configuration.allocation_method  = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
359                     configuration.max_bitpool_value  = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet));
360                     configuration.min_bitpool_value  = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet));
361 
362                     // and pre-select this endpoint
363                     local_seid = avdtp_stream_endpoint_seid(stream_endpoint);
364                     remote_seid = avdtp_subevent_signaling_media_codec_sbc_capability_get_remote_seid(packet);
365                     a2dp_source_set_config_sbc(cid, local_seid, remote_seid, &configuration);
366                 }
367             }
368 #endif
369             break;
370 
371         // forward codec capability
372         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY:
373             cid = avdtp_subevent_signaling_media_codec_mpeg_audio_capability_get_avdtp_cid(packet);
374             a2dp_source_handle_media_capability(cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY, packet, size);
375             break;
376         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY:
377             cid = avdtp_subevent_signaling_media_codec_mpeg_aac_capability_get_avdtp_cid(packet);
378             a2dp_source_handle_media_capability(cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY, packet, size);
379             break;
380         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY:
381             cid = avdtp_subevent_signaling_media_codec_atrac_capability_get_avdtp_cid(packet);
382             a2dp_source_handle_media_capability(cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY, packet, size);
383             break;
384         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY:
385             cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet);
386             a2dp_source_handle_media_capability(cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY, packet, size);
387             break;
388 
389         // not forwarded
390         case AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY:
391         case AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY:
392         case AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY:
393         case AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY:
394         case AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY:
395         case AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY:
396             break;
397 
398         case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY:
399             cid = avdtp_subevent_signaling_delay_reporting_capability_get_avdtp_cid(packet);
400             connection = avdtp_get_connection_for_avdtp_cid(cid);
401             btstack_assert(connection != NULL);
402             log_info("received AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY, cid 0x%02x, state %d", cid, connection->a2dp_source_config_process.state);
403 
404             if (connection->a2dp_source_config_process.state != A2DP_GET_CAPABILITIES) break;
405 
406             // store delay reporting capability
407             a2dp_source_sep_discovery_seps[a2dp_source_sep_discovery_index].registered_service_categories |= 1 << AVDTP_DELAY_REPORTING;
408 
409             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY);
410             break;
411 
412         case AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE:
413             cid = avdtp_subevent_signaling_capabilities_done_get_avdtp_cid(packet);
414             connection = avdtp_get_connection_for_avdtp_cid(cid);
415             btstack_assert(connection != NULL);
416 
417             if (connection->a2dp_source_config_process.state != A2DP_GET_CAPABILITIES) break;
418 
419             // forward capabilities done for endpoint
420             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_SIGNALING_CAPABILITIES_DONE);
421 
422             // endpoint was not suitable, check next one
423             a2dp_source_sep_discovery_index++;
424             if (a2dp_source_sep_discovery_index >= a2dp_source_sep_discovery_count){
425 
426                 // emit 'all capabilities for all seps reported'
427                 uint8_t event[6];
428                 uint8_t pos = 0;
429                 event[pos++] = HCI_EVENT_A2DP_META;
430                 event[pos++] = sizeof(event) - 2;
431                 event[pos++] = A2DP_SUBEVENT_SIGNALING_CAPABILITIES_COMPLETE;
432                 little_endian_store_16(event, pos, cid);
433                 a2dp_emit_source(event, sizeof(event));
434 
435                 // do we have a valid config?
436                 if (connection->a2dp_source_config_process.have_config){
437                     connection->a2dp_source_config_process.state = A2DP_SET_CONFIGURATION;
438                     connection->a2dp_source_config_process.have_config = false;
439                     break;
440                 }
441 
442 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
443                 connection->a2dp_source_state = A2DP_DISCOVERY_DONE;
444                 // TODO call a2dp_discover_seps_with_next_waiting_connection?
445                 break;
446 #else
447                 // we didn't find a suitable SBC stream endpoint, sorry.
448                 if (connection->a2dp_source_config_process.outgoing_active){
449                     connection->a2dp_source_config_process.outgoing_active = false;
450                     connection = avdtp_get_connection_for_avdtp_cid(cid);
451                     btstack_assert(connection != NULL);
452                     a2dp_emit_source_streaming_connection_failed(connection,
453                                                                  ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND);
454                 }
455                 connection->a2dp_source_config_process.state = A2DP_CONNECTED;
456                 a2dp_source_sep_discovery_cid = 0;
457                 a2dp_discover_seps_with_next_waiting_connection();
458 #endif
459             }
460             break;
461 
462         case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORT:
463             cid = avdtp_subevent_signaling_delay_report_get_avdtp_cid(packet);
464             connection = avdtp_get_connection_for_avdtp_cid(cid);
465             btstack_assert(connection != NULL);
466 
467             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORT);
468             break;
469 
470             // forward codec configuration
471         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:
472             local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet);
473             a2dp_handle_received_configuration(packet, local_seid);
474             a2dp_replace_subevent_id_and_emit_source(packet, size,
475                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION);
476             break;
477         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION:
478             local_seid = avdtp_subevent_signaling_media_codec_mpeg_audio_configuration_get_local_seid(packet);
479             a2dp_handle_received_configuration(packet, local_seid);
480             a2dp_replace_subevent_id_and_emit_source(packet, size,
481                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION);
482             break;
483         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION:
484             local_seid = avdtp_subevent_signaling_media_codec_mpeg_aac_configuration_get_local_seid(packet);
485             a2dp_handle_received_configuration(packet, local_seid);
486             a2dp_replace_subevent_id_and_emit_source(packet, size,
487                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION);
488             break;
489         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION:
490             local_seid = avdtp_subevent_signaling_media_codec_atrac_configuration_get_local_seid(packet);
491             a2dp_handle_received_configuration(packet, local_seid);
492             a2dp_replace_subevent_id_and_emit_source(packet, size,
493                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION);
494             break;
495         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION:
496             local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet);
497             a2dp_handle_received_configuration(packet, local_seid);
498             a2dp_replace_subevent_id_and_emit_source(packet, size,
499                                                      A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION);
500             break;
501 
502         case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW:
503             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW);
504             break;
505 
506         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
507             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
508             connection = avdtp_get_connection_for_avdtp_cid(cid);
509             btstack_assert(connection != NULL);
510 
511             if (connection->a2dp_source_config_process.state != A2DP_W4_OPEN_STREAM_WITH_SEID) break;
512 
513             connection->a2dp_source_config_process.outgoing_active = false;
514             status = avdtp_subevent_streaming_connection_established_get_status(packet);
515             if (status != ERROR_CODE_SUCCESS){
516                 log_info("A2DP source streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status);
517                 a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED);
518                 break;
519             }
520 
521             log_info("A2DP source streaming connection established --- avdtp_cid 0x%02x, local seid 0x%02x, remote seid 0x%02x", cid,
522                 avdtp_subevent_streaming_connection_established_get_local_seid(packet),
523                 avdtp_subevent_streaming_connection_established_get_remote_seid(packet));
524             connection->a2dp_source_config_process.state = A2DP_STREAMING_OPENED;
525             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED);
526             break;
527 
528         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
529             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
530             connection = avdtp_get_connection_for_avdtp_cid(cid);
531             btstack_assert(connection != NULL);
532 
533 			// restart set config timer while remote is active for current cid
534 			if (a2dp_source_set_config_timer_active &&
535 			    (avdtp_subevent_signaling_accept_get_is_initiator(packet) == 0) &&
536 			    (cid == a2dp_source_sep_discovery_cid)){
537 
538 			    a2dp_source_set_config_timer_restart();
539 				break;
540 			}
541 
542             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
543 
544             log_info("A2DP cmd %s accepted, global state %d, cid 0x%02x", avdtp_si2str(signal_identifier), connection->a2dp_source_config_process.state, cid);
545 
546             switch (connection->a2dp_source_config_process.state){
547                 case A2DP_GET_CAPABILITIES:
548                     remote_seid = a2dp_source_sep_discovery_seps[a2dp_source_sep_discovery_index].seid;
549                     log_info("A2DP get capabilities for remote seid 0x%02x", remote_seid);
550                     avdtp_source_get_all_capabilities(cid, remote_seid);
551                     return;
552 
553                 case A2DP_SET_CONFIGURATION:
554                     a2dp_source_set_config(connection);
555                     return;
556 
557                 case A2DP_W2_OPEN_STREAM_WITH_SEID:
558                     log_info("A2DP open stream ... local seid 0x%02x, active remote seid 0x%02x",
559                              avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
560                              connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.seid);
561                     connection->a2dp_source_config_process.state = A2DP_W4_OPEN_STREAM_WITH_SEID;
562                     avdtp_source_open_stream(cid,
563                                              avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
564                                              connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.seid);
565                     break;
566 
567                 case A2DP_W2_RECONFIGURE_WITH_SEID:
568                     log_info("A2DP reconfigured ... local seid 0x%02x, active remote seid 0x%02x",
569                              avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
570                              connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.seid);
571                     a2dp_emit_source_stream_reconfigured(cid, avdtp_stream_endpoint_seid(
572                             connection->a2dp_source_config_process.local_stream_endpoint), ERROR_CODE_SUCCESS);
573                     connection->a2dp_source_config_process.state = A2DP_STREAMING_OPENED;
574                     break;
575 
576                 case A2DP_STREAMING_OPENED:
577                     switch (signal_identifier){
578                         case  AVDTP_SI_START:
579                             a2dp_emit_source_stream_event(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
580                                                           A2DP_SUBEVENT_STREAM_STARTED);
581                             break;
582                         case AVDTP_SI_SUSPEND:
583                             a2dp_emit_source_stream_event(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
584                                                           A2DP_SUBEVENT_STREAM_SUSPENDED);
585                             break;
586                         case AVDTP_SI_ABORT:
587                         case AVDTP_SI_CLOSE:
588                             a2dp_emit_source_stream_event(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
589                                                           A2DP_SUBEVENT_STREAM_STOPPED);
590                             break;
591                         default:
592                             break;
593                     }
594                     break;
595 
596                 default:
597                     break;
598             }
599             break;
600 
601         case AVDTP_SUBEVENT_SIGNALING_REJECT:
602             cid = avdtp_subevent_signaling_reject_get_avdtp_cid(packet);
603             connection = avdtp_get_connection_for_avdtp_cid(cid);
604             btstack_assert(connection != NULL);
605 
606             if (avdtp_subevent_signaling_reject_get_is_initiator(packet) == 0) break;
607 
608             switch (connection->a2dp_source_config_process.state) {
609                 case A2DP_W2_RECONFIGURE_WITH_SEID:
610                     log_info("A2DP reconfigure failed ... local seid 0x%02x, active remote seid 0x%02x",
611                              avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
612                              connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.seid);
613                     a2dp_emit_source_stream_reconfigured(cid, avdtp_stream_endpoint_seid(
614                             connection->a2dp_source_config_process.local_stream_endpoint), ERROR_CODE_UNSPECIFIED_ERROR);
615                     connection->a2dp_source_config_process.state = A2DP_STREAMING_OPENED;
616                     break;
617                 default:
618                     connection->a2dp_source_config_process.state = A2DP_CONNECTED;
619                     break;
620             }
621 
622             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
623             break;
624 
625         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
626             cid = avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet);
627             connection = avdtp_get_connection_for_avdtp_cid(cid);
628             btstack_assert(connection != NULL);
629 
630             if (avdtp_subevent_signaling_general_reject_get_is_initiator(packet) == 0) break;
631 
632             connection->a2dp_source_config_process.state = A2DP_CONNECTED;
633             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
634             break;
635 
636         case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:
637             cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet);
638             connection = avdtp_get_connection_for_avdtp_cid(cid);
639             btstack_assert(connection != NULL);
640 
641             connection->a2dp_source_config_process.state = A2DP_CONFIGURED;
642             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_STREAM_RELEASED);
643             break;
644 
645         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:
646             cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet);
647             connection = avdtp_get_connection_for_avdtp_cid(cid);
648             btstack_assert(connection != NULL);
649 
650             // connect/release are passed on to app
651             if (a2dp_source_sep_discovery_cid == cid){
652                 a2dp_source_set_config_timer_stop();
653                 connection->a2dp_source_config_process.stream_endpoint_configured = false;
654                 connection->a2dp_source_config_process.local_stream_endpoint = NULL;
655 
656                 connection->a2dp_source_config_process.state = A2DP_IDLE;
657                 a2dp_source_sep_discovery_cid = 0;
658             }
659             a2dp_replace_subevent_id_and_emit_source(packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED);
660             break;
661 
662         default:
663             break;
664     }
665 }
666 void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){
667     btstack_assert(callback != NULL);
668 
669     avdtp_source_register_packet_handler(&a2dp_source_packet_handler_internal);
670     a2dp_register_source_packet_handler(callback);
671 }
672 
673 void a2dp_source_init(void){
674     a2dp_init();
675     avdtp_source_init();
676 }
677 
678 void a2dp_source_deinit(void){
679     a2dp_deinit();
680     avdtp_source_deinit();
681     a2dp_source_media_config_validator = NULL;
682     a2dp_source_sep_discovery_cid = 0;
683 }
684 
685 avdtp_stream_endpoint_t * a2dp_source_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type,
686                                                              const uint8_t *codec_capabilities, uint16_t codec_capabilities_len,
687                                                              uint8_t * codec_configuration, uint16_t codec_configuration_len){
688     avdtp_stream_endpoint_t * stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type);
689     if (!stream_endpoint){
690         return NULL;
691     }
692     avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(stream_endpoint));
693     avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(stream_endpoint), media_type, media_codec_type,
694                                                codec_capabilities, codec_capabilities_len);
695 	avdtp_source_register_delay_reporting_category(avdtp_stream_endpoint_seid(stream_endpoint));
696 
697 	// store user codec configuration buffer
698 	stream_endpoint->media_codec_type = media_codec_type;
699 	stream_endpoint->media_codec_configuration_info = codec_configuration;
700     stream_endpoint->media_codec_configuration_len  = codec_configuration_len;
701 
702     return stream_endpoint;
703 }
704 
705 void a2dp_source_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){
706     avdtp_source_finalize_stream_endpoint(stream_endpoint);
707 }
708 
709 uint8_t a2dp_source_establish_stream(bd_addr_t remote_addr, uint16_t *avdtp_cid) {
710 
711     uint16_t outgoing_cid;
712 
713     avdtp_connection_t * connection = avdtp_get_connection_for_bd_addr(remote_addr);
714     if (connection == NULL){
715         uint8_t status = avdtp_source_connect(remote_addr, &outgoing_cid);
716         if (status != ERROR_CODE_SUCCESS) {
717             // if there's already a connection for for remote addr, avdtp_source_connect fails,
718             // but the stream will get set-up nevertheless
719             return status;
720         }
721         connection = avdtp_get_connection_for_avdtp_cid(outgoing_cid);
722         btstack_assert(connection != NULL);
723 
724         // setup state
725         connection->a2dp_source_config_process.outgoing_active = true;
726         connection->a2dp_source_config_process.state = A2DP_W4_CONNECTED;
727         *avdtp_cid = outgoing_cid;
728 
729     } else {
730         if (connection->a2dp_source_config_process.outgoing_active || connection->a2dp_source_config_process.stream_endpoint_configured) {
731             return ERROR_CODE_COMMAND_DISALLOWED;
732         }
733 
734         // check state
735         switch (connection->a2dp_source_config_process.state){
736             case A2DP_IDLE:
737             case A2DP_CONNECTED:
738                 // restart process e.g. if there no suitable stream endpoints or they had been in use
739                 connection->a2dp_source_config_process.outgoing_active = true;
740                 *avdtp_cid = connection->avdtp_cid;
741                 a2dp_source_ready_for_sep_discovery(connection);
742                 break;
743             default:
744                 return ERROR_CODE_COMMAND_DISALLOWED;
745         }
746     }
747     return ERROR_CODE_SUCCESS;
748 }
749 
750 uint8_t a2dp_source_disconnect(uint16_t avdtp_cid){
751     return avdtp_disconnect(avdtp_cid);
752 }
753 
754 
755 uint8_t a2dp_source_start_stream(uint16_t avdtp_cid, uint8_t local_seid){
756     return avdtp_start_stream(avdtp_cid, local_seid);
757 }
758 
759 uint8_t a2dp_source_pause_stream(uint16_t avdtp_cid, uint8_t local_seid){
760     return avdtp_suspend_stream(avdtp_cid, local_seid);
761 }
762 
763 void a2dp_source_stream_endpoint_request_can_send_now(uint16_t avdtp_cid, uint8_t local_seid){
764     avdtp_source_stream_endpoint_request_can_send_now(avdtp_cid, local_seid);
765 }
766 
767 int a2dp_max_media_payload_size(uint16_t avdtp_cid, uint8_t local_seid){
768     return avdtp_max_media_payload_size(avdtp_cid, local_seid);
769 }
770 
771 int a2dp_source_stream_send_media_payload(uint16_t avdtp_cid, uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){
772     return avdtp_source_stream_send_media_payload(avdtp_cid, local_seid, storage, num_bytes_to_copy, num_frames, marker);
773 }
774 
775 uint8_t a2dp_source_stream_send_media_payload_rtp(uint16_t a2dp_cid, uint8_t local_seid, uint8_t marker, uint8_t * payload, uint16_t payload_size){
776     return avdtp_source_stream_send_media_payload_rtp(a2dp_cid, local_seid, marker, payload, payload_size);
777 }
778 
779 uint8_t	a2dp_source_stream_send_media_packet(uint16_t a2dp_cid, uint8_t local_seid, const uint8_t * packet, uint16_t size){
780     return avdtp_source_stream_send_media_packet(a2dp_cid, local_seid, packet, size);
781 }
782 
783 static uint8_t a2dp_source_config_init(avdtp_connection_t *connection, uint8_t local_seid, uint8_t remote_seid,
784                                        avdtp_media_codec_type_t codec_type) {
785 
786     // check state
787     switch (connection->a2dp_source_config_process.state){
788         case A2DP_DISCOVERY_DONE:
789         case A2DP_GET_CAPABILITIES:
790             break;
791         default:
792             return ERROR_CODE_COMMAND_DISALLOWED;
793     }
794 
795     // lookup local stream endpoint
796     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
797     if (stream_endpoint == NULL){
798         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
799     }
800 
801     // lookup remote stream endpoint
802     avdtp_sep_t * remote_sep = NULL;
803     uint8_t i;
804     for (i=0; i < a2dp_source_sep_discovery_count; i++){
805         if (a2dp_source_sep_discovery_seps[i].seid == remote_seid){
806             remote_sep = &a2dp_source_sep_discovery_seps[i];
807         }
808     }
809     if (remote_sep == NULL){
810         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
811     }
812 
813     // set media configuration
814     stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
815     stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
816     stream_endpoint->remote_configuration.media_codec.media_codec_type = codec_type;
817     // remote seid to use
818     stream_endpoint->set_config_remote_seid = remote_seid;
819     // enable delay reporting if supported
820     if (remote_sep->registered_service_categories & (1<<AVDTP_DELAY_REPORTING)){
821         stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_DELAY_REPORTING, 1);
822     }
823 
824     // suitable Sink stream endpoint found, configure it
825     connection->a2dp_source_config_process.local_stream_endpoint = stream_endpoint;
826     connection->a2dp_source_config_process.have_config = true;
827 
828 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
829     // continue outgoing configuration
830     if (connection->a2dp_source_state == A2DP_DISCOVERY_DONE){
831         connection->a2dp_source_state = A2DP_SET_CONFIGURATION;
832     }
833 #endif
834 
835     return ERROR_CODE_SUCCESS;
836 }
837 
838 uint8_t a2dp_source_set_config_sbc(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_sbc_t * configuration){
839     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
840     if (connection == NULL){
841         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
842     }
843 
844     uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_SBC);
845     if (status != 0) {
846         return status;
847     }
848     // set config in reserved buffer
849     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info;
850     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4;
851     avdtp_config_sbc_store(connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
852 
853 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
854     a2dp_source_set_config(connection);
855 #endif
856 
857     return ERROR_CODE_SUCCESS;
858 }
859 
860 uint8_t a2dp_source_set_config_mpeg_audio(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_audio_t * configuration){
861     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
862     if (connection == NULL){
863         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
864     }
865 
866     uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_1_2_AUDIO);
867     if (status != 0) {
868         return status;
869     }
870 
871     // set config in reserved buffer
872     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *)connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info;
873     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4;
874     avdtp_config_mpeg_audio_store(connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
875 
876 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
877     a2dp_source_set_config(connection);
878 #endif
879 
880     return ERROR_CODE_SUCCESS;
881 }
882 
883 uint8_t a2dp_source_set_config_mpeg_aac(uint16_t a2dp_cid,  uint8_t local_seid,  uint8_t remote_seid, const avdtp_configuration_mpeg_aac_t * configuration){
884     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
885     if (connection == NULL){
886         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
887     }
888 
889     uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_2_4_AAC);
890     if (status != 0) {
891         return status;
892     }
893     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info;
894     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 6;
895     avdtp_config_mpeg_aac_store(connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
896 
897 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
898     a2dp_source_set_config(connection);
899 #endif
900 
901     return ERROR_CODE_SUCCESS;
902 }
903 
904 uint8_t a2dp_source_set_config_atrac(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_atrac_t * configuration){
905     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
906     if (connection == NULL){
907         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
908     }
909 
910     uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_ATRAC_FAMILY);
911     if (status != 0) {
912         return status;
913     }
914 
915     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info;
916     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 7;
917     avdtp_config_atrac_store(connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration);
918 
919 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
920     a2dp_source_set_config(connection);
921 #endif
922 
923     return ERROR_CODE_SUCCESS;
924 }
925 
926 uint8_t a2dp_source_set_config_other(uint16_t a2dp_cid,  uint8_t local_seid, uint8_t remote_seid,
927                                      const uint8_t * media_codec_information, uint8_t media_codec_information_len){
928     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid);
929     if (connection == NULL){
930         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
931     }
932 
933     uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_NON_A2DP);
934     if (status != 0) {
935         return status;
936     }
937 
938     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) media_codec_information;
939     connection->a2dp_source_config_process.local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_information_len;
940 
941 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG
942     a2dp_source_set_config(connection);
943 #endif
944 
945     return status;
946 }
947 
948 uint8_t a2dp_source_reconfigure_stream_sampling_frequency(uint16_t avdtp_cid, uint32_t sampling_frequency){
949     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
950     if (connection == NULL){
951         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
952     }
953 
954     if (connection->a2dp_source_config_process.state != A2DP_STREAMING_OPENED) {
955         return ERROR_CODE_COMMAND_DISALLOWED;
956     }
957 
958     btstack_assert(connection->a2dp_source_config_process.local_stream_endpoint != NULL);
959 
960     log_info("Reconfigure avdtp_cid 0x%02x", avdtp_cid);
961 
962     avdtp_media_codec_type_t codec_type = connection->a2dp_source_config_process.local_stream_endpoint->sep.capabilities.media_codec.media_codec_type;
963     uint8_t codec_info_len;
964     switch (codec_type){
965         case AVDTP_CODEC_SBC:
966             codec_info_len = 4;
967             (void)memcpy(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len);
968             avdtp_config_sbc_set_sampling_frequency(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, sampling_frequency);
969             break;
970         case AVDTP_CODEC_MPEG_1_2_AUDIO:
971             codec_info_len = 4;
972             (void)memcpy(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len);
973             avdtp_config_mpeg_audio_set_sampling_frequency(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, sampling_frequency);
974             break;
975         case AVDTP_CODEC_MPEG_2_4_AAC:
976             codec_info_len = 6;
977             (void)memcpy(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len);
978             avdtp_config_mpeg_aac_set_sampling_frequency(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, sampling_frequency);
979             break;
980         case AVDTP_CODEC_ATRAC_FAMILY:
981             codec_info_len = 7;
982             (void)memcpy(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len);
983             avdtp_config_atrac_set_sampling_frequency(connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info, sampling_frequency);
984             break;
985         default:
986             return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
987     }
988 
989     avdtp_capabilities_t new_configuration;
990     new_configuration.media_codec.media_type = AVDTP_AUDIO;
991     new_configuration.media_codec.media_codec_type = codec_type;
992     new_configuration.media_codec.media_codec_information_len = codec_info_len;
993     new_configuration.media_codec.media_codec_information = connection->a2dp_source_config_process.local_stream_endpoint->media_codec_info;
994 
995     // start reconfigure
996     connection->a2dp_source_config_process.state = A2DP_W2_RECONFIGURE_WITH_SEID;
997 
998     return avdtp_source_reconfigure(
999             avdtp_cid,
1000             avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint),
1001             connection->a2dp_source_config_process.local_stream_endpoint->remote_sep.seid,
1002             1 << AVDTP_MEDIA_CODEC,
1003             new_configuration
1004     );
1005 }
1006 
1007 static uint8_t a2dp_source_media_config_validator_callback(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size){
1008     uint8_t error = 0;
1009     if (a2dp_source_media_config_validator != NULL) {
1010         // update subevent id and call validator
1011         uint8_t avdtp_subevent_id = event[2];
1012         uint8_t a2dp_subevent_id = a2dp_subevent_id_for_avdtp_subevent_id(avdtp_subevent_id);
1013         uint8_t * subevent_field = (uint8_t *) &event[2];
1014         *subevent_field = a2dp_subevent_id;
1015         error = (*a2dp_source_media_config_validator)(stream_endpoint, event, size);
1016         *subevent_field = avdtp_subevent_id;
1017     }
1018     return error;
1019 }
1020 
1021 void a2dp_source_register_media_config_validator(uint8_t (*callback)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size)){
1022     a2dp_source_media_config_validator = callback;
1023     avdtp_source_register_media_config_validator(&a2dp_source_media_config_validator_callback);
1024 }
1025 
1026