12dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta/******************************************************************************
22dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *
32dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  Copyright (C) 2014 The Android Open Source Project
42dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  Copyright (C) 2003-2012 Broadcom Corporation
52dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *
62dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  Licensed under the Apache License, Version 2.0 (the "License");
72dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  you may not use this file except in compliance with the License.
82dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  You may obtain a copy of the License at:
92dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *
102dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  http://www.apache.org/licenses/LICENSE-2.0
112dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *
122dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  Unless required by applicable law or agreed to in writing, software
132dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  distributed under the License is distributed on an "AS IS" BASIS,
142dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
152dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  See the License for the specific language governing permissions and
162dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  limitations under the License.
172dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *
182dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta ******************************************************************************/
192dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
202dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta/******************************************************************************
212dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *
222dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *  This file contains action functions for MCE.
232dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta *
242dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta ******************************************************************************/
252dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
262dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include <hardware/bluetooth.h>
272dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include <arpa/inet.h>
282dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
292dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "bt_types.h"
302dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "gki.h"
312dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "utl.h"
322dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "bta_sys.h"
332dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "bta_api.h"
342dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "bta_mce_api.h"
352dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "bta_mce_int.h"
362dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "btm_api.h"
372dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "btm_int.h"
382dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include "sdp_api.h"
392dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta#include <string.h>
402dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
412dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta/*****************************************************************************
422dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**  Constants
432dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta*****************************************************************************/
442dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
452dc999935d4c69ae31ea93d9014165965dd373d6Hemant Guptastatic const tBT_UUID bta_mce_mas_uuid = {
462dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    .len = 2,
472dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    .uu.uuid16 = UUID_SERVCLASS_MESSAGE_ACCESS
482dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta};
492dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
502dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta/*******************************************************************************
512dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
522dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Function     bta_mce_search_cback
532dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
542dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Description  Callback from btm after search is completed
552dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
562dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Returns      void
572dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
582dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta*******************************************************************************/
592dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
602dc999935d4c69ae31ea93d9014165965dd373d6Hemant Guptastatic void bta_mce_search_cback(UINT16 result, void * user_data)
612dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta{
622dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    tSDP_DISC_REC *p_rec = NULL;
632dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    tBTA_MCE_MAS_DISCOVERY_COMP evt_data;
642dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    int found = 0;
652dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
662dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    APPL_TRACE_DEBUG("bta_mce_start_discovery_cback res: 0x%x", result);
672dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
682dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_NONE;
692dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
702dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    if (bta_mce_cb.p_dm_cback == NULL) return;
712dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
722dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    evt_data.status = BTA_MCE_FAILURE;
732dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    bdcpy(evt_data.remote_addr, bta_mce_cb.remote_addr);
742dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    evt_data.num_mas = 0;
752dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
762dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    if (result == SDP_SUCCESS || result == SDP_DB_FULL)
772dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    {
782dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        do
792dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        {
802dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            tSDP_DISC_ATTR *p_attr;
812dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            tSDP_PROTOCOL_ELEM pe;
822dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
832dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            p_rec = SDP_FindServiceUUIDInDb(p_bta_mce_cfg->p_sdp_db, (tBT_UUID*) &bta_mce_mas_uuid, p_rec);
842dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
852dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            APPL_TRACE_DEBUG("p_rec:%p", p_rec);
862dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
872dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            if (p_rec == NULL)
882dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                break;
892dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
902dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            if (!SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe))
912dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                continue;
922dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
932dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            evt_data.mas[found].scn = pe.params[0];
942dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
952dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_NAME)) == NULL)
962dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                continue;
972dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
982dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            evt_data.mas[found].p_srv_name = (char *) p_attr->attr_value.v.array;
992dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            evt_data.mas[found].srv_name_len= SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
1002dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1012dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_MAS_INSTANCE_ID)) == NULL)
1022dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                break;
1032dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1042dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                 evt_data.mas[found].instance_id = p_attr->attr_value.v.u8;
1052dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1062dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_MSG_TYPE)) == NULL)
1072dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                break;
1082dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1092dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            evt_data.mas[found].msg_type = p_attr->attr_value.v.u8;
1102dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1112dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            found++;
1122dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        } while (p_rec != NULL && found < BTA_MCE_MAX_MAS_INSTANCES);
1132dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1142dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        evt_data.num_mas = found;
1152dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        evt_data.status = BTA_MCE_SUCCESS;
1162dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    }
1172dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1182dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, (tBTA_MCE*) &evt_data, user_data);
1192dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta}
1202dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1212dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta/*******************************************************************************
1222dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1232dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Function     bta_mce_enable
1242dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1252dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Description  Initializes the MCE I/F
1262dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1272dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Returns      void
1282dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1292dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta*******************************************************************************/
1302dc999935d4c69ae31ea93d9014165965dd373d6Hemant Guptavoid bta_mce_enable(tBTA_MCE_MSG *p_data)
1312dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta{
1322dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    tBTA_MCE_STATUS status = BTA_MCE_SUCCESS;
1332dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    bta_mce_cb.p_dm_cback = p_data->enable.p_cback;
1342dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    bta_mce_cb.p_dm_cback(BTA_MCE_ENABLE_EVT, (tBTA_MCE *)&status, NULL);
1352dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta}
1362dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1372dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta/*******************************************************************************
1382dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1392dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Function     bta_mce_get_remote_mas_instances
1402dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1412dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Description  Discovers MAS instances on remote device
1422dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1432dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta** Returns      void
1442dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta**
1452dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta*******************************************************************************/
1462dc999935d4c69ae31ea93d9014165965dd373d6Hemant Guptavoid bta_mce_get_remote_mas_instances(tBTA_MCE_MSG *p_data)
1472dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta{
1482dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    if(p_data == NULL)
1492dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    {
1502dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    APPL_TRACE_DEBUG("MCE control block handle is null");
1512dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    return;
1522dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    }
1532dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    tBTA_MCE_STATUS status = BTA_MCE_FAILURE;
1542dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1552dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    APPL_TRACE_DEBUG("%s in, sdp_active:%d", __FUNCTION__, bta_mce_cb.sdp_active);
1562dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1572dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    if (bta_mce_cb.sdp_active != BTA_MCE_SDP_ACT_NONE)
1582dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    {
1592dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        /* SDP is still in progress */
1602dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        status = BTA_MCE_BUSY;
1612dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        if(bta_mce_cb.p_dm_cback)
1622dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, (tBTA_MCE *)&status, NULL);
1632dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1642dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        return;
1652dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    }
1662dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1672dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_YES;
1682dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    bdcpy(bta_mce_cb.remote_addr, p_data->get_rmt_mas.bd_addr);
1692dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1702dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    SDP_InitDiscoveryDb (p_bta_mce_cfg->p_sdp_db, p_bta_mce_cfg->sdp_db_size, 1,
1712dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                                (tBT_UUID*) &bta_mce_mas_uuid, 0, NULL);
1722dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1732dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    if (!SDP_ServiceSearchAttributeRequest2(p_data->get_rmt_mas.bd_addr, p_bta_mce_cfg->p_sdp_db,
1742dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta                                                bta_mce_search_cback, NULL))
1752dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    {
1762dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_NONE;
1772dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta
1782dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        /* failed to start SDP. report the failure right away */
1792dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta        if (bta_mce_cb.p_dm_cback)
1802dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta            bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, (tBTA_MCE *)&status, NULL);
1812dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    }
1822dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    /*
1832dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    else report the result when the cback is called
1842dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta    */
1852dc999935d4c69ae31ea93d9014165965dd373d6Hemant Gupta}
186