xref: /btstack/src/classic/avdtp_sink.c (revision 3e3fbf3df68dce1ab27af5efcdf0ea980eb93f6c)
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 <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "btstack.h"
47 #include "avdtp.h"
48 #include "avdtp_sink.h"
49 #include "avdtp_util.h"
50 #include "avdtp_initiator.h"
51 #include "avdtp_acceptor.h"
52 
53 static avdtp_context_t avdtp_sink_context;
54 
55 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
56 
57 void avdtp_sink_register_media_transport_category(uint8_t seid){
58     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
59     avdtp_register_media_transport_category(stream_endpoint);
60 }
61 
62 void avdtp_sink_register_reporting_category(uint8_t seid){
63     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
64     avdtp_register_reporting_category(stream_endpoint);
65 }
66 
67 void avdtp_sink_register_delay_reporting_category(uint8_t seid){
68     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
69     avdtp_register_delay_reporting_category(stream_endpoint);
70 }
71 
72 void avdtp_sink_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
73     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
74     avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets);
75 }
76 
77 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){
78     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
79     avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len);
80 }
81 
82 void avdtp_sink_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){
83     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
84     avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery);
85 }
86 
87 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){
88     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
89     avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len);
90 }
91 
92 void avdtp_sink_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){
93     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_sink_context);
94     avdtp_register_multiplexing_category(stream_endpoint, fragmentation);
95 }
96 
97 // // media, reporting. recovery
98 // void avdtp_sink_register_media_transport_identifier_for_multiplexing_category(uint8_t seid, uint8_t fragmentation){
99 
100 // }
101 
102 
103 /* END: tracking can send now requests pro l2cap cid */
104 // TODO remove
105 
106 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
107     avdtp_packet_handler(packet_type, channel, packet, size, &avdtp_sink_context);
108 }
109 
110 // TODO: find out which security level is needed, and replace LEVEL_0 in avdtp_sink_init
111 void avdtp_sink_init(void){
112     avdtp_sink_context.stream_endpoints = NULL;
113     avdtp_sink_context.connections = NULL;
114     avdtp_sink_context.stream_endpoints_id_counter = 0;
115     avdtp_sink_context.packet_handler = packet_handler;
116 
117     l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0);
118 }
119 
120 avdtp_stream_endpoint_t * avdtp_sink_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){
121     return avdtp_create_stream_endpoint(sep_type, media_type, &avdtp_sink_context);
122 }
123 
124 void avdtp_sink_register_media_handler(void (*callback)(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size)){
125     if (callback == NULL){
126         log_error("avdtp_sink_register_media_handler called with NULL callback");
127         return;
128     }
129     avdtp_sink_context.handle_media_data = callback;
130 }
131 
132 void avdtp_sink_register_packet_handler(btstack_packet_handler_t callback){
133     if (callback == NULL){
134         log_error("avdtp_sink_register_packet_handler called with NULL callback");
135         return;
136     }
137     avdtp_sink_context.avdtp_callback = callback;
138 }
139 
140 void avdtp_sink_connect(bd_addr_t remote){
141     avdtp_connect(remote, AVDTP_SOURCE, &avdtp_sink_context);
142 }
143 
144 void avdtp_sink_disconnect(uint16_t avdtp_cid){
145     avdtp_disconnect(avdtp_cid, &avdtp_sink_context);
146 }
147 
148 void avdtp_sink_open_stream(uint16_t avdtp_cid, uint8_t int_seid, uint8_t acp_seid){
149     avdtp_open_stream(avdtp_cid, int_seid, acp_seid, &avdtp_sink_context);
150 }
151 
152 void avdtp_sink_start_stream(uint8_t int_seid){
153     avdtp_start_stream(int_seid, &avdtp_sink_context);
154 }
155 
156 void avdtp_sink_stop_stream(uint8_t int_seid){
157     avdtp_stop_stream(int_seid, &avdtp_sink_context);
158 }
159 
160 void avdtp_sink_abort_stream(uint8_t int_seid){
161     avdtp_abort_stream(int_seid, &avdtp_sink_context);
162 }
163 
164 void avdtp_sink_suspend(uint8_t int_seid){
165     avdtp_suspend_stream(int_seid, &avdtp_sink_context);
166 }
167 
168 void avdtp_sink_discover_stream_endpoints(uint16_t avdtp_cid){
169     avdtp_discover_stream_endpoints(avdtp_cid, &avdtp_sink_context);
170 }
171 
172 void avdtp_sink_get_capabilities(uint16_t avdtp_cid, uint8_t acp_seid){
173     avdtp_get_capabilities(avdtp_cid, acp_seid, &avdtp_sink_context);
174 }
175 
176 void avdtp_sink_get_all_capabilities(uint16_t avdtp_cid, uint8_t acp_seid){
177     avdtp_get_all_capabilities(avdtp_cid, acp_seid, &avdtp_sink_context);
178 }
179 
180 void avdtp_sink_get_configuration(uint16_t avdtp_cid, uint8_t acp_seid){
181     avdtp_get_configuration(avdtp_cid, acp_seid, &avdtp_sink_context);
182 }
183 
184 void avdtp_sink_set_configuration(uint16_t avdtp_cid, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
185     avdtp_set_configuration(avdtp_cid, int_seid, acp_seid, configured_services_bitmap, configuration, &avdtp_sink_context);
186 }
187 
188 void avdtp_sink_reconfigure(uint16_t avdtp_cid, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
189     avdtp_reconfigure(avdtp_cid, int_seid, acp_seid, configured_services_bitmap, configuration, &avdtp_sink_context);
190 }
191 
192