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