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