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