xref: /btstack/src/classic/a2dp_source.c (revision 8d9aebf58cd62062bf057f11efbc1327a0d4f7be)
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 MATTHIAS
25  * RINGWALD 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 #define BTSTACK_FILE__ "a2dp_source.c"
40 
41 #include <stdint.h>
42 #include <string.h>
43 
44 #include "bluetooth_psm.h"
45 #include "bluetooth_sdp.h"
46 #include "btstack_debug.h"
47 #include "btstack_event.h"
48 #include "classic/a2dp_source.h"
49 #include "classic/avdtp_source.h"
50 #include "classic/avdtp_util.h"
51 #include "classic/sdp_util.h"
52 #include "l2cap.h"
53 
54 #define AVDTP_MAX_SEP_NUM 10
55 
56 static const char * default_a2dp_source_service_name = "BTstack A2DP Source Service";
57 static const char * default_a2dp_source_service_provider_name = "BTstack A2DP Source Service Provider";
58 static avdtp_context_t a2dp_source_context;
59 
60 static a2dp_state_t app_state = A2DP_IDLE;
61 static avdtp_stream_endpoint_context_t sc;
62 static avdtp_sep_t remote_seps[AVDTP_MAX_SEP_NUM];
63 static int num_remote_seps = 0;
64 
65 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
66 
67 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){
68     uint8_t* attribute;
69     de_create_sequence(service);
70 
71     // 0x0000 "Service Record Handle"
72     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
73     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
74 
75     // 0x0001 "Service Class ID List"
76     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
77     attribute = de_push_sequence(service);
78     {
79         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE);
80     }
81     de_pop_sequence(service, attribute);
82 
83     // 0x0004 "Protocol Descriptor List"
84     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
85     attribute = de_push_sequence(service);
86     {
87         uint8_t* l2cpProtocol = de_push_sequence(attribute);
88         {
89             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
90             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVDTP);
91         }
92         de_pop_sequence(attribute, l2cpProtocol);
93 
94         uint8_t* avProtocol = de_push_sequence(attribute);
95         {
96             de_add_number(avProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);  // avProtocol_service
97             de_add_number(avProtocol,  DE_UINT, DE_SIZE_16,  0x0103);  // version
98         }
99         de_pop_sequence(attribute, avProtocol);
100     }
101     de_pop_sequence(service, attribute);
102 
103     // 0x0005 "Public Browse Group"
104     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
105     attribute = de_push_sequence(service);
106     {
107         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
108     }
109     de_pop_sequence(service, attribute);
110 
111     // 0x0009 "Bluetooth Profile Descriptor List"
112     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
113     attribute = de_push_sequence(service);
114     {
115         uint8_t *a2dProfile = de_push_sequence(attribute);
116         {
117             de_add_number(a2dProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION);
118             de_add_number(a2dProfile,  DE_UINT, DE_SIZE_16, 0x0103);
119         }
120         de_pop_sequence(attribute, a2dProfile);
121     }
122     de_pop_sequence(service, attribute);
123 
124 
125     // 0x0100 "Service Name"
126     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
127     if (service_name){
128         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
129     } else {
130         de_add_data(service,  DE_STRING, strlen(default_a2dp_source_service_name), (uint8_t *) default_a2dp_source_service_name);
131     }
132 
133     // 0x0100 "Provider Name"
134     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
135     if (service_provider_name){
136         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
137     } else {
138         de_add_data(service,  DE_STRING, strlen(default_a2dp_source_service_provider_name), (uint8_t *) default_a2dp_source_service_provider_name);
139     }
140 
141     // 0x0311 "Supported Features"
142     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
143     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
144 }
145 
146 
147 static void a2dp_streaming_emit_can_send_media_packet_now(btstack_packet_handler_t callback, uint16_t cid, uint8_t seid){
148     if (!callback) return;
149     uint8_t event[8];
150     int pos = 0;
151     event[pos++] = HCI_EVENT_A2DP_META;
152     event[pos++] = sizeof(event) - 2;
153     event[pos++] = A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW;
154     little_endian_store_16(event, pos, cid);
155     pos += 2;
156     event[pos++] = seid;
157     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
158 }
159 
160 static inline void a2dp_signaling_emit_delay_report_capability(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
161     if (!callback) return;
162     event[0] = HCI_EVENT_A2DP_META;
163     event[2] = A2DP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY;
164     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
165 }
166 
167 static inline void a2dp_signaling_emit_capabilities_done(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
168     if (!callback) return;
169     event[0] = HCI_EVENT_A2DP_META;
170     event[2] = A2DP_SUBEVENT_SIGNALING_CAPABILITIES_DONE;
171     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
172 }
173 
174 static inline void a2dp_signaling_emit_delay_report(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
175     if (!callback) return;
176     event[0] = HCI_EVENT_A2DP_META;
177     event[2] = A2DP_SUBEVENT_SIGNALING_DELAY_REPORT;
178     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
179 }
180 
181 static inline void a2dp_signaling_emit_media_codec_sbc(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
182     if (!callback) return;
183     if (event_size < 18) return;
184     event[0] = HCI_EVENT_A2DP_META;
185     event[2] = A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION;
186     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
187 }
188 
189 static inline void a2dp_signaling_emit_reject_cmd(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
190     if (!callback) return;
191     if (event_size < 18) return;
192     event[0] = HCI_EVENT_A2DP_META;
193     event[2] = A2DP_SUBEVENT_COMMAND_REJECTED;
194     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
195 }
196 
197 static void a2dp_signaling_emit_connection_established(btstack_packet_handler_t callback, uint16_t cid, bd_addr_t addr, uint8_t status){
198     if (!callback) return;
199     uint8_t event[12];
200     int pos = 0;
201     event[pos++] = HCI_EVENT_A2DP_META;
202     event[pos++] = sizeof(event) - 2;
203     event[pos++] = A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED;
204     little_endian_store_16(event, pos, cid);
205     pos += 2;
206     reverse_bd_addr(addr,&event[pos]);
207     pos += 6;
208     event[pos++] = status;
209     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
210 }
211 
212 static void a2dp_signaling_emit_control_command(btstack_packet_handler_t callback, uint16_t cid, uint8_t local_seid, uint8_t cmd){
213     if (!callback) return;
214     uint8_t event[6];
215     int pos = 0;
216     event[pos++] = HCI_EVENT_A2DP_META;
217     event[pos++] = sizeof(event) - 2;
218     event[pos++] = cmd;
219     little_endian_store_16(event, pos, cid);
220     pos += 2;
221     event[pos++] = local_seid;
222     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
223 }
224 
225 static void a2dp_signaling_emit_reconfigured(btstack_packet_handler_t callback, uint16_t cid, uint8_t local_seid, uint8_t status){
226     if (!callback) return;
227     uint8_t event[7];
228     int pos = 0;
229     event[pos++] = HCI_EVENT_A2DP_META;
230     event[pos++] = sizeof(event) - 2;
231     event[pos++] = A2DP_SUBEVENT_STREAM_RECONFIGURED;
232     little_endian_store_16(event, pos, cid);
233     pos += 2;
234     event[pos++] = local_seid;
235     event[pos++] = status;
236     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
237 }
238 
239 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
240     UNUSED(channel);
241     UNUSED(size);
242 
243     uint8_t signal_identifier;
244     uint8_t status;
245     uint8_t local_seid;
246     uint8_t remote_seid;
247     uint16_t cid;
248     bd_addr_t address;
249 
250     if (packet_type != HCI_EVENT_PACKET) return;
251     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return;
252 
253     switch (packet[2]){
254         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:{
255             avdtp_subevent_signaling_connection_established_get_bd_addr(packet, sc.remote_addr);
256             cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
257             status = avdtp_subevent_signaling_connection_established_get_status(packet);
258 
259             if (status != 0){
260                 log_info("AVDTP_SUBEVENT_SIGNALING_CONNECTION failed status %d ---", status);
261                 app_state = A2DP_IDLE;
262                 a2dp_signaling_emit_connection_established(a2dp_source_context.a2dp_callback, cid, sc.remote_addr, status);
263                 break;
264             }
265             log_info("A2DP_SUBEVENT_SIGNALING_CONNECTION established avdtp_cid 0x%02x ---", a2dp_source_context.avdtp_cid);
266             sc.active_remote_sep = NULL;
267             sc.active_remote_sep_index = 0;
268             app_state = A2DP_W2_DISCOVER_SEPS;
269             num_remote_seps = 0;
270             memset(remote_seps, 0, sizeof(avdtp_sep_t) * AVDTP_MAX_SEP_NUM);
271             a2dp_signaling_emit_connection_established(a2dp_source_context.a2dp_callback, cid, sc.remote_addr, status);
272             avdtp_source_discover_stream_endpoints(cid);
273             break;
274         }
275 
276         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:{
277             log_info("A2DP received SBC capability, received: local seid %d, remote seid %d, expected: local seid %d, remote seid %d",
278                 avdtp_subevent_signaling_media_codec_sbc_capability_get_local_seid(packet),
279                 avdtp_subevent_signaling_media_codec_sbc_capability_get_remote_seid(packet),
280                 avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid );
281 
282             if (!sc.local_stream_endpoint) {
283                 log_error("invalid local seid %d", avdtp_subevent_signaling_media_codec_sbc_capability_get_local_seid(packet));
284                 return;
285             }
286 
287             uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
288             uint8_t channel_mode = avdtp_choose_sbc_channel_mode(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
289             uint8_t block_length = avdtp_choose_sbc_block_length(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
290             uint8_t subbands = avdtp_choose_sbc_subbands(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
291 
292             uint8_t allocation_method = avdtp_choose_sbc_allocation_method(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
293             uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet));
294             uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet));
295 
296 
297             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[0] = (sampling_frequency << 4) | channel_mode;
298             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[1] = (block_length << 4) | (subbands << 2) | allocation_method;
299             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[2] = min_bitpool_value;
300             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[3] = max_bitpool_value;
301 
302             sc.local_stream_endpoint->remote_configuration_bitmap = store_bit16(sc.local_stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
303             sc.local_stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
304             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
305 
306             app_state = A2DP_W2_SET_CONFIGURATION;
307             break;
308         }
309         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY:
310             log_info("received non SBC codec. not implemented");
311             break;
312 
313         case AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY:
314             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY, remote seid %d", avdtp_subevent_signaling_media_transport_capability_get_remote_seid(packet));
315             break;
316         case AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY:
317             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY, remote seid %d", avdtp_subevent_signaling_reporting_capability_get_remote_seid(packet));
318             break;
319         case AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY:
320             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY, remote seid %d", avdtp_subevent_signaling_recovery_capability_get_remote_seid(packet));
321             break;
322         case AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY:
323             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY, remote seid %d", avdtp_subevent_signaling_content_protection_capability_get_remote_seid(packet));
324             break;
325         case AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY:
326             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY, remote seid %d", avdtp_subevent_signaling_header_compression_capability_get_remote_seid(packet));
327             break;
328         case AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY:
329             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY, remote seid %d", avdtp_subevent_signaling_multiplexing_capability_get_remote_seid(packet));
330             break;
331         case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY:
332             a2dp_signaling_emit_delay_report_capability(a2dp_source_context.a2dp_callback, packet, size);
333             break;
334         case AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE:
335             a2dp_signaling_emit_capabilities_done(a2dp_source_context.a2dp_callback, packet, size);
336             break;
337 
338         case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORT:
339             // forward packet:
340             a2dp_signaling_emit_delay_report(a2dp_source_context.a2dp_callback, packet, size);
341             break;
342         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{
343             // TODO check cid
344             sc.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet);
345             sc.channel_mode = avdtp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet);
346             sc.block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet);
347             sc.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet);
348             sc.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet);
349             sc.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet);
350             sc.min_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet);
351             // TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet);
352             log_info("A2DP received SBC Config: sample rate %u, max bitpool %u., remote seid %d", sc.sampling_frequency, sc.max_bitpool_value, avdtp_subevent_signaling_media_codec_sbc_configuration_get_remote_seid(packet));
353             app_state = A2DP_W2_OPEN_STREAM_WITH_SEID;
354             a2dp_signaling_emit_media_codec_sbc(a2dp_source_context.a2dp_callback, packet, size);
355             break;
356         }
357 
358         case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW:
359             cid = avdtp_subevent_streaming_can_send_media_packet_now_get_avdtp_cid(packet);
360             local_seid = avdtp_subevent_streaming_can_send_media_packet_now_get_avdtp_cid(packet);
361             // log_info("A2DP STREAMING_CAN_SEND_MEDIA_PACKET_NOW cid 0x%02x, local_seid %d", cid, local_seid);
362             a2dp_streaming_emit_can_send_media_packet_now(a2dp_source_context.a2dp_callback, cid, local_seid);
363             break;
364 
365         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
366             avdtp_subevent_streaming_connection_established_get_bd_addr(packet, address);
367             status = avdtp_subevent_streaming_connection_established_get_status(packet);
368             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
369             remote_seid = avdtp_subevent_streaming_connection_established_get_remote_seid(packet);
370             local_seid  = avdtp_subevent_streaming_connection_established_get_local_seid(packet);
371             if (status != 0){
372                 log_info("A2DP streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status);
373                 a2dp_streaming_emit_connection_established(a2dp_source_context.a2dp_callback, cid, address, local_seid, remote_seid, status);
374                 break;
375             }
376             log_info("A2DP streaming connection established --- avdtp_cid 0x%02x, local seid %d, remote seid %d", cid, local_seid, remote_seid);
377             app_state = A2DP_STREAMING_OPENED;
378             a2dp_streaming_emit_connection_established(a2dp_source_context.a2dp_callback, cid, address, local_seid, remote_seid, 0);
379             break;
380 
381         case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND:{
382             avdtp_sep_t sep;
383             sep.seid = avdtp_subevent_signaling_sep_found_get_remote_seid(packet);;
384             sep.in_use = avdtp_subevent_signaling_sep_found_get_in_use(packet);
385             sep.media_type = (avdtp_media_type_t) avdtp_subevent_signaling_sep_found_get_media_type(packet);
386             sep.type = (avdtp_sep_type_t) avdtp_subevent_signaling_sep_found_get_sep_type(packet);
387             log_info("A2DP Found sep: remote seid %u, in_use %d, media type %d, sep type %d (1-SNK), index %d", sep.seid, sep.in_use, sep.media_type, sep.type, num_remote_seps);
388             remote_seps[num_remote_seps++] = sep;
389             break;
390         }
391         case AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE:
392             app_state = A2DP_W2_GET_CAPABILITIES;
393             sc.active_remote_sep_index = 0;
394             break;
395 
396         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
397             // TODO check cid
398             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
399             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
400             log_info("A2DP cmd %s accepted , cid 0x%2x, local seid %d", avdtp_si2str(signal_identifier), cid, avdtp_subevent_signaling_accept_get_local_seid(packet));
401 
402             if (avdtp_subevent_signaling_accept_get_is_initiator(packet) != 1) break;
403 
404             switch (app_state){
405                 case A2DP_W2_GET_CAPABILITIES:
406                     if (sc.active_remote_sep_index < num_remote_seps){
407                         sc.active_remote_sep = &remote_seps[sc.active_remote_sep_index++];
408                         log_info("A2DP get capabilities for remote seid %d", sc.active_remote_sep->seid);
409                         avdtp_source_get_capabilities(cid, sc.active_remote_sep->seid);
410                     }
411                     break;
412                 case A2DP_W2_SET_CONFIGURATION:{
413                     if (!sc.local_stream_endpoint) return;
414                     log_info("A2DP initiate set configuration locally and wait for response ... local seid %d, remote seid %d", avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid);
415                     app_state = A2DP_IDLE;
416                     avdtp_source_set_configuration(cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid, sc.local_stream_endpoint->remote_configuration_bitmap, sc.local_stream_endpoint->remote_configuration);
417                     break;
418                 }
419                 case A2DP_W2_RECONFIGURE_WITH_SEID:
420                     log_info("A2DP reconfigured ... local seid %d, active remote seid %d", avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid);
421                     a2dp_signaling_emit_reconfigured(a2dp_source_context.a2dp_callback, cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), 0);
422                     app_state = A2DP_STREAMING_OPENED;
423                     break;
424                 case A2DP_W2_OPEN_STREAM_WITH_SEID:{
425                     log_info("A2DP open stream ... local seid %d, active remote seid %d", avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid);
426                     app_state = A2DP_W4_OPEN_STREAM_WITH_SEID;
427                     avdtp_source_open_stream(cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid);
428                     break;
429                 }
430                 case A2DP_STREAMING_OPENED:
431                     if (!a2dp_source_context.a2dp_callback) return;
432                     switch (signal_identifier){
433                         case  AVDTP_SI_START:
434                             a2dp_signaling_emit_control_command(a2dp_source_context.a2dp_callback, cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), A2DP_SUBEVENT_STREAM_STARTED);
435                             break;
436                         case AVDTP_SI_SUSPEND:
437                             a2dp_signaling_emit_control_command(a2dp_source_context.a2dp_callback, cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), A2DP_SUBEVENT_STREAM_SUSPENDED);
438                             break;
439                         case AVDTP_SI_ABORT:
440                         case AVDTP_SI_CLOSE:
441                             a2dp_signaling_emit_control_command(a2dp_source_context.a2dp_callback, cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), A2DP_SUBEVENT_STREAM_STOPPED);
442                             break;
443                         default:
444                             break;
445                     }
446                     break;
447                 default:
448                     app_state = A2DP_IDLE;
449                     break;
450             }
451 
452             break;
453         case AVDTP_SUBEVENT_SIGNALING_REJECT:
454         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
455             app_state = A2DP_IDLE;
456             a2dp_signaling_emit_reject_cmd(a2dp_source_context.a2dp_callback, packet, size);
457             break;
458         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:{
459             app_state = A2DP_IDLE;
460             uint8_t event[6];
461             int pos = 0;
462             event[pos++] = HCI_EVENT_A2DP_META;
463             event[pos++] = sizeof(event) - 2;
464             event[pos++] = A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED;
465             little_endian_store_16(event, pos, avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet));
466             pos += 2;
467             event[pos++] = avdtp_subevent_streaming_connection_released_get_local_seid(packet);
468             (*a2dp_source_context.a2dp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
469             break;
470         }
471         case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:{
472             app_state = A2DP_IDLE;
473             uint8_t event[6];
474             int pos = 0;
475             event[pos++] = HCI_EVENT_A2DP_META;
476             event[pos++] = sizeof(event) - 2;
477             event[pos++] = A2DP_SUBEVENT_STREAM_RELEASED;
478             little_endian_store_16(event, pos, avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet));
479             pos += 2;
480             event[pos++] = avdtp_subevent_streaming_connection_released_get_local_seid(packet);
481             (*a2dp_source_context.a2dp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
482             break;
483         }
484         default:
485             app_state = A2DP_IDLE;
486             log_info("not implemented");
487             break;
488     }
489 }
490 void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){
491     if (callback == NULL){
492         log_error("a2dp_source_register_packet_handler called with NULL callback");
493         return;
494     }
495     avdtp_source_register_packet_handler(&packet_handler);
496     a2dp_source_context.a2dp_callback = callback;
497 }
498 
499 void a2dp_source_init(void){
500     avdtp_source_init(&a2dp_source_context);
501 }
502 
503 avdtp_stream_endpoint_t * a2dp_source_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type,
504     uint8_t * codec_capabilities, uint16_t codec_capabilities_len,
505     uint8_t * media_codec_info, uint16_t media_codec_info_len){
506     avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type);
507     if (!local_stream_endpoint){
508         return NULL;
509     }
510     avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
511     avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type,
512         codec_capabilities, codec_capabilities_len);
513 
514     local_stream_endpoint->remote_configuration.media_codec.media_codec_information     = media_codec_info;
515     local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_info_len;
516     sc.local_stream_endpoint = local_stream_endpoint;
517     avdtp_source_register_delay_reporting_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
518     return local_stream_endpoint;
519 }
520 
521 uint8_t a2dp_source_establish_stream(bd_addr_t remote_addr, uint8_t loc_seid, uint16_t * a2dp_cid){
522     sc.local_stream_endpoint = avdtp_stream_endpoint_for_seid(loc_seid, &a2dp_source_context);
523     if (!sc.local_stream_endpoint){
524         log_error(" no local_stream_endpoint for seid %d", loc_seid);
525         return AVDTP_SEID_DOES_NOT_EXIST;
526     }
527     (void)memcpy(sc.remote_addr, remote_addr, 6);
528     return avdtp_source_connect(remote_addr, a2dp_cid);
529 }
530 
531 uint8_t a2dp_source_disconnect(uint16_t a2dp_cid){
532     return avdtp_disconnect(a2dp_cid, &a2dp_source_context);
533 }
534 
535 uint8_t a2dp_source_reconfigure_stream_sampling_frequency(uint16_t a2dp_cid, uint32_t sampling_frequency){
536     // UNUSED(sampling_frequency);
537 
538     log_info("a2dp_source_reconfigure_stream");
539 
540     (void)memcpy(sc.local_stream_endpoint->reconfigure_media_codec_sbc_info,
541                  sc.local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information,
542                  4);
543 
544     // update sampling frequency
545     uint8_t config = sc.local_stream_endpoint->reconfigure_media_codec_sbc_info[0] & 0x0f;
546     switch (sampling_frequency){
547         case 48000:
548             config |= (AVDTP_SBC_48000 << 4);
549             break;
550         case 44100:
551             config |= (AVDTP_SBC_44100 << 4);
552             break;
553         case 32000:
554             config |= (AVDTP_SBC_32000 << 4);
555             break;
556         case 16000:
557             config |= (AVDTP_SBC_16000 << 4);
558             break;
559         default:
560             log_error("Unsupported sampling frequency %u", sampling_frequency);
561             return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
562     }
563     sc.local_stream_endpoint->reconfigure_media_codec_sbc_info[0] = config;
564 
565     avdtp_capabilities_t new_configuration;
566     new_configuration.media_codec.media_type = AVDTP_AUDIO;
567     new_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
568     new_configuration.media_codec.media_codec_information_len = 4;
569     new_configuration.media_codec.media_codec_information = sc.local_stream_endpoint->reconfigure_media_codec_sbc_info;
570 
571     // sttart reconfigure
572     app_state = A2DP_W2_RECONFIGURE_WITH_SEID;
573     return avdtp_source_reconfigure(
574         a2dp_cid,
575         avdtp_stream_endpoint_seid(sc.local_stream_endpoint),
576         sc.active_remote_sep->seid,
577         1 << AVDTP_MEDIA_CODEC,
578         new_configuration
579         );
580 }
581 
582 uint8_t a2dp_source_start_stream(uint16_t a2dp_cid, uint8_t local_seid){
583     return avdtp_start_stream(a2dp_cid, local_seid, &a2dp_source_context);
584 }
585 
586 uint8_t a2dp_source_pause_stream(uint16_t a2dp_cid, uint8_t local_seid){
587     return avdtp_suspend_stream(a2dp_cid, local_seid, &a2dp_source_context);
588 }
589 
590 void a2dp_source_stream_endpoint_request_can_send_now(uint16_t a2dp_cid, uint8_t local_seid){
591     avdtp_source_stream_endpoint_request_can_send_now(a2dp_cid, local_seid);
592 }
593 
594 int a2dp_max_media_payload_size(uint16_t a2dp_cid, uint8_t local_seid){
595     return avdtp_max_media_payload_size(a2dp_cid, local_seid);
596 }
597 
598 int a2dp_source_stream_send_media_payload(uint16_t a2dp_cid, uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){
599     return avdtp_source_stream_send_media_payload(a2dp_cid, local_seid, storage, num_bytes_to_copy, num_frames, marker);
600 }
601