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_set_a2dp_sdp_ver
139  *
140  * Description      This function allows the script wrapper to change the
141  *                  a2dp version
142  *
143  * Returns          None
144  *
145  ******************************************************************************/
a2d_set_a2dp_sdp_ver(UINT16 a2dp_sdp_ver)146 void a2d_set_a2dp_sdp_ver(UINT16 a2dp_sdp_ver)
147 {
148     a2d_cb.a2dp_sdp_ver = a2dp_sdp_ver;
149 }
150 
151 /******************************************************************************
152 **
153 ** Function         A2D_AddRecord
154 **
155 ** Description      This function is called by a server application to add
156 **                  SRC or SNK information to an SDP record.  Prior to
157 **                  calling this function the application must call
158 **                  SDP_CreateRecord() to create an SDP record.
159 **
160 **                  Input Parameters:
161 **                      service_uuid:  Indicates SRC or SNK.
162 **
163 **                      p_service_name:  Pointer to a null-terminated character
164 **                      string containing the service name.
165 **
166 **                      p_provider_name:  Pointer to a null-terminated character
167 **                      string containing the provider name.
168 **
169 **                      features:  Profile supported features.
170 **
171 **                      sdp_handle:  SDP handle returned by SDP_CreateRecord().
172 **
173 **                  Output Parameters:
174 **                      None.
175 **
176 ** Returns          A2D_SUCCESS if function execution succeeded,
177 **                  A2D_INVALID_PARAMS if bad parameters are given.
178 **                  A2D_FAIL if function execution failed.
179 **
180 ******************************************************************************/
A2D_AddRecord(UINT16 service_uuid,char * p_service_name,char * p_provider_name,UINT16 features,UINT32 sdp_handle)181 tA2D_STATUS A2D_AddRecord(UINT16 service_uuid, char *p_service_name, char *p_provider_name,
182                           UINT16 features, UINT32 sdp_handle)
183 {
184     UINT16      browse_list[1];
185     BOOLEAN     result = TRUE;
186     UINT8       temp[8];
187     UINT8       *p;
188     tSDP_PROTOCOL_ELEM  proto_list [A2D_NUM_PROTO_ELEMS];
189 
190     A2D_TRACE_API("A2D_AddRecord uuid: %x", service_uuid);
191 
192     if ( (sdp_handle == 0) ||
193             (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ) {
194         return A2D_INVALID_PARAMS;
195     }
196 
197     /* add service class id list */
198     result &= SDP_AddServiceClassIdList(sdp_handle, 1, &service_uuid);
199 
200     memset((void *) proto_list, 0 , A2D_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
201 
202     /* add protocol descriptor list   */
203     proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
204     proto_list[0].num_params = 1;
205     proto_list[0].params[0] = AVDT_PSM;
206     proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
207     proto_list[1].num_params = 1;
208     proto_list[1].params[0] = a2d_cb.avdt_sdp_ver;
209 
210     result &= SDP_AddProtocolList(sdp_handle, A2D_NUM_PROTO_ELEMS, proto_list);
211 
212     /* add profile descriptor list   */
213     result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, a2d_cb.a2dp_sdp_ver);
214 
215     /* add supported feature */
216     if (features != 0) {
217         p = temp;
218         UINT16_TO_BE_STREAM(p, features);
219         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
220                                    (UINT32)2, (UINT8 *)temp);
221     }
222 
223     /* add provider name */
224     if (p_provider_name != NULL) {
225         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
226                                    (UINT32)(strlen(p_provider_name) + 1), (UINT8 *) p_provider_name);
227     }
228 
229     /* add service name */
230     if (p_service_name != NULL) {
231         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
232                                    (UINT32)(strlen(p_service_name) + 1), (UINT8 *) p_service_name);
233     }
234 
235     /* add browse group list */
236     browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
237     result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
238 
239 
240     return (result ? A2D_SUCCESS : A2D_FAIL);
241 }
242 
243 /******************************************************************************
244 **
245 ** Function         A2D_FindService
246 **
247 ** Description      This function is called by a client application to
248 **                  perform service discovery and retrieve SRC or SNK SDP
249 **                  record information from a server.  Information is
250 **                  returned for the first service record found on the
251 **                  server that matches the service UUID.  The callback
252 **                  function will be executed when service discovery is
253 **                  complete.  There can only be one outstanding call to
254 **                  A2D_FindService() at a time; the application must wait
255 **                  for the callback before it makes another call to
256 **                  the function.
257 **
258 **                  Input Parameters:
259 **                      service_uuid:  Indicates SRC or SNK.
260 **
261 **                      bd_addr:  BD address of the peer device.
262 **
263 **                      p_db:  Pointer to the information to initialize
264 **                             the discovery database.
265 **
266 **                      p_cback:  Pointer to the A2D_FindService()
267 **                      callback function.
268 **
269 **                  Output Parameters:
270 **                      None.
271 **
272 ** Returns          A2D_SUCCESS if function execution succeeded,
273 **                  A2D_INVALID_PARAMS if bad parameters are given.
274 **                  A2D_BUSY if discovery is already in progress.
275 **                  A2D_FAIL if function execution failed.
276 **
277 ******************************************************************************/
A2D_FindService(UINT16 service_uuid,BD_ADDR bd_addr,tA2D_SDP_DB_PARAMS * p_db,tA2D_FIND_CBACK * p_cback)278 tA2D_STATUS A2D_FindService(UINT16 service_uuid, BD_ADDR bd_addr,
279                             tA2D_SDP_DB_PARAMS *p_db, tA2D_FIND_CBACK *p_cback)
280 {
281     tSDP_UUID   uuid_list;
282     BOOLEAN     result = TRUE;
283     UINT16      a2d_attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2D_NUM_ATTR, if changed */
284                                    ATTR_ID_BT_PROFILE_DESC_LIST,
285                                    ATTR_ID_SUPPORTED_FEATURES,
286                                    ATTR_ID_SERVICE_NAME,
287                                    ATTR_ID_PROTOCOL_DESC_LIST,
288                                    ATTR_ID_PROVIDER_NAME
289                                   };
290 
291     A2D_TRACE_API("A2D_FindService uuid: %x", service_uuid);
292     if ( (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
293             p_db == NULL || p_db->p_db == NULL || p_cback == NULL) {
294         return A2D_INVALID_PARAMS;
295     }
296 
297     if ( a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE ||
298             a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK) {
299         return A2D_BUSY;
300     }
301 
302     /* set up discovery database */
303     uuid_list.len = LEN_UUID_16;
304     uuid_list.uu.uuid16 = service_uuid;
305 
306     if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
307         p_db->p_attrs  = a2d_attr_list;
308         p_db->num_attr = A2D_NUM_ATTR;
309     }
310 
311     result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr,
312                                  p_db->p_attrs);
313 
314     if (result == TRUE) {
315         /* store service_uuid and discovery db pointer */
316         a2d_cb.find.p_db = p_db->p_db;
317         a2d_cb.find.service_uuid = service_uuid;
318         a2d_cb.find.p_cback = p_cback;
319 
320         /* perform service search */
321         result = SDP_ServiceSearchAttributeRequest(bd_addr, p_db->p_db, a2d_sdp_cback);
322         if (FALSE == result) {
323             a2d_cb.find.service_uuid = 0;
324         }
325     }
326 
327     return (result ? A2D_SUCCESS : A2D_FAIL);
328 }
329 
330 /******************************************************************************
331 **
332 ** Function         A2D_SetTraceLevel
333 **
334 ** Description      Sets the trace level for A2D. If 0xff is passed, the
335 **                  current trace level is returned.
336 **
337 **                  Input Parameters:
338 **                      new_level:  The level to set the A2D tracing to:
339 **                      0xff-returns the current setting.
340 **                      0-turns off tracing.
341 **                      >= 1-Errors.
342 **                      >= 2-Warnings.
343 **                      >= 3-APIs.
344 **                      >= 4-Events.
345 **                      >= 5-Debug.
346 **
347 ** Returns          The new trace level or current trace level if
348 **                  the input parameter is 0xff.
349 **
350 ******************************************************************************/
A2D_SetTraceLevel(UINT8 new_level)351 UINT8 A2D_SetTraceLevel (UINT8 new_level)
352 {
353     if (new_level != 0xFF) {
354         a2d_cb.trace_level = new_level;
355     }
356 
357     return (a2d_cb.trace_level);
358 }
359 
360 /******************************************************************************
361 ** Function         A2D_BitsSet
362 **
363 ** Description      Check the given num for the number of bits set
364 ** Returns          A2D_SET_ONE_BIT, if one and only one bit is set
365 **                  A2D_SET_ZERO_BIT, if all bits clear
366 **                  A2D_SET_MULTL_BIT, if multiple bits are set
367 ******************************************************************************/
A2D_BitsSet(UINT8 num)368 UINT8 A2D_BitsSet(UINT8 num)
369 {
370     UINT8 count;
371     UINT8 res;
372     if (num == 0) {
373         res = A2D_SET_ZERO_BIT;
374     } else {
375         count = (num & (num - 1));
376         res = ((count == 0) ? A2D_SET_ONE_BIT : A2D_SET_MULTL_BIT);
377     }
378     return res;
379 }
380 
381 /*******************************************************************************
382 **
383 ** Function         A2D_Init
384 **
385 ** Description      This function is called to initialize the control block
386 **                  for this layer.  It must be called before accessing any
387 **                  other API functions for this layer.  It is typically called
388 **                  once during the start up of the stack.
389 **
390 ** Returns          status
391 **
392 *******************************************************************************/
A2D_Init(void)393 bt_status_t A2D_Init(void)
394 {
395 #if (A2D_DYNAMIC_MEMORY)
396     a2d_cb_ptr = (tA2D_CB *)osi_malloc(sizeof(tA2D_CB));
397     if (!a2d_cb_ptr) {
398         return BT_STATUS_NOMEM;
399     }
400 #endif /* #if (A2D_DYNAMIC_MEMORY) */
401     memset(&a2d_cb, 0, sizeof(tA2D_CB));
402 
403     a2d_cb.avdt_sdp_ver = AVDT_VERSION;
404     a2d_cb.a2dp_sdp_ver = A2D_VERSION;
405 
406 #if defined(A2D_INITIAL_TRACE_LEVEL)
407     a2d_cb.trace_level  = A2D_INITIAL_TRACE_LEVEL;
408 #else
409     a2d_cb.trace_level  = BT_TRACE_LEVEL_NONE;
410 #endif
411     return BT_STATUS_SUCCESS;
412 }
413 
414 /*******************************************************************************
415 **
416 ** Function         A2D_Deinit
417 **
418 ** Description      This function is called to deinitialize the control block
419 **                  for this layer.
420 **
421 ** Returns          void
422 **
423 *******************************************************************************/
A2D_Deinit(void)424 void A2D_Deinit(void)
425 {
426 #if (A2D_DYNAMIC_MEMORY)
427     if (a2d_cb_ptr) {
428         osi_free(a2d_cb_ptr);
429         a2d_cb_ptr = NULL;
430     }
431 #endif /* #if (A2D_DYNAMIC_MEMORY) */
432 }
433 
434 #endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */
435