diff --git a/test/avdtp/avdtp.h b/test/avdtp/avdtp.h index 81438a8cf..fe6c697f5 100644 --- a/test/avdtp/avdtp.h +++ b/test/avdtp/avdtp.h @@ -279,18 +279,17 @@ typedef enum { } avdtp_service_mode_t; typedef enum { - AVDTP_IDLE, - AVDTP_CONFIGURATION_SUBSTATEMACHINE, - AVDTP_CONFIGURED, - AVDTP_W2_ANSWER_OPEN_STREAM, - AVDTP_W4_L2CAP_FOR_MEDIA_CONNECTED, - AVDTP_OPEN, // 5 - AVDTP_W2_ANSWER_START_SINGLE_STREAM, - AVDTP_W4_STREAMING_CONNECTION_OPEN, - AVDTP_STREAMING, // 8 - AVDTP_CLOSING, - AVDTP_ABORTING, - AVDTP_W4_L2CAP_FOR_MEDIA_DISCONNECTED + AVDTP_STREAM_ENDPOINT_IDLE, + AVDTP_STREAM_ENDPOINT_CONFIGURED, + AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM, + AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED, + AVDTP_STREAM_ENDPOINT_OPENED, // 5 + AVDTP_STREAM_ENDPOINT_W2_ANSWER_START_STREAM, + AVDTP_STREAM_ENDPOINT_W4_STREAMING_CONNECTION_OPEN, + AVDTP_STREAM_ENDPOINT_STREAMING, // 8 + AVDTP_STREAM_ENDPOINT_CLOSING, + AVDTP_STREAM_ENDPOINT_ABORTING, + AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED } avdtp_stream_endpoint_state_t; typedef enum { @@ -332,7 +331,9 @@ typedef struct { typedef enum { AVDTP_SIGNALING_CONNECTION_IDLE, AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED, - AVDTP_SIGNALING_CONNECTION_OPENED, + AVDTP_SIGNALING_CONNECTION_CONFIGURATION_SUBSTATEMACHINE, + AVDTP_SIGNALING_CONNECTION_CONFIGURED, + AVDTP_SIGNALING_CONNECTION_STREAM_ENPOINT_IN_USE, AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED } avdtp_connection_state_t; @@ -346,17 +347,18 @@ typedef struct { avdtp_connection_state_t state; avdtp_service_mode_t service_mode; + avdtp_initiator_stream_config_state_t initiator_config_state; + avdtp_acceptor_stream_config_state_t acceptor_config_state; + uint8_t disconnect; uint8_t initiator_transaction_label; uint8_t acceptor_transaction_label; uint8_t query_seid; - + avdtp_signal_identifier_t unknown_signal_identifier; + btstack_linked_list_t can_send_now_signaling_channel_requests; } avdtp_connection_t; -typedef struct { - -} avdtp_stream_endpoint_context_t; typedef struct avdtp_stream_endpoint { btstack_linked_item_t item; @@ -367,16 +369,12 @@ typedef struct avdtp_stream_endpoint { uint16_t l2cap_recovery_cid; avdtp_stream_endpoint_state_t state; - avdtp_initiator_stream_config_state_t initiator_config_state; - avdtp_acceptor_stream_config_state_t acceptor_config_state; - // active connection avdtp_connection_t * connection; // store remote seps avdtp_sep_t remote_seps[MAX_NUM_SEPS]; uint8_t remote_seps_num; - avdtp_signal_identifier_t unknown_signal_identifier; // currently active remote seid uint8_t remote_sep_index; // register request for L2cap connection release diff --git a/test/avdtp/avdtp_acceptor.c b/test/avdtp/avdtp_acceptor.c index 3747f13e5..1d6478c33 100644 --- a/test/avdtp/avdtp_acceptor.c +++ b/test/avdtp/avdtp_acceptor.c @@ -190,8 +190,8 @@ static inline int avdtp_acceptor_send_capabilities(uint16_t cid, uint8_t transac command[pos++] = 0x02; command[pos++] = 0x00; - printf(" avdtp_acceptor_send_capabilities_response: \n"); - printf_hexdump(command, pos); + // printf(" avdtp_acceptor_send_capabilities_response: \n"); + // printf_hexdump(command, pos); return l2cap_send(cid, command, pos); } @@ -203,15 +203,6 @@ static int avdtp_acceptor_send_all_capabilities_response(uint16_t cid, uint8_t t return avdtp_acceptor_send_capabilities(cid, transaction_label, sep, 1); } -// static int avdtp_acceptor_send_configuration_reject_response(uint16_t cid, uint8_t transaction_label){ -// uint8_t command[3]; -// command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG); -// command[1] = (uint8_t)AVDTP_SET_CONFIGURATION; -// command[2] = 0x00; -// // TODO: add reason for reject AVDTP 8.9 -// return l2cap_send(cid, command, sizeof(command)); -// } - int avdtp_acceptor_send_accept_response(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ uint8_t command[2]; command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG); @@ -219,48 +210,65 @@ int avdtp_acceptor_send_accept_response(uint16_t cid, avdtp_signal_identifier_t return l2cap_send(cid, command, sizeof(command)); } -void avdtp_acceptor_stream_config_subsm_init(avdtp_stream_endpoint_t * stream_endpoint){ - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; +void avdtp_acceptor_stream_config_subsm_init(avdtp_connection_t * connection){ + connection->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; } -int avdtp_acceptor_stream_config_subsm_is_done(avdtp_stream_endpoint_t * stream_endpoint){ - return stream_endpoint->acceptor_config_state == AVDTP_ACCEPTOR_STREAM_CONFIG_DONE; +int avdtp_acceptor_stream_config_subsm_is_done(avdtp_connection_t * connection){ + return connection->acceptor_config_state == AVDTP_ACCEPTOR_STREAM_CONFIG_DONE; } -int avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size){ +static avdtp_stream_endpoint_t * get_avdtp_stream_endpoint_for_active_seid(uint8_t seid){ + btstack_linked_list_iterator_t it; + btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints); + while (btstack_linked_list_iterator_has_next(&it)){ + avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); + if (stream_endpoint->sep.seid == seid){ + return stream_endpoint; + } + } + return NULL; +} + +int avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, uint8_t *packet, uint16_t size){ + if (!connection) return 0; + avdtp_signaling_packet_header_t signaling_header; avdtp_read_signaling_header(&signaling_header, packet, size); // printf("SIGNALING HEADER: tr_label %d, packet_type %d, msg_type %d, signal_identifier %02x\n", // signaling_header.transaction_label, signaling_header.packet_type, signaling_header.message_type, signaling_header.signal_identifier); - printf("acceptor sm: state %d, si %d\n", stream_endpoint->acceptor_config_state, signaling_header.signal_identifier); + // printf("acceptor sm: state %d, si %d\n", connection->acceptor_config_state, signaling_header.signal_identifier); int i = 2; avdtp_sep_t sep; connection->acceptor_transaction_label = signaling_header.transaction_label; - + avdtp_stream_endpoint_t * stream_endpoint = NULL; int request_to_send = 1; - switch (stream_endpoint->acceptor_config_state){ + switch (connection->acceptor_config_state){ case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE: switch (signaling_header.signal_identifier){ case AVDTP_SI_DISCOVER: printf(" ACP -> AVDTP_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS\n"); - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS; + connection->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS; break; case AVDTP_SI_GET_ALL_CAPABILITIES: printf(" ACP -> AVDTP_SI_GET_ALL_CAPABILITIES\n"); - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES; + connection->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES; break; case AVDTP_SI_GET_CAPABILITIES: printf(" ACP -> AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES\n"); connection->query_seid = packet[2] >> 2; - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES; + connection->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES; break; case AVDTP_SI_SET_CONFIGURATION: - printf(" ACP -> AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION\n"); + printf("AVDTP_SI_SET_CONFIGURATION \n"); connection->query_seid = packet[2] >> 2; sep.seid = packet[3] >> 2; + + stream_endpoint = get_avdtp_stream_endpoint_for_active_seid(connection->query_seid); + if (!stream_endpoint) return 0; // find or add sep for (i=0; i < stream_endpoint->remote_seps_num; i++){ if (stream_endpoint->remote_seps[i].seid == sep.seid){ @@ -269,18 +277,18 @@ int avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, avdtp_st } sep.registered_service_categories = avdtp_unpack_service_capabilities(&sep.capabilities, packet+4, size-4); stream_endpoint->remote_seps[stream_endpoint->remote_sep_index] = sep; - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION; + connection->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION; break; default: printf(" ACP -> NOT IMPLEMENTED, Reject signal_identifier %02x\n", signaling_header.signal_identifier); - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD; - stream_endpoint->unknown_signal_identifier = signaling_header.signal_identifier; + connection->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD; + connection->unknown_signal_identifier = signaling_header.signal_identifier; break; } - l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); break; case AVDTP_ACCEPTOR_STREAM_CONFIG_DONE: + request_to_send = 0; printf(" ACP: AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION\n"); break; default: @@ -291,6 +299,7 @@ int avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, avdtp_st return request_to_send; } + static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ uint8_t command[2]; command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG); @@ -298,34 +307,40 @@ static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_ident return l2cap_send(cid, command, sizeof(command)); } -int avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint){ +int avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection){ int sent = 1; - // printf("acceptor run: state %d\n", stream_endpoint->acceptor_config_state); - switch (stream_endpoint->acceptor_config_state){ + avdtp_stream_endpoint_t * stream_endpoint = NULL; + switch (connection->acceptor_config_state){ case AVDTP_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS: printf(" AVDTP_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS -> AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE\n"); - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; + connection->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *)&stream_endpoints); break; case AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES: - if (connection->query_seid != stream_endpoint->sep.seid) return 0; + stream_endpoint = get_avdtp_stream_endpoint_for_active_seid(connection->query_seid); + if (!stream_endpoint) return 0; printf(" AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES -> AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE\n"); - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; + connection->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; avdtp_acceptor_send_capabilities_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, stream_endpoint->sep); break; case AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES: - if (connection->query_seid != stream_endpoint->sep.seid) return 0; + stream_endpoint = get_avdtp_stream_endpoint_for_active_seid(connection->query_seid); + if (!stream_endpoint) return 0;connection->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; printf(" AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES -> AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE\n"); - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; avdtp_acceptor_send_all_capabilities_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, stream_endpoint->sep); break; case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD: - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; - avdtp_acceptor_send_response_reject(connection->l2cap_signaling_cid, stream_endpoint->unknown_signal_identifier, connection->acceptor_transaction_label); + connection->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; + avdtp_acceptor_send_response_reject(connection->l2cap_signaling_cid, connection->unknown_signal_identifier, connection->acceptor_transaction_label); break; case AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION: + printf(" ... AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION seid\n"); + stream_endpoint = get_avdtp_stream_endpoint_for_active_seid(connection->query_seid); + if (!stream_endpoint) return 0; printf(" AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION -> AVDTP_ACCEPTOR_STREAM_CONFIG_DONE\n"); - stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_DONE; + connection->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_DONE; + stream_endpoint->connection = connection; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED; avdtp_acceptor_send_accept_response(connection->l2cap_signaling_cid, AVDTP_SI_SET_CONFIGURATION, connection->acceptor_transaction_label); break; case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE: diff --git a/test/avdtp/avdtp_acceptor.h b/test/avdtp/avdtp_acceptor.h index 88d78738f..3102ca7ca 100644 --- a/test/avdtp/avdtp_acceptor.h +++ b/test/avdtp/avdtp_acceptor.h @@ -51,10 +51,10 @@ extern "C" { #endif -void avdtp_acceptor_stream_config_subsm_init(avdtp_stream_endpoint_t * stream_endpoint); -int avdtp_acceptor_stream_config_subsm_is_done(avdtp_stream_endpoint_t * stream_endpoint); -int avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size); -int avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint); +void avdtp_acceptor_stream_config_subsm_init(avdtp_connection_t * connection); +int avdtp_acceptor_stream_config_subsm_is_done(avdtp_connection_t * connection); +int avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, uint8_t *packet, uint16_t size); +int avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection); int avdtp_acceptor_send_accept_response(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label); #if defined __cplusplus diff --git a/test/avdtp/avdtp_initiator.c b/test/avdtp/avdtp_initiator.c index cb228b2e7..99a542691 100644 --- a/test/avdtp/avdtp_initiator.c +++ b/test/avdtp/avdtp_initiator.c @@ -47,125 +47,125 @@ #include "avdtp_util.h" #include "avdtp_initiator.h" -static int avdtp_initiator_send_signaling_cmd(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ - uint8_t command[2]; - command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG); - command[1] = (uint8_t)identifier; - return l2cap_send(cid, command, sizeof(command)); +// static int avdtp_initiator_send_signaling_cmd(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){ +// uint8_t command[2]; +// command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG); +// command[1] = (uint8_t)identifier; +// return l2cap_send(cid, command, sizeof(command)); +// } + +// static int avdtp_initiator_send_get_all_capabilities_cmd(uint16_t cid, uint8_t transaction_label, uint8_t sep_id){ +// uint8_t command[3]; +// command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG); +// command[1] = AVDTP_SI_GET_ALL_CAPABILITIES; +// command[2] = sep_id << 2; +// return l2cap_send(cid, command, sizeof(command)); +// } + +// static int avdtp_initiator_send_get_capabilities_cmd(uint16_t cid, uint8_t transaction_label, uint8_t sep_id){ +// uint8_t command[3]; +// command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG); +// command[1] = AVDTP_SI_GET_CAPABILITIES; +// command[2] = sep_id << 2; +// return l2cap_send(cid, command, sizeof(command)); +// } + +void avdtp_initiator_stream_config_subsm_init(avdtp_connection_t * connection){ + connection->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE; } -static int avdtp_initiator_send_get_all_capabilities_cmd(uint16_t cid, uint8_t transaction_label, uint8_t sep_id){ - uint8_t command[3]; - command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG); - command[1] = AVDTP_SI_GET_ALL_CAPABILITIES; - command[2] = sep_id << 2; - return l2cap_send(cid, command, sizeof(command)); +int avdtp_initiator_stream_config_subsm_is_done(avdtp_connection_t * connection){ + return 1; //connection->initiator_config_state == AVDTP_INITIATOR_STREAM_CONFIG_DONE; } -static int avdtp_initiator_send_get_capabilities_cmd(uint16_t cid, uint8_t transaction_label, uint8_t sep_id){ - uint8_t command[3]; - command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_CMD_MSG); - command[1] = AVDTP_SI_GET_CAPABILITIES; - command[2] = sep_id << 2; - return l2cap_send(cid, command, sizeof(command)); -} - -void avdtp_initiator_stream_config_subsm_init(avdtp_stream_endpoint_t * stream_endpoint){ - stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE; -} - -int avdtp_initiator_stream_config_subsm_is_done(avdtp_stream_endpoint_t * stream_endpoint){ - return stream_endpoint->initiator_config_state == AVDTP_INITIATOR_STREAM_CONFIG_DONE; -} - -int avdtp_initiator_stream_config_subsm(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size){ +int avdtp_initiator_stream_config_subsm(avdtp_connection_t * connection, uint8_t *packet, uint16_t size){ return 0; - if (avdtp_initiator_stream_config_subsm_run(connection, stream_endpoint)) return 1; - int i; - int responded = 1; - avdtp_sep_t sep; + // if (avdtp_initiator_stream_config_subsm_run(connection, stream_endpoint)) return 1; + // int i; + // int responded = 1; + // avdtp_sep_t sep; - avdtp_signaling_packet_header_t signaling_header; - avdtp_read_signaling_header(&signaling_header, packet, size); + // avdtp_signaling_packet_header_t signaling_header; + // avdtp_read_signaling_header(&signaling_header, packet, size); - switch (stream_endpoint->initiator_config_state){ - case AVDTP_INITIATOR_W4_SEPS_DISCOVERED: - printf(" AVDTP_INITIATOR_W4_SEPS_DISCOVERED -> AVDTP_INITIATOR_W2_GET_CAPABILITIES\n"); + // switch (connection->initiator_config_state){ + // case AVDTP_INITIATOR_W4_SEPS_DISCOVERED: + // printf(" AVDTP_INITIATOR_W4_SEPS_DISCOVERED -> AVDTP_INITIATOR_W2_GET_CAPABILITIES\n"); - if (signaling_header.transaction_label != connection->initiator_transaction_label){ - printf("unexpected transaction label, got %d, expected %d\n", signaling_header.transaction_label, connection->initiator_transaction_label); - return 0; - } - if (signaling_header.signal_identifier != AVDTP_SI_DISCOVER) { - printf("unexpected signal identifier ...\n"); - return 0; - } - if (signaling_header.message_type != AVDTP_RESPONSE_ACCEPT_MSG){ - printf("request rejected...\n"); - return 0; - } - if (size == 3){ - printf("ERROR code %02x\n", packet[2]); - return 0; - } - for (i = 2; i> 2; - if (sep.seid < 0x01 || sep.seid > 0x3E){ - printf("invalid sep id\n"); - return 0; - } - sep.in_use = (packet[i] >> 1) & 0x01; - sep.media_type = (avdtp_media_type_t)(packet[i+1] >> 4); - sep.type = (avdtp_sep_type_t)((packet[i+1] >> 3) & 0x01); - stream_endpoint->remote_seps[stream_endpoint->remote_seps_num++] = sep; - // printf("found sep: seid %u, in_use %d, media type %d, sep type %d (1-SNK)\n", - // sep.seid, sep.in_use, sep.media_type, sep.type); - } - stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_GET_CAPABILITIES; - connection->initiator_transaction_label++; - l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); - responded = 1; - break; - case AVDTP_INITIATOR_W4_CAPABILITIES: - printf(" Received basic capabilities -> NOT IMPLEMENTED\n"); - responded = 0; - break; - case AVDTP_INITIATOR_W4_ALL_CAPABILITIES: - printf(" Received all capabilities -> NOT IMPLEMENTED\n"); - responded = 0; - break; - default: - printf(" INT : NOT IMPLEMENTED sig. ID %02x\n", signaling_header.signal_identifier); - //printf_hexdump( packet, size ); - responded = 0; - break; - } - return responded; + // if (signaling_header.transaction_label != connection->initiator_transaction_label){ + // printf("unexpected transaction label, got %d, expected %d\n", signaling_header.transaction_label, connection->initiator_transaction_label); + // return 0; + // } + // if (signaling_header.signal_identifier != AVDTP_SI_DISCOVER) { + // printf("unexpected signal identifier ...\n"); + // return 0; + // } + // if (signaling_header.message_type != AVDTP_RESPONSE_ACCEPT_MSG){ + // printf("request rejected...\n"); + // return 0; + // } + // if (size == 3){ + // printf("ERROR code %02x\n", packet[2]); + // return 0; + // } + // for (i = 2; i> 2; + // if (sep.seid < 0x01 || sep.seid > 0x3E){ + // printf("invalid sep id\n"); + // return 0; + // } + // sep.in_use = (packet[i] >> 1) & 0x01; + // sep.media_type = (avdtp_media_type_t)(packet[i+1] >> 4); + // sep.type = (avdtp_sep_type_t)((packet[i+1] >> 3) & 0x01); + // stream_endpoint->remote_seps[stream_endpoint->remote_seps_num++] = sep; + // // printf("found sep: seid %u, in_use %d, media type %d, sep type %d (1-SNK)\n", + // // sep.seid, sep.in_use, sep.media_type, sep.type); + // } + // connection->initiator_config_state = AVDTP_INITIATOR_W2_GET_CAPABILITIES; + // connection->initiator_transaction_label++; + // l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); + // responded = 1; + // break; + // case AVDTP_INITIATOR_W4_CAPABILITIES: + // printf(" Received basic capabilities -> NOT IMPLEMENTED\n"); + // responded = 0; + // break; + // case AVDTP_INITIATOR_W4_ALL_CAPABILITIES: + // printf(" Received all capabilities -> NOT IMPLEMENTED\n"); + // responded = 0; + // break; + // default: + // printf(" INT : NOT IMPLEMENTED sig. ID %02x\n", signaling_header.signal_identifier); + // //printf_hexdump( packet, size ); + // responded = 0; + // break; + // } + // return responded; } -int avdtp_initiator_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint){ +int avdtp_initiator_stream_config_subsm_run(avdtp_connection_t * connection){ return 0; - int sent = 1; - switch (stream_endpoint->initiator_config_state){ - case AVDTP_INITIATOR_STREAM_CONFIG_IDLE: - case AVDTP_INITIATOR_W2_DISCOVER_SEPS: - printf(" AVDTP_INITIATOR_STREAM_CONFIG_IDLE | AVDTP_INITIATOR_W2_DISCOVER_SEPS -> AVDTP_INITIATOR_W4_SEPS_DISCOVERED\n"); - stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W4_SEPS_DISCOVERED; - avdtp_initiator_send_signaling_cmd(connection->l2cap_signaling_cid, AVDTP_SI_DISCOVER, connection->initiator_transaction_label); - break; - case AVDTP_INITIATOR_W2_GET_CAPABILITIES: - printf(" AVDTP_INITIATOR_W2_GET_CAPABILITIES -> AVDTP_INITIATOR_W4_CAPABILITIES\n"); - stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W4_CAPABILITIES; - avdtp_initiator_send_get_capabilities_cmd(connection->l2cap_signaling_cid, connection->initiator_transaction_label, connection->query_seid); - break; - case AVDTP_INITIATOR_W2_GET_ALL_CAPABILITIES: - printf(" AVDTP_INITIATOR_W2_GET_ALL_CAPABILITIES -> AVDTP_INITIATOR_W4_ALL_CAPABILITIES\n"); - stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W4_ALL_CAPABILITIES; - avdtp_initiator_send_get_all_capabilities_cmd(connection->l2cap_signaling_cid, connection->initiator_transaction_label, connection->query_seid); - break; - default: - sent = 0; - break; - } - return sent; + // int sent = 1; + // switch (connection->initiator_config_state){ + // case AVDTP_INITIATOR_STREAM_CONFIG_IDLE: + // case AVDTP_INITIATOR_W2_DISCOVER_SEPS: + // printf(" AVDTP_INITIATOR_STREAM_CONFIG_IDLE | AVDTP_INITIATOR_W2_DISCOVER_SEPS -> AVDTP_INITIATOR_W4_SEPS_DISCOVERED\n"); + // connection->initiator_config_state = AVDTP_INITIATOR_W4_SEPS_DISCOVERED; + // avdtp_initiator_send_signaling_cmd(connection->l2cap_signaling_cid, AVDTP_SI_DISCOVER, connection->initiator_transaction_label); + // break; + // case AVDTP_INITIATOR_W2_GET_CAPABILITIES: + // printf(" AVDTP_INITIATOR_W2_GET_CAPABILITIES -> AVDTP_INITIATOR_W4_CAPABILITIES\n"); + // connection->initiator_config_state = AVDTP_INITIATOR_W4_CAPABILITIES; + // avdtp_initiator_send_get_capabilities_cmd(connection->l2cap_signaling_cid, connection->initiator_transaction_label, connection->query_seid); + // break; + // case AVDTP_INITIATOR_W2_GET_ALL_CAPABILITIES: + // printf(" AVDTP_INITIATOR_W2_GET_ALL_CAPABILITIES -> AVDTP_INITIATOR_W4_ALL_CAPABILITIES\n"); + // connection->initiator_config_state = AVDTP_INITIATOR_W4_ALL_CAPABILITIES; + // avdtp_initiator_send_get_all_capabilities_cmd(connection->l2cap_signaling_cid, connection->initiator_transaction_label, connection->query_seid); + // break; + // default: + // sent = 0; + // break; + // } + // return sent; } diff --git a/test/avdtp/avdtp_initiator.h b/test/avdtp/avdtp_initiator.h index e795f3cb6..70a049e4c 100644 --- a/test/avdtp/avdtp_initiator.h +++ b/test/avdtp/avdtp_initiator.h @@ -51,10 +51,10 @@ extern "C" { #endif -void avdtp_initiator_stream_config_subsm_init(avdtp_stream_endpoint_t * stream_endpoint); -int avdtp_initiator_stream_config_subsm_is_done(avdtp_stream_endpoint_t * stream_endpoint); -int avdtp_initiator_stream_config_subsm(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size); -int avdtp_initiator_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint); +void avdtp_initiator_stream_config_subsm_init(avdtp_connection_t * connection); +int avdtp_initiator_stream_config_subsm_is_done(avdtp_connection_t * connection); +int avdtp_initiator_stream_config_subsm(avdtp_connection_t * connection, uint8_t *packet, uint16_t size); +int avdtp_initiator_stream_config_subsm_run(avdtp_connection_t * connection); #if defined __cplusplus } diff --git a/test/avdtp/avdtp_sink.c b/test/avdtp/avdtp_sink.c index afa9ed022..66495a5a4 100644 --- a/test/avdtp/avdtp_sink.c +++ b/test/avdtp/avdtp_sink.c @@ -70,26 +70,26 @@ static btstack_linked_list_t * avdtp_sink_get_clients_waiting_for_can_send(uint1 return clients; } -// TODO list of connections -static void avdtp_sink_handle_can_send_now(uint16_t l2cap_cid, avdtp_connection_t * connection){ - btstack_linked_list_t * clients = avdtp_sink_get_clients_waiting_for_can_send(l2cap_cid, connection); - if (!clients) return; +// // TODO list of connections +// static void avdtp_sink_handle_can_send_now(uint16_t l2cap_cid, avdtp_connection_t * connection){ +// btstack_linked_list_t * clients = avdtp_sink_get_clients_waiting_for_can_send(l2cap_cid, connection); +// if (!clients) return; - while (!btstack_linked_list_empty(clients)){ - // handle first client - btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) clients; - btstack_linked_list_remove(clients, (btstack_linked_item_t *) client); - client->callback(client->context); +// while (!btstack_linked_list_empty(clients)){ +// // handle first client +// btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) *clients; +// btstack_linked_list_remove(clients, (btstack_linked_item_t *) client); +// client->callback(client->context); - // request again if needed - if (!l2cap_can_send_packet_now(l2cap_cid)){ - if (!btstack_linked_list_empty(clients)){ - l2cap_request_can_send_now_event(l2cap_cid); - } - return; - } - } -} +// // request again if needed +// if (!l2cap_can_send_packet_now(l2cap_cid)){ +// if (!btstack_linked_list_empty(clients)){ +// l2cap_request_can_send_now_event(l2cap_cid); +// } +// return; +// } +// } +// } static void avdtp_sink_register_can_send_now_callback(uint16_t l2cap_cid, avdtp_connection_t * connection, btstack_context_callback_registration_t * callback_registration){ btstack_linked_list_t * clients = avdtp_sink_get_clients_waiting_for_can_send(l2cap_cid, connection); @@ -202,22 +202,12 @@ static avdtp_stream_endpoint_t * get_avdtp_stream_endpoint_for_seid(uint16_t sei return NULL; } -static avdtp_stream_endpoint_t * get_avdtp_stream_endpoint_for_connection(avdtp_connection_t * connection){ - btstack_linked_list_iterator_t it; - btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints); - while (btstack_linked_list_iterator_has_next(&it)){ - avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); - if (stream_endpoint->connection == connection){ - return stream_endpoint; - } - } - return NULL; -} -static avdtp_connection_t * avdtp_sink_create_connection(bd_addr_t remote_addr, hci_con_handle_t con_handle, uint16_t local_cid){ + +static avdtp_connection_t * avdtp_sink_create_connection(bd_addr_t remote_addr){ avdtp_connection_t * connection = btstack_memory_avdtp_connection_get(); - connection->l2cap_signaling_cid = local_cid; - connection->con_handle = con_handle; + connection->l2cap_signaling_cid = 0; + connection->con_handle = 0; connection->state = AVDTP_SIGNALING_CONNECTION_IDLE; connection->initiator_transaction_label++; memcpy(connection->remote_addr, remote_addr, 6); @@ -361,7 +351,7 @@ static avdtp_connection_t * get_avdtp_connection_for_con_handle(hci_con_handle_t return NULL; } -static avdtp_connection_t * get_avdtp_connection_for_l2cap_cid(uint16_t l2cap_cid){ +static avdtp_connection_t * get_avdtp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid){ btstack_linked_list_iterator_t it; btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &avdtp_connections); while (btstack_linked_list_iterator_has_next(&it)){ @@ -391,118 +381,99 @@ static avdtp_stream_endpoint_t * get_avdtp_stream_endpoint_for_l2cap_cid(uint16_ } static void avdtp_sink_remove_connection_context(avdtp_stream_endpoint_t * stream_endpoint){ + if (!stream_endpoint) return; btstack_linked_list_remove(&stream_endpoints, (btstack_linked_item_t*) stream_endpoint); } -static void handle_l2cap_signaling_data_packet(avdtp_connection_t * connection, uint8_t *packet, uint16_t size){ + +static void handle_l2cap_signaling_data_packet_for_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size){ + if (!connection) return; if (size < 2) { log_error("l2cap data packet too small"); return; } + int request_to_send = 0; avdtp_signaling_packet_header_t signaling_header; avdtp_read_signaling_header(&signaling_header, packet, size); + printf(" handle_l2cap_signaling_data_packet_for_connection %p \n", connection); - // printf("SIGNALING HEADER: tr_label %d, packet_type %d, msg_type %d, signal_identifier %02x\n", - // signaling_header.transaction_label, signaling_header.packet_type, signaling_header.message_type, signaling_header.signal_identifier); + switch (connection->state){ + case AVDTP_SIGNALING_CONNECTION_CONFIGURATION_SUBSTATEMACHINE: - btstack_linked_list_iterator_t it; - btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints); - - int request_to_send = 0; - while (btstack_linked_list_iterator_has_next(&it) && !request_to_send){ - avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); - if (stream_endpoint->state == AVDTP_CONFIGURATION_SUBSTATEMACHINE){ - if (avdtp_initiator_stream_config_subsm_is_done(stream_endpoint) || avdtp_acceptor_stream_config_subsm_is_done(stream_endpoint)){ - printf("AVDTP_CONFIGURATION_SUBSTATEMACHINE -> AVDTP_CONFIGURED\n"); - stream_endpoint->state = AVDTP_CONFIGURED; - } - } - request_to_send = 1; - switch (stream_endpoint->state){ - case AVDTP_CONFIGURATION_SUBSTATEMACHINE: - if (signaling_header.message_type == AVDTP_CMD_MSG){ - request_to_send = avdtp_acceptor_stream_config_subsm(connection, stream_endpoint, packet, size); - break; - } - request_to_send = avdtp_initiator_stream_config_subsm(connection, stream_endpoint, packet, size); + if (signaling_header.message_type == AVDTP_CMD_MSG){ + request_to_send = avdtp_acceptor_stream_config_subsm(connection, packet, size); break; - case AVDTP_CONFIGURED: - switch (signaling_header.signal_identifier){ - case AVDTP_SI_OPEN: - printf("AVDTP_CONFIGURED -> AVDTP_W2_ANSWER_OPEN_STREAM %d\n", signaling_header.transaction_label); - if (stream_endpoint->sep.seid != packet[2] >> 2) return; - stream_endpoint->state = AVDTP_W2_ANSWER_OPEN_STREAM; - connection->acceptor_transaction_label = signaling_header.transaction_label; - l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); + } + request_to_send = avdtp_initiator_stream_config_subsm(connection, packet, size); + break; + default: + break; + } - btstack_context_callback_registration_t callback_registration; - callback_registration.callback = &avdtp_sink_run_for_connection; - callback_registration.context = (void*)stream_endpoint; - avdtp_sink_register_can_send_now_callback(connection->l2cap_signaling_cid, connection, &callback_registration); - break; - default: - request_to_send = 0; - printf("AVDTP_CONFIGURED -> NOT IMPLEMENTED signal_identifier %d, state %d\n", signaling_header.signal_identifier, stream_endpoint->state); - break; - } - break; - case AVDTP_OPEN: - switch (signaling_header.signal_identifier){ - case AVDTP_SI_START: - printf("AVDTP_OPEN -> AVDTP_W2_ANSWER_START_SINGLE_STREAM\n"); - if (stream_endpoint->sep.seid != packet[2] >> 2) return; - stream_endpoint->sep.in_use = 1; - stream_endpoint->state = AVDTP_W2_ANSWER_START_SINGLE_STREAM; - connection->acceptor_transaction_label = signaling_header.transaction_label; - l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); - break; - default: - request_to_send = 0; - printf("AVDTP_OPEN -> NOT IMPLEMENTED signal_identifier %d\n", signaling_header.signal_identifier); - break; - } - break; - default: - request_to_send = 0; - printf("handle_l2cap_signaling_data_packet: state %d -> NOT IMPLEMENTED signal_identifier %d\n", stream_endpoint->state, signaling_header.signal_identifier); - // printf_hexdump( packet, size ); - break; - } - - } if (request_to_send){ - printf("request_to_send cid %02x\n", connection->l2cap_signaling_cid); l2cap_request_can_send_now_event(connection->l2cap_signaling_cid); + return; } } -static void avdtp_sink_handle_open_connection(bd_addr_t remote_addr, hci_con_handle_t con_handle, uint16_t local_cid){ - avdtp_connection_t * connection = get_avdtp_connection_for_l2cap_cid(local_cid); - if (!connection){ - avdtp_sink_create_connection(remote_addr, con_handle, local_cid); - l2cap_request_can_send_now_event(local_cid); // TODO register can send now +static void handle_l2cap_signaling_data_packet_for_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size){ + // if (stream_endpoint->connection->state < AVDTP_SIGNALING_CONNECTION_CONFIGURED) return; + if (!stream_endpoint) return; + if (!stream_endpoint->connection) return; + + int request_to_send = 0; + avdtp_signaling_packet_header_t signaling_header; + avdtp_read_signaling_header(&signaling_header, packet, size); + printf(" handle_l2cap_signaling_data_packet_for_stream_endpoint %d \n", stream_endpoint->sep.seid); + + switch (stream_endpoint->state){ + case AVDTP_STREAM_ENDPOINT_CONFIGURED: + switch (signaling_header.signal_identifier){ + case AVDTP_SI_OPEN: + printf("AVDTP_CONFIGURED -> AVDTP_W2_ANSWER_OPEN_STREAM %d\n", signaling_header.transaction_label); + if (stream_endpoint->sep.seid != packet[2] >> 2) return; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM; + stream_endpoint->connection->acceptor_transaction_label = signaling_header.transaction_label; + l2cap_request_can_send_now_event(stream_endpoint->connection->l2cap_signaling_cid); + + btstack_context_callback_registration_t callback_registration; + callback_registration.callback = &avdtp_sink_run_for_connection; + callback_registration.context = (void*)stream_endpoint; + + + request_to_send = 1; + avdtp_sink_register_can_send_now_callback(stream_endpoint->connection->l2cap_signaling_cid, stream_endpoint->connection, &callback_registration); + break; + default: + printf("AVDTP_CONFIGURED -> NOT IMPLEMENTED signal_identifier %d, state %d\n", signaling_header.signal_identifier, stream_endpoint->state); + break; + } + break; + case AVDTP_STREAM_ENDPOINT_OPENED: + switch (signaling_header.signal_identifier){ + case AVDTP_SI_START: + printf("AVDTP_OPEN -> AVDTP_W2_ANSWER_START_SINGLE_STREAM\n"); + if (stream_endpoint->sep.seid != packet[2] >> 2) return; + stream_endpoint->sep.in_use = 1; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_ANSWER_START_STREAM; + stream_endpoint->connection->acceptor_transaction_label = signaling_header.transaction_label; + request_to_send = 1; + break; + default: + printf("AVDTP_OPEN -> NOT IMPLEMENTED signal_identifier %d\n", signaling_header.signal_identifier); + break; + } + break; + default: + printf("handle_l2cap_signaling_data_packet: state %d -> NOT IMPLEMENTED signal_identifier %d\n", stream_endpoint->state, signaling_header.signal_identifier); + // printf_hexdump( packet, size ); + break; + } + if (request_to_send){ + l2cap_request_can_send_now_event(stream_endpoint->connection->l2cap_signaling_cid); + return; } - - // if (stream_endpoint->l2cap_media_cid == 0 || stream_endpoint->l2cap_media_cid == local_cid){ - // printf("L2CAP_EVENT_CHANNEL_OPENED Media, state %d \n", stream_endpoint->state); - // if (stream_endpoint->state != AVDTP_W4_L2CAP_FOR_MEDIA_CONNECTED) break; - // stream_endpoint->l2cap_media_cid = local_cid; - // stream_endpoint->state = AVDTP_OPEN; - // break; - // } - - // if (stream_endpoint->l2cap_reporting_cid == 0 || stream_endpoint->l2cap_reporting_cid == local_cid){ - // printf("L2CAP_EVENT_CHANNEL_OPENED reporting, state %d \n", stream_endpoint->state); - // stream_endpoint->l2cap_reporting_cid = local_cid; - // break; - // } - - // if (stream_endpoint->l2cap_recovery_cid == 0 || stream_endpoint->l2cap_recovery_cid == local_cid){ - // printf("L2CAP_EVENT_CHANNEL_OPENED recovery, state %d \n", stream_endpoint->state); - // stream_endpoint->l2cap_recovery_cid = local_cid; - // break; - // } } static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ @@ -515,23 +486,21 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe switch (packet_type) { case L2CAP_DATA_PACKET: - connection = get_avdtp_connection_for_l2cap_cid(channel); - if (!connection){ - stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(channel); - if (!stream_endpoint){ - log_error("avdtp L2CAP_DATA_PACKET: no connection and no stream enpoint for local cid 0x%02x not found", channel); - return; + connection = get_avdtp_connection_for_l2cap_signaling_cid(channel); + if (connection){ + if (connection->state < AVDTP_SIGNALING_CONNECTION_CONFIGURED){ + handle_l2cap_signaling_data_packet_for_connection(connection, packet, size); } + break; } - if (channel == connection->l2cap_signaling_cid){ - handle_l2cap_signaling_data_packet(connection, packet, size); - break; - } - - stream_endpoint = get_avdtp_stream_endpoint_for_connection(connection); + stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(channel); if (!stream_endpoint){ - log_error("avdtp L2CAP_DATA_PACKET: connection for local cid 0x%02x not found", channel); + printf("avdtp L2CAP_DATA_PACKET: no stream enpoint for local cid 0x%02x found", channel); + break; + } + if (channel == stream_endpoint->connection->l2cap_signaling_cid){ + handle_l2cap_signaling_data_packet_for_stream_endpoint(stream_endpoint, packet, size); break; } @@ -552,8 +521,29 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe switch (hci_event_packet_get_type(packet)) { case L2CAP_EVENT_INCOMING_CONNECTION: l2cap_event_incoming_connection_get_address(packet, event_addr); - l2cap_accept_connection(l2cap_event_incoming_connection_get_local_cid(packet)); - break; + local_cid = l2cap_event_incoming_connection_get_local_cid(packet); + + connection = get_avdtp_connection_for_bd_addr(event_addr); + if (!connection){ + connection = avdtp_sink_create_connection(event_addr); + } + + if (connection && channel == connection->l2cap_signaling_cid){ + if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return; + connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; + l2cap_accept_connection(local_cid); + break; + } + + stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(local_cid); + if (stream_endpoint && channel == stream_endpoint->l2cap_media_cid){ + if (stream_endpoint->state > AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) return; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED; + printf("TODO incoming media"); + l2cap_accept_connection(local_cid); + return; + } + return; case L2CAP_EVENT_CHANNEL_OPENED: // inform about new l2cap connection @@ -577,8 +567,30 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe bd_addr_to_str(event_addr), con_handle, psm, local_cid, l2cap_event_channel_opened_get_remote_cid(packet)); if (psm != PSM_AVDTP) break; + + connection = get_avdtp_connection_for_bd_addr(event_addr); + if (!connection){ + log_error("L2CAP_EVENT_CHANNEL_OPENED: connection not found"); + return; + } + if (connection->l2cap_signaling_cid == 0 && connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) { + connection->l2cap_signaling_cid = local_cid; + connection->con_handle = con_handle; + connection->state = AVDTP_SIGNALING_CONNECTION_CONFIGURATION_SUBSTATEMACHINE; + avdtp_acceptor_stream_config_subsm_init(connection); + printf(" -> AVDTP_SIGNALING_CONNECTION_CONFIGURATION_SUBSTATEMACHINE\n"); + // l2cap_request_can_send_now_event(local_cid); + break; + } - avdtp_sink_handle_open_connection(event_addr, con_handle, local_cid); + // stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(local_cid); + // if (stream_endpoint && local_cid == stream_endpoint->l2cap_media_cid){ + // if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) return; + // printf("TODO incoming media"); + // stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; + // l2cap_request_can_send_now_event(local_cid); + // return; + // } break; case L2CAP_EVENT_CHANNEL_CLOSED: @@ -601,14 +613,14 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe if (stream_endpoint->l2cap_media_cid == local_cid){ log_info("L2CAP_EVENT_CHANNEL_CLOSED media cid 0x%0x", local_cid); - stream_endpoint->state = AVDTP_CONFIGURED; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED; stream_endpoint->l2cap_media_cid = 0; break; } if (connection->l2cap_signaling_cid == local_cid){ log_info("L2CAP_EVENT_CHANNEL_CLOSED signaling cid 0x%0x", local_cid); - stream_endpoint->state = AVDTP_IDLE; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE; avdtp_sink_remove_connection_context(stream_endpoint); break; } @@ -617,15 +629,15 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe case HCI_EVENT_DISCONNECTION_COMPLETE: break; case L2CAP_EVENT_CAN_SEND_NOW: - connection = get_avdtp_connection_for_l2cap_cid(channel); - if (!connection) { - stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(channel); - if (!stream_endpoint->connection) break; - connection = stream_endpoint->connection; - break; - } - avdtp_sink_handle_can_send_now(channel, connection); - break; + // connection = get_avdtp_connection_for_l2cap_signaling_cid(channel); + // if (!connection) { + // stream_endpoint = get_avdtp_stream_endpoint_for_l2cap_cid(channel); + // if (!stream_endpoint->connection) break; + // connection = stream_endpoint->connection; + // break; + // } + // avdtp_sink_handle_can_send_now(channel, connection); + // break; default: printf("unknown HCI event type %02x\n", hci_event_packet_get_type(packet)); break; @@ -672,31 +684,52 @@ static void avdtp_sink_run_for_connection(void * context){ l2cap_disconnect(connection->l2cap_signaling_cid, 0); return; } + } static void avdtp_sink_run(void){ btstack_linked_list_iterator_t it; + btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &avdtp_connections); + int sent; + while (btstack_linked_list_iterator_has_next(&it)){ + avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); + // signaling + if (connection->state == AVDTP_SIGNALING_CONNECTION_CONFIGURATION_SUBSTATEMACHINE){ + if (!l2cap_can_send_packet_now(connection->l2cap_signaling_cid)) { + //printf("avdtp_sink_run: request cannot send for 0x%02x\n", connection->l2cap_signaling_cid); + return; + } + sent = avdtp_acceptor_stream_config_subsm_run(connection); + if (avdtp_acceptor_stream_config_subsm_is_done(connection)){ + printf(" -> AVDTP_SIGNALING_CONNECTION_CONFIGURED\n"); + connection->state = AVDTP_SIGNALING_CONNECTION_CONFIGURED; + } + if (sent) return; + } + } + + return; + btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints); while (btstack_linked_list_iterator_has_next(&it)){ avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); if (stream_endpoint->disconnect){ switch (stream_endpoint->state){ - case AVDTP_IDLE: - case AVDTP_CONFIGURATION_SUBSTATEMACHINE: - case AVDTP_CONFIGURED: - case AVDTP_W4_L2CAP_FOR_MEDIA_DISCONNECTED: + case AVDTP_STREAM_ENDPOINT_IDLE: + case AVDTP_STREAM_ENDPOINT_CONFIGURED: + case AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED: stream_endpoint->disconnect = 0; break; - case AVDTP_W2_ANSWER_OPEN_STREAM: + case AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM: stream_endpoint->disconnect = 0; - stream_endpoint->state = AVDTP_CONFIGURED; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED; break; - case AVDTP_W4_L2CAP_FOR_MEDIA_CONNECTED: + case AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED: break; default: stream_endpoint->disconnect = 0; - stream_endpoint->state = AVDTP_W4_L2CAP_FOR_MEDIA_DISCONNECTED; + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED; l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0); return; } @@ -707,40 +740,21 @@ static void avdtp_sink_run(void){ btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) stream_endpoints); while (btstack_linked_list_iterator_has_next(&it)){ avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); - if (stream_endpoint->state < AVDTP_OPEN){ - if (stream_endpoint->state == AVDTP_CONFIGURATION_SUBSTATEMACHINE){ - if (avdtp_initiator_stream_config_subsm_is_done(stream_endpoint) || avdtp_acceptor_stream_config_subsm_is_done(stream_endpoint)){ - printf("AVDTP_CONFIGURATION_SUBSTATEMACHINE -> AVDTP_CONFIGURED\n"); - stream_endpoint->state = AVDTP_CONFIGURED; - } - } - // signaling - if (!l2cap_can_send_packet_now(stream_endpoint->connection->l2cap_signaling_cid)) { - //printf("avdtp_sink_run: request cannot send for 0x%02x\n", connection->l2cap_signaling_cid); - return; - } - - switch (stream_endpoint->state){ - case AVDTP_CONFIGURATION_SUBSTATEMACHINE: - if (!avdtp_acceptor_stream_config_subsm_run(stream_endpoint->connection, stream_endpoint)) { - if (!avdtp_initiator_stream_config_subsm_run(stream_endpoint->connection, stream_endpoint)) break; - } - return; - case AVDTP_W2_ANSWER_OPEN_STREAM: - printf("AVDTP_W2_ANSWER_OPEN_STREAM -> AVDTP_OPEN\n"); - stream_endpoint->state = AVDTP_W4_L2CAP_FOR_MEDIA_CONNECTED; - avdtp_acceptor_send_accept_response(stream_endpoint->connection->l2cap_signaling_cid, AVDTP_SI_OPEN, stream_endpoint->connection->acceptor_transaction_label); - break; - case AVDTP_W2_ANSWER_START_SINGLE_STREAM: - printf("AVDTP_W2_ANSWER_START_SINGLE_STREAM -> AVDTP_W4_STREAMING_CONNECTION_OPEN\n"); - stream_endpoint->state = AVDTP_W4_STREAMING_CONNECTION_OPEN; - avdtp_acceptor_send_accept_response(stream_endpoint->connection->l2cap_signaling_cid, AVDTP_SI_START, stream_endpoint->connection->acceptor_transaction_label); - break; - default: - printf("avdtp_sink_run: state %d -> NOT IMPLEMENTED\n", stream_endpoint->state); - break; - } + switch (stream_endpoint->connection->state){ + case AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM: + printf(" -> AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED\n"); + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED; + avdtp_acceptor_send_accept_response(stream_endpoint->connection->l2cap_signaling_cid, AVDTP_SI_OPEN, stream_endpoint->connection->acceptor_transaction_label); + break; + case AVDTP_STREAM_ENDPOINT_W2_ANSWER_START_STREAM: + printf(" -> AVDTP_STREAM_ENDPOINT_W4_STREAMING_CONNECTION_OPEN \n"); + stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_STREAMING_CONNECTION_OPEN; + avdtp_acceptor_send_accept_response(stream_endpoint->connection->l2cap_signaling_cid, AVDTP_SI_START, stream_endpoint->connection->acceptor_transaction_label); + break; + default: + printf("avdtp_sink_run: state %d -> NOT IMPLEMENTED\n", stream_endpoint->state); + break; } } } @@ -750,7 +764,7 @@ static void avdtp_sink_run(void){ void avdtp_sink_connect(bd_addr_t bd_addr){ avdtp_connection_t * connection = get_avdtp_connection_for_bd_addr(bd_addr); if (!connection){ - connection = avdtp_sink_create_connection(bd_addr,0,0); + connection = avdtp_sink_create_connection(bd_addr); } if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return; connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; @@ -768,9 +782,9 @@ void avdtp_sink_disconnect(uint16_t con_handle){ btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &stream_endpoints); while (btstack_linked_list_iterator_has_next(&it)){ avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); - stream_endpoint->disconnect = stream_endpoint->state != AVDTP_IDLE; + stream_endpoint->disconnect = stream_endpoint->state != AVDTP_STREAM_ENDPOINT_IDLE; } - avdtp_sink_run(); + //avdtp_sink_run(connection); }