1 /******************************************************************************
2 *
3 * Copyright 2002-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * Common API for the Advanced Audio Distribution Profile (A2DP)
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bluetooth-a2dp"
26
27 #include "a2dp_api.h"
28
29 #include <bluetooth/log.h>
30 #include <string.h>
31
32 #include <cstdint>
33
34 #include "a2dp_constants.h"
35 #include "a2dp_int.h"
36 #include "avdt_api.h"
37 #include "internal_include/bt_target.h"
38 #include "osi/include/allocator.h"
39 #include "sdp_discovery_db.h"
40 #include "sdp_status.h"
41 #include "sdpdefs.h"
42 #include "stack/include/bt_types.h"
43 #include "stack/include/bt_uuid16.h"
44 #include "stack/include/sdp_api.h"
45 #include "types/bluetooth/uuid.h"
46 #include "types/raw_address.h"
47
48 using namespace bluetooth;
49 using namespace bluetooth::legacy::stack::sdp;
50
51 using bluetooth::Uuid;
52
53 /*****************************************************************************
54 * Global data
55 ****************************************************************************/
56 tA2DP_CB a2dp_cb;
57 static uint16_t a2dp_attr_list[] = {
58 ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2DP_NUM_ATTR, if changed */
59 ATTR_ID_BT_PROFILE_DESC_LIST, ATTR_ID_SUPPORTED_FEATURES, ATTR_ID_SERVICE_NAME,
60 ATTR_ID_PROTOCOL_DESC_LIST, ATTR_ID_PROVIDER_NAME};
61
62 /******************************************************************************
63 *
64 * Function a2dp_sdp_cback
65 *
66 * Description This is the SDP callback function used by A2DP_FindService.
67 * This function will be executed by SDP when the service
68 * search is completed. If the search is successful, it
69 * finds the first record in the database that matches the
70 * UUID of the search. Then retrieves various parameters
71 * from the record. When it is finished it calls the
72 * application callback function.
73 *
74 * Returns Nothing.
75 *
76 *****************************************************************************/
a2dp_sdp_cback(const RawAddress &,tSDP_STATUS status)77 static void a2dp_sdp_cback(const RawAddress& /* bd_addr */, tSDP_STATUS status) {
78 tSDP_DISC_REC* p_rec = NULL;
79 tSDP_DISC_ATTR* p_attr;
80 bool found = false;
81 tA2DP_Service a2dp_svc;
82 tSDP_PROTOCOL_ELEM elem;
83 RawAddress peer_address = RawAddress::kEmpty;
84
85 log::info("status: {}", status);
86
87 if (status == tSDP_STATUS::SDP_SUCCESS) {
88 /* loop through all records we found */
89 do {
90 /* get next record; if none found, we're done */
91 if ((p_rec = get_legacy_stack_sdp_api()->db.SDP_FindServiceInDb(
92 a2dp_cb.find.p_db, a2dp_cb.find.service_uuid, p_rec)) == NULL) {
93 break;
94 }
95 memset(&a2dp_svc, 0, sizeof(tA2DP_Service));
96 peer_address = p_rec->remote_bd_addr;
97
98 /* get service name */
99 if ((p_attr = get_legacy_stack_sdp_api()->record.SDP_FindAttributeInRec(
100 p_rec, ATTR_ID_SERVICE_NAME)) != NULL) {
101 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == TEXT_STR_DESC_TYPE) {
102 a2dp_svc.p_service_name = (char*)p_attr->attr_value.v.array;
103 a2dp_svc.service_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
104 } else {
105 log::error("ATTR_ID_SERVICE_NAME attr type not STR!!");
106 }
107 } else {
108 log::error("ATTR_ID_SERVICE_NAME attr not found!!");
109 }
110
111 /* get provider name */
112 if ((p_attr = get_legacy_stack_sdp_api()->record.SDP_FindAttributeInRec(
113 p_rec, ATTR_ID_PROVIDER_NAME)) != NULL) {
114 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == TEXT_STR_DESC_TYPE) {
115 a2dp_svc.p_provider_name = (char*)p_attr->attr_value.v.array;
116 a2dp_svc.provider_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
117 } else {
118 log::error("ATTR_ID_PROVIDER_NAME attr type not STR!!");
119 }
120 } else {
121 log::error("ATTR_ID_PROVIDER_NAME attr not found!!");
122 }
123
124 /* get supported features */
125 if ((p_attr = get_legacy_stack_sdp_api()->record.SDP_FindAttributeInRec(
126 p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL) {
127 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UINT_DESC_TYPE &&
128 SDP_DISC_ATTR_LEN(p_attr->attr_len_type) >= 2) {
129 a2dp_svc.features = p_attr->attr_value.v.u16;
130 } else {
131 log::error("ATTR_ID_SUPPORTED_FEATURES attr type not STR!!");
132 }
133 } else {
134 log::error("ATTR_ID_SUPPORTED_FEATURES attr not found!!");
135 }
136
137 /* get AVDTP version */
138 if (get_legacy_stack_sdp_api()->record.SDP_FindProtocolListElemInRec(
139 p_rec, UUID_PROTOCOL_AVDTP, &elem)) {
140 a2dp_svc.avdt_version = elem.params[0];
141 log::verbose("avdt_version: 0x{:x}", a2dp_svc.avdt_version);
142 }
143
144 /* we've got everything, we're done */
145 found = true;
146 break;
147 } while (true);
148 }
149
150 a2dp_cb.find.service_uuid = 0;
151 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
152 /* return info from sdp record in app callback function */
153 if (!a2dp_cb.find.p_cback.is_null()) {
154 a2dp_cb.find.p_cback.Run(found, &a2dp_svc, peer_address);
155 }
156
157 return;
158 }
159
160 /******************************************************************************
161 *
162 * Function A2DP_AddRecord
163 *
164 * Description This function is called by a server application to add
165 * SRC or SNK information to an SDP record. Prior to
166 * calling this function the application must call
167 * SDP_CreateRecord() to create an SDP record.
168 *
169 * Input Parameters:
170 * service_uuid: Indicates SRC or SNK.
171 *
172 * p_service_name: Pointer to a null-terminated character
173 * string containing the service name.
174 *
175 * p_provider_name: Pointer to a null-terminated character
176 * string containing the provider name.
177 *
178 * features: Profile supported features.
179 *
180 * sdp_handle: SDP handle returned by SDP_CreateRecord().
181 *
182 * Output Parameters:
183 * None.
184 *
185 * Returns true if function execution succeeded,
186 * false if bad parameters are given or execution failed.
187 *
188 *****************************************************************************/
A2DP_AddRecord(uint16_t service_uuid,char * p_service_name,char * p_provider_name,uint16_t features,uint32_t sdp_handle)189 bool A2DP_AddRecord(uint16_t service_uuid, char* p_service_name, char* p_provider_name,
190 uint16_t features, uint32_t sdp_handle) {
191 uint16_t browse_list[1];
192 bool result = true;
193 uint8_t temp[8];
194 uint8_t* p;
195 tSDP_PROTOCOL_ELEM proto_list[A2DP_NUM_PROTO_ELEMS];
196
197 log::verbose("uuid: 0x{:x}", service_uuid);
198
199 if ((sdp_handle == 0) ||
200 (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK)) {
201 return false;
202 }
203
204 /* add service class id list */
205 result &= get_legacy_stack_sdp_api()->handle.SDP_AddServiceClassIdList(sdp_handle, 1,
206 &service_uuid);
207
208 memset((void*)proto_list, 0, A2DP_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
209
210 /* add protocol descriptor list */
211 proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
212 proto_list[0].num_params = 1;
213 proto_list[0].params[0] = AVDT_PSM;
214 proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
215 proto_list[1].num_params = 1;
216 proto_list[1].params[0] = A2DP_GetAvdtpVersion();
217
218 result &= get_legacy_stack_sdp_api()->handle.SDP_AddProtocolList(sdp_handle, A2DP_NUM_PROTO_ELEMS,
219 proto_list);
220
221 /* add profile descriptor list */
222 result &= get_legacy_stack_sdp_api()->handle.SDP_AddProfileDescriptorList(
223 sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, A2DP_VERSION);
224
225 /* add supported feature */
226 if (features != 0) {
227 p = temp;
228 UINT16_TO_BE_STREAM(p, features);
229 result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
230 sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, (uint32_t)2, (uint8_t*)temp);
231 }
232
233 /* add provider name */
234 if (p_provider_name != NULL) {
235 result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
236 sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
237 (uint32_t)(strlen(p_provider_name) + 1), (uint8_t*)p_provider_name);
238 }
239
240 /* add service name */
241 if (p_service_name != NULL) {
242 result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
243 sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
244 (uint32_t)(strlen(p_service_name) + 1), (uint8_t*)p_service_name);
245 }
246
247 /* add browse group list */
248 browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
249 result &= get_legacy_stack_sdp_api()->handle.SDP_AddUuidSequence(
250 sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
251
252 return result;
253 }
254
255 /******************************************************************************
256 *
257 * Function A2DP_FindService
258 *
259 * Description This function is called by a client application to
260 * perform service discovery and retrieve SRC or SNK SDP
261 * record information from a server. Information is
262 * returned for the first service record found on the
263 * server that matches the service UUID. The callback
264 * function will be executed when service discovery is
265 * complete. There can only be one outstanding call to
266 * A2DP_FindService() at a time; the application must wait
267 * for the callback before it makes another call to
268 * the function.
269 *
270 * Input Parameters:
271 * service_uuid: Indicates SRC or SNK.
272 *
273 * bd_addr: BD address of the peer device.
274 *
275 * p_db: Pointer to the information to initialize
276 * the discovery database.
277 *
278 * p_cback: Pointer to the A2DP_FindService()
279 * callback function.
280 *
281 * Output Parameters:
282 * None.
283 *
284 * Returns A2DP_SUCCESS if function execution succeeded,
285 * A2DP_BUSY if discovery is already in progress.
286 * A2DP_FAIL if function execution failed.
287 *
288 *****************************************************************************/
A2DP_FindService(uint16_t service_uuid,const RawAddress & bd_addr,tA2DP_SDP_DB_PARAMS * p_db,tA2DP_FIND_CBACK p_cback)289 tA2DP_STATUS A2DP_FindService(uint16_t service_uuid, const RawAddress& bd_addr,
290 tA2DP_SDP_DB_PARAMS* p_db, tA2DP_FIND_CBACK p_cback) {
291 if ((service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
292 p_db == NULL || p_cback.is_null()) {
293 log::error("Cannot find service for peer {} UUID 0x{:04x}: invalid parameters", bd_addr,
294 service_uuid);
295 return A2DP_FAIL;
296 }
297
298 if (a2dp_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE ||
299 a2dp_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK || a2dp_cb.find.p_db != NULL) {
300 log::error("Cannot find service for peer {} UUID 0x{:04x}: busy", bd_addr, service_uuid);
301 return A2DP_BUSY;
302 }
303
304 if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
305 p_db->p_attrs = a2dp_attr_list;
306 p_db->num_attr = A2DP_NUM_ATTR;
307 }
308
309 a2dp_cb.find.p_db = (tSDP_DISCOVERY_DB*)osi_malloc(p_db->db_len);
310 Uuid uuid_list = Uuid::From16Bit(service_uuid);
311
312 if (!get_legacy_stack_sdp_api()->service.SDP_InitDiscoveryDb(
313 a2dp_cb.find.p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr, p_db->p_attrs)) {
314 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
315 log::error("Unable to initialize SDP discovery for peer {} UUID 0x{:04X}", bd_addr,
316 service_uuid);
317 return A2DP_FAIL;
318 }
319
320 /* store service_uuid */
321 a2dp_cb.find.service_uuid = service_uuid;
322 a2dp_cb.find.p_cback = p_cback;
323
324 /* perform service search */
325 if (!get_legacy_stack_sdp_api()->service.SDP_ServiceSearchAttributeRequest(
326 bd_addr, a2dp_cb.find.p_db, a2dp_sdp_cback)) {
327 a2dp_cb.find.service_uuid = 0;
328 a2dp_cb.find.p_cback.Reset();
329 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
330 log::error("Cannot find service for peer {} UUID 0x{:04x}: SDP error", bd_addr, service_uuid);
331 return A2DP_FAIL;
332 }
333 log::info("A2DP service discovery for peer {} UUID 0x{:04x}: SDP search started", bd_addr,
334 service_uuid);
335 return A2DP_SUCCESS;
336 }
337
338 /******************************************************************************
339 * Function A2DP_BitsSet
340 *
341 * Description Check the given num for the number of bits set
342 * Returns A2DP_SET_ONE_BIT, if one and only one bit is set
343 * A2DP_SET_ZERO_BIT, if all bits clear
344 * A2DP_SET_MULTL_BIT, if multiple bits are set
345 *****************************************************************************/
A2DP_BitsSet(uint64_t num)346 uint8_t A2DP_BitsSet(uint64_t num) {
347 if (num == 0) {
348 return A2DP_SET_ZERO_BIT;
349 }
350 if ((num & (num - 1)) == 0) {
351 return A2DP_SET_ONE_BIT;
352 }
353 return A2DP_SET_MULTL_BIT;
354 }
355
356 /*******************************************************************************
357 *
358 * Function A2DP_Init
359 *
360 * Description This function is called to initialize the control block
361 * for this layer. It must be called before accessing any
362 * other API functions for this layer. It is typically called
363 * once during the start up of the stack.
364 *
365 * Returns void
366 *
367 ******************************************************************************/
A2DP_Init(void)368 void A2DP_Init(void) {
369 memset(&a2dp_cb, 0, sizeof(tA2DP_CB));
370 }
371
A2DP_GetAvdtpVersion()372 uint16_t A2DP_GetAvdtpVersion() { return AVDT_VERSION; }
373