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