Home | History | Annotate | Download | only in hf_client
      1 /******************************************************************************
      2  *
      3  *  Copyright (c) 2014 The Android Open Source Project
      4  *  Copyright (C) 2003-2012 Broadcom Corporation
      5  *
      6  *  Licensed under the Apache License, Version 2.0 (the "License");
      7  *  you may not use this file except in compliance with the License.
      8  *  You may obtain a copy of the License at:
      9  *
     10  *  http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  *  Unless required by applicable law or agreed to in writing, software
     13  *  distributed under the License is distributed on an "AS IS" BASIS,
     14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  *  See the License for the specific language governing permissions and
     16  *  limitations under the License.
     17  *
     18  ******************************************************************************/
     19 
     20 /******************************************************************************
     21  *
     22  *  This file contains the audio gateway functions performing SDP
     23  *  operations.
     24  *
     25  ******************************************************************************/
     26 
     27 #include <string.h>
     28 #include "bta_api.h"
     29 #include "bta_sys.h"
     30 #include "bt_utils.h"
     31 #include "bta_hf_client_api.h"
     32 #include "bta_hf_client_int.h"
     33 
     34 /* Number of protocol elements in protocol element list. */
     35 #define BTA_HF_CLIENT_NUM_PROTO_ELEMS      2
     36 
     37 /* Number of elements in service class id list. */
     38 #define BTA_HF_CLIENT_NUM_SVC_ELEMS        2
     39 
     40 /*******************************************************************************
     41 **
     42 ** Function         bta_hf_client_sdp_cback
     43 **
     44 ** Description      SDP callback function.
     45 **
     46 **
     47 ** Returns          void
     48 **
     49 *******************************************************************************/
     50 static void bta_hf_client_sdp_cback(UINT16 status)
     51 {
     52     UINT16 event;
     53     tBTA_HF_CLIENT_DISC_RESULT *p_buf =
     54         (tBTA_HF_CLIENT_DISC_RESULT *)osi_malloc(sizeof(tBTA_HF_CLIENT_DISC_RESULT));
     55 
     56     APPL_TRACE_DEBUG("bta_hf_client_sdp_cback status:0x%x", status);
     57 
     58     /* set event according to int/acp */
     59     if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_ACP)
     60         event = BTA_HF_CLIENT_DISC_ACP_RES_EVT;
     61     else
     62         event = BTA_HF_CLIENT_DISC_INT_RES_EVT;
     63 
     64     p_buf->hdr.event = event;
     65     p_buf->status = status;
     66 
     67     bta_sys_sendmsg(p_buf);
     68 }
     69 
     70 /******************************************************************************
     71 **
     72 ** Function         bta_hf_client_add_record
     73 **
     74 ** Description      This function is called by a server application to add
     75 **                  HFP Client information to an SDP record.  Prior to
     76 **                  calling this function the application must call
     77 **                  SDP_CreateRecord() to create an SDP record.
     78 **
     79 ** Returns          TRUE if function execution succeeded,
     80 **                  FALSE if function execution failed.
     81 **
     82 ******************************************************************************/
     83 BOOLEAN bta_hf_client_add_record(char *p_service_name, UINT8 scn,
     84                           tBTA_HF_CLIENT_FEAT features, UINT32 sdp_handle)
     85 {
     86     tSDP_PROTOCOL_ELEM  proto_elem_list[BTA_HF_CLIENT_NUM_PROTO_ELEMS];
     87     UINT16              svc_class_id_list[BTA_HF_CLIENT_NUM_SVC_ELEMS];
     88     UINT16              browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
     89     UINT16              version;
     90     UINT16              profile_uuid;
     91     BOOLEAN             result = TRUE;
     92     UINT8               buf[2];
     93     UINT16              sdp_features = 0;
     94 
     95     APPL_TRACE_DEBUG("bta_hf_client_add_record");
     96 
     97     memset( proto_elem_list, 0 , BTA_HF_CLIENT_NUM_PROTO_ELEMS*sizeof(tSDP_PROTOCOL_ELEM));
     98 
     99     /* add the protocol element sequence */
    100     proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
    101     proto_elem_list[0].num_params = 0;
    102     proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
    103     proto_elem_list[1].num_params = 1;
    104     proto_elem_list[1].params[0] = scn;
    105     result &= SDP_AddProtocolList(sdp_handle, BTA_HF_CLIENT_NUM_PROTO_ELEMS, proto_elem_list);
    106 
    107     /* add service class id list */
    108     svc_class_id_list[0] = UUID_SERVCLASS_HF_HANDSFREE;
    109     svc_class_id_list[1] = UUID_SERVCLASS_GENERIC_AUDIO;
    110     result &= SDP_AddServiceClassIdList(sdp_handle, BTA_HF_CLIENT_NUM_SVC_ELEMS, svc_class_id_list);
    111 
    112     /* add profile descriptor list */
    113     profile_uuid = UUID_SERVCLASS_HF_HANDSFREE;
    114     version = HFP_VERSION_1_6;
    115 
    116     result &= SDP_AddProfileDescriptorList(sdp_handle, profile_uuid, version);
    117 
    118     /* add service name */
    119     if (p_service_name != NULL && p_service_name[0] != 0)
    120     {
    121         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
    122                     (UINT32)(strlen(p_service_name)+1), (UINT8 *) p_service_name);
    123     }
    124 
    125     /* add features */
    126     if (features & BTA_HF_CLIENT_FEAT_ECNR)
    127        sdp_features |= BTA_HF_CLIENT_FEAT_ECNR;
    128 
    129     if (features & BTA_HF_CLIENT_FEAT_3WAY)
    130        sdp_features |= BTA_HF_CLIENT_FEAT_3WAY;
    131 
    132     if (features & BTA_HF_CLIENT_FEAT_CLI)
    133        sdp_features |= BTA_HF_CLIENT_FEAT_CLI;
    134 
    135     if (features & BTA_HF_CLIENT_FEAT_VREC)
    136        sdp_features |= BTA_HF_CLIENT_FEAT_VREC;
    137 
    138     if (features & BTA_HF_CLIENT_FEAT_VOL)
    139        sdp_features |= BTA_HF_CLIENT_FEAT_VOL;
    140 
    141     /* Codec bit position is different in SDP (bit 5) and in BRSF (bit 7) */
    142     if (features & BTA_HF_CLIENT_FEAT_CODEC)
    143        sdp_features |= 0x0020;
    144 
    145     UINT16_TO_BE_FIELD(buf, sdp_features);
    146     result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, 2, buf);
    147 
    148     /* add browse group list */
    149     result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
    150 
    151     return result;
    152 }
    153 
    154 /*******************************************************************************
    155 **
    156 ** Function         bta_hf_client_create_record
    157 **
    158 ** Description      Create SDP record for registered service.
    159 **
    160 **
    161 ** Returns          void
    162 **
    163 *******************************************************************************/
    164 void bta_hf_client_create_record(tBTA_HF_CLIENT_DATA *p_data)
    165 {
    166     /* add sdp record if not already registered */
    167     if (bta_hf_client_cb.sdp_handle == 0)
    168     {
    169         bta_hf_client_cb.sdp_handle = SDP_CreateRecord();
    170         bta_hf_client_cb.scn = BTM_AllocateSCN();
    171         bta_hf_client_add_record(p_data->api_register.name,
    172                                  bta_hf_client_cb.scn,
    173                                  p_data->api_register.features,
    174                                  bta_hf_client_cb.sdp_handle);
    175 
    176         bta_sys_add_uuid(UUID_SERVCLASS_HF_HANDSFREE);
    177     }
    178 
    179 }
    180 
    181 /*******************************************************************************
    182 **
    183 ** Function         bta_hf_client_del_record
    184 **
    185 ** Description      Delete SDP record for registered service.
    186 **
    187 **
    188 ** Returns          void
    189 **
    190 *******************************************************************************/
    191 void bta_hf_client_del_record(tBTA_HF_CLIENT_DATA *p_data)
    192 {
    193     UNUSED(p_data);
    194 
    195     APPL_TRACE_DEBUG("bta_hf_client_del_record");
    196 
    197     if (bta_hf_client_cb.sdp_handle != 0)
    198     {
    199         SDP_DeleteRecord(bta_hf_client_cb.sdp_handle);
    200         bta_hf_client_cb.sdp_handle = 0;
    201         BTM_FreeSCN(bta_hf_client_cb.scn);
    202         BTM_SecClrService(BTM_SEC_SERVICE_HF_HANDSFREE);
    203         bta_sys_remove_uuid(UUID_SERVCLASS_HF_HANDSFREE);
    204     }
    205 }
    206 
    207 /*******************************************************************************
    208 **
    209 ** Function         bta_hf_client_sdp_find_attr
    210 **
    211 ** Description      Process SDP discovery results to find requested attribute
    212 **
    213 **
    214 ** Returns          TRUE if results found, FALSE otherwise.
    215 **
    216 *******************************************************************************/
    217 BOOLEAN bta_hf_client_sdp_find_attr(void)
    218 {
    219     tSDP_DISC_REC       *p_rec = NULL;
    220     tSDP_DISC_ATTR      *p_attr;
    221     tSDP_PROTOCOL_ELEM  pe;
    222     BOOLEAN             result = FALSE;
    223 
    224     bta_hf_client_cb.scb.peer_version = HFP_VERSION_1_1;   /* Default version */
    225 
    226     /* loop through all records we found */
    227     while (TRUE)
    228     {
    229         /* get next record; if none found, we're done */
    230         if ((p_rec = SDP_FindServiceInDb(bta_hf_client_cb.scb.p_disc_db, UUID_SERVCLASS_AG_HANDSFREE, p_rec)) == NULL)
    231         {
    232             break;
    233         }
    234 
    235         /* get scn from proto desc list if initiator */
    236         if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_INT)
    237         {
    238             if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe))
    239             {
    240                 bta_hf_client_cb.scb.peer_scn = (UINT8) pe.params[0];
    241             }
    242             else
    243             {
    244                 continue;
    245             }
    246         }
    247 
    248         /* get profile version (if failure, version parameter is not updated) */
    249         SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_HF_HANDSFREE, &bta_hf_client_cb.scb.peer_version);
    250 
    251         /* get features */
    252         if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL)
    253         {
    254             /* Found attribute. Get value. */
    255             /* There might be race condition between SDP and BRSF.  */
    256             /* Do not update if we already received BRSF.           */
    257             if (bta_hf_client_cb.scb.peer_features == 0)
    258             {
    259                 bta_hf_client_cb.scb.peer_features = p_attr->attr_value.v.u16;
    260 
    261                 /* SDP and BRSF WBS bit are different, correct it if set */
    262                 if (bta_hf_client_cb.scb.peer_features & 0x0020)
    263                 {
    264                     bta_hf_client_cb.scb.peer_features &= ~0x0020;
    265                     bta_hf_client_cb.scb.peer_features |= BTA_HF_CLIENT_PEER_CODEC;
    266                 }
    267 
    268                 /* get network for ability to reject calls */
    269                 if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_NETWORK)) != NULL)
    270                 {
    271                     if (p_attr->attr_value.v.u16 == 0x01)
    272                     {
    273                         bta_hf_client_cb.scb.peer_features |= BTA_HF_CLIENT_PEER_REJECT;
    274                     }
    275                 }
    276             }
    277         }
    278 
    279         /* found what we needed */
    280         result = TRUE;
    281         break;
    282     }
    283 
    284     APPL_TRACE_DEBUG("%s peer_version=0x%x peer_features=0x%x",
    285                       __FUNCTION__, bta_hf_client_cb.scb.peer_version,
    286                       bta_hf_client_cb.scb.peer_features);
    287 
    288     return result;
    289 }
    290 
    291 /*******************************************************************************
    292 **
    293 ** Function         bta_hf_client_do_disc
    294 **
    295 ** Description      Do service discovery.
    296 **
    297 **
    298 ** Returns          void
    299 **
    300 *******************************************************************************/
    301 void bta_hf_client_do_disc(void)
    302 {
    303     tSDP_UUID       uuid_list[2];
    304     UINT16          num_uuid = 1;
    305     UINT16          attr_list[4];
    306     UINT8           num_attr;
    307     BOOLEAN         db_inited = FALSE;
    308 
    309     /* initiator; get proto list and features */
    310     if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_INT)
    311     {
    312         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
    313         attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
    314         attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
    315         attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
    316         num_attr = 4;
    317         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
    318     }
    319     /* acceptor; get features */
    320     else
    321     {
    322         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
    323         attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
    324         attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
    325         num_attr = 3;
    326         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
    327     }
    328 
    329     /* allocate buffer for sdp database */
    330     bta_hf_client_cb.scb.p_disc_db = (tSDP_DISCOVERY_DB *)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
    331 
    332     /* set up service discovery database; attr happens to be attr_list len */
    333     uuid_list[0].len = LEN_UUID_16;
    334     uuid_list[1].len = LEN_UUID_16;
    335     db_inited = SDP_InitDiscoveryDb(bta_hf_client_cb.scb.p_disc_db,
    336                                     BT_DEFAULT_BUFFER_SIZE, num_uuid,
    337                                     uuid_list, num_attr, attr_list);
    338 
    339     if (db_inited)
    340     {
    341         /*Service discovery not initiated */
    342         db_inited = SDP_ServiceSearchAttributeRequest(bta_hf_client_cb.scb.peer_addr,
    343                             bta_hf_client_cb.scb.p_disc_db, bta_hf_client_sdp_cback);
    344     }
    345 
    346     if (!db_inited)
    347     {
    348         /*free discover db */
    349         bta_hf_client_free_db(NULL);
    350         /* sent failed event */
    351         bta_hf_client_sm_execute(BTA_HF_CLIENT_DISC_FAIL_EVT, NULL);
    352     }
    353 
    354 }
    355 
    356 /*******************************************************************************
    357 **
    358 ** Function         bta_hf_client_free_db
    359 **
    360 ** Description      Free discovery database.
    361 **
    362 **
    363 ** Returns          void
    364 **
    365 *******************************************************************************/
    366 void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA *p_data)
    367 {
    368     UNUSED(p_data);
    369     osi_free_and_reset((void **)&bta_hf_client_cb.scb.p_disc_db);
    370 }
    371