xref: /btstack/src/classic/avdtp_initiator.c (revision a06bcae0f7f63d9d69b2f846d04300ea7058ea66)
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_initiator.c"
39 
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack.h"
47 #include "avdtp.h"
48 #include "avdtp_util.h"
49 #include "avdtp_initiator.h"
50 
51 
52 static int avdtp_initiator_send_signaling_cmd(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
53     uint8_t command[2];
54     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG);
55     command[1] = (uint8_t)identifier;
56     return l2cap_send(cid, command, sizeof(command));
57 }
58 
59 static int avdtp_initiator_send_signaling_cmd_with_seid(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label, uint8_t sep_id){
60     uint8_t command[3];
61     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG);
62     command[1] = (uint8_t)identifier;
63     command[2] = sep_id << 2;
64     return l2cap_send(cid, command, sizeof(command));
65 }
66 
67 void avdtp_initiator_stream_config_subsm(avdtp_connection_t * connection, uint8_t *packet, uint16_t size, int offset, avdtp_context_t * context){
68     // int status = 0;
69     avdtp_stream_endpoint_t * stream_endpoint = NULL;
70 
71     uint8_t remote_sep_index;
72     avdtp_sep_t sep;
73     if (connection->initiator_connection_state == AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER) {
74         connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE;
75     } else {
76         stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(connection->remote_seid, context);
77         if (!stream_endpoint){
78             stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context);
79         }
80         if (!stream_endpoint) return;
81         sep.seid = connection->remote_seid;
82 
83         if (stream_endpoint->initiator_config_state != AVDTP_INITIATOR_W4_ANSWER) return;
84         stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
85     }
86 
87     switch (connection->signaling_packet.message_type){
88         case AVDTP_RESPONSE_ACCEPT_MSG:
89             switch (connection->signaling_packet.signal_identifier){
90                 case AVDTP_SI_DISCOVER:{
91                     if (connection->signaling_packet.transaction_label != connection->initiator_transaction_label){
92                         log_info("    unexpected transaction label, got %d, expected %d", connection->signaling_packet.transaction_label, connection->initiator_transaction_label);
93                         // status = BAD_HEADER_FORMAT;
94                         break;
95                     }
96 
97                     if (size == 3){
98                         log_info("    ERROR code %02x", packet[offset]);
99                         break;
100                     }
101 
102                 int i;
103                     for (i = offset; i < size; i += 2){
104                         sep.seid = packet[i] >> 2;
105                         offset++;
106                         if (sep.seid < 0x01 || sep.seid > 0x3E){
107                             log_info("    invalid sep id");
108                             // status = BAD_ACP_SEID;
109                             break;
110                         }
111                         sep.in_use = (packet[i] >> 1) & 0x01;
112                         sep.media_type = (avdtp_media_type_t)(packet[i+1] >> 4);
113                         sep.type = (avdtp_sep_type_t)((packet[i+1] >> 3) & 0x01);
114 
115                         if (avdtp_find_remote_sep(connection, sep.seid) == 0xFF){
116                             connection->remote_seps[connection->remote_seps_num++] = sep;
117                         }
118                         avdtp_signaling_emit_sep(context->avdtp_callback, connection->avdtp_cid, sep);
119                     }
120                     break;
121                 }
122 
123                 case AVDTP_SI_GET_CAPABILITIES:
124                 case AVDTP_SI_GET_ALL_CAPABILITIES:
125                     sep.registered_service_categories = avdtp_unpack_service_capabilities(connection, &sep.capabilities, packet+offset, size-offset);
126                     if (get_bit16(sep.registered_service_categories, AVDTP_MEDIA_CODEC)){
127                         switch (sep.capabilities.media_codec.media_codec_type){
128                             case AVDTP_CODEC_SBC:
129                                 avdtp_signaling_emit_media_codec_sbc_capability(context->avdtp_callback, connection->avdtp_cid, connection->local_seid, connection->remote_seid, sep.capabilities.media_codec);
130                                 break;
131                             default:
132                                 avdtp_signaling_emit_media_codec_other_capability(context->avdtp_callback, connection->avdtp_cid, connection->local_seid, connection->remote_seid, sep.capabilities.media_codec);
133                                 break;
134                         }
135                     }
136                     break;
137 
138                 case AVDTP_SI_GET_CONFIGURATION:
139                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, packet+offset, size-offset);
140                     if (get_bit16(sep.configured_service_categories, AVDTP_MEDIA_CODEC)){
141                         switch (sep.configuration.media_codec.media_codec_type){
142                             case AVDTP_CODEC_SBC:
143                                 avdtp_signaling_emit_media_codec_sbc_configuration(context->avdtp_callback, connection->avdtp_cid, connection->local_seid, connection->remote_seid, sep.configuration.media_codec);
144                                 break;
145                             default:
146                                 avdtp_signaling_emit_media_codec_other_configuration(context->avdtp_callback, connection->avdtp_cid, connection->local_seid,  connection->remote_seid, sep.configuration.media_codec);
147                                 break;
148                         }
149                     }
150                     break;
151 
152                 case AVDTP_SI_RECONFIGURE:
153                     if (!stream_endpoint){
154                         log_error("AVDTP_SI_RECONFIGURE: stream endpoint is null");
155                         break;
156                     }
157                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+4, connection->signaling_packet.size-4);
158                     // TODO check if configuration is supported
159 
160                     remote_sep_index = avdtp_find_remote_sep(connection, sep.seid);
161                     if (remote_sep_index != 0xFF){
162                         stream_endpoint->remote_sep_index = remote_sep_index;
163                         connection->remote_seps[stream_endpoint->remote_sep_index] = sep;
164                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED;
165                         log_info("INT: update seid %d, to %p", connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint);
166                     }
167                     break;
168 
169                 case AVDTP_SI_SET_CONFIGURATION:{
170                     if (!stream_endpoint){
171                         log_error("AVDTP_SI_SET_CONFIGURATION: stream endpoint is null");
172                         break;
173                     }
174                     sep.configured_service_categories = stream_endpoint->remote_capabilities_bitmap;
175                     sep.configuration = stream_endpoint->remote_capabilities;
176                     sep.in_use = 1;
177                     // TODO check if configuration is supported
178 
179                     // find or add sep
180                     remote_sep_index = avdtp_find_remote_sep(connection, sep.seid);
181                     if (remote_sep_index != 0xFF){
182                         stream_endpoint->remote_sep_index = remote_sep_index;
183                     } else {
184                         stream_endpoint->remote_sep_index = connection->remote_seps_num;
185                         connection->remote_seps_num++;
186                     }
187                     connection->remote_seps[stream_endpoint->remote_sep_index] = sep;
188                     log_info("INT: configured remote seid %d, to %p", connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint);
189                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED;
190                     break;
191                 }
192 
193                 case AVDTP_SI_OPEN:
194                     if (!stream_endpoint){
195                         log_error("AVDTP_SI_OPEN: stream endpoint is null");
196                         break;
197                     }
198                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM) {
199                         log_error("AVDTP_SI_OPEN in wrong stream endpoint state");
200                         return;
201                     }
202                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED;
203                     connection->local_seid = stream_endpoint->sep.seid;
204                     l2cap_create_channel(context->packet_handler, connection->remote_addr, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, NULL);
205                     return;
206                 case AVDTP_SI_START:
207                     if (!stream_endpoint){
208                         log_error("AVDTP_SI_START: stream endpoint is null");
209                         break;
210                     }
211                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_OPENED) {
212                         log_error("AVDTP_SI_START in wrong stream endpoint state");
213                         return;
214                     }
215                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
216                     break;
217                 case AVDTP_SI_SUSPEND:
218                     if (!stream_endpoint){
219                         log_error("AVDTP_SI_SUSPEND: stream endpoint is null");
220                         break;
221                     }
222                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_STREAMING) {
223                         log_error("AVDTP_SI_SUSPEND in wrong stream endpoint state");
224                         return;
225                     }
226                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
227                     break;
228                 case AVDTP_SI_CLOSE:
229                     if (!stream_endpoint){
230                         log_error("AVDTP_SI_CLOSE: stream endpoint is null");
231                         break;
232                     }
233                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CLOSING;
234                     break;
235                 case AVDTP_SI_ABORT:
236                     if (!stream_endpoint){
237                         log_error("AVDTP_SI_ABORT: stream endpoint is null");
238                         break;
239                     }
240                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING;
241                     break;
242                 default:
243                     log_info("    AVDTP_RESPONSE_ACCEPT_MSG, signal %d not implemented", connection->signaling_packet.signal_identifier);
244                     break;
245             }
246             avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, 0, connection->signaling_packet.signal_identifier);
247             connection->initiator_transaction_label++;
248             break;
249         case AVDTP_RESPONSE_REJECT_MSG:
250             log_info("    AVDTP_RESPONSE_REJECT_MSG signal %d", connection->signaling_packet.signal_identifier);
251             avdtp_signaling_emit_reject(context->avdtp_callback, connection->avdtp_cid, connection->local_seid, connection->signaling_packet.signal_identifier);
252             return;
253         case AVDTP_GENERAL_REJECT_MSG:
254             log_info("    AVDTP_GENERAL_REJECT_MSG signal %d", connection->signaling_packet.signal_identifier);
255             avdtp_signaling_emit_general_reject(context->avdtp_callback, connection->avdtp_cid, connection->local_seid, connection->signaling_packet.signal_identifier);
256             return;
257         default:
258             break;
259     }
260 }
261 
262 void avdtp_initiator_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_context_t * context){
263 int sent = 1;
264     switch (connection->initiator_connection_state){
265         case AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS:
266             log_info("INT: AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS");
267             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER;
268             avdtp_initiator_send_signaling_cmd(connection->l2cap_signaling_cid, AVDTP_SI_DISCOVER, connection->initiator_transaction_label);
269             break;
270         case AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES:
271             log_info("INT: AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES");
272             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER;
273             avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_GET_CAPABILITIES, connection->initiator_transaction_label, connection->remote_seid);
274             break;
275         case AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES:
276             log_info("INT: AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES");
277             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER;
278             avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_GET_ALL_CAPABILITIES, connection->initiator_transaction_label, connection->remote_seid);
279             break;
280         case AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION:
281             log_info("INT: AVDTP_INITIATOR_W4_GET_CONFIGURATION");
282             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_ANSWER;
283             avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_GET_CONFIGURATION, connection->initiator_transaction_label, connection->remote_seid);
284             break;
285         default:
286             sent = 0;
287             break;
288     }
289 
290     if (sent) return;
291     sent = 1;
292 
293     avdtp_stream_endpoint_t * stream_endpoint = NULL;
294 
295     stream_endpoint = avdtp_stream_endpoint_associated_with_acp_seid(connection->remote_seid, context);
296     if (!stream_endpoint){
297         stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context);
298     }
299     if (!stream_endpoint) return;
300 
301     avdtp_initiator_stream_endpoint_state_t stream_endpoint_state = stream_endpoint->initiator_config_state;
302     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W4_ANSWER;
303 
304     if (stream_endpoint->start_stream){
305         stream_endpoint->start_stream = 0;
306         if (stream_endpoint->state == AVDTP_STREAM_ENDPOINT_OPENED){
307             connection->local_seid = stream_endpoint->sep.seid;
308             connection->remote_seid = connection->remote_seps[stream_endpoint->remote_sep_index].seid;
309             avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_START, connection->initiator_transaction_label++, connection->remote_seid);
310             return;
311         }
312         return;
313     }
314 
315     if (stream_endpoint->stop_stream){
316         stream_endpoint->stop_stream = 0;
317         if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED){
318             connection->local_seid = stream_endpoint->sep.seid;
319             connection->remote_seid = connection->remote_seps[stream_endpoint->remote_sep_index].seid;
320             avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_CLOSE, connection->initiator_transaction_label++, connection->remote_seid);
321             return;
322         }
323     }
324 
325     if (stream_endpoint->abort_stream){
326         stream_endpoint->abort_stream = 0;
327         switch (stream_endpoint->state){
328             case AVDTP_STREAM_ENDPOINT_CONFIGURED:
329             case AVDTP_STREAM_ENDPOINT_CLOSING:
330             case AVDTP_STREAM_ENDPOINT_OPENED:
331             case AVDTP_STREAM_ENDPOINT_STREAMING:
332                 connection->local_seid = stream_endpoint->sep.seid;
333                 connection->remote_seid = connection->remote_seps[stream_endpoint->remote_sep_index].seid;
334                 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING;
335                 avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_ABORT, connection->initiator_transaction_label++, connection->remote_seid);
336                 return;
337             default:
338                 break;
339         }
340     }
341 
342     if (stream_endpoint->suspend_stream){
343         stream_endpoint->suspend_stream = 0;
344         if (stream_endpoint->state == AVDTP_STREAM_ENDPOINT_STREAMING){
345             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
346             avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_SUSPEND, connection->initiator_transaction_label, connection->remote_seid);
347             return;
348         }
349     }
350 
351     if (stream_endpoint->send_stream){
352         stream_endpoint->send_stream = 0;
353         if (stream_endpoint->state == AVDTP_STREAM_ENDPOINT_STREAMING){
354             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
355             avdtp_streaming_emit_can_send_media_packet_now(context->avdtp_callback, stream_endpoint->l2cap_media_cid, stream_endpoint->sep.seid, stream_endpoint->sequence_number);
356             return;
357         }
358     }
359 
360 
361     switch (stream_endpoint_state){
362         case AVDTP_INITIATOR_W2_SET_CONFIGURATION:
363         case AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID:{
364             log_info("INT: AVDTP_INITIATOR_W2_(RE)CONFIGURATION bitmap, int seid %d, acp seid %d", connection->local_seid, connection->remote_seid);
365             // log_info_hexdump(  connection->remote_capabilities.media_codec.media_codec_information,  connection->remote_capabilities.media_codec.media_codec_information_len);
366             connection->signaling_packet.acp_seid = connection->remote_seid;
367             connection->signaling_packet.int_seid = connection->local_seid;
368 
369             connection->signaling_packet.signal_identifier = AVDTP_SI_SET_CONFIGURATION;
370 
371             if (stream_endpoint_state == AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID){
372                 connection->signaling_packet.signal_identifier = AVDTP_SI_RECONFIGURE;
373             }
374 
375             avdtp_prepare_capabilities(&connection->signaling_packet, connection->initiator_transaction_label, stream_endpoint->remote_configuration_bitmap, stream_endpoint->remote_configuration, connection->signaling_packet.signal_identifier);
376             l2cap_reserve_packet_buffer();
377             uint8_t * out_buffer = l2cap_get_outgoing_buffer();
378             uint16_t pos = avdtp_signaling_create_fragment(connection->l2cap_signaling_cid, &connection->signaling_packet, out_buffer);
379             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
380                 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_FRAGMENTATED_COMMAND;
381                 log_info("INT: fragmented");
382             }
383             l2cap_send_prepared(connection->l2cap_signaling_cid, pos);
384             break;
385         }
386         case AVDTP_INITIATOR_FRAGMENTATED_COMMAND:{
387             l2cap_reserve_packet_buffer();
388             uint8_t * out_buffer = l2cap_get_outgoing_buffer();
389             uint16_t pos = avdtp_signaling_create_fragment(connection->l2cap_signaling_cid, &connection->signaling_packet, out_buffer);
390             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
391                 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_FRAGMENTATED_COMMAND;
392                 log_info("INT: fragmented");
393             }
394             l2cap_send_prepared(connection->l2cap_signaling_cid, pos);
395             break;
396         }
397         case AVDTP_INITIATOR_W2_OPEN_STREAM:
398             switch (stream_endpoint->state){
399                 case AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM:
400                     log_info("INT: AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM");
401                     avdtp_initiator_send_signaling_cmd_with_seid(connection->l2cap_signaling_cid, AVDTP_SI_OPEN, connection->initiator_transaction_label, connection->remote_seid);
402                     break;
403                 default:
404                     sent = 0;
405                     break;
406             }
407             break;
408         default:
409             sent = 0;
410             break;
411     }
412 
413     // check fragmentation
414     if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
415         avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
416     }
417 }
418