btif_gatt_client.c revision 6e2d9db5148176d88d7f48c3b2ad5b27c57ca14d
1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2014 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 *
22 *  Filename:      btif_gatt_client.c
23 *
24 *  Description:   GATT client implementation
25 *
26 *******************************************************************************/
27
28#include <hardware/bluetooth.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <errno.h>
32#include <string.h>
33
34#define LOG_TAG "BtGatt.btif"
35
36#include "btif_common.h"
37#include "btif_util.h"
38
39#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
40
41#include "btif_gatt_multi_adv_util.h"
42#include <hardware/bt_gatt.h>
43#include "bta_api.h"
44#include "bta_gatt_api.h"
45#include "bd.h"
46#include "btif_storage.h"
47#include "btif_config.h"
48
49#include "btif_gatt.h"
50#include "btif_gatt_util.h"
51#include "btif_dm.h"
52#include "btif_storage.h"
53
54#include "vendor_api.h"
55
56/*******************************************************************************
57**  Constants & Macros
58********************************************************************************/
59
60#define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
61    {\
62        BTIF_TRACE_WARNING("%s: BTGATT not initialized", __FUNCTION__);\
63        return BT_STATUS_NOT_READY;\
64    } else {\
65        BTIF_TRACE_DEBUG("%s", __FUNCTION__);\
66    }
67
68
69typedef enum {
70    BTIF_GATTC_REGISTER_APP = 1000,
71    BTIF_GATTC_UNREGISTER_APP,
72    BTIF_GATTC_SCAN_START,
73    BTIF_GATTC_SCAN_STOP,
74    BTIF_GATTC_OPEN,
75    BTIF_GATTC_CLOSE,
76    BTIF_GATTC_SEARCH_SERVICE,
77    BTIF_GATTC_GET_FIRST_CHAR,
78    BTIF_GATTC_GET_NEXT_CHAR,
79    BTIF_GATTC_GET_FIRST_CHAR_DESCR,
80    BTIF_GATTC_GET_NEXT_CHAR_DESCR,
81    BTIF_GATTC_GET_FIRST_INCL_SERVICE,
82    BTIF_GATTC_GET_NEXT_INCL_SERVICE,
83    BTIF_GATTC_READ_CHAR,
84    BTIF_GATTC_READ_CHAR_DESCR,
85    BTIF_GATTC_WRITE_CHAR,
86    BTIF_GATTC_WRITE_CHAR_DESCR,
87    BTIF_GATTC_EXECUTE_WRITE,
88    BTIF_GATTC_REG_FOR_NOTIFICATION,
89    BTIF_GATTC_DEREG_FOR_NOTIFICATION,
90    BTIF_GATTC_REFRESH,
91    BTIF_GATTC_READ_RSSI,
92    BTIF_GATTC_LISTEN,
93    BTIF_GATTC_SET_ADV_DATA,
94    BTIF_GATTC_CONFIGURE_MTU,
95    BTIF_GATTC_CONN_PARAM_UPDT,
96    BTIF_GATTC_SCAN_FILTER_PARAM_SETUP,
97    BTIF_GATTC_SCAN_FILTER_CONFIG,
98    BTIF_GATTC_SCAN_FILTER_CLEAR,
99    BTIF_GATTC_SCAN_FILTER_ENABLE,
100    BTIF_GATTC_SET_SCAN_PARAMS,
101    BTIF_GATTC_ADV_INSTANCE_ENABLE,
102    BTIF_GATTC_ADV_INSTANCE_UPDATE,
103    BTIF_GATTC_ADV_INSTANCE_SET_DATA,
104    BTIF_GATTC_ADV_INSTANCE_DISABLE,
105    BTIF_GATTC_CONFIG_STORAGE_PARAMS,
106    BTIF_GATTC_ENABLE_BATCH_SCAN,
107    BTIF_GATTC_READ_BATCH_SCAN_REPORTS,
108    BTIF_GATTC_DISABLE_BATCH_SCAN
109} btif_gattc_event_t;
110
111#define BTIF_GATT_MAX_OBSERVED_DEV 40
112
113#define BTIF_GATT_OBSERVE_EVT   0x1000
114#define BTIF_GATTC_RSSI_EVT     0x1001
115#define BTIF_GATTC_SCAN_FILTER_EVT   0x1003
116
117#define ENABLE_BATCH_SCAN 1
118#define DISABLE_BATCH_SCAN 0
119
120/*******************************************************************************
121**  Local type definitions
122********************************************************************************/
123typedef struct
124{
125    uint8_t report_format;
126    uint16_t data_len;
127    uint8_t num_records;
128    uint8_t *p_rep_data;
129} btgatt_batch_reports;
130
131typedef struct
132{
133    uint8_t  status;
134    uint8_t  client_if;
135    uint8_t  filt_index;
136    uint8_t  adv_state;
137    uint8_t  action;
138    uint8_t  avbl_space;
139    uint8_t  lost_timeout;
140    bt_bdaddr_t bd_addr;
141    uint8_t  batch_scan_full_max;
142    uint8_t  batch_scan_trunc_max;
143    uint8_t  batch_scan_notify_threshold;
144    tBTA_BLE_SCAN_MODE scan_mode;
145    uint32_t scan_interval;
146    uint32_t scan_window;
147    tBTA_BLE_DISCARD_RULE discard_rule;
148    tBLE_ADDR_TYPE        addr_type;
149    btgatt_batch_reports read_reports;
150} btgatt_batch_track_cb_t;
151
152typedef tBTA_DM_BLE_PF_FILT_PARAMS btgatt_adv_filt_param_t;
153
154typedef struct
155{
156    uint8_t     client_if;
157    uint8_t     action;
158    tBTA_DM_BLE_PF_COND_TYPE filt_type;
159    bt_bdaddr_t bd_addr;
160    uint8_t     value[BTGATT_MAX_ATTR_LEN];
161    uint8_t     value_len;
162    uint8_t     filt_index;
163    uint16_t    conn_id;
164    uint16_t    company_id_mask;
165    bt_uuid_t   uuid;
166    bt_uuid_t   uuid_mask;
167    uint8_t     value_mask[BTGATT_MAX_ATTR_LEN];
168    uint8_t     value_mask_len;
169    uint8_t     has_mask;
170    uint8_t     addr_type;
171    uint8_t     status;
172    tBTA_DM_BLE_PF_AVBL_SPACE avbl_space;
173    tBTA_DM_BLE_SCAN_COND_OP cond_op;
174    btgatt_adv_filt_param_t adv_filt_param;
175} btgatt_adv_filter_cb_t;
176
177typedef struct
178{
179    uint8_t     value[BTGATT_MAX_ATTR_LEN];
180    uint8_t     inst_id;
181    bt_bdaddr_t bd_addr;
182    btgatt_srvc_id_t srvc_id;
183    btgatt_srvc_id_t incl_srvc_id;
184    btgatt_gatt_id_t char_id;
185    btgatt_gatt_id_t descr_id;
186    bt_uuid_t   uuid;
187    bt_uuid_t   uuid_mask;
188    uint16_t    conn_id;
189    uint16_t    len;
190    uint16_t    mask;
191    uint16_t    scan_interval;
192    uint16_t    scan_window;
193    uint8_t     client_if;
194    uint8_t     action;
195    uint8_t     is_direct;
196    uint8_t     search_all;
197    uint8_t     auth_req;
198    uint8_t     write_type;
199    uint8_t     status;
200    uint8_t     addr_type;
201    uint8_t     start;
202    uint8_t     has_mask;
203    int8_t      rssi;
204    uint8_t     flag;
205    tBT_DEVICE_TYPE device_type;
206    btgatt_transport_t transport;
207} __attribute__((packed)) btif_gattc_cb_t;
208
209typedef struct
210{
211    bt_bdaddr_t bd_addr;
212    uint16_t    min_interval;
213    uint16_t    max_interval;
214    uint16_t    timeout;
215    uint16_t    latency;
216} btif_conn_param_cb_t;
217
218typedef struct
219{
220    bt_bdaddr_t bd_addr;
221    BOOLEAN     in_use;
222}__attribute__((packed)) btif_gattc_dev_t;
223
224typedef struct
225{
226    btif_gattc_dev_t remote_dev[BTIF_GATT_MAX_OBSERVED_DEV];
227    uint8_t            addr_type;
228    uint8_t            next_storage_idx;
229}__attribute__((packed)) btif_gattc_dev_cb_t;
230
231/*******************************************************************************
232**  Static variables
233********************************************************************************/
234
235extern const btgatt_callbacks_t *bt_gatt_callbacks;
236static btif_gattc_dev_cb_t  btif_gattc_dev_cb;
237static btif_gattc_dev_cb_t  *p_dev_cb = &btif_gattc_dev_cb;
238static uint8_t rssi_request_client_if;
239
240/*******************************************************************************
241**  Static functions
242********************************************************************************/
243
244static void btapp_gattc_req_data(UINT16 event, char *p_dest, char *p_src)
245{
246    tBTA_GATTC *p_dest_data = (tBTA_GATTC*) p_dest;
247    tBTA_GATTC *p_src_data = (tBTA_GATTC*) p_src;
248
249    if (!p_src_data || !p_dest_data)
250       return;
251
252    // Copy basic structure first
253    memcpy(p_dest_data, p_src_data, sizeof(tBTA_GATTC));
254
255    // Allocate buffer for request data if necessary
256    switch (event)
257    {
258        case BTA_GATTC_READ_CHAR_EVT:
259        case BTA_GATTC_READ_DESCR_EVT:
260
261            if (p_src_data->read.p_value != NULL)
262            {
263                p_dest_data->read.p_value = GKI_getbuf(sizeof(tBTA_GATT_READ_VAL));
264
265                if (p_dest_data->read.p_value != NULL)
266                {
267                    memcpy(p_dest_data->read.p_value, p_src_data->read.p_value,
268                        sizeof(tBTA_GATT_READ_VAL));
269
270                    // Allocate buffer for att value if necessary
271                    if (get_uuid16(&p_src_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
272                      && p_src_data->read.p_value->unformat.len > 0
273                      && p_src_data->read.p_value->unformat.p_value != NULL)
274                    {
275                        p_dest_data->read.p_value->unformat.p_value =
276                                       GKI_getbuf(p_src_data->read.p_value->unformat.len);
277                        if (p_dest_data->read.p_value->unformat.p_value != NULL)
278                        {
279                            memcpy(p_dest_data->read.p_value->unformat.p_value,
280                                   p_src_data->read.p_value->unformat.p_value,
281                                   p_src_data->read.p_value->unformat.len);
282                        }
283                    }
284                }
285            }
286            else
287            {
288                BTIF_TRACE_WARNING("%s :Src read.p_value ptr is NULL for event  0x%x",
289                                    __FUNCTION__, event);
290                p_dest_data->read.p_value = NULL;
291
292            }
293            break;
294
295        default:
296            break;
297    }
298}
299
300static void btapp_gattc_free_req_data(UINT16 event, tBTA_GATTC *p_data)
301{
302    switch (event)
303    {
304        case BTA_GATTC_READ_CHAR_EVT:
305        case BTA_GATTC_READ_DESCR_EVT:
306            if (p_data != NULL && p_data->read.p_value != NULL)
307            {
308                if (get_uuid16 (&p_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
309                  && p_data->read.p_value->unformat.len > 0
310                  && p_data->read.p_value->unformat.p_value != NULL)
311                {
312                    GKI_freebuf(p_data->read.p_value->unformat.p_value);
313                }
314                GKI_freebuf(p_data->read.p_value);
315            }
316            break;
317
318        default:
319            break;
320    }
321}
322
323static void btif_gattc_init_dev_cb(void)
324{
325    memset(p_dev_cb, 0, sizeof(btif_gattc_dev_cb_t));
326}
327
328static void btif_gattc_add_remote_bdaddr (BD_ADDR p_bda, uint8_t addr_type)
329{
330    BOOLEAN found=FALSE;
331    uint8_t i;
332    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
333    {
334        if (!p_dev_cb->remote_dev[i].in_use )
335        {
336            memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
337            p_dev_cb->addr_type = addr_type;
338            p_dev_cb->remote_dev[i].in_use = TRUE;
339            BTIF_TRACE_DEBUG("%s device added idx=%d", __FUNCTION__, i  );
340            break;
341        }
342    }
343
344    if ( i == BTIF_GATT_MAX_OBSERVED_DEV)
345    {
346        i= p_dev_cb->next_storage_idx;
347        memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
348        p_dev_cb->addr_type = addr_type;
349        p_dev_cb->remote_dev[i].in_use = TRUE;
350        BTIF_TRACE_DEBUG("%s device overwrite idx=%d", __FUNCTION__, i  );
351        p_dev_cb->next_storage_idx++;
352        if(p_dev_cb->next_storage_idx >= BTIF_GATT_MAX_OBSERVED_DEV)
353               p_dev_cb->next_storage_idx = 0;
354    }
355}
356
357static BOOLEAN btif_gattc_find_bdaddr (BD_ADDR p_bda)
358{
359    uint8_t i;
360    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
361    {
362        if (p_dev_cb->remote_dev[i].in_use &&
363            !memcmp(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN))
364        {
365            return TRUE;
366        }
367    }
368    return FALSE;
369}
370
371static void btif_gattc_update_properties ( btif_gattc_cb_t *p_btif_cb )
372{
373    uint8_t remote_name_len;
374    uint8_t *p_eir_remote_name=NULL;
375    bt_bdname_t bdname;
376
377    p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
378                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
379
380    if(p_eir_remote_name == NULL)
381    {
382        p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
383                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
384    }
385
386    if(p_eir_remote_name)
387    {
388        memcpy(bdname.name, p_eir_remote_name, remote_name_len);
389        bdname.name[remote_name_len]='\0';
390
391        BTIF_TRACE_DEBUG("%s BLE device name=%s len=%d dev_type=%d", __FUNCTION__, bdname.name,
392              remote_name_len, p_btif_cb->device_type  );
393        btif_dm_update_ble_remote_properties( p_btif_cb->bd_addr.address,   bdname.name,
394                                               p_btif_cb->device_type);
395    }
396
397    btif_storage_set_remote_addr_type( &p_btif_cb->bd_addr, p_btif_cb->addr_type);
398}
399
400static void btif_gattc_upstreams_evt(uint16_t event, char* p_param)
401{
402    BTIF_TRACE_EVENT("%s: Event %d", __FUNCTION__, event);
403
404    tBTA_GATTC *p_data = (tBTA_GATTC*) p_param;
405    switch (event)
406    {
407        case BTA_GATTC_REG_EVT:
408        {
409            bt_uuid_t app_uuid;
410            bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
411            HAL_CBACK(bt_gatt_callbacks, client->register_client_cb
412                , p_data->reg_oper.status
413                , p_data->reg_oper.client_if
414                , &app_uuid
415            );
416            break;
417        }
418
419        case BTA_GATTC_DEREG_EVT:
420            break;
421
422        case BTA_GATTC_READ_CHAR_EVT:
423        {
424            btgatt_read_params_t data;
425            set_read_value(&data, &p_data->read);
426
427            HAL_CBACK(bt_gatt_callbacks, client->read_characteristic_cb
428                , p_data->read.conn_id, p_data->read.status, &data);
429            break;
430        }
431
432        case BTA_GATTC_WRITE_CHAR_EVT:
433        case BTA_GATTC_PREP_WRITE_EVT:
434        {
435            btgatt_write_params_t data;
436            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
437            bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
438
439            HAL_CBACK(bt_gatt_callbacks, client->write_characteristic_cb
440                , p_data->write.conn_id, p_data->write.status, &data
441            );
442            break;
443        }
444
445        case BTA_GATTC_EXEC_EVT:
446        {
447            HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb
448                , p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status
449            );
450            break;
451        }
452
453        case BTA_GATTC_SEARCH_CMPL_EVT:
454        {
455            HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb
456                , p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
457            break;
458        }
459
460        case BTA_GATTC_SEARCH_RES_EVT:
461        {
462            btgatt_srvc_id_t data;
463            bta_to_btif_srvc_id(&data, &(p_data->srvc_res.service_uuid));
464            HAL_CBACK(bt_gatt_callbacks, client->search_result_cb
465                , p_data->srvc_res.conn_id, &data);
466            break;
467        }
468
469        case BTA_GATTC_READ_DESCR_EVT:
470        {
471            btgatt_read_params_t data;
472            set_read_value(&data, &p_data->read);
473
474            HAL_CBACK(bt_gatt_callbacks, client->read_descriptor_cb
475                , p_data->read.conn_id, p_data->read.status, &data);
476            break;
477        }
478
479        case BTA_GATTC_WRITE_DESCR_EVT:
480        {
481            btgatt_write_params_t data;
482            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
483            bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
484            bta_to_btif_gatt_id(&data.descr_id, &p_data->write.descr_type);
485
486            HAL_CBACK(bt_gatt_callbacks, client->write_descriptor_cb
487                , p_data->write.conn_id, p_data->write.status, &data);
488            break;
489        }
490
491        case BTA_GATTC_NOTIF_EVT:
492        {
493            btgatt_notify_params_t data;
494
495            bdcpy(data.bda.address, p_data->notify.bda);
496
497            bta_to_btif_srvc_id(&data.srvc_id, &p_data->notify.char_id.srvc_id);
498            bta_to_btif_gatt_id(&data.char_id, &p_data->notify.char_id.char_id);
499            memcpy(data.value, p_data->notify.value, p_data->notify.len);
500
501            data.is_notify = p_data->notify.is_notify;
502            data.len = p_data->notify.len;
503
504            HAL_CBACK(bt_gatt_callbacks, client->notify_cb
505                , p_data->notify.conn_id, &data);
506
507            if (p_data->notify.is_notify == FALSE)
508            {
509                BTA_GATTC_SendIndConfirm(p_data->notify.conn_id,
510                                         &p_data->notify.char_id);
511            }
512            break;
513        }
514
515        case BTA_GATTC_OPEN_EVT:
516        {
517            bt_bdaddr_t bda;
518            bdcpy(bda.address, p_data->open.remote_bda);
519
520            HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id
521                , p_data->open.status, p_data->open.client_if, &bda);
522
523            if (GATT_DEF_BLE_MTU_SIZE != p_data->open.mtu && p_data->open.mtu)
524            {
525                HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb, p_data->open.conn_id
526                    , p_data->open.status , p_data->open.mtu);
527            }
528
529            if (p_data->open.status == BTA_GATT_OK)
530                btif_gatt_check_encrypted_link(p_data->open.remote_bda);
531            break;
532        }
533
534        case BTA_GATTC_CLOSE_EVT:
535        {
536            bt_bdaddr_t bda;
537            bdcpy(bda.address, p_data->close.remote_bda);
538            HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id
539                , p_data->status, p_data->close.client_if, &bda);
540            break;
541        }
542
543        case BTA_GATTC_ACL_EVT:
544            BTIF_TRACE_EVENT("BTA_GATTC_ACL_EVT: status = %d", p_data->status);
545            /* Ignore for now */
546            break;
547
548        case BTA_GATTC_CANCEL_OPEN_EVT:
549            break;
550
551        case BTIF_GATT_OBSERVE_EVT:
552        {
553            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
554            uint8_t remote_name_len;
555            uint8_t *p_eir_remote_name=NULL;
556            bt_device_type_t dev_type;
557            bt_property_t properties;
558
559            p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
560                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
561
562            if(p_eir_remote_name == NULL)
563            {
564                p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
565                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
566            }
567
568            if ((p_btif_cb->addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name))
569            {
570               if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
571               {
572                  static const char* exclude_filter[] =
573                        {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
574
575                  btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
576                  btif_gattc_update_properties(p_btif_cb);
577                  btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
578                  BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
579               }
580
581            }
582
583            if (( p_btif_cb->device_type == BT_DEVICE_TYPE_DUMO)&&
584               (p_btif_cb->flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
585               (p_btif_cb->flag & BTA_BLE_DMT_HOST_SPT))
586             {
587                btif_storage_set_dmt_support_type (&(p_btif_cb->bd_addr), TRUE);
588             }
589
590             dev_type =  p_btif_cb->device_type;
591             BTIF_STORAGE_FILL_PROPERTY(&properties,
592                        BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
593             btif_storage_set_remote_device_property(&(p_btif_cb->bd_addr), &properties);
594
595            HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb,
596                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value);
597            break;
598        }
599
600        case BTIF_GATTC_RSSI_EVT:
601        {
602            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
603            HAL_CBACK(bt_gatt_callbacks, client->read_remote_rssi_cb, p_btif_cb->client_if,
604                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->status);
605            break;
606        }
607
608        case BTA_GATTC_LISTEN_EVT:
609        {
610            HAL_CBACK(bt_gatt_callbacks, client->listen_cb
611                , p_data->reg_oper.status
612                , p_data->reg_oper.client_if
613            );
614            break;
615        }
616
617        case BTA_GATTC_CFG_MTU_EVT:
618        {
619            HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb, p_data->cfg_mtu.conn_id
620                , p_data->cfg_mtu.status , p_data->cfg_mtu.mtu);
621            break;
622        }
623
624        case BTA_GATTC_MULT_ADV_ENB_EVT:
625        {
626            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
627            btif_multi_adv_add_instid_map(p_btif_cb->client_if,
628                p_btif_cb->inst_id,false);
629            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_enable_cb
630                    , p_btif_cb->client_if
631                    , p_btif_cb->status
632                );
633            break;
634        }
635
636        case BTA_GATTC_MULT_ADV_UPD_EVT:
637        {
638            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
639            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_update_cb
640                , p_btif_cb->client_if
641                , p_btif_cb->status
642            );
643            break;
644        }
645
646        case BTA_GATTC_MULT_ADV_DATA_EVT:
647         {
648            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
649            btif_gattc_cleanup_inst_cb(p_btif_cb->inst_id);
650            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_data_cb
651                , p_btif_cb->client_if
652                , p_btif_cb->status
653            );
654            break;
655        }
656
657        case BTA_GATTC_MULT_ADV_DIS_EVT:
658        {
659            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
660            btif_gattc_clear_clientif(p_btif_cb->client_if);
661            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_disable_cb
662                , p_btif_cb->client_if
663                , p_btif_cb->status
664            );
665            break;
666        }
667
668        case BTA_GATTC_ADV_DATA_EVT:
669        {
670            btif_gattc_cleanup_inst_cb(STD_ADV_INSTID);
671            /* No HAL callback available */
672            break;
673        }
674
675        case BTA_GATTC_CONGEST_EVT:
676            HAL_CBACK(bt_gatt_callbacks, client->congestion_cb
677                , p_data->congest.conn_id
678                , p_data->congest.congested
679            );
680            break;
681
682        case BTA_GATTC_BTH_SCAN_CFG_EVT:
683        {
684            btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
685            HAL_CBACK(bt_gatt_callbacks, client->batchscan_cfg_storage_cb
686                , p_data->client_if
687                , p_data->status
688            );
689            break;
690        }
691
692        case BTA_GATTC_BTH_SCAN_ENB_EVT:
693        {
694            btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
695            HAL_CBACK(bt_gatt_callbacks, client->batchscan_enb_disable_cb
696                    , ENABLE_BATCH_SCAN
697                    , p_data->client_if
698                    , p_data->status);
699            break;
700        }
701
702        case BTA_GATTC_BTH_SCAN_DIS_EVT:
703        {
704            btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
705            HAL_CBACK(bt_gatt_callbacks, client->batchscan_enb_disable_cb
706                    , DISABLE_BATCH_SCAN
707                    , p_data->client_if
708                    , p_data->status);
709            break;
710        }
711
712        case BTA_GATTC_BTH_SCAN_THR_EVT:
713        {
714            btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
715            HAL_CBACK(bt_gatt_callbacks, client->batchscan_threshold_cb
716                    , p_data->client_if);
717            break;
718        }
719
720        case BTA_GATTC_BTH_SCAN_RD_EVT:
721        {
722            btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
723            uint8_t *p_rep_data = NULL;
724
725            if(p_data->read_reports.data_len > 0)
726            {
727                p_rep_data = GKI_getbuf(p_data->read_reports.data_len);
728                memcpy(p_rep_data, p_data->read_reports.p_rep_data, p_data->read_reports.data_len);
729            }
730
731            HAL_CBACK(bt_gatt_callbacks, client->batchscan_reports_cb
732                    , p_data->client_if, p_data->status, p_data->read_reports.report_format
733                    , p_data->read_reports.num_records, p_data->read_reports.data_len, p_rep_data);
734            break;
735        }
736
737        case BTA_GATTC_SCAN_FLT_CFG_EVT:
738        {
739            btgatt_adv_filter_cb_t *p_btif_cb = (btgatt_adv_filter_cb_t*) p_param;
740            HAL_CBACK(bt_gatt_callbacks, client->scan_filter_cfg_cb, p_btif_cb->action,
741                      p_btif_cb->client_if, p_btif_cb->status, p_btif_cb->cond_op,
742                      p_btif_cb->avbl_space);
743            break;
744        }
745
746        case BTA_GATTC_SCAN_FLT_PARAM_EVT:
747        {
748            btgatt_adv_filter_cb_t *p_data = (btgatt_adv_filter_cb_t*) p_param;
749            BTIF_TRACE_DEBUG("BTA_GATTC_SCAN_FLT_PARAM_EVT: %d, %d, %d, %d",p_data->client_if,
750                p_data->action, p_data->avbl_space, p_data->status);
751            HAL_CBACK(bt_gatt_callbacks, client->scan_filter_param_cb
752                    , p_data->action, p_data->client_if, p_data->status
753                    , p_data->avbl_space);
754            break;
755        }
756
757        case BTA_GATTC_SCAN_FLT_STATUS_EVT:
758        {
759            btgatt_adv_filter_cb_t *p_data = (btgatt_adv_filter_cb_t*) p_param;
760            BTIF_TRACE_DEBUG("BTA_GATTC_SCAN_FLT_STATUS_EVT: %d, %d, %d",p_data->client_if,
761                p_data->action, p_data->status);
762            HAL_CBACK(bt_gatt_callbacks, client->scan_filter_status_cb
763                    , p_data->action, p_data->client_if, p_data->status);
764            break;
765        }
766
767        case BTA_GATTC_ADV_VSC_EVT:
768        {
769            btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
770            HAL_CBACK(bt_gatt_callbacks, client->track_adv_event_cb
771                    ,p_data->client_if, p_data->filt_index, p_data->addr_type, &p_data->bd_addr
772                    ,p_data->adv_state);
773            break;
774        }
775
776        default:
777            BTIF_TRACE_ERROR("%s: Unhandled event (%d)!", __FUNCTION__, event);
778            break;
779    }
780
781    btapp_gattc_free_req_data(event, p_data);
782}
783
784static void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
785{
786    bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt,
787                    (uint16_t) event, (void*) p_data, sizeof(tBTA_GATTC), btapp_gattc_req_data);
788    ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
789}
790
791static void bta_gattc_multi_adv_cback(tBTA_BLE_MULTI_ADV_EVT event, UINT8 inst_id,
792                                    void *p_ref, tBTA_STATUS call_status)
793{
794    btif_gattc_cb_t btif_cb;
795    tBTA_GATTC_EVT upevt;
796    uint8_t client_if = 0;
797
798    if(NULL == p_ref)
799    {
800        BTIF_TRACE_ERROR("%s Invalid p_ref received",__FUNCTION__);
801        return;
802    }
803
804    client_if = *(UINT8 *) p_ref;
805    BTIF_TRACE_DEBUG("%s -Inst ID %d, Status:%x, client_if:%d",__FUNCTION__,inst_id, call_status,
806                       client_if);
807
808    btif_cb.status = call_status;
809    btif_cb.client_if = client_if;
810    // Store the inst_id obtained from stack layer now
811    btif_cb.inst_id = inst_id;
812
813    switch(event)
814    {
815        case BTA_BLE_MULTI_ADV_ENB_EVT:
816            upevt = BTA_GATTC_MULT_ADV_ENB_EVT;
817            break;
818
819        case BTA_BLE_MULTI_ADV_DISABLE_EVT:
820            upevt = BTA_GATTC_MULT_ADV_DIS_EVT;
821            break;
822
823        case BTA_BLE_MULTI_ADV_PARAM_EVT:
824            upevt = BTA_GATTC_MULT_ADV_UPD_EVT;
825            break;
826
827        case BTA_BLE_MULTI_ADV_DATA_EVT:
828            upevt = BTA_GATTC_MULT_ADV_DATA_EVT;
829            break;
830
831        default:
832            return;
833    }
834
835    bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt, (uint16_t) upevt,
836                        (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
837    ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
838}
839
840static void bta_gattc_set_adv_data_cback(tBTA_STATUS call_status)
841{
842    UNUSED(call_status);
843    btif_gattc_cb_t btif_cb;
844    btif_cb.status = call_status;
845    btif_cb.action = 0;
846    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_ADV_DATA_EVT,
847                          (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
848}
849
850static void bta_batch_scan_setup_cb (tBTA_BLE_BATCH_SCAN_EVT evt,
851                                            tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
852{
853    UINT8 upevt = 0;
854    btgatt_batch_track_cb_t btif_scan_track_cb;
855
856    btif_scan_track_cb.status = status;
857    btif_scan_track_cb.client_if = ref_value;
858    BTIF_TRACE_DEBUG("bta_batch_scan_setup_cb-Status:%x, client_if:%d, evt=%d",
859            status, ref_value, evt);
860
861    switch(evt)
862    {
863        case BTA_BLE_BATCH_SCAN_ENB_EVT:
864        {
865           upevt = BTA_GATTC_BTH_SCAN_ENB_EVT;
866           break;
867        }
868
869        case BTA_BLE_BATCH_SCAN_DIS_EVT:
870        {
871           upevt = BTA_GATTC_BTH_SCAN_DIS_EVT;
872           break;
873        }
874
875        case BTA_BLE_BATCH_SCAN_CFG_STRG_EVT:
876        {
877           upevt = BTA_GATTC_BTH_SCAN_CFG_EVT;
878           break;
879        }
880
881        case BTA_BLE_BATCH_SCAN_DATA_EVT:
882        {
883           upevt = BTA_GATTC_BTH_SCAN_RD_EVT;
884           break;
885        }
886
887        case BTA_BLE_BATCH_SCAN_THRES_EVT:
888        {
889           upevt = BTA_GATTC_BTH_SCAN_THR_EVT;
890           break;
891        }
892
893        default:
894            return;
895    }
896
897    btif_transfer_context(btif_gattc_upstreams_evt, upevt,(char*) &btif_scan_track_cb,
898                          sizeof(btgatt_batch_track_cb_t), NULL);
899
900}
901
902static void bta_batch_scan_threshold_cb(tBTA_DM_BLE_REF_VALUE ref_value)
903{
904    btgatt_batch_track_cb_t btif_scan_track_cb;
905    btif_scan_track_cb.status = 0;
906    btif_scan_track_cb.client_if = ref_value;
907
908    BTIF_TRACE_DEBUG("%s - client_if:%d",__FUNCTION__, ref_value);
909
910    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_BTH_SCAN_THR_EVT,
911                          (char*) &btif_scan_track_cb, sizeof(btif_gattc_cb_t), NULL);
912}
913
914static void bta_batch_scan_reports_cb(tBTA_DM_BLE_REF_VALUE ref_value, UINT8 report_format,
915                                            UINT8 num_records, UINT16 data_len,
916                                            UINT8* p_rep_data, tBTA_STATUS status)
917{
918    btgatt_batch_track_cb_t btif_scan_track_cb;
919    BTIF_TRACE_DEBUG("%s - client_if:%d, %d, %d, %d",__FUNCTION__, ref_value, status, num_records,
920                                    data_len);
921
922    btif_scan_track_cb.status = status;
923
924    btif_scan_track_cb.client_if = ref_value;
925    btif_scan_track_cb.read_reports.report_format = report_format;
926    btif_scan_track_cb.read_reports.data_len = data_len;
927    btif_scan_track_cb.read_reports.num_records = num_records;
928
929    if(data_len > 0)
930    {
931        btif_scan_track_cb.read_reports.p_rep_data = GKI_getbuf(data_len);
932        memcpy(btif_scan_track_cb.read_reports.p_rep_data, p_rep_data, data_len);
933    }
934
935    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_BTH_SCAN_RD_EVT,
936        (char*) &btif_scan_track_cb, sizeof(btgatt_batch_track_cb_t), NULL);
937
938    if(data_len > 0)
939        GKI_freebuf(btif_scan_track_cb.read_reports.p_rep_data);
940}
941
942static void bta_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
943{
944    btif_gattc_cb_t btif_cb;
945    uint8_t len;
946
947    switch (event)
948    {
949        case BTA_DM_INQ_RES_EVT:
950        {
951            bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr);
952            btif_cb.device_type = p_data->inq_res.device_type;
953            btif_cb.rssi = p_data->inq_res.rssi;
954            btif_cb.addr_type = p_data->inq_res.ble_addr_type;
955            btif_cb.flag = p_data->inq_res.flag;
956            if (p_data->inq_res.p_eir)
957            {
958                memcpy(btif_cb.value, p_data->inq_res.p_eir, 62);
959                if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
960                                      &len))
961                {
962                    p_data->inq_res.remt_name_not_required  = TRUE;
963                }
964            }
965        }
966        break;
967
968        case BTA_DM_INQ_CMPL_EVT:
969        {
970            BTIF_TRACE_DEBUG("%s  BLE observe complete. Num Resp %d",
971                              __FUNCTION__,p_data->inq_cmpl.num_resps);
972            return;
973        }
974
975        default:
976        BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
977        return;
978    }
979    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT,
980                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
981}
982
983static void bta_track_adv_event_cb(int filt_index, tBLE_ADDR_TYPE addr_type, BD_ADDR bda,
984                                        int adv_state, tBTA_DM_BLE_REF_VALUE ref_value)
985{
986    btgatt_batch_track_cb_t btif_scan_track_cb;
987    BTIF_TRACE_DEBUG("%s :%d, %d, %d, %d",
988        __FUNCTION__,filt_index, addr_type, adv_state, ref_value);
989    btif_scan_track_cb.filt_index = filt_index;
990    btif_scan_track_cb.addr_type = addr_type;
991    memcpy(btif_scan_track_cb.bd_addr.address, bda, sizeof(BD_ADDR));
992    btif_scan_track_cb.client_if = ref_value;
993    btif_scan_track_cb.adv_state = adv_state;
994    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_ADV_VSC_EVT,
995                          (char*) &btif_scan_track_cb, sizeof(btgatt_batch_track_cb_t), NULL);
996}
997
998static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result)
999{
1000    btif_gattc_cb_t btif_cb;
1001
1002    bdcpy(btif_cb.bd_addr.address, p_result->rem_bda);
1003    btif_cb.rssi = p_result->rssi;
1004    btif_cb.status = p_result->status;
1005    btif_cb.client_if = rssi_request_client_if;
1006    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT,
1007                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1008}
1009
1010static void bta_scan_filt_cfg_cb(tBTA_DM_BLE_PF_ACTION action, tBTA_DM_BLE_SCAN_COND_OP cfg_op,
1011                                tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, tBTA_STATUS status,
1012                                tBTA_DM_BLE_REF_VALUE ref_value)
1013{
1014    btgatt_adv_filter_cb_t btif_cb;
1015    btif_cb.status = status;
1016    btif_cb.action = action;
1017    btif_cb.cond_op = cfg_op;
1018    btif_cb.avbl_space = avbl_space;
1019    btif_cb.client_if = ref_value;
1020    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_CFG_EVT,
1021                          (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
1022}
1023
1024static void bta_scan_filt_param_setup_cb(UINT8 action_type,
1025                                        tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
1026                                        tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
1027{
1028    btgatt_adv_filter_cb_t btif_cb;
1029
1030    btif_cb.status = status;
1031    btif_cb.action = action_type;
1032    btif_cb.client_if = ref_value;
1033    btif_cb.avbl_space = avbl_space;
1034    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_PARAM_EVT,
1035                          (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
1036}
1037
1038static void bta_scan_filt_status_cb(UINT8 action, tBTA_STATUS status,
1039                                    tBTA_DM_BLE_REF_VALUE ref_value)
1040{
1041    btgatt_adv_filter_cb_t btif_cb;
1042
1043    btif_cb.status = status;
1044    btif_cb.action = action;
1045    btif_cb.client_if = ref_value;
1046    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_STATUS_EVT,
1047                          (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
1048}
1049
1050static void btgattc_handle_event(uint16_t event, char* p_param)
1051{
1052    tBTA_GATT_STATUS           status;
1053    tBT_UUID                   uuid;
1054    tBTA_GATT_SRVC_ID          srvc_id;
1055    tGATT_CHAR_PROP            out_char_prop;
1056    tBTA_GATTC_CHAR_ID         in_char_id;
1057    tBTA_GATTC_CHAR_ID         out_char_id;
1058    tBTA_GATTC_CHAR_DESCR_ID   in_char_descr_id;
1059    tBTA_GATTC_CHAR_DESCR_ID   out_char_descr_id;
1060    tBTA_GATTC_INCL_SVC_ID     in_incl_svc_id;
1061    tBTA_GATTC_INCL_SVC_ID     out_incl_svc_id;
1062    tBTA_GATT_UNFMT            descr_val;
1063
1064    btif_gattc_cb_t* p_cb = (btif_gattc_cb_t*) p_param;
1065    if (!p_cb) return;
1066
1067    BTIF_TRACE_EVENT("%s: Event %d", __FUNCTION__, event);
1068
1069    switch (event)
1070    {
1071        case BTIF_GATTC_REGISTER_APP:
1072            btif_to_bta_uuid(&uuid, &p_cb->uuid);
1073            btif_gattc_init_multi_adv_cb();
1074            BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
1075            break;
1076
1077        case BTIF_GATTC_UNREGISTER_APP:
1078            btif_gattc_destroy_multi_adv_cb();
1079            BTA_GATTC_AppDeregister(p_cb->client_if);
1080            break;
1081
1082        case BTIF_GATTC_SCAN_START:
1083            btif_gattc_init_dev_cb();
1084            BTA_DmBleObserve(TRUE, 0, bta_scan_results_cb);
1085            break;
1086
1087        case BTIF_GATTC_SCAN_STOP:
1088            BTA_DmBleObserve(FALSE, 0, 0);
1089            break;
1090
1091        case BTIF_GATTC_OPEN:
1092        {
1093            // Ensure device is in inquiry database
1094            int addr_type = 0;
1095            int device_type = 0;
1096            tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
1097
1098            if (btif_get_device_type(p_cb->bd_addr.address, &addr_type, &device_type) == TRUE
1099                  && device_type != BT_DEVICE_TYPE_BREDR)
1100                BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type);
1101
1102            // Mark background connections
1103            if (!p_cb->is_direct)
1104                BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
1105
1106            switch(device_type)
1107            {
1108                case BT_DEVICE_TYPE_BREDR:
1109                    transport = BTA_GATT_TRANSPORT_BR_EDR;
1110                    break;
1111
1112                case BT_DEVICE_TYPE_BLE:
1113                    transport = BTA_GATT_TRANSPORT_LE;
1114                    break;
1115
1116                case BT_DEVICE_TYPE_DUMO:
1117                    if ((p_cb->transport == GATT_TRANSPORT_LE) &&
1118                        (btif_storage_is_dmt_supported_device(&(p_cb->bd_addr)) == TRUE))
1119                        transport = BTA_GATT_TRANSPORT_LE;
1120                    else
1121                        transport = BTA_GATT_TRANSPORT_BR_EDR;
1122                    break;
1123            }
1124
1125            // Connect!
1126            BTIF_TRACE_DEBUG ("BTA_GATTC_Open Transport  = %d, dev type = %d",
1127                                transport, device_type);
1128            BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct, transport);
1129            break;
1130        }
1131
1132        case BTIF_GATTC_CLOSE:
1133            // Disconnect established connections
1134            if (p_cb->conn_id != 0)
1135                BTA_GATTC_Close(p_cb->conn_id);
1136            else
1137                BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE);
1138
1139            // Cancel pending background connections (remove from whitelist)
1140            BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE);
1141            break;
1142
1143        case BTIF_GATTC_SEARCH_SERVICE:
1144        {
1145            if (p_cb->search_all)
1146            {
1147                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL);
1148            } else {
1149                btif_to_bta_uuid(&uuid, &p_cb->uuid);
1150                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid);
1151            }
1152            break;
1153        }
1154
1155        case BTIF_GATTC_GET_FIRST_CHAR:
1156        {
1157            btgatt_gatt_id_t char_id;
1158            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
1159            status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL,
1160                                            &out_char_id, &out_char_prop);
1161
1162            if (status == 0)
1163                bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
1164
1165            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
1166                p_cb->conn_id, status, &p_cb->srvc_id,
1167                &char_id, out_char_prop);
1168            break;
1169        }
1170
1171        case BTIF_GATTC_GET_NEXT_CHAR:
1172        {
1173            btgatt_gatt_id_t char_id;
1174            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1175            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1176
1177            status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL,
1178                                            &out_char_id, &out_char_prop);
1179
1180            if (status == 0)
1181                bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
1182
1183            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
1184                p_cb->conn_id, status, &p_cb->srvc_id,
1185                &char_id, out_char_prop);
1186            break;
1187        }
1188
1189        case BTIF_GATTC_GET_FIRST_CHAR_DESCR:
1190        {
1191            btgatt_gatt_id_t descr_id;
1192            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1193            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1194
1195            status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL,
1196                                                    &out_char_descr_id);
1197
1198            if (status == 0)
1199                bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
1200
1201            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
1202                p_cb->conn_id, status, &p_cb->srvc_id,
1203                &p_cb->char_id, &descr_id);
1204            break;
1205        }
1206
1207        case BTIF_GATTC_GET_NEXT_CHAR_DESCR:
1208        {
1209            btgatt_gatt_id_t descr_id;
1210            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
1211            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
1212            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
1213
1214            status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id
1215                                        , NULL, &out_char_descr_id);
1216
1217            if (status == 0)
1218                bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
1219
1220            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
1221                p_cb->conn_id, status, &p_cb->srvc_id,
1222                &p_cb->char_id, &descr_id);
1223            break;
1224        }
1225
1226        case BTIF_GATTC_GET_FIRST_INCL_SERVICE:
1227        {
1228            btgatt_srvc_id_t incl_srvc_id;
1229            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
1230
1231            status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id,
1232                        &srvc_id, NULL, &out_incl_svc_id);
1233
1234            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
1235
1236            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
1237                p_cb->conn_id, status, &p_cb->srvc_id,
1238                &incl_srvc_id);
1239            break;
1240        }
1241
1242        case BTIF_GATTC_GET_NEXT_INCL_SERVICE:
1243        {
1244            btgatt_srvc_id_t incl_srvc_id;
1245            btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id);
1246            btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id);
1247
1248            status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id,
1249                        &in_incl_svc_id, NULL, &out_incl_svc_id);
1250
1251            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
1252
1253            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
1254                p_cb->conn_id, status, &p_cb->srvc_id,
1255                &incl_srvc_id);
1256            break;
1257        }
1258
1259        case BTIF_GATTC_READ_CHAR:
1260            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1261            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1262
1263            BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req);
1264            break;
1265
1266        case BTIF_GATTC_READ_CHAR_DESCR:
1267            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
1268            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
1269            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
1270
1271            BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req);
1272            break;
1273
1274        case BTIF_GATTC_WRITE_CHAR:
1275            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1276            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1277
1278            BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id,
1279                                     p_cb->write_type,
1280                                     p_cb->len,
1281                                     p_cb->value,
1282                                     p_cb->auth_req);
1283            break;
1284
1285        case BTIF_GATTC_WRITE_CHAR_DESCR:
1286            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
1287            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
1288            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
1289
1290            descr_val.len = p_cb->len;
1291            descr_val.p_value = p_cb->value;
1292
1293            BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id,
1294                                     p_cb->write_type, &descr_val,
1295                                     p_cb->auth_req);
1296            break;
1297
1298        case BTIF_GATTC_EXECUTE_WRITE:
1299            BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action);
1300            break;
1301
1302        case BTIF_GATTC_REG_FOR_NOTIFICATION:
1303            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1304            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1305
1306            status = BTA_GATTC_RegisterForNotifications(p_cb->client_if,
1307                                    p_cb->bd_addr.address, &in_char_id);
1308
1309            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
1310                p_cb->conn_id, 1, status, &p_cb->srvc_id,
1311                &p_cb->char_id);
1312            break;
1313
1314        case BTIF_GATTC_DEREG_FOR_NOTIFICATION:
1315            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1316            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1317
1318            status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if,
1319                                        p_cb->bd_addr.address, &in_char_id);
1320
1321            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
1322                p_cb->conn_id, 0, status, &p_cb->srvc_id,
1323                &p_cb->char_id);
1324            break;
1325
1326        case BTIF_GATTC_REFRESH:
1327            BTA_GATTC_Refresh(p_cb->bd_addr.address);
1328            break;
1329
1330        case BTIF_GATTC_READ_RSSI:
1331            rssi_request_client_if = p_cb->client_if;
1332            BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
1333            break;
1334
1335        case BTIF_GATTC_SCAN_FILTER_PARAM_SETUP:
1336        {
1337            btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
1338            if(1 == p_adv_filt_cb->adv_filt_param.dely_mode)
1339               BTA_DmBleTrackAdvertiser(p_adv_filt_cb->client_if, bta_track_adv_event_cb);
1340            BTA_DmBleScanFilterSetup(p_adv_filt_cb->action, p_adv_filt_cb->filt_index,
1341                &p_adv_filt_cb->adv_filt_param, NULL, bta_scan_filt_param_setup_cb,
1342                p_adv_filt_cb->client_if);
1343            break;
1344        }
1345
1346        case BTIF_GATTC_SCAN_FILTER_CONFIG:
1347        {
1348            btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
1349            tBTA_DM_BLE_PF_COND_PARAM cond;
1350            memset(&cond, 0, sizeof(cond));
1351
1352            switch (p_adv_filt_cb->filt_type)
1353            {
1354                case BTA_DM_BLE_PF_ADDR_FILTER: // 0
1355                    bdcpy(cond.target_addr.bda, p_adv_filt_cb->bd_addr.address);
1356                    cond.target_addr.type = p_adv_filt_cb->addr_type;
1357                    BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
1358                                              p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
1359                                              &cond, bta_scan_filt_cfg_cb,
1360                                              p_adv_filt_cb->client_if);
1361                    break;
1362
1363                case BTA_DM_BLE_PF_SRVC_DATA: // 1
1364                    BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
1365                                            p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
1366                                            NULL, bta_scan_filt_cfg_cb, p_adv_filt_cb->client_if);
1367                    break;
1368
1369                case BTA_DM_BLE_PF_SRVC_UUID: // 2
1370                {
1371                    tBTA_DM_BLE_PF_COND_MASK uuid_mask;
1372
1373                    cond.srvc_uuid.p_target_addr = NULL;
1374                    cond.srvc_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
1375                    btif_to_bta_uuid(&cond.srvc_uuid.uuid, &p_adv_filt_cb->uuid);
1376
1377                    cond.srvc_uuid.p_uuid_mask = NULL;
1378                    if (p_adv_filt_cb->has_mask)
1379                    {
1380                        btif_to_bta_uuid_mask(&uuid_mask, &p_adv_filt_cb->uuid_mask);
1381                        cond.srvc_uuid.p_uuid_mask = &uuid_mask;
1382                    }
1383                    BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
1384                                              p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
1385                                              &cond, bta_scan_filt_cfg_cb,
1386                                              p_adv_filt_cb->client_if);
1387                    break;
1388                }
1389
1390                case BTA_DM_BLE_PF_SRVC_SOL_UUID: // 3
1391                {
1392                    cond.solicitate_uuid.p_target_addr = NULL;
1393                    cond.solicitate_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
1394                    btif_to_bta_uuid(&cond.solicitate_uuid.uuid, &p_adv_filt_cb->uuid);
1395                    BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
1396                                              p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
1397                                              &cond, bta_scan_filt_cfg_cb,
1398                                              p_adv_filt_cb->client_if);
1399                    break;
1400                }
1401
1402                case BTA_DM_BLE_PF_LOCAL_NAME: // 4
1403                {
1404                    cond.local_name.data_len = p_adv_filt_cb->value_len;
1405                    cond.local_name.p_data = p_adv_filt_cb->value;
1406                    BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
1407                                              p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
1408                                              &cond, bta_scan_filt_cfg_cb,
1409                                              p_adv_filt_cb->client_if);
1410                    break;
1411                }
1412
1413                case BTA_DM_BLE_PF_MANU_DATA: // 5
1414                {
1415                    cond.manu_data.company_id = p_adv_filt_cb->conn_id;
1416                    cond.manu_data.company_id_mask = p_adv_filt_cb->company_id_mask;
1417                    cond.manu_data.data_len = p_adv_filt_cb->value_len;
1418                    cond.manu_data.p_pattern = p_adv_filt_cb->value;
1419                    cond.manu_data.p_pattern_mask = p_adv_filt_cb->value_mask;
1420                    BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
1421                                              p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
1422                                              &cond, bta_scan_filt_cfg_cb,
1423                                              p_adv_filt_cb->client_if);
1424                    break;
1425                }
1426
1427                case BTA_DM_BLE_PF_SRVC_DATA_PATTERN: //6
1428                {
1429                    cond.srvc_data.data_len = p_adv_filt_cb->value_len;
1430                    cond.srvc_data.p_pattern = p_adv_filt_cb->value;
1431                    cond.srvc_data.p_pattern_mask = p_adv_filt_cb->value_mask;
1432                    BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
1433                                                p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
1434                                                &cond, bta_scan_filt_cfg_cb,
1435                                                p_adv_filt_cb->client_if);
1436                   break;
1437                }
1438
1439                default:
1440                    BTIF_TRACE_ERROR("%s: Unknown filter type (%d)!", __FUNCTION__, p_cb->action);
1441                    break;
1442            }
1443            break;
1444        }
1445
1446        case BTIF_GATTC_SCAN_FILTER_CLEAR:
1447        {
1448            btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
1449            BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_CLEAR, BTA_DM_BLE_PF_TYPE_ALL,
1450                                        p_adv_filt_cb->filt_index, NULL, bta_scan_filt_cfg_cb,
1451                                        p_adv_filt_cb->client_if);
1452            break;
1453        }
1454
1455        case BTIF_GATTC_SCAN_FILTER_ENABLE:
1456        {
1457            btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
1458            BTA_DmEnableScanFilter(p_adv_filt_cb->action, bta_scan_filt_status_cb,
1459                                   p_adv_filt_cb->client_if);
1460            break;
1461        }
1462
1463        case BTIF_GATTC_LISTEN:
1464#if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && (BLE_PERIPHERAL_MODE_SUPPORT == TRUE))
1465            BTA_GATTC_Listen(p_cb->client_if, p_cb->start, NULL);
1466#else
1467            BTA_GATTC_Broadcast(p_cb->client_if, p_cb->start);
1468#endif
1469            break;
1470
1471        case BTIF_GATTC_SET_ADV_DATA:
1472        {
1473            btif_adv_data_t *p_adv_data = (btif_adv_data_t*) p_param;
1474            int cbindex = CLNT_IF_IDX;
1475            if(cbindex >= 0 && NULL != p_adv_data)
1476            {
1477                btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1478                if(!btif_gattc_copy_datacb(cbindex, p_adv_data, false))
1479                    return;
1480
1481                if (!p_adv_data->set_scan_rsp)
1482                {
1483                    BTA_DmBleSetAdvConfig(p_multi_adv_data_cb->inst_cb[cbindex].mask,
1484                        &p_multi_adv_data_cb->inst_cb[cbindex].data, bta_gattc_set_adv_data_cback);
1485                }
1486                else
1487                {
1488                    BTA_DmBleSetScanRsp(p_multi_adv_data_cb->inst_cb[cbindex].mask,
1489                        &p_multi_adv_data_cb->inst_cb[cbindex].data, bta_gattc_set_adv_data_cback);
1490                }
1491                break;
1492            }
1493        }
1494
1495        case BTIF_GATTC_ADV_INSTANCE_ENABLE:
1496        {
1497            btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
1498            int arrindex = btif_multi_adv_add_instid_map(p_inst_cb->client_if,INVALID_ADV_INST,
1499                                                        true);
1500            int cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
1501            if(cbindex >= 0 && arrindex >= 0)
1502            {
1503                btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1504                memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param,
1505                       &p_inst_cb->param, sizeof(tBTA_BLE_ADV_PARAMS));
1506
1507                BTA_BleEnableAdvInstance(&(p_multi_adv_data_cb->inst_cb[cbindex].param),
1508                    bta_gattc_multi_adv_cback,
1509                    &(p_multi_adv_data_cb->clntif_map[arrindex][CLNT_IF_IDX]));
1510            }
1511            else
1512                BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_ENABLE_ADV",__FUNCTION__);
1513            break;
1514        }
1515
1516        case BTIF_GATTC_ADV_INSTANCE_UPDATE:
1517        {
1518            btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
1519            int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
1520            int cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
1521            if(inst_id >= 0 && cbindex >= 0 && NULL != p_inst_cb)
1522            {
1523                btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1524                memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param, &p_inst_cb->param,
1525                        sizeof(tBTA_BLE_ADV_PARAMS));
1526                BTA_BleUpdateAdvInstParam((UINT8)inst_id,
1527                    &(p_multi_adv_data_cb->inst_cb[cbindex].param));
1528            }
1529            else
1530                BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_UPDATE_ADV", __FUNCTION__);
1531            break;
1532        }
1533
1534        case BTIF_GATTC_ADV_INSTANCE_SET_DATA:
1535        {
1536            btif_adv_data_t *p_adv_data = (btif_adv_data_t*) p_param;
1537            int cbindex = btif_gattc_obtain_idx_for_datacb(p_adv_data->client_if, CLNT_IF_IDX);
1538            int inst_id = btif_multi_adv_instid_for_clientif(p_adv_data->client_if);
1539            if(inst_id < 0 || cbindex < 0)
1540            {
1541               BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_SETADV_INST_DATA", __FUNCTION__);
1542               return;
1543            }
1544
1545            if(!btif_gattc_copy_datacb(cbindex, p_adv_data, true))
1546                return;
1547
1548            btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1549            BTA_BleCfgAdvInstData((UINT8)inst_id, p_multi_adv_data_cb->inst_cb[cbindex].is_scan_rsp,
1550                      p_multi_adv_data_cb->inst_cb[cbindex].mask,
1551                      &p_multi_adv_data_cb->inst_cb[cbindex].data);
1552            break;
1553        }
1554
1555        case BTIF_GATTC_ADV_INSTANCE_DISABLE:
1556        {
1557            btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
1558            int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
1559            if(inst_id >=0)
1560                BTA_BleDisableAdvInstance((UINT8)inst_id);
1561            else
1562                BTIF_TRACE_ERROR("%s invalid instance ID in BTIF_GATTC_DISABLE_ADV",__FUNCTION__);
1563            break;
1564        }
1565
1566        case BTIF_GATTC_CONFIGURE_MTU:
1567            BTA_GATTC_ConfigureMTU(p_cb->conn_id, p_cb->len);
1568            break;
1569
1570        case BTIF_GATTC_CONN_PARAM_UPDT:
1571        {
1572            btif_conn_param_cb_t *p_conn_param_cb = (btif_conn_param_cb_t*) p_param;
1573            if (BTA_DmGetConnectionState(p_conn_param_cb->bd_addr.address))
1574            {
1575                BTA_DmBleUpdateConnectionParams(p_conn_param_cb->bd_addr.address,
1576                               p_conn_param_cb->min_interval, p_conn_param_cb->max_interval,
1577                               p_conn_param_cb->latency, p_conn_param_cb->timeout);
1578            } else {
1579                BTA_DmSetBlePrefConnParams(p_conn_param_cb->bd_addr.address,
1580                               p_conn_param_cb->min_interval, p_conn_param_cb->max_interval,
1581                               p_conn_param_cb->latency, p_conn_param_cb->timeout);
1582            }
1583            break;
1584        }
1585
1586        case BTIF_GATTC_SET_SCAN_PARAMS:
1587            BTM_BleSetScanParams(p_cb->scan_interval, p_cb->scan_window, BTM_BLE_SCAN_MODE_ACTI);
1588            break;
1589
1590        case BTIF_GATTC_CONFIG_STORAGE_PARAMS:
1591        {
1592            btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
1593            BTA_DmBleSetStorageParams(p_scan_track_cb->batch_scan_full_max,
1594               p_scan_track_cb->batch_scan_trunc_max, p_scan_track_cb->batch_scan_notify_threshold,
1595               bta_batch_scan_setup_cb, bta_batch_scan_threshold_cb, bta_batch_scan_reports_cb,
1596               (tBTA_DM_BLE_REF_VALUE) p_scan_track_cb->client_if);
1597            break;
1598        }
1599
1600        case BTIF_GATTC_ENABLE_BATCH_SCAN:
1601        {
1602            btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
1603            BTA_DmBleEnableBatchScan(p_scan_track_cb->scan_mode, p_scan_track_cb->scan_interval,
1604               p_scan_track_cb->scan_window, p_scan_track_cb->discard_rule,
1605               p_scan_track_cb->addr_type, p_scan_track_cb->client_if);
1606            break;
1607        }
1608
1609        case BTIF_GATTC_DISABLE_BATCH_SCAN:
1610        {
1611            btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
1612            BTA_DmBleDisableBatchScan(p_scan_track_cb->client_if);
1613            break;
1614        }
1615
1616        case BTIF_GATTC_READ_BATCH_SCAN_REPORTS:
1617        {
1618            btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
1619            BTA_DmBleReadScanReports(p_scan_track_cb->scan_mode, p_scan_track_cb->client_if);
1620            break;
1621        }
1622
1623        default:
1624            BTIF_TRACE_ERROR("%s: Unknown event (%d)!", __FUNCTION__, event);
1625            break;
1626    }
1627}
1628
1629/*******************************************************************************
1630**  Client API Functions
1631********************************************************************************/
1632
1633static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid)
1634{
1635    CHECK_BTGATT_INIT();
1636    btif_gattc_cb_t btif_cb;
1637    memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t));
1638    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP,
1639                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1640}
1641
1642static bt_status_t btif_gattc_unregister_app(int client_if )
1643{
1644    CHECK_BTGATT_INIT();
1645    btif_gattc_cb_t btif_cb;
1646    btif_cb.client_if = (uint8_t) client_if;
1647    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP,
1648                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1649}
1650
1651static bt_status_t btif_gattc_scan( bool start )
1652{
1653    CHECK_BTGATT_INIT();
1654    btif_gattc_cb_t btif_cb;
1655    return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP,
1656                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1657}
1658
1659static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr,
1660                                        bool is_direct,int transport)
1661{
1662    CHECK_BTGATT_INIT();
1663    btif_gattc_cb_t btif_cb;
1664    btif_cb.client_if = (uint8_t) client_if;
1665    btif_cb.is_direct = is_direct ? 1 : 0;
1666    btif_cb.transport = (btgatt_transport_t)transport;
1667    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1668    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN,
1669                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1670}
1671
1672static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id)
1673{
1674    CHECK_BTGATT_INIT();
1675    btif_gattc_cb_t btif_cb;
1676    btif_cb.client_if = (uint8_t) client_if;
1677    btif_cb.conn_id = (uint16_t) conn_id;
1678    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1679    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE,
1680                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1681}
1682
1683static bt_status_t btif_gattc_listen(int client_if, bool start)
1684{
1685    CHECK_BTGATT_INIT();
1686    btif_gattc_cb_t btif_cb;
1687    btif_cb.client_if = (uint8_t) client_if;
1688    btif_cb.start = start ? 1 : 0;
1689    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_LISTEN,
1690                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1691}
1692
1693static bt_status_t btif_gattc_set_adv_data(int client_if, bool set_scan_rsp, bool include_name,
1694                bool include_txpower, int min_interval, int max_interval, int appearance,
1695                uint16_t manufacturer_len, char* manufacturer_data,
1696                uint16_t service_data_len, char* service_data,
1697                uint16_t service_uuid_len, char* service_uuid)
1698{
1699    CHECK_BTGATT_INIT();
1700    bt_status_t status =0;
1701
1702    btif_adv_data_t adv_data;
1703
1704    btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name,
1705        include_txpower, min_interval, max_interval, appearance, manufacturer_len,
1706        manufacturer_data, service_data_len, service_data, service_uuid_len, service_uuid,
1707        &adv_data);
1708
1709    status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_ADV_DATA,
1710                       (char*) &adv_data, sizeof(btif_adv_data_t), NULL);
1711
1712    if (NULL != adv_data.p_service_data)
1713        GKI_freebuf(adv_data.p_service_data);
1714
1715    if (NULL != adv_data.p_service_uuid)
1716        GKI_freebuf(adv_data.p_service_uuid);
1717
1718    if (NULL != adv_data.p_manufacturer_data)
1719        GKI_freebuf(adv_data.p_manufacturer_data);
1720
1721    return status;
1722}
1723
1724static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
1725{
1726    CHECK_BTGATT_INIT();
1727    btif_gattc_cb_t btif_cb;
1728    btif_cb.client_if = (uint8_t) client_if;
1729    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1730    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH,
1731                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1732}
1733
1734static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid )
1735{
1736    CHECK_BTGATT_INIT();
1737    btif_gattc_cb_t btif_cb;
1738    btif_cb.conn_id = (uint16_t) conn_id;
1739    btif_cb.search_all = filter_uuid ? 0 : 1;
1740    if (filter_uuid)
1741        memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t));
1742    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE,
1743                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1744}
1745
1746static bt_status_t btif_gattc_get_characteristic( int conn_id
1747        , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
1748{
1749    CHECK_BTGATT_INIT();
1750    btif_gattc_cb_t btif_cb;
1751    btif_cb.conn_id = (uint16_t) conn_id;
1752    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1753    if (start_char_id)
1754    {
1755        memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_gatt_id_t));
1756        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR,
1757                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1758    }
1759    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR,
1760                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1761}
1762
1763static bt_status_t btif_gattc_get_descriptor( int conn_id
1764        , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id
1765        , btgatt_gatt_id_t *start_descr_id)
1766{
1767    CHECK_BTGATT_INIT();
1768    btif_gattc_cb_t btif_cb;
1769    btif_cb.conn_id = (uint16_t) conn_id;
1770    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1771    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1772    if (start_descr_id)
1773    {
1774        memcpy(&btif_cb.descr_id, start_descr_id, sizeof(btgatt_gatt_id_t));
1775        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR,
1776                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1777    }
1778
1779    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR,
1780                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1781}
1782
1783static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
1784                                                   btgatt_srvc_id_t *start_incl_srvc_id)
1785{
1786    CHECK_BTGATT_INIT();
1787    btif_gattc_cb_t btif_cb;
1788    btif_cb.conn_id = (uint16_t) conn_id;
1789    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1790    if (start_incl_srvc_id)
1791    {
1792        memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t));
1793        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE,
1794                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1795    }
1796    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE,
1797                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1798}
1799
1800static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id,
1801                                        btgatt_gatt_id_t* char_id, int auth_req )
1802{
1803    CHECK_BTGATT_INIT();
1804    btif_gattc_cb_t btif_cb;
1805    btif_cb.conn_id = (uint16_t) conn_id;
1806    btif_cb.auth_req = (uint8_t) auth_req;
1807    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1808    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1809    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR,
1810                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1811}
1812
1813static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
1814                                              btgatt_gatt_id_t* char_id,
1815                                              btgatt_gatt_id_t* descr_id,
1816                                              int auth_req )
1817{
1818    CHECK_BTGATT_INIT();
1819    btif_gattc_cb_t btif_cb;
1820    btif_cb.conn_id = (uint16_t) conn_id;
1821    btif_cb.auth_req = (uint8_t) auth_req;
1822    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1823    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1824    memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
1825    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR,
1826                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1827}
1828
1829static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id,
1830                                         btgatt_gatt_id_t* char_id, int write_type,
1831                                         int len, int auth_req, char* p_value)
1832{
1833    CHECK_BTGATT_INIT();
1834    btif_gattc_cb_t btif_cb;
1835    btif_cb.conn_id = (uint16_t) conn_id;
1836    btif_cb.auth_req = (uint8_t) auth_req;
1837    btif_cb.write_type = (uint8_t) write_type;
1838    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
1839    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1840    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1841    memcpy(btif_cb.value, p_value, btif_cb.len);
1842    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR,
1843                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1844}
1845
1846static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
1847                                               btgatt_gatt_id_t* char_id,
1848                                               btgatt_gatt_id_t* descr_id,
1849                                               int write_type, int len, int auth_req,
1850                                               char* p_value)
1851{
1852    CHECK_BTGATT_INIT();
1853    btif_gattc_cb_t btif_cb;
1854    btif_cb.conn_id = (uint16_t) conn_id;
1855    btif_cb.auth_req = (uint8_t) auth_req;
1856    btif_cb.write_type = (uint8_t) write_type;
1857    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
1858    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1859    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1860    memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
1861    memcpy(btif_cb.value, p_value, btif_cb.len);
1862    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR,
1863                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1864}
1865
1866static bt_status_t btif_gattc_execute_write(int conn_id, int execute)
1867{
1868    CHECK_BTGATT_INIT();
1869    btif_gattc_cb_t btif_cb;
1870    btif_cb.conn_id = (uint16_t) conn_id;
1871    btif_cb.action = (uint8_t) execute;
1872    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE,
1873                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1874}
1875
1876static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
1877                                                   btgatt_srvc_id_t* srvc_id,
1878                                                   btgatt_gatt_id_t* char_id)
1879{
1880    CHECK_BTGATT_INIT();
1881    btif_gattc_cb_t btif_cb;
1882    btif_cb.client_if = (uint8_t) client_if;
1883    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1884    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1885    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1886    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION,
1887                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1888}
1889
1890static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
1891                                                     btgatt_srvc_id_t* srvc_id,
1892                                                     btgatt_gatt_id_t* char_id)
1893{
1894    CHECK_BTGATT_INIT();
1895    btif_gattc_cb_t btif_cb;
1896    btif_cb.client_if = (uint8_t) client_if;
1897    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1898    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1899    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1900    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION,
1901                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1902}
1903
1904static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
1905{
1906    CHECK_BTGATT_INIT();
1907    btif_gattc_cb_t btif_cb;
1908    btif_cb.client_if = (uint8_t) client_if;
1909    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1910    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI,
1911                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1912}
1913
1914static bt_status_t btif_gattc_configure_mtu(int conn_id, int mtu)
1915{
1916    CHECK_BTGATT_INIT();
1917    btif_gattc_cb_t btif_cb;
1918    btif_cb.conn_id = conn_id;
1919    btif_cb.len = mtu; // Re-use len field
1920    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONFIGURE_MTU,
1921                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1922}
1923
1924static bt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t *bd_addr, int min_interval,
1925                    int max_interval, int latency, int timeout)
1926{
1927    CHECK_BTGATT_INIT();
1928    btif_conn_param_cb_t btif_cb;
1929    btif_cb.min_interval = min_interval;
1930    btif_cb.max_interval = max_interval;
1931    btif_cb.latency = latency;
1932    btif_cb.timeout = timeout;
1933    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1934    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONN_PARAM_UPDT,
1935                                 (char*) &btif_cb, sizeof(btif_conn_param_cb_t), NULL);
1936}
1937
1938static bt_status_t btif_gattc_scan_filter_param_setup(int client_if, int action,
1939    int filt_index, int feat_seln, int list_logic_type, int filt_logic_type, int rssi_high_thres,
1940    int rssi_low_thres, int dely_mode, int found_timeout, int lost_timeout, int found_timeout_cnt)
1941{
1942    CHECK_BTGATT_INIT();
1943    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1944    btgatt_adv_filter_cb_t btif_filt_cb;
1945    btif_filt_cb.action = action;
1946    btif_filt_cb.client_if = client_if;
1947    btif_filt_cb.filt_index = filt_index;
1948    btif_filt_cb.adv_filt_param.feat_seln = feat_seln;
1949    btif_filt_cb.adv_filt_param.list_logic_type = list_logic_type;
1950    btif_filt_cb.adv_filt_param.filt_logic_type = filt_logic_type;
1951    btif_filt_cb.adv_filt_param.rssi_high_thres = rssi_high_thres;
1952    btif_filt_cb.adv_filt_param.rssi_low_thres = rssi_low_thres;
1953    btif_filt_cb.adv_filt_param.dely_mode = dely_mode;
1954    btif_filt_cb.adv_filt_param.found_timeout = found_timeout;
1955    btif_filt_cb.adv_filt_param.lost_timeout = lost_timeout;
1956    btif_filt_cb.adv_filt_param.found_timeout_cnt = found_timeout_cnt;
1957    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_PARAM_SETUP,
1958                                 (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
1959}
1960
1961static bt_status_t btif_gattc_scan_filter_add_remove(int client_if, int action,
1962                              int filt_type, int filt_index, int company_id,
1963                              int company_id_mask, const bt_uuid_t *p_uuid,
1964                              const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
1965                              char addr_type, int data_len, char* p_data, int mask_len,
1966                              char* p_mask)
1967{
1968    CHECK_BTGATT_INIT();
1969    btgatt_adv_filter_cb_t btif_filt_cb;
1970    BTIF_TRACE_DEBUG("%s, %d, %d", __FUNCTION__, action, filt_type);
1971
1972    /* If data is passed, both mask and data have to be the same length */
1973    if(data_len != mask_len && NULL != p_data && NULL != p_mask)
1974        return BT_STATUS_PARM_INVALID;
1975
1976    btif_filt_cb.client_if = client_if;
1977    btif_filt_cb.action = action;
1978    btif_filt_cb.filt_index = filt_index;
1979    btif_filt_cb.filt_type = filt_type;
1980    btif_filt_cb.conn_id = company_id;
1981    btif_filt_cb.company_id_mask = company_id_mask ? company_id_mask : 0xFFFF;
1982    if(bd_addr)
1983      bdcpy(btif_filt_cb.bd_addr.address, bd_addr->address);
1984
1985    btif_filt_cb.addr_type = addr_type;
1986    btif_filt_cb.has_mask = (p_uuid_mask != NULL);
1987
1988    if (p_uuid != NULL)
1989        memcpy(&btif_filt_cb.uuid, p_uuid, sizeof(bt_uuid_t));
1990    if (p_uuid_mask != NULL)
1991        memcpy(&btif_filt_cb.uuid_mask, p_uuid_mask, sizeof(bt_uuid_t));
1992    if (p_data != NULL && data_len != 0)
1993    {
1994        memcpy(btif_filt_cb.value, p_data, data_len);
1995        btif_filt_cb.value_len = data_len;
1996        memcpy(btif_filt_cb.value_mask, p_mask, mask_len);
1997        btif_filt_cb.value_mask_len = mask_len;
1998    }
1999    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CONFIG,
2000                                 (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
2001}
2002
2003static bt_status_t btif_gattc_scan_filter_clear(int client_if, int filt_index)
2004{
2005    CHECK_BTGATT_INIT();
2006    BTIF_TRACE_DEBUG("%s, %d", __FUNCTION__, filt_index);
2007
2008    btgatt_adv_filter_cb_t btif_filt_cb;
2009    btif_filt_cb.client_if = client_if;
2010    btif_filt_cb.filt_index = filt_index;
2011    btif_filt_cb.action = BTA_DM_BLE_SCAN_COND_CLEAR;
2012    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CONFIG,
2013                                 (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
2014}
2015
2016static bt_status_t btif_gattc_scan_filter_enable(int client_if, bool enable)
2017{
2018    int action = 0;
2019    CHECK_BTGATT_INIT();
2020    BTIF_TRACE_DEBUG("%s, %d", __FUNCTION__, enable);
2021
2022    btgatt_adv_filter_cb_t btif_filt_cb;
2023    btif_filt_cb.client_if = client_if;
2024    if(true == enable)
2025        action = 1;
2026    btif_filt_cb.action = action;
2027    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_ENABLE,
2028                                 (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
2029}
2030
2031static bt_status_t btif_gattc_set_scan_parameters(int scan_interval, int scan_window)
2032{
2033    CHECK_BTGATT_INIT();
2034    btif_gattc_cb_t btif_cb;
2035    btif_cb.scan_interval = scan_interval;
2036    btif_cb.scan_window = scan_window;
2037    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_SCAN_PARAMS,
2038                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
2039}
2040
2041static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr )
2042{
2043    int device_type = 0;
2044    char bd_addr_str[18] = {0};
2045
2046    bd2str(bd_addr, &bd_addr_str);
2047    if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type))
2048        return device_type;
2049    return 0;
2050}
2051
2052static bt_status_t btif_gattc_multi_adv_enable(int client_if, int min_interval, int max_interval,
2053                                            int adv_type, int chnl_map, int tx_power)
2054{
2055    CHECK_BTGATT_INIT();
2056    btgatt_multi_adv_inst_cb adv_cb;
2057    adv_cb.client_if = (uint8_t) client_if;
2058
2059    adv_cb.param.adv_int_min = min_interval;
2060    adv_cb.param.adv_int_max = max_interval;
2061    adv_cb.param.adv_type = adv_type;
2062    adv_cb.param.channel_map = chnl_map;
2063    adv_cb.param.adv_filter_policy = 0;
2064    adv_cb.param.tx_power = tx_power;
2065    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_ENABLE,
2066                             (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
2067}
2068
2069static bt_status_t btif_gattc_multi_adv_update(int client_if, int min_interval, int max_interval,
2070                                            int adv_type, int chnl_map,int tx_power)
2071{
2072    CHECK_BTGATT_INIT();
2073    btgatt_multi_adv_inst_cb adv_cb;
2074    adv_cb.client_if = (uint8_t) client_if;
2075
2076    adv_cb.param.adv_int_min = min_interval;
2077    adv_cb.param.adv_int_max = max_interval;
2078    adv_cb.param.adv_type = adv_type;
2079    adv_cb.param.channel_map = chnl_map;
2080    adv_cb.param.adv_filter_policy = 0;
2081    adv_cb.param.tx_power = tx_power;
2082    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_UPDATE,
2083                         (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
2084}
2085
2086static bt_status_t btif_gattc_multi_adv_setdata(int client_if, bool set_scan_rsp,
2087                                                   bool include_name, bool incl_txpower,
2088                                                   int appearance, uint16_t manufacturer_len,
2089                                                   char* manufacturer_data,
2090                                                   uint16_t service_data_len,
2091                                                   char* service_data, uint16_t service_uuid_len,
2092                                                   char* service_uuid)
2093{
2094    CHECK_BTGATT_INIT();
2095
2096    int min_interval = 0, max_interval = 0;
2097    bt_status_t status =0;
2098
2099    btif_adv_data_t multi_adv_data_inst;
2100
2101    btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name, incl_txpower,
2102        min_interval, max_interval, appearance, manufacturer_len, manufacturer_data,
2103        service_data_len, service_data, service_uuid_len, service_uuid, &multi_adv_data_inst);
2104
2105    status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_SET_DATA,
2106                       (char*) &multi_adv_data_inst, sizeof(btif_adv_data_t), NULL);
2107
2108    if (NULL != multi_adv_data_inst.p_service_data)
2109        GKI_freebuf(multi_adv_data_inst.p_service_data);
2110
2111    if (NULL != multi_adv_data_inst.p_service_uuid)
2112        GKI_freebuf(multi_adv_data_inst.p_service_uuid);
2113
2114    if (NULL != multi_adv_data_inst.p_manufacturer_data)
2115        GKI_freebuf(multi_adv_data_inst.p_manufacturer_data);
2116
2117    return status;
2118}
2119
2120static bt_status_t btif_gattc_multi_adv_disable(int client_if)
2121{
2122    CHECK_BTGATT_INIT();
2123    btgatt_multi_adv_inst_cb adv_cb;
2124    adv_cb.client_if = (uint8_t) client_if;
2125
2126    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_DISABLE,
2127                           (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
2128}
2129
2130static bt_status_t btif_gattc_cfg_storage(int client_if,int batch_scan_full_max,
2131    int batch_scan_trunc_max, int batch_scan_notify_threshold)
2132{
2133    CHECK_BTGATT_INIT();
2134    btgatt_batch_track_cb_t bt_scan_cb;
2135    bt_scan_cb.client_if = (uint8_t) client_if;
2136    bt_scan_cb.batch_scan_full_max = batch_scan_full_max;
2137    bt_scan_cb.batch_scan_trunc_max = batch_scan_trunc_max;
2138    bt_scan_cb.batch_scan_notify_threshold = batch_scan_notify_threshold;
2139    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONFIG_STORAGE_PARAMS,
2140                                 (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
2141}
2142
2143static bt_status_t btif_gattc_enb_batch_scan(int client_if,int scan_mode, int scan_interval,
2144                int scan_window, int addr_type, int discard_rule)
2145{
2146    CHECK_BTGATT_INIT();
2147    btgatt_batch_track_cb_t bt_scan_cb;
2148    bt_scan_cb.client_if = (uint8_t) client_if;
2149    bt_scan_cb.scan_mode = scan_mode;
2150    bt_scan_cb.scan_interval = scan_interval;
2151    bt_scan_cb.scan_window = scan_window;
2152    bt_scan_cb.discard_rule = discard_rule;
2153    bt_scan_cb.addr_type = addr_type;
2154    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ENABLE_BATCH_SCAN,
2155                                 (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
2156}
2157
2158static bt_status_t btif_gattc_dis_batch_scan(int client_if)
2159{
2160    CHECK_BTGATT_INIT();
2161    btgatt_batch_track_cb_t bt_scan_cb;
2162    bt_scan_cb.client_if = (uint8_t) client_if;
2163    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DISABLE_BATCH_SCAN,
2164                                 (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
2165}
2166
2167static bt_status_t btif_gattc_read_batch_scan_reports(int client_if, int scan_mode)
2168{
2169    CHECK_BTGATT_INIT();
2170    btgatt_batch_track_cb_t bt_scan_cb;
2171    bt_scan_cb.client_if = (uint8_t) client_if;
2172    bt_scan_cb.scan_mode = scan_mode;
2173    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_BATCH_SCAN_REPORTS,
2174                                 (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
2175}
2176
2177extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params);
2178
2179static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params)
2180{
2181    return btif_gattc_test_command_impl(command, params);
2182}
2183
2184
2185const btgatt_client_interface_t btgattClientInterface = {
2186    btif_gattc_register_app,
2187    btif_gattc_unregister_app,
2188    btif_gattc_scan,
2189    btif_gattc_open,
2190    btif_gattc_close,
2191    btif_gattc_listen,
2192    btif_gattc_refresh,
2193    btif_gattc_search_service,
2194    btif_gattc_get_included_service,
2195    btif_gattc_get_characteristic,
2196    btif_gattc_get_descriptor,
2197    btif_gattc_read_char,
2198    btif_gattc_write_char,
2199    btif_gattc_read_char_descr,
2200    btif_gattc_write_char_descr,
2201    btif_gattc_execute_write,
2202    btif_gattc_reg_for_notification,
2203    btif_gattc_dereg_for_notification,
2204    btif_gattc_read_remote_rssi,
2205    btif_gattc_scan_filter_param_setup,
2206    btif_gattc_scan_filter_add_remove,
2207    btif_gattc_scan_filter_clear,
2208    btif_gattc_scan_filter_enable,
2209    btif_gattc_get_device_type,
2210    btif_gattc_set_adv_data,
2211    btif_gattc_configure_mtu,
2212    btif_gattc_conn_parameter_update,
2213    btif_gattc_set_scan_parameters,
2214    btif_gattc_multi_adv_enable,
2215    btif_gattc_multi_adv_update,
2216    btif_gattc_multi_adv_setdata,
2217    btif_gattc_multi_adv_disable,
2218    btif_gattc_cfg_storage,
2219    btif_gattc_enb_batch_scan,
2220    btif_gattc_dis_batch_scan,
2221    btif_gattc_read_batch_scan_reports,
2222    btif_gattc_test_command
2223};
2224
2225#endif
2226