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