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 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("acceptor SM received SET_CONFIGURATION cmd: role is_initiator %d", connection->is_initiator); 197 if (connection->is_initiator){ 198 if (connection->is_configuration_initiated_locally){ 199 log_info("ACP: Set configuration already initiated locally, reject cmd "); 200 // fire configuration parsing errors 201 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 202 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD; 203 connection->is_initiator = 1; 204 break; 205 } 206 connection->is_initiator = 0; 207 log_info("acceptor SM received SET_CONFIGURATION cmd: change role to acceptor, is_initiator %d", connection->is_initiator); 208 } 209 210 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION "); 211 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE; 212 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION; 213 connection->reject_service_category = 0; 214 stream_endpoint->connection = connection; 215 avdtp_sep_t sep; 216 sep.seid = connection->signaling_packet.command[offset++] >> 2; 217 sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset); 218 sep.in_use = 1; 219 220 if (connection->error_code){ 221 log_info("fire configuration parsing errors "); 222 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 223 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 224 break; 225 } 226 // find or add sep 227 228 stream_endpoint->remote_sep_index = avdtp_find_remote_sep(stream_endpoint->connection, sep.seid); 229 log_info("ACP .. seid %d, index %d", sep.seid, stream_endpoint->remote_sep_index); 230 231 if (stream_endpoint->remote_sep_index != AVDTP_INVALID_SEP_INDEX){ 232 if (stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].in_use){ 233 // if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_OPENED){ 234 // stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 235 // log_info("ACP: update seid %d, to %p", stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 236 // break; 237 // } 238 // reject if already configured 239 connection->error_code = SEP_IN_USE; 240 // find first registered category and fire the error 241 connection->reject_service_category = 0; 242 int i; 243 for (i = 1; i < 9; i++){ 244 if (get_bit16(sep.configured_service_categories, i)){ 245 connection->reject_service_category = i; 246 break; 247 } 248 } 249 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 250 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 251 } else { 252 stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 253 log_info("ACP: update seid %d, to %p", stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 254 } 255 } else { 256 // add new 257 log_info("ACP: seid %d not found in %p", sep.seid, stream_endpoint); 258 stream_endpoint->remote_sep_index = connection->remote_seps_num; 259 connection->remote_seps_num++; 260 connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 261 log_info("ACP: add seid %d, to %p", connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 262 } 263 264 avdtp_emit_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), &sep.configuration, sep.configured_service_categories); 265 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 266 break; 267 } 268 case AVDTP_SI_RECONFIGURE:{ 269 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE; 270 connection->reject_service_category = 0; 271 272 avdtp_sep_t sep; 273 sep.seid = connection->local_seid; 274 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE seid %d", sep.seid); 275 sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset); 276 if (connection->error_code){ 277 // fire configuration parsing errors 278 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 279 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 280 break; 281 } 282 283 // find sep or raise error 284 stream_endpoint->remote_sep_index = avdtp_find_remote_sep(stream_endpoint->connection, sep.seid); 285 if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX){ 286 log_info("ACP: REJECT AVDTP_SI_RECONFIGURE, BAD_ACP_SEID"); 287 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 288 connection->error_code = BAD_ACP_SEID; 289 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 290 break; 291 } 292 stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index] = sep; 293 log_info("ACP: update seid %d, to %p", stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index].seid, stream_endpoint); 294 295 avdtp_emit_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), &sep.configuration, sep.configured_service_categories); 296 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 297 break; 298 } 299 300 case AVDTP_SI_GET_CONFIGURATION: 301 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION"); 302 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION; 303 break; 304 case AVDTP_SI_OPEN: 305 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURED){ 306 log_info("ACP: REJECT AVDTP_SI_OPEN, BAD_STATE"); 307 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 308 connection->error_code = BAD_STATE; 309 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 310 break; 311 } 312 log_info("ACP: AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM"); 313 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM; 314 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED; 315 connection->local_seid = stream_endpoint->sep.seid; 316 break; 317 case AVDTP_SI_START: 318 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_OPENED){ 319 log_info("ACP: REJECT AVDTP_SI_START, BAD_STATE"); 320 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 321 connection->error_code = BAD_STATE; 322 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 323 break; 324 } 325 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM"); 326 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM; 327 break; 328 case AVDTP_SI_CLOSE: 329 switch (stream_endpoint->state){ 330 case AVDTP_STREAM_ENDPOINT_OPENED: 331 case AVDTP_STREAM_ENDPOINT_STREAMING: 332 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM"); 333 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CLOSING; 334 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM; 335 break; 336 default: 337 log_info("ACP: AVDTP_SI_CLOSE, bad state %d ", stream_endpoint->state); 338 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 339 connection->error_code = BAD_STATE; 340 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 341 break; 342 } 343 break; 344 case AVDTP_SI_ABORT: 345 switch (stream_endpoint->state){ 346 case AVDTP_STREAM_ENDPOINT_CONFIGURED: 347 case AVDTP_STREAM_ENDPOINT_CLOSING: 348 case AVDTP_STREAM_ENDPOINT_OPENED: 349 case AVDTP_STREAM_ENDPOINT_STREAMING: 350 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM"); 351 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING; 352 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM; 353 break; 354 default: 355 log_info("ACP: AVDTP_SI_ABORT, bad state %d ", stream_endpoint->state); 356 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 357 connection->error_code = BAD_STATE; 358 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 359 break; 360 } 361 break; 362 case AVDTP_SI_SUSPEND: 363 log_info(" entering AVDTP_SI_SUSPEND"); 364 switch (stream_endpoint->state){ 365 case AVDTP_STREAM_ENDPOINT_OPENED: 366 case AVDTP_STREAM_ENDPOINT_STREAMING: 367 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 368 connection->num_suspended_seids--; 369 if (connection->num_suspended_seids <= 0){ 370 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM"); 371 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM; 372 } 373 break; 374 default: 375 log_info("ACP: AVDTP_SI_SUSPEND, bad state "); 376 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 377 connection->error_code = BAD_STATE; 378 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 379 break; 380 } 381 382 //stream_endpoint->state = AVDTP_STREAM_ENDPOINT_SUSPENDING; 383 //stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_SUSPEND_STREAM; 384 break; 385 default: 386 log_info("ACP: NOT IMPLEMENTED, Reject signal_identifier %02x", connection->signaling_packet.signal_identifier); 387 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD; 388 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 389 break; 390 } 391 break; 392 default: 393 return; 394 } 395 396 if (!request_to_send){ 397 log_info("ACP: NOT IMPLEMENTED"); 398 } 399 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 400 } 401 402 static int avdtp_acceptor_send_seps_response(uint16_t cid, uint8_t transaction_label, avdtp_stream_endpoint_t * endpoints){ 403 uint8_t command[2+2*AVDTP_MAX_NUM_SEPS]; 404 int pos = 0; 405 command[pos++] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG); 406 command[pos++] = (uint8_t)AVDTP_SI_DISCOVER; 407 408 btstack_linked_list_iterator_t it; 409 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) endpoints); 410 while (btstack_linked_list_iterator_has_next(&it)){ 411 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 412 command[pos++] = (stream_endpoint->sep.seid << 2) | (stream_endpoint->sep.in_use<<1); 413 command[pos++] = (stream_endpoint->sep.media_type << 4) | (stream_endpoint->sep.type << 3); 414 } 415 return l2cap_send(cid, command, pos); 416 } 417 418 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){ 419 uint8_t command[4]; 420 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG); 421 command[1] = (uint8_t)identifier; 422 command[2] = category; 423 command[3] = error_code; 424 return l2cap_send(cid, command, sizeof(command)); 425 } 426 427 static int avdtp_acceptor_send_response_general_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ 428 uint8_t command[2]; 429 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG); 430 command[1] = (uint8_t)identifier; 431 return l2cap_send(cid, command, sizeof(command)); 432 } 433 434 static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ 435 uint8_t command[2]; 436 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG); 437 command[1] = (uint8_t)identifier; 438 return l2cap_send(cid, command, sizeof(command)); 439 } 440 441 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){ 442 uint8_t command[3]; 443 command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG); 444 command[1] = (uint8_t)identifier; 445 command[2] = error_code; 446 return l2cap_send(cid, command, sizeof(command)); 447 } 448 449 void avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_context_t * context){ 450 int sent = 1; 451 btstack_linked_list_t * stream_endpoints = &context->stream_endpoints; 452 453 switch (connection->acceptor_connection_state){ 454 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS: 455 connection->state = AVDTP_SIGNALING_CONNECTION_OPENED; 456 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 457 avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *) stream_endpoints); 458 break; 459 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE: 460 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 461 avdtp_acceptor_send_response_reject_with_error_code(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->error_code, connection->acceptor_transaction_label); 462 break; 463 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE: 464 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 465 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); 466 break; 467 case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE: 468 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE; 469 avdtp_acceptor_send_response_general_reject(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->acceptor_transaction_label); 470 break; 471 default: 472 sent = 0; 473 break; 474 } 475 if (sent){ 476 log_info("ACP: DONE"); 477 return; 478 } 479 480 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context); 481 if (!stream_endpoint) return; 482 483 uint8_t reject_service_category = connection->reject_service_category; 484 avdtp_signal_identifier_t reject_signal_identifier = connection->reject_signal_identifier; 485 uint8_t error_code = connection->error_code; 486 uint16_t cid = stream_endpoint->connection ? stream_endpoint->connection->l2cap_signaling_cid : connection->l2cap_signaling_cid; 487 uint8_t trid = stream_endpoint->connection ? stream_endpoint->connection->acceptor_transaction_label : connection->acceptor_transaction_label; 488 489 avdtp_acceptor_stream_endpoint_state_t acceptor_config_state = stream_endpoint->acceptor_config_state; 490 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; 491 uint8_t * out_buffer; 492 uint16_t pos; 493 494 switch (acceptor_config_state){ 495 case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE: 496 break; 497 case AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES: 498 avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_CAPABILITIES); 499 l2cap_reserve_packet_buffer(); 500 out_buffer = l2cap_get_outgoing_buffer(); 501 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 502 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 503 stream_endpoint->acceptor_config_state = acceptor_config_state; 504 log_info("ACP: fragmented"); 505 } else { 506 log_info("ACP:DONE"); 507 } 508 l2cap_send_prepared(cid, pos); 509 break; 510 case AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES: 511 avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES); 512 l2cap_reserve_packet_buffer(); 513 out_buffer = l2cap_get_outgoing_buffer(); 514 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 515 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 516 stream_endpoint->acceptor_config_state = acceptor_config_state; 517 log_info("ACP: fragmented"); 518 } else { 519 log_info("ACP:DONE"); 520 } 521 l2cap_send_prepared(cid, pos); 522 break; 523 case AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION: 524 log_info("ACP: DONE"); 525 log_info(" -> AVDTP_STREAM_ENDPOINT_CONFIGURED"); 526 stream_endpoint->connection = connection; 527 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED; 528 // TODO: consider reconfiguration 529 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION); 530 break; 531 case AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE: 532 log_info("ACP: DONE "); 533 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE); 534 break; 535 536 case AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION:{ 537 avdtp_sep_t sep = stream_endpoint->connection->remote_seps[stream_endpoint->remote_sep_index]; 538 avdtp_prepare_capabilities(&connection->signaling_packet, trid, sep.configured_service_categories, sep.configuration, AVDTP_SI_GET_CONFIGURATION); 539 l2cap_reserve_packet_buffer(); 540 out_buffer = l2cap_get_outgoing_buffer(); 541 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 542 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 543 stream_endpoint->acceptor_config_state = acceptor_config_state; 544 log_info("ACP: fragmented"); 545 } else { 546 log_info("ACP:DONE"); 547 } 548 l2cap_send_prepared(cid, pos); 549 break; 550 } 551 case AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM: 552 log_info("ACP: DONE"); 553 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN); 554 break; 555 case AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM: 556 log_info("ACP: DONE "); 557 log_info(" -> AVDTP_STREAM_ENDPOINT_STREAMING "); 558 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING; 559 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START); 560 break; 561 case AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM: 562 log_info("ACP: DONE"); 563 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE); 564 break; 565 case AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM: 566 log_info("ACP: DONE"); 567 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT); 568 break; 569 case AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM: 570 log_info("ACP: DONE"); 571 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 572 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND); 573 break; 574 case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD: 575 log_info("ACP: DONE REJECT"); 576 connection->reject_signal_identifier = AVDTP_SI_NONE; 577 avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid); 578 break; 579 case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE: 580 log_info("ACP: DONE REJECT CATEGORY"); 581 connection->reject_service_category = 0; 582 avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid); 583 break; 584 case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE: 585 log_info("ACP: DONE REJECT"); 586 connection->reject_signal_identifier = AVDTP_SI_NONE; 587 connection->error_code = 0; 588 avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid); 589 break; 590 default: 591 log_info("ACP: NOT IMPLEMENTED"); 592 sent = 0; 593 break; 594 } 595 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 596 // check fragmentation 597 if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){ 598 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 599 } 600 } 601