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 "gki.h"
     34 
     35 /* Number of protocol elements in protocol element list. */
     36 #define BTA_AG_NUM_PROTO_ELEMS      2
     37 
     38 /* Number of elements in service class id list. */
     39 #define BTA_AG_NUM_SVC_ELEMS        2
     40 
     41 /* size of database for service discovery */
     42 #ifndef BTA_AG_DISC_BUF_SIZE
     43 #define BTA_AG_DISC_BUF_SIZE        GKI_MAX_BUF_SIZE
     44 #endif
     45 
     46 /* declare sdp callback functions */
     47 void bta_ag_sdp_cback_1(UINT16 status);
     48 void bta_ag_sdp_cback_2(UINT16 status);
     49 void bta_ag_sdp_cback_3(UINT16 status);
     50 
     51 /* SDP callback function table */
     52 typedef tSDP_DISC_CMPL_CB *tBTA_AG_SDP_CBACK;
     53 const tBTA_AG_SDP_CBACK bta_ag_sdp_cback_tbl[] =
     54 {
     55     bta_ag_sdp_cback_1,
     56     bta_ag_sdp_cback_2,
     57     bta_ag_sdp_cback_3
     58 };
     59 
     60 /*******************************************************************************
     61 **
     62 ** Function         bta_ag_sdp_cback
     63 **
     64 ** Description      SDP callback function.
     65 **
     66 **
     67 ** Returns          void
     68 **
     69 *******************************************************************************/
     70 static void bta_ag_sdp_cback(UINT16 status, UINT8 idx)
     71 {
     72     tBTA_AG_DISC_RESULT *p_buf;
     73     UINT16              event;
     74     tBTA_AG_SCB         *p_scb;
     75 
     76     APPL_TRACE_DEBUG1("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         if ((p_buf = (tBTA_AG_DISC_RESULT *) GKI_getbuf(sizeof(tBTA_AG_DISC_RESULT))) != NULL)
     91         {
     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 **
    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 *******************************************************************************/
    112 void bta_ag_sdp_cback_1(UINT16 status) {bta_ag_sdp_cback(status, 1);}
    113 void bta_ag_sdp_cback_2(UINT16 status) {bta_ag_sdp_cback(status, 2);}
    114 void bta_ag_sdp_cback_3(UINT16 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 ******************************************************************************/
    129 BOOLEAN bta_ag_add_record(UINT16 service_uuid, char *p_service_name, UINT8 scn,
    130                           tBTA_AG_FEAT features, UINT32 sdp_handle)
    131 {
    132     tSDP_PROTOCOL_ELEM  proto_elem_list[BTA_AG_NUM_PROTO_ELEMS];
    133     UINT16              svc_class_id_list[BTA_AG_NUM_SVC_ELEMS];
    134     UINT16              browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
    135     UINT16              version;
    136     UINT16              profile_uuid;
    137     UINT8               network;
    138     BOOLEAN             result = TRUE;
    139     BOOLEAN             codec_supported = FALSE;
    140     UINT8               buf[2];
    141 
    142     APPL_TRACE_DEBUG1("bta_ag_add_record uuid: %x", 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 = HFP_VERSION_1_6;
    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)(strlen(p_service_name)+1), (UINT8 *) 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 *******************************************************************************/
    215 void 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 *******************************************************************************/
    253 void bta_ag_del_records(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
    254 {
    255     tBTA_AG_SCB         *p = &bta_ag_cb.scb[0];
    256     tBTA_SERVICE_MASK   services;
    257     tBTA_SERVICE_MASK   others = 0;
    258     int                 i;
    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_DEBUG1("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 *) GKI_getbuf(BTA_AG_DISC_BUF_SIZE);
    456 
    457     if(p_scb->p_disc_db)
    458     {
    459         /* set up service discovery database; attr happens to be attr_list len */
    460         uuid_list[0].len = LEN_UUID_16;
    461         uuid_list[1].len = LEN_UUID_16;
    462         db_inited = SDP_InitDiscoveryDb(p_scb->p_disc_db, BTA_AG_DISC_BUF_SIZE, num_uuid,
    463                             uuid_list, num_attr, attr_list);
    464     }
    465 
    466     if(db_inited)
    467     {
    468         /*Service discovery not initiated */
    469         db_inited = SDP_ServiceSearchAttributeRequest(p_scb->peer_addr, p_scb->p_disc_db,
    470                                       bta_ag_sdp_cback_tbl[bta_ag_scb_to_idx(p_scb) - 1]);
    471     }
    472 
    473     if(!db_inited)
    474     {
    475         /*free discover db */
    476         bta_ag_free_db(p_scb, NULL);
    477         /* sent failed event */
    478         bta_ag_sm_execute(p_scb, BTA_AG_DISC_FAIL_EVT, NULL);
    479     }
    480 
    481 }
    482 
    483 /*******************************************************************************
    484 **
    485 ** Function         bta_ag_free_db
    486 **
    487 ** Description      Free discovery database.
    488 **
    489 **
    490 ** Returns          void
    491 **
    492 *******************************************************************************/
    493 void bta_ag_free_db(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
    494 {
    495     if (p_scb->p_disc_db != NULL)
    496     {
    497         GKI_freebuf(p_scb->p_disc_db);
    498         p_scb->p_disc_db = NULL;
    499     }
    500 }
    501