xref: /btstack/src/classic/a2dp_source.c (revision 274391e8b2f0d74a87eb98f291f95ef8eb95044e)
1b442c9e6SMilanka Ringwald 
2b442c9e6SMilanka Ringwald /*
3b442c9e6SMilanka Ringwald  * Copyright (C) 2016 BlueKitchen GmbH
4b442c9e6SMilanka Ringwald  *
5b442c9e6SMilanka Ringwald  * Redistribution and use in source and binary forms, with or without
6b442c9e6SMilanka Ringwald  * modification, are permitted provided that the following conditions
7b442c9e6SMilanka Ringwald  * are met:
8b442c9e6SMilanka Ringwald  *
9b442c9e6SMilanka Ringwald  * 1. Redistributions of source code must retain the above copyright
10b442c9e6SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer.
11b442c9e6SMilanka Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
12b442c9e6SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer in the
13b442c9e6SMilanka Ringwald  *    documentation and/or other materials provided with the distribution.
14b442c9e6SMilanka Ringwald  * 3. Neither the name of the copyright holders nor the names of
15b442c9e6SMilanka Ringwald  *    contributors may be used to endorse or promote products derived
16b442c9e6SMilanka Ringwald  *    from this software without specific prior written permission.
17b442c9e6SMilanka Ringwald  * 4. Any redistribution, use, or modification is done solely for
18b442c9e6SMilanka Ringwald  *    personal benefit and not for any commercial purpose or for
19b442c9e6SMilanka Ringwald  *    monetary gain.
20b442c9e6SMilanka Ringwald  *
21b442c9e6SMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
22b442c9e6SMilanka Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23b442c9e6SMilanka Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24b442c9e6SMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
25b442c9e6SMilanka Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26b442c9e6SMilanka Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27b442c9e6SMilanka Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28b442c9e6SMilanka Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29b442c9e6SMilanka Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30b442c9e6SMilanka Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31b442c9e6SMilanka Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b442c9e6SMilanka Ringwald  * SUCH DAMAGE.
33b442c9e6SMilanka Ringwald  *
34b442c9e6SMilanka Ringwald  * Please inquire about commercial licensing options at
35b442c9e6SMilanka Ringwald  * [email protected]
36b442c9e6SMilanka Ringwald  *
37b442c9e6SMilanka Ringwald  */
38b442c9e6SMilanka Ringwald 
39b442c9e6SMilanka Ringwald #define __BTSTACK_FILE__ "a2dp_source.c"
40b442c9e6SMilanka Ringwald 
41b442c9e6SMilanka Ringwald #include <stdint.h>
42b442c9e6SMilanka Ringwald #include <stdio.h>
43b442c9e6SMilanka Ringwald #include <stdlib.h>
44b442c9e6SMilanka Ringwald #include <string.h>
45b442c9e6SMilanka Ringwald #include <unistd.h>
46b442c9e6SMilanka Ringwald 
47b442c9e6SMilanka Ringwald #include "btstack.h"
48b442c9e6SMilanka Ringwald #include "avdtp.h"
49b442c9e6SMilanka Ringwald #include "avdtp_util.h"
50b442c9e6SMilanka Ringwald #include "avdtp_source.h"
51b442c9e6SMilanka Ringwald #include "a2dp_source.h"
52b442c9e6SMilanka Ringwald 
53b442c9e6SMilanka Ringwald static const char * default_a2dp_source_service_name = "BTstack A2DP Source Service";
54b442c9e6SMilanka Ringwald static const char * default_a2dp_source_service_provider_name = "BTstack A2DP Source Service Provider";
55*274391e8SMilanka Ringwald static avdtp_context_t a2dp_source_context;
56b442c9e6SMilanka Ringwald 
57b442c9e6SMilanka Ringwald // static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
58b442c9e6SMilanka Ringwald 
59b442c9e6SMilanka Ringwald void a2dp_source_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
60b442c9e6SMilanka Ringwald     uint8_t* attribute;
61b442c9e6SMilanka Ringwald     de_create_sequence(service);
62b442c9e6SMilanka Ringwald 
63b442c9e6SMilanka Ringwald     // 0x0000 "Service Record Handle"
64b442c9e6SMilanka Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
65b442c9e6SMilanka Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
66b442c9e6SMilanka Ringwald 
67b442c9e6SMilanka Ringwald     // 0x0001 "Service Class ID List"
68b442c9e6SMilanka Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
69b442c9e6SMilanka Ringwald     attribute = de_push_sequence(service);
70b442c9e6SMilanka Ringwald     {
71b442c9e6SMilanka Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE);
72b442c9e6SMilanka Ringwald     }
73b442c9e6SMilanka Ringwald     de_pop_sequence(service, attribute);
74b442c9e6SMilanka Ringwald 
75b442c9e6SMilanka Ringwald     // 0x0004 "Protocol Descriptor List"
76b442c9e6SMilanka Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
77b442c9e6SMilanka Ringwald     attribute = de_push_sequence(service);
78b442c9e6SMilanka Ringwald     {
79b442c9e6SMilanka Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
80b442c9e6SMilanka Ringwald         {
81b442c9e6SMilanka Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
82b442c9e6SMilanka Ringwald             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);
83b442c9e6SMilanka Ringwald         }
84b442c9e6SMilanka Ringwald         de_pop_sequence(attribute, l2cpProtocol);
85b442c9e6SMilanka Ringwald 
86b442c9e6SMilanka Ringwald         uint8_t* avProtocol = de_push_sequence(attribute);
87b442c9e6SMilanka Ringwald         {
88b442c9e6SMilanka Ringwald             de_add_number(avProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);  // avProtocol_service
89b442c9e6SMilanka Ringwald             de_add_number(avProtocol,  DE_UINT, DE_SIZE_16,  0x0103);  // version
90b442c9e6SMilanka Ringwald         }
91b442c9e6SMilanka Ringwald         de_pop_sequence(attribute, avProtocol);
92b442c9e6SMilanka Ringwald     }
93b442c9e6SMilanka Ringwald     de_pop_sequence(service, attribute);
94b442c9e6SMilanka Ringwald 
95b442c9e6SMilanka Ringwald     // 0x0005 "Public Browse Group"
96b442c9e6SMilanka Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
97b442c9e6SMilanka Ringwald     attribute = de_push_sequence(service);
98b442c9e6SMilanka Ringwald     {
99b442c9e6SMilanka Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
100b442c9e6SMilanka Ringwald     }
101b442c9e6SMilanka Ringwald     de_pop_sequence(service, attribute);
102b442c9e6SMilanka Ringwald 
103b442c9e6SMilanka Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
104b442c9e6SMilanka Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
105b442c9e6SMilanka Ringwald     attribute = de_push_sequence(service);
106b442c9e6SMilanka Ringwald     {
107b442c9e6SMilanka Ringwald         uint8_t *a2dProfile = de_push_sequence(attribute);
108b442c9e6SMilanka Ringwald         {
109b442c9e6SMilanka Ringwald             de_add_number(a2dProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION);
110b442c9e6SMilanka Ringwald             de_add_number(a2dProfile,  DE_UINT, DE_SIZE_16, 0x0103);
111b442c9e6SMilanka Ringwald         }
112b442c9e6SMilanka Ringwald         de_pop_sequence(attribute, a2dProfile);
113b442c9e6SMilanka Ringwald     }
114b442c9e6SMilanka Ringwald     de_pop_sequence(service, attribute);
115b442c9e6SMilanka Ringwald 
116b442c9e6SMilanka Ringwald 
117b442c9e6SMilanka Ringwald     // 0x0100 "Service Name"
118b442c9e6SMilanka Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
119b442c9e6SMilanka Ringwald     if (service_name){
120b442c9e6SMilanka Ringwald         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
121b442c9e6SMilanka Ringwald     } else {
122b442c9e6SMilanka Ringwald         de_add_data(service,  DE_STRING, strlen(default_a2dp_source_service_name), (uint8_t *) default_a2dp_source_service_name);
123b442c9e6SMilanka Ringwald     }
124b442c9e6SMilanka Ringwald 
125b442c9e6SMilanka Ringwald     // 0x0100 "Provider Name"
126b442c9e6SMilanka Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
127b442c9e6SMilanka Ringwald     if (service_provider_name){
128b442c9e6SMilanka Ringwald         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
129b442c9e6SMilanka Ringwald     } else {
130b442c9e6SMilanka Ringwald         de_add_data(service,  DE_STRING, strlen(default_a2dp_source_service_provider_name), (uint8_t *) default_a2dp_source_service_provider_name);
131b442c9e6SMilanka Ringwald     }
132b442c9e6SMilanka Ringwald 
133b442c9e6SMilanka Ringwald     // 0x0311 "Supported Features"
134b442c9e6SMilanka Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
135b442c9e6SMilanka Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
136b442c9e6SMilanka Ringwald }
137b442c9e6SMilanka Ringwald 
138b442c9e6SMilanka Ringwald // static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
139*274391e8SMilanka Ringwald //     UNUSED(channel);
140*274391e8SMilanka Ringwald //     UNUSED(size);
141*274391e8SMilanka Ringwald //     UNUSED(packet_type);
142*274391e8SMilanka Ringwald //     UNUSED(packet);
143*274391e8SMilanka Ringwald 
144b442c9e6SMilanka Ringwald // }
145*274391e8SMilanka Ringwald 
146*274391e8SMilanka Ringwald void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){
147*274391e8SMilanka Ringwald     if (callback == NULL){
148*274391e8SMilanka Ringwald         log_error("a2dp_source_register_packet_handler called with NULL callback");
149*274391e8SMilanka Ringwald         return;
150*274391e8SMilanka Ringwald     }
151*274391e8SMilanka Ringwald     avdtp_source_register_packet_handler(callback);
152*274391e8SMilanka Ringwald     a2dp_source_context.a2dp_callback = callback;
153*274391e8SMilanka Ringwald }
154*274391e8SMilanka Ringwald 
155*274391e8SMilanka Ringwald void a2dp_source_init(void){
156*274391e8SMilanka Ringwald     avdtp_source_init(&a2dp_source_context);
157*274391e8SMilanka Ringwald     // l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0);
158*274391e8SMilanka Ringwald }
159*274391e8SMilanka Ringwald 
160*274391e8SMilanka Ringwald avdtp_stream_endpoint_t * a2dp_source_create_stream_endpoint(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){
161*274391e8SMilanka Ringwald     avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type);
162*274391e8SMilanka Ringwald     avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
163*274391e8SMilanka Ringwald     avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type, media_codec_info, media_codec_info_len);
164*274391e8SMilanka Ringwald     return local_stream_endpoint;
165*274391e8SMilanka Ringwald }
166