summaryrefslogtreecommitdiffstats
path: root/stack/a2dp/a2d_api.c
blob: dbff646f19562dd7aea2777cb13a844756e7d3ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*****************************************************************************
**
**  Name:       a2d_api.c
**
**  Description:Common API for the Advanced Audio Distribution Profile (A2DP)
**
**  Copyright (c) 2002-2009, Broadcom Corp., All Rights Reserved.
**  WIDCOMM Bluetooth Core. Proprietary and confidential.
**
*****************************************************************************/
#include <string.h>
#include "bt_target.h"
#include "sdpdefs.h"
#include "a2d_api.h"
#include "a2d_int.h"
#include "avdt_api.h"

/*****************************************************************************
**  Global data
*****************************************************************************/
#if A2D_DYNAMIC_MEMORY == FALSE
tA2D_CB a2d_cb;
#endif


/******************************************************************************
**
** Function         a2d_sdp_cback
**
** Description      This is the SDP callback function used by A2D_FindService.
**                  This function will be executed by SDP when the service
**                  search is completed.  If the search is successful, it
**                  finds the first record in the database that matches the
**                  UUID of the search.  Then retrieves various parameters
**                  from the record.  When it is finished it calls the
**                  application callback function.
**
** Returns          Nothing.
**
******************************************************************************/
static void a2d_sdp_cback(UINT16 status)
{
    tSDP_DISC_REC       *p_rec = NULL;
    tSDP_DISC_ATTR      *p_attr;
    BOOLEAN             found = FALSE;
    tA2D_Service        a2d_svc;
    tSDP_PROTOCOL_ELEM  elem;

    A2D_TRACE_API1("a2d_sdp_cback status: %d", status);

    if (status == SDP_SUCCESS)
    {
        /* loop through all records we found */
        do
        {
            /* get next record; if none found, we're done */
            if ((p_rec = SDP_FindServiceInDb(a2d_cb.find.p_db, 
                            a2d_cb.find.service_uuid, p_rec)) == NULL)
            {
                break;
            }
            memset(&a2d_svc, 0, sizeof(tA2D_Service));

            /* get service name */
            if ((p_attr = SDP_FindAttributeInRec(p_rec,
                            ATTR_ID_SERVICE_NAME)) != NULL)
            {
                a2d_svc.p_service_name = (char *) p_attr->attr_value.v.array;
                a2d_svc.service_len    = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
            }

            /* get provider name */
            if ((p_attr = SDP_FindAttributeInRec(p_rec,
                            ATTR_ID_PROVIDER_NAME)) != NULL)
            {
                a2d_svc.p_provider_name = (char *) p_attr->attr_value.v.array;
                a2d_svc.provider_len    = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
            }

            /* get supported features */
            if ((p_attr = SDP_FindAttributeInRec(p_rec,
                            ATTR_ID_SUPPORTED_FEATURES)) != NULL)
            {
                a2d_svc.features = p_attr->attr_value.v.u16;
            }

            /* get AVDTP version */
            if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_AVDTP, &elem))
            {
                a2d_svc.avdt_version = elem.params[0];
                A2D_TRACE_DEBUG1("avdt_version: 0x%x", a2d_svc.avdt_version);
            }

            /* we've got everything, we're done */
            found = TRUE;
            break;

        } while (TRUE);
    }

    a2d_cb.find.service_uuid = 0;
    /* return info from sdp record in app callback function */
    if (a2d_cb.find.p_cback != NULL)
    {
        (*a2d_cb.find.p_cback)(found, &a2d_svc);
    }

    return;
}

/*******************************************************************************
**
** Function         a2d_set_avdt_sdp_ver
**
** Description      This function allows the script wrapper to change the 
**                  avdt version of a2dp.
**
** Returns          None
**
*******************************************************************************/
void a2d_set_avdt_sdp_ver (UINT16 avdt_sdp_ver)
{
    a2d_cb.avdt_sdp_ver = avdt_sdp_ver;
}

/******************************************************************************
**
** Function         A2D_AddRecord
**
** Description      This function is called by a server application to add
**                  SRC or SNK information to an SDP record.  Prior to
**                  calling this function the application must call
**                  SDP_CreateRecord() to create an SDP record.
**
**                  Input Parameters:
**                      service_uuid:  Indicates SRC or SNK.
**
**                      p_service_name:  Pointer to a null-terminated character
**                      string containing the service name.
**
**                      p_provider_name:  Pointer to a null-terminated character
**                      string containing the provider name.
**
**                      features:  Profile supported features.
**
**                      sdp_handle:  SDP handle returned by SDP_CreateRecord().
**
**                  Output Parameters:
**                      None.
**
** Returns          A2D_SUCCESS if function execution succeeded,
**                  A2D_INVALID_PARAMS if bad parameters are given.
**                  A2D_FAIL if function execution failed.
**
******************************************************************************/
tA2D_STATUS A2D_AddRecord(UINT16 service_uuid, char *p_service_name, char *p_provider_name,
        UINT16 features, UINT32 sdp_handle)
{
    UINT16      browse_list[1];
    BOOLEAN     result = TRUE;
    UINT8       temp[8];
    UINT8       *p;
    tSDP_PROTOCOL_ELEM  proto_list [A2D_NUM_PROTO_ELEMS];

    A2D_TRACE_API1("A2D_AddRecord uuid: %x", service_uuid);

    if( (sdp_handle == 0) ||
        (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) )
        return A2D_INVALID_PARAMS;

    /* add service class id list */
    result &= SDP_AddServiceClassIdList(sdp_handle, 1, &service_uuid);

    memset((void*) proto_list, 0 , A2D_NUM_PROTO_ELEMS*sizeof(tSDP_PROTOCOL_ELEM));
        
    /* add protocol descriptor list   */
    proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
    proto_list[0].num_params = 1;
    proto_list[0].params[0] = AVDT_PSM;
    proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
    proto_list[1].num_params = 1;
    proto_list[1].params[0] = a2d_cb.avdt_sdp_ver;

    result &= SDP_AddProtocolList(sdp_handle, A2D_NUM_PROTO_ELEMS, proto_list);

    /* add profile descriptor list   */
    result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, A2D_VERSION);

    /* add supported feature */
    if (features != 0)
    {
        p = temp;
        UINT16_TO_BE_STREAM(p, features);
        result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
                  (UINT32)2, (UINT8*)temp);
    }

    /* add provider name */
    if (p_provider_name != NULL)
    {
        result &= SDP_AddAttribute(sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
                    (UINT32)(strlen(p_provider_name)+1), (UINT8 *) p_provider_name);
    }

    /* add service name */
    if (p_service_name != NULL)
    {
        result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
                    (UINT32)(strlen(p_service_name)+1), (UINT8 *) p_service_name);
    }

    /* add browse group list */
    browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
    result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);


    return (result ? A2D_SUCCESS : A2D_FAIL);
}

/******************************************************************************
**
** Function         A2D_FindService
**
** Description      This function is called by a client application to
**                  perform service discovery and retrieve SRC or SNK SDP
**                  record information from a server.  Information is
**                  returned for the first service record found on the
**                  server that matches the service UUID.  The callback
**                  function will be executed when service discovery is
**                  complete.  There can only be one outstanding call to
**                  A2D_FindService() at a time; the application must wait
**                  for the callback before it makes another call to
**                  the function.
**
**                  Input Parameters:
**                      service_uuid:  Indicates SRC or SNK.
**
**                      bd_addr:  BD address of the peer device.
**
**                      p_db:  Pointer to the information to initialize
**                             the discovery database.
**                      
**                      p_cback:  Pointer to the A2D_FindService()
**                      callback function.
**
**                  Output Parameters:
**                      None.
**
** Returns          A2D_SUCCESS if function execution succeeded,
**                  A2D_INVALID_PARAMS if bad parameters are given.
**                  A2D_BUSY if discovery is already in progress.
**                  A2D_FAIL if function execution failed.
**
******************************************************************************/
tA2D_STATUS A2D_FindService(UINT16 service_uuid, BD_ADDR bd_addr,
                        tA2D_SDP_DB_PARAMS *p_db, tA2D_FIND_CBACK *p_cback)
{
    tSDP_UUID   uuid_list;
    BOOLEAN     result = TRUE;
    UINT16      a2d_attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2D_NUM_ATTR, if changed */
                                   ATTR_ID_BT_PROFILE_DESC_LIST,
                                   ATTR_ID_SUPPORTED_FEATURES,
                                   ATTR_ID_SERVICE_NAME,
                                   ATTR_ID_PROTOCOL_DESC_LIST,
                                   ATTR_ID_PROVIDER_NAME};

    A2D_TRACE_API1("A2D_FindService uuid: %x", service_uuid);
    if( (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
        p_db == NULL || p_db->p_db == NULL || p_cback == NULL)
        return A2D_INVALID_PARAMS;

    if( a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE || 
        a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK)
        return A2D_BUSY;

    /* set up discovery database */
    uuid_list.len = LEN_UUID_16;
    uuid_list.uu.uuid16 = service_uuid;

    if(p_db->p_attrs == NULL || p_db->num_attr == 0)
    {
        p_db->p_attrs  = a2d_attr_list;
        p_db->num_attr = A2D_NUM_ATTR;
    }

    result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr,
                                 p_db->p_attrs);

    if (result == TRUE)
    {
        /* store service_uuid and discovery db pointer */
        a2d_cb.find.p_db = p_db->p_db;
        a2d_cb.find.service_uuid = service_uuid;
        a2d_cb.find.p_cback = p_cback;

        /* perform service search */
        result = SDP_ServiceSearchAttributeRequest(bd_addr, p_db->p_db, a2d_sdp_cback);
        if(FALSE == result)
        {
            a2d_cb.find.service_uuid = 0;
        }
    }

    return (result ? A2D_SUCCESS : A2D_FAIL);
}

/******************************************************************************
**
** Function         A2D_SetTraceLevel
**
** Description      Sets the trace level for A2D. If 0xff is passed, the
**                  current trace level is returned.
**
**                  Input Parameters:
**                      new_level:  The level to set the A2D tracing to:
**                      0xff-returns the current setting.
**                      0-turns off tracing.
**                      >= 1-Errors.
**                      >= 2-Warnings.
**                      >= 3-APIs.
**                      >= 4-Events.
**                      >= 5-Debug.
**
** Returns          The new trace level or current trace level if
**                  the input parameter is 0xff.
**
******************************************************************************/
UINT8 A2D_SetTraceLevel (UINT8 new_level)
{
    if (new_level != 0xFF)
        a2d_cb.trace_level = new_level;

    return (a2d_cb.trace_level);
}

/******************************************************************************
** Function         A2D_BitsSet
**
** Description      Check the given num for the number of bits set
** Returns          A2D_SET_ONE_BIT, if one and only one bit is set
**                  A2D_SET_ZERO_BIT, if all bits clear
**                  A2D_SET_MULTL_BIT, if multiple bits are set
******************************************************************************/
UINT8 A2D_BitsSet(UINT8 num)
{
    UINT8   count;
    BOOLEAN res;
    if(num == 0)
        res = A2D_SET_ZERO_BIT;
    else
    {
        count = (num & (num - 1));
        res = ((count==0)?A2D_SET_ONE_BIT:A2D_SET_MULTL_BIT);
    }
    return res;
}

/*******************************************************************************
**
** Function         A2D_Init
**
** Description      This function is called to initialize the control block
**                  for this layer.  It must be called before accessing any 
**                  other API functions for this layer.  It is typically called
**                  once during the start up of the stack.  
**
** Returns          void
**
*******************************************************************************/
void A2D_Init(void)
{
    memset(&a2d_cb, 0, sizeof(tA2D_CB));

    a2d_cb.avdt_sdp_ver = AVDT_VERSION;
    
#if defined(A2D_INITIAL_TRACE_LEVEL)
    a2d_cb.trace_level  = A2D_INITIAL_TRACE_LEVEL;
#else
    a2d_cb.trace_level  = BT_TRACE_LEVEL_NONE;
#endif
}