bta_ag_sdp.cc revision d7ffd64accbd50a27289a388856e56244ccbb5da
1/******************************************************************************
2 *
3 *  Copyright (C) 2003-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 *  This file contains the audio gateway functions performing SDP
22 *  operations.
23 *
24 ******************************************************************************/
25
26#include <string.h>
27#include "bta_api.h"
28#include "bta_sys.h"
29#include "bta_ag_api.h"
30#include "bta_ag_int.h"
31#include "sdp_api.h"
32#include "btm_api.h"
33#include "bt_common.h"
34#include "osi/include/osi.h"
35#include "utl.h"
36
37/* Number of protocol elements in protocol element list. */
38#define BTA_AG_NUM_PROTO_ELEMS      2
39
40/* Number of elements in service class id list. */
41#define BTA_AG_NUM_SVC_ELEMS        2
42
43/* size of database for service discovery */
44#ifndef BTA_AG_DISC_BUF_SIZE
45#define BTA_AG_DISC_BUF_SIZE        BT_DEFAULT_BUFFER_SIZE
46#endif
47
48/* declare sdp callback functions */
49void bta_ag_sdp_cback_1(uint16_t status);
50void bta_ag_sdp_cback_2(uint16_t status);
51void bta_ag_sdp_cback_3(uint16_t status);
52
53/* SDP callback function table */
54typedef tSDP_DISC_CMPL_CB *tBTA_AG_SDP_CBACK;
55const tBTA_AG_SDP_CBACK bta_ag_sdp_cback_tbl[] =
56{
57    bta_ag_sdp_cback_1,
58    bta_ag_sdp_cback_2,
59    bta_ag_sdp_cback_3
60};
61
62/*******************************************************************************
63**
64** Function         bta_ag_sdp_cback
65**
66** Description      SDP callback function.
67**
68**
69** Returns          void
70**
71*******************************************************************************/
72static void bta_ag_sdp_cback(uint16_t status, uint8_t idx)
73{
74    uint16_t             event;
75    tBTA_AG_SCB         *p_scb;
76
77    APPL_TRACE_DEBUG("%s status:0x%x", __func__, status);
78
79    if ((p_scb = bta_ag_scb_by_idx(idx)) != NULL)
80    {
81        /* set event according to int/acp */
82        if (p_scb->role == BTA_AG_ACP)
83        {
84            event = BTA_AG_DISC_ACP_RES_EVT;
85        }
86        else
87        {
88            event = BTA_AG_DISC_INT_RES_EVT;
89        }
90
91        tBTA_AG_DISC_RESULT *p_buf =
92            (tBTA_AG_DISC_RESULT *)osi_malloc(sizeof(tBTA_AG_DISC_RESULT));
93        p_buf->hdr.event = event;
94        p_buf->hdr.layer_specific = idx;
95        p_buf->status = status;
96        bta_sys_sendmsg(p_buf);
97    }
98}
99
100/*******************************************************************************
101**
102** Function         bta_ag_sdp_cback_1 to 3
103**
104** Description      SDP callback functions.  Since there is no way to
105**                  distinguish scb from the callback we need separate
106**                  callbacks for each scb.
107**
108**
109** Returns          void
110**
111*******************************************************************************/
112void bta_ag_sdp_cback_1(uint16_t status) {bta_ag_sdp_cback(status, 1);}
113void bta_ag_sdp_cback_2(uint16_t status) {bta_ag_sdp_cback(status, 2);}
114void bta_ag_sdp_cback_3(uint16_t status) {bta_ag_sdp_cback(status, 3);}
115
116/******************************************************************************
117**
118** Function         bta_ag_add_record
119**
120** Description      This function is called by a server application to add
121**                  HSP or HFP information to an SDP record.  Prior to
122**                  calling this function the application must call
123**                  SDP_CreateRecord() to create an SDP record.
124**
125** Returns          true if function execution succeeded,
126**                  false if function execution failed.
127**
128******************************************************************************/
129bool bta_ag_add_record(uint16_t service_uuid, char *p_service_name, uint8_t scn,
130                          tBTA_AG_FEAT features, uint32_t sdp_handle)
131{
132    tSDP_PROTOCOL_ELEM  proto_elem_list[BTA_AG_NUM_PROTO_ELEMS];
133    uint16_t            svc_class_id_list[BTA_AG_NUM_SVC_ELEMS];
134    uint16_t            browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
135    uint16_t            version;
136    uint16_t            profile_uuid;
137    uint8_t             network;
138    bool                result = true;
139    bool                codec_supported = false;
140    uint8_t             buf[2];
141
142    APPL_TRACE_DEBUG("%s uuid: %x", __func__, service_uuid);
143
144    memset( proto_elem_list, 0 , BTA_AG_NUM_PROTO_ELEMS*sizeof(tSDP_PROTOCOL_ELEM));
145
146    /* add the protocol element sequence */
147    proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
148    proto_elem_list[0].num_params = 0;
149    proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
150    proto_elem_list[1].num_params = 1;
151    proto_elem_list[1].params[0] = scn;
152    result &= SDP_AddProtocolList(sdp_handle, BTA_AG_NUM_PROTO_ELEMS, proto_elem_list);
153
154    /* add service class id list */
155    svc_class_id_list[0] = service_uuid;
156    svc_class_id_list[1] = UUID_SERVCLASS_GENERIC_AUDIO;
157    result &= SDP_AddServiceClassIdList(sdp_handle, BTA_AG_NUM_SVC_ELEMS, svc_class_id_list);
158
159    /* add profile descriptor list */
160    if (service_uuid == UUID_SERVCLASS_AG_HANDSFREE)
161    {
162        profile_uuid = UUID_SERVCLASS_HF_HANDSFREE;
163        version = BTA_HFP_VERSION;
164    }
165    else
166    {
167        profile_uuid = UUID_SERVCLASS_HEADSET;
168        version = HSP_VERSION_1_2;
169    }
170    result &= SDP_AddProfileDescriptorList(sdp_handle, profile_uuid, version);
171
172    /* add service name */
173    if (p_service_name != NULL && p_service_name[0] != 0)
174    {
175        result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
176                    (uint32_t)(strlen(p_service_name)+1), (uint8_t *) p_service_name);
177    }
178
179    /* add features and network */
180    if (service_uuid == UUID_SERVCLASS_AG_HANDSFREE)
181    {
182        network = (features & BTA_AG_FEAT_REJECT) ? 1 : 0;
183        result &= SDP_AddAttribute(sdp_handle, ATTR_ID_DATA_STORES_OR_NETWORK,
184                    UINT_DESC_TYPE, 1, &network);
185
186        if (features & BTA_AG_FEAT_CODEC)
187            codec_supported = true;
188
189        features &= BTA_AG_SDP_FEAT_SPEC;
190
191        /* Codec bit position is different in SDP and in BRSF */
192        if (codec_supported)
193            features |= 0x0020;
194
195        UINT16_TO_BE_FIELD(buf, features);
196        result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, 2, buf);
197    }
198
199    /* add browse group list */
200    result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
201
202    return result;
203}
204
205/*******************************************************************************
206**
207** Function         bta_ag_create_records
208**
209** Description      Create SDP records for registered services.
210**
211**
212** Returns          void
213**
214*******************************************************************************/
215void bta_ag_create_records(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
216{
217    int                 i;
218    tBTA_SERVICE_MASK   services;
219
220    services = p_scb->reg_services >> BTA_HSP_SERVICE_ID;
221    for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1)
222    {
223        /* if service is set in mask */
224        if (services & 1)
225        {
226            /* add sdp record if not already registered */
227            if (bta_ag_cb.profile[i].sdp_handle == 0)
228            {
229                bta_ag_cb.profile[i].sdp_handle = SDP_CreateRecord();
230                bta_ag_cb.profile[i].scn = BTM_AllocateSCN();
231                bta_ag_add_record(bta_ag_uuid[i], p_data->api_register.p_name[i],
232                    bta_ag_cb.profile[i].scn, p_data->api_register.features,
233                    bta_ag_cb.profile[i].sdp_handle);
234                bta_sys_add_uuid(bta_ag_uuid[i]);
235            }
236        }
237    }
238
239    p_scb->hsp_version = HSP_VERSION_1_2;
240
241}
242
243/*******************************************************************************
244**
245** Function         bta_ag_del_records
246**
247** Description      Delete SDP records for any registered services.
248**
249**
250** Returns          void
251**
252*******************************************************************************/
253void bta_ag_del_records(tBTA_AG_SCB *p_scb,
254                        UNUSED_ATTR tBTA_AG_DATA *p_data)
255{
256    tBTA_AG_SCB         *p = &bta_ag_cb.scb[0];
257    tBTA_SERVICE_MASK   services;
258    tBTA_SERVICE_MASK   others = 0;
259    int                 i;
260
261    /* get services of all other registered servers */
262    for (i = 0; i < BTA_AG_NUM_IDX; i++, p++)
263    {
264        if (p_scb == p)
265        {
266            continue;
267        }
268
269        if (p->in_use && p->dealloc == false)
270        {
271            others |= p->reg_services;
272        }
273    }
274
275    others >>= BTA_HSP_SERVICE_ID;
276    services = p_scb->reg_services >> BTA_HSP_SERVICE_ID;
277    for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1, others >>= 1)
278    {
279        /* if service registered for this scb and not registered for any other scb */
280        if (((services & 1) == 1) && ((others & 1) == 0))
281        {
282            APPL_TRACE_DEBUG("bta_ag_del_records %d", i);
283            if (bta_ag_cb.profile[i].sdp_handle != 0)
284            {
285                SDP_DeleteRecord(bta_ag_cb.profile[i].sdp_handle);
286                bta_ag_cb.profile[i].sdp_handle = 0;
287            }
288            BTM_FreeSCN(bta_ag_cb.profile[i].scn);
289            BTM_SecClrService(bta_ag_sec_id[i]);
290            bta_sys_remove_uuid(bta_ag_uuid[i]);
291        }
292    }
293}
294
295/*******************************************************************************
296**
297** Function         bta_ag_sdp_find_attr
298**
299** Description      Process SDP discovery results to find requested attributes
300**                  for requested service.
301**
302**
303** Returns          true if results found, false otherwise.
304**
305*******************************************************************************/
306bool bta_ag_sdp_find_attr(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK service)
307{
308    tSDP_DISC_REC       *p_rec = NULL;
309    tSDP_DISC_ATTR      *p_attr;
310    tSDP_PROTOCOL_ELEM  pe;
311    uint16_t              uuid;
312    bool             result = false;
313
314    if (service & BTA_HFP_SERVICE_MASK)
315    {
316        uuid = UUID_SERVCLASS_HF_HANDSFREE;
317        p_scb->peer_version = HFP_VERSION_1_1;   /* Default version */
318    }
319    else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT)
320    {
321        uuid = UUID_SERVCLASS_HEADSET_HS;
322        p_scb->peer_version = 0x0100;   /* Default version */
323    }
324    else
325    {
326        return result;
327    }
328
329    /* loop through all records we found */
330    while (true)
331    {
332        /* get next record; if none found, we're done */
333        if ((p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec)) == NULL)
334        {
335            if (uuid == UUID_SERVCLASS_HEADSET_HS)
336            {
337                /* Search again in case the peer device is HSP v1.0 */
338                uuid = UUID_SERVCLASS_HEADSET;
339                if ((p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec)) == NULL)
340                {
341                    break;
342                }
343            }
344            else
345                break;
346        }
347
348        /* get scn from proto desc list if initiator */
349        if (p_scb->role == BTA_AG_INT)
350        {
351            if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe))
352            {
353                p_scb->peer_scn = (uint8_t) pe.params[0];
354            }
355            else
356            {
357                continue;
358            }
359        }
360
361        /* get profile version (if failure, version parameter is not updated) */
362        SDP_FindProfileVersionInRec(p_rec, uuid, &p_scb->peer_version);
363
364        /* get features if HFP */
365        if (service & BTA_HFP_SERVICE_MASK)
366        {
367            if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL)
368            {
369                /* Found attribute. Get value. */
370                /* There might be race condition between SDP and BRSF.  */
371                /* Do not update if we already received BRSF.           */
372                if (p_scb->peer_features == 0)
373                    p_scb->peer_features = p_attr->attr_value.v.u16;
374            }
375        }
376        else    /* HSP */
377        {
378            if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL)) != NULL)
379            {
380                /* Remote volume control of HSP */
381                if (p_attr->attr_value.v.u8)
382                    p_scb->peer_features |= BTA_AG_PEER_FEAT_VOL;
383                else
384                    p_scb->peer_features &= ~BTA_AG_PEER_FEAT_VOL;
385            }
386
387        }
388
389        /* found what we needed */
390        result = true;
391        break;
392    }
393    return result;
394}
395
396/*******************************************************************************
397**
398** Function         bta_ag_do_disc
399**
400** Description      Do service discovery.
401**
402**
403** Returns          void
404**
405*******************************************************************************/
406void bta_ag_do_disc(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK service)
407{
408    tSDP_UUID       uuid_list[2];
409    uint16_t          num_uuid = 1;
410    uint16_t          attr_list[4];
411    uint8_t           num_attr;
412    bool         db_inited = false;
413
414    /* HFP initiator; get proto list and features */
415    if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_INT)
416    {
417        attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
418        attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
419        attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
420        attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
421        num_attr = 4;
422        uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
423    }
424    /* HFP acceptor; get features */
425    else if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_ACP)
426    {
427        attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
428        attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
429        attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
430        num_attr = 3;
431        uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
432    }
433    /* HSP initiator; get proto list */
434    else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT)
435    {
436        attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
437        attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
438        attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
439        attr_list[3] = ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL;
440        num_attr = 4;
441
442        uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HEADSET;        /* Legacy from HSP v1.0 */
443        if (p_scb->hsp_version >= HSP_VERSION_1_2)
444        {
445            uuid_list[1].uu.uuid16 = UUID_SERVCLASS_HEADSET_HS;
446            num_uuid = 2;
447        }
448    }
449    /* HSP acceptor; no discovery */
450    else
451    {
452        return;
453    }
454
455    /* allocate buffer for sdp database */
456    p_scb->p_disc_db = (tSDP_DISCOVERY_DB *)osi_malloc(BTA_AG_DISC_BUF_SIZE);
457    /* set up service discovery database; attr happens to be attr_list len */
458    uuid_list[0].len = LEN_UUID_16;
459    uuid_list[1].len = LEN_UUID_16;
460    db_inited = SDP_InitDiscoveryDb(p_scb->p_disc_db, BTA_AG_DISC_BUF_SIZE,
461                                    num_uuid, uuid_list, num_attr, attr_list);
462
463    if(db_inited)
464    {
465        /*Service discovery not initiated */
466        db_inited = SDP_ServiceSearchAttributeRequest(p_scb->peer_addr, p_scb->p_disc_db,
467                                      bta_ag_sdp_cback_tbl[bta_ag_scb_to_idx(p_scb) - 1]);
468    }
469
470    if(!db_inited)
471    {
472        /*free discover db */
473        bta_ag_free_db(p_scb, NULL);
474        /* sent failed event */
475        bta_ag_sm_execute(p_scb, BTA_AG_DISC_FAIL_EVT, NULL);
476    }
477
478}
479
480/*******************************************************************************
481**
482** Function         bta_ag_free_db
483**
484** Description      Free discovery database.
485**
486**
487** Returns          void
488**
489*******************************************************************************/
490void bta_ag_free_db(tBTA_AG_SCB *p_scb,
491                    UNUSED_ATTR tBTA_AG_DATA *p_data)
492{
493    osi_free_and_reset((void **)&p_scb->p_disc_db);
494}
495