xref: /btstack/src/classic/avdtp.c (revision ca93b47d3c0a65b91d7a5dc8586f4c82a3b3e1c2)
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 #define __BTSTACK_FILE__ "avdtp.c"
39 
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "btstack.h"
48 #include "avdtp.h"
49 #include "avdtp_util.h"
50 #include "avdtp_acceptor.h"
51 #include "avdtp_initiator.h"
52 
53 static uint8_t audio_samples_storage[44100*4]; // 1s buffer
54 // static btstack_ring_buffer_t audio_ring_buffer;
55 
56 static uint8_t sbc_samples_storage[44100*4];
57 // static btstack_ring_buffer_t sbc_ring_buffer;
58 
59 static void (*handle_media_data)(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size);
60 
61 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint){
62     if (!stream_endpoint){
63         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
64         return;
65     }
66     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1);
67     stream_endpoint->sep.registered_service_categories = bitmap;
68     printf("registered services AVDTP_MEDIA_TRANSPORT(%d) %02x\n", AVDTP_MEDIA_TRANSPORT, stream_endpoint->sep.registered_service_categories);
69 }
70 
71 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
72     if (!stream_endpoint){
73         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
74         return;
75     }
76     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_REPORTING, 1);
77     stream_endpoint->sep.registered_service_categories = bitmap;
78 }
79 
80 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
81     if (!stream_endpoint){
82         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
83         return;
84     }
85     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1);
86     stream_endpoint->sep.registered_service_categories = bitmap;
87 }
88 
89 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
90     if (!stream_endpoint){
91         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
92         return;
93     }
94     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1);
95     stream_endpoint->sep.registered_service_categories = bitmap;
96     stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733
97     stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size;
98     stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets;
99 }
100 
101 void avdtp_register_content_protection_category(avdtp_stream_endpoint_t * stream_endpoint, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){
102     if (!stream_endpoint){
103         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
104         return;
105     }
106     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1);
107     stream_endpoint->sep.registered_service_categories = bitmap;
108     stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type;
109     stream_endpoint->sep.capabilities.content_protection.cp_type_value = cp_type_value;
110     stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = cp_type_value_len;
111 }
112 
113 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){
114     if (!stream_endpoint){
115         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
116         return;
117     }
118     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1);
119     stream_endpoint->sep.registered_service_categories = bitmap;
120     stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch;
121     stream_endpoint->sep.capabilities.header_compression.media = media;
122     stream_endpoint->sep.capabilities.header_compression.recovery = recovery;
123 }
124 
125 void avdtp_register_media_codec_category(avdtp_stream_endpoint_t * stream_endpoint, 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){
126     if (!stream_endpoint){
127         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
128         return;
129     }
130     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1);
131     stream_endpoint->sep.registered_service_categories = bitmap;
132     printf("registered services AVDTP_MEDIA_CODEC(%d) %02x\n", AVDTP_MEDIA_CODEC, stream_endpoint->sep.registered_service_categories);
133     stream_endpoint->sep.capabilities.media_codec.media_type = media_type;
134     stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type;
135     stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info;
136     stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len;
137 }
138 
139 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){
140     if (!stream_endpoint){
141         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
142         return;
143     }
144     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1);
145     stream_endpoint->sep.registered_service_categories = bitmap;
146     stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation;
147 }
148 
149 
150 /* START: tracking can send now requests pro l2cap cid */
151 void avdtp_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid, avdtp_context_t * context){
152     if (connection->wait_to_send_acceptor){
153         connection->wait_to_send_acceptor = 0;
154         avdtp_acceptor_stream_config_subsm_run(connection, context);
155     } else if (connection->wait_to_send_initiator){
156         connection->wait_to_send_initiator = 0;
157         avdtp_initiator_stream_config_subsm_run(connection, context);
158     } else if (connection->wait_to_send_self){
159         connection->wait_to_send_self = 0;
160         if (connection->disconnect){
161             btstack_linked_list_iterator_t it;
162             btstack_linked_list_iterator_init(&it, &context->stream_endpoints);
163             while (btstack_linked_list_iterator_has_next(&it)){
164                 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
165                 if (stream_endpoint->connection == connection){
166                     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED && stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED){
167                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED;
168                         avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
169                         l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0);
170                         return;
171                     }
172                 }
173             }
174             connection->disconnect = 0;
175             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED;
176             l2cap_disconnect(connection->l2cap_signaling_cid, 0);
177             return;
178         }
179     }
180 
181     // re-register
182     int more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator || connection->wait_to_send_self;
183     if (more_to_send){
184         l2cap_request_can_send_now_event(l2cap_cid);
185     }
186 }
187 /* END: tracking can send now requests pro l2cap cid */
188 
189 avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, avdtp_context_t * context){
190     avdtp_connection_t * connection = btstack_memory_avdtp_connection_get();
191     memset(connection, 0, sizeof(avdtp_connection_t));
192     connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
193     connection->initiator_transaction_label++;
194     memcpy(connection->remote_addr, remote_addr, 6);
195     btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection);
196     return connection;
197 }
198 
199 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type, avdtp_context_t * context){
200     avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get();
201     memset(stream_endpoint, 0, sizeof(avdtp_stream_endpoint_t));
202     context->stream_endpoints_id_counter++;
203     stream_endpoint->sep.seid = context->stream_endpoints_id_counter;
204     stream_endpoint->sep.media_type = media_type;
205     stream_endpoint->sep.type = sep_type;
206 
207     memset(audio_samples_storage, 0, sizeof(audio_samples_storage));
208     btstack_ring_buffer_init(&stream_endpoint->audio_ring_buffer, audio_samples_storage, sizeof(audio_samples_storage));
209 
210     memset(sbc_samples_storage, 0, sizeof(sbc_samples_storage));
211     btstack_ring_buffer_init(&stream_endpoint->sbc_ring_buffer, sbc_samples_storage, sizeof(sbc_samples_storage));
212 
213 
214     btstack_linked_list_add(&context->stream_endpoints, (btstack_linked_item_t *) stream_endpoint);
215     return stream_endpoint;
216 }
217 
218 
219 static void handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size, avdtp_context_t * context){
220     int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
221     switch (connection->signaling_packet.message_type){
222         case AVDTP_CMD_MSG:
223             avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context);
224             break;
225         default:
226             avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context);
227             break;
228     }
229 }
230 
231 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, avdtp_context_t * context){
232     uint16_t local_cid;
233     switch (packet_type){
234         case L2CAP_DATA_PACKET:{
235             int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
236             if (connection->signaling_packet.message_type == AVDTP_CMD_MSG){
237                 avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context);
238             } else {
239                 avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context);
240             }
241             break;
242         }
243         case HCI_EVENT_PACKET:
244             switch (event){
245                 case L2CAP_EVENT_CHANNEL_OPENED:
246                     if (stream_endpoint->l2cap_media_cid == 0){
247                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) return;
248                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
249                         stream_endpoint->connection = connection;
250                         stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
251                         printf(" -> AVDTP_STREAM_ENDPOINT_OPENED, stream endpoint %p, connection %p\n", stream_endpoint, connection);
252                         avdtp_streaming_emit_connection_established(context->avdtp_callback, stream_endpoint->l2cap_media_cid, 0);
253                         break;
254                     }
255                     break;
256                 case L2CAP_EVENT_CHANNEL_CLOSED:
257                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
258                     if (stream_endpoint->l2cap_media_cid == local_cid){
259                         stream_endpoint->l2cap_media_cid = 0;
260                         printf(" -> L2CAP_EVENT_CHANNEL_CLOSED: AVDTP_STREAM_ENDPOINT_IDLE\n");
261                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
262                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
263                         stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
264                         stream_endpoint->remote_seps_num = 0;
265                         memset(stream_endpoint->remote_seps, 0, sizeof(avdtp_sep_t)*MAX_NUM_SEPS);
266                         stream_endpoint->remote_sep_index = 0;
267                         break;
268                     }
269                     if (stream_endpoint->l2cap_recovery_cid == local_cid){
270                         log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
271                         stream_endpoint->l2cap_recovery_cid = 0;
272                         break;
273                     }
274 
275                     if (stream_endpoint->l2cap_reporting_cid == local_cid){
276                         log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
277                         stream_endpoint->l2cap_reporting_cid = 0;
278                         break;
279                     }
280                     break;
281                 default:
282                     break;
283             }
284             break;
285         default:
286             break;
287     }
288 }
289 
290 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avdtp_context_t * context){
291     bd_addr_t event_addr;
292     hci_con_handle_t con_handle;
293     uint16_t psm;
294     uint16_t local_cid;
295     avdtp_stream_endpoint_t * stream_endpoint = NULL;
296     avdtp_connection_t * connection = NULL;
297     btstack_linked_list_t * avdtp_connections = &context->connections;
298     btstack_linked_list_t * stream_endpoints =  &context->stream_endpoints;
299     handle_media_data = context->handle_media_data;
300     // printf("avdtp_packet_handler packet type %02x, event %02x \n", packet_type, hci_event_packet_get_type(packet));
301     switch (packet_type) {
302         case L2CAP_DATA_PACKET:
303             connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
304             if (connection){
305                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
306                 break;
307             }
308 
309             stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
310             if (!stream_endpoint){
311                 if (!connection) break;
312                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
313                 break;
314             }
315 
316             if (channel == stream_endpoint->connection->l2cap_signaling_cid){
317                 stream_endpoint_state_machine(stream_endpoint->connection, stream_endpoint, L2CAP_DATA_PACKET, 0, packet, size, context);
318                 break;
319             }
320 
321             if (channel == stream_endpoint->l2cap_media_cid){
322                 (*handle_media_data)(stream_endpoint, packet, size);
323                 break;
324             }
325 
326             if (channel == stream_endpoint->l2cap_reporting_cid){
327                 // TODO
328                 printf("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED\n");
329             } else if (channel == stream_endpoint->l2cap_recovery_cid){
330                 // TODO
331                 printf("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED\n");
332             } else {
333                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
334             }
335             break;
336 
337         case HCI_EVENT_PACKET:
338             switch (hci_event_packet_get_type(packet)) {
339                 case L2CAP_EVENT_INCOMING_CONNECTION:
340                     l2cap_event_incoming_connection_get_address(packet, event_addr);
341                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
342 
343                     connection = avdtp_connection_for_bd_addr(event_addr, context);
344                     if (!connection){
345                         connection = avdtp_create_connection(event_addr, context);
346                         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
347                         l2cap_accept_connection(local_cid);
348                         break;
349                     }
350 
351                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->query_seid, context);
352                     if (!stream_endpoint) {
353                         printf("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d\n", connection->query_seid);
354                         break;
355                     }
356 
357                     if (stream_endpoint->l2cap_media_cid == 0){
358                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) break;
359                         l2cap_accept_connection(local_cid);
360                         break;
361                     }
362                     break;
363 
364                 case L2CAP_EVENT_CHANNEL_OPENED:
365                     // inform about new l2cap connection
366                     l2cap_event_channel_opened_get_address(packet, event_addr);
367                     if (l2cap_event_channel_opened_get_status(packet)){
368                         log_error("L2CAP connection to connection %s failed. status code 0x%02x",
369                             bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet));
370                         break;
371                     }
372                     psm = l2cap_event_channel_opened_get_psm(packet);
373                     if (psm != BLUETOOTH_PROTOCOL_AVDTP){
374                         log_error("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED");
375                         return;
376                     }
377 
378                     con_handle = l2cap_event_channel_opened_get_handle(packet);
379                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
380 
381                     // printf("L2CAP_EVENT_CHANNEL_OPENED: Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
382                     //        bd_addr_to_str(event_addr), con_handle, psm, local_cid,  l2cap_event_channel_opened_get_remote_cid(packet));
383 
384                     if (psm != BLUETOOTH_PROTOCOL_AVDTP) break;
385 
386                     connection = avdtp_connection_for_bd_addr(event_addr, context);
387                     if (!connection) break;
388 
389                     if (connection->l2cap_signaling_cid == 0) {
390                         if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) break;
391                         connection->l2cap_signaling_cid = local_cid;
392                         connection->con_handle = con_handle;
393                         connection->query_seid = 0;
394                         connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
395                         printf(" -> AVDTP_SIGNALING_CONNECTION_OPENED, connection %p\n", connection);
396                         avdtp_signaling_emit_connection_established(context->avdtp_callback, con_handle, event_addr, 0);
397                         break;
398                     }
399 
400                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->query_seid, context);
401                     if (!stream_endpoint){
402                         printf("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found");
403                         return;
404                     }
405                     stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_OPENED, packet, size, context);
406                     break;
407 
408                 case L2CAP_EVENT_CHANNEL_CLOSED:
409                     // data: event (8), len(8), channel (16)
410                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
411                     connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context);
412                     printf(" -> L2CAP_EVENT_CHANNEL_CLOSED signaling cid 0x%0x\n", local_cid);
413 
414                     if (connection){
415                         printf(" -> AVDTP_STREAM_ENDPOINT_IDLE, connection closed\n");
416                         btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection);
417                         btstack_linked_list_iterator_t it;
418                         btstack_linked_list_iterator_init(&it, stream_endpoints);
419                         while (btstack_linked_list_iterator_has_next(&it)){
420                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
421                             avdtp_initialize_stream_endpoint(_stream_endpoint);
422                         }
423                         break;
424                     }
425 
426                     stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context);
427                     if (!stream_endpoint) return;
428 
429                     stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_CLOSED, packet, size, context);
430                     break;
431 
432                 case HCI_EVENT_DISCONNECTION_COMPLETE:
433                     break;
434 
435                 case L2CAP_EVENT_CAN_SEND_NOW:
436                     connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
437                     if (!connection) {
438                         stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
439                         if (!stream_endpoint->connection) break;
440                         if (stream_endpoint->state == AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND){
441                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
442                             //send sbc
443                             uint8_t  rtp_version = 2;
444                             uint8_t  padding = 0;
445                             uint8_t  extension = 0;
446                             uint8_t  csrc_count = 0;
447                             uint8_t  marker = 0;
448                             uint8_t  payload_type = 0x60;
449                             uint16_t sequence_number = stream_endpoint->sequence_number;
450                             uint32_t timestamp = btstack_run_loop_get_time_ms();
451                             uint32_t ssrc = 0x11223344;
452 
453                             // rtp header (min size 12B)
454                             int pos = 0;
455                             int mtu = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
456 
457                             uint8_t media_packet[mtu];
458                             media_packet[pos++] = (rtp_version << 6) | (padding << 5) | (extension << 4) | csrc_count;
459                             media_packet[pos++] = (marker << 1) | payload_type;
460                             big_endian_store_16(media_packet, pos, sequence_number);
461                             pos += 2;
462                             big_endian_store_32(media_packet, pos, timestamp);
463                             pos += 4;
464                             big_endian_store_32(media_packet, pos, ssrc); // only used for multicast
465                             pos += 4;
466 
467                             // media payload
468                             // sbc_header (size 1B)
469                             uint8_t sbc_header_index = pos;
470                             pos++;
471                             uint8_t fragmentation = 0;
472                             uint8_t starting_packet = 0; // set to 1 for the first packet of a fragmented SBC frame
473                             uint8_t last_packet = 0;     // set to 1 for the last packet of a fragmented SBC frame
474                             uint8_t num_frames = 0;
475 
476                             uint32_t total_sbc_bytes_read = 0;
477                             uint8_t  sbc_frame_size = 0;
478                             // payload
479                             while (mtu - 13 - total_sbc_bytes_read >= 120 && btstack_ring_buffer_bytes_available(&stream_endpoint->sbc_ring_buffer)){
480                                 uint32_t number_of_bytes_read = 0;
481                                 btstack_ring_buffer_read(&stream_endpoint->sbc_ring_buffer, &sbc_frame_size, 1, &number_of_bytes_read);
482                                 btstack_ring_buffer_read(&stream_endpoint->sbc_ring_buffer, media_packet + pos, sbc_frame_size, &number_of_bytes_read);
483                                 pos += sbc_frame_size;
484                                 total_sbc_bytes_read += sbc_frame_size;
485                                 num_frames++;
486                                 // printf("send sbc frame: timestamp %d, seq. nr %d\n", timestamp, stream_endpoint->sequence_number);
487                             }
488                             media_packet[sbc_header_index] =  (fragmentation << 7) | (starting_packet << 6) | (last_packet << 5) | num_frames;
489                             stream_endpoint->sequence_number++;
490                             l2cap_send(stream_endpoint->l2cap_media_cid, media_packet, pos);
491                             if (btstack_ring_buffer_bytes_available(&stream_endpoint->sbc_ring_buffer)){
492                                 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND;
493                                 l2cap_request_can_send_now_event(stream_endpoint->l2cap_media_cid);
494                             }
495                         }
496                         connection = stream_endpoint->connection;
497                     }
498                     avdtp_handle_can_send_now(connection, channel, context);
499                     break;
500                 default:
501                     printf("unknown HCI event type %02x\n", hci_event_packet_get_type(packet));
502                     break;
503             }
504             break;
505 
506         default:
507             // other packet type
508             break;
509     }
510 }
511 
512 void avdtp_disconnect(uint16_t con_handle, avdtp_context_t * context){
513     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
514     if (!connection) return;
515     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE) return;
516     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return;
517 
518     connection->disconnect = 1;
519     avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
520 }
521 
522 void avdtp_open_stream(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
523     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
524     if (!connection){
525         printf("avdtp_media_connect: no connection for handle 0x%02x found\n", con_handle);
526         return;
527     }
528 
529     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
530         printf("avdtp_media_connect: wrong connection state %d\n", connection->state);
531         return;
532     }
533 
534     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(acp_seid, context);
535     if (!stream_endpoint) {
536         printf("avdtp_media_connect: no stream_endpoint with acp seid %d found\n", acp_seid);
537         return;
538     }
539 
540     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return;
541     if (stream_endpoint->remote_sep_index == 0xFF) return;
542 
543     connection->initiator_transaction_label++;
544     connection->acp_seid = acp_seid;
545     connection->int_seid = stream_endpoint->sep.seid;
546     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
547     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
548     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
549 }
550 
551 void avdtp_start_stream(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
552     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
553     if (!connection){
554         printf("avdtp_media_connect: no connection for handle 0x%02x found\n", con_handle);
555         return;
556     }
557 
558     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
559         printf("avdtp_media_connect: wrong connection state %d\n", connection->state);
560         return;
561     }
562 
563     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(acp_seid, context);
564     if (!stream_endpoint) {
565         printf("avdtp_media_connect: no stream_endpoint with acp_seid %d found\n", acp_seid);
566         return;
567     }
568     if (stream_endpoint->remote_sep_index == 0xFF) return;
569     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_OPENED) return;
570     connection->initiator_transaction_label++;
571     connection->acp_seid = acp_seid;
572     connection->int_seid = stream_endpoint->sep.seid;
573     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_STREAMING_START;
574     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
575 }
576 
577 void avdtp_stop_stream(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
578     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
579     if (!connection){
580         printf("avdtp_stop_stream: no connection for handle 0x%02x found\n", con_handle);
581         return;
582     }
583     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
584         printf("avdtp_stop_stream: wrong connection state %d\n", connection->state);
585         return;
586     }
587 
588     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(acp_seid, context);
589     if (!stream_endpoint) {
590         printf("avdtp_stop_stream: no stream_endpoint with acp seid %d found\n", acp_seid);
591         return;
592     }
593     if (stream_endpoint->remote_sep_index == 0xFF) return;
594     switch (stream_endpoint->state){
595         case AVDTP_STREAM_ENDPOINT_OPENED:
596         case AVDTP_STREAM_ENDPOINT_STREAMING:
597             printf(" AVDTP_INITIATOR_W2_STREAMING_STOP \n");
598             connection->initiator_transaction_label++;
599             connection->acp_seid = acp_seid;
600             connection->int_seid = stream_endpoint->sep.seid;
601             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_STREAMING_STOP;
602             avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
603             break;
604         default:
605             break;
606     }
607 }
608 
609 void avdtp_abort_stream(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
610     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
611     if (!connection){
612         printf("avdtp_abort_stream: no connection for handle 0x%02x found\n", con_handle);
613         return;
614     }
615 
616     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
617         printf("avdtp_abort_stream: wrong connection state %d\n", connection->state);
618         return;
619     }
620 
621     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(acp_seid, context);
622     if (!stream_endpoint) {
623         printf("avdtp_abort_stream: no stream_endpoint for seid %d found\n", acp_seid);
624         return;
625     }
626     if (stream_endpoint->remote_sep_index == 0xFF) return;
627     switch (stream_endpoint->state){
628         case AVDTP_STREAM_ENDPOINT_CONFIGURED:
629         case AVDTP_STREAM_ENDPOINT_CLOSING:
630         case AVDTP_STREAM_ENDPOINT_OPENED:
631         case AVDTP_STREAM_ENDPOINT_STREAMING:
632             printf(" AVDTP_INITIATOR_W2_STREAMING_ABORT \n");
633             connection->initiator_transaction_label++;
634             connection->acp_seid = acp_seid;
635             connection->int_seid = stream_endpoint->sep.seid;
636             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_STREAMING_ABORT;
637             avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
638             break;
639         default:
640             break;
641     }
642 }
643 
644 void avdtp_discover_stream_endpoints(uint16_t con_handle, avdtp_context_t * context){
645     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
646     if (!connection){
647         printf("avdtp_discover_stream_endpoints: no connection for handle 0x%02x found\n", con_handle);
648         return;
649     }
650     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
651 
652     switch (connection->initiator_connection_state){
653         case AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE:
654             connection->initiator_transaction_label++;
655             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
656             avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
657             break;
658         default:
659             printf("avdtp_discover_stream_endpoints: wrong state\n");
660             break;
661     }
662 }
663 
664 
665 void avdtp_get_capabilities(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
666     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
667     if (!connection){
668         printf("avdtp_get_capabilities: no connection for handle 0x%02x found\n", con_handle);
669         return;
670     }
671     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
672     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
673     connection->initiator_transaction_label++;
674     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
675     connection->acp_seid = acp_seid;
676     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
677 }
678 
679 
680 void avdtp_get_all_capabilities(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
681     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
682     if (!connection){
683         printf("avdtp_get_all_capabilities: no connection for handle 0x%02x found\n", con_handle);
684         return;
685     }
686     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
687     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
688     connection->initiator_transaction_label++;
689     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
690     connection->acp_seid = acp_seid;
691     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
692 }
693 
694 void avdtp_get_configuration(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
695     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
696     if (!connection){
697         printf("avdtp_get_configuration: no connection for handle 0x%02x found\n", con_handle);
698         return;
699     }
700     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
701     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
702     connection->initiator_transaction_label++;
703     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
704     connection->acp_seid = acp_seid;
705     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
706 }
707 
708 void avdtp_set_configuration(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){
709     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
710     if (!connection){
711         log_error("avdtp_set_configuration: no connection for handle 0x%02x found\n", con_handle);
712         return;
713     }
714     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
715     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
716 
717     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(int_seid, context);
718     if (!stream_endpoint) {
719         log_error("avdtp_set_configuration: no initiator stream endpoint for seid %d\n", int_seid);
720         return;
721     }
722     // printf("avdtp_set_configuration int seid %d, acp seid %d\n", int_seid, acp_seid);
723 
724     connection->initiator_transaction_label++;
725     connection->acp_seid = acp_seid;
726     connection->int_seid = int_seid;
727     connection->remote_capabilities_bitmap = configured_services_bitmap;
728     connection->remote_capabilities = configuration;
729     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
730     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
731 }
732 
733 void avdtp_reconfigure(uint16_t con_handle, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){
734     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
735     if (!connection){
736         printf("avdtp_reconfigure: no connection for handle 0x%02x found\n", con_handle);
737         return;
738     }
739     //TODO: if opened only app capabilities, enable reconfigure for not opened
740     if (connection->state < AVDTP_SIGNALING_CONNECTION_OPENED) return;
741     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
742     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(acp_seid, context);
743     if (!stream_endpoint) return;
744     if (stream_endpoint->remote_sep_index == 0xFF) return;
745     connection->initiator_transaction_label++;
746     connection->acp_seid = acp_seid;
747     connection->int_seid = stream_endpoint->sep.seid;
748     connection->remote_capabilities_bitmap = configured_services_bitmap;
749     connection->remote_capabilities = configuration;
750     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
751     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
752 }
753 
754 void avdtp_suspend(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context){
755     avdtp_connection_t * connection = avdtp_connection_for_con_handle(con_handle, context);
756     if (!connection){
757         printf("avdtp_suspend: no connection for handle 0x%02x found\n", con_handle);
758         return;
759     }
760     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
761     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
762     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(acp_seid, context);
763     if (!stream_endpoint) return;
764     if (stream_endpoint->remote_sep_index == 0xFF) return;
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_SUSPEND_STREAM_WITH_SEID;
769     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
770 }
771