1 2 /* 3 * Copyright (C) 2016 BlueKitchen GmbH 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the copyright holders nor the names of 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 4. Any redistribution, use, or modification is done solely for 18 * personal benefit and not for any commercial purpose or for 19 * monetary gain. 20 * 21 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 25 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Please inquire about commercial licensing options at 35 * [email protected] 36 * 37 */ 38 39 #define BTSTACK_FILE__ "avdtp_source.c" 40 41 42 #include <stdint.h> 43 #include <string.h> 44 45 #include "bluetooth_psm.h" 46 #include "bluetooth_sdp.h" 47 #include "btstack_debug.h" 48 #include "btstack_event.h" 49 #include "l2cap.h" 50 51 #include "classic/avdtp.h" 52 #include "classic/avdtp_util.h" 53 #include "classic/avdtp_source.h" 54 55 static avdtp_context_t * avdtp_source_context; 56 57 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 58 59 void avdtp_source_register_media_transport_category(uint8_t seid){ 60 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 61 avdtp_register_media_transport_category(stream_endpoint); 62 } 63 64 void avdtp_source_register_reporting_category(uint8_t seid){ 65 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 66 avdtp_register_reporting_category(stream_endpoint); 67 } 68 69 void avdtp_source_register_delay_reporting_category(uint8_t seid){ 70 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 71 avdtp_register_delay_reporting_category(stream_endpoint); 72 } 73 74 void avdtp_source_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){ 75 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 76 avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets); 77 } 78 79 void avdtp_source_register_content_protection_category(uint8_t seid, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){ 80 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 81 avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len); 82 } 83 84 void avdtp_source_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){ 85 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 86 avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery); 87 } 88 89 void avdtp_source_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){ 90 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 91 avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len); 92 } 93 94 void avdtp_source_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){ 95 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context); 96 avdtp_register_multiplexing_category(stream_endpoint, fragmentation); 97 } 98 99 avdtp_stream_endpoint_t * avdtp_source_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){ 100 return avdtp_create_stream_endpoint(sep_type, media_type, avdtp_source_context); 101 } 102 103 void avdtp_source_register_packet_handler(btstack_packet_handler_t callback){ 104 if (callback == NULL){ 105 log_error("avdtp_source_register_packet_handler called with NULL callback"); 106 return; 107 } 108 avdtp_source_context->avdtp_callback = callback; 109 } 110 111 uint8_t avdtp_source_connect(bd_addr_t remote, uint16_t * avdtp_cid){ 112 return avdtp_connect(remote, AVDTP_SINK, avdtp_source_context, avdtp_cid); 113 } 114 115 uint8_t avdtp_source_disconnect(uint16_t avdtp_cid){ 116 return avdtp_disconnect(avdtp_cid, avdtp_source_context); 117 } 118 119 uint8_t avdtp_source_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid){ 120 return avdtp_open_stream(avdtp_cid, local_seid, remote_seid, avdtp_source_context); 121 } 122 123 uint8_t avdtp_source_start_stream(uint16_t avdtp_cid, uint8_t local_seid){ 124 return avdtp_start_stream(avdtp_cid, local_seid, avdtp_source_context); 125 } 126 127 uint8_t avdtp_source_stop_stream(uint16_t avdtp_cid, uint8_t local_seid){ 128 return avdtp_stop_stream(avdtp_cid, local_seid, avdtp_source_context); 129 } 130 131 uint8_t avdtp_source_abort_stream(uint16_t avdtp_cid, uint8_t local_seid){ 132 return avdtp_abort_stream(avdtp_cid, local_seid, avdtp_source_context); 133 } 134 135 uint8_t avdtp_source_suspend(uint16_t avdtp_cid, uint8_t local_seid){ 136 return avdtp_suspend_stream(avdtp_cid, local_seid, avdtp_source_context); 137 } 138 139 uint8_t avdtp_source_discover_stream_endpoints(uint16_t avdtp_cid){ 140 return avdtp_discover_stream_endpoints(avdtp_cid, avdtp_source_context); 141 } 142 143 uint8_t avdtp_source_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){ 144 return avdtp_get_capabilities(avdtp_cid, remote_seid, avdtp_source_context); 145 } 146 147 uint8_t avdtp_source_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){ 148 return avdtp_get_all_capabilities(avdtp_cid, remote_seid, avdtp_source_context); 149 } 150 151 uint8_t avdtp_source_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid){ 152 return avdtp_get_configuration(avdtp_cid, remote_seid, avdtp_source_context); 153 } 154 155 uint8_t avdtp_source_set_configuration(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){ 156 return avdtp_set_configuration(avdtp_cid, local_seid, remote_seid, configured_services_bitmap, configuration, avdtp_source_context); 157 } 158 159 uint8_t avdtp_source_reconfigure(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){ 160 return avdtp_reconfigure(avdtp_cid, local_seid, remote_seid, configured_services_bitmap, configuration, avdtp_source_context); 161 } 162 163 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 164 avdtp_packet_handler(packet_type, channel, packet, size, avdtp_source_context); 165 } 166 167 void avdtp_source_init(avdtp_context_t * avdtp_context){ 168 if (!avdtp_context){ 169 log_error("avdtp_source_context is NULL"); 170 return; 171 } 172 avdtp_source_context = avdtp_context; 173 avdtp_source_context->stream_endpoints = NULL; 174 avdtp_source_context->connections = NULL; 175 avdtp_source_context->stream_endpoints_id_counter = 0; 176 avdtp_source_context->packet_handler = packet_handler; 177 178 l2cap_register_service(&packet_handler, BLUETOOTH_PSM_AVDTP, 0xffff, LEVEL_2); 179 } 180 181