1 /******************************************************************************
3 * Copyright 2003-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This is the private file for the file transfer client (FTC).
23 ******************************************************************************/
24 #ifndef BTA_GATTC_INT_H
25 #define BTA_GATTC_INT_H
27 #include "bt_target.h"
29 #include "bta_gatt_api.h"
31 #include "database_builder.h"
32 #include "osi/include/fixed_queue.h"
34 #include "bt_common.h"
36 #include <base/logging.h>
37 #include <base/strings/stringprintf.h>
38 #include "types/bt_transport.h"
40 /*****************************************************************************
41 * Constants and data types
42 ****************************************************************************/
44 BTA_GATTC_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_GATTC),
45 BTA_GATTC_INT_OPEN_FAIL_EVT,
46 BTA_GATTC_API_CANCEL_OPEN_EVT,
47 BTA_GATTC_INT_CANCEL_OPEN_OK_EVT,
49 BTA_GATTC_API_READ_EVT,
50 BTA_GATTC_API_WRITE_EVT,
51 BTA_GATTC_API_EXEC_EVT,
52 BTA_GATTC_API_CFG_MTU_EVT,
54 BTA_GATTC_API_CLOSE_EVT,
56 BTA_GATTC_API_SEARCH_EVT,
57 BTA_GATTC_API_CONFIRM_EVT,
58 BTA_GATTC_API_READ_MULTI_EVT,
60 BTA_GATTC_INT_CONN_EVT,
61 BTA_GATTC_INT_DISCOVER_EVT,
62 BTA_GATTC_DISCOVER_CMPL_EVT,
63 BTA_GATTC_OP_CMPL_EVT,
64 BTA_GATTC_INT_DISCONN_EVT
66 typedef uint16_t tBTA_GATTC_INT_EVT;
68 #define BTA_GATTC_SERVICE_CHANGED_LEN 4
70 /* max client application GATTC can support */
71 #ifndef BTA_GATTC_CL_MAX
72 #define BTA_GATTC_CL_MAX 32
75 /* max known devices GATTC can support in Bluetooth spec */
76 #ifndef BTA_GATTC_KNOWN_SR_MAX
77 #define BTA_GATTC_KNOWN_SR_MAX 255
80 #define BTA_GATTC_CONN_MAX GATT_MAX_PHY_CHANNEL
82 #ifndef BTA_GATTC_CLCB_MAX
83 #define BTA_GATTC_CLCB_MAX GATT_CL_MAX_LCB
86 #define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE
88 /* internal strucutre for GATTC register API */
91 RawAddress remote_bda;
94 tBT_TRANSPORT transport;
95 uint8_t initiating_phys;
97 } tBTA_GATTC_API_OPEN;
99 typedef tBTA_GATTC_API_OPEN tBTA_GATTC_API_CANCEL_OPEN;
103 tGATT_AUTH_REQ auth_req;
105 // read by handle data
109 bluetooth::Uuid uuid;
113 tBTA_GATTC_EVT cmpl_evt;
114 GATT_READ_OP_CB read_cb;
116 } tBTA_GATTC_API_READ;
120 tGATT_AUTH_REQ auth_req;
122 tGATT_WRITE_TYPE write_type;
126 GATT_WRITE_OP_CB write_cb;
128 } tBTA_GATTC_API_WRITE;
133 } tBTA_GATTC_API_EXEC;
138 } tBTA_GATTC_API_CONFIRM;
140 typedef tGATT_CL_COMPLETE tBTA_GATTC_CMPL;
146 tBTA_GATTC_CMPL* p_cmpl;
147 } tBTA_GATTC_OP_CMPL;
151 bluetooth::Uuid* p_srvc_uuid;
152 } tBTA_GATTC_API_SEARCH;
156 tGATT_AUTH_REQ auth_req;
158 uint16_t handles[GATT_MAX_READ_MULTI_HANDLES];
159 } tBTA_GATTC_API_READ_MULTI;
164 } tBTA_GATTC_API_CFG_MTU;
168 RawAddress remote_bda;
171 tBT_TRANSPORT transport;
172 tGATT_DISCONN_REASON reason;
173 } tBTA_GATTC_INT_CONN;
177 tBTA_GATTC_API_OPEN api_conn;
178 tBTA_GATTC_API_CANCEL_OPEN api_cancel_conn;
179 tBTA_GATTC_API_READ api_read;
180 tBTA_GATTC_API_SEARCH api_search;
181 tBTA_GATTC_API_WRITE api_write;
182 tBTA_GATTC_API_CONFIRM api_confirm;
183 tBTA_GATTC_API_EXEC api_exec;
184 tBTA_GATTC_API_READ_MULTI api_read_multi;
185 tBTA_GATTC_API_CFG_MTU api_mtu;
186 tBTA_GATTC_OP_CMPL op_cmpl;
187 tBTA_GATTC_INT_CONN int_conn;
191 BTA_GATTC_IDLE_ST = 0, /* Idle */
192 BTA_GATTC_W4_CONN_ST, /* Wait for connection - (optional) */
193 BTA_GATTC_CONN_ST, /* connected state */
194 BTA_GATTC_DISCOVER_ST /* discover is in progress */
196 typedef uint8_t tBTA_GATTC_STATE;
200 RawAddress server_bda;
203 #define BTA_GATTC_SERV_IDLE 0
204 #define BTA_GATTC_SERV_LOAD 1
205 #define BTA_GATTC_SERV_SAVE 2
206 #define BTA_GATTC_SERV_DISC 3
207 #define BTA_GATTC_SERV_DISC_ACT 4
211 gatt::Database gatt_database;
212 uint8_t update_count; /* indication received */
213 uint8_t num_clcb; /* number of associated CLCB */
215 gatt::DatabaseBuilder pending_discovery;
217 /* used only during service discovery, when reading Extended Characteristic
219 bool read_multiple_not_supported;
221 uint8_t srvc_hdl_chg; /* service handle change indication pending */
222 bool srvc_hdl_db_hash; /* read db hash pending */
223 uint8_t srvc_disc_count; /* current discovery retry count */
224 uint16_t attr_index; /* cahce NV saving/loading attribute index */
229 #ifndef BTA_GATTC_NOTIF_REG_MAX
230 #define BTA_GATTC_NOTIF_REG_MAX 64
235 RawAddress remote_bda;
237 } tBTA_GATTC_NOTIF_REG;
240 tBTA_GATTC_CBACK* p_cback;
242 tGATT_IF client_if; /* client interface with BTE stack for this application */
243 uint8_t num_clcb; /* number of associated CLCB */
245 bluetooth::Uuid app_uuid;
246 tBTA_GATTC_NOTIF_REG notif_reg[BTA_GATTC_NOTIF_REG_MAX];
249 /* client channel is a mapping between a BTA client(cl_id) and a remote BD
252 uint16_t bta_conn_id; /* client channel ID, unique for clcb */
254 tBT_TRANSPORT transport; /* channel transport */
255 tBTA_GATTC_RCB* p_rcb; /* pointer to the registration CB */
256 tBTA_GATTC_SERV* p_srcb; /* server cache CB */
257 tBTA_GATTC_DATA* p_q_cmd; /* command in queue waiting for execution */
259 // request during discover state
260 #define BTA_GATTC_DISCOVER_REQ_NONE 0
261 #define BTA_GATTC_DISCOVER_REQ_READ_EXT_PROP_DESC 1
262 #define BTA_GATTC_DISCOVER_REQ_READ_DB_HASH 2
264 uint8_t request_during_discovery; /* request during discover state */
266 #define BTA_GATTC_NO_SCHEDULE 0
267 #define BTA_GATTC_DISC_WAITING 0x01
268 #define BTA_GATTC_REQ_WAITING 0x10
270 uint8_t auto_update; /* auto update is waiting */
273 tBTA_GATTC_STATE state;
278 /* back ground connection tracking information */
279 #if GATT_MAX_APPS <= 8
280 typedef uint8_t tBTA_GATTC_CIF_MASK;
281 #elif GATT_MAX_APPS <= 16
282 typedef uint16_t tBTA_GATTC_CIF_MASK;
283 #elif GATT_MAX_APPS <= 32
284 typedef uint32_t tBTA_GATTC_CIF_MASK;
289 RawAddress remote_bda;
290 tBTA_GATTC_CIF_MASK cif_mask;
296 RawAddress remote_bda;
300 BTA_GATTC_STATE_DISABLED,
301 BTA_GATTC_STATE_ENABLING,
302 BTA_GATTC_STATE_ENABLED,
303 BTA_GATTC_STATE_DISABLING
309 tBTA_GATTC_CONN conn_track[BTA_GATTC_CONN_MAX];
310 tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX];
311 tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX];
313 tBTA_GATTC_CLCB clcb[BTA_GATTC_CLCB_MAX];
314 tBTA_GATTC_SERV known_server[BTA_GATTC_KNOWN_SR_MAX];
317 /*****************************************************************************
319 ****************************************************************************/
321 /* GATTC control block */
322 extern tBTA_GATTC_CB bta_gattc_cb;
324 /*****************************************************************************
325 * Function prototypes
326 ****************************************************************************/
327 extern bool bta_gattc_hdl_event(BT_HDR* p_msg);
328 extern bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event,
329 tBTA_GATTC_DATA* p_data);
331 /* function processed outside SM */
332 extern void bta_gattc_disable();
333 extern void bta_gattc_register(const bluetooth::Uuid& app_uuid,
334 tBTA_GATTC_CBACK* p_data,
335 BtaAppRegisterCallback cb, bool eatt_support);
336 extern void bta_gattc_process_api_open(tBTA_GATTC_DATA* p_msg);
337 extern void bta_gattc_process_api_open_cancel(tBTA_GATTC_DATA* p_msg);
338 extern void bta_gattc_deregister(tBTA_GATTC_RCB* p_clreg);
340 /* function within state machine */
341 extern void bta_gattc_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
342 extern void bta_gattc_open_fail(tBTA_GATTC_CLCB* p_clcb,
343 tBTA_GATTC_DATA* p_data);
344 extern void bta_gattc_open_error(tBTA_GATTC_CLCB* p_clcb,
345 tBTA_GATTC_DATA* p_data);
347 extern void bta_gattc_cancel_open(tBTA_GATTC_CLCB* p_clcb,
348 tBTA_GATTC_DATA* p_data);
349 extern void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB* p_clcb,
350 tBTA_GATTC_DATA* p_data);
351 extern void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB* p_clcb,
352 tBTA_GATTC_DATA* p_data);
354 extern void bta_gattc_conn(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
356 extern void bta_gattc_close(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
357 extern void bta_gattc_close_fail(tBTA_GATTC_CLCB* p_clcb,
358 tBTA_GATTC_DATA* p_data);
359 extern void bta_gattc_disc_close(tBTA_GATTC_CLCB* p_clcb,
360 tBTA_GATTC_DATA* p_data);
362 extern void bta_gattc_start_discover(tBTA_GATTC_CLCB* p_clcb,
363 tBTA_GATTC_DATA* p_data);
364 extern void bta_gattc_start_discover_internal(tBTA_GATTC_CLCB* p_clcb);
365 extern void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB* p_clcb,
366 tBTA_GATTC_DATA* p_data);
367 extern void bta_gattc_read(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
368 extern void bta_gattc_write(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
369 extern void bta_gattc_op_cmpl(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
370 extern void bta_gattc_q_cmd(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
371 extern void bta_gattc_search(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
372 extern void bta_gattc_fail(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
373 extern void bta_gattc_confirm(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
374 extern void bta_gattc_execute(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
375 extern void bta_gattc_read_multi(tBTA_GATTC_CLCB* p_clcb,
376 tBTA_GATTC_DATA* p_data);
377 extern void bta_gattc_ci_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
378 extern void bta_gattc_ci_close(tBTA_GATTC_CLCB* p_clcb,
379 tBTA_GATTC_DATA* p_data);
380 extern void bta_gattc_op_cmpl_during_discovery(tBTA_GATTC_CLCB* p_clcb,
381 tBTA_GATTC_DATA* p_data);
382 extern void bta_gattc_restart_discover(tBTA_GATTC_CLCB* p_clcb,
383 tBTA_GATTC_DATA* p_msg);
384 extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN* p_data,
385 tBTA_GATTC_RCB* p_clreg);
386 extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN* p_data);
387 extern void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg,
389 const RawAddress& remote_bda,
390 uint16_t conn_id, tBT_TRANSPORT transport,
392 extern void bta_gattc_process_api_refresh(const RawAddress& remote_bda);
393 extern void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
394 extern void bta_gattc_listen(tBTA_GATTC_DATA* p_msg);
395 extern void bta_gattc_broadcast(tBTA_GATTC_DATA* p_msg);
397 /* utility functions */
398 extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if,
399 const RawAddress& remote_bda,
400 tBT_TRANSPORT transport);
401 extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(uint16_t conn_id);
402 extern tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tGATT_IF client_if,
403 const RawAddress& remote_bda,
404 tBT_TRANSPORT transport);
405 extern void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb);
406 extern tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tGATT_IF client_if,
407 const RawAddress& remote_bda,
408 tBT_TRANSPORT transport);
409 extern tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if);
410 extern tBTA_GATTC_SERV* bta_gattc_find_srcb(const RawAddress& bda);
411 extern tBTA_GATTC_SERV* bta_gattc_srcb_alloc(const RawAddress& bda);
412 extern tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(uint16_t conn_id);
413 extern tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg);
414 extern tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg);
416 extern bool bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
418 extern bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg,
419 tBTA_GATTC_SERV* p_srcb,
420 tBTA_GATTC_NOTIFY* p_notify);
421 extern bool bta_gattc_mark_bg_conn(tGATT_IF client_if,
422 const RawAddress& remote_bda, bool add);
423 extern bool bta_gattc_check_bg_conn(tGATT_IF client_if,
424 const RawAddress& remote_bda, uint8_t role);
425 extern uint8_t bta_gattc_num_reg_app(void);
426 extern void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb,
428 uint16_t start_handle,
429 uint16_t end_handle);
430 extern tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(const RawAddress& bda);
431 extern bool bta_gattc_is_robust_caching_enabled();
433 /* discovery functions */
434 extern void bta_gattc_disc_res_cback(uint16_t conn_id,
435 tGATT_DISC_TYPE disc_type,
436 tGATT_DISC_RES* p_data);
437 extern void bta_gattc_disc_cmpl_cback(uint16_t conn_id,
438 tGATT_DISC_TYPE disc_type,
439 tGATT_STATUS status);
440 extern tGATT_STATUS bta_gattc_discover_pri_service(uint16_t conn_id,
441 tBTA_GATTC_SERV* p_server_cb,
442 tGATT_DISC_TYPE disc_type);
443 extern void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb,
444 bluetooth::Uuid* p_uuid);
445 extern const std::list<gatt::Service>* bta_gattc_get_services(uint16_t conn_id);
446 extern const gatt::Service* bta_gattc_get_service_for_handle(uint16_t conn_id,
448 const gatt::Characteristic* bta_gattc_get_characteristic_srcb(
449 tBTA_GATTC_SERV* p_srcb, uint16_t handle);
450 extern const gatt::Service* bta_gattc_get_service_for_handle_srcb(
451 tBTA_GATTC_SERV* p_srcb, uint16_t handle);
452 extern const gatt::Characteristic* bta_gattc_get_characteristic(
453 uint16_t conn_id, uint16_t handle);
454 extern const gatt::Descriptor* bta_gattc_get_descriptor(uint16_t conn_id,
456 extern const gatt::Characteristic* bta_gattc_get_owning_characteristic(
457 uint16_t conn_id, uint16_t handle);
458 extern void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle,
459 uint16_t end_handle, btgatt_db_element_t** db,
461 extern void bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb);
462 extern void bta_gattc_reset_discover_st(tBTA_GATTC_SERV* p_srcb,
463 tGATT_STATUS status);
465 extern tBTA_GATTC_CONN* bta_gattc_conn_alloc(const RawAddress& remote_bda);
466 extern tBTA_GATTC_CONN* bta_gattc_conn_find(const RawAddress& remote_bda);
467 extern tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(const RawAddress& remote_bda);
468 extern bool bta_gattc_conn_dealloc(const RawAddress& remote_bda);
470 extern bool bta_gattc_cache_load(tBTA_GATTC_SERV* p_srcb);
471 extern void bta_gattc_cache_reset(const RawAddress& server_bda);
473 extern bool bta_gattc_read_db_hash(tBTA_GATTC_CLCB* p_clcb);
475 #endif /* BTA_GATTC_INT_H */