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     tBTA_HF_CLIENT_DISC_RESULT *p_buf;
     53     UINT16                     event;
     54 
     55     APPL_TRACE_DEBUG("bta_hf_client_sdp_cback status:0x%x", status);
     56 
     57     /* set event according to int/acp */
     58     if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_ACP)
     59     {
     60         event = BTA_HF_CLIENT_DISC_ACP_RES_EVT;
     61     }
     62     else
     63     {
     64         event = BTA_HF_CLIENT_DISC_INT_RES_EVT;
     65     }
     66 
     67     if ((p_buf = (tBTA_HF_CLIENT_DISC_RESULT *) GKI_getbuf(sizeof(tBTA_HF_CLIENT_DISC_RESULT))) != NULL)
     68     {
     69         p_buf->hdr.event = event;
     70         p_buf->status = status;
     71         bta_sys_sendmsg(p_buf);
     72     }
     73 }
     74 
     75 /******************************************************************************
     76 **
     77 ** Function         bta_hf_client_add_record
     78 **
     79 ** Description      This function is called by a server application to add
     80 **                  HFP Client information to an SDP record.  Prior to
     81 **                  calling this function the application must call
     82 **                  SDP_CreateRecord() to create an SDP record.
     83 **
     84 ** Returns          TRUE if function execution succeeded,
     85 **                  FALSE if function execution failed.
     86 **
     87 ******************************************************************************/
     88 BOOLEAN bta_hf_client_add_record(char *p_service_name, UINT8 scn,
     89                           tBTA_HF_CLIENT_FEAT features, UINT32 sdp_handle)
     90 {
     91     tSDP_PROTOCOL_ELEM  proto_elem_list[BTA_HF_CLIENT_NUM_PROTO_ELEMS];
     92     UINT16              svc_class_id_list[BTA_HF_CLIENT_NUM_SVC_ELEMS];
     93     UINT16              browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
     94     UINT16              version;
     95     UINT16              profile_uuid;
     96     BOOLEAN             result = TRUE;
     97     BOOLEAN             codec_supported = FALSE;
     98     UINT8               buf[2];
     99     UINT16              sdp_features = 0;
    100 
    101     APPL_TRACE_DEBUG("bta_hf_client_add_record");
    102 
    103     memset( proto_elem_list, 0 , BTA_HF_CLIENT_NUM_PROTO_ELEMS*sizeof(tSDP_PROTOCOL_ELEM));
    104 
    105     /* add the protocol element sequence */
    106     proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
    107     proto_elem_list[0].num_params = 0;
    108     proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
    109     proto_elem_list[1].num_params = 1;
    110     proto_elem_list[1].params[0] = scn;
    111     result &= SDP_AddProtocolList(sdp_handle, BTA_HF_CLIENT_NUM_PROTO_ELEMS, proto_elem_list);
    112 
    113     /* add service class id list */
    114     svc_class_id_list[0] = UUID_SERVCLASS_HF_HANDSFREE;
    115     svc_class_id_list[1] = UUID_SERVCLASS_GENERIC_AUDIO;
    116     result &= SDP_AddServiceClassIdList(sdp_handle, BTA_HF_CLIENT_NUM_SVC_ELEMS, svc_class_id_list);
    117 
    118     /* add profile descriptor list */
    119     profile_uuid = UUID_SERVCLASS_HF_HANDSFREE;
    120     version = HFP_VERSION_1_6;
    121 
    122     result &= SDP_AddProfileDescriptorList(sdp_handle, profile_uuid, version);
    123 
    124     /* add service name */
    125     if (p_service_name != NULL && p_service_name[0] != 0)
    126     {
    127         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
    128                     (UINT32)(strlen(p_service_name)+1), (UINT8 *) p_service_name);
    129     }
    130 
    131     /* add features */
    132     if (features & BTA_HF_CLIENT_FEAT_ECNR)
    133        sdp_features |= BTA_HF_CLIENT_FEAT_ECNR;
    134 
    135     if (features & BTA_HF_CLIENT_FEAT_3WAY)
    136        sdp_features |= BTA_HF_CLIENT_FEAT_3WAY;
    137 
    138     if (features & BTA_HF_CLIENT_FEAT_CLI)
    139        sdp_features |= BTA_HF_CLIENT_FEAT_CLI;
    140 
    141     if (features & BTA_HF_CLIENT_FEAT_VREC)
    142        sdp_features |= BTA_HF_CLIENT_FEAT_VREC;
    143 
    144     if (features & BTA_HF_CLIENT_FEAT_VOL)
    145        sdp_features |= BTA_HF_CLIENT_FEAT_VOL;
    146 
    147     /* Codec bit position is different in SDP (bit 5) and in BRSF (bit 7) */
    148     if (features & BTA_HF_CLIENT_FEAT_CODEC)
    149        sdp_features |= 0x0020;
    150 
    151     UINT16_TO_BE_FIELD(buf, sdp_features);
    152     result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, 2, buf);
    153 
    154     /* add browse group list */
    155     result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
    156 
    157     return result;
    158 }
    159 
    160 /*******************************************************************************
    161 **
    162 ** Function         bta_hf_client_create_record
    163 **
    164 ** Description      Create SDP record for registered service.
    165 **
    166 **
    167 ** Returns          void
    168 **
    169 *******************************************************************************/
    170 void bta_hf_client_create_record(tBTA_HF_CLIENT_DATA *p_data)
    171 {
    172     /* add sdp record if not already registered */
    173     if (bta_hf_client_cb.sdp_handle == 0)
    174     {
    175         bta_hf_client_cb.sdp_handle = SDP_CreateRecord();
    176         bta_hf_client_cb.scn = BTM_AllocateSCN();
    177         bta_hf_client_add_record(p_data->api_register.name,
    178                                  bta_hf_client_cb.scn,
    179                                  p_data->api_register.features,
    180                                  bta_hf_client_cb.sdp_handle);
    181 
    182         bta_sys_add_uuid(UUID_SERVCLASS_HF_HANDSFREE);
    183     }
    184 
    185 }
    186 
    187 /*******************************************************************************
    188 **
    189 ** Function         bta_hf_client_del_record
    190 **
    191 ** Description      Delete SDP record for registered service.
    192 **
    193 **
    194 ** Returns          void
    195 **
    196 *******************************************************************************/
    197 void bta_hf_client_del_record(tBTA_HF_CLIENT_DATA *p_data)
    198 {
    199     UNUSED(p_data);
    200 
    201     APPL_TRACE_DEBUG("bta_hf_client_del_record");
    202 
    203     if (bta_hf_client_cb.sdp_handle != 0)
    204     {
    205         SDP_DeleteRecord(bta_hf_client_cb.sdp_handle);
    206         bta_hf_client_cb.sdp_handle = 0;
    207         BTM_FreeSCN(bta_hf_client_cb.scn);
    208         BTM_SecClrService(BTM_SEC_SERVICE_HF_HANDSFREE);
    209         bta_sys_remove_uuid(UUID_SERVCLASS_HF_HANDSFREE);
    210     }
    211 }
    212 
    213 /*******************************************************************************
    214 **
    215 ** Function         bta_hf_client_sdp_find_attr
    216 **
    217 ** Description      Process SDP discovery results to find requested attribute
    218 **
    219 **
    220 ** Returns          TRUE if results found, FALSE otherwise.
    221 **
    222 *******************************************************************************/
    223 BOOLEAN bta_hf_client_sdp_find_attr(void)
    224 {
    225     tSDP_DISC_REC       *p_rec = NULL;
    226     tSDP_DISC_ATTR      *p_attr;
    227     tSDP_PROTOCOL_ELEM  pe;
    228     BOOLEAN             result = FALSE;
    229 
    230     bta_hf_client_cb.scb.peer_version = HFP_VERSION_1_1;   /* Default version */
    231 
    232     /* loop through all records we found */
    233     while (TRUE)
    234     {
    235         /* get next record; if none found, we're done */
    236         if ((p_rec = SDP_FindServiceInDb(bta_hf_client_cb.scb.p_disc_db, UUID_SERVCLASS_AG_HANDSFREE, p_rec)) == NULL)
    237         {
    238             break;
    239         }
    240 
    241         /* get scn from proto desc list if initiator */
    242         if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_INT)
    243         {
    244             if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe))
    245             {
    246                 bta_hf_client_cb.scb.peer_scn = (UINT8) pe.params[0];
    247             }
    248             else
    249             {
    250                 continue;
    251             }
    252         }
    253 
    254         /* get profile version (if failure, version parameter is not updated) */
    255         SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_HF_HANDSFREE, &bta_hf_client_cb.scb.peer_version);
    256 
    257         /* get features */
    258         if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL)
    259         {
    260             /* Found attribute. Get value. */
    261             /* There might be race condition between SDP and BRSF.  */
    262             /* Do not update if we already received BRSF.           */
    263             if (bta_hf_client_cb.scb.peer_features == 0)
    264             {
    265                 bta_hf_client_cb.scb.peer_features = p_attr->attr_value.v.u16;
    266 
    267                 /* SDP and BRSF WBS bit are different, correct it if set */
    268                 if (bta_hf_client_cb.scb.peer_features & 0x0020)
    269                 {
    270                     bta_hf_client_cb.scb.peer_features &= ~0x0020;
    271                     bta_hf_client_cb.scb.peer_features |= BTA_HF_CLIENT_PEER_CODEC;
    272                 }
    273 
    274                 /* get network for ability to reject calls */
    275                 if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_NETWORK)) != NULL)
    276                 {
    277                     if (p_attr->attr_value.v.u16 == 0x01)
    278                     {
    279                         bta_hf_client_cb.scb.peer_features |= BTA_HF_CLIENT_PEER_REJECT;
    280                     }
    281                 }
    282             }
    283         }
    284 
    285         /* found what we needed */
    286         result = TRUE;
    287         break;
    288     }
    289 
    290     APPL_TRACE_DEBUG("%s peer_version=0x%x peer_features=0x%x",
    291                       __FUNCTION__, bta_hf_client_cb.scb.peer_version,
    292                       bta_hf_client_cb.scb.peer_features);
    293 
    294     return result;
    295 }
    296 
    297 /*******************************************************************************
    298 **
    299 ** Function         bta_hf_client_do_disc
    300 **
    301 ** Description      Do service discovery.
    302 **
    303 **
    304 ** Returns          void
    305 **
    306 *******************************************************************************/
    307 void bta_hf_client_do_disc(void)
    308 {
    309     tSDP_UUID       uuid_list[2];
    310     UINT16          num_uuid = 1;
    311     UINT16          attr_list[4];
    312     UINT8           num_attr;
    313     BOOLEAN         db_inited = FALSE;
    314 
    315     /* initiator; get proto list and features */
    316     if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_INT)
    317     {
    318         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
    319         attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
    320         attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
    321         attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
    322         num_attr = 4;
    323         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
    324     }
    325     /* acceptor; get features */
    326     else
    327     {
    328         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
    329         attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
    330         attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
    331         num_attr = 3;
    332         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
    333     }
    334 
    335     /* allocate buffer for sdp database */
    336     bta_hf_client_cb.scb.p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(GKI_MAX_BUF_SIZE);
    337 
    338     if (bta_hf_client_cb.scb.p_disc_db)
    339     {
    340         /* set up service discovery database; attr happens to be attr_list len */
    341         uuid_list[0].len = LEN_UUID_16;
    342         uuid_list[1].len = LEN_UUID_16;
    343         db_inited = SDP_InitDiscoveryDb(bta_hf_client_cb.scb.p_disc_db, GKI_MAX_BUF_SIZE, num_uuid,
    344                             uuid_list, num_attr, attr_list);
    345     }
    346 
    347     if (db_inited)
    348     {
    349         /*Service discovery not initiated */
    350         db_inited = SDP_ServiceSearchAttributeRequest(bta_hf_client_cb.scb.peer_addr,
    351                             bta_hf_client_cb.scb.p_disc_db, bta_hf_client_sdp_cback);
    352     }
    353 
    354     if (!db_inited)
    355     {
    356         /*free discover db */
    357         bta_hf_client_free_db(NULL);
    358         /* sent failed event */
    359         bta_hf_client_sm_execute(BTA_HF_CLIENT_DISC_FAIL_EVT, NULL);
    360     }
    361 
    362 }
    363 
    364 /*******************************************************************************
    365 **
    366 ** Function         bta_hf_client_free_db
    367 **
    368 ** Description      Free discovery database.
    369 **
    370 **
    371 ** Returns          void
    372 **
    373 *******************************************************************************/
    374 void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA *p_data)
    375 {
    376     UNUSED(p_data);
    377 
    378     if (bta_hf_client_cb.scb.p_disc_db != NULL)
    379     {
    380         GKI_freebuf(bta_hf_client_cb.scb.p_disc_db);
    381         bta_hf_client_cb.scb.p_disc_db = NULL;
    382     }
    383 }
    384