xref: /btstack/src/classic/avdtp_sink.c (revision cfd54eb73cd29e7bf738f261fb454a84a1bb66b0)
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 #define BTSTACK_FILE__ "avdtp_sink.c"
39 
40 #include <stdint.h>
41 #include <string.h>
42 
43 #include "bluetooth_psm.h"
44 #include "bluetooth_sdp.h"
45 #include "btstack_debug.h"
46 #include "btstack_event.h"
47 #include "l2cap.h"
48 
49 #include "classic/avdtp.h"
50 #include "classic/avdtp_acceptor.h"
51 #include "classic/avdtp_initiator.h"
52 #include "classic/avdtp_sink.h"
53 #include "classic/avdtp_util.h"
54 
55 void avdtp_sink_register_media_transport_category(uint8_t seid){
56     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
57     avdtp_register_media_transport_category(stream_endpoint);
58 }
59 
60 void avdtp_sink_register_reporting_category(uint8_t seid){
61     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
62     avdtp_register_reporting_category(stream_endpoint);
63 }
64 
65 void avdtp_sink_register_delay_reporting_category(uint8_t seid){
66     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
67     avdtp_register_delay_reporting_category(stream_endpoint);
68 }
69 
70 void avdtp_sink_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
71     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
72     avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets);
73 }
74 
75 void avdtp_sink_register_content_protection_category(uint8_t seid, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){
76     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
77     avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len);
78 }
79 
80 void avdtp_sink_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){
81     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
82     avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery);
83 }
84 
85 void avdtp_sink_register_media_codec_category(uint8_t seid, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, uint8_t * media_codec_info, uint16_t media_codec_info_len){
86     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
87     avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len);
88 }
89 
90 void avdtp_sink_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){
91     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
92     avdtp_register_multiplexing_category(stream_endpoint, fragmentation);
93 }
94 /* END: tracking can send now requests pro l2cap cid */
95 
96 void avdtp_sink_register_packet_handler(btstack_packet_handler_t callback){
97     btstack_assert(callback != NULL);
98 
99     avdtp_register_sink_packet_handler(callback);
100 }
101 
102 void avdtp_sink_init(void) {
103     avdtp_init();
104 }
105 
106 avdtp_stream_endpoint_t * avdtp_sink_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){
107     return avdtp_create_stream_endpoint(sep_type, media_type);
108 }
109 
110 void avdtp_sink_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){
111     avdtp_finalize_stream_endpoint(stream_endpoint);
112 }
113 
114 void avdtp_sink_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){
115     avdtp_register_media_handler(callback);
116 }
117 
118 uint8_t avdtp_sink_connect(bd_addr_t remote, uint16_t * avdtp_cid){
119     return avdtp_connect(remote, AVDTP_ROLE_SINK, avdtp_cid);
120 }
121 
122 uint8_t avdtp_sink_disconnect(uint16_t avdtp_cid){
123     return avdtp_disconnect(avdtp_cid);
124 }
125 
126 uint8_t avdtp_sink_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid){
127     return avdtp_open_stream(avdtp_cid, local_seid, remote_seid);
128 }
129 
130 uint8_t avdtp_sink_start_stream(uint16_t avdtp_cid, uint8_t local_seid){
131     return avdtp_start_stream(avdtp_cid, local_seid);
132 }
133 
134 uint8_t avdtp_sink_stop_stream(uint16_t avdtp_cid, uint8_t local_seid){
135     return avdtp_stop_stream(avdtp_cid, local_seid);
136 }
137 
138 uint8_t avdtp_sink_abort_stream(uint16_t avdtp_cid, uint8_t local_seid){
139     return avdtp_abort_stream(avdtp_cid, local_seid);
140 }
141 
142 uint8_t avdtp_sink_suspend(uint16_t avdtp_cid, uint8_t local_seid){
143     return avdtp_suspend_stream(avdtp_cid, local_seid);
144 }
145 
146 uint8_t avdtp_sink_discover_stream_endpoints(uint16_t avdtp_cid){
147     return avdtp_discover_stream_endpoints(avdtp_cid);
148 }
149 
150 uint8_t avdtp_sink_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){
151     return avdtp_get_capabilities(avdtp_cid, remote_seid);
152 }
153 
154 uint8_t avdtp_sink_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){
155     return avdtp_get_all_capabilities(avdtp_cid, remote_seid);
156 }
157 
158 uint8_t avdtp_sink_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid){
159     return avdtp_get_configuration(avdtp_cid, remote_seid);
160 }
161 
162 uint8_t avdtp_sink_set_configuration(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
163     return avdtp_set_configuration(avdtp_cid, local_seid, remote_seid, configured_services_bitmap, configuration);
164 }
165 
166 uint8_t avdtp_sink_reconfigure(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
167     return avdtp_reconfigure(avdtp_cid, local_seid, remote_seid, configured_services_bitmap, configuration);
168 }
169 
170 uint8_t avdtp_sink_delay_report(uint16_t avdtp_cid, uint8_t local_seid, uint16_t delay_100us){
171     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
172     if (!connection){
173         log_error("delay_report: no connection for signaling cid 0x%02x found", avdtp_cid);
174         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
175     }
176 
177     if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) ||
178         (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) {
179         log_error("delay_report: connection in wrong state, state %d, initiator state %d", connection->state, connection->initiator_connection_state);
180         return ERROR_CODE_COMMAND_DISALLOWED;
181     }
182 
183     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid);
184     if (!stream_endpoint) {
185         log_error("delay_report: no stream_endpoint with seid %d found", local_seid);
186         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
187     }
188 
189     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED){
190         log_error("Stream endpoint seid %d in wrong state %d", local_seid, stream_endpoint->state);
191         return ERROR_CODE_COMMAND_DISALLOWED;
192     }
193 
194     connection->initiator_transaction_label++;
195     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_DELAY_REPORT;
196     connection->delay_ms = delay_100us;
197     connection->initiator_local_seid = local_seid;
198     connection->initiator_remote_seid = stream_endpoint->remote_sep.seid;
199     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
200     return ERROR_CODE_SUCCESS;
201 }
202 
203