avdtp.c (951d2774cf6daf1d302e906f6127b08e7452001d) avdtp.c (f751daa3e83b8c78f824b3d6cfa8412a4f191539)
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

--- 60 unchanged lines hidden (view full) ---

69static uint16_t initiator_transaction_id_counter = 0;
70
71static int record_id = -1;
72static uint8_t attribute_value[45];
73static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
74
75static void (*avdtp_sink_handle_media_data)(uint8_t local_seid, uint8_t *packet, uint16_t size);
76
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

--- 60 unchanged lines hidden (view full) ---

69static uint16_t initiator_transaction_id_counter = 0;
70
71static int record_id = -1;
72static uint8_t attribute_value[45];
73static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
74
75static void (*avdtp_sink_handle_media_data)(uint8_t local_seid, uint8_t *packet, uint16_t size);
76
77btstack_linked_list_t * avdtp_get_stream_endpoints(void){
78 return &stream_endpoints;
77static uint16_t avdtp_cid_counter = 0;
78
79static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
80
81btstack_packet_handler_t
82avdtp_packet_handler_for_stream_endpoint(const avdtp_stream_endpoint_t *stream_endpoint) {
83 return (stream_endpoint->sep.type == AVDTP_SOURCE) ? avdtp_source_callback : avdtp_sink_callback;
79}
80
84}
85
81// typedef struct {
82// btstack_linked_list_t * avdtp_connections;
83// avdtp_connection_t * connection;
84// btstack_packet_handler_t avdtp_callback;
85// avdtp_sep_type_t query_role;
86// btstack_packet_handler_t packet_handler;
87// uint16_t avdtp_l2cap_psm;
88// uint16_t avdtp_version;
89// uint8_t role_supported;
90// } avdtp_sdp_query_context_t;
86static void avdtp_streaming_emit_connection_established(avdtp_stream_endpoint_t *stream_endpoint, uint8_t status) {
87 uint8_t event[14];
88 int pos = 0;
89 event[pos++] = HCI_EVENT_AVDTP_META;
90 event[pos++] = sizeof(event) - 2;
91 event[pos++] = AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED;
92 little_endian_store_16(event, pos, stream_endpoint->connection->avdtp_cid);
93 pos += 2;
94 reverse_bd_addr(stream_endpoint->connection->remote_addr, &event[pos]);
95 pos += 6;
96 event[pos++] = avdtp_local_seid(stream_endpoint);
97 event[pos++] = avdtp_remote_seid(stream_endpoint);
98 event[pos++] = status;
91
99
92static uint16_t avdtp_cid_counter = 0;
100 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
101 (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
102}
93
103
94static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
104static void avdtp_streaming_emit_connection_released(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid, uint8_t local_seid) {
105 uint8_t event[6];
106 int pos = 0;
107 event[pos++] = HCI_EVENT_AVDTP_META;
108 event[pos++] = sizeof(event) - 2;
109 event[pos++] = AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED;
110 little_endian_store_16(event, pos, avdtp_cid);
111 pos += 2;
112 event[pos++] = local_seid;
95
113
114 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
115 (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
116}
96
117
118void avdtp_streaming_emit_can_send_media_packet_now(avdtp_stream_endpoint_t *stream_endpoint, uint16_t sequence_number) {
119 uint8_t event[8];
120 int pos = 0;
121 event[pos++] = HCI_EVENT_AVDTP_META;
122 event[pos++] = sizeof(event) - 2;
123 event[pos++] = AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW;
124 little_endian_store_16(event, pos, stream_endpoint->connection->avdtp_cid);
125 pos += 2;
126 event[pos++] = avdtp_local_seid(stream_endpoint);
127 little_endian_store_16(event, pos, sequence_number);
128 pos += 2;
129
130 btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
131 (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
132}
133
134btstack_linked_list_t * avdtp_get_stream_endpoints(void){
135 return &stream_endpoints;
136}
137
97static avdtp_connection_t * avdtp_get_connection_for_bd_addr(bd_addr_t addr){
98 btstack_linked_list_iterator_t it;
99 btstack_linked_list_iterator_init(&it, &connections);
100 while (btstack_linked_list_iterator_has_next(&it)){
101 avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
102 if (memcmp(addr, connection->remote_addr, 6) != 0) continue;
103 return connection;
104 }

--- 712 unchanged lines hidden (view full) ---

817 stream_endpoint = avdtp_get_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid);
818 if (!stream_endpoint){
819 log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found for signaling cid 0x%02x", connection->l2cap_signaling_cid);
820 return;
821 }
822 if (status != ERROR_CODE_SUCCESS){
823 log_info("AVDTP_STREAM_ENDPOINT_OPENED failed with status %d, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", status, connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
824 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
138static avdtp_connection_t * avdtp_get_connection_for_bd_addr(bd_addr_t addr){
139 btstack_linked_list_iterator_t it;
140 btstack_linked_list_iterator_init(&it, &connections);
141 while (btstack_linked_list_iterator_has_next(&it)){
142 avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
143 if (memcmp(addr, connection->remote_addr, 6) != 0) continue;
144 return connection;
145 }

--- 712 unchanged lines hidden (view full) ---

858 stream_endpoint = avdtp_get_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid);
859 if (!stream_endpoint){
860 log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found for signaling cid 0x%02x", connection->l2cap_signaling_cid);
861 return;
862 }
863 if (status != ERROR_CODE_SUCCESS){
864 log_info("AVDTP_STREAM_ENDPOINT_OPENED failed with status %d, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", status, connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
865 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
825 avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), status);
866 avdtp_streaming_emit_connection_established(stream_endpoint, status);
826 break;
827 }
828 switch (stream_endpoint->state){
829 case AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED:
830 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
867 break;
868 }
869 switch (stream_endpoint->state){
870 case AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED:
871 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
831 stream_endpoint->connection = connection;
832 stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
833 stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
834
835 log_info("AVDTP_STREAM_ENDPOINT_OPENED, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
872 stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
873 stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
874
875 log_info("AVDTP_STREAM_ENDPOINT_OPENED, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
836 avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
837
876 avdtp_streaming_emit_connection_established(stream_endpoint, ERROR_CODE_SUCCESS);
838 break;
839 default:
840 log_info("AVDTP_STREAM_ENDPOINT_OPENED failed - stream endpoint in wrong state %d, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", stream_endpoint->state, connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
877 break;
878 default:
879 log_info("AVDTP_STREAM_ENDPOINT_OPENED failed - stream endpoint in wrong state %d, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", stream_endpoint->state, connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
841 avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE);
880 avdtp_streaming_emit_connection_established(stream_endpoint, AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE);
842 break;
843 }
844 break;
845
846 default:
847 log_info("L2CAP connection to %s ignored: status code 0x%02x, connection state %d", bd_addr_to_str(event_addr), status, connection->state);
848 break;
849 }

--- 5 unchanged lines hidden (view full) ---

855 connection = avdtp_get_connection_for_l2cap_signaling_cid(local_cid);
856
857 log_info("Received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
858
859 if (stream_endpoint){
860 if (stream_endpoint->l2cap_media_cid == local_cid){
861 connection = stream_endpoint->connection;
862 if (connection) {
881 break;
882 }
883 break;
884
885 default:
886 log_info("L2CAP connection to %s ignored: status code 0x%02x, connection state %d", bd_addr_to_str(event_addr), status, connection->state);
887 break;
888 }

--- 5 unchanged lines hidden (view full) ---

894 connection = avdtp_get_connection_for_l2cap_signaling_cid(local_cid);
895
896 log_info("Received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
897
898 if (stream_endpoint){
899 if (stream_endpoint->l2cap_media_cid == local_cid){
900 connection = stream_endpoint->connection;
901 if (connection) {
863 avdtp_streaming_emit_connection_released(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint));
902 avdtp_streaming_emit_connection_released(stream_endpoint,
903 connection->avdtp_cid,
904 avdtp_local_seid(stream_endpoint));
864 }
865 avdtp_reset_stream_endpoint(stream_endpoint);
866 break;
867 }
868 if (stream_endpoint->l2cap_recovery_cid == local_cid){
869 log_info("L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
870 stream_endpoint->l2cap_recovery_cid = 0;
871 break;

--- 503 unchanged lines hidden ---
905 }
906 avdtp_reset_stream_endpoint(stream_endpoint);
907 break;
908 }
909 if (stream_endpoint->l2cap_recovery_cid == local_cid){
910 log_info("L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
911 stream_endpoint->l2cap_recovery_cid = 0;
912 break;

--- 503 unchanged lines hidden ---