xref: /btstack/src/classic/avdtp_sink.c (revision f9f2075ceac5e9dc08e9abea437e43d733a3a0ea)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD 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 
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "btstack.h"
46 #include "avdtp.h"
47 #include "avdtp_sink.h"
48 #include "avdtp_util.h"
49 #include "avdtp_initiator.h"
50 #include "avdtp_acceptor.h"
51 
52 static const char * default_avdtp_sink_service_name = "BTstack AVDTP Sink Service";
53 static const char * default_avdtp_sink_service_provider_name = "BTstack AVDTP Sink Service Provider";
54 
55 // TODO list of devices
56 static btstack_linked_list_t avdtp_connections;
57 static uint16_t stream_endpoints_id_counter;
58 
59 btstack_linked_list_t stream_endpoints;
60 btstack_packet_handler_t avdtp_sink_callback;
61 
62 
63 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
64 
65 static void (*handle_media_data)(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size);
66 
67 void a2dp_sink_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, SDP_ServiceRecordHandle);
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, SDP_ServiceClassIDList);
77     attribute = de_push_sequence(service);
78     {
79         de_add_number(attribute, DE_UUID, DE_SIZE_16, AUDIO_SINK_GROUP);
80     }
81     de_pop_sequence(service, attribute);
82 
83     // 0x0004 "Protocol Descriptor List"
84     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
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, SDP_L2CAPProtocol);
90             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, 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, PSM_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, SDP_BrowseGroupList); // public browse group
105     attribute = de_push_sequence(service);
106     {
107         de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
108     }
109     de_pop_sequence(service, attribute);
110 
111     // 0x0009 "Bluetooth Profile Descriptor List"
112     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
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, 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_avdtp_sink_service_name), (uint8_t *) default_avdtp_sink_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_avdtp_sink_service_provider_name), (uint8_t *) default_avdtp_sink_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 avdtp_connection_t * avdtp_sink_create_connection(bd_addr_t remote_addr){
148     avdtp_connection_t * connection = btstack_memory_avdtp_connection_get();
149     memset(connection, 0, sizeof(avdtp_connection_t));
150     connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
151     connection->initiator_transaction_label++;
152     memcpy(connection->remote_addr, remote_addr, 6);
153     btstack_linked_list_add(&avdtp_connections, (btstack_linked_item_t *) connection);
154     return connection;
155 }
156 
157 avdtp_stream_endpoint_t * avdtp_sink_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){
158     avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get();
159     memset(stream_endpoint, 0, sizeof(avdtp_stream_endpoint_t));
160     stream_endpoints_id_counter++;
161     stream_endpoint->sep.seid = stream_endpoints_id_counter;
162     stream_endpoint->sep.media_type = media_type;
163     stream_endpoint->sep.type = sep_type;
164     btstack_linked_list_add(&stream_endpoints, (btstack_linked_item_t *) stream_endpoint);
165     return stream_endpoint;
166 }
167 
168 void avdtp_sink_register_media_transport_category(uint8_t seid){
169     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
170     if (!stream_endpoint){
171         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
172         return;
173     }
174     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1);
175     stream_endpoint->sep.registered_service_categories = bitmap;
176     printf("registered services AVDTP_MEDIA_TRANSPORT(%d) %02x\n", AVDTP_MEDIA_TRANSPORT, stream_endpoint->sep.registered_service_categories);
177 }
178 
179 void avdtp_sink_register_reporting_category(uint8_t seid){
180     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
181     if (!stream_endpoint){
182         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
183         return;
184     }
185     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_REPORTING, 1);
186     stream_endpoint->sep.registered_service_categories = bitmap;
187 }
188 
189 void avdtp_sink_register_delay_reporting_category(uint8_t seid){
190     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
191     if (!stream_endpoint){
192         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
193         return;
194     }
195     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1);
196     stream_endpoint->sep.registered_service_categories = bitmap;
197 }
198 
199 void avdtp_sink_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
200     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
201     if (!stream_endpoint){
202         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
203         return;
204     }
205     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1);
206     stream_endpoint->sep.registered_service_categories = bitmap;
207     stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733
208     stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size;
209     stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets;
210 }
211 
212 void avdtp_sink_register_content_protection_category(uint8_t seid, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){
213     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
214     if (!stream_endpoint){
215         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
216         return;
217     }
218     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1);
219     stream_endpoint->sep.registered_service_categories = bitmap;
220     stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type;
221     stream_endpoint->sep.capabilities.content_protection.cp_type_value = cp_type_value;
222     stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = cp_type_value_len;
223 }
224 
225 void avdtp_sink_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){
226     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
227     if (!stream_endpoint){
228         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
229         return;
230     }
231     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1);
232     stream_endpoint->sep.registered_service_categories = bitmap;
233     stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch;
234     stream_endpoint->sep.capabilities.header_compression.media = media;
235     stream_endpoint->sep.capabilities.header_compression.recovery = recovery;
236 }
237 
238 void avdtp_sink_register_media_codec_category(uint8_t seid, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, const uint8_t * media_codec_info, uint16_t media_codec_info_len){
239     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
240     if (!stream_endpoint){
241         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
242         return;
243     }
244     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1);
245     stream_endpoint->sep.registered_service_categories = bitmap;
246     printf("registered services AVDTP_MEDIA_CODEC(%d) %02x\n", AVDTP_MEDIA_CODEC, stream_endpoint->sep.registered_service_categories);
247     stream_endpoint->sep.capabilities.media_codec.media_type = media_type;
248     stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type;
249     stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info;
250     stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len;
251 }
252 
253 void avdtp_sink_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){
254     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(seid);
255     if (!stream_endpoint){
256         log_error("avdtp_sink_register_media_transport_category: stream endpoint with seid %d is not registered", seid);
257         return;
258     }
259     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1);
260     stream_endpoint->sep.registered_service_categories = bitmap;
261     stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation;
262 }
263 
264 // // media, reporting. recovery
265 // void avdtp_sink_register_media_transport_identifier_for_multiplexing_category(uint8_t seid, uint8_t fragmentation){
266 
267 // }
268 
269 
270 static avdtp_connection_t * get_avdtp_connection_for_bd_addr(bd_addr_t addr){
271     btstack_linked_list_iterator_t it;
272     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &avdtp_connections);
273     while (btstack_linked_list_iterator_has_next(&it)){
274         avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
275         if (memcmp(addr, connection->remote_addr, 6) != 0) continue;
276         return connection;
277     }
278     return NULL;
279 }
280 
281 static avdtp_connection_t * get_avdtp_connection_for_con_handle(hci_con_handle_t con_handle){
282     btstack_linked_list_iterator_t it;
283     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &avdtp_connections);
284     while (btstack_linked_list_iterator_has_next(&it)){
285         avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
286         if (connection->con_handle != con_handle) continue;
287         return connection;
288     }
289     return NULL;
290 }
291 
292 static avdtp_connection_t * get_avdtp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid){
293     btstack_linked_list_iterator_t it;
294     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &avdtp_connections);
295     while (btstack_linked_list_iterator_has_next(&it)){
296         avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
297         if (connection->l2cap_signaling_cid != l2cap_cid) continue;
298         return connection;
299     }
300     return NULL;
301 }
302 
303 static avdtp_stream_endpoint_t * get_avdtp_stream_endpoint_for_l2cap_cid(uint16_t l2cap_cid){
304     btstack_linked_list_iterator_t it;
305     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints);
306     while (btstack_linked_list_iterator_has_next(&it)){
307         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
308         if (stream_endpoint->l2cap_media_cid == l2cap_cid){
309             return stream_endpoint;
310         }
311         if (stream_endpoint->l2cap_reporting_cid == l2cap_cid){
312             return stream_endpoint;
313         }
314         if (stream_endpoint->l2cap_recovery_cid == l2cap_cid){
315             return stream_endpoint;
316         }
317     }
318     return NULL;
319 }
320 
321 /* START: tracking can send now requests pro l2cap cid */
322 static void avdtp_sink_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid){
323     if (connection->wait_to_send_acceptor){
324         connection->wait_to_send_acceptor = 0;
325         avdtp_acceptor_stream_config_subsm_run(connection);
326     } else if (connection->wait_to_send_initiator){
327         connection->wait_to_send_initiator = 0;
328         avdtp_initiator_stream_config_subsm_run(connection);
329     } else if (connection->wait_to_send_self){
330         connection->wait_to_send_self = 0;
331         if (connection->disconnect){
332             btstack_linked_list_iterator_t it;
333             btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints);
334             while (btstack_linked_list_iterator_has_next(&it)){
335                 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
336                 if (stream_endpoint->connection == connection){
337                     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED && stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED){
338                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED;
339                         avdtp_sink_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
340                         l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0);
341                         return;
342                     }
343                 }
344             }
345             connection->disconnect = 0;
346             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED;
347             l2cap_disconnect(connection->l2cap_signaling_cid, 0);
348             return;
349         }
350     }
351 
352     // re-register
353     int more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator || connection->wait_to_send_self;
354     if (more_to_send){
355         l2cap_request_can_send_now_event(l2cap_cid);
356     }
357 }
358 
359 
360 /* END: tracking can send now requests pro l2cap cid */
361 // TODO remove
362 static void handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size){
363     int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
364     switch (connection->signaling_packet.message_type){
365         case AVDTP_CMD_MSG:
366             avdtp_acceptor_stream_config_subsm(connection, packet, size, offset);
367             break;
368         default:
369             avdtp_initiator_stream_config_subsm(connection, packet, size, offset);
370             break;
371     }
372 }
373 
374 static void stream_endpoint_state_machine(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint, uint8_t packet_type, uint8_t event, uint8_t *packet, uint16_t size){
375     uint16_t local_cid;
376     switch (packet_type){
377         case L2CAP_DATA_PACKET:{
378             int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
379             if (connection->signaling_packet.message_type == AVDTP_CMD_MSG){
380                 avdtp_acceptor_stream_config_subsm(connection, packet, size, offset);
381             } else {
382                 avdtp_initiator_stream_config_subsm(connection, packet, size, offset);
383             }
384             break;
385         }
386         case HCI_EVENT_PACKET:
387             switch (event){
388                 case L2CAP_EVENT_CHANNEL_OPENED:
389                     if (stream_endpoint->l2cap_media_cid == 0){
390                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) return;
391                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
392                         stream_endpoint->connection = connection;
393                         stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);;
394                         printf(" -> AVDTP_STREAM_ENDPOINT_OPENED, stream endpoint %p, connection %p\n", stream_endpoint, connection);
395                         break;
396                     }
397                     break;
398                 case L2CAP_EVENT_CHANNEL_CLOSED:
399                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
400                     if (stream_endpoint->l2cap_media_cid == local_cid){
401                         stream_endpoint->l2cap_media_cid = 0;
402                         printf(" -> L2CAP_EVENT_CHANNEL_CLOSED: AVDTP_STREAM_ENDPOINT_IDLE\n");
403                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
404                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
405                         stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
406                         stream_endpoint->remote_seps_num = 0;
407                         memset(stream_endpoint->remote_seps, 0, sizeof(avdtp_sep_t)*MAX_NUM_SEPS);
408                         stream_endpoint->remote_sep_index = 0;
409                         break;
410                     }
411                     if (stream_endpoint->l2cap_recovery_cid == local_cid){
412                         log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
413                         stream_endpoint->l2cap_recovery_cid = 0;
414                         break;
415                     }
416 
417                     if (stream_endpoint->l2cap_reporting_cid == local_cid){
418                         log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
419                         stream_endpoint->l2cap_reporting_cid = 0;
420                         break;
421                     }
422                     break;
423                 default:
424                     break;
425             }
426             break;
427         default:
428             break;
429     }
430 }
431 
432 static void avdtp_initialize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){
433     stream_endpoint->connection = NULL;
434     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
435     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
436     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
437     stream_endpoint->remote_sep_index = 0;
438     stream_endpoint->media_disconnect = 0;
439     stream_endpoint->remote_seps_num = 0;
440     stream_endpoint->sep.in_use = 0;
441     memset(stream_endpoint->remote_seps, 0, sizeof(stream_endpoint->remote_seps));
442     stream_endpoint->remote_sep_index = 0;
443 }
444 
445 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
446     bd_addr_t event_addr;
447     hci_con_handle_t con_handle;
448     uint16_t psm;
449     uint16_t local_cid;
450     avdtp_stream_endpoint_t * stream_endpoint = NULL;
451     avdtp_connection_t * connection = NULL;
452 
453     switch (packet_type) {
454         case L2CAP_DATA_PACKET:
455             connection = get_avdtp_connection_for_l2cap_signaling_cid(channel);
456             if (connection){
457                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
458                 break;
459             }
460 
461             stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(channel);
462             if (!stream_endpoint){
463                 if (!connection) break;
464                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
465                 break;
466             }
467 
468             if (channel == stream_endpoint->connection->l2cap_signaling_cid){
469                 stream_endpoint_state_machine(stream_endpoint->connection, stream_endpoint, L2CAP_DATA_PACKET, 0, packet, size);
470                 break;
471             }
472 
473             if (channel == stream_endpoint->l2cap_media_cid){
474                 (*handle_media_data)(stream_endpoint, packet, size);
475                 break;
476             }
477 
478             if (channel == stream_endpoint->l2cap_reporting_cid){
479                 // TODO
480                 printf("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED\n");
481             } else if (channel == stream_endpoint->l2cap_recovery_cid){
482                 // TODO
483                 printf("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED\n");
484             } else {
485                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
486             }
487             break;
488 
489         case HCI_EVENT_PACKET:
490             switch (hci_event_packet_get_type(packet)) {
491                 case L2CAP_EVENT_INCOMING_CONNECTION:
492                     l2cap_event_incoming_connection_get_address(packet, event_addr);
493                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
494 
495                     connection = get_avdtp_connection_for_bd_addr(event_addr);
496                     if (!connection){
497                         connection = avdtp_sink_create_connection(event_addr);
498                         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
499                         l2cap_accept_connection(local_cid);
500                         break;
501                     }
502 
503                     stream_endpoint = get_avdtp_stream_endpoint_for_seid(connection->query_seid);
504                     if (!stream_endpoint) {
505                         printf("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d\n", connection->query_seid);
506                         break;
507                     }
508 
509                     if (stream_endpoint->l2cap_media_cid == 0){
510                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) break;
511                         l2cap_accept_connection(local_cid);
512                         break;
513                     }
514                     break;
515 
516                 case L2CAP_EVENT_CHANNEL_OPENED:
517                     // inform about new l2cap connection
518                     l2cap_event_channel_opened_get_address(packet, event_addr);
519 
520                     if (l2cap_event_channel_opened_get_status(packet)){
521                         log_error("L2CAP connection to connection %s failed. status code 0x%02x",
522                             bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet));
523                         break;
524                     }
525                     psm = l2cap_event_channel_opened_get_psm(packet);
526                     if (psm != PSM_AVDTP){
527                         log_error("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED");
528                         return;
529                     }
530 
531                     con_handle = l2cap_event_channel_opened_get_handle(packet);
532                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
533 
534                     // printf("L2CAP_EVENT_CHANNEL_OPENED: Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
535                     //        bd_addr_to_str(event_addr), con_handle, psm, local_cid,  l2cap_event_channel_opened_get_remote_cid(packet));
536 
537                     if (psm != PSM_AVDTP) break;
538 
539                     connection = get_avdtp_connection_for_bd_addr(event_addr);
540                     if (!connection) break;
541 
542                     if (connection->l2cap_signaling_cid == 0) {
543                         if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) break;
544                         connection->l2cap_signaling_cid = local_cid;
545                         connection->con_handle = con_handle;
546                         connection->query_seid = 0;
547                         connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
548                         printf(" -> AVDTP_SIGNALING_CONNECTION_OPENED, connection %p\n", connection);
549                         avdtp_signaling_emit_connection_established(avdtp_sink_callback, con_handle, event_addr, 0);
550                         break;
551                     }
552 
553                     stream_endpoint = get_avdtp_stream_endpoint_for_seid(connection->query_seid);
554                     if (!stream_endpoint){
555                         printf("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found");
556                         return;
557                     }
558                     stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_OPENED, packet, size);
559                     break;
560 
561                 case L2CAP_EVENT_CHANNEL_CLOSED:
562                     // data: event (8), len(8), channel (16)
563                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
564                     connection = get_avdtp_connection_for_l2cap_signaling_cid(local_cid);
565                     printf(" -> L2CAP_EVENT_CHANNEL_CLOSED signaling cid 0x%0x\n", local_cid);
566 
567                     if (connection){
568                         printf(" -> AVDTP_STREAM_ENDPOINT_IDLE, connection closed\n");
569                         btstack_linked_list_remove(&avdtp_connections, (btstack_linked_item_t*) connection);
570                         btstack_linked_list_iterator_t it;
571                         btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints);
572                         while (btstack_linked_list_iterator_has_next(&it)){
573                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
574                             avdtp_initialize_stream_endpoint(_stream_endpoint);
575                         }
576                         break;
577                     }
578 
579                     stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(local_cid);
580                     if (!stream_endpoint) return;
581 
582                     stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_CLOSED, packet, size);
583                     break;
584 
585                 case HCI_EVENT_DISCONNECTION_COMPLETE:
586                     break;
587 
588                 case L2CAP_EVENT_CAN_SEND_NOW:
589                     connection = get_avdtp_connection_for_l2cap_signaling_cid(channel);
590                     if (!connection) {
591                         stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(channel);
592                         if (!stream_endpoint->connection) break;
593                         connection = stream_endpoint->connection;
594                     }
595                     avdtp_sink_handle_can_send_now(connection, channel);
596                     break;
597                 default:
598                     printf("unknown HCI event type %02x\n", hci_event_packet_get_type(packet));
599                     break;
600             }
601             break;
602 
603         default:
604             // other packet type
605             break;
606     }
607 }
608 
609 // TODO: find out which security level is needed, and replace LEVEL_0 in avdtp_sink_init
610 void avdtp_sink_init(void){
611     stream_endpoints = NULL;
612     avdtp_connections = NULL;
613     stream_endpoints_id_counter = 0;
614     l2cap_register_service(&packet_handler, PSM_AVDTP, 0xffff, LEVEL_0);
615 }
616 
617 void avdtp_sink_register_media_handler(void (*callback)(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size)){
618     if (callback == NULL){
619         log_error("avdtp_sink_register_media_handler called with NULL callback");
620         return;
621     }
622     handle_media_data = callback;
623 }
624 
625 void avdtp_sink_register_packet_handler(btstack_packet_handler_t callback){
626     if (callback == NULL){
627         log_error("avdtp_sink_register_packet_handler called with NULL callback");
628         return;
629     }
630     avdtp_sink_callback = callback;
631 }
632 
633 void avdtp_sink_connect(bd_addr_t bd_addr){
634     avdtp_connection_t * connection = get_avdtp_connection_for_bd_addr(bd_addr);
635     if (!connection){
636         connection = avdtp_sink_create_connection(bd_addr);
637     }
638     if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return;
639     connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
640     l2cap_create_channel(packet_handler, connection->remote_addr, PSM_AVDTP, 0xffff, NULL);
641 }
642 
643 void avdtp_sink_disconnect(uint16_t con_handle){
644     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
645     if (!connection) return;
646     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE) return;
647     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return;
648 
649     connection->disconnect = 1;
650     avdtp_sink_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
651 }
652 
653 void avdtp_sink_open_stream(uint16_t con_handle, uint8_t acp_seid){
654     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
655     if (!connection){
656         printf("avdtp_sink_media_connect: no connection for handle 0x%02x found\n", con_handle);
657         return;
658     }
659 
660     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
661         printf("avdtp_sink_media_connect: wrong connection state %d\n", connection->state);
662         return;
663     }
664 
665     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_associated_with_acp_seid(acp_seid);
666     if (!stream_endpoint) {
667         printf("avdtp_sink_media_connect: no stream_endpoint with acp seid %d found\n", acp_seid);
668         return;
669     }
670 
671     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return;
672     if (stream_endpoint->remote_sep_index == 0xFF) return;
673 
674     printf(" AVDTP_INITIATOR_W2_MEDIA_CONNECT \n");
675     connection->initiator_transaction_label++;
676     connection->acp_seid = acp_seid;
677     connection->int_seid = stream_endpoint->sep.seid;
678     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_MEDIA_CONNECT;
679     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
680 }
681 
682 void avdtp_sink_start_stream(uint16_t con_handle, uint8_t acp_seid){
683     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
684     if (!connection){
685         printf("avdtp_sink_media_connect: no connection for handle 0x%02x found\n", con_handle);
686         return;
687     }
688 
689     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
690         printf("avdtp_sink_media_connect: wrong connection state %d\n", connection->state);
691         return;
692     }
693 
694     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_associated_with_acp_seid(acp_seid);
695     if (!stream_endpoint) {
696         printf("avdtp_sink_media_connect: no stream_endpoint with acp_seid %d found\n", acp_seid);
697         return;
698     }
699     if (stream_endpoint->remote_sep_index == 0xFF) return;
700     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_OPENED) return;
701     printf(" AVDTP_INITIATOR_W2_STREAMING_START \n");
702     connection->initiator_transaction_label++;
703     connection->acp_seid = acp_seid;
704     connection->int_seid = stream_endpoint->sep.seid;
705     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_STREAMING_START;
706     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
707 }
708 
709 void avdtp_sink_stop_stream(uint16_t con_handle, uint8_t acp_seid){
710     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
711     if (!connection){
712         printf("avdtp_sink_stop_stream: no connection for handle 0x%02x found\n", con_handle);
713         return;
714     }
715     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
716         printf("avdtp_sink_stop_stream: wrong connection state %d\n", connection->state);
717         return;
718     }
719 
720     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_associated_with_acp_seid(acp_seid);
721     if (!stream_endpoint) {
722         printf("avdtp_sink_stop_stream: no stream_endpoint with acp seid %d found\n", acp_seid);
723         return;
724     }
725     if (stream_endpoint->remote_sep_index == 0xFF) return;
726     switch (stream_endpoint->state){
727         case AVDTP_STREAM_ENDPOINT_OPENED:
728         case AVDTP_STREAM_ENDPOINT_STREAMING:
729             printf(" AVDTP_INITIATOR_W2_STREAMING_STOP \n");
730             connection->initiator_transaction_label++;
731             connection->acp_seid = acp_seid;
732             connection->int_seid = stream_endpoint->sep.seid;
733             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_STREAMING_STOP;
734             avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
735             break;
736         default:
737             break;
738     }
739 }
740 
741 void avdtp_sink_abort_stream(uint16_t con_handle, uint8_t acp_seid){
742     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
743     if (!connection){
744         printf("avdtp_sink_abort_stream: no connection for handle 0x%02x found\n", con_handle);
745         return;
746     }
747 
748     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
749         printf("avdtp_sink_abort_stream: wrong connection state %d\n", connection->state);
750         return;
751     }
752 
753     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_associated_with_acp_seid(acp_seid);
754     if (!stream_endpoint) {
755         printf("avdtp_sink_abort_stream: no stream_endpoint for seid %d found\n", acp_seid);
756         return;
757     }
758     if (stream_endpoint->remote_sep_index == 0xFF) return;
759     switch (stream_endpoint->state){
760         case AVDTP_STREAM_ENDPOINT_CONFIGURED:
761         case AVDTP_STREAM_ENDPOINT_CLOSING:
762         case AVDTP_STREAM_ENDPOINT_OPENED:
763         case AVDTP_STREAM_ENDPOINT_STREAMING:
764             printf(" AVDTP_INITIATOR_W2_STREAMING_ABORT \n");
765             connection->initiator_transaction_label++;
766             connection->acp_seid = acp_seid;
767             connection->int_seid = stream_endpoint->sep.seid;
768             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_STREAMING_ABORT;
769             avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
770             break;
771         default:
772             break;
773     }
774 }
775 
776 void avdtp_sink_discover_stream_endpoints(uint16_t con_handle){
777     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
778     if (!connection){
779         printf("avdtp_sink_discover_stream_endpoints: no connection for handle 0x%02x found\n", con_handle);
780         return;
781     }
782     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
783 
784     switch (connection->initiator_connection_state){
785         case AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE:
786             connection->initiator_transaction_label++;
787             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
788             avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
789             break;
790         default:
791             printf("avdtp_sink_discover_stream_endpoints: wrong state\n");
792             break;
793     }
794 }
795 
796 
797 void avdtp_sink_get_capabilities(uint16_t con_handle, uint8_t acp_seid){
798     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
799     if (!connection){
800         printf("avdtp_sink_get_capabilities: no connection for handle 0x%02x found\n", con_handle);
801         return;
802     }
803     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
804     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
805     connection->initiator_transaction_label++;
806     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
807     connection->acp_seid = acp_seid;
808     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
809 }
810 
811 
812 void avdtp_sink_get_all_capabilities(uint16_t con_handle, uint8_t acp_seid){
813     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
814     if (!connection){
815         printf("avdtp_sink_get_all_capabilities: no connection for handle 0x%02x found\n", con_handle);
816         return;
817     }
818     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
819     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
820     connection->initiator_transaction_label++;
821     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
822     connection->acp_seid = acp_seid;
823     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
824 }
825 
826 void avdtp_sink_get_configuration(uint16_t con_handle, uint8_t acp_seid){
827     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
828     if (!connection){
829         printf("avdtp_sink_get_configuration: no connection for handle 0x%02x found\n", con_handle);
830         return;
831     }
832     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
833     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
834     connection->initiator_transaction_label++;
835     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
836     connection->acp_seid = acp_seid;
837     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
838 }
839 
840 void avdtp_sink_set_configuration(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
841     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
842     if (!connection){
843         printf("avdtp_sink_set_configuration: no connection for handle 0x%02x found\n", con_handle);
844         return;
845     }
846     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
847     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
848 
849     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_for_seid(int_seid);
850     if (!stream_endpoint) {
851         printf("avdtp_sink_set_configuration: no initiator stream endpoint for seid %d\n", int_seid);
852         return;
853     }
854     printf("avdtp_sink_set_configuration int seid %d, acp seid %d\n", int_seid, acp_seid);
855 
856     connection->initiator_transaction_label++;
857     connection->acp_seid = acp_seid;
858     connection->int_seid = int_seid;
859     connection->remote_capabilities_bitmap = configured_services_bitmap;
860     connection->remote_capabilities = configuration;
861     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
862     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
863 }
864 
865 void avdtp_sink_reconfigure(uint16_t con_handle, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
866     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
867     if (!connection){
868         printf("avdtp_sink_reconfigure: no connection for handle 0x%02x found\n", con_handle);
869         return;
870     }
871     //TODO: if opened only app capabilities, enable reconfigure for not opened
872     if (connection->state < AVDTP_SIGNALING_CONNECTION_OPENED) return;
873     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
874     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_associated_with_acp_seid(acp_seid);
875     if (!stream_endpoint) return;
876     if (stream_endpoint->remote_sep_index == 0xFF) return;
877     connection->initiator_transaction_label++;
878     connection->acp_seid = acp_seid;
879     connection->int_seid = stream_endpoint->sep.seid;
880     connection->remote_capabilities_bitmap = configured_services_bitmap;
881     connection->remote_capabilities = configuration;
882     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
883     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
884 }
885 
886 void avdtp_sink_suspend(uint16_t con_handle, uint8_t acp_seid){
887     avdtp_connection_t * connection = get_avdtp_connection_for_con_handle(con_handle);
888     if (!connection){
889         printf("avdtp_sink_suspend: no connection for handle 0x%02x found\n", con_handle);
890         return;
891     }
892     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
893     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
894     avdtp_stream_endpoint_t * stream_endpoint = get_avdtp_stream_endpoint_associated_with_acp_seid(acp_seid);
895     if (!stream_endpoint) return;
896     if (stream_endpoint->remote_sep_index == 0xFF) return;
897     connection->initiator_transaction_label++;
898     connection->acp_seid = acp_seid;
899     connection->int_seid = stream_endpoint->sep.seid;
900     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SUSPEND_STREAM_WITH_SEID;
901     avdtp_sink_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
902 }