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