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