bta_gattc_int.h revision 221e9bf6c9e9f974efd92ea4f054dbdba248d2db
1/****************************************************************************** 2 * 3 * Copyright (C) 2003-2012 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 * This is the private file for the file transfer client (FTC). 22 * 23 ******************************************************************************/ 24#ifndef BTA_GATTC_INT_H 25#define BTA_GATTC_INT_H 26 27#include "bt_target.h" 28 29#include "bta_gatt_api.h" 30#include "bta_sys.h" 31#include "osi/include/fixed_queue.h" 32 33#include "bt_common.h" 34 35/***************************************************************************** 36 * Constants and data types 37 ****************************************************************************/ 38enum { 39 BTA_GATTC_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_GATTC), 40 BTA_GATTC_INT_OPEN_FAIL_EVT, 41 BTA_GATTC_API_CANCEL_OPEN_EVT, 42 BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, 43 44 BTA_GATTC_API_READ_EVT, 45 BTA_GATTC_API_WRITE_EVT, 46 BTA_GATTC_API_EXEC_EVT, 47 BTA_GATTC_API_CFG_MTU_EVT, 48 49 BTA_GATTC_API_CLOSE_EVT, 50 51 BTA_GATTC_API_SEARCH_EVT, 52 BTA_GATTC_API_CONFIRM_EVT, 53 BTA_GATTC_API_READ_MULTI_EVT, 54 BTA_GATTC_API_REFRESH_EVT, 55 56 BTA_GATTC_INT_CONN_EVT, 57 BTA_GATTC_INT_DISCOVER_EVT, 58 BTA_GATTC_DISCOVER_CMPL_EVT, 59 BTA_GATTC_OP_CMPL_EVT, 60 BTA_GATTC_INT_DISCONN_EVT, 61 62 BTA_GATTC_INT_START_IF_EVT, 63 BTA_GATTC_API_DEREG_EVT, 64 BTA_GATTC_API_DISABLE_EVT, 65 BTA_GATTC_ENC_CMPL_EVT 66}; 67typedef uint16_t tBTA_GATTC_INT_EVT; 68 69#define BTA_GATTC_SERVICE_CHANGED_LEN 4 70 71/* max client application GATTC can support */ 72#ifndef BTA_GATTC_CL_MAX 73#define BTA_GATTC_CL_MAX 32 74#endif 75 76/* max known devices GATTC can support */ 77#ifndef BTA_GATTC_KNOWN_SR_MAX 78#define BTA_GATTC_KNOWN_SR_MAX 10 79#endif 80 81#define BTA_GATTC_CONN_MAX GATT_MAX_PHY_CHANNEL 82 83#ifndef BTA_GATTC_CLCB_MAX 84#define BTA_GATTC_CLCB_MAX GATT_CL_MAX_LCB 85#endif 86 87#define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE 88 89/* internal strucutre for GATTC register API */ 90typedef struct { 91 BT_HDR hdr; 92 tBTA_GATTC_IF client_if; 93} tBTA_GATTC_INT_START_IF; 94 95typedef tBTA_GATTC_INT_START_IF tBTA_GATTC_API_DEREG; 96typedef tBTA_GATTC_INT_START_IF tBTA_GATTC_INT_DEREG; 97 98typedef struct { 99 BT_HDR hdr; 100 BD_ADDR remote_bda; 101 tBTA_GATTC_IF client_if; 102 bool is_direct; 103 tBTA_TRANSPORT transport; 104} tBTA_GATTC_API_OPEN; 105 106typedef tBTA_GATTC_API_OPEN tBTA_GATTC_API_CANCEL_OPEN; 107 108typedef struct { 109 BT_HDR hdr; 110 tBTA_GATT_AUTH_REQ auth_req; 111 uint16_t handle; 112 tBTA_GATTC_EVT cmpl_evt; 113 GATT_READ_OP_CB read_cb; 114 void* read_cb_data; 115} tBTA_GATTC_API_READ; 116 117typedef struct { 118 BT_HDR hdr; 119 tBTA_GATT_AUTH_REQ auth_req; 120 uint16_t handle; 121 tBTA_GATTC_WRITE_TYPE write_type; 122 uint16_t offset; 123 uint16_t len; 124 uint8_t* p_value; 125 GATT_WRITE_OP_CB write_cb; 126 void* write_cb_data; 127} tBTA_GATTC_API_WRITE; 128 129typedef struct { 130 BT_HDR hdr; 131 bool is_execute; 132} tBTA_GATTC_API_EXEC; 133 134typedef struct { 135 BT_HDR hdr; 136 uint16_t handle; 137} tBTA_GATTC_API_CONFIRM; 138 139typedef tGATT_CL_COMPLETE tBTA_GATTC_CMPL; 140 141typedef struct { 142 BT_HDR hdr; 143 uint8_t op_code; 144 tGATT_STATUS status; 145 tBTA_GATTC_CMPL* p_cmpl; 146} tBTA_GATTC_OP_CMPL; 147 148typedef struct { 149 BT_HDR hdr; 150 tBT_UUID* p_srvc_uuid; 151} tBTA_GATTC_API_SEARCH; 152 153typedef struct { 154 BT_HDR hdr; 155 tBTA_GATT_AUTH_REQ auth_req; 156 uint8_t num_attr; 157 uint16_t handles[GATT_MAX_READ_MULTI_HANDLES]; 158} tBTA_GATTC_API_READ_MULTI; 159 160typedef struct { 161 BT_HDR hdr; 162 uint16_t mtu; 163} tBTA_GATTC_API_CFG_MTU; 164 165typedef struct { 166 BT_HDR hdr; 167 BD_ADDR remote_bda; 168 tBTA_GATTC_IF client_if; 169 uint8_t role; 170 tBT_TRANSPORT transport; 171 tGATT_DISCONN_REASON reason; 172} tBTA_GATTC_INT_CONN; 173 174typedef struct { 175 BT_HDR hdr; 176 BD_ADDR remote_bda; 177 tBTA_GATTC_IF client_if; 178} tBTA_GATTC_ENC_CMPL; 179 180typedef union { 181 BT_HDR hdr; 182 tBTA_GATTC_API_DEREG api_dereg; 183 tBTA_GATTC_API_OPEN api_conn; 184 tBTA_GATTC_API_CANCEL_OPEN api_cancel_conn; 185 tBTA_GATTC_API_READ api_read; 186 tBTA_GATTC_API_SEARCH api_search; 187 tBTA_GATTC_API_WRITE api_write; 188 tBTA_GATTC_API_CONFIRM api_confirm; 189 tBTA_GATTC_API_EXEC api_exec; 190 tBTA_GATTC_API_READ_MULTI api_read_multi; 191 tBTA_GATTC_API_CFG_MTU api_mtu; 192 tBTA_GATTC_OP_CMPL op_cmpl; 193 tBTA_GATTC_INT_CONN int_conn; 194 tBTA_GATTC_ENC_CMPL enc_cmpl; 195 196 tBTA_GATTC_INT_START_IF int_start_if; 197 tBTA_GATTC_INT_DEREG int_dereg; 198 199} tBTA_GATTC_DATA; 200 201/* GATT server cache on the client */ 202 203typedef struct { 204 tBT_UUID uuid; 205 uint16_t s_handle; 206 uint16_t e_handle; 207 // this field is set only for characteristic 208 uint16_t char_decl_handle; 209 bool is_primary; 210 tBTA_GATT_CHAR_PROP property; 211} tBTA_GATTC_ATTR_REC; 212 213#define BTA_GATTC_MAX_CACHE_CHAR 40 214#define BTA_GATTC_ATTR_LIST_SIZE \ 215 (BTA_GATTC_MAX_CACHE_CHAR * sizeof(tBTA_GATTC_ATTR_REC)) 216 217#ifndef BTA_GATTC_CACHE_SRVR_SIZE 218#define BTA_GATTC_CACHE_SRVR_SIZE 600 219#endif 220 221enum { 222 BTA_GATTC_IDLE_ST = 0, /* Idle */ 223 BTA_GATTC_W4_CONN_ST, /* Wait for connection - (optional) */ 224 BTA_GATTC_CONN_ST, /* connected state */ 225 BTA_GATTC_DISCOVER_ST /* discover is in progress */ 226}; 227typedef uint8_t tBTA_GATTC_STATE; 228 229typedef struct { 230 bool in_use; 231 BD_ADDR server_bda; 232 bool connected; 233 234#define BTA_GATTC_SERV_IDLE 0 235#define BTA_GATTC_SERV_LOAD 1 236#define BTA_GATTC_SERV_SAVE 2 237#define BTA_GATTC_SERV_DISC 3 238#define BTA_GATTC_SERV_DISC_ACT 4 239 240 uint8_t state; 241 242 list_t* p_srvc_cache; /* list of tBTA_GATTC_SERVICE */ 243 uint8_t update_count; /* indication received */ 244 uint8_t num_clcb; /* number of associated CLCB */ 245 246 tBTA_GATTC_ATTR_REC* p_srvc_list; 247 uint8_t cur_srvc_idx; 248 uint8_t cur_char_idx; 249 uint8_t next_avail_idx; 250 uint8_t total_srvc; 251 uint8_t total_char; 252 253 uint8_t srvc_hdl_chg; /* service handle change indication pending */ 254 uint16_t attr_index; /* cahce NV saving/loading attribute index */ 255 256 uint16_t mtu; 257} tBTA_GATTC_SERV; 258 259#ifndef BTA_GATTC_NOTIF_REG_MAX 260#define BTA_GATTC_NOTIF_REG_MAX 15 261#endif 262 263typedef struct { 264 bool in_use; 265 BD_ADDR remote_bda; 266 uint16_t handle; 267} tBTA_GATTC_NOTIF_REG; 268 269typedef struct { 270 tBTA_GATTC_CBACK* p_cback; 271 bool in_use; 272 tBTA_GATTC_IF 273 client_if; /* client interface with BTE stack for this application */ 274 uint8_t num_clcb; /* number of associated CLCB */ 275 bool dereg_pending; 276 tBT_UUID app_uuid; 277 tBTA_GATTC_NOTIF_REG notif_reg[BTA_GATTC_NOTIF_REG_MAX]; 278} tBTA_GATTC_RCB; 279 280/* client channel is a mapping between a BTA client(cl_id) and a remote BD 281 * address */ 282typedef struct { 283 uint16_t bta_conn_id; /* client channel ID, unique for clcb */ 284 BD_ADDR bda; 285 tBTA_TRANSPORT transport; /* channel transport */ 286 tBTA_GATTC_RCB* p_rcb; /* pointer to the registration CB */ 287 tBTA_GATTC_SERV* p_srcb; /* server cache CB */ 288 tBTA_GATTC_DATA* p_q_cmd; /* command in queue waiting for execution */ 289 290#define BTA_GATTC_NO_SCHEDULE 0 291#define BTA_GATTC_DISC_WAITING 0x01 292#define BTA_GATTC_REQ_WAITING 0x10 293 294 uint8_t auto_update; /* auto update is waiting */ 295 bool disc_active; 296 bool in_use; 297 tBTA_GATTC_STATE state; 298 tBTA_GATT_STATUS status; 299 uint16_t reason; 300} tBTA_GATTC_CLCB; 301 302/* back ground connection tracking information */ 303#if GATT_MAX_APPS <= 8 304typedef uint8_t tBTA_GATTC_CIF_MASK; 305#elif GATT_MAX_APPS <= 16 306typedef uint16_t tBTA_GATTC_CIF_MASK; 307#elif GATT_MAX_APPS <= 32 308typedef uint32_t tBTA_GATTC_CIF_MASK; 309#endif 310 311typedef struct { 312 bool in_use; 313 BD_ADDR remote_bda; 314 tBTA_GATTC_CIF_MASK cif_mask; 315 316} tBTA_GATTC_BG_TCK; 317 318typedef struct { 319 bool in_use; 320 BD_ADDR remote_bda; 321} tBTA_GATTC_CONN; 322 323enum { 324 BTA_GATTC_STATE_DISABLED, 325 BTA_GATTC_STATE_ENABLING, 326 BTA_GATTC_STATE_ENABLED, 327 BTA_GATTC_STATE_DISABLING 328}; 329 330typedef struct { 331 uint8_t state; 332 333 tBTA_GATTC_CONN conn_track[BTA_GATTC_CONN_MAX]; 334 tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX]; 335 tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX]; 336 337 tBTA_GATTC_CLCB clcb[BTA_GATTC_CLCB_MAX]; 338 tBTA_GATTC_SERV known_server[BTA_GATTC_KNOWN_SR_MAX]; 339} tBTA_GATTC_CB; 340 341/***************************************************************************** 342 * Global data 343 ****************************************************************************/ 344 345/* GATTC control block */ 346extern tBTA_GATTC_CB bta_gattc_cb; 347 348/***************************************************************************** 349 * Function prototypes 350 ****************************************************************************/ 351extern bool bta_gattc_hdl_event(BT_HDR* p_msg); 352extern bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event, 353 tBTA_GATTC_DATA* p_data); 354 355/* function processed outside SM */ 356extern void bta_gattc_disable(); 357extern void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_data, 358 BtaAppRegisterCallback cb); 359extern void bta_gattc_start_if(tBTA_GATTC_DATA* p_data); 360extern void bta_gattc_process_api_open(tBTA_GATTC_DATA* p_msg); 361extern void bta_gattc_process_api_open_cancel(tBTA_GATTC_DATA* p_msg); 362extern void bta_gattc_deregister(tBTA_GATTC_RCB* p_clreg); 363extern void bta_gattc_process_enc_cmpl(tBTA_GATTC_DATA* p_msg); 364 365/* function within state machine */ 366extern void bta_gattc_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 367extern void bta_gattc_open_fail(tBTA_GATTC_CLCB* p_clcb, 368 tBTA_GATTC_DATA* p_data); 369extern void bta_gattc_open_error(tBTA_GATTC_CLCB* p_clcb, 370 tBTA_GATTC_DATA* p_data); 371 372extern void bta_gattc_cancel_open(tBTA_GATTC_CLCB* p_clcb, 373 tBTA_GATTC_DATA* p_data); 374extern void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB* p_clcb, 375 tBTA_GATTC_DATA* p_data); 376extern void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB* p_clcb, 377 tBTA_GATTC_DATA* p_data); 378 379extern void bta_gattc_conn(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 380 381extern void bta_gattc_close(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 382extern void bta_gattc_close_fail(tBTA_GATTC_CLCB* p_clcb, 383 tBTA_GATTC_DATA* p_data); 384extern void bta_gattc_disc_close(tBTA_GATTC_CLCB* p_clcb, 385 tBTA_GATTC_DATA* p_data); 386 387extern void bta_gattc_start_discover(tBTA_GATTC_CLCB* p_clcb, 388 tBTA_GATTC_DATA* p_data); 389extern void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB* p_clcb, 390 tBTA_GATTC_DATA* p_data); 391extern void bta_gattc_read(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 392extern void bta_gattc_write(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 393extern void bta_gattc_op_cmpl(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 394extern void bta_gattc_q_cmd(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 395extern void bta_gattc_search(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 396extern void bta_gattc_fail(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 397extern void bta_gattc_confirm(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 398extern void bta_gattc_execute(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 399extern void bta_gattc_read_multi(tBTA_GATTC_CLCB* p_clcb, 400 tBTA_GATTC_DATA* p_data); 401extern void bta_gattc_ci_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 402extern void bta_gattc_ci_close(tBTA_GATTC_CLCB* p_clcb, 403 tBTA_GATTC_DATA* p_data); 404extern void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB* p_clcb, 405 tBTA_GATTC_DATA* p_data); 406extern void bta_gattc_restart_discover(tBTA_GATTC_CLCB* p_clcb, 407 tBTA_GATTC_DATA* p_msg); 408extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN* p_data, 409 tBTA_GATTC_RCB* p_clreg); 410extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN* p_data); 411extern void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg, 412 tBTA_GATT_STATUS status, 413 BD_ADDR remote_bda, uint16_t conn_id, 414 tBTA_TRANSPORT transport, uint16_t mtu); 415extern void bta_gattc_process_api_refresh(tBTA_GATTC_DATA* p_msg); 416extern void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 417extern void bta_gattc_listen(tBTA_GATTC_DATA* p_msg); 418extern void bta_gattc_broadcast(tBTA_GATTC_DATA* p_msg); 419 420/* utility functions */ 421extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if, 422 BD_ADDR remote_bda, 423 tBTA_TRANSPORT transport); 424extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(uint16_t conn_id); 425extern tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, 426 BD_ADDR remote_bda, 427 tBTA_TRANSPORT transport); 428extern void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb); 429extern tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, 430 BD_ADDR remote_bda, 431 tBTA_TRANSPORT transport); 432extern tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if); 433extern tBTA_GATTC_SERV* bta_gattc_find_srcb(BD_ADDR bda); 434extern tBTA_GATTC_SERV* bta_gattc_srcb_alloc(BD_ADDR bda); 435extern tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(uint16_t conn_id); 436extern tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg); 437extern tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg); 438 439extern bool bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); 440 441extern bool bta_gattc_uuid_compare(const tBT_UUID* p_src, const tBT_UUID* p_tar, 442 bool is_precise); 443extern bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg, 444 tBTA_GATTC_SERV* p_srcb, 445 tBTA_GATTC_NOTIFY* p_notify); 446extern bool bta_gattc_mark_bg_conn(tBTA_GATTC_IF client_if, 447 BD_ADDR_PTR remote_bda, bool add); 448extern bool bta_gattc_check_bg_conn(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, 449 uint8_t role); 450extern uint8_t bta_gattc_num_reg_app(void); 451extern void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb, 452 uint16_t conn_id, 453 uint16_t start_handle, 454 uint16_t end_handle); 455extern tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(BD_ADDR bda); 456 457/* discovery functions */ 458extern void bta_gattc_disc_res_cback(uint16_t conn_id, 459 tGATT_DISC_TYPE disc_type, 460 tGATT_DISC_RES* p_data); 461extern void bta_gattc_disc_cmpl_cback(uint16_t conn_id, 462 tGATT_DISC_TYPE disc_type, 463 tGATT_STATUS status); 464extern tBTA_GATT_STATUS bta_gattc_discover_procedure( 465 uint16_t conn_id, tBTA_GATTC_SERV* p_server_cb, uint8_t disc_type); 466extern tBTA_GATT_STATUS bta_gattc_discover_pri_service( 467 uint16_t conn_id, tBTA_GATTC_SERV* p_server_cb, uint8_t disc_type); 468extern void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, tBT_UUID* p_uuid); 469extern const list_t* bta_gattc_get_services(uint16_t conn_id); 470extern const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle( 471 uint16_t conn_id, uint16_t handle); 472tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb( 473 tBTA_GATTC_SERV* p_srcb, uint16_t handle); 474extern tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic(uint16_t conn_id, 475 uint16_t handle); 476extern tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(uint16_t conn_id, 477 uint16_t handle); 478extern void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle, 479 uint16_t end_handle, btgatt_db_element_t** db, 480 int* count); 481extern tBTA_GATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb); 482extern void bta_gattc_rebuild_cache(tBTA_GATTC_SERV* p_srcv, uint16_t num_attr, 483 tBTA_GATTC_NV_ATTR* attr); 484extern void bta_gattc_cache_save(tBTA_GATTC_SERV* p_srvc_cb, uint16_t conn_id); 485extern void bta_gattc_reset_discover_st(tBTA_GATTC_SERV* p_srcb, 486 tBTA_GATT_STATUS status); 487 488extern tBTA_GATTC_CONN* bta_gattc_conn_alloc(BD_ADDR remote_bda); 489extern tBTA_GATTC_CONN* bta_gattc_conn_find(BD_ADDR remote_bda); 490extern tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(BD_ADDR remote_bda); 491extern bool bta_gattc_conn_dealloc(BD_ADDR remote_bda); 492 493extern bool bta_gattc_cache_load(tBTA_GATTC_CLCB* p_clcb); 494extern void bta_gattc_cache_reset(BD_ADDR server_bda); 495 496#endif /* BTA_GATTC_INT_H */ 497