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 #include <stdint.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 #include "btstack.h" 44 #include "classic/avdtp.h" 45 #include "classic/avdtp_util.h" 46 #include "classic/avdtp_acceptor.h" 47 48 49 static int avdtp_acceptor_send_accept_response(uint16_t cid, uint8_t transaction_label, avdtp_signal_identifier_t identifier){ 50 uint8_t command[2]; 51 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG); 52 command[1] = (uint8_t)identifier; 53 return l2cap_send(cid, command, sizeof(command)); 54 } 55 56 static int avdtp_acceptor_process_chunk(avdtp_signaling_packet_t * signaling_packet, uint8_t * packet, uint16_t size){ 57 memcpy(signaling_packet->command + signaling_packet->size, packet, size); 58 signaling_packet->size += size; 59 return signaling_packet->packet_type == AVDTP_SINGLE_PACKET || signaling_packet->packet_type == AVDTP_END_PACKET; 60 } 61 62 static int avdtp_acceptor_validate_msg_length(avdtp_signal_identifier_t signal_identifier, uint16_t msg_size){ 63 int minimal_msg_lenght = 2; 64 switch (signal_identifier){ 65 case AVDTP_SI_GET_CAPABILITIES: 66 case AVDTP_SI_GET_ALL_CAPABILITIES: 67 case AVDTP_SI_SET_CONFIGURATION: 68 case AVDTP_SI_GET_CONFIGURATION: 69 case AVDTP_SI_START: 70 case AVDTP_SI_CLOSE: 71 case AVDTP_SI_ABORT: 72 case AVDTP_SI_RECONFIGURE: 73 case AVDTP_SI_OPEN: 74 minimal_msg_lenght = 3; 75 break; 76 default: 77 break; 78 } 79 return msg_size >= minimal_msg_lenght; 80 } 81 82 void avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, uint8_t * packet, uint16_t size, int offset, avdtp_context_t * context){ 83 avdtp_stream_endpoint_t * stream_endpoint; 84 connection->acceptor_transaction_label = connection->signaling_packet.transaction_label; 85 86 if (!avdtp_acceptor_validate_msg_length(connection->signaling_packet.signal_identifier, size)) { 87 connection->error_code = BAD_LENGTH; 88 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 89 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 90 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 91 return; 92 } 93 94 switch (connection->signaling_packet.signal_identifier){ 95 case AVDTP_SI_DISCOVER: 96 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return; 97 log_info("ACP: AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS"); 98 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS; 99 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 100 return; 101 case AVDTP_SI_GET_CAPABILITIES: 102 case AVDTP_SI_GET_ALL_CAPABILITIES: 103 case AVDTP_SI_SET_CONFIGURATION: 104 case AVDTP_SI_GET_CONFIGURATION: 105 case AVDTP_SI_START: 106 case AVDTP_SI_CLOSE: 107 case AVDTP_SI_ABORT: 108 case AVDTP_SI_OPEN: 109 case AVDTP_SI_RECONFIGURE: 110 connection->local_seid = packet[offset++] >> 2; 111 stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context); 112 if (!stream_endpoint){ 113 log_info("ACP: cmd %d - RESPONSE REJECT", connection->signaling_packet.signal_identifier); 114 connection->error_code = BAD_ACP_SEID; 115 if (connection->signaling_packet.signal_identifier == AVDTP_SI_OPEN){ 116 connection->error_code = BAD_STATE; 117 } 118 119 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 120 if (connection->signaling_packet.signal_identifier == AVDTP_SI_RECONFIGURE){ 121 connection->reject_service_category = connection->local_seid; 122 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 123 } 124 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 125 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 126 return; 127 } 128 break; 129 130 case AVDTP_SI_SUSPEND:{ 131 int i; 132 log_info("ACP: AVDTP_SI_SUSPEND seids: "); 133 connection->num_suspended_seids = 0; 134 135 for (i = offset; i < size; i++){ 136 connection->suspended_seids[connection->num_suspended_seids] = packet[i] >> 2; 137 offset++; 138 log_info("%d, ", connection->suspended_seids[connection->num_suspended_seids]); 139 connection->num_suspended_seids++; 140 } 141 142 if (connection->num_suspended_seids == 0) { 143 log_info("ACP: CATEGORY RESPONSE REJECT BAD_ACP_SEID"); 144 connection->error_code = BAD_ACP_SEID; 145 connection->reject_service_category = connection->local_seid; 146 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 147 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 148 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 149 return; 150 } 151 // deal with first susspended seid 152 connection->local_seid = connection->suspended_seids[0]; 153 stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context); 154 if (!stream_endpoint){ 155 log_info("ACP: stream_endpoint not found, CATEGORY RESPONSE REJECT BAD_ACP_SEID"); 156 connection->error_code = BAD_ACP_SEID; 157 connection->reject_service_category = connection->local_seid; 158 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 159 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 160 connection->num_suspended_seids = 0; 161 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 162 return; 163 } 164 break; 165 } 166 default: 167 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE; 168 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 169 log_info("AVDTP_CMD_MSG signal %d not implemented, general reject", connection->signaling_packet.signal_identifier); 170 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 171 return; 172 } 173 174 if (!stream_endpoint) { 175 return; 176 } 177 178 if (!avdtp_acceptor_process_chunk(&connection->signaling_packet, packet, size)) return; 179 180 uint16_t packet_size = connection->signaling_packet.size; 181 connection->signaling_packet.size = 0; 182 183 int request_to_send = 1; 184 switch (stream_endpoint->acceptor_config_state){ 185 case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE: 186 switch (connection->signaling_packet.signal_identifier){ 187 case AVDTP_SI_GET_ALL_CAPABILITIES: 188 log_info("ACP: AVDTP_SI_GET_ALL_CAPABILITIES"); 189 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES; 190 break; 191 case AVDTP_SI_GET_CAPABILITIES: 192 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES"); 193 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES; 194 break; 195 case AVDTP_SI_SET_CONFIGURATION:{ 196 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION "); 197 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION; 198 connection->reject_service_category = 0; 199 stream_endpoint->connection = connection; 200 avdtp_sep_t sep; 201 sep.seid = connection->signaling_packet.command[offset++] >> 2; 202 sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset); 203 sep.in_use = 1; 204 205 if (connection->error_code){ 206 log_info("fire configuration parsing errors "); 207 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 208 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 209 break; 210 } 211 // find or add sep 212 213 stream_endpoint->remote_sep_index = avdtp_find_remote_sep(stream_endpoint->connection, sep.seid); 214 log_info("ACP .. seid %d, index %d", sep.seid, stream_endpoint->remote_sep_index); 215 216 if (stream_endpoint->remote_sep_index != AVDTP_INVALID_SEP_INDEX){ 217 if (stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].in_use){ 218 // if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_OPENED){ 219 // stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 220 // log_info("ACP: update seid %d, to %p", stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 221 // break; 222 // } 223 // reject if already configured 224 connection->error_code = SEP_IN_USE; 225 // find first registered category and fire the error 226 connection->reject_service_category = 0; 227 int i; 228 for (i = 1; i < 9; i++){ 229 if (get_bit16(sep.configured_service_categories, i)){ 230 connection->reject_service_category = i; 231 break; 232 } 233 } 234 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 235 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 236 } else { 237 stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 238 log_info("ACP: update seid %d, to %p", stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 239 } 240 } else { 241 // add new 242 log_info("ACP: seid %d not found in %p", sep.seid, stream_endpoint); 243 stream_endpoint->remote_sep_index = connection->remote_seps_num; 244 connection->remote_seps_num++; 245 connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 246 log_info("ACP: add seid %d, to %p", connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 247 } 248 if (get_bit16(sep.configured_service_categories, AVDTP_MEDIA_CODEC)){ 249 switch (sep.configuration.media_codec.media_codec_type){ 250 case AVDTP_CODEC_SBC: 251 avdtp_signaling_emit_media_codec_sbc_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), sep.configuration.media_codec); 252 break; 253 default: 254 avdtp_signaling_emit_media_codec_other_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), sep.configuration.media_codec); 255 break; 256 } 257 } 258 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 259 break; 260 } 261 case AVDTP_SI_RECONFIGURE:{ 262 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE; 263 connection->reject_service_category = 0; 264 265 avdtp_sep_t sep; 266 sep.seid = connection->local_seid; 267 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE seid %d", sep.seid); 268 sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset); 269 if (connection->error_code){ 270 // fire configuration parsing errors 271 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 272 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 273 break; 274 } 275 276 // find sep or raise error 277 stream_endpoint->remote_sep_index = avdtp_find_remote_sep(stream_endpoint->connection, sep.seid); 278 if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX){ 279 log_info("ACP: REJECT AVDTP_SI_RECONFIGURE, BAD_ACP_SEID"); 280 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 281 connection->error_code = BAD_ACP_SEID; 282 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 283 break; 284 } 285 stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 286 log_info("ACP: update seid %d, to %p", stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 287 288 if (get_bit16(sep.configured_service_categories, AVDTP_MEDIA_CODEC)){ 289 switch (sep.capabilities.media_codec.media_codec_type){ 290 case AVDTP_CODEC_SBC: 291 avdtp_signaling_emit_media_codec_sbc_reconfiguration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), sep.configuration.media_codec); 292 break; 293 default: 294 avdtp_signaling_emit_media_codec_other_reconfiguration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), sep.configuration.media_codec); 295 break; 296 } 297 } 298 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 299 break; 300 } 301 302 case AVDTP_SI_GET_CONFIGURATION: 303 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION"); 304 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION; 305 break; 306 case AVDTP_SI_OPEN: 307 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURED){ 308 log_info("ACP: REJECT AVDTP_SI_OPEN, BAD_STATE"); 309 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 310 connection->error_code = BAD_STATE; 311 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 312 break; 313 } 314 log_info("ACP: AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM"); 315 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM; 316 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED; 317 connection->local_seid = stream_endpoint->sep.seid; 318 break; 319 case AVDTP_SI_START: 320 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_OPENED){ 321 log_info("ACP: REJECT AVDTP_SI_START, BAD_STATE"); 322 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 323 connection->error_code = BAD_STATE; 324 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 325 break; 326 } 327 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM"); 328 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM; 329 break; 330 case AVDTP_SI_CLOSE: 331 switch (stream_endpoint->state){ 332 case AVDTP_STREAM_ENDPOINT_OPENED: 333 case AVDTP_STREAM_ENDPOINT_STREAMING: 334 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM"); 335 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CLOSING; 336 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM; 337 break; 338 default: 339 log_info("ACP: AVDTP_SI_CLOSE, bad state %d ", stream_endpoint->state); 340 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 341 connection->error_code = BAD_STATE; 342 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 343 break; 344 } 345 break; 346 case AVDTP_SI_ABORT: 347 switch (stream_endpoint->state){ 348 case AVDTP_STREAM_ENDPOINT_CONFIGURED: 349 case AVDTP_STREAM_ENDPOINT_CLOSING: 350 case AVDTP_STREAM_ENDPOINT_OPENED: 351 case AVDTP_STREAM_ENDPOINT_STREAMING: 352 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM"); 353 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING; 354 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM; 355 break; 356 default: 357 log_info("ACP: AVDTP_SI_ABORT, bad state %d ", stream_endpoint->state); 358 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 359 connection->error_code = BAD_STATE; 360 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 361 break; 362 } 363 break; 364 case AVDTP_SI_SUSPEND: 365 log_info(" entering AVDTP_SI_SUSPEND"); 366 switch (stream_endpoint->state){ 367 case AVDTP_STREAM_ENDPOINT_OPENED: 368 case AVDTP_STREAM_ENDPOINT_STREAMING: 369 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 370 connection->num_suspended_seids--; 371 if (connection->num_suspended_seids <= 0){ 372 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM"); 373 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM; 374 } 375 break; 376 default: 377 log_info("ACP: AVDTP_SI_SUSPEND, bad state "); 378 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 379 connection->error_code = BAD_STATE; 380 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 381 break; 382 } 383 384 //stream_endpoint->state = AVDTP_STREAM_ENDPOINT_SUSPENDING; 385 //stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_SUSPEND_STREAM; 386 break; 387 default: 388 log_info("ACP: NOT IMPLEMENTED, Reject signal_identifier %02x", connection->signaling_packet.signal_identifier); 389 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD; 390 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 391 break; 392 } 393 break; 394 default: 395 return; 396 } 397 398 if (!request_to_send){ 399 log_info("ACP: NOT IMPLEMENTED"); 400 } 401 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 402 } 403 404 static int avdtp_acceptor_send_seps_response(uint16_t cid, uint8_t transaction_label, avdtp_stream_endpoint_t * endpoints){ 405 uint8_t command[2+2*MAX_NUM_SEPS]; 406 int pos = 0; 407 command[pos++] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG); 408 command[pos++] = (uint8_t)AVDTP_SI_DISCOVER; 409 410 btstack_linked_list_iterator_t it; 411 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) endpoints); 412 while (btstack_linked_list_iterator_has_next(&it)){ 413 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 414 command[pos++] = (stream_endpoint->sep.seid << 2) | (stream_endpoint->sep.in_use<<1); 415 command[pos++] = (stream_endpoint->sep.media_type << 4) | (stream_endpoint->sep.type << 3); 416 } 417 return l2cap_send(cid, command, pos); 418 } 419 420 static int avdtp_acceptor_send_response_reject_service_category(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t category, uint8_t error_code, uint8_t transaction_label){ 421 uint8_t command[4]; 422 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG); 423 command[1] = (uint8_t)identifier; 424 command[2] = category; 425 command[3] = error_code; 426 return l2cap_send(cid, command, sizeof(command)); 427 } 428 429 static int avdtp_acceptor_send_response_general_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ 430 uint8_t command[2]; 431 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG); 432 command[1] = (uint8_t)identifier; 433 return l2cap_send(cid, command, sizeof(command)); 434 } 435 436 static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ 437 uint8_t command[2]; 438 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG); 439 command[1] = (uint8_t)identifier; 440 return l2cap_send(cid, command, sizeof(command)); 441 } 442 443 static int avdtp_acceptor_send_response_reject_with_error_code(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t error_code, uint8_t transaction_label){ 444 uint8_t command[3]; 445 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG); 446 command[1] = (uint8_t)identifier; 447 command[2] = error_code; 448 return l2cap_send(cid, command, sizeof(command)); 449 } 450 451 void avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_context_t * context){ 452 int sent = 1; 453 btstack_linked_list_t * stream_endpoints = &context->stream_endpoints; 454 455 switch (connection->acceptor_connection_state){ 456 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS: 457 connection->state = AVDTP_SIGNALING_CONNECTION_OPENED; 458 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 459 avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *) stream_endpoints); 460 break; 461 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE: 462 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 463 avdtp_acceptor_send_response_reject_with_error_code(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->error_code, connection->acceptor_transaction_label); 464 break; 465 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE: 466 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 467 avdtp_acceptor_send_response_reject_service_category(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->reject_service_category, connection->error_code, connection->acceptor_transaction_label); 468 break; 469 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE: 470 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 471 avdtp_acceptor_send_response_general_reject(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->acceptor_transaction_label); 472 break; 473 default: 474 sent = 0; 475 break; 476 } 477 if (sent){ 478 log_info("ACP: DONE"); 479 return; 480 } 481 482 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context); 483 if (!stream_endpoint) return; 484 485 uint8_t reject_service_category = connection->reject_service_category; 486 avdtp_signal_identifier_t reject_signal_identifier = connection->reject_signal_identifier; 487 uint8_t error_code = connection->error_code; 488 uint16_t cid = stream_endpoint->connection ? stream_endpoint->connection->l2cap_signaling_cid : connection->l2cap_signaling_cid; 489 uint8_t trid = stream_endpoint->connection ? stream_endpoint->connection->acceptor_transaction_label : connection->acceptor_transaction_label; 490 491 avdtp_acceptor_stream_endpoint_state_t acceptor_config_state = stream_endpoint->acceptor_config_state; 492 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; 493 uint8_t * out_buffer; 494 uint16_t pos; 495 496 switch (acceptor_config_state){ 497 case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE: 498 break; 499 case AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES: 500 avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_CAPABILITIES); 501 l2cap_reserve_packet_buffer(); 502 out_buffer = l2cap_get_outgoing_buffer(); 503 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 504 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 505 stream_endpoint->acceptor_config_state = acceptor_config_state; 506 log_info("ACP: fragmented"); 507 } else { 508 log_info("ACP:DONE"); 509 } 510 l2cap_send_prepared(cid, pos); 511 break; 512 case AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES: 513 avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES); 514 l2cap_reserve_packet_buffer(); 515 out_buffer = l2cap_get_outgoing_buffer(); 516 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 517 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 518 stream_endpoint->acceptor_config_state = acceptor_config_state; 519 log_info("ACP: fragmented"); 520 } else { 521 log_info("ACP:DONE"); 522 } 523 l2cap_send_prepared(cid, pos); 524 break; 525 case AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION: 526 log_info("ACP: DONE"); 527 log_info(" -> AVDTP_STREAM_ENDPOINT_CONFIGURED"); 528 stream_endpoint->connection = connection; 529 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED; 530 // TODO: consider reconfiguration 531 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION); 532 break; 533 case AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE: 534 log_info("ACP: DONE "); 535 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE); 536 break; 537 538 case AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION:{ 539 avdtp_sep_t sep = stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index]; 540 avdtp_prepare_capabilities(&connection->signaling_packet, trid, sep.configured_service_categories, sep.configuration, AVDTP_SI_GET_CONFIGURATION); 541 l2cap_reserve_packet_buffer(); 542 out_buffer = l2cap_get_outgoing_buffer(); 543 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 544 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 545 stream_endpoint->acceptor_config_state = acceptor_config_state; 546 log_info("ACP: fragmented"); 547 } else { 548 log_info("ACP:DONE"); 549 } 550 l2cap_send_prepared(cid, pos); 551 break; 552 } 553 case AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM: 554 log_info("ACP: DONE"); 555 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN); 556 break; 557 case AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM: 558 log_info("ACP: DONE "); 559 log_info(" -> AVDTP_STREAM_ENDPOINT_STREAMING "); 560 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING; 561 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START); 562 break; 563 case AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM: 564 log_info("ACP: DONE"); 565 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE); 566 break; 567 case AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM: 568 log_info("ACP: DONE"); 569 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT); 570 break; 571 case AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM: 572 log_info("ACP: DONE"); 573 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 574 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND); 575 break; 576 case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD: 577 log_info("ACP: DONE REJECT"); 578 connection->reject_signal_identifier = AVDTP_SI_NONE; 579 avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid); 580 break; 581 case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE: 582 log_info("ACP: DONE REJECT CATEGORY"); 583 connection->reject_service_category = 0; 584 avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid); 585 break; 586 case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE: 587 log_info("ACP: DONE REJECT"); 588 connection->reject_signal_identifier = AVDTP_SI_NONE; 589 connection->error_code = 0; 590 avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid); 591 break; 592 default: 593 log_info("ACP: NOT IMPLEMENTED"); 594 sent = 0; 595 break; 596 } 597 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 598 // check fragmentation 599 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 600 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 601 } 602 } 603