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