btif_gatt_client.c revision 8444d1e649864c28b0c124bc2d16f7c153f01f14
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 <hardware/bt_gatt.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <errno.h>
33#include <string.h>
34
35#define LOG_TAG "BtGatt.btif"
36
37#include "btif_common.h"
38#include "btif_util.h"
39
40#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
41
42#include "gki.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 CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
59    {\
60        ALOGW("%s: BTGATT not initialized", __FUNCTION__);\
61        return BT_STATUS_NOT_READY;\
62    } else {\
63        ALOGD("%s", __FUNCTION__);\
64    }
65
66
67typedef enum {
68    BTIF_GATTC_REGISTER_APP = 1000,
69    BTIF_GATTC_UNREGISTER_APP,
70    BTIF_GATTC_SCAN_START,
71    BTIF_GATTC_SCAN_STOP,
72    BTIF_GATTC_OPEN,
73    BTIF_GATTC_CLOSE,
74    BTIF_GATTC_SEARCH_SERVICE,
75    BTIF_GATTC_GET_FIRST_CHAR,
76    BTIF_GATTC_GET_NEXT_CHAR,
77    BTIF_GATTC_GET_FIRST_CHAR_DESCR,
78    BTIF_GATTC_GET_NEXT_CHAR_DESCR,
79    BTIF_GATTC_GET_FIRST_INCL_SERVICE,
80    BTIF_GATTC_GET_NEXT_INCL_SERVICE,
81    BTIF_GATTC_READ_CHAR,
82    BTIF_GATTC_READ_CHAR_DESCR,
83    BTIF_GATTC_WRITE_CHAR,
84    BTIF_GATTC_WRITE_CHAR_DESCR,
85    BTIF_GATTC_EXECUTE_WRITE,
86    BTIF_GATTC_REG_FOR_NOTIFICATION,
87    BTIF_GATTC_DEREG_FOR_NOTIFICATION,
88    BTIF_GATTC_REFRESH,
89    BTIF_GATTC_READ_RSSI
90} btif_gattc_event_t;
91
92#define BTIF_GATT_MAX_OBSERVED_DEV 40
93
94#define BTIF_GATT_OBSERVE_EVT   0x1000
95#define BTIF_GATTC_RSSI_EVT     0x1001
96
97/*******************************************************************************
98**  Local type definitions
99********************************************************************************/
100
101typedef struct
102{
103    uint8_t     value[BTGATT_MAX_ATTR_LEN];
104    bt_bdaddr_t bd_addr;
105    btgatt_srvc_id_t srvc_id;
106    btgatt_srvc_id_t incl_srvc_id;
107    btgatt_char_id_t char_id;
108    bt_uuid_t   uuid;
109    uint16_t    conn_id;
110    uint16_t    len;
111    uint8_t     client_if;
112    uint8_t     action;
113    uint8_t     is_direct;
114    uint8_t     search_all;
115    uint8_t     auth_req;
116    uint8_t     write_type;
117    uint8_t     status;
118    uint8_t     addr_type;
119    int8_t      rssi;
120    tBT_DEVICE_TYPE device_type;
121} __attribute__((packed)) btif_gattc_cb_t;
122
123typedef struct
124{
125    bt_bdaddr_t bd_addr;
126    BOOLEAN     in_use;
127}__attribute__((packed)) btif_gattc_dev_t;
128
129typedef struct
130{
131    btif_gattc_dev_t remote_dev[BTIF_GATT_MAX_OBSERVED_DEV];
132    uint8_t        addr_type;
133    uint8_t        next_storage_idx;
134}__attribute__((packed)) btif_gattc_dev_cb_t;
135
136/*******************************************************************************
137**  Static variables
138********************************************************************************/
139
140extern const btgatt_callbacks_t *bt_gatt_callbacks;
141static btif_gattc_dev_cb_t  btif_gattc_dev_cb;
142static btif_gattc_dev_cb_t  *p_dev_cb = &btif_gattc_dev_cb;
143static uint8_t rssi_request_client_if;
144
145/*******************************************************************************
146**  Static functions
147********************************************************************************/
148
149static void btapp_gattc_req_data(UINT16 event, char *p_dest, char *p_src)
150{
151    tBTA_GATTC *p_dest_data = (tBTA_GATTC*)p_dest;
152    tBTA_GATTC *p_src_data = (tBTA_GATTC*)p_src;
153
154    if (!p_src_data || !p_dest_data)
155       return;
156
157    // Copy basic structure first
158    memcpy(p_dest_data, p_src_data, sizeof(tBTA_GATTC));
159
160    // Allocate buffer for request data if necessary
161    switch (event)
162    {
163        case BTA_GATTC_READ_CHAR_EVT:
164        case BTA_GATTC_READ_DESCR_EVT:
165            p_dest_data->read.p_value = GKI_getbuf(sizeof(tBTA_GATT_READ_VAL));
166
167            if (p_dest_data->read.p_value != NULL)
168            {
169                memcpy(p_dest_data->read.p_value, p_src_data->read.p_value,
170                    sizeof(tBTA_GATT_READ_VAL));
171
172                // Allocate buffer for att value if necessary
173                if (get_uuid16(&p_src_data->read.descr_type) != GATT_UUID_CHAR_AGG_FORMAT
174                  && p_src_data->read.p_value->unformat.p_value != NULL)
175                {
176                    p_dest_data->read.p_value->unformat.p_value =
177                                   GKI_getbuf(p_src_data->read.p_value->unformat.len);
178                    if (p_dest_data->read.p_value->unformat.p_value != NULL)
179                    {
180                        memcpy(p_dest_data->read.p_value->unformat.p_value,
181                               p_src_data->read.p_value->unformat.p_value,
182                               p_src_data->read.p_value->unformat.len);
183                    }
184                }
185            }
186            break;
187
188        default:
189            break;
190    }
191}
192
193static void btapp_gattc_free_req_data(UINT16 event, tBTA_GATTC *p_data)
194{
195    switch (event)
196    {
197        case BTA_GATTC_READ_CHAR_EVT:
198        case BTA_GATTC_READ_DESCR_EVT:
199            if (p_data != NULL && p_data->read.p_value != NULL)
200            {
201                if (get_uuid16 (&p_data->read.descr_type) != GATT_UUID_CHAR_AGG_FORMAT
202                  && p_data->read.p_value->unformat.p_value != NULL)
203                {
204                    GKI_freebuf(p_data->read.p_value->unformat.p_value);
205                }
206                GKI_freebuf(p_data->read.p_value);
207            }
208            break;
209
210        default:
211            break;
212    }
213}
214
215static void btif_gattc_init_dev_cb(void)
216{
217    memset(p_dev_cb, 0, sizeof(btif_gattc_dev_cb_t));
218}
219
220static void btif_gattc_add_remote_bdaddr (BD_ADDR p_bda, uint8_t addr_type)
221{
222    BOOLEAN found=FALSE;
223    uint8_t i;
224    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
225    {
226        if (!p_dev_cb->remote_dev[i].in_use )
227        {
228            memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
229            p_dev_cb->addr_type = addr_type;
230            p_dev_cb->remote_dev[i].in_use = TRUE;
231            ALOGD("%s device added idx=%d", __FUNCTION__, i  );
232            break;
233        }
234    }
235
236    if ( i == BTIF_GATT_MAX_OBSERVED_DEV)
237    {
238        i= p_dev_cb->next_storage_idx;
239        memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
240        p_dev_cb->addr_type = addr_type;
241        p_dev_cb->remote_dev[i].in_use = TRUE;
242        ALOGD("%s device overwrite idx=%d", __FUNCTION__, i  );
243        p_dev_cb->next_storage_idx++;
244        if(p_dev_cb->next_storage_idx >= BTIF_GATT_MAX_OBSERVED_DEV)
245               p_dev_cb->next_storage_idx = 0;
246    }
247}
248
249static BOOLEAN btif_gattc_find_bdaddr (BD_ADDR p_bda)
250{
251    uint8_t i;
252    for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
253    {
254        if (p_dev_cb->remote_dev[i].in_use &&
255            !memcmp(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN))
256        {
257            return TRUE;
258        }
259    }
260    return FALSE;
261}
262
263static void btif_gattc_update_properties ( btif_gattc_cb_t *p_btif_cb )
264{
265    uint8_t remote_name_len;
266    uint8_t *p_eir_remote_name=NULL;
267    bt_bdname_t bdname;
268
269    p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
270                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
271
272    if(p_eir_remote_name == NULL)
273    {
274        p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
275                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
276    }
277
278    if(p_eir_remote_name)
279    {
280         memcpy(bdname.name, p_eir_remote_name, remote_name_len);
281         bdname.name[remote_name_len]='\0';
282
283        ALOGD("%s BLE device name=%s len=%d dev_type=%d", __FUNCTION__, bdname.name,
284              remote_name_len, p_btif_cb->device_type  );
285        btif_dm_update_ble_remote_properties( p_btif_cb->bd_addr.address,   bdname.name,
286                                               p_btif_cb->device_type);
287    }
288
289    btif_storage_set_remote_addr_type( &p_btif_cb->bd_addr, p_btif_cb->addr_type);
290}
291
292static void btif_gattc_upstreams_evt(uint16_t event, char* p_param)
293{
294    ALOGD("%s: Event %d", __FUNCTION__, event);
295
296    tBTA_GATTC *p_data = (tBTA_GATTC*)p_param;
297    switch (event)
298    {
299        case BTA_GATTC_REG_EVT:
300        {
301            bt_uuid_t app_uuid;
302            bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
303            HAL_CBACK(bt_gatt_callbacks, client->register_client_cb
304                , p_data->reg_oper.status
305                , p_data->reg_oper.client_if
306                , &app_uuid
307            );
308            break;
309        }
310
311        case BTA_GATTC_DEREG_EVT:
312            break;
313
314        case BTA_GATTC_READ_CHAR_EVT:
315        {
316            btgatt_read_params_t data;
317            set_read_value(&data, &p_data->read);
318
319            HAL_CBACK(bt_gatt_callbacks, client->read_characteristic_cb
320                , p_data->read.conn_id, p_data->read.status, &data);
321            break;
322        }
323
324        case BTA_GATTC_WRITE_CHAR_EVT:
325        case BTA_GATTC_PREP_WRITE_EVT:
326        {
327            btgatt_write_params_t data;
328            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
329            bta_to_btif_char_id(&data.char_id, &p_data->write.char_id);
330
331            HAL_CBACK(bt_gatt_callbacks, client->write_characteristic_cb
332                , p_data->write.conn_id, p_data->write.status, &data
333            );
334            break;
335        }
336
337        case BTA_GATTC_EXEC_EVT:
338        {
339            HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb
340                , p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status
341            );
342            break;
343        }
344
345        case BTA_GATTC_SEARCH_CMPL_EVT:
346        {
347            HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb
348                , p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
349            break;
350        }
351
352        case BTA_GATTC_SEARCH_RES_EVT:
353        {
354            btgatt_srvc_id_t data;
355            bta_to_btif_srvc_id(&data, &(p_data->srvc_res.service_uuid));
356            HAL_CBACK(bt_gatt_callbacks, client->search_result_cb
357                , p_data->srvc_res.conn_id, &data);
358            break;
359        }
360
361        case BTA_GATTC_READ_DESCR_EVT:
362        {
363            btgatt_read_params_t data;
364            set_read_value(&data, &p_data->read);
365
366            HAL_CBACK(bt_gatt_callbacks, client->read_descriptor_cb
367                , p_data->read.conn_id, p_data->read.status, &data);
368            break;
369        }
370
371        case BTA_GATTC_WRITE_DESCR_EVT:
372        {
373            btgatt_write_params_t data;
374            bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
375            bta_to_btif_char_id(&data.char_id, &p_data->write.char_id);
376            bta_to_btif_uuid(&data.descr_id, &p_data->write.descr_type);
377
378            HAL_CBACK(bt_gatt_callbacks, client->write_descriptor_cb
379                , p_data->write.conn_id, p_data->write.status, &data);
380            break;
381        }
382
383        case BTA_GATTC_NOTIF_EVT:
384        {
385            btgatt_notify_params_t data;
386
387            bdcpy(data.bda.address, p_data->notify.bda);
388
389            bta_to_btif_srvc_id(&data.srvc_id, &p_data->notify.char_id.srvc_id);
390            bta_to_btif_char_id(&data.char_id, &p_data->notify.char_id.char_id);
391            memcpy(data.value, p_data->notify.value, p_data->notify.len);
392
393            data.is_notify = p_data->notify.is_notify;
394            data.len = p_data->notify.len;
395
396            HAL_CBACK(bt_gatt_callbacks, client->notify_cb
397                , p_data->notify.conn_id, &data);
398
399            if (p_data->notify.is_notify == FALSE)
400            {
401                BTA_GATTC_SendIndConfirm(p_data->notify.conn_id,
402                                         &p_data->notify.char_id);
403            }
404            break;
405        }
406
407        case BTA_GATTC_OPEN_EVT:
408        {
409            bt_bdaddr_t bda;
410            bdcpy(bda.address, p_data->open.remote_bda);
411
412            if (p_data->open.status == BTA_GATT_OK)
413                btif_gatt_check_encrypted_link(p_data->open.remote_bda);
414
415            HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id
416                , p_data->open.status, p_data->open.client_if, &bda);
417            break;
418        }
419
420        case BTA_GATTC_CLOSE_EVT:
421        {
422            bt_bdaddr_t bda;
423            bdcpy(bda.address, p_data->close.remote_bda);
424            HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id
425                , p_data->status, p_data->close.client_if, &bda);
426
427            if(p_data->status == BTA_GATT_OK)
428                btif_gatt_remove_encrypted_link(p_data->close.remote_bda);
429            break;
430        }
431
432        case BTA_GATTC_ACL_EVT:
433            ALOGD("BTA_GATTC_ACL_EVT: status = %d", p_data->status);
434            /* Ignore for now */
435            break;
436
437        case BTA_GATTC_CANCEL_OPEN_EVT:
438            break;
439
440        case BTIF_GATT_OBSERVE_EVT:
441        {
442            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
443            if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
444            {
445                btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
446                btif_gattc_update_properties(p_btif_cb);
447            }
448            HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb,
449                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value);
450            break;
451        }
452
453        case BTIF_GATTC_RSSI_EVT:
454        {
455            btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
456            HAL_CBACK(bt_gatt_callbacks, client->read_remote_rssi_cb, p_btif_cb->client_if,
457                      &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->status);
458            break;
459        }
460
461        default:
462            ALOGE("%s: Unhandled event (%d)!", __FUNCTION__, event);
463            break;
464    }
465
466    btapp_gattc_free_req_data(event, p_data);
467}
468
469static void bte_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
470{
471    bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt,
472                    (uint16_t) event, (void*)p_data, sizeof(tBTA_GATTC), btapp_gattc_req_data);
473    ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
474}
475
476static void bte_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
477{
478    btif_gattc_cb_t btif_cb;
479    uint8_t len;
480
481    switch (event)
482    {
483        case BTA_DM_INQ_RES_EVT:
484        {
485            bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr);
486            btif_cb.device_type = p_data->inq_res.device_type;
487            btif_cb.rssi = p_data->inq_res.rssi;
488            btif_cb.addr_type = p_data->inq_res.ble_addr_type;
489            if (p_data->inq_res.p_eir)
490            {
491                memcpy(btif_cb.value, p_data->inq_res.p_eir, 62);
492                if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
493                                      &len))
494                {
495                    p_data->inq_res.remt_name_not_required  = TRUE;
496                }
497            }
498        }
499        break;
500
501        case BTA_DM_INQ_CMPL_EVT:
502        {
503            BTIF_TRACE_DEBUG2("%s  BLE observe complete. Num Resp %d",
504                              __FUNCTION__,p_data->inq_cmpl.num_resps);
505            return;
506        }
507
508        default:
509        BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
510        return;
511    }
512    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT,
513                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
514}
515
516static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result)
517{
518    btif_gattc_cb_t btif_cb;
519
520    bdcpy(btif_cb.bd_addr.address, p_result->rem_bda);
521    btif_cb.rssi = p_result->rssi;
522    btif_cb.status = p_result->status;
523    btif_cb.client_if = rssi_request_client_if;
524    btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT,
525                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
526}
527
528
529static void btgattc_handle_event(uint16_t event, char* p_param)
530{
531    tBTA_GATT_STATUS           status;
532    tBT_UUID                   uuid;
533    tBTA_GATT_SRVC_ID          srvc_id;
534    tGATT_CHAR_PROP            out_char_prop;
535    tBTA_GATTC_CHAR_ID         in_char_id;
536    tBTA_GATTC_CHAR_ID         out_char_id;
537    tBTA_GATTC_CHAR_DESCR_ID   in_char_descr_id;
538    tBTA_GATTC_CHAR_DESCR_ID   out_char_descr_id;
539    tBTA_GATTC_INCL_SVC_ID     in_incl_svc_id;
540    tBTA_GATTC_INCL_SVC_ID     out_incl_svc_id;
541    tBTA_GATT_UNFMT            descr_val;
542
543    btif_gattc_cb_t* p_cb = (btif_gattc_cb_t*)p_param;
544    if (!p_cb) return;
545
546    ALOGD("%s: Event %d", __FUNCTION__, event);
547
548    switch (event)
549    {
550        case BTIF_GATTC_REGISTER_APP:
551            btif_to_bta_uuid(&uuid, &p_cb->uuid);
552            BTA_GATTC_AppRegister(&uuid, bte_gattc_cback);
553            break;
554
555        case BTIF_GATTC_UNREGISTER_APP:
556            BTA_GATTC_AppDeregister(p_cb->client_if);
557            break;
558
559        case BTIF_GATTC_SCAN_START:
560            btif_gattc_init_dev_cb();
561            BTA_DmBleObserve(TRUE, 0, bte_scan_results_cb);
562            break;
563
564        case BTIF_GATTC_SCAN_STOP:
565            BTA_DmBleObserve(FALSE, 0, 0);
566            break;
567
568        case BTIF_GATTC_OPEN:
569            if (!p_cb->is_direct)
570                BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
571
572            BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct);
573            break;
574
575        case BTIF_GATTC_CLOSE:
576            // Disconnect establiched connections
577            if (p_cb->conn_id != 0)
578                BTA_GATTC_Close(p_cb->conn_id);
579            else
580                BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE);
581
582            // Cancel pending background connections (remove from whitelist)
583            BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE);
584            break;
585
586        case BTIF_GATTC_SEARCH_SERVICE:
587        {
588            if (p_cb->search_all)
589            {
590                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL);
591            } else {
592                btif_to_bta_uuid(&uuid, &p_cb->uuid);
593                BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid);
594            }
595            break;
596        }
597
598        case BTIF_GATTC_GET_FIRST_CHAR:
599        {
600            btgatt_char_id_t char_id;
601            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
602            status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL,
603                                            &out_char_id, &out_char_prop);
604
605            if (status == 0)
606                bta_to_btif_char_id(&char_id, &out_char_id.char_id);
607
608            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
609                p_cb->conn_id, status, &p_cb->srvc_id,
610                &char_id, out_char_prop);
611            break;
612        }
613
614        case BTIF_GATTC_GET_NEXT_CHAR:
615        {
616            btgatt_char_id_t char_id;
617            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
618            btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
619
620            status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL,
621                                            &out_char_id, &out_char_prop);
622
623            if (status == 0)
624                bta_to_btif_char_id(&char_id, &out_char_id.char_id);
625
626            HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
627                p_cb->conn_id, status, &p_cb->srvc_id,
628                &char_id, out_char_prop);
629            break;
630        }
631
632        case BTIF_GATTC_GET_FIRST_CHAR_DESCR:
633        {
634            bt_uuid_t descr_id;
635            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
636            btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
637
638            status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL,
639                                                    &out_char_descr_id);
640
641            if (status == 0)
642                bta_to_btif_uuid(&descr_id, &out_char_descr_id.descr_type);
643
644            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
645                p_cb->conn_id, status, &p_cb->srvc_id,
646                &p_cb->char_id, &descr_id);
647            break;
648        }
649
650        case BTIF_GATTC_GET_NEXT_CHAR_DESCR:
651        {
652            bt_uuid_t descr_id;
653            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
654            btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
655            btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid);
656
657            status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id
658                                        , NULL, &out_char_descr_id);
659
660            if (status == 0)
661                bta_to_btif_uuid(&descr_id, &out_char_descr_id.descr_type);
662
663            HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
664                p_cb->conn_id, status, &p_cb->srvc_id,
665                &p_cb->char_id, &descr_id);
666            break;
667        }
668
669        case BTIF_GATTC_GET_FIRST_INCL_SERVICE:
670        {
671            btgatt_srvc_id_t incl_srvc_id;
672            btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
673
674            status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id,
675                        &srvc_id, NULL, &out_incl_svc_id);
676
677            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
678
679            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
680                p_cb->conn_id, status, &p_cb->srvc_id,
681                &incl_srvc_id);
682            break;
683        }
684
685        case BTIF_GATTC_GET_NEXT_INCL_SERVICE:
686        {
687            btgatt_srvc_id_t incl_srvc_id;
688            btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id);
689            btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id);
690
691            status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id,
692                        &in_incl_svc_id, NULL, &out_incl_svc_id);
693
694            bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
695
696            HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
697                p_cb->conn_id, status, &p_cb->srvc_id,
698                &incl_srvc_id);
699            break;
700        }
701
702        case BTIF_GATTC_READ_CHAR:
703            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
704            btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
705
706            BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req);
707            break;
708
709        case BTIF_GATTC_READ_CHAR_DESCR:
710            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
711            btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
712            btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid);
713
714            BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req);
715            break;
716
717        case BTIF_GATTC_WRITE_CHAR:
718            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
719            btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
720
721            BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id,
722                                     p_cb->write_type,
723                                     p_cb->len,
724                                     p_cb->value,
725                                     p_cb->auth_req);
726            break;
727
728        case BTIF_GATTC_WRITE_CHAR_DESCR:
729            btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
730            btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
731            btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid);
732
733            descr_val.len = p_cb->len;
734            descr_val.p_value = p_cb->value;
735
736            BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id,
737                                     p_cb->write_type, &descr_val,
738                                     p_cb->auth_req);
739            break;
740
741        case BTIF_GATTC_EXECUTE_WRITE:
742            BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action);
743            break;
744
745        case BTIF_GATTC_REG_FOR_NOTIFICATION:
746            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
747            btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
748
749            status = BTA_GATTC_RegisterForNotifications(p_cb->client_if,
750                                    p_cb->bd_addr.address, &in_char_id);
751
752            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
753                p_cb->conn_id, 1, status, &p_cb->srvc_id,
754                &p_cb->char_id);
755            break;
756
757        case BTIF_GATTC_DEREG_FOR_NOTIFICATION:
758            btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
759            btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
760
761            status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if,
762                                        p_cb->bd_addr.address, &in_char_id);
763
764            HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
765                p_cb->conn_id, 0, status, &p_cb->srvc_id,
766                &p_cb->char_id);
767            break;
768
769        case BTIF_GATTC_REFRESH:
770            BTA_GATTC_Refresh(p_cb->bd_addr.address);
771            break;
772
773        case BTIF_GATTC_READ_RSSI:
774            rssi_request_client_if = p_cb->client_if;
775            BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
776            break;
777
778        default:
779            ALOGE("%s: Unknown event (%d)!", __FUNCTION__, event);
780            break;
781    }
782}
783
784/*******************************************************************************
785**  Client API Functions
786********************************************************************************/
787
788static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid)
789{
790    CHECK_BTGATT_INIT();
791    btif_gattc_cb_t btif_cb;
792    memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t));
793    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP,
794                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
795}
796
797static bt_status_t btif_gattc_unregister_app(int client_if )
798{
799    CHECK_BTGATT_INIT();
800    btif_gattc_cb_t btif_cb;
801    btif_cb.client_if = (uint8_t) client_if;
802    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP,
803                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
804}
805
806static bt_status_t btif_gattc_scan( int client_if, bool start )
807{
808    CHECK_BTGATT_INIT();
809    btif_gattc_cb_t btif_cb;
810    btif_cb.client_if = (uint8_t) client_if;
811    return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP,
812                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
813}
814
815static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr, bool is_direct )
816{
817    CHECK_BTGATT_INIT();
818    btif_gattc_cb_t btif_cb;
819    btif_cb.client_if = (uint8_t) client_if;
820    btif_cb.is_direct = is_direct ? 1 : 0;
821    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
822    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN,
823                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
824}
825
826static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id)
827{
828    CHECK_BTGATT_INIT();
829    btif_gattc_cb_t btif_cb;
830    btif_cb.client_if = (uint8_t) client_if;
831    btif_cb.conn_id = (uint16_t) conn_id;
832    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
833    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE,
834                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
835}
836
837static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
838{
839    CHECK_BTGATT_INIT();
840    btif_gattc_cb_t btif_cb;
841    btif_cb.client_if = (uint8_t) client_if;
842    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
843    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH,
844                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
845}
846
847static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid )
848{
849    CHECK_BTGATT_INIT();
850    btif_gattc_cb_t btif_cb;
851    btif_cb.conn_id = (uint16_t) conn_id;
852    btif_cb.search_all = filter_uuid ? 0 : 1;
853    if (filter_uuid)
854        memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t));
855    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE,
856                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
857}
858
859static bt_status_t btif_gattc_get_characteristic( int conn_id
860        , btgatt_srvc_id_t *srvc_id, btgatt_char_id_t *start_char_id)
861{
862    CHECK_BTGATT_INIT();
863    btif_gattc_cb_t btif_cb;
864    btif_cb.conn_id = (uint16_t) conn_id;
865    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
866    if (start_char_id)
867    {
868        memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_char_id_t));
869        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR,
870                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
871    }
872    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR,
873                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
874}
875
876static bt_status_t btif_gattc_get_descriptor( int conn_id
877        , btgatt_srvc_id_t *srvc_id, btgatt_char_id_t *char_id
878        , bt_uuid_t *start_descr_id)
879{
880    CHECK_BTGATT_INIT();
881    btif_gattc_cb_t btif_cb;
882    btif_cb.conn_id = (uint16_t) conn_id;
883    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
884    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
885    if (start_descr_id)
886    {
887        memcpy(&btif_cb.uuid, start_descr_id, sizeof(bt_uuid_t));
888        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR,
889                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
890    }
891
892    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR,
893                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
894}
895
896static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
897                                                   btgatt_srvc_id_t *start_incl_srvc_id)
898{
899    CHECK_BTGATT_INIT();
900    btif_gattc_cb_t btif_cb;
901    btif_cb.conn_id = (uint16_t) conn_id;
902    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
903    if (start_incl_srvc_id)
904    {
905        memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t));
906        return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE,
907                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
908    }
909    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE,
910                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
911}
912
913static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id,
914                                        btgatt_char_id_t* char_id, int auth_req )
915{
916    CHECK_BTGATT_INIT();
917    btif_gattc_cb_t btif_cb;
918    btif_cb.conn_id = (uint16_t) conn_id;
919    btif_cb.auth_req = (uint8_t) auth_req;
920    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
921    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
922    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR,
923                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
924}
925
926static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
927                                              btgatt_char_id_t* char_id, bt_uuid_t* descr_id,
928                                              int auth_req )
929{
930    CHECK_BTGATT_INIT();
931    btif_gattc_cb_t btif_cb;
932    btif_cb.conn_id = (uint16_t) conn_id;
933    btif_cb.auth_req = (uint8_t) auth_req;
934    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
935    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
936    memcpy(&btif_cb.uuid, descr_id, sizeof(bt_uuid_t));
937    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR,
938                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
939}
940
941static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id,
942                                         btgatt_char_id_t* char_id, int write_type,
943                                         int len, int auth_req, char* p_value)
944{
945    CHECK_BTGATT_INIT();
946    btif_gattc_cb_t btif_cb;
947    btif_cb.conn_id = (uint16_t) conn_id;
948    btif_cb.auth_req = (uint8_t) auth_req;
949    btif_cb.write_type = (uint8_t) write_type;
950    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
951    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
952    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
953    memcpy(btif_cb.value, p_value, btif_cb.len);
954    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR,
955                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
956}
957
958static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
959                                               btgatt_char_id_t* char_id, bt_uuid_t* descr_id,
960                                               int write_type, int len, int auth_req,
961                                               char* p_value)
962{
963    CHECK_BTGATT_INIT();
964    btif_gattc_cb_t btif_cb;
965    btif_cb.conn_id = (uint16_t) conn_id;
966    btif_cb.auth_req = (uint8_t) auth_req;
967    btif_cb.write_type = (uint8_t) write_type;
968    btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
969    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
970    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
971    memcpy(&btif_cb.uuid, descr_id, sizeof(bt_uuid_t));
972    memcpy(btif_cb.value, p_value, btif_cb.len);
973    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR,
974                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
975}
976
977static bt_status_t btif_gattc_execute_write(int conn_id, int execute)
978{
979    CHECK_BTGATT_INIT();
980    btif_gattc_cb_t btif_cb;
981    btif_cb.conn_id = (uint16_t) conn_id;
982    btif_cb.action = (uint8_t) execute;
983    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE,
984                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
985}
986
987static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
988                                                   btgatt_srvc_id_t* srvc_id,
989                                                   btgatt_char_id_t* char_id)
990{
991    CHECK_BTGATT_INIT();
992    btif_gattc_cb_t btif_cb;
993    btif_cb.client_if = (uint8_t) client_if;
994    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
995    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
996    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
997    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION,
998                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
999}
1000
1001static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
1002                                                     btgatt_srvc_id_t* srvc_id,
1003                                                     btgatt_char_id_t* char_id)
1004{
1005    CHECK_BTGATT_INIT();
1006    btif_gattc_cb_t btif_cb;
1007    btif_cb.client_if = (uint8_t) client_if;
1008    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1009    memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1010    memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
1011    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION,
1012                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1013}
1014
1015static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
1016{
1017    CHECK_BTGATT_INIT();
1018    btif_gattc_cb_t btif_cb;
1019    btif_cb.client_if = (uint8_t) client_if;
1020    bdcpy(btif_cb.bd_addr.address, bd_addr->address);
1021    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI,
1022                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
1023}
1024
1025static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr )
1026{
1027    int device_type = 0;
1028    char bd_addr_str[18] = {0};
1029
1030    bd2str(bd_addr, &bd_addr_str);
1031    if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type))
1032        return device_type;
1033    return 0;
1034}
1035
1036extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params);
1037
1038static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params)
1039{
1040    return btif_gattc_test_command_impl(command, params);
1041}
1042
1043
1044const btgatt_client_interface_t btgattClientInterface = {
1045    btif_gattc_register_app,
1046    btif_gattc_unregister_app,
1047    btif_gattc_scan,
1048    btif_gattc_open,
1049    btif_gattc_close,
1050    btif_gattc_refresh,
1051    btif_gattc_search_service,
1052    btif_gattc_get_included_service,
1053    btif_gattc_get_characteristic,
1054    btif_gattc_get_descriptor,
1055    btif_gattc_read_char,
1056    btif_gattc_write_char,
1057    btif_gattc_read_char_descr,
1058    btif_gattc_write_char_descr,
1059    btif_gattc_execute_write,
1060    btif_gattc_reg_for_notification,
1061    btif_gattc_dereg_for_notification,
1062    btif_gattc_read_remote_rssi,
1063    btif_gattc_get_device_type,
1064    btif_gattc_test_command
1065};
1066
1067#endif
1068