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 "a2dp_api"
26
27 #include "a2dp_api.h"
28
29 #include <string.h>
30
31 #include "a2dp_int.h"
32 #include "avdt_api.h"
33 #include "bt_target.h"
34 #include "main/shim/dumpsys.h"
35 #include "osi/include/allocator.h"
36 #include "osi/include/log.h"
37 #include "sdpdefs.h"
38 #include "stack/include/bt_types.h"
39 #include "types/bluetooth/uuid.h"
40 #include "types/raw_address.h"
41
42 using bluetooth::Uuid;
43
44 /*****************************************************************************
45 * Global data
46 ****************************************************************************/
47 tA2DP_CB a2dp_cb;
48 static uint16_t a2dp_attr_list[] = {
49 ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2DP_NUM_ATTR, if changed */
50 ATTR_ID_BT_PROFILE_DESC_LIST, ATTR_ID_SUPPORTED_FEATURES,
51 ATTR_ID_SERVICE_NAME, ATTR_ID_PROTOCOL_DESC_LIST,
52 ATTR_ID_PROVIDER_NAME};
53
54 /******************************************************************************
55 *
56 * Function a2dp_sdp_cback
57 *
58 * Description This is the SDP callback function used by A2DP_FindService.
59 * This function will be executed by SDP when the service
60 * search is completed. If the search is successful, it
61 * finds the first record in the database that matches the
62 * UUID of the search. Then retrieves various parameters
63 * from the record. When it is finished it calls the
64 * application callback function.
65 *
66 * Returns Nothing.
67 *
68 *****************************************************************************/
a2dp_sdp_cback(tSDP_STATUS status)69 static void a2dp_sdp_cback(tSDP_STATUS status) {
70 tSDP_DISC_REC* p_rec = NULL;
71 tSDP_DISC_ATTR* p_attr;
72 bool found = false;
73 tA2DP_Service a2dp_svc;
74 tSDP_PROTOCOL_ELEM elem;
75 RawAddress peer_address = RawAddress::kEmpty;
76
77 LOG_INFO("%s: status: %d", __func__, status);
78
79 if (status == SDP_SUCCESS) {
80 /* loop through all records we found */
81 do {
82 /* get next record; if none found, we're done */
83 if ((p_rec = SDP_FindServiceInDb(
84 a2dp_cb.find.p_db, a2dp_cb.find.service_uuid, p_rec)) == NULL) {
85 break;
86 }
87 memset(&a2dp_svc, 0, sizeof(tA2DP_Service));
88 peer_address = p_rec->remote_bd_addr;
89
90 /* get service name */
91 if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_NAME)) != NULL) {
92 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == TEXT_STR_DESC_TYPE) {
93 a2dp_svc.p_service_name = (char*)p_attr->attr_value.v.array;
94 a2dp_svc.service_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
95 } else {
96 LOG_ERROR("ATTR_ID_SERVICE_NAME attr type not STR!!");
97 }
98 } else {
99 LOG_ERROR("ATTR_ID_SERVICE_NAME attr not found!!");
100 }
101
102 /* get provider name */
103 if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_PROVIDER_NAME)) !=
104 NULL) {
105 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == TEXT_STR_DESC_TYPE) {
106 a2dp_svc.p_provider_name = (char*)p_attr->attr_value.v.array;
107 a2dp_svc.provider_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
108 } else {
109 LOG_ERROR("ATTR_ID_PROVIDER_NAME attr type not STR!!");
110 }
111 } else {
112 LOG_ERROR("ATTR_ID_PROVIDER_NAME attr not found!!");
113 }
114
115 /* get supported features */
116 if ((p_attr = SDP_FindAttributeInRec(
117 p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL) {
118 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UINT_DESC_TYPE &&
119 SDP_DISC_ATTR_LEN(p_attr->attr_len_type) >= 2) {
120 a2dp_svc.features = p_attr->attr_value.v.u16;
121 } else {
122 LOG_ERROR("ATTR_ID_SUPPORTED_FEATURES attr type not STR!!");
123 }
124 } else {
125 LOG_ERROR("ATTR_ID_SUPPORTED_FEATURES attr not found!!");
126 }
127
128 /* get AVDTP version */
129 if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_AVDTP, &elem)) {
130 a2dp_svc.avdt_version = elem.params[0];
131 LOG_VERBOSE("avdt_version: 0x%x", a2dp_svc.avdt_version);
132 }
133
134 /* we've got everything, we're done */
135 found = true;
136 break;
137
138 } while (true);
139 }
140
141 a2dp_cb.find.service_uuid = 0;
142 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
143 /* return info from sdp record in app callback function */
144 if (a2dp_cb.find.p_cback != NULL) {
145 (*a2dp_cb.find.p_cback)(found, &a2dp_svc, peer_address);
146 }
147
148 return;
149 }
150
151 /*******************************************************************************
152 *
153 * Function a2dp_set_avdt_sdp_ver
154 *
155 * Description This function allows the script wrapper to change the
156 * avdt version of a2dp.
157 *
158 * Returns None
159 *
160 ******************************************************************************/
a2dp_set_avdt_sdp_ver(uint16_t avdt_sdp_ver)161 void a2dp_set_avdt_sdp_ver(uint16_t avdt_sdp_ver) {
162 a2dp_cb.avdt_sdp_ver = avdt_sdp_ver;
163 }
164
165 /******************************************************************************
166 *
167 * Function A2DP_AddRecord
168 *
169 * Description This function is called by a server application to add
170 * SRC or SNK information to an SDP record. Prior to
171 * calling this function the application must call
172 * SDP_CreateRecord() to create an SDP record.
173 *
174 * Input Parameters:
175 * service_uuid: Indicates SRC or SNK.
176 *
177 * p_service_name: Pointer to a null-terminated character
178 * string containing the service name.
179 *
180 * p_provider_name: Pointer to a null-terminated character
181 * string containing the provider name.
182 *
183 * features: Profile supported features.
184 *
185 * sdp_handle: SDP handle returned by SDP_CreateRecord().
186 *
187 * Output Parameters:
188 * None.
189 *
190 * Returns A2DP_SUCCESS if function execution succeeded,
191 * A2DP_INVALID_PARAMS if bad parameters are given.
192 * A2DP_FAIL if function execution failed.
193 *
194 *****************************************************************************/
A2DP_AddRecord(uint16_t service_uuid,char * p_service_name,char * p_provider_name,uint16_t features,uint32_t sdp_handle)195 tA2DP_STATUS A2DP_AddRecord(uint16_t service_uuid, char* p_service_name,
196 char* p_provider_name, uint16_t features,
197 uint32_t sdp_handle) {
198 uint16_t browse_list[1];
199 bool result = true;
200 uint8_t temp[8];
201 uint8_t* p;
202 tSDP_PROTOCOL_ELEM proto_list[A2DP_NUM_PROTO_ELEMS];
203
204 LOG_VERBOSE("%s: uuid: 0x%x", __func__, service_uuid);
205
206 if ((sdp_handle == 0) || (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE &&
207 service_uuid != UUID_SERVCLASS_AUDIO_SINK))
208 return A2DP_INVALID_PARAMS;
209
210 /* add service class id list */
211 result &= SDP_AddServiceClassIdList(sdp_handle, 1, &service_uuid);
212
213 memset((void*)proto_list, 0,
214 A2DP_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
215
216 /* add protocol descriptor list */
217 proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
218 proto_list[0].num_params = 1;
219 proto_list[0].params[0] = AVDT_PSM;
220 proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
221 proto_list[1].num_params = 1;
222 proto_list[1].params[0] = a2dp_cb.avdt_sdp_ver;
223
224 result &= SDP_AddProtocolList(sdp_handle, A2DP_NUM_PROTO_ELEMS, proto_list);
225
226 /* add profile descriptor list */
227 result &= SDP_AddProfileDescriptorList(
228 sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, A2DP_VERSION);
229
230 /* add supported feature */
231 if (features != 0) {
232 p = temp;
233 UINT16_TO_BE_STREAM(p, features);
234 result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES,
235 UINT_DESC_TYPE, (uint32_t)2, (uint8_t*)temp);
236 }
237
238 /* add provider name */
239 if (p_provider_name != NULL) {
240 result &= SDP_AddAttribute(
241 sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
242 (uint32_t)(strlen(p_provider_name) + 1), (uint8_t*)p_provider_name);
243 }
244
245 /* add service name */
246 if (p_service_name != NULL) {
247 result &= SDP_AddAttribute(
248 sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
249 (uint32_t)(strlen(p_service_name) + 1), (uint8_t*)p_service_name);
250 }
251
252 /* add browse group list */
253 browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
254 result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1,
255 browse_list);
256
257 return (result ? A2DP_SUCCESS : A2DP_FAIL);
258 }
259
260 /******************************************************************************
261 *
262 * Function A2DP_FindService
263 *
264 * Description This function is called by a client application to
265 * perform service discovery and retrieve SRC or SNK SDP
266 * record information from a server. Information is
267 * returned for the first service record found on the
268 * server that matches the service UUID. The callback
269 * function will be executed when service discovery is
270 * complete. There can only be one outstanding call to
271 * A2DP_FindService() at a time; the application must wait
272 * for the callback before it makes another call to
273 * the function.
274 *
275 * Input Parameters:
276 * service_uuid: Indicates SRC or SNK.
277 *
278 * bd_addr: BD address of the peer device.
279 *
280 * p_db: Pointer to the information to initialize
281 * the discovery database.
282 *
283 * p_cback: Pointer to the A2DP_FindService()
284 * callback function.
285 *
286 * Output Parameters:
287 * None.
288 *
289 * Returns A2DP_SUCCESS if function execution succeeded,
290 * A2DP_INVALID_PARAMS if bad parameters are given.
291 * A2DP_BUSY if discovery is already in progress.
292 * A2DP_FAIL if function execution failed.
293 *
294 *****************************************************************************/
A2DP_FindService(uint16_t service_uuid,const RawAddress & bd_addr,tA2DP_SDP_DB_PARAMS * p_db,tA2DP_FIND_CBACK * p_cback)295 tA2DP_STATUS A2DP_FindService(uint16_t service_uuid, const RawAddress& bd_addr,
296 tA2DP_SDP_DB_PARAMS* p_db,
297 tA2DP_FIND_CBACK* p_cback) {
298 if ((service_uuid != UUID_SERVCLASS_AUDIO_SOURCE &&
299 service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
300 p_db == NULL || p_cback == NULL) {
301 LOG_ERROR("Cannot find service for peer %s UUID 0x%04x: invalid parameters",
302 ADDRESS_TO_LOGGABLE_CSTR(bd_addr), service_uuid);
303 return A2DP_INVALID_PARAMS;
304 }
305
306 if (a2dp_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE ||
307 a2dp_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK ||
308 a2dp_cb.find.p_db != NULL) {
309 LOG_ERROR("Cannot find service for peer %s UUID 0x%04x: busy",
310 ADDRESS_TO_LOGGABLE_CSTR(bd_addr), service_uuid);
311 return A2DP_BUSY;
312 }
313
314 if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
315 p_db->p_attrs = a2dp_attr_list;
316 p_db->num_attr = A2DP_NUM_ATTR;
317 }
318
319 a2dp_cb.find.p_db = (tSDP_DISCOVERY_DB*)osi_malloc(p_db->db_len);
320 Uuid uuid_list = Uuid::From16Bit(service_uuid);
321
322 if (!SDP_InitDiscoveryDb(a2dp_cb.find.p_db, p_db->db_len, 1, &uuid_list,
323 p_db->num_attr, p_db->p_attrs)) {
324 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
325 LOG_ERROR("Unable to initialize SDP discovery for peer %s UUID 0x%04X",
326 ADDRESS_TO_LOGGABLE_CSTR(bd_addr), service_uuid);
327 return A2DP_FAIL;
328 }
329
330 /* store service_uuid */
331 a2dp_cb.find.service_uuid = service_uuid;
332 a2dp_cb.find.p_cback = p_cback;
333
334 /* perform service search */
335 if (!SDP_ServiceSearchAttributeRequest(bd_addr, a2dp_cb.find.p_db,
336 a2dp_sdp_cback)) {
337 a2dp_cb.find.service_uuid = 0;
338 a2dp_cb.find.p_cback = NULL;
339 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
340 LOG_ERROR("Cannot find service for peer %s UUID 0x%04x: SDP error",
341 ADDRESS_TO_LOGGABLE_CSTR(bd_addr), service_uuid);
342 return A2DP_FAIL;
343 }
344 LOG_INFO("A2DP service discovery for peer %s UUID 0x%04x: SDP search started",
345 ADDRESS_TO_LOGGABLE_CSTR(bd_addr), service_uuid);
346 return A2DP_SUCCESS;
347 }
348
349 /******************************************************************************
350 *
351 * Function A2DP_SetTraceLevel
352 *
353 * Description Sets the trace level for A2D. If 0xff is passed, the
354 * current trace level is returned.
355 *
356 * Input Parameters:
357 * new_level: The level to set the A2DP tracing to:
358 * 0xff-returns the current setting.
359 * 0-turns off tracing.
360 * >= 1-Errors.
361 * >= 2-Warnings.
362 * >= 3-APIs.
363 * >= 4-Events.
364 * >= 5-Debug.
365 *
366 * Returns The new trace level or current trace level if
367 * the input parameter is 0xff.
368 *
369 *****************************************************************************/
A2DP_SetTraceLevel(uint8_t new_level)370 uint8_t A2DP_SetTraceLevel(uint8_t new_level) {
371 if (new_level != 0xFF) a2dp_cb.trace_level = new_level;
372
373 return (a2dp_cb.trace_level);
374 }
375
376 /******************************************************************************
377 * Function A2DP_BitsSet
378 *
379 * Description Check the given num for the number of bits set
380 * Returns A2DP_SET_ONE_BIT, if one and only one bit is set
381 * A2DP_SET_ZERO_BIT, if all bits clear
382 * A2DP_SET_MULTL_BIT, if multiple bits are set
383 *****************************************************************************/
A2DP_BitsSet(uint64_t num)384 uint8_t A2DP_BitsSet(uint64_t num) {
385 if (num == 0) return A2DP_SET_ZERO_BIT;
386 if ((num & (num - 1)) == 0) return A2DP_SET_ONE_BIT;
387 return A2DP_SET_MULTL_BIT;
388 }
389
390 /*******************************************************************************
391 *
392 * Function A2DP_Init
393 *
394 * Description This function is called to initialize the control block
395 * for this layer. It must be called before accessing any
396 * other API functions for this layer. It is typically called
397 * once during the start up of the stack.
398 *
399 * Returns void
400 *
401 ******************************************************************************/
A2DP_Init(void)402 void A2DP_Init(void) {
403 memset(&a2dp_cb, 0, sizeof(tA2DP_CB));
404
405 a2dp_cb.avdt_sdp_ver = AVDT_VERSION;
406
407 #if defined(A2DP_INITIAL_TRACE_LEVEL)
408 a2dp_cb.trace_level = A2DP_INITIAL_TRACE_LEVEL;
409 #else
410 a2dp_cb.trace_level = BT_TRACE_LEVEL_NONE;
411 #endif
412 }
413
A2DP_GetAvdtpVersion()414 uint16_t A2DP_GetAvdtpVersion() { return a2dp_cb.avdt_sdp_ver; }
415