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