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