1ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji/******************************************************************************
2ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
3ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  Copyright (C) 2014  Broadcom Corporation
4ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
5ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  Licensed under the Apache License, Version 2.0 (the "License");
6ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  you may not use this file except in compliance with the License.
7ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  You may obtain a copy of the License at:
8ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
9ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  http://www.apache.org/licenses/LICENSE-2.0
10ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
11ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  Unless required by applicable law or agreed to in writing, software
12ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  distributed under the License is distributed on an "AS IS" BASIS,
13ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  See the License for the specific language governing permissions and
15ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  limitations under the License.
16ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
17ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji ******************************************************************************/
18ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
19ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
20ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji/*******************************************************************************
21ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
22ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  Filename:      btif_gatt_multi_adv_util.c
23ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
24ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *  Description:   Multi ADV helper implementation
25ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *
26ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji *******************************************************************************/
27ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
28ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#include <stdio.h>
29ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#include <stdlib.h>
30c7503db23f91676b2df732becb9579d5ccdcc7a7Ian Coolidge#include <string.h>
31cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach#include "btu.h"
32bf0e4b26d8f0748ff4f9522a2ac7001bfb9ca5ccWei Wang#include "bt_target.h"
33ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
34f8027005333c88a2f097cfd70d15c3d54c7764aeChris Manton#define LOG_TAG "bt_btif_gatt"
35ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#if (BLE_INCLUDED == TRUE)
36ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
37ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#include "btif_gatt_multi_adv_util.h"
38ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#include "btif_common.h"
39ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#include <hardware/bt_gatt.h>
40ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#include "bta_gatt_api.h"
41ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#include "btif_gatt_util.h"
42ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
43ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji/*******************************************************************************
44ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji**  Static variables
45ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji********************************************************************************/
46b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Callojistatic int user_app_count = 0;
47ffb3960bb05f9acb0717d213612403487e9ad433Satya Callojistatic btgatt_multi_adv_common_data *p_multi_adv_com_data_cb = NULL;
48ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
49ffb3960bb05f9acb0717d213612403487e9ad433Satya Callojibtgatt_multi_adv_common_data *btif_obtain_multi_adv_data_cb()
50ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
51cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji    int max_adv_inst = BTM_BleMaxMultiAdvInstanceCount();
52cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji    if (0 == max_adv_inst)
53cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji        max_adv_inst = 1;
5416fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
55cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji    BTIF_TRACE_DEBUG("%s, Count:%d", __FUNCTION__, max_adv_inst);
56b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji
5716fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (NULL == p_multi_adv_com_data_cb)
5816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    {
5916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham        p_multi_adv_com_data_cb = GKI_getbuf(sizeof(btgatt_multi_adv_common_data));
6016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham        if (NULL != p_multi_adv_com_data_cb)
6116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham        {
629dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            memset(p_multi_adv_com_data_cb, 0, sizeof(btgatt_multi_adv_common_data));
6316fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
649dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            /* Storing both client_if and inst_id details */
659dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            p_multi_adv_com_data_cb->clntif_map =
66cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji                  GKI_getbuf(( max_adv_inst * INST_ID_IDX_MAX)* sizeof(INT8));
679dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            memset(p_multi_adv_com_data_cb->clntif_map, 0 ,
68cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji                  ( max_adv_inst * INST_ID_IDX_MAX)* sizeof(INT8));
6916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
70cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji            p_multi_adv_com_data_cb->inst_cb = GKI_getbuf(( max_adv_inst + 1 )
7116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                                              * sizeof(btgatt_multi_adv_inst_cb));
729dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            memset(p_multi_adv_com_data_cb->inst_cb, 0 ,
73cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji                 ( max_adv_inst + 1) * sizeof(btgatt_multi_adv_inst_cb));
749dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach
75cafed88eb2caeaece08da1110625f4567a16ddd1Satya Calloji            for (int i=0; i < max_adv_inst * 2; i += 2)
769dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            {
779dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach                p_multi_adv_com_data_cb->clntif_map[i] = INVALID_ADV_INST;
789dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach                p_multi_adv_com_data_cb->clntif_map[i+1] = INVALID_ADV_INST;
799dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            }
8016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham        }
8116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    }
8216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
83ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    return p_multi_adv_com_data_cb;
84ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
85ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
86b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Callojivoid btif_gattc_incr_app_count(void)
87ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
889dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    // TODO: Instead of using a fragile reference counter here, one could
899dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    //       simply track the client_if instances that are in the map.
90b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji    ++user_app_count;
91ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
92ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
93b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Callojivoid btif_gattc_decr_app_count(void)
94ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
95b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji    if (user_app_count > 0)
96b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji        user_app_count --;
9716fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
98b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji    if(user_app_count == 0 && NULL != p_multi_adv_com_data_cb)
99ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
100b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji       GKI_freebuf (p_multi_adv_com_data_cb->clntif_map);
101b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji       GKI_freebuf (p_multi_adv_com_data_cb->inst_cb);
102b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji       GKI_freebuf(p_multi_adv_com_data_cb);
103b407515ffa145b1a2cab01467da03c92aeb0e8ebSatya Calloji       p_multi_adv_com_data_cb = NULL;
104ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
105ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
106ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
107ffb3960bb05f9acb0717d213612403487e9ad433Satya Callojiint btif_multi_adv_add_instid_map(int client_if, int inst_id, BOOLEAN gen_temp_instid)
108ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
10916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    int i=1;
110ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
111ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
11216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (NULL == p_multi_adv_data_cb)
113ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return INVALID_ADV_INST;
114ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
11516fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    for (i=1; i <  BTM_BleMaxMultiAdvInstanceCount(); i++)
116ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
11716fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham       if (client_if == p_multi_adv_data_cb->clntif_map[i + i])
118ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji       {
11916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham          if (!gen_temp_instid)
120ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji          {
121ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             // Write the final inst_id value obtained from stack layer
12216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham             p_multi_adv_data_cb->clntif_map[i + (i + 1)] = inst_id;
123e8c3d75b75493911ebf0f99c83676359657178f7Sharvil Nanavati             BTIF_TRACE_DEBUG("%s -Index: %d, Found client_if: %d", __FUNCTION__,
12416fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                i, p_multi_adv_data_cb->clntif_map[i + i]);
125ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             break;
126ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji          }
127ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji          else
128ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji          {
129ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji              //Store the passed in inst_id value
13016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham             if (inst_id != INVALID_ADV_INST)
13116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                 p_multi_adv_data_cb->clntif_map[i + (i + 1)] = inst_id;
132ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             else
13316fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                 p_multi_adv_data_cb->clntif_map[i + (i + 1)] = (i + 1);
13416fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
135e8c3d75b75493911ebf0f99c83676359657178f7Sharvil Nanavati             BTIF_TRACE_DEBUG("%s - Index:%d,Found client_if: %d", __FUNCTION__,
13616fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                i, p_multi_adv_data_cb->clntif_map[i + i]);
137ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             break;
138ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji          }
139ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji       }
140ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
141ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
14216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (i <  BTM_BleMaxMultiAdvInstanceCount())
143ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return i;
144ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
145ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // If client ID if is not found, then write both values
14616fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    for (i=1; i <  BTM_BleMaxMultiAdvInstanceCount(); i++)
147ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
14816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham        if (INVALID_ADV_INST == p_multi_adv_data_cb->clntif_map[i + i])
149ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        {
15016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham            p_multi_adv_data_cb->clntif_map[i + i] = client_if;
15116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham            if (inst_id != INVALID_ADV_INST)
15216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham               p_multi_adv_data_cb->clntif_map[i + (i + 1)] = inst_id;
15316fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham            else
15416fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                p_multi_adv_data_cb->clntif_map[i + (i + 1)] = (i + 1);
15516fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham            BTIF_TRACE_DEBUG("%s -Not found - Index:%d, client_if: %d, Inst ID: %d",
156ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                            __FUNCTION__,i,
15716fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                            p_multi_adv_data_cb->clntif_map[i + i],
15816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham                            p_multi_adv_data_cb->clntif_map[i + (i + 1)]);
15916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham            break;
160ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        }
161ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
162ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
16316fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (i <  BTM_BleMaxMultiAdvInstanceCount())
164ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return i;
165ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    return INVALID_ADV_INST;
166ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
167ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
168ffb3960bb05f9acb0717d213612403487e9ad433Satya Callojiint btif_multi_adv_instid_for_clientif(int client_if)
169ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
17016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    int i=1, ret = INVALID_ADV_INST;
171ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
172ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
173ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
17416fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (NULL == p_multi_adv_data_cb)
175ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return INVALID_ADV_INST;
176ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
177ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Retrieve the existing inst_id for the client_if value
17816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    for (i=1; i <  BTM_BleMaxMultiAdvInstanceCount(); i++)
179ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
18016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham       if (client_if == p_multi_adv_data_cb->clntif_map[i + i])
18116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham       {
18216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham           BTIF_TRACE_DEBUG("%s - Client if found", __FUNCTION__, client_if);
18316fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham           ret = p_multi_adv_data_cb->clntif_map[i + (i + 1)];
18416fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham       }
185ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
186ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
18716fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    return ret;
188ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
189ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
19016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadhamint btif_gattc_obtain_idx_for_datacb(int value, int clnt_inst_index)
191ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
19216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    int i=1;
193ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
194ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
195ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
19616fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (NULL == p_multi_adv_data_cb)
197ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return INVALID_ADV_INST;
198ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
199ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Retrieve the array index for the inst_id value
20016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    for (i=1; i <  BTM_BleMaxMultiAdvInstanceCount(); i++)
201ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
20216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham       if (value == p_multi_adv_data_cb->clntif_map[i + (i + clnt_inst_index)])
203ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji           break;
204ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
205ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
20616fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (i <  BTM_BleMaxMultiAdvInstanceCount())
207ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
20816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham        BTIF_TRACE_DEBUG("%s, %d",__FUNCTION__,i);
20916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham        return i;
210ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
211ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
212e8c3d75b75493911ebf0f99c83676359657178f7Sharvil Nanavati    BTIF_TRACE_DEBUG("%s Invalid instance",__FUNCTION__);
213ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    return INVALID_ADV_INST;
214ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
215ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
216ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
217ffb3960bb05f9acb0717d213612403487e9ad433Satya Callojivoid btif_gattc_adv_data_packager(int client_if, bool set_scan_rsp,
218ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                bool include_name, bool include_txpower, int min_interval, int max_interval,
2199dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach                int appearance, int manufacturer_len, char* manufacturer_data,
2209dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach                int service_data_len, char* service_data, int service_uuid_len,
221ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                char* service_uuid, btif_adv_data_t *p_multi_adv_inst)
222ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
223ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    memset(p_multi_adv_inst, 0 , sizeof(btif_adv_data_t));
224ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
225ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->client_if = (uint8_t) client_if;
226ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->set_scan_rsp = set_scan_rsp;
227ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->include_name = include_name;
228ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->include_txpower = include_txpower;
229ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->min_interval = min_interval;
230ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->max_interval = max_interval;
231ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->appearance = appearance;
232ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->manufacturer_len = manufacturer_len;
233ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
23416fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (manufacturer_len > 0)
235ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
236ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_inst->p_manufacturer_data = GKI_getbuf(manufacturer_len);
237ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        memcpy(p_multi_adv_inst->p_manufacturer_data, manufacturer_data, manufacturer_len);
238ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
239ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
240ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->service_data_len = service_data_len;
24116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (service_data_len > 0)
242ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
243ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_inst->p_service_data = GKI_getbuf(service_data_len);
244ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        memcpy(p_multi_adv_inst->p_service_data, service_data, service_data_len);
245ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
246ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
247ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    p_multi_adv_inst->service_uuid_len = service_uuid_len;
24816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (service_uuid_len > 0)
249ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
250ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_inst->p_service_uuid = GKI_getbuf(service_uuid_len);
251ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        memcpy(p_multi_adv_inst->p_service_uuid, service_uuid, service_uuid_len);
252ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
253ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
254ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
2550d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidgevoid btif_gattc_adv_data_cleanup(const btif_adv_data_t* adv)
256ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
2570d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge    if (adv->p_service_data)
2580d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge        GKI_freebuf(adv->p_service_data);
2590d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge
2600d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge    if (adv->p_service_uuid)
2610d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge        GKI_freebuf(adv->p_service_uuid);
2620d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge
2630d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge    if (adv->p_manufacturer_data)
2640d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge        GKI_freebuf(adv->p_manufacturer_data);
2650d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge}
2660d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge
2670d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge
2680d45f25e4102b14d942efa530f0e6a1a6c19787aIan CoolidgeBOOLEAN btif_gattc_copy_datacb(int cbindex, const btif_adv_data_t *p_adv_data,
2690d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge                               BOOLEAN bInstData) {
270ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
27116fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (NULL == p_multi_adv_data_cb || cbindex < 0)
272ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji       return false;
273ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
274e8c3d75b75493911ebf0f99c83676359657178f7Sharvil Nanavati    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
275ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    memset(&p_multi_adv_data_cb->inst_cb[cbindex].data, 0, sizeof(tBTA_BLE_ADV_DATA));
2767ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    p_multi_adv_data_cb->inst_cb[cbindex].mask = 0;
277ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
2783489ca9d44bbf0d3f00b1f5cd9675694774d48a8Satya Calloji    p_multi_adv_data_cb->inst_cb[cbindex].is_scan_rsp = p_adv_data->set_scan_rsp ? 1 : 0;
279ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (!p_adv_data->set_scan_rsp)
280ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
281ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         p_multi_adv_data_cb->inst_cb[cbindex].mask = BTM_BLE_AD_BIT_FLAGS;
2829dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach         p_multi_adv_data_cb->inst_cb[cbindex].data.flag = ADV_FLAGS_GENERAL;
2839dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach         if (p_multi_adv_data_cb->inst_cb[cbindex].timeout_s)
2849dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach             p_multi_adv_data_cb->inst_cb[cbindex].data.flag = ADV_FLAGS_LIMITED;
285962f1d1f9dc22d939bea4e9fb34a67028ff185fdAndre Eisenbach         if (p_multi_adv_data_cb->inst_cb[cbindex].param.adv_type == BTA_BLE_NON_CONNECT_EVT)
286962f1d1f9dc22d939bea4e9fb34a67028ff185fdAndre Eisenbach             p_multi_adv_data_cb->inst_cb[cbindex].data.flag &=
287962f1d1f9dc22d939bea4e9fb34a67028ff185fdAndre Eisenbach                    ~(BTA_DM_LIMITED_DISC | BTA_DM_GENERAL_DISC);
288962f1d1f9dc22d939bea4e9fb34a67028ff185fdAndre Eisenbach         if (p_multi_adv_data_cb->inst_cb[cbindex].data.flag == 0)
289962f1d1f9dc22d939bea4e9fb34a67028ff185fdAndre Eisenbach            p_multi_adv_data_cb->inst_cb[cbindex].mask = 0;
290ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
291ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
292ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_adv_data->include_name)
293ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_DEV_NAME;
294ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
295ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_adv_data->include_txpower)
296ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_TX_PWR;
297ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
298ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (false == bInstData && p_adv_data->min_interval > 0 && p_adv_data->max_interval > 0 &&
299ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_adv_data->max_interval > p_adv_data->min_interval)
300ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
301ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_INT_RANGE;
302ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].data.int_range.low =
303ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                                        p_adv_data->min_interval;
304ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].data.int_range.hi =
305ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                                        p_adv_data->max_interval;
306ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
307ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    else
30816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (true == bInstData)
309ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
310ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        if (p_multi_adv_data_cb->inst_cb[cbindex].param.adv_int_min > 0 &&
311ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            p_multi_adv_data_cb->inst_cb[cbindex].param.adv_int_max > 0 &&
312ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            p_multi_adv_data_cb->inst_cb[cbindex].param.adv_int_max >
313ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            p_multi_adv_data_cb->inst_cb[cbindex].param.adv_int_min)
314ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        {
315ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji              p_multi_adv_data_cb->inst_cb[cbindex].data.int_range.low =
316ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji              p_multi_adv_data_cb->inst_cb[cbindex].param.adv_int_min;
317ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji              p_multi_adv_data_cb->inst_cb[cbindex].data.int_range.hi =
318ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji              p_multi_adv_data_cb->inst_cb[cbindex].param.adv_int_max;
319ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        }
320ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
321ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        if (p_adv_data->include_txpower)
322ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        {
323ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            p_multi_adv_data_cb->inst_cb[cbindex].data.tx_power =
324ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                p_multi_adv_data_cb->inst_cb[cbindex].param.tx_power;
325ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        }
326ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
327ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
328ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_adv_data->appearance != 0)
329ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
330ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_APPEARANCE;
331ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].data.appearance = p_adv_data->appearance;
332ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
333ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
334ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_adv_data->manufacturer_len > 0 && p_adv_data->p_manufacturer_data != NULL)
335ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
336ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         p_multi_adv_data_cb->inst_cb[cbindex].data.p_manu =
337ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                            GKI_getbuf(sizeof(tBTA_BLE_MANU));
33816fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham         if (p_multi_adv_data_cb->inst_cb[cbindex].data.p_manu != NULL)
339ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         {
340ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            p_multi_adv_data_cb->inst_cb[cbindex].data.p_manu->p_val =
341ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                            GKI_getbuf(p_adv_data->manufacturer_len);
342ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            if (p_multi_adv_data_cb->inst_cb[cbindex].data.p_manu->p_val != NULL)
343ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            {
344ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                 p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_MANU;
345ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                 p_multi_adv_data_cb->inst_cb[cbindex].data.p_manu->len =
346ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                                p_adv_data->manufacturer_len;
347ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                 memcpy(p_multi_adv_data_cb->inst_cb[cbindex].data.p_manu->p_val,
348ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                    p_adv_data->p_manufacturer_data, p_adv_data->manufacturer_len);
349ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            }
350ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         }
3517ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    }
352ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
3537ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    tBTA_BLE_PROP_ELEM *p_elem_service_data = NULL;
3547ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    if (p_adv_data->service_data_len > 0 && p_adv_data->p_service_data != NULL)
3557ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    {
356e8c3d75b75493911ebf0f99c83676359657178f7Sharvil Nanavati         BTIF_TRACE_DEBUG("%s - In service_data", __FUNCTION__);
357ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         p_elem_service_data = GKI_getbuf(sizeof(tBTA_BLE_PROP_ELEM));
358ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         if (p_elem_service_data != NULL)
359ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         {
360ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             p_elem_service_data->p_val = GKI_getbuf(p_adv_data->service_data_len);
361ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             if (p_elem_service_data->p_val != NULL)
362ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             {
363ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                 p_elem_service_data->adv_type = BTM_BLE_AD_TYPE_SERVICE_DATA;
364ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                 p_elem_service_data->len = p_adv_data->service_data_len;
365ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                 memcpy(p_elem_service_data->p_val, p_adv_data->p_service_data,
366ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                             p_adv_data->service_data_len);
367ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             } else {
368ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                     GKI_freebuf(p_elem_service_data);
369ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji                     p_elem_service_data = NULL;
370ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji               }
371ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji         }
3727ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    }
373ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
3747ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    if (NULL != p_elem_service_data)
3757ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    {
3767ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        p_multi_adv_data_cb->inst_cb[cbindex].data.p_proprietary =
3777ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                                                   GKI_getbuf(sizeof(tBTA_BLE_PROPRIETARY));
3787ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        if (NULL != p_multi_adv_data_cb->inst_cb[cbindex].data.p_proprietary)
3797ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        {
3807ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            tBTA_BLE_PROP_ELEM *p_elem = NULL;
3817ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            tBTA_BLE_PROPRIETARY *p_prop = p_multi_adv_data_cb->inst_cb[cbindex].data.p_proprietary;
3827ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            p_prop->num_elem = 0;
3837ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_PROPRIETARY;
3847ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            p_prop->num_elem = 1;
3857ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            p_prop->p_elem = GKI_getbuf(sizeof(tBTA_BLE_PROP_ELEM) * p_prop->num_elem);
3867ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            p_elem = p_prop->p_elem;
3877ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            if (NULL != p_elem)
3887ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                memcpy(p_elem++, p_elem_service_data, sizeof(tBTA_BLE_PROP_ELEM));
3897ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji            GKI_freebuf(p_elem_service_data);
3907ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        }
3917ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    }
392ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
3930d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge    if (p_adv_data->service_uuid_len && p_adv_data->p_service_uuid)
3947ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    {
3957ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        UINT16 *p_uuid_out16 = NULL;
3967ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        UINT32 *p_uuid_out32 = NULL;
3970d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge        for (int position = 0; position < p_adv_data->service_uuid_len; position += LEN_UUID_128)
3987ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        {
3997ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji             bt_uuid_t uuid;
4000d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge             memset(&uuid, 0, sizeof(uuid));
4010d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge             memcpy(&uuid.uu, p_adv_data->p_service_uuid + position, LEN_UUID_128);
4027ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji
4037ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji             tBT_UUID bt_uuid;
4040d45f25e4102b14d942efa530f0e6a1a6c19787aIan Coolidge             memset(&bt_uuid, 0, sizeof(bt_uuid));
4057ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji             btif_to_bta_uuid(&bt_uuid, &uuid);
4067ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji
4077ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji             switch(bt_uuid.len)
408ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             {
4097ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                case (LEN_UUID_16):
4107ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                {
4117ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                  if (NULL == p_multi_adv_data_cb->inst_cb[cbindex].data.p_services)
4127ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                  {
4137ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_services =
4147ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                                                          GKI_getbuf(sizeof(tBTA_BLE_SERVICE));
4157ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_services->list_cmpl = FALSE;
4167ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_services->num_service = 0;
4177ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_services->p_uuid =
4187ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                              GKI_getbuf(p_adv_data->service_uuid_len / LEN_UUID_128 * LEN_UUID_16);
4197ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_uuid_out16 = p_multi_adv_data_cb->inst_cb[cbindex].data.p_services->p_uuid;
4207ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                  }
4217ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji
4227ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                  if (NULL != p_multi_adv_data_cb->inst_cb[cbindex].data.p_services->p_uuid)
4237ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                  {
4247ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                     BTIF_TRACE_DEBUG("%s - In 16-UUID_data", __FUNCTION__);
4257ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                     p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_SERVICE;
4267ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                     ++p_multi_adv_data_cb->inst_cb[cbindex].data.p_services->num_service;
4277ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                     *p_uuid_out16++ = bt_uuid.uu.uuid16;
4287ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                  }
4297ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                  break;
4307ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                }
4317ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji
4327ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                case (LEN_UUID_32):
4337ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                {
4347ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   if (NULL == p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b)
4357ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   {
4367ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b =
4377ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                                                          GKI_getbuf(sizeof(tBTA_BLE_32SERVICE));
4387ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b->list_cmpl = FALSE;
4397ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b->num_service = 0;
4407ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b->p_uuid =
4417ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                             GKI_getbuf(p_adv_data->service_uuid_len / LEN_UUID_128 * LEN_UUID_32);
4427ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_uuid_out32 = p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b->p_uuid;
4437ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   }
4447ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji
4457ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   if (NULL != p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b->p_uuid)
4467ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   {
4477ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      BTIF_TRACE_DEBUG("%s - In 32-UUID_data", __FUNCTION__);
4487ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_SERVICE_32;
4497ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      ++p_multi_adv_data_cb->inst_cb[cbindex].data.p_service_32b->num_service;
4507ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      *p_uuid_out32++ = bt_uuid.uu.uuid32;
4517ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   }
4527ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   break;
4537ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                }
4547ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji
4557ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                case (LEN_UUID_128):
4567ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                {
4577ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   /* Currently, only one 128-bit UUID is supported */
4587ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   if (NULL == p_multi_adv_data_cb->inst_cb[cbindex].data.p_services_128b)
4597ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   {
4607ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      p_multi_adv_data_cb->inst_cb[cbindex].data.p_services_128b =
4617ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                                                          GKI_getbuf(sizeof(tBTA_BLE_128SERVICE));
4627ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      if (NULL != p_multi_adv_data_cb->inst_cb[cbindex].data.p_services_128b)
4637ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      {
4647ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                         BTIF_TRACE_DEBUG("%s - In 128-UUID_data", __FUNCTION__);
4657ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                         p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_SERVICE_128;
4667ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                         memcpy(p_multi_adv_data_cb->inst_cb[cbindex].data.p_services_128b->uuid128,
4677ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                                                         bt_uuid.uu.uuid128, LEN_UUID_128);
4687ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                         BTIF_TRACE_DEBUG("%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", bt_uuid.uu.uuid128[0],
4697ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                            bt_uuid.uu.uuid128[1],bt_uuid.uu.uuid128[2], bt_uuid.uu.uuid128[3],
4707ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                            bt_uuid.uu.uuid128[4],bt_uuid.uu.uuid128[5],bt_uuid.uu.uuid128[6],
4717ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                            bt_uuid.uu.uuid128[7],bt_uuid.uu.uuid128[8],bt_uuid.uu.uuid128[9],
4727ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                            bt_uuid.uu.uuid128[10],bt_uuid.uu.uuid128[11],bt_uuid.uu.uuid128[12],
4737ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                            bt_uuid.uu.uuid128[13],bt_uuid.uu.uuid128[14],bt_uuid.uu.uuid128[15]);
4747ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                         p_multi_adv_data_cb->inst_cb[cbindex].data.p_services_128b->list_cmpl = TRUE;
4757ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                      }
4767ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   }
4777ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                   break;
4787ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                }
4797ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji
4807ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                default:
4817ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji                     break;
482ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji             }
4837ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji        }
4847ddf2d88501a2dd17045fb2f126ec99dcac9b5d9Satya Calloji    }
485ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
486ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji     return true;
487ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
488ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
4893770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Callojivoid btif_gattc_clear_clientif(int client_if, BOOLEAN stop_timer)
490ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
491ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
49216fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (NULL == p_multi_adv_data_cb)
493ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return;
494ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
495ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Clear both the inst_id and client_if values
496169cd7a221a79474d36e5c1697e862c1d640c064Andre Eisenbach    for (int i=0; i < BTM_BleMaxMultiAdvInstanceCount()*2; i+=2)
497ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
4989dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        if (client_if == p_multi_adv_data_cb->clntif_map[i])
4999dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        {
5003770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji            btif_gattc_cleanup_inst_cb(p_multi_adv_data_cb->clntif_map[i+1], stop_timer);
5013770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji            if (stop_timer)
5023770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji            {
5033770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji                p_multi_adv_data_cb->clntif_map[i] = INVALID_ADV_INST;
5043770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji                p_multi_adv_data_cb->clntif_map[i+1] = INVALID_ADV_INST;
5053770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji                BTIF_TRACE_DEBUG("Cleaning up index %d for clnt_if :%d,", i/2, client_if);
5063770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji            }
5079dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach            break;
5089dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        }
509ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
510ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
511ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
5123770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Callojivoid btif_gattc_cleanup_inst_cb(int inst_id, BOOLEAN stop_timer)
513ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
514ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Check for invalid instance id
51516fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (inst_id < 0 || inst_id >= BTM_BleMaxMultiAdvInstanceCount())
516ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return;
517ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
518ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
51916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham    if (NULL == p_multi_adv_data_cb)
520ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        return;
521ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
5223770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    int cbindex = (STD_ADV_INSTID == inst_id) ?
5233770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        STD_ADV_INSTID : btif_gattc_obtain_idx_for_datacb(inst_id, INST_ID_IDX);
5243770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    if (cbindex < 0) return;
525ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
5263770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    BTIF_TRACE_DEBUG("Cleaning up multi_inst_cb for inst_id %d, cbindex %d", inst_id, cbindex);
5273770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    btif_gattc_cleanup_multi_inst_cb(&p_multi_adv_data_cb->inst_cb[cbindex], stop_timer);
528ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
529ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
5303770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Callojivoid btif_gattc_cleanup_multi_inst_cb(btgatt_multi_adv_inst_cb *p_multi_inst_cb,
5313770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji                                             BOOLEAN stop_timer)
532ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji{
533cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach    if (p_multi_inst_cb == NULL)
534cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach        return;
535cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach
5369dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    // Discoverability timer cleanup
5373770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    if (stop_timer)
5383770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    {
5393770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        if (p_multi_inst_cb->tle_limited_timer.in_use)
5403770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji            btu_stop_timer_oneshot(&p_multi_inst_cb->tle_limited_timer);
5413770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        p_multi_inst_cb->tle_limited_timer.in_use = 0;
5423770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    }
5439dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach
544ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Manufacturer data cleanup
545ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_multi_inst_cb->data.p_manu != NULL)
546ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
5473770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_manu->p_val);
5483770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_manu);
549ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
550ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
551ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Proprietary data cleanup
552ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_multi_inst_cb->data.p_proprietary != NULL)
553ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
554ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        int i = 0;
555ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        tBTA_BLE_PROP_ELEM *p_elem = p_multi_inst_cb->data.p_proprietary->p_elem;
556ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        while (i++ != p_multi_inst_cb->data.p_proprietary->num_elem
557ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            && p_elem)
558ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        {
5593770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji            btif_gattc_cleanup((void**) &p_elem->p_val);
560ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji            ++p_elem;
561ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji        }
562ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
5633770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_proprietary->p_elem);
5643770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_proprietary);
565ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
566ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
567ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Service list cleanup
568ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_multi_inst_cb->data.p_services != NULL)
569ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
5703770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_services->p_uuid);
5713770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_services);
572ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
573ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
574ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    // Service data cleanup
575ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_multi_inst_cb->data.p_service_data != NULL)
576ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
5773770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_service_data->p_val);
5783770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_service_data);
579ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
580ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
5813770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_services_128b);
582ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
583ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_multi_inst_cb->data.p_service_32b != NULL)
584ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
5853770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_service_32b->p_uuid);
5863770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_service_32b);
587ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
588ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
589ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_multi_inst_cb->data.p_sol_services != NULL)
590ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
5913770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_sol_services->p_uuid);
5923770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_sol_services);
593ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
594ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
595ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    if (p_multi_inst_cb->data.p_sol_service_32b != NULL)
596ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    {
5973770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_sol_service_32b->p_uuid);
5983770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji        btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_sol_service_32b);
599ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji    }
600ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
6013770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji    btif_gattc_cleanup((void**) &p_multi_inst_cb->data.p_sol_service_128b);
6023770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji}
6033770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji
6043770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Callojivoid btif_gattc_cleanup(void** buf)
6053770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji{
6063770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji   if (NULL == *buf) return;
6073770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji   GKI_freebuf(*buf);
6083770c47387d3dfc6d2e2d32f4409a82119e726e3Satya Calloji   *buf = NULL;
609ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji}
610ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji
611cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbachvoid btif_multi_adv_timer_ctrl(int client_if, TIMER_CBACK cb)
6129dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach{
6139dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    int inst_id = btif_multi_adv_instid_for_clientif(client_if);
6149dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    if (inst_id == INVALID_ADV_INST)
6159dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        return;
6169dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach
6179dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    int cbindex = btif_gattc_obtain_idx_for_datacb(inst_id, INST_ID_IDX);
6189dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    if (cbindex == INVALID_ADV_INST)
6199dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        return;
6209dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach
6219dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
6229dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    if (p_multi_adv_data_cb == NULL)
6239dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        return;
6249dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach
6259dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    if (cb == NULL)
6269dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    {
627cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach        if (p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer.in_use)
628cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach            btu_stop_timer_oneshot(&p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer);
6299dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    } else {
6309dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        if (p_multi_adv_data_cb->inst_cb[cbindex].timeout_s != 0)
6319dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        {
632cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach            if (p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer.in_use)
633cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach                btu_stop_timer_oneshot(&p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer);
634cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach
635cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach            memset(&p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer, 0, sizeof(TIMER_LIST_ENT));
636cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach            p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer.param = (UINT32)cb;
637cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach            p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer.data = (UINT32)client_if;
638cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach            btu_start_timer_oneshot(&p_multi_adv_data_cb->inst_cb[cbindex].tle_limited_timer,
639cf32e8d4e0cabac8432a3c6c6d8ece27d6067770Andre Eisenbach                    BTU_TTYPE_USER_FUNC, p_multi_adv_data_cb->inst_cb[cbindex].timeout_s);
6409dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach        }
6419dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach    }
6429dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach}
6439dd5a9c11f2022fa9709f0c34f04560c21105152Andre Eisenbach
644ffb3960bb05f9acb0717d213612403487e9ad433Satya Calloji#endif
645