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