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 "gki.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/*******************************************************************************
55**  Constants & Macros
56********************************************************************************/
57
58#define ADV_FLAGS 0x02
59
60#define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
61    {\
62        ALOGW("%s: BTGATT not initialized", __FUNCTION__);\
63        return BT_STATUS_NOT_READY;\
64    } else {\
65        ALOGD("%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_event_t;
95
96#define BTIF_GATT_MAX_OBSERVED_DEV 40
97
98#define BTIF_GATT_OBSERVE_EVT   0x1000
99#define BTIF_GATTC_RSSI_EVT     0x1001
100
101/*******************************************************************************
102**  Local type definitions
103********************************************************************************/
104
105typedef struct
106{
107    tBTA_BLE_AD_MASK mask;
108    tBTA_BLE_ADV_DATA data;
109} btgatt_adv_data;
110
111typedef struct
112{
113    uint8_t     value[BTGATT_MAX_ATTR_LEN];
114    btgatt_adv_data adv_data;
115    bt_bdaddr_t bd_addr;
116    btgatt_srvc_id_t srvc_id;
117    btgatt_srvc_id_t incl_srvc_id;
118    btgatt_gatt_id_t char_id;
119    btgatt_gatt_id_t descr_id;
120    bt_uuid_t   uuid;
121    uint16_t    conn_id;
122    uint16_t    len;
123    uint8_t     client_if;
124    uint8_t     action;
125    uint8_t     is_direct;
126    uint8_t     search_all;
127    uint8_t     auth_req;
128    uint8_t     write_type;
129    uint8_t     status;
130    uint8_t     addr_type;
131    uint8_t     start;
132    int8_t      rssi;
133    tBT_DEVICE_TYPE device_type;
134} __attribute__((packed)) btif_gattc_cb_t;
135
136typedef struct
137{
138    bt_bdaddr_t bd_addr;
139    BOOLEAN     in_use;
140}__attribute__((packed)) btif_gattc_dev_t;
141
142typedef struct
143{
144    btif_gattc_dev_t remote_dev[BTIF_GATT_MAX_OBSERVED_DEV];
145    uint8_t        addr_type;
146    uint8_t        next_storage_idx;
147}__attribute__((packed)) btif_gattc_dev_cb_t;
148
149/*******************************************************************************
150**  Static variables
151********************************************************************************/
152
153extern const btgatt_callbacks_t *bt_gatt_callbacks;
154static btif_gattc_dev_cb_t  btif_gattc_dev_cb;
155static btif_gattc_dev_cb_t  *p_dev_cb = &btif_gattc_dev_cb;
156static uint8_t rssi_request_client_if;
157
158/*******************************************************************************
159**  Static functions
160********************************************************************************/
161
162static void btapp_gattc_req_data(UINT16 event, char *p_dest, char *p_src)
163{
164    tBTA_GATTC *p_dest_data = (tBTA_GATTC*)p_dest;
165    tBTA_GATTC *p_src_data = (tBTA_GATTC*)p_src;
166
167    if (!p_src_data || !p_dest_data)
168       return;
169
170    // Copy basic structure first
171    memcpy(p_dest_data, p_src_data, sizeof(tBTA_GATTC));
172
173    // Allocate buffer for request data if necessary
174    switch (event)
175    {
176        case BTA_GATTC_READ_CHAR_EVT:
177        case BTA_GATTC_READ_DESCR_EVT:
178
179            if (p_src_data->read.p_value != NULL)
180            {
181                p_dest_data->read.p_value = GKI_getbuf(sizeof(tBTA_GATT_READ_VAL));
182
183                if (p_dest_data->read.p_value != NULL)
184                {
185                    memcpy(p_dest_data->read.p_value, p_src_data->read.p_value,
186                        sizeof(tBTA_GATT_READ_VAL));
187
188                    // Allocate buffer for att value if necessary
189                    if (get_uuid16(&p_src_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
190                      && p_src_data->read.p_value->unformat.len > 0
191                      && p_src_data->read.p_value->unformat.p_value != NULL)
192                    {
193                        p_dest_data->read.p_value->unformat.p_value =
194                                       GKI_getbuf(p_src_data->read.p_value->unformat.len);
195                        if (p_dest_data->read.p_value->unformat.p_value != NULL)
196                        {
197                            memcpy(p_dest_data->read.p_value->unformat.p_value,
198                                   p_src_data->read.p_value->unformat.p_value,
199                                   p_src_data->read.p_value->unformat.len);
200                        }
201                    }
202                }
203            }
204            else
205            {
206                BTIF_TRACE_WARNING2("%s :Src read.p_value ptr is NULL for event  0x%x",
207                                    __FUNCTION__, event);
208                p_dest_data->read.p_value = NULL;
209
210            }
211            break;
212
213        default:
214            break;
215    }
216}
217
218static void btapp_gattc_free_req_data(UINT16 event, tBTA_GATTC *p_data)
219{
220    switch (event)
221    {
222        case BTA_GATTC_READ_CHAR_EVT:
223        case BTA_GATTC_READ_DESCR_EVT:
224            if (p_data != NULL && p_data->read.p_value != NULL)
225            {
226                if (get_uuid16 (&p_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
227                  && p_data->read.p_value->unformat.len > 0
228                  && p_data->read.p_value->unformat.p_value != NULL)
229                {
230                    GKI_freebuf(p_data->read.p_value->unformat.p_value);
231                }
232                GKI_freebuf(p_data->read.p_value);
233            }
234            break;
235
236        default:
237            break;
238    }
239}
240
241static void btif_gattc_init_dev_cb(void)
242{
243    memset(p_dev_cb, 0, sizeof(btif_gattc_dev_cb_t));
244}
245
246static void btif_gattc_add_remote_bdaddr (BD_ADDR p_bda, uint8_t addr_type)
247{
248    BOOLEAN found=FALSE;
249    uint8_t i;
250    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
251    {
252        if (!p_dev_cb->remote_dev[i].in_use )
253        {
254            memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
255            p_dev_cb->addr_type = addr_type;
256            p_dev_cb->remote_dev[i].in_use = TRUE;
257            ALOGD("%s device added idx=%d", __FUNCTION__, i  );
258            break;
259        }
260    }
261
262    if ( i == BTIF_GATT_MAX_OBSERVED_DEV)
263    {
264        i= p_dev_cb->next_storage_idx;
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 overwrite idx=%d", __FUNCTION__, i  );
269        p_dev_cb->next_storage_idx++;
270        if(p_dev_cb->next_storage_idx >= BTIF_GATT_MAX_OBSERVED_DEV)
271               p_dev_cb->next_storage_idx = 0;
272    }
273}
274
275static BOOLEAN btif_gattc_find_bdaddr (BD_ADDR p_bda)
276{
277    uint8_t i;
278    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
279    {
280        if (p_dev_cb->remote_dev[i].in_use &&
281            !memcmp(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN))
282        {
283            return TRUE;
284        }
285    }
286    return FALSE;
287}
288
289static void btif_gattc_update_properties ( btif_gattc_cb_t *p_btif_cb )
290{
291    uint8_t remote_name_len;
292    uint8_t *p_eir_remote_name=NULL;
293    bt_bdname_t bdname;
294
295    p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
296                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
297
298    if(p_eir_remote_name == NULL)
299    {
300        p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
301                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
302    }
303
304    if(p_eir_remote_name)
305    {
306         memcpy(bdname.name, p_eir_remote_name, remote_name_len);
307         bdname.name[remote_name_len]='\0';
308
309        ALOGD("%s BLE device name=%s len=%d dev_type=%d", __FUNCTION__, bdname.name,
310              remote_name_len, p_btif_cb->device_type  );
311        btif_dm_update_ble_remote_properties( p_btif_cb->bd_addr.address,   bdname.name,
312                                               p_btif_cb->device_type);
313    }
314
315    btif_storage_set_remote_addr_type( &p_btif_cb->bd_addr, p_btif_cb->addr_type);
316}
317
318static void btif_gattc_upstreams_evt(uint16_t event, char* p_param)
319{
320    ALOGD("%s: Event %d", __FUNCTION__, event);
321
322    tBTA_GATTC *p_data = (tBTA_GATTC*)p_param;
323    switch (event)
324    {
325        case BTA_GATTC_REG_EVT:
326        {
327            bt_uuid_t app_uuid;
328            bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
329            HAL_CBACK(bt_gatt_callbacks, client->register_client_cb
330                , p_data->reg_oper.status
331                , p_data->reg_oper.client_if
332                , &app_uuid
333            );
334            break;
335        }
336
337        case BTA_GATTC_DEREG_EVT:
338            break;
339
340        case BTA_GATTC_READ_CHAR_EVT:
341        {
342            btgatt_read_params_t data;
343            set_read_value(&data, &p_data->read);
344
345            HAL_CBACK(bt_gatt_callbacks, client->read_characteristic_cb
346                , p_data->read.conn_id, p_data->read.status, &data);
347            break;
348        }
349
350        case BTA_GATTC_WRITE_CHAR_EVT:
351        case BTA_GATTC_PREP_WRITE_EVT:
352        {
353            btgatt_write_params_t data;
354            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
355            bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
356
357            HAL_CBACK(bt_gatt_callbacks, client->write_characteristic_cb
358                , p_data->write.conn_id, p_data->write.status, &data
359            );
360            break;
361        }
362
363        case BTA_GATTC_EXEC_EVT:
364        {
365            HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb
366                , p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status
367            );
368            break;
369        }
370
371        case BTA_GATTC_SEARCH_CMPL_EVT:
372        {
373            HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb
374                , p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
375            break;
376        }
377
378        case BTA_GATTC_SEARCH_RES_EVT:
379        {
380            btgatt_srvc_id_t data;
381            bta_to_btif_srvc_id(&data, &(p_data->srvc_res.service_uuid));
382            HAL_CBACK(bt_gatt_callbacks, client->search_result_cb
383                , p_data->srvc_res.conn_id, &data);
384            break;
385        }
386
387        case BTA_GATTC_READ_DESCR_EVT:
388        {
389            btgatt_read_params_t data;
390            set_read_value(&data, &p_data->read);
391
392            HAL_CBACK(bt_gatt_callbacks, client->read_descriptor_cb
393                , p_data->read.conn_id, p_data->read.status, &data);
394            break;
395        }
396
397        case BTA_GATTC_WRITE_DESCR_EVT:
398        {
399            btgatt_write_params_t data;
400            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
401            bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
402            bta_to_btif_gatt_id(&data.descr_id, &p_data->write.descr_type);
403
404            HAL_CBACK(bt_gatt_callbacks, client->write_descriptor_cb
405                , p_data->write.conn_id, p_data->write.status, &data);
406            break;
407        }
408
409        case BTA_GATTC_NOTIF_EVT:
410        {
411            btgatt_notify_params_t data;
412
413            bdcpy(data.bda.address, p_data->notify.bda);
414
415            bta_to_btif_srvc_id(&data.srvc_id, &p_data->notify.char_id.srvc_id);
416            bta_to_btif_gatt_id(&data.char_id, &p_data->notify.char_id.char_id);
417            memcpy(data.value, p_data->notify.value, p_data->notify.len);
418
419            data.is_notify = p_data->notify.is_notify;
420            data.len = p_data->notify.len;
421
422            HAL_CBACK(bt_gatt_callbacks, client->notify_cb
423                , p_data->notify.conn_id, &data);
424
425            if (p_data->notify.is_notify == FALSE)
426            {
427                BTA_GATTC_SendIndConfirm(p_data->notify.conn_id,
428                                         &p_data->notify.char_id);
429            }
430            break;
431        }
432
433        case BTA_GATTC_OPEN_EVT:
434        {
435            bt_bdaddr_t bda;
436            bdcpy(bda.address, p_data->open.remote_bda);
437
438            HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id
439                , p_data->open.status, p_data->open.client_if, &bda);
440
441            if (p_data->open.status == BTA_GATT_OK)
442                btif_gatt_check_encrypted_link(p_data->open.remote_bda);
443            break;
444        }
445
446        case BTA_GATTC_CLOSE_EVT:
447        {
448            bt_bdaddr_t bda;
449            bdcpy(bda.address, p_data->close.remote_bda);
450            HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id
451                , p_data->status, p_data->close.client_if, &bda);
452            break;
453        }
454
455        case BTA_GATTC_ACL_EVT:
456            ALOGD("BTA_GATTC_ACL_EVT: status = %d", p_data->status);
457            /* Ignore for now */
458            break;
459
460        case BTA_GATTC_CANCEL_OPEN_EVT:
461            break;
462
463        case BTIF_GATT_OBSERVE_EVT:
464        {
465            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
466            uint8_t remote_name_len;
467            uint8_t *p_eir_remote_name=NULL;
468
469            p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
470                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
471
472            if(p_eir_remote_name == NULL)
473            {
474                p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
475                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
476            }
477
478            if ((p_btif_cb->addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name))
479            {
480               if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
481               {
482                  static const char* exclude_filter[] =
483                        {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
484
485                  btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
486                  btif_gattc_update_properties(p_btif_cb);
487                  btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
488                  BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
489               }
490
491            }
492            HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb,
493                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value);
494            break;
495        }
496
497        case BTIF_GATTC_RSSI_EVT:
498        {
499            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
500            HAL_CBACK(bt_gatt_callbacks, client->read_remote_rssi_cb, p_btif_cb->client_if,
501                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->status);
502            break;
503        }
504
505        case BTA_GATTC_LISTEN_EVT:
506        {
507            HAL_CBACK(bt_gatt_callbacks, client->listen_cb
508                , p_data->reg_oper.status
509                , p_data->reg_oper.client_if
510            );
511            break;
512        }
513        default:
514            ALOGE("%s: Unhandled event (%d)!", __FUNCTION__, event);
515            break;
516    }
517
518    btapp_gattc_free_req_data(event, p_data);
519}
520
521static void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
522{
523    bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt,
524                    (uint16_t) event, (void*)p_data, sizeof(tBTA_GATTC), btapp_gattc_req_data);
525    ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
526}
527
528static void bta_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
529{
530    btif_gattc_cb_t btif_cb;
531    uint8_t len;
532
533    switch (event)
534    {
535        case BTA_DM_INQ_RES_EVT:
536        {
537            bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr);
538            btif_cb.device_type = p_data->inq_res.device_type;
539            btif_cb.rssi = p_data->inq_res.rssi;
540            btif_cb.addr_type = p_data->inq_res.ble_addr_type;
541            if (p_data->inq_res.p_eir)
542            {
543                memcpy(btif_cb.value, p_data->inq_res.p_eir, 62);
544                if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
545                                      &len))
546                {
547                    p_data->inq_res.remt_name_not_required  = TRUE;
548                }
549            }
550        }
551        break;
552
553        case BTA_DM_INQ_CMPL_EVT:
554        {
555            BTIF_TRACE_DEBUG2("%s  BLE observe complete. Num Resp %d",
556                              __FUNCTION__,p_data->inq_cmpl.num_resps);
557            return;
558        }
559
560        default:
561        BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
562        return;
563    }
564    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT,
565                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
566}
567
568static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result)
569{
570    btif_gattc_cb_t btif_cb;
571
572    bdcpy(btif_cb.bd_addr.address, p_result->rem_bda);
573    btif_cb.rssi = p_result->rssi;
574    btif_cb.status = p_result->status;
575    btif_cb.client_if = rssi_request_client_if;
576    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT,
577                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
578}
579
580
581static void btgattc_handle_event(uint16_t event, char* p_param)
582{
583    tBTA_GATT_STATUS           status;
584    tBT_UUID                   uuid;
585    tBTA_GATT_SRVC_ID          srvc_id;
586    tGATT_CHAR_PROP            out_char_prop;
587    tBTA_GATTC_CHAR_ID         in_char_id;
588    tBTA_GATTC_CHAR_ID         out_char_id;
589    tBTA_GATTC_CHAR_DESCR_ID   in_char_descr_id;
590    tBTA_GATTC_CHAR_DESCR_ID   out_char_descr_id;
591    tBTA_GATTC_INCL_SVC_ID     in_incl_svc_id;
592    tBTA_GATTC_INCL_SVC_ID     out_incl_svc_id;
593    tBTA_GATT_UNFMT            descr_val;
594
595    btif_gattc_cb_t* p_cb = (btif_gattc_cb_t*)p_param;
596    if (!p_cb) return;
597
598    ALOGD("%s: Event %d", __FUNCTION__, event);
599
600    switch (event)
601    {
602        case BTIF_GATTC_REGISTER_APP:
603            btif_to_bta_uuid(&uuid, &p_cb->uuid);
604            BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
605            break;
606
607        case BTIF_GATTC_UNREGISTER_APP:
608            BTA_GATTC_AppDeregister(p_cb->client_if);
609            break;
610
611        case BTIF_GATTC_SCAN_START:
612            btif_gattc_init_dev_cb();
613            BTA_DmBleObserve(TRUE, 0, bta_scan_results_cb);
614            break;
615
616        case BTIF_GATTC_SCAN_STOP:
617            BTA_DmBleObserve(FALSE, 0, 0);
618            break;
619
620        case BTIF_GATTC_OPEN:
621        {
622            // Ensure device is in inquiry database
623            int addr_type = 0;
624            int device_type = 0;
625
626            if (btif_get_device_type(p_cb->bd_addr.address, &addr_type, &device_type) == TRUE
627                  && device_type != BT_DEVICE_TYPE_BREDR)
628                BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type);
629
630            // Mark background connections
631            if (!p_cb->is_direct)
632                BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
633
634            // Connect!
635            BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct);
636            break;
637        }
638
639        case BTIF_GATTC_CLOSE:
640            // Disconnect established connections
641            if (p_cb->conn_id != 0)
642                BTA_GATTC_Close(p_cb->conn_id);
643            else
644                BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE);
645
646            // Cancel pending background connections (remove from whitelist)
647            BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE);
648            break;
649
650        case BTIF_GATTC_SEARCH_SERVICE:
651        {
652            if (p_cb->search_all)
653            {
654                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL);
655            } else {
656                btif_to_bta_uuid(&uuid, &p_cb->uuid);
657                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid);
658            }
659            break;
660        }
661
662        case BTIF_GATTC_GET_FIRST_CHAR:
663        {
664            btgatt_gatt_id_t char_id;
665            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
666            status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL,
667                                            &out_char_id, &out_char_prop);
668
669            if (status == 0)
670                bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
671
672            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
673                p_cb->conn_id, status, &p_cb->srvc_id,
674                &char_id, out_char_prop);
675            break;
676        }
677
678        case BTIF_GATTC_GET_NEXT_CHAR:
679        {
680            btgatt_gatt_id_t char_id;
681            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
682            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
683
684            status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL,
685                                            &out_char_id, &out_char_prop);
686
687            if (status == 0)
688                bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
689
690            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
691                p_cb->conn_id, status, &p_cb->srvc_id,
692                &char_id, out_char_prop);
693            break;
694        }
695
696        case BTIF_GATTC_GET_FIRST_CHAR_DESCR:
697        {
698            btgatt_gatt_id_t descr_id;
699            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
700            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
701
702            status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL,
703                                                    &out_char_descr_id);
704
705            if (status == 0)
706                bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
707
708            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
709                p_cb->conn_id, status, &p_cb->srvc_id,
710                &p_cb->char_id, &descr_id);
711            break;
712        }
713
714        case BTIF_GATTC_GET_NEXT_CHAR_DESCR:
715        {
716            btgatt_gatt_id_t descr_id;
717            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
718            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
719            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
720
721            status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id
722                                        , NULL, &out_char_descr_id);
723
724            if (status == 0)
725                bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
726
727            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
728                p_cb->conn_id, status, &p_cb->srvc_id,
729                &p_cb->char_id, &descr_id);
730            break;
731        }
732
733        case BTIF_GATTC_GET_FIRST_INCL_SERVICE:
734        {
735            btgatt_srvc_id_t incl_srvc_id;
736            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
737
738            status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id,
739                        &srvc_id, NULL, &out_incl_svc_id);
740
741            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
742
743            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
744                p_cb->conn_id, status, &p_cb->srvc_id,
745                &incl_srvc_id);
746            break;
747        }
748
749        case BTIF_GATTC_GET_NEXT_INCL_SERVICE:
750        {
751            btgatt_srvc_id_t incl_srvc_id;
752            btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id);
753            btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id);
754
755            status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id,
756                        &in_incl_svc_id, NULL, &out_incl_svc_id);
757
758            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
759
760            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
761                p_cb->conn_id, status, &p_cb->srvc_id,
762                &incl_srvc_id);
763            break;
764        }
765
766        case BTIF_GATTC_READ_CHAR:
767            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
768            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
769
770            BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req);
771            break;
772
773        case BTIF_GATTC_READ_CHAR_DESCR:
774            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
775            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
776            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
777
778            BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req);
779            break;
780
781        case BTIF_GATTC_WRITE_CHAR:
782            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
783            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
784
785            BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id,
786                                     p_cb->write_type,
787                                     p_cb->len,
788                                     p_cb->value,
789                                     p_cb->auth_req);
790            break;
791
792        case BTIF_GATTC_WRITE_CHAR_DESCR:
793            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
794            btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
795            btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
796
797            descr_val.len = p_cb->len;
798            descr_val.p_value = p_cb->value;
799
800            BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id,
801                                     p_cb->write_type, &descr_val,
802                                     p_cb->auth_req);
803            break;
804
805        case BTIF_GATTC_EXECUTE_WRITE:
806            BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action);
807            break;
808
809        case BTIF_GATTC_REG_FOR_NOTIFICATION:
810            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
811            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
812
813            status = BTA_GATTC_RegisterForNotifications(p_cb->client_if,
814                                    p_cb->bd_addr.address, &in_char_id);
815
816            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
817                p_cb->conn_id, 1, status, &p_cb->srvc_id,
818                &p_cb->char_id);
819            break;
820
821        case BTIF_GATTC_DEREG_FOR_NOTIFICATION:
822            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
823            btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
824
825            status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if,
826                                        p_cb->bd_addr.address, &in_char_id);
827
828            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
829                p_cb->conn_id, 0, status, &p_cb->srvc_id,
830                &p_cb->char_id);
831            break;
832
833        case BTIF_GATTC_REFRESH:
834            BTA_GATTC_Refresh(p_cb->bd_addr.address);
835            break;
836
837        case BTIF_GATTC_READ_RSSI:
838            rssi_request_client_if = p_cb->client_if;
839            BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
840            break;
841
842        case BTIF_GATTC_LISTEN:
843#ifdef BLE_PERIPHERAL_MODE_SUPPORT
844            BTA_GATTC_Listen(p_cb->client_if, p_cb->start, NULL);
845#else
846            BTA_GATTC_Broadcast(p_cb->client_if, p_cb->start);
847#endif
848            break;
849
850        case BTIF_GATTC_SET_ADV_DATA:
851        {
852            if (p_cb->start == 0)
853                BTA_DmBleSetAdvConfig(p_cb->adv_data.mask, &p_cb->adv_data.data);
854            else
855                BTA_DmBleSetScanRsp(p_cb->adv_data.mask, &p_cb->adv_data.data);
856
857            // Cleanup ...
858
859            // ... manufacturer data
860            if (p_cb->adv_data.data.manu.p_val != NULL)
861                GKI_freebuf(p_cb->adv_data.data.manu.p_val);
862
863            // ... service data
864            if (p_cb->adv_data.data.p_proprietary != NULL)
865            {
866                int i = 0;
867                tBTA_BLE_PROP_ELEM *p_elem = p_cb->adv_data.data.p_proprietary->p_elem;
868                while (i++ != p_cb->adv_data.data.p_proprietary->num_elem && p_elem)
869                {
870                    if (p_elem->p_val != NULL)
871                        GKI_freebuf(p_elem->p_val);
872                    ++p_elem;
873                }
874                if (p_cb->adv_data.data.p_proprietary->p_elem != NULL)
875                    GKI_freebuf(p_cb->adv_data.data.p_proprietary->p_elem);
876                GKI_freebuf(p_cb->adv_data.data.p_proprietary);
877            }
878
879            // ... service list
880            if (p_cb->adv_data.data.services.p_uuid != NULL)
881                GKI_freebuf(p_cb->adv_data.data.services.p_uuid);
882
883            break;
884        }
885
886        default:
887            ALOGE("%s: Unknown event (%d)!", __FUNCTION__, event);
888            break;
889    }
890}
891
892/*******************************************************************************
893**  Client API Functions
894********************************************************************************/
895
896static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid)
897{
898    CHECK_BTGATT_INIT();
899    btif_gattc_cb_t btif_cb;
900    memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t));
901    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP,
902                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
903}
904
905static bt_status_t btif_gattc_unregister_app(int client_if )
906{
907    CHECK_BTGATT_INIT();
908    btif_gattc_cb_t btif_cb;
909    btif_cb.client_if = (uint8_t) client_if;
910    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP,
911                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
912}
913
914static bt_status_t btif_gattc_scan( int client_if, bool start )
915{
916    CHECK_BTGATT_INIT();
917    btif_gattc_cb_t btif_cb;
918    btif_cb.client_if = (uint8_t) client_if;
919    return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP,
920                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
921}
922
923static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr, bool is_direct )
924{
925    CHECK_BTGATT_INIT();
926    btif_gattc_cb_t btif_cb;
927    btif_cb.client_if = (uint8_t) client_if;
928    btif_cb.is_direct = is_direct ? 1 : 0;
929    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
930    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN,
931                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
932}
933
934static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id)
935{
936    CHECK_BTGATT_INIT();
937    btif_gattc_cb_t btif_cb;
938    btif_cb.client_if = (uint8_t) client_if;
939    btif_cb.conn_id = (uint16_t) conn_id;
940    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
941    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE,
942                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
943}
944
945static bt_status_t btif_gattc_listen(int client_if, bool start)
946{
947    CHECK_BTGATT_INIT();
948    btif_gattc_cb_t btif_cb;
949    btif_cb.client_if = (uint8_t) client_if;
950    btif_cb.start = start ? 1 : 0;
951    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_LISTEN,
952                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
953}
954
955static bt_status_t btif_gattc_set_adv_data(int client_if, bool set_scan_rsp, bool include_name,
956                bool include_txpower, int min_interval, int max_interval, int appearance,
957                uint16_t manufacturer_len, char* manufacturer_data,
958                uint16_t service_data_len, char* service_data,
959                uint16_t service_uuid_len, char* service_uuid)
960{
961    CHECK_BTGATT_INIT();
962    btif_gattc_cb_t btif_cb;
963    memset(&btif_cb, 0, sizeof(btif_gattc_cb_t));
964    memset(&btif_cb.adv_data, 0, sizeof(btgatt_adv_data));
965
966    btif_cb.client_if = (uint8_t) client_if;
967    btif_cb.start = set_scan_rsp ? 1 : 0;
968
969    if (!set_scan_rsp)
970    {
971        btif_cb.adv_data.mask = BTM_BLE_AD_BIT_FLAGS;
972        btif_cb.adv_data.data.flag = ADV_FLAGS;
973    }
974
975    if (include_name)
976        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_DEV_NAME;
977
978    if (include_txpower)
979        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_TX_PWR;
980
981    if (min_interval > 0 && max_interval > 0 && max_interval > min_interval)
982    {
983        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_INT_RANGE;
984        btif_cb.adv_data.data.int_range.low = min_interval;
985        btif_cb.adv_data.data.int_range.hi = max_interval;
986    }
987
988    if (appearance != 0)
989    {
990        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_APPEARANCE;
991        btif_cb.adv_data.data.appearance = appearance;
992    }
993
994    if (manufacturer_len > 0 && manufacturer_data != NULL)
995    {
996        btif_cb.adv_data.data.manu.p_val = GKI_getbuf(manufacturer_len);
997        if (btif_cb.adv_data.data.manu.p_val != NULL)
998        {
999            btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_MANU;
1000            btif_cb.adv_data.data.manu.len = manufacturer_len;
1001            memcpy(btif_cb.adv_data.data.manu.p_val, manufacturer_data, manufacturer_len);
1002        }
1003    }
1004
1005    tBTA_BLE_PROP_ELEM *p_elem_service_data = NULL;
1006    tBTA_BLE_PROP_ELEM *p_elem_service_128 = NULL;
1007
1008    if (service_data_len > 0 && service_data != NULL)
1009    {
1010        p_elem_service_data = GKI_getbuf(sizeof(tBTA_BLE_PROP_ELEM));
1011        if (p_elem_service_data != NULL)
1012        {
1013            p_elem_service_data->p_val = GKI_getbuf(service_data_len);
1014            if (p_elem_service_data->p_val != NULL)
1015            {
1016                p_elem_service_data->adv_type = BTM_BLE_AD_TYPE_SERVICE_DATA;
1017                p_elem_service_data->len = service_data_len;
1018                memcpy(p_elem_service_data->p_val, service_data, service_data_len);
1019
1020            } else {
1021                GKI_freebuf(p_elem_service_data);
1022                p_elem_service_data = NULL;
1023            }
1024        }
1025    }
1026
1027    if (service_uuid_len > 0 && service_uuid != NULL)
1028    {
1029        btif_cb.adv_data.data.services.list_cmpl = FALSE;
1030        btif_cb.adv_data.data.services.num_service = 0;
1031
1032        btif_cb.adv_data.data.services.p_uuid =
1033            GKI_getbuf(service_uuid_len / LEN_UUID_128 * LEN_UUID_16);
1034        if (btif_cb.adv_data.data.services.p_uuid != NULL)
1035        {
1036            UINT16 *p_uuid_out = btif_cb.adv_data.data.services.p_uuid;
1037            while (service_uuid_len >= LEN_UUID_128)
1038            {
1039                bt_uuid_t uuid;
1040                memset(&uuid, 0, sizeof(bt_uuid_t));
1041                memcpy(&uuid.uu, service_uuid, LEN_UUID_128);
1042
1043                tBT_UUID bt_uuid;
1044                memset(&bt_uuid, 0, sizeof(tBT_UUID));
1045                btif_to_bta_uuid(&bt_uuid, &uuid);
1046
1047                if (bt_uuid.len == LEN_UUID_16)
1048                {
1049                    btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_SERVICE;
1050                    ++btif_cb.adv_data.data.services.num_service;
1051                    *p_uuid_out++ = bt_uuid.uu.uuid16;
1052
1053                } else if (bt_uuid.len == LEN_UUID_128 && p_elem_service_128 == NULL) {
1054                    /* Currently, only one 128-bit UUID is supported */
1055                    p_elem_service_128 = GKI_getbuf(sizeof(tBTA_BLE_PROP_ELEM));
1056                    if (p_elem_service_128 != NULL)
1057                    {
1058                        p_elem_service_128->p_val = GKI_getbuf(LEN_UUID_128);
1059                        if (p_elem_service_128->p_val != NULL)
1060                        {
1061                            p_elem_service_128->adv_type = BTM_BLE_AD_TYPE_128SRV_PART;
1062                            p_elem_service_128->len = LEN_UUID_128;
1063                            memcpy(p_elem_service_128->p_val, bt_uuid.uu.uuid128, LEN_UUID_128);
1064
1065                        } else {
1066                            GKI_freebuf(p_elem_service_128);
1067                            p_elem_service_128 = NULL;
1068                        }
1069                    }
1070                }
1071
1072                service_uuid += LEN_UUID_128;
1073                service_uuid_len -= LEN_UUID_128;
1074            }
1075        }
1076    }
1077
1078    if (p_elem_service_data != NULL || p_elem_service_128 != NULL)
1079    {
1080        btif_cb.adv_data.data.p_proprietary = GKI_getbuf(sizeof(tBTA_BLE_PROPRIETARY));
1081        if (btif_cb.adv_data.data.p_proprietary != NULL)
1082        {
1083            tBTA_BLE_PROPRIETARY *p_prop = btif_cb.adv_data.data.p_proprietary;
1084            tBTA_BLE_PROP_ELEM *p_elem = NULL;
1085            p_prop->num_elem = 0;
1086            btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_PROPRIETARY;
1087
1088            if (p_elem_service_128 != NULL)
1089                ++p_prop->num_elem;
1090
1091            if (p_elem_service_data != NULL)
1092                ++p_prop->num_elem;
1093
1094            p_prop->p_elem = GKI_getbuf(sizeof(tBTA_BLE_PROP_ELEM) * p_prop->num_elem);
1095            p_elem = p_prop->p_elem;
1096
1097            if (p_elem_service_128 != NULL)
1098            {
1099                memcpy(p_elem++, p_elem_service_128, sizeof(tBTA_BLE_PROP_ELEM));
1100                GKI_freebuf(p_elem_service_128);
1101            }
1102
1103            if (p_elem_service_data != NULL)
1104            {
1105                memcpy(p_elem++, p_elem_service_data, sizeof(tBTA_BLE_PROP_ELEM));
1106                GKI_freebuf(p_elem_service_data);
1107            }
1108        }
1109    }
1110
1111    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_ADV_DATA,
1112                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1113}
1114
1115static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
1116{
1117    CHECK_BTGATT_INIT();
1118    btif_gattc_cb_t btif_cb;
1119    btif_cb.client_if = (uint8_t) client_if;
1120    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1121    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH,
1122                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1123}
1124
1125static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid )
1126{
1127    CHECK_BTGATT_INIT();
1128    btif_gattc_cb_t btif_cb;
1129    btif_cb.conn_id = (uint16_t) conn_id;
1130    btif_cb.search_all = filter_uuid ? 0 : 1;
1131    if (filter_uuid)
1132        memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t));
1133    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE,
1134                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1135}
1136
1137static bt_status_t btif_gattc_get_characteristic( int conn_id
1138        , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
1139{
1140    CHECK_BTGATT_INIT();
1141    btif_gattc_cb_t btif_cb;
1142    btif_cb.conn_id = (uint16_t) conn_id;
1143    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1144    if (start_char_id)
1145    {
1146        memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_gatt_id_t));
1147        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR,
1148                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1149    }
1150    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR,
1151                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1152}
1153
1154static bt_status_t btif_gattc_get_descriptor( int conn_id
1155        , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id
1156        , btgatt_gatt_id_t *start_descr_id)
1157{
1158    CHECK_BTGATT_INIT();
1159    btif_gattc_cb_t btif_cb;
1160    btif_cb.conn_id = (uint16_t) conn_id;
1161    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1162    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1163    if (start_descr_id)
1164    {
1165        memcpy(&btif_cb.descr_id, start_descr_id, sizeof(btgatt_gatt_id_t));
1166        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR,
1167                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1168    }
1169
1170    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR,
1171                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1172}
1173
1174static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
1175                                                   btgatt_srvc_id_t *start_incl_srvc_id)
1176{
1177    CHECK_BTGATT_INIT();
1178    btif_gattc_cb_t btif_cb;
1179    btif_cb.conn_id = (uint16_t) conn_id;
1180    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1181    if (start_incl_srvc_id)
1182    {
1183        memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t));
1184        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE,
1185                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1186    }
1187    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE,
1188                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1189}
1190
1191static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id,
1192                                        btgatt_gatt_id_t* char_id, int auth_req )
1193{
1194    CHECK_BTGATT_INIT();
1195    btif_gattc_cb_t btif_cb;
1196    btif_cb.conn_id = (uint16_t) conn_id;
1197    btif_cb.auth_req = (uint8_t) auth_req;
1198    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1199    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1200    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR,
1201                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1202}
1203
1204static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
1205                                              btgatt_gatt_id_t* char_id,
1206                                              btgatt_gatt_id_t* descr_id,
1207                                              int auth_req )
1208{
1209    CHECK_BTGATT_INIT();
1210    btif_gattc_cb_t btif_cb;
1211    btif_cb.conn_id = (uint16_t) conn_id;
1212    btif_cb.auth_req = (uint8_t) auth_req;
1213    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1214    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1215    memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
1216    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR,
1217                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1218}
1219
1220static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id,
1221                                         btgatt_gatt_id_t* char_id, int write_type,
1222                                         int len, int auth_req, char* p_value)
1223{
1224    CHECK_BTGATT_INIT();
1225    btif_gattc_cb_t btif_cb;
1226    btif_cb.conn_id = (uint16_t) conn_id;
1227    btif_cb.auth_req = (uint8_t) auth_req;
1228    btif_cb.write_type = (uint8_t) write_type;
1229    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
1230    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1231    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1232    memcpy(btif_cb.value, p_value, btif_cb.len);
1233    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR,
1234                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1235}
1236
1237static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
1238                                               btgatt_gatt_id_t* char_id,
1239                                               btgatt_gatt_id_t* descr_id,
1240                                               int write_type, int len, int auth_req,
1241                                               char* p_value)
1242{
1243    CHECK_BTGATT_INIT();
1244    btif_gattc_cb_t btif_cb;
1245    btif_cb.conn_id = (uint16_t) conn_id;
1246    btif_cb.auth_req = (uint8_t) auth_req;
1247    btif_cb.write_type = (uint8_t) write_type;
1248    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
1249    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1250    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1251    memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
1252    memcpy(btif_cb.value, p_value, btif_cb.len);
1253    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR,
1254                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1255}
1256
1257static bt_status_t btif_gattc_execute_write(int conn_id, int execute)
1258{
1259    CHECK_BTGATT_INIT();
1260    btif_gattc_cb_t btif_cb;
1261    btif_cb.conn_id = (uint16_t) conn_id;
1262    btif_cb.action = (uint8_t) execute;
1263    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE,
1264                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1265}
1266
1267static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
1268                                                   btgatt_srvc_id_t* srvc_id,
1269                                                   btgatt_gatt_id_t* char_id)
1270{
1271    CHECK_BTGATT_INIT();
1272    btif_gattc_cb_t btif_cb;
1273    btif_cb.client_if = (uint8_t) client_if;
1274    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1275    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1276    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1277    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION,
1278                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1279}
1280
1281static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
1282                                                     btgatt_srvc_id_t* srvc_id,
1283                                                     btgatt_gatt_id_t* char_id)
1284{
1285    CHECK_BTGATT_INIT();
1286    btif_gattc_cb_t btif_cb;
1287    btif_cb.client_if = (uint8_t) client_if;
1288    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1289    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1290    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
1291    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION,
1292                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1293}
1294
1295static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
1296{
1297    CHECK_BTGATT_INIT();
1298    btif_gattc_cb_t btif_cb;
1299    btif_cb.client_if = (uint8_t) client_if;
1300    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1301    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI,
1302                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1303}
1304
1305static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr )
1306{
1307    int device_type = 0;
1308    char bd_addr_str[18] = {0};
1309
1310    bd2str(bd_addr, &bd_addr_str);
1311    if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type))
1312        return device_type;
1313    return 0;
1314}
1315
1316extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params);
1317
1318static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params)
1319{
1320    return btif_gattc_test_command_impl(command, params);
1321}
1322
1323
1324const btgatt_client_interface_t btgattClientInterface = {
1325    btif_gattc_register_app,
1326    btif_gattc_unregister_app,
1327    btif_gattc_scan,
1328    btif_gattc_open,
1329    btif_gattc_close,
1330    btif_gattc_listen,
1331    btif_gattc_refresh,
1332    btif_gattc_search_service,
1333    btif_gattc_get_included_service,
1334    btif_gattc_get_characteristic,
1335    btif_gattc_get_descriptor,
1336    btif_gattc_read_char,
1337    btif_gattc_write_char,
1338    btif_gattc_read_char_descr,
1339    btif_gattc_write_char_descr,
1340    btif_gattc_execute_write,
1341    btif_gattc_reg_for_notification,
1342    btif_gattc_dereg_for_notification,
1343    btif_gattc_read_remote_rssi,
1344    btif_gattc_get_device_type,
1345    btif_gattc_set_adv_data,
1346    btif_gattc_test_command
1347};
1348
1349#endif
1350