xref: /btstack/src/classic/a2dp_sink.c (revision b397c740f56f5937fa7fa5a169ff9b66feef6779)
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__ "a2dp_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_util.h"
48 #include "avdtp_sink.h"
49 #include "a2dp_sink.h"
50 
51 static const char * default_a2dp_sink_service_name = "BTstack A2DP Sink Service";
52 static const char * default_a2dp_sink_service_provider_name = "BTstack A2DP Sink Service Provider";
53 
54 // static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
55 
56 void a2dp_sink_create_sdp_record(uint8_t * service,  uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
57     uint8_t* attribute;
58     de_create_sequence(service);
59 
60     // 0x0000 "Service Record Handle"
61     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
62     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
63 
64     // 0x0001 "Service Class ID List"
65     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
66     attribute = de_push_sequence(service);
67     {
68         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SINK);
69     }
70     de_pop_sequence(service, attribute);
71 
72     // 0x0004 "Protocol Descriptor List"
73     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
74     attribute = de_push_sequence(service);
75     {
76         uint8_t* l2cpProtocol = de_push_sequence(attribute);
77         {
78             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
79             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);
80         }
81         de_pop_sequence(attribute, l2cpProtocol);
82 
83         uint8_t* avProtocol = de_push_sequence(attribute);
84         {
85             de_add_number(avProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);  // avProtocol_service
86             de_add_number(avProtocol,  DE_UINT, DE_SIZE_16,  0x0103);  // version
87         }
88         de_pop_sequence(attribute, avProtocol);
89     }
90     de_pop_sequence(service, attribute);
91 
92     // 0x0005 "Public Browse Group"
93     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
94     attribute = de_push_sequence(service);
95     {
96         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
97     }
98     de_pop_sequence(service, attribute);
99 
100     // 0x0009 "Bluetooth Profile Descriptor List"
101     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
102     attribute = de_push_sequence(service);
103     {
104         uint8_t *a2dProfile = de_push_sequence(attribute);
105         {
106             de_add_number(a2dProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION);
107             de_add_number(a2dProfile,  DE_UINT, DE_SIZE_16, 0x0103);
108         }
109         de_pop_sequence(attribute, a2dProfile);
110     }
111     de_pop_sequence(service, attribute);
112 
113 
114     // 0x0100 "Service Name"
115     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
116     if (service_name){
117         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
118     } else {
119         de_add_data(service,  DE_STRING, strlen(default_a2dp_sink_service_name), (uint8_t *) default_a2dp_sink_service_name);
120     }
121 
122     // 0x0100 "Provider Name"
123     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
124     if (service_provider_name){
125         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
126     } else {
127         de_add_data(service,  DE_STRING, strlen(default_a2dp_sink_service_provider_name), (uint8_t *) default_a2dp_sink_service_provider_name);
128     }
129 
130     // 0x0311 "Supported Features"
131     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
132     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
133 }
134