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