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 (void)memcpy(signaling_packet->command + signaling_packet->size, packet, 59 size); 60 signaling_packet->size += size; 61 return (signaling_packet->packet_type == AVDTP_SINGLE_PACKET) || (signaling_packet->packet_type == AVDTP_END_PACKET); 62 } 63 64 static int avdtp_acceptor_validate_msg_length(avdtp_signal_identifier_t signal_identifier, uint16_t msg_size){ 65 int minimal_msg_lenght = 2; 66 switch (signal_identifier){ 67 case AVDTP_SI_GET_CAPABILITIES: 68 case AVDTP_SI_GET_ALL_CAPABILITIES: 69 case AVDTP_SI_SET_CONFIGURATION: 70 case AVDTP_SI_GET_CONFIGURATION: 71 case AVDTP_SI_START: 72 case AVDTP_SI_CLOSE: 73 case AVDTP_SI_ABORT: 74 case AVDTP_SI_RECONFIGURE: 75 case AVDTP_SI_OPEN: 76 minimal_msg_lenght = 3; 77 break; 78 default: 79 break; 80 } 81 return msg_size >= minimal_msg_lenght; 82 } 83 84 void avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, uint8_t * packet, uint16_t size, int offset, avdtp_context_t * context){ 85 avdtp_stream_endpoint_t * stream_endpoint; 86 connection->acceptor_transaction_label = connection->signaling_packet.transaction_label; 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 // handle error cases 96 switch (connection->signaling_packet.signal_identifier){ 97 case AVDTP_SI_DISCOVER: 98 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return; 99 log_info("ACP: AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS"); 100 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS; 101 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 102 return; 103 case AVDTP_SI_GET_CAPABILITIES: 104 case AVDTP_SI_GET_ALL_CAPABILITIES: 105 case AVDTP_SI_SET_CONFIGURATION: 106 case AVDTP_SI_GET_CONFIGURATION: 107 case AVDTP_SI_START: 108 case AVDTP_SI_CLOSE: 109 case AVDTP_SI_ABORT: 110 case AVDTP_SI_OPEN: 111 case AVDTP_SI_RECONFIGURE: 112 case AVDTP_SI_DELAYREPORT: 113 connection->local_seid = packet[offset++] >> 2; 114 stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context); 115 if (!stream_endpoint){ 116 log_info("ACP: cmd %d - RESPONSE REJECT", connection->signaling_packet.signal_identifier); 117 connection->error_code = BAD_ACP_SEID; 118 if (connection->signaling_packet.signal_identifier == AVDTP_SI_OPEN){ 119 connection->error_code = BAD_STATE; 120 } 121 122 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE; 123 if (connection->signaling_packet.signal_identifier == AVDTP_SI_RECONFIGURE){ 124 connection->reject_service_category = connection->local_seid; 125 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 126 } 127 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 128 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 129 return; 130 } 131 break; 132 133 case AVDTP_SI_SUSPEND:{ 134 int i; 135 log_info("ACP: AVDTP_SI_SUSPEND seids: "); 136 connection->num_suspended_seids = 0; 137 138 for (i = offset; i < size; i++){ 139 connection->suspended_seids[connection->num_suspended_seids] = packet[i] >> 2; 140 offset++; 141 log_info("%d, ", connection->suspended_seids[connection->num_suspended_seids]); 142 connection->num_suspended_seids++; 143 } 144 145 if (connection->num_suspended_seids == 0) { 146 log_info("ACP: CATEGORY RESPONSE REJECT BAD_ACP_SEID"); 147 connection->error_code = BAD_ACP_SEID; 148 connection->reject_service_category = connection->local_seid; 149 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 150 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 151 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 152 return; 153 } 154 // deal with first susspended seid 155 connection->local_seid = connection->suspended_seids[0]; 156 stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context); 157 if (!stream_endpoint){ 158 log_info("ACP: stream_endpoint not found, CATEGORY RESPONSE REJECT BAD_ACP_SEID"); 159 connection->error_code = BAD_ACP_SEID; 160 connection->reject_service_category = connection->local_seid; 161 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 162 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 163 connection->num_suspended_seids = 0; 164 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 165 return; 166 } 167 break; 168 } 169 default: 170 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE; 171 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 172 log_info("AVDTP_CMD_MSG signal %d not implemented, general reject", connection->signaling_packet.signal_identifier); 173 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 174 return; 175 } 176 177 if (!stream_endpoint) { 178 return; 179 } 180 181 if (!avdtp_acceptor_process_chunk(&connection->signaling_packet, packet, size)) return; 182 183 uint16_t packet_size = connection->signaling_packet.size; 184 connection->signaling_packet.size = 0; 185 186 int request_to_send = 1; 187 switch (stream_endpoint->acceptor_config_state){ 188 case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE: 189 switch (connection->signaling_packet.signal_identifier){ 190 case AVDTP_SI_DELAYREPORT: 191 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT"); 192 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT; 193 avdtp_signaling_emit_delay(context->avdtp_callback, connection->avdtp_cid, connection->local_seid, big_endian_read_16(packet, offset)); 194 break; 195 196 case AVDTP_SI_GET_ALL_CAPABILITIES: 197 log_info("ACP: AVDTP_SI_GET_ALL_CAPABILITIES"); 198 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES; 199 break; 200 case AVDTP_SI_GET_CAPABILITIES: 201 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES"); 202 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES; 203 break; 204 case AVDTP_SI_SET_CONFIGURATION:{ 205 // log_info("acceptor SM received SET_CONFIGURATION cmd: role is_initiator %d", connection->is_initiator); 206 if (connection->is_initiator){ 207 if (connection->is_configuration_initiated_locally){ 208 log_info("ACP: Set configuration already initiated locally, reject cmd "); 209 // fire configuration parsing errors 210 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 211 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD; 212 connection->is_initiator = 1; 213 break; 214 } 215 connection->is_initiator = 0; 216 log_info("acceptor SM received SET_CONFIGURATION cmd: change role to acceptor, is_initiator %d", connection->is_initiator); 217 } 218 219 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION connection %p", connection); 220 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE; 221 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION; 222 connection->reject_service_category = 0; 223 stream_endpoint->connection = connection; 224 avdtp_sep_t sep; 225 sep.seid = connection->signaling_packet.command[offset++] >> 2; 226 sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset); 227 sep.in_use = 1; 228 229 if (connection->error_code){ 230 log_info("fire configuration parsing errors "); 231 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 232 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 233 break; 234 } 235 // find or add sep 236 237 log_info("ACP .. seid %d, remote sep seid %d", sep.seid, stream_endpoint->remote_sep.seid); 238 239 if (is_avdtp_remote_seid_registered(stream_endpoint)){ 240 if (stream_endpoint->remote_sep.in_use){ 241 log_info("reject as it is already in use"); 242 connection->error_code = SEP_IN_USE; 243 // find first registered category and fire the error 244 connection->reject_service_category = 0; 245 int i; 246 for (i = 1; i < 9; i++){ 247 if (get_bit16(sep.configured_service_categories, i)){ 248 connection->reject_service_category = i; 249 break; 250 } 251 } 252 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 253 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 254 } else { 255 stream_endpoint->remote_sep = sep; 256 log_info("ACP: update seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint); 257 } 258 } else { 259 // add new 260 log_info("ACP: seid %d not found in %p", sep.seid, stream_endpoint); 261 stream_endpoint->remote_sep = sep; 262 log_info("ACP: add seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint); 263 } 264 265 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); 266 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 267 break; 268 } 269 case AVDTP_SI_RECONFIGURE:{ 270 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE; 271 connection->reject_service_category = 0; 272 273 avdtp_sep_t sep; 274 sep.seid = connection->local_seid; 275 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE seid %d", sep.seid); 276 sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset); 277 if (connection->error_code){ 278 // fire configuration parsing errors 279 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier; 280 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE; 281 break; 282 } 283 284 // find sep or raise error 285 if (!is_avdtp_remote_seid_registered(stream_endpoint)){ 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->remote_sep = sep; 293 log_info("ACP: update seid %d, to %p", stream_endpoint->remote_sep.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, state %d", stream_endpoint->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_DELAY_REPORT: 511 log_info("ACP: DONE "); 512 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_DELAYREPORT); 513 break; 514 case AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES: 515 avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES); 516 l2cap_reserve_packet_buffer(); 517 out_buffer = l2cap_get_outgoing_buffer(); 518 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 519 if ((connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->signaling_packet.packet_type != AVDTP_END_PACKET)){ 520 stream_endpoint->acceptor_config_state = acceptor_config_state; 521 log_info("ACP: fragmented"); 522 } else { 523 log_info("ACP:DONE"); 524 } 525 l2cap_send_prepared(cid, pos); 526 break; 527 case AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION: 528 log_info("ACP: DONE"); 529 log_info(" -> AVDTP_STREAM_ENDPOINT_CONFIGURED"); 530 stream_endpoint->connection = connection; 531 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED; 532 // TODO: consider reconfiguration 533 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION); 534 break; 535 case AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE: 536 log_info("ACP: DONE "); 537 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE); 538 break; 539 540 case AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION:{ 541 avdtp_sep_t sep = stream_endpoint->remote_sep; 542 avdtp_prepare_capabilities(&connection->signaling_packet, trid, sep.configured_service_categories, sep.configuration, AVDTP_SI_GET_CONFIGURATION); 543 l2cap_reserve_packet_buffer(); 544 out_buffer = l2cap_get_outgoing_buffer(); 545 pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer); 546 if ((connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->signaling_packet.packet_type != AVDTP_END_PACKET)){ 547 stream_endpoint->acceptor_config_state = acceptor_config_state; 548 log_info("ACP: fragmented"); 549 } else { 550 log_info("ACP:DONE"); 551 } 552 l2cap_send_prepared(cid, pos); 553 break; 554 } 555 case AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM: 556 log_info("ACP: DONE"); 557 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN); 558 break; 559 case AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM: 560 log_info("ACP: DONE "); 561 log_info(" -> AVDTP_STREAM_ENDPOINT_STREAMING "); 562 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING; 563 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START); 564 break; 565 case AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM: 566 log_info("ACP: DONE"); 567 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE); 568 break; 569 case AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM: 570 log_info("ACP: DONE"); 571 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT); 572 break; 573 case AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM: 574 log_info("ACP: DONE"); 575 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 576 avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND); 577 break; 578 case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD: 579 log_info("ACP: DONE REJECT"); 580 connection->reject_signal_identifier = AVDTP_SI_NONE; 581 avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid); 582 break; 583 case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE: 584 log_info("ACP: DONE REJECT CATEGORY"); 585 connection->reject_service_category = 0; 586 avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid); 587 break; 588 case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE: 589 log_info("ACP: DONE REJECT"); 590 connection->reject_signal_identifier = AVDTP_SI_NONE; 591 connection->error_code = 0; 592 avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid); 593 break; 594 default: 595 log_info("ACP: NOT IMPLEMENTED"); 596 sent = 0; 597 break; 598 } 599 avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier); 600 // check fragmentation 601 if ((connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->signaling_packet.packet_type != AVDTP_END_PACKET)){ 602 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid); 603 } 604 } 605