btif_gatt_client.c revision a6ce7751d84218c193eb90d390aef23217b1737e
1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2013 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 "bta_vendor_api.h"
55#include "vendor_api.h"
56
57/*******************************************************************************
58**  Constants & Macros
59********************************************************************************/
60
61#define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
62    {\
63        ALOGW("%s: BTGATT not initialized", __FUNCTION__);\
64        return BT_STATUS_NOT_READY;\
65    } else {\
66        ALOGD("%s", __FUNCTION__);\
67    }
68
69
70typedef enum {
71    BTIF_GATTC_REGISTER_APP = 1000,
72    BTIF_GATTC_UNREGISTER_APP,
73    BTIF_GATTC_SCAN_START,
74    BTIF_GATTC_SCAN_STOP,
75    BTIF_GATTC_OPEN,
76    BTIF_GATTC_CLOSE,
77    BTIF_GATTC_SEARCH_SERVICE,
78    BTIF_GATTC_GET_FIRST_CHAR,
79    BTIF_GATTC_GET_NEXT_CHAR,
80    BTIF_GATTC_GET_FIRST_CHAR_DESCR,
81    BTIF_GATTC_GET_NEXT_CHAR_DESCR,
82    BTIF_GATTC_GET_FIRST_INCL_SERVICE,
83    BTIF_GATTC_GET_NEXT_INCL_SERVICE,
84    BTIF_GATTC_READ_CHAR,
85    BTIF_GATTC_READ_CHAR_DESCR,
86    BTIF_GATTC_WRITE_CHAR,
87    BTIF_GATTC_WRITE_CHAR_DESCR,
88    BTIF_GATTC_EXECUTE_WRITE,
89    BTIF_GATTC_REG_FOR_NOTIFICATION,
90    BTIF_GATTC_DEREG_FOR_NOTIFICATION,
91    BTIF_GATTC_REFRESH,
92    BTIF_GATTC_READ_RSSI,
93    BTIF_GATTC_LISTEN,
94    BTIF_GATTC_SET_ADV_DATA,
95    BTIF_GATTC_CONFIGURE_MTU,
96    BTIF_GATTC_SCAN_FILTER_ENABLE,
97    BTIF_GATTC_SCAN_FILTER_CONFIG,
98    BTIF_GATTC_SCAN_FILTER_CLEAR,
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_event_t;
105
106#define BTIF_GATT_MAX_OBSERVED_DEV 40
107
108#define BTIF_GATT_OBSERVE_EVT   0x1000
109#define BTIF_GATTC_RSSI_EVT     0x1001
110#define BTIF_GATTC_SCAN_FILTER_EVT   0x1003
111
112/*******************************************************************************
113**  Local type definitions
114********************************************************************************/
115
116typedef struct
117{
118    uint8_t     value[BTGATT_MAX_ATTR_LEN];
119    uint8_t     inst_id;
120    bt_bdaddr_t bd_addr;
121    btgatt_srvc_id_t srvc_id;
122    btgatt_srvc_id_t incl_srvc_id;
123    btgatt_gatt_id_t char_id;
124    btgatt_gatt_id_t descr_id;
125    bt_uuid_t   uuid;
126    bt_uuid_t   uuid_mask;
127    uint16_t    conn_id;
128    uint16_t    len;
129    uint16_t    mask;
130    uint16_t    scan_interval;
131    uint16_t    scan_window;
132    uint8_t     client_if;
133    uint8_t     action;
134    uint8_t     is_direct;
135    uint8_t     search_all;
136    uint8_t     auth_req;
137    uint8_t     write_type;
138    uint8_t     status;
139    uint8_t     addr_type;
140    uint8_t     start;
141    uint8_t     has_mask;
142    int8_t      rssi;
143    uint8_t     flag;
144    tBT_DEVICE_TYPE device_type;
145    btgatt_transport_t transport;
146} __attribute__((packed)) btif_gattc_cb_t;
147
148typedef struct
149{
150    bt_bdaddr_t bd_addr;
151    BOOLEAN     in_use;
152}__attribute__((packed)) btif_gattc_dev_t;
153
154typedef struct
155{
156    btif_gattc_dev_t remote_dev[BTIF_GATT_MAX_OBSERVED_DEV];
157    uint8_t            addr_type;
158    uint8_t            next_storage_idx;
159}__attribute__((packed)) btif_gattc_dev_cb_t;
160
161/*******************************************************************************
162**  Static variables
163********************************************************************************/
164
165extern const btgatt_callbacks_t *bt_gatt_callbacks;
166static btif_gattc_dev_cb_t  btif_gattc_dev_cb;
167static btif_gattc_dev_cb_t  *p_dev_cb = &btif_gattc_dev_cb;
168static uint8_t rssi_request_client_if;
169
170/*******************************************************************************
171**  Static functions
172********************************************************************************/
173
174static void btapp_gattc_req_data(UINT16 event, char *p_dest, char *p_src)
175{
176    tBTA_GATTC *p_dest_data = (tBTA_GATTC*)p_dest;
177    tBTA_GATTC *p_src_data = (tBTA_GATTC*)p_src;
178
179    if (!p_src_data || !p_dest_data)
180       return;
181
182    // Copy basic structure first
183    memcpy(p_dest_data, p_src_data, sizeof(tBTA_GATTC));
184
185    // Allocate buffer for request data if necessary
186    switch (event)
187    {
188        case BTA_GATTC_READ_CHAR_EVT:
189        case BTA_GATTC_READ_DESCR_EVT:
190
191            if (p_src_data->read.p_value != NULL)
192            {
193                p_dest_data->read.p_value = GKI_getbuf(sizeof(tBTA_GATT_READ_VAL));
194
195                if (p_dest_data->read.p_value != NULL)
196                {
197                    memcpy(p_dest_data->read.p_value, p_src_data->read.p_value,
198                        sizeof(tBTA_GATT_READ_VAL));
199
200                    // Allocate buffer for att value if necessary
201                    if (get_uuid16(&p_src_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
202                      && p_src_data->read.p_value->unformat.len > 0
203                      && p_src_data->read.p_value->unformat.p_value != NULL)
204                    {
205                        p_dest_data->read.p_value->unformat.p_value =
206                                       GKI_getbuf(p_src_data->read.p_value->unformat.len);
207                        if (p_dest_data->read.p_value->unformat.p_value != NULL)
208                        {
209                            memcpy(p_dest_data->read.p_value->unformat.p_value,
210                                   p_src_data->read.p_value->unformat.p_value,
211                                   p_src_data->read.p_value->unformat.len);
212                        }
213                    }
214                }
215            }
216            else
217            {
218                BTIF_TRACE_WARNING2("%s :Src read.p_value ptr is NULL for event  0x%x",
219                                    __FUNCTION__, event);
220                p_dest_data->read.p_value = NULL;
221
222            }
223            break;
224
225        default:
226            break;
227    }
228}
229
230static void btapp_gattc_free_req_data(UINT16 event, tBTA_GATTC *p_data)
231{
232    switch (event)
233    {
234        case BTA_GATTC_READ_CHAR_EVT:
235        case BTA_GATTC_READ_DESCR_EVT:
236            if (p_data != NULL && p_data->read.p_value != NULL)
237            {
238                if (get_uuid16 (&p_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
239                  && p_data->read.p_value->unformat.len > 0
240                  && p_data->read.p_value->unformat.p_value != NULL)
241                {
242                    GKI_freebuf(p_data->read.p_value->unformat.p_value);
243                }
244                GKI_freebuf(p_data->read.p_value);
245            }
246            break;
247
248        default:
249            break;
250    }
251}
252
253static void btif_gattc_init_dev_cb(void)
254{
255    memset(p_dev_cb, 0, sizeof(btif_gattc_dev_cb_t));
256}
257static void btif_gattc_add_remote_bdaddr (BD_ADDR p_bda, uint8_t addr_type)
258{
259    BOOLEAN found=FALSE;
260    uint8_t i;
261    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
262    {
263        if (!p_dev_cb->remote_dev[i].in_use )
264        {
265            memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
266            p_dev_cb->addr_type = addr_type;
267            p_dev_cb->remote_dev[i].in_use = TRUE;
268            ALOGD("%s device added idx=%d", __FUNCTION__, i  );
269            break;
270        }
271    }
272
273    if ( i == BTIF_GATT_MAX_OBSERVED_DEV)
274    {
275        i= p_dev_cb->next_storage_idx;
276        memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
277        p_dev_cb->addr_type = addr_type;
278        p_dev_cb->remote_dev[i].in_use = TRUE;
279        ALOGD("%s device overwrite idx=%d", __FUNCTION__, i  );
280        p_dev_cb->next_storage_idx++;
281        if(p_dev_cb->next_storage_idx >= BTIF_GATT_MAX_OBSERVED_DEV)
282               p_dev_cb->next_storage_idx = 0;
283    }
284}
285
286static BOOLEAN btif_gattc_find_bdaddr (BD_ADDR p_bda)
287{
288    uint8_t i;
289    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
290    {
291        if (p_dev_cb->remote_dev[i].in_use &&
292            !memcmp(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN))
293        {
294            return TRUE;
295        }
296    }
297    return FALSE;
298}
299
300static void btif_gattc_update_properties ( btif_gattc_cb_t *p_btif_cb )
301{
302    uint8_t remote_name_len;
303    uint8_t *p_eir_remote_name=NULL;
304    bt_bdname_t bdname;
305
306    p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
307                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
308
309    if(p_eir_remote_name == NULL)
310    {
311        p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
312                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
313    }
314
315    if(p_eir_remote_name)
316    {
317         memcpy(bdname.name, p_eir_remote_name, remote_name_len);
318         bdname.name[remote_name_len]='\0';
319
320        ALOGD("%s BLE device name=%s len=%d dev_type=%d", __FUNCTION__, bdname.name,
321              remote_name_len, p_btif_cb->device_type  );
322        btif_dm_update_ble_remote_properties( p_btif_cb->bd_addr.address,   bdname.name,
323                                               p_btif_cb->device_type);
324    }
325
326    btif_storage_set_remote_addr_type( &p_btif_cb->bd_addr, p_btif_cb->addr_type);
327}
328
329static void btif_gattc_upstreams_evt(uint16_t event, char* p_param)
330{
331    ALOGD("%s: Event %d", __FUNCTION__, event);
332
333    tBTA_GATTC *p_data = (tBTA_GATTC*)p_param;
334    switch (event)
335    {
336        case BTA_GATTC_REG_EVT:
337        {
338            bt_uuid_t app_uuid;
339            bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
340            HAL_CBACK(bt_gatt_callbacks, client->register_client_cb
341                , p_data->reg_oper.status
342                , p_data->reg_oper.client_if
343                , &app_uuid
344            );
345            break;
346        }
347
348        case BTA_GATTC_DEREG_EVT:
349            break;
350
351        case BTA_GATTC_READ_CHAR_EVT:
352        {
353            btgatt_read_params_t data;
354            set_read_value(&data, &p_data->read);
355
356            HAL_CBACK(bt_gatt_callbacks, client->read_characteristic_cb
357                , p_data->read.conn_id, p_data->read.status, &data);
358            break;
359        }
360
361        case BTA_GATTC_WRITE_CHAR_EVT:
362        case BTA_GATTC_PREP_WRITE_EVT:
363        {
364            btgatt_write_params_t data;
365            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
366            bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
367
368            HAL_CBACK(bt_gatt_callbacks, client->write_characteristic_cb
369                , p_data->write.conn_id, p_data->write.status, &data
370            );
371            break;
372        }
373
374        case BTA_GATTC_EXEC_EVT:
375        {
376            HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb
377                , p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status
378            );
379            break;
380        }
381
382        case BTA_GATTC_SEARCH_CMPL_EVT:
383        {
384            HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb
385                , p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
386            break;
387        }
388
389        case BTA_GATTC_SEARCH_RES_EVT:
390        {
391            btgatt_srvc_id_t data;
392            bta_to_btif_srvc_id(&data, &(p_data->srvc_res.service_uuid));
393            HAL_CBACK(bt_gatt_callbacks, client->search_result_cb
394                , p_data->srvc_res.conn_id, &data);
395            break;
396        }
397
398        case BTA_GATTC_READ_DESCR_EVT:
399        {
400            btgatt_read_params_t data;
401            set_read_value(&data, &p_data->read);
402
403            HAL_CBACK(bt_gatt_callbacks, client->read_descriptor_cb
404                , p_data->read.conn_id, p_data->read.status, &data);
405            break;
406        }
407
408        case BTA_GATTC_WRITE_DESCR_EVT:
409        {
410            btgatt_write_params_t data;
411            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
412            bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
413            bta_to_btif_gatt_id(&data.descr_id, &p_data->write.descr_type);
414
415            HAL_CBACK(bt_gatt_callbacks, client->write_descriptor_cb
416                , p_data->write.conn_id, p_data->write.status, &data);
417            break;
418        }
419
420        case BTA_GATTC_NOTIF_EVT:
421        {
422            btgatt_notify_params_t data;
423
424            bdcpy(data.bda.address, p_data->notify.bda);
425
426            bta_to_btif_srvc_id(&data.srvc_id, &p_data->notify.char_id.srvc_id);
427            bta_to_btif_gatt_id(&data.char_id, &p_data->notify.char_id.char_id);
428            memcpy(data.value, p_data->notify.value, p_data->notify.len);
429
430            data.is_notify = p_data->notify.is_notify;
431            data.len = p_data->notify.len;
432
433            HAL_CBACK(bt_gatt_callbacks, client->notify_cb
434                , p_data->notify.conn_id, &data);
435
436            if (p_data->notify.is_notify == FALSE)
437            {
438                BTA_GATTC_SendIndConfirm(p_data->notify.conn_id,
439                                         &p_data->notify.char_id);
440            }
441            break;
442        }
443
444        case BTA_GATTC_OPEN_EVT:
445        {
446            bt_bdaddr_t bda;
447            bdcpy(bda.address, p_data->open.remote_bda);
448
449            HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id
450                , p_data->open.status, p_data->open.client_if, &bda);
451
452            if (GATT_DEF_BLE_MTU_SIZE != p_data->open.mtu && p_data->open.mtu)
453            {
454                HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb, p_data->open.conn_id
455                    , p_data->open.status , p_data->open.mtu);
456            }
457
458            if (p_data->open.status == BTA_GATT_OK)
459                btif_gatt_check_encrypted_link(p_data->open.remote_bda);
460            break;
461        }
462
463        case BTA_GATTC_CLOSE_EVT:
464        {
465            bt_bdaddr_t bda;
466            bdcpy(bda.address, p_data->close.remote_bda);
467            HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id
468                , p_data->status, p_data->close.client_if, &bda);
469            break;
470        }
471
472        case BTA_GATTC_ACL_EVT:
473            ALOGD("BTA_GATTC_ACL_EVT: status = %d", p_data->status);
474            /* Ignore for now */
475            break;
476
477        case BTA_GATTC_CANCEL_OPEN_EVT:
478            break;
479
480        case BTIF_GATT_OBSERVE_EVT:
481        {
482            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
483            uint8_t remote_name_len;
484            uint8_t *p_eir_remote_name=NULL;
485            bt_device_type_t dev_type;
486            bt_property_t properties;
487
488            p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
489                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
490
491            if(p_eir_remote_name == NULL)
492            {
493                p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
494                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
495            }
496
497            if ((p_btif_cb->addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name))
498            {
499               if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
500               {
501                  static const char* exclude_filter[] =
502                        {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
503
504                  btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
505                  btif_gattc_update_properties(p_btif_cb);
506                  btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
507                  BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
508               }
509
510            }
511
512            if (( p_btif_cb->device_type == BT_DEVICE_TYPE_DUMO)&&
513               (p_btif_cb->flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
514               (p_btif_cb->flag & BTA_BLE_DMT_HOST_SPT))
515             {
516                btif_storage_set_dmt_support_type (&(p_btif_cb->bd_addr), TRUE);
517             }
518
519             dev_type =  p_btif_cb->device_type;
520             BTIF_STORAGE_FILL_PROPERTY(&properties,
521                        BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
522             btif_storage_set_remote_device_property(&(p_btif_cb->bd_addr), &properties);
523
524            HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb,
525                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value);
526            break;
527        }
528
529        case BTIF_GATTC_RSSI_EVT:
530        {
531            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
532            HAL_CBACK(bt_gatt_callbacks, client->read_remote_rssi_cb, p_btif_cb->client_if,
533                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->status);
534            break;
535        }
536
537        case BTA_GATTC_LISTEN_EVT:
538        {
539            HAL_CBACK(bt_gatt_callbacks, client->listen_cb
540                , p_data->reg_oper.status
541                , p_data->reg_oper.client_if
542            );
543            break;
544        }
545
546        case BTA_GATTC_CFG_MTU_EVT:
547        {
548            HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb, p_data->cfg_mtu.conn_id
549                , p_data->cfg_mtu.status , p_data->cfg_mtu.mtu);
550            break;
551        }
552        case BTIF_GATTC_SCAN_FILTER_EVT:
553        {
554            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
555            HAL_CBACK(bt_gatt_callbacks, client->scan_filter_cb, p_btif_cb->action,
556                      p_btif_cb->status);
557            break;
558        }
559
560        case BTA_GATTC_MULT_ADV_ENB_EVT:
561        {
562            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
563            btif_multi_adv_add_instid_map(p_btif_cb->client_if,
564                p_btif_cb->inst_id,false);
565            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_enable_cb
566                    , p_btif_cb->client_if
567                    , p_btif_cb->status
568                );
569            break;
570        }
571
572        case BTA_GATTC_MULT_ADV_UPD_EVT:
573        {
574            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
575            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_update_cb
576                , p_btif_cb->client_if
577                , p_btif_cb->status
578            );
579            break;
580        }
581
582        case BTA_GATTC_MULT_ADV_DATA_EVT:
583         {
584            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
585            btif_gattc_cleanup_inst_cb(p_btif_cb->inst_id);
586            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_data_cb
587                , p_btif_cb->client_if
588                , p_btif_cb->status
589            );
590            break;
591        }
592
593        case BTA_GATTC_MULT_ADV_DIS_EVT:
594        {
595            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
596            btif_gattc_clear_clientif(p_btif_cb->client_if);
597            HAL_CBACK(bt_gatt_callbacks, client->multi_adv_disable_cb
598                , p_btif_cb->client_if
599                , p_btif_cb->status
600            );
601            break;
602        }
603
604        case BTA_GATTC_ADV_DATA_EVT:
605        {
606            btif_gattc_cleanup_inst_cb(STD_ADV_INSTID);
607            /* No HAL callback available */
608            break;
609        }
610
611        default:
612            ALOGE("%s: Unhandled event (%d)!", __FUNCTION__, event);
613            break;
614    }
615
616    btapp_gattc_free_req_data(event, p_data);
617}
618
619static void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
620{
621    bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt,
622                    (uint16_t) event, (void*)p_data, sizeof(tBTA_GATTC), btapp_gattc_req_data);
623    ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
624}
625
626static void bta_gattc_multi_adv_cback(tBTA_BLE_MULTI_ADV_EVT event, UINT8 inst_id,
627                                    void *p_ref, tBTA_STATUS call_status)
628{
629    btif_gattc_cb_t btif_cb;
630    tBTA_GATTC_EVT upevt;
631    uint8_t client_if = 0;
632
633    if(NULL == p_ref)
634    {
635        BTIF_TRACE_ERROR1("%s Invalid p_ref received",__FUNCTION__);
636        return;
637    }
638
639    client_if = *(UINT8 *)p_ref;
640    BTIF_TRACE_DEBUG4("%s -Inst ID %d, Status:%x, client_if:%d",__FUNCTION__,inst_id, call_status,
641                       client_if);
642
643    btif_cb.status = call_status;
644    btif_cb.client_if = client_if;
645    btif_cb.inst_id = inst_id;
646
647    switch(event)
648    {
649        case BTA_BLE_MULTI_ADV_ENB_EVT:
650        {
651            upevt = BTA_GATTC_MULT_ADV_ENB_EVT;
652            break;
653        }
654
655        case BTA_BLE_MULTI_ADV_DISABLE_EVT:
656            upevt = BTA_GATTC_MULT_ADV_DIS_EVT;
657            break;
658
659        case BTA_BLE_MULTI_ADV_PARAM_EVT:
660            upevt = BTA_GATTC_MULT_ADV_UPD_EVT;
661            break;
662
663        case BTA_BLE_MULTI_ADV_DATA_EVT:
664            upevt = BTA_GATTC_MULT_ADV_DATA_EVT;
665            break;
666
667        default:
668            return;
669    }
670
671    bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt, (uint16_t) upevt,
672                        (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
673    ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
674}
675
676static void bta_gattc_set_adv_data_cback(tBTA_STATUS call_status)
677{
678    UNUSED(call_status);
679    btif_gattc_cb_t btif_cb;
680    btif_cb.status = call_status;
681    btif_cb.action = 0;
682    btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_ADV_DATA_EVT,
683                          (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
684}
685
686static void bta_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
687{
688    btif_gattc_cb_t btif_cb;
689    uint8_t len;
690
691    switch (event)
692    {
693        case BTA_DM_INQ_RES_EVT:
694        {
695            bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr);
696            btif_cb.device_type = p_data->inq_res.device_type;
697            btif_cb.rssi = p_data->inq_res.rssi;
698            btif_cb.addr_type = p_data->inq_res.ble_addr_type;
699            btif_cb.flag = p_data->inq_res.flag;
700            if (p_data->inq_res.p_eir)
701            {
702                memcpy(btif_cb.value, p_data->inq_res.p_eir, 62);
703                if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
704                                      &len))
705                {
706                    p_data->inq_res.remt_name_not_required  = TRUE;
707                }
708            }
709        }
710        break;
711
712        case BTA_DM_INQ_CMPL_EVT:
713        {
714            BTIF_TRACE_DEBUG2("%s  BLE observe complete. Num Resp %d",
715                              __FUNCTION__,p_data->inq_cmpl.num_resps);
716            return;
717        }
718
719        default:
720        BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
721        return;
722    }
723    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT,
724                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
725}
726
727static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result)
728{
729    btif_gattc_cb_t btif_cb;
730
731    bdcpy(btif_cb.bd_addr.address, p_result->rem_bda);
732    btif_cb.rssi = p_result->rssi;
733    btif_cb.status = p_result->status;
734    btif_cb.client_if = rssi_request_client_if;
735    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT,
736                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
737}
738
739static void bta_scan_filter_cmpl_cb(tBTA_DM_BLE_PF_EVT event,
740                        tBTA_DM_BLE_PF_COND_TYPE cfg_cond, tBTA_STATUS status)
741{
742    btif_gattc_cb_t btif_cb;
743    btif_cb.status = status;
744    btif_cb.action = event;
745    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_SCAN_FILTER_EVT,
746                          (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
747}
748
749static void btgattc_handle_event(uint16_t event, char* p_param)
750{
751    tBTA_GATT_STATUS           status;
752    tBT_UUID                   uuid;
753    tBTA_GATT_SRVC_ID          srvc_id;
754    tGATT_CHAR_PROP            out_char_prop;
755    tBTA_GATTC_CHAR_ID         in_char_id;
756    tBTA_GATTC_CHAR_ID         out_char_id;
757    tBTA_GATTC_CHAR_DESCR_ID   in_char_descr_id;
758    tBTA_GATTC_CHAR_DESCR_ID   out_char_descr_id;
759    tBTA_GATTC_INCL_SVC_ID     in_incl_svc_id;
760    tBTA_GATTC_INCL_SVC_ID     out_incl_svc_id;
761    tBTA_GATT_UNFMT            descr_val;
762
763    btif_gattc_cb_t* p_cb = NULL;
764    btif_adv_data_t *p_adv_data = NULL;
765    btgatt_multi_adv_inst_cb *p_inst_cb = NULL;
766
767    if(BTIF_GATTC_ADV_INSTANCE_ENABLE == event || BTIF_GATTC_ADV_INSTANCE_DISABLE == event ||
768        BTIF_GATTC_ADV_INSTANCE_UPDATE == event)
769    {
770        p_inst_cb = (btgatt_multi_adv_inst_cb*)p_param;
771    }
772    else
773    {
774        if(BTIF_GATTC_ADV_INSTANCE_SET_DATA == event || BTIF_GATTC_SET_ADV_DATA == event)
775            p_adv_data = (btif_adv_data_t*)p_param;
776        else
777            p_cb = (btif_gattc_cb_t*)p_param;
778    }
779
780    if (!p_cb && !p_adv_data && !p_inst_cb) return;
781
782    ALOGD("%s: Event %d", __FUNCTION__, event);
783
784    switch (event)
785    {
786        case BTIF_GATTC_REGISTER_APP:
787            btif_to_bta_uuid(&uuid, &p_cb->uuid);
788            btif_gattc_init_multi_adv_cb();
789            BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
790            break;
791
792        case BTIF_GATTC_UNREGISTER_APP:
793            btif_gattc_destroy_multi_adv_cb();
794            BTA_GATTC_AppDeregister(p_cb->client_if);
795            break;
796
797        case BTIF_GATTC_SCAN_START:
798            btif_gattc_init_dev_cb();
799            BTA_DmBleObserve(TRUE, 0, bta_scan_results_cb);
800            break;
801
802        case BTIF_GATTC_SCAN_STOP:
803            BTA_DmBleObserve(FALSE, 0, 0);
804            break;
805
806        case BTIF_GATTC_OPEN:
807        {
808            // Ensure device is in inquiry database
809            int addr_type = 0;
810            int device_type = 0;
811            tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
812
813            if (btif_get_device_type(p_cb->bd_addr.address, &addr_type, &device_type) == TRUE
814                  && device_type != BT_DEVICE_TYPE_BREDR)
815                BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type);
816
817            // Mark background connections
818            if (!p_cb->is_direct)
819                BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
820
821            switch(device_type)
822            {
823                case BT_DEVICE_TYPE_BREDR:
824                    transport = BTA_GATT_TRANSPORT_BR_EDR;
825                    break;
826
827                case BT_DEVICE_TYPE_BLE:
828                    transport = BTA_GATT_TRANSPORT_LE;
829                    break;
830
831                case BT_DEVICE_TYPE_DUMO:
832                    if ((p_cb->transport == GATT_TRANSPORT_LE) &&
833                        (btif_storage_is_dmt_supported_device(&(p_cb->bd_addr)) == TRUE))
834                        transport = BTA_GATT_TRANSPORT_LE;
835                    else
836                        transport = BTA_GATT_TRANSPORT_BR_EDR;
837                    break;
838            }
839
840            // Connect!
841            BTIF_TRACE_DEBUG2 ("BTA_GATTC_Open Transport  = %d, dev type = %d",
842                                transport, device_type);
843            BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct, transport);
844            break;
845        }
846
847        case BTIF_GATTC_CLOSE:
848            // Disconnect established connections
849            if (p_cb->conn_id != 0)
850                BTA_GATTC_Close(p_cb->conn_id);
851            else
852                BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE);
853
854            // Cancel pending background connections (remove from whitelist)
855            BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE);
856            break;
857
858        case BTIF_GATTC_SEARCH_SERVICE:
859        {
860            if (p_cb->search_all)
861            {
862                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL);
863            } else {
864                btif_to_bta_uuid(&uuid, &p_cb->uuid);
865                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid);
866            }
867            break;
868        }
869
870        case BTIF_GATTC_GET_FIRST_CHAR:
871        {
872            btgatt_gatt_id_t char_id;
873            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
874            status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL,
875                                            &out_char_id, &out_char_prop);
876
877            if (status == 0)
878                bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
879
880            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
881                p_cb->conn_id, status, &p_cb->srvc_id,
882                &char_id, out_char_prop);
883            break;
884        }
885
886        case BTIF_GATTC_GET_NEXT_CHAR:
887        {
888            btgatt_gatt_id_t char_id;
889            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
890            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
891
892            status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL,
893                                            &out_char_id, &out_char_prop);
894
895            if (status == 0)
896                bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
897
898            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
899                p_cb->conn_id, status, &p_cb->srvc_id,
900                &char_id, out_char_prop);
901            break;
902        }
903
904        case BTIF_GATTC_GET_FIRST_CHAR_DESCR:
905        {
906            btgatt_gatt_id_t descr_id;
907            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
908            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
909
910            status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL,
911                                                    &out_char_descr_id);
912
913            if (status == 0)
914                bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
915
916            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
917                p_cb->conn_id, status, &p_cb->srvc_id,
918                &p_cb->char_id, &descr_id);
919            break;
920        }
921
922        case BTIF_GATTC_GET_NEXT_CHAR_DESCR:
923        {
924            btgatt_gatt_id_t descr_id;
925            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
926            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
927            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
928
929            status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id
930                                        , NULL, &out_char_descr_id);
931
932            if (status == 0)
933                bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
934
935            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
936                p_cb->conn_id, status, &p_cb->srvc_id,
937                &p_cb->char_id, &descr_id);
938            break;
939        }
940
941        case BTIF_GATTC_GET_FIRST_INCL_SERVICE:
942        {
943            btgatt_srvc_id_t incl_srvc_id;
944            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
945
946            status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id,
947                        &srvc_id, NULL, &out_incl_svc_id);
948
949            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
950
951            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
952                p_cb->conn_id, status, &p_cb->srvc_id,
953                &incl_srvc_id);
954            break;
955        }
956
957        case BTIF_GATTC_GET_NEXT_INCL_SERVICE:
958        {
959            btgatt_srvc_id_t incl_srvc_id;
960            btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id);
961            btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id);
962
963            status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id,
964                        &in_incl_svc_id, NULL, &out_incl_svc_id);
965
966            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
967
968            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
969                p_cb->conn_id, status, &p_cb->srvc_id,
970                &incl_srvc_id);
971            break;
972        }
973
974        case BTIF_GATTC_READ_CHAR:
975            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
976            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
977
978            BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req);
979            break;
980
981        case BTIF_GATTC_READ_CHAR_DESCR:
982            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
983            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
984            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
985
986            BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req);
987            break;
988
989        case BTIF_GATTC_WRITE_CHAR:
990            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
991            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
992
993            BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id,
994                                     p_cb->write_type,
995                                     p_cb->len,
996                                     p_cb->value,
997                                     p_cb->auth_req);
998            break;
999
1000        case BTIF_GATTC_WRITE_CHAR_DESCR:
1001            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
1002            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
1003            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
1004
1005            descr_val.len = p_cb->len;
1006            descr_val.p_value = p_cb->value;
1007
1008            BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id,
1009                                     p_cb->write_type, &descr_val,
1010                                     p_cb->auth_req);
1011            break;
1012
1013        case BTIF_GATTC_EXECUTE_WRITE:
1014            BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action);
1015            break;
1016
1017        case BTIF_GATTC_REG_FOR_NOTIFICATION:
1018            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1019            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1020
1021            status = BTA_GATTC_RegisterForNotifications(p_cb->client_if,
1022                                    p_cb->bd_addr.address, &in_char_id);
1023
1024            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
1025                p_cb->conn_id, 1, status, &p_cb->srvc_id,
1026                &p_cb->char_id);
1027            break;
1028
1029        case BTIF_GATTC_DEREG_FOR_NOTIFICATION:
1030            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
1031            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
1032
1033            status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if,
1034                                        p_cb->bd_addr.address, &in_char_id);
1035
1036            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
1037                p_cb->conn_id, 0, status, &p_cb->srvc_id,
1038                &p_cb->char_id);
1039            break;
1040
1041        case BTIF_GATTC_REFRESH:
1042            BTA_GATTC_Refresh(p_cb->bd_addr.address);
1043            break;
1044
1045        case BTIF_GATTC_READ_RSSI:
1046            rssi_request_client_if = p_cb->client_if;
1047            BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
1048            break;
1049
1050        case BTIF_GATTC_SCAN_FILTER_ENABLE:
1051            BTA_DmBleEnableFilterCondition(p_cb->action, NULL, bta_scan_filter_cmpl_cb);
1052            break;
1053
1054        case BTIF_GATTC_SCAN_FILTER_CONFIG:
1055        {
1056            tBTA_DM_BLE_PF_COND_PARAM cond;
1057            memset(&cond, 0, sizeof(cond));
1058
1059            switch (p_cb->action)
1060            {
1061                case BTA_DM_BLE_PF_ADDR_FILTER: // 0
1062                    bdcpy(cond.target_addr.bda, p_cb->bd_addr.address);
1063                    cond.target_addr.type = p_cb->addr_type;
1064                    BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_ADD,
1065                                              p_cb->action, &cond,
1066                                              bta_scan_filter_cmpl_cb);
1067                    break;
1068
1069                case BTA_DM_BLE_PF_SRVC_DATA: // 1
1070                    BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_ADD,
1071                                              p_cb->action, NULL,
1072                                              bta_scan_filter_cmpl_cb);
1073                    break;
1074
1075                case BTA_DM_BLE_PF_SRVC_UUID: // 2
1076                {
1077                    tBTA_DM_BLE_PF_COND_MASK uuid_mask;
1078
1079                    cond.srvc_uuid.p_target_addr = NULL;
1080                    cond.srvc_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
1081                    btif_to_bta_uuid(&cond.srvc_uuid.uuid, &p_cb->uuid);
1082
1083                    cond.srvc_uuid.p_uuid_mask = NULL;
1084                    if (p_cb->has_mask)
1085                    {
1086                        btif_to_bta_uuid_mask(&uuid_mask, &p_cb->uuid_mask);
1087                        cond.srvc_uuid.p_uuid_mask = &uuid_mask;
1088                    }
1089                    BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_ADD,
1090                                              p_cb->action, &cond,
1091                                              bta_scan_filter_cmpl_cb);
1092                    break;
1093                }
1094
1095                case BTA_DM_BLE_PF_SRVC_SOL_UUID: // 3
1096                {
1097                    cond.solicitate_uuid.p_target_addr = NULL;
1098                    cond.solicitate_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
1099                    btif_to_bta_uuid(&cond.solicitate_uuid.uuid, &p_cb->uuid);
1100                    BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_ADD,
1101                                              p_cb->action, &cond,
1102                                              bta_scan_filter_cmpl_cb);
1103                    break;
1104                }
1105
1106                case BTA_DM_BLE_PF_LOCAL_NAME: // 4
1107                {
1108                    cond.local_name.data_len = p_cb->len;
1109                    cond.local_name.p_data = p_cb->value;
1110                    BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_ADD,
1111                                              p_cb->action, &cond,
1112                                              bta_scan_filter_cmpl_cb);
1113                    break;
1114                }
1115
1116                case BTA_DM_BLE_PF_MANU_DATA: // 5
1117                {
1118                    cond.manu_data.company_id = p_cb->conn_id;
1119                    cond.manu_data.company_id_mask = p_cb->mask;
1120                    cond.manu_data.data_len = p_cb->len;
1121                    cond.manu_data.p_pattern = p_cb->value;
1122                    cond.manu_data.p_pattern_mask = &p_cb->value[p_cb->len];
1123                    BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_ADD,
1124                                              p_cb->action, &cond,
1125                                              bta_scan_filter_cmpl_cb);
1126                    break;
1127                }
1128
1129                default:
1130                    ALOGE("%s: Unknown filter type (%d)!", __FUNCTION__, p_cb->action);
1131                    break;
1132            }
1133            break;
1134        }
1135
1136        case BTIF_GATTC_SCAN_FILTER_CLEAR:
1137        {
1138            BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_CLEAR, BTA_DM_BLE_PF_TYPE_ALL,
1139                                      NULL, bta_scan_filter_cmpl_cb);
1140            break;
1141        }
1142
1143        case BTIF_GATTC_LISTEN:
1144#if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && (BLE_PERIPHERAL_MODE_SUPPORT == TRUE))
1145            BTA_GATTC_Listen(p_cb->client_if, p_cb->start, NULL);
1146#else
1147            BTA_GATTC_Broadcast(p_cb->client_if, p_cb->start);
1148#endif
1149            break;
1150
1151        case BTIF_GATTC_SET_ADV_DATA:
1152        {
1153            int cbindex = CLNT_IF_IDX;
1154            if(cbindex >= 0 && NULL != p_adv_data)
1155            {
1156                btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1157                if(!btif_gattc_copy_datacb(cbindex, p_adv_data, false))
1158                    return;
1159
1160                if (!p_adv_data->set_scan_rsp)
1161                {
1162                    BTA_DmBleSetAdvConfig(p_multi_adv_data_cb->inst_cb[cbindex].mask,
1163                        &p_multi_adv_data_cb->inst_cb[cbindex].data, bta_gattc_set_adv_data_cback);
1164                }
1165                else
1166                {
1167                    BTA_DmBleSetScanRsp(p_multi_adv_data_cb->inst_cb[cbindex].mask,
1168                        &p_multi_adv_data_cb->inst_cb[cbindex].data, bta_gattc_set_adv_data_cback);
1169                }
1170                break;
1171            }
1172        }
1173
1174        case BTIF_GATTC_ADV_INSTANCE_ENABLE:
1175        {
1176            if(NULL == p_inst_cb)
1177               return;
1178
1179            int arrindex = btif_multi_adv_add_instid_map(p_inst_cb->client_if,INVALID_ADV_INST,
1180                                                        true);
1181            int cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
1182            if(cbindex >= 0 && arrindex >= 0)
1183            {
1184                btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1185                memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param,
1186                       &p_inst_cb->param, sizeof(tBTA_BLE_ADV_PARAMS));
1187
1188                BTA_BleEnableAdvInstance(&(p_multi_adv_data_cb->inst_cb[cbindex].param),
1189                    bta_gattc_multi_adv_cback,
1190                    &(p_multi_adv_data_cb->clntif_map[arrindex][CLNT_IF_IDX]));
1191            }
1192            else
1193                BTIF_TRACE_ERROR1("%s invalid index in BTIF_GATTC_ENABLE_ADV",__FUNCTION__);
1194            break;
1195        }
1196
1197        case BTIF_GATTC_ADV_INSTANCE_UPDATE:
1198        {
1199            if(NULL == p_inst_cb)
1200               return;
1201
1202            int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
1203            int cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
1204            if(inst_id >= 0 && cbindex >= 0 && NULL != p_inst_cb)
1205            {
1206                btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1207                memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param, &p_inst_cb->param,
1208                        sizeof(tBTA_BLE_ADV_PARAMS));
1209                BTA_BleUpdateAdvInstParam((UINT8)inst_id,
1210                    &(p_multi_adv_data_cb->inst_cb[cbindex].param));
1211            }
1212            else
1213                BTIF_TRACE_ERROR1("%s invalid index in BTIF_GATTC_UPDATE_ADV", __FUNCTION__);
1214            break;
1215        }
1216
1217        case BTIF_GATTC_ADV_INSTANCE_SET_DATA:
1218        {
1219            if(NULL == p_adv_data)
1220               return;
1221
1222            int cbindex = btif_gattc_obtain_idx_for_datacb(p_adv_data->client_if, CLNT_IF_IDX);
1223            int inst_id = btif_multi_adv_instid_for_clientif(p_adv_data->client_if);
1224            if(inst_id < 0 || cbindex < 0)
1225            {
1226               BTIF_TRACE_ERROR1("%s invalid index in BTIF_GATTC_SETADV_INST_DATA", __FUNCTION__);
1227               return;
1228            }
1229
1230            if(!btif_gattc_copy_datacb(cbindex, p_adv_data, true))
1231                return;
1232
1233            btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
1234            BTA_BleCfgAdvInstData((UINT8)inst_id, p_multi_adv_data_cb->inst_cb[cbindex].is_scan_rsp,
1235                      p_multi_adv_data_cb->inst_cb[cbindex].mask,
1236                      &p_multi_adv_data_cb->inst_cb[cbindex].data);
1237            break;
1238        }
1239
1240        case BTIF_GATTC_ADV_INSTANCE_DISABLE:
1241        {
1242            if(NULL == p_inst_cb)
1243               return;
1244
1245            int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
1246            if(inst_id >=0)
1247                BTA_BleDisableAdvInstance((UINT8)inst_id);
1248            else
1249                BTIF_TRACE_ERROR1("%s invalid instance ID in BTIF_GATTC_DISABLE_ADV",__FUNCTION__);
1250            break;
1251        }
1252
1253        case BTIF_GATTC_CONFIGURE_MTU:
1254            BTA_GATTC_ConfigureMTU(p_cb->conn_id, p_cb->len);
1255            break;
1256
1257        case BTIF_GATTC_SET_SCAN_PARAMS:
1258            BTM_BleSetScanParams(p_cb->scan_interval, p_cb->scan_window, BTM_BLE_SCAN_MODE_ACTI);
1259            break;
1260
1261        default:
1262            ALOGE("%s: Unknown event (%d)!", __FUNCTION__, event);
1263            break;
1264    }
1265}
1266
1267/*******************************************************************************
1268**  Client API Functions
1269********************************************************************************/
1270
1271static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid)
1272{
1273    CHECK_BTGATT_INIT();
1274    btif_gattc_cb_t btif_cb;
1275    memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t));
1276    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP,
1277                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1278}
1279
1280static bt_status_t btif_gattc_unregister_app(int client_if )
1281{
1282    CHECK_BTGATT_INIT();
1283    btif_gattc_cb_t btif_cb;
1284    btif_cb.client_if = (uint8_t) client_if;
1285    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP,
1286                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1287}
1288
1289static bt_status_t btif_gattc_scan( bool start )
1290{
1291    CHECK_BTGATT_INIT();
1292    btif_gattc_cb_t btif_cb;
1293    return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP,
1294                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1295}
1296
1297static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr,
1298                                        bool is_direct,int transport)
1299{
1300    CHECK_BTGATT_INIT();
1301    btif_gattc_cb_t btif_cb;
1302    btif_cb.client_if = (uint8_t) client_if;
1303    btif_cb.is_direct = is_direct ? 1 : 0;
1304    btif_cb.transport = (btgatt_transport_t)transport;
1305    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1306    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN,
1307                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1308}
1309
1310static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id)
1311{
1312    CHECK_BTGATT_INIT();
1313    btif_gattc_cb_t btif_cb;
1314    btif_cb.client_if = (uint8_t) client_if;
1315    btif_cb.conn_id = (uint16_t) conn_id;
1316    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1317    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE,
1318                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1319}
1320
1321static bt_status_t btif_gattc_listen(int client_if, bool start)
1322{
1323    CHECK_BTGATT_INIT();
1324    btif_gattc_cb_t btif_cb;
1325    btif_cb.client_if = (uint8_t) client_if;
1326    btif_cb.start = start ? 1 : 0;
1327    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_LISTEN,
1328                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1329}
1330
1331static bt_status_t btif_gattc_set_adv_data(int client_if, bool set_scan_rsp, bool include_name,
1332                bool include_txpower, int min_interval, int max_interval, int appearance,
1333                uint16_t manufacturer_len, char* manufacturer_data,
1334                uint16_t service_data_len, char* service_data,
1335                uint16_t service_uuid_len, char* service_uuid)
1336{
1337    CHECK_BTGATT_INIT();
1338    bt_status_t status =0;
1339
1340    btif_adv_data_t adv_data;
1341
1342    btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name,
1343        include_txpower, min_interval, max_interval, appearance, manufacturer_len,
1344        manufacturer_data, service_data_len, service_data, service_uuid_len, service_uuid,
1345        &adv_data);
1346
1347    status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_ADV_DATA,
1348                       (char*) &adv_data, sizeof(btif_adv_data_t), NULL);
1349
1350    if (NULL != adv_data.p_service_data)
1351        GKI_freebuf(adv_data.p_service_data);
1352
1353    if (NULL != adv_data.p_service_uuid)
1354        GKI_freebuf(adv_data.p_service_uuid);
1355
1356    if (NULL != adv_data.p_manufacturer_data)
1357        GKI_freebuf(adv_data.p_manufacturer_data);
1358
1359    return status;
1360}
1361
1362static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
1363{
1364    CHECK_BTGATT_INIT();
1365    btif_gattc_cb_t btif_cb;
1366    btif_cb.client_if = (uint8_t) client_if;
1367    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1368    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH,
1369                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1370}
1371
1372static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid )
1373{
1374    CHECK_BTGATT_INIT();
1375    btif_gattc_cb_t btif_cb;
1376    btif_cb.conn_id = (uint16_t) conn_id;
1377    btif_cb.search_all = filter_uuid ? 0 : 1;
1378    if (filter_uuid)
1379        memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t));
1380    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE,
1381                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1382}
1383
1384static bt_status_t btif_gattc_get_characteristic( int conn_id
1385        , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
1386{
1387    CHECK_BTGATT_INIT();
1388    btif_gattc_cb_t btif_cb;
1389    btif_cb.conn_id = (uint16_t) conn_id;
1390    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1391    if (start_char_id)
1392    {
1393        memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_gatt_id_t));
1394        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR,
1395                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1396    }
1397    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR,
1398                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1399}
1400
1401static bt_status_t btif_gattc_get_descriptor( int conn_id
1402        , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id
1403        , btgatt_gatt_id_t *start_descr_id)
1404{
1405    CHECK_BTGATT_INIT();
1406    btif_gattc_cb_t btif_cb;
1407    btif_cb.conn_id = (uint16_t) conn_id;
1408    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1409    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1410    if (start_descr_id)
1411    {
1412        memcpy(&btif_cb.descr_id, start_descr_id, sizeof(btgatt_gatt_id_t));
1413        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR,
1414                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1415    }
1416
1417    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR,
1418                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1419}
1420
1421static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
1422                                                   btgatt_srvc_id_t *start_incl_srvc_id)
1423{
1424    CHECK_BTGATT_INIT();
1425    btif_gattc_cb_t btif_cb;
1426    btif_cb.conn_id = (uint16_t) conn_id;
1427    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1428    if (start_incl_srvc_id)
1429    {
1430        memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t));
1431        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE,
1432                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1433    }
1434    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE,
1435                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1436}
1437
1438static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id,
1439                                        btgatt_gatt_id_t* char_id, int auth_req )
1440{
1441    CHECK_BTGATT_INIT();
1442    btif_gattc_cb_t btif_cb;
1443    btif_cb.conn_id = (uint16_t) conn_id;
1444    btif_cb.auth_req = (uint8_t) auth_req;
1445    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1446    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1447    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR,
1448                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1449}
1450
1451static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
1452                                              btgatt_gatt_id_t* char_id,
1453                                              btgatt_gatt_id_t* descr_id,
1454                                              int auth_req )
1455{
1456    CHECK_BTGATT_INIT();
1457    btif_gattc_cb_t btif_cb;
1458    btif_cb.conn_id = (uint16_t) conn_id;
1459    btif_cb.auth_req = (uint8_t) auth_req;
1460    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1461    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1462    memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
1463    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR,
1464                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1465}
1466
1467static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id,
1468                                         btgatt_gatt_id_t* char_id, int write_type,
1469                                         int len, int auth_req, char* p_value)
1470{
1471    CHECK_BTGATT_INIT();
1472    btif_gattc_cb_t btif_cb;
1473    btif_cb.conn_id = (uint16_t) conn_id;
1474    btif_cb.auth_req = (uint8_t) auth_req;
1475    btif_cb.write_type = (uint8_t) write_type;
1476    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
1477    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1478    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1479    memcpy(btif_cb.value, p_value, btif_cb.len);
1480    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR,
1481                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1482}
1483
1484static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
1485                                               btgatt_gatt_id_t* char_id,
1486                                               btgatt_gatt_id_t* descr_id,
1487                                               int write_type, int len, int auth_req,
1488                                               char* p_value)
1489{
1490    CHECK_BTGATT_INIT();
1491    btif_gattc_cb_t btif_cb;
1492    btif_cb.conn_id = (uint16_t) conn_id;
1493    btif_cb.auth_req = (uint8_t) auth_req;
1494    btif_cb.write_type = (uint8_t) write_type;
1495    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
1496    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1497    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1498    memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
1499    memcpy(btif_cb.value, p_value, btif_cb.len);
1500    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR,
1501                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1502}
1503
1504static bt_status_t btif_gattc_execute_write(int conn_id, int execute)
1505{
1506    CHECK_BTGATT_INIT();
1507    btif_gattc_cb_t btif_cb;
1508    btif_cb.conn_id = (uint16_t) conn_id;
1509    btif_cb.action = (uint8_t) execute;
1510    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE,
1511                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1512}
1513
1514static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
1515                                                   btgatt_srvc_id_t* srvc_id,
1516                                                   btgatt_gatt_id_t* char_id)
1517{
1518    CHECK_BTGATT_INIT();
1519    btif_gattc_cb_t btif_cb;
1520    btif_cb.client_if = (uint8_t) client_if;
1521    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1522    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1523    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1524    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION,
1525                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1526}
1527
1528static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
1529                                                     btgatt_srvc_id_t* srvc_id,
1530                                                     btgatt_gatt_id_t* char_id)
1531{
1532    CHECK_BTGATT_INIT();
1533    btif_gattc_cb_t btif_cb;
1534    btif_cb.client_if = (uint8_t) client_if;
1535    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1536    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1537    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1538    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION,
1539                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1540}
1541
1542static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
1543{
1544    CHECK_BTGATT_INIT();
1545    btif_gattc_cb_t btif_cb;
1546    btif_cb.client_if = (uint8_t) client_if;
1547    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1548    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI,
1549                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1550}
1551
1552static bt_status_t btif_gattc_configure_mtu(int conn_id, int mtu)
1553{
1554    CHECK_BTGATT_INIT();
1555    btif_gattc_cb_t btif_cb;
1556    btif_cb.conn_id = conn_id;
1557    btif_cb.len = mtu; // Re-use len field
1558    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONFIGURE_MTU,
1559                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1560}
1561
1562static bt_status_t btif_gattc_scan_filter_enable(int enable )
1563{
1564    CHECK_BTGATT_INIT();
1565    btif_gattc_cb_t btif_cb;
1566    btif_cb.action = enable;
1567    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_ENABLE,
1568                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1569}
1570
1571static bt_status_t btif_gattc_scan_filter_add(int type, int company_id, int company_mask,
1572                              int len, const bt_uuid_t *p_uuid, const bt_uuid_t *p_uuid_mask,
1573                              const bt_bdaddr_t *bd_addr, char addr_type, const char* p_value)
1574{
1575    CHECK_BTGATT_INIT();
1576    btif_gattc_cb_t btif_cb;
1577
1578    if (len > (BTGATT_MAX_ATTR_LEN / 2))
1579        len = BTGATT_MAX_ATTR_LEN / 2;
1580
1581    btif_cb.action = type;
1582    btif_cb.len = len;
1583    btif_cb.conn_id = company_id;
1584    btif_cb.mask = company_mask ? company_mask : 0xFFFF;
1585    if(bd_addr)
1586      bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1587    btif_cb.addr_type = addr_type;
1588    btif_cb.has_mask = (p_uuid_mask != NULL);
1589
1590    if (p_uuid != NULL)
1591        memcpy(&btif_cb.uuid, p_uuid, sizeof(bt_uuid_t));
1592    if (p_uuid_mask != NULL)
1593        memcpy(&btif_cb.uuid_mask, p_uuid_mask, sizeof(bt_uuid_t));
1594    if (p_value != NULL && len != 0)
1595        memcpy(btif_cb.value, p_value, len * 2 /* PATTERN CONTAINS MASK */);
1596    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CONFIG,
1597                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1598}
1599
1600static bt_status_t btif_gattc_scan_filter_clear()
1601{
1602    CHECK_BTGATT_INIT();
1603    btif_gattc_cb_t btif_cb;
1604    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CLEAR,
1605                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1606}
1607
1608static bt_status_t btif_gattc_set_scan_parameters(int scan_interval, int scan_window)
1609{
1610    CHECK_BTGATT_INIT();
1611    btif_gattc_cb_t btif_cb;
1612    btif_cb.scan_interval = scan_interval;
1613    btif_cb.scan_window = scan_window;
1614    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_SCAN_PARAMS,
1615                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1616}
1617
1618static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr )
1619{
1620    int device_type = 0;
1621    char bd_addr_str[18] = {0};
1622
1623    bd2str(bd_addr, &bd_addr_str);
1624    if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type))
1625        return device_type;
1626    return 0;
1627}
1628
1629static bt_status_t btif_gattc_multi_adv_enable(int client_if, int min_interval, int max_interval,
1630                                            int adv_type, int chnl_map, int tx_power)
1631{
1632    CHECK_BTGATT_INIT();
1633    btgatt_multi_adv_inst_cb adv_cb;
1634    adv_cb.client_if = (uint8_t) client_if;
1635
1636    adv_cb.param.adv_int_min = min_interval;
1637    adv_cb.param.adv_int_max = max_interval;
1638    adv_cb.param.adv_type = adv_type;
1639    adv_cb.param.channel_map = chnl_map;
1640    adv_cb.param.adv_filter_policy = 0;
1641    adv_cb.param.tx_power = tx_power;
1642    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_ENABLE,
1643                             (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
1644}
1645
1646static bt_status_t btif_gattc_multi_adv_update(int client_if, int min_interval, int max_interval,
1647                                            int adv_type, int chnl_map,int tx_power)
1648{
1649    CHECK_BTGATT_INIT();
1650    btgatt_multi_adv_inst_cb adv_cb;
1651    adv_cb.client_if = (uint8_t) client_if;
1652
1653    adv_cb.param.adv_int_min = min_interval;
1654    adv_cb.param.adv_int_max = max_interval;
1655    adv_cb.param.adv_type = adv_type;
1656    adv_cb.param.channel_map = chnl_map;
1657    adv_cb.param.adv_filter_policy = 0;
1658    adv_cb.param.tx_power = tx_power;
1659    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_UPDATE,
1660                         (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
1661}
1662
1663static bt_status_t btif_gattc_multi_adv_setdata(int client_if, bool set_scan_rsp,
1664                                                   bool include_name, bool incl_txpower,
1665                                                   int appearance, uint16_t manufacturer_len,
1666                                                   char* manufacturer_data,
1667                                                   uint16_t service_data_len,
1668                                                   char* service_data, uint16_t service_uuid_len,
1669                                                   char* service_uuid)
1670{
1671    CHECK_BTGATT_INIT();
1672
1673    int min_interval = 0, max_interval = 0;
1674    bt_status_t status =0;
1675
1676    btif_adv_data_t multi_adv_data_inst;
1677
1678    btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name, incl_txpower,
1679        min_interval, max_interval, appearance, manufacturer_len, manufacturer_data,
1680        service_data_len, service_data, service_uuid_len, service_uuid, &multi_adv_data_inst);
1681
1682    status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_SET_DATA,
1683                       (char*) &multi_adv_data_inst, sizeof(btif_adv_data_t), NULL);
1684
1685    if (NULL != multi_adv_data_inst.p_service_data)
1686        GKI_freebuf(multi_adv_data_inst.p_service_data);
1687
1688    if (NULL != multi_adv_data_inst.p_service_uuid)
1689        GKI_freebuf(multi_adv_data_inst.p_service_uuid);
1690
1691    if (NULL != multi_adv_data_inst.p_manufacturer_data)
1692        GKI_freebuf(multi_adv_data_inst.p_manufacturer_data);
1693
1694    return status;
1695}
1696
1697static bt_status_t btif_gattc_multi_adv_disable(int client_if)
1698{
1699    CHECK_BTGATT_INIT();
1700    btgatt_multi_adv_inst_cb adv_cb;
1701    adv_cb.client_if = (uint8_t) client_if;
1702
1703    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_DISABLE,
1704                           (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
1705}
1706
1707extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params);
1708
1709static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params)
1710{
1711    return btif_gattc_test_command_impl(command, params);
1712}
1713
1714
1715const btgatt_client_interface_t btgattClientInterface = {
1716    btif_gattc_register_app,
1717    btif_gattc_unregister_app,
1718    btif_gattc_scan,
1719    btif_gattc_open,
1720    btif_gattc_close,
1721    btif_gattc_listen,
1722    btif_gattc_refresh,
1723    btif_gattc_search_service,
1724    btif_gattc_get_included_service,
1725    btif_gattc_get_characteristic,
1726    btif_gattc_get_descriptor,
1727    btif_gattc_read_char,
1728    btif_gattc_write_char,
1729    btif_gattc_read_char_descr,
1730    btif_gattc_write_char_descr,
1731    btif_gattc_execute_write,
1732    btif_gattc_reg_for_notification,
1733    btif_gattc_dereg_for_notification,
1734    btif_gattc_read_remote_rssi,
1735    btif_gattc_scan_filter_enable,
1736    btif_gattc_scan_filter_add,
1737    btif_gattc_scan_filter_clear,
1738    btif_gattc_get_device_type,
1739    btif_gattc_set_adv_data,
1740    btif_gattc_configure_mtu,
1741    btif_gattc_set_scan_parameters,
1742    btif_gattc_multi_adv_enable,
1743    btif_gattc_multi_adv_update,
1744    btif_gattc_multi_adv_setdata,
1745    btif_gattc_multi_adv_disable,
1746    btif_gattc_test_command
1747};
1748
1749#endif
1750