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