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