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
29 #include "bt_target.h" // Must be first to define build configuration
31 #include "bta/gatt/database.h"
32 #include "bta/gatt/database_builder.h"
33 #include "bta/include/bta_gatt_api.h"
34 #include "bta/sys/bta_sys.h"
35 #include "stack/include/gatt_api.h"
36 #include "types/bt_transport.h"
38 /*****************************************************************************
39 * Constants and data types
40 ****************************************************************************/
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,
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,
52 BTA_GATTC_API_CLOSE_EVT,
54 BTA_GATTC_API_SEARCH_EVT,
55 BTA_GATTC_API_CONFIRM_EVT,
56 BTA_GATTC_API_READ_MULTI_EVT,
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
64 typedef uint16_t tBTA_GATTC_INT_EVT;
66 #define BTA_GATTC_SERVICE_CHANGED_LEN 4
68 /* max client application GATTC can support */
69 #ifndef BTA_GATTC_CL_MAX
70 #define BTA_GATTC_CL_MAX 32
73 /* max known devices GATTC can support in Bluetooth spec */
74 #ifndef BTA_GATTC_KNOWN_SR_MAX
75 #define BTA_GATTC_KNOWN_SR_MAX 255
78 #define BTA_GATTC_CONN_MAX GATT_MAX_PHY_CHANNEL
80 #ifndef BTA_GATTC_CLCB_MAX
81 #define BTA_GATTC_CLCB_MAX GATT_CL_MAX_LCB
84 #define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE
86 /* internal strucutre for GATTC register API */
89 RawAddress remote_bda;
92 tBT_TRANSPORT transport;
93 uint8_t initiating_phys;
95 } tBTA_GATTC_API_OPEN;
97 typedef tBTA_GATTC_API_OPEN tBTA_GATTC_API_CANCEL_OPEN;
101 tGATT_AUTH_REQ auth_req;
103 // read by handle data
107 bluetooth::Uuid uuid;
111 tBTA_GATTC_EVT cmpl_evt;
112 GATT_READ_OP_CB read_cb;
114 } tBTA_GATTC_API_READ;
118 tGATT_AUTH_REQ auth_req;
120 tGATT_WRITE_TYPE write_type;
124 GATT_WRITE_OP_CB write_cb;
126 } tBTA_GATTC_API_WRITE;
131 } tBTA_GATTC_API_EXEC;
136 } tBTA_GATTC_API_CONFIRM;
138 typedef tGATT_CL_COMPLETE tBTA_GATTC_CMPL;
144 tBTA_GATTC_CMPL* p_cmpl;
145 } tBTA_GATTC_OP_CMPL;
149 bluetooth::Uuid* p_srvc_uuid;
150 } tBTA_GATTC_API_SEARCH;
154 tGATT_AUTH_REQ auth_req;
156 uint16_t handles[GATT_MAX_READ_MULTI_HANDLES];
157 } tBTA_GATTC_API_READ_MULTI;
162 GATT_CONFIGURE_MTU_OP_CB mtu_cb;
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;
277 /* back ground connection tracking information */
278 #if GATT_MAX_APPS <= 8
279 typedef uint8_t tBTA_GATTC_CIF_MASK;
280 #elif GATT_MAX_APPS <= 16
281 typedef uint16_t tBTA_GATTC_CIF_MASK;
282 #elif GATT_MAX_APPS <= 32
283 typedef uint32_t tBTA_GATTC_CIF_MASK;
288 RawAddress remote_bda;
289 tBTA_GATTC_CIF_MASK cif_mask;
295 RawAddress remote_bda;
299 BTA_GATTC_STATE_DISABLED,
300 BTA_GATTC_STATE_ENABLING,
301 BTA_GATTC_STATE_ENABLED,
302 BTA_GATTC_STATE_DISABLING
308 tBTA_GATTC_CONN conn_track[BTA_GATTC_CONN_MAX];
309 tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX];
310 tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX];
312 tBTA_GATTC_CLCB clcb[BTA_GATTC_CLCB_MAX];
313 tBTA_GATTC_SERV known_server[BTA_GATTC_KNOWN_SR_MAX];
316 /*****************************************************************************
318 ****************************************************************************/
320 /* GATTC control block */
321 extern tBTA_GATTC_CB bta_gattc_cb;
323 /*****************************************************************************
324 * Function prototypes
325 ****************************************************************************/
326 extern bool bta_gattc_hdl_event(BT_HDR_RIGID* p_msg);
327 extern bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event,
328 tBTA_GATTC_DATA* p_data);
330 /* function processed outside SM */
331 extern void bta_gattc_disable();
332 extern void bta_gattc_register(const bluetooth::Uuid& app_uuid,
333 tBTA_GATTC_CBACK* p_data,
334 BtaAppRegisterCallback cb, bool eatt_support);
335 extern void bta_gattc_process_api_open(tBTA_GATTC_DATA* p_msg);
336 extern void bta_gattc_process_api_open_cancel(tBTA_GATTC_DATA* p_msg);
337 extern void bta_gattc_deregister(tBTA_GATTC_RCB* p_clreg);
339 /* function within state machine */
340 extern void bta_gattc_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
341 extern void bta_gattc_open_fail(tBTA_GATTC_CLCB* p_clcb,
342 tBTA_GATTC_DATA* p_data);
343 extern void bta_gattc_open_error(tBTA_GATTC_CLCB* p_clcb,
344 tBTA_GATTC_DATA* p_data);
346 extern void bta_gattc_cancel_open(tBTA_GATTC_CLCB* p_clcb,
347 tBTA_GATTC_DATA* p_data);
348 extern void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB* p_clcb,
349 tBTA_GATTC_DATA* p_data);
350 extern void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB* p_clcb,
351 tBTA_GATTC_DATA* p_data);
353 extern void bta_gattc_conn(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
355 extern void bta_gattc_close(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
356 extern void bta_gattc_close_fail(tBTA_GATTC_CLCB* p_clcb,
357 tBTA_GATTC_DATA* p_data);
358 extern void bta_gattc_disc_close(tBTA_GATTC_CLCB* p_clcb,
359 tBTA_GATTC_DATA* p_data);
361 extern void bta_gattc_start_discover(tBTA_GATTC_CLCB* p_clcb,
362 tBTA_GATTC_DATA* p_data);
363 extern void bta_gattc_start_discover_internal(tBTA_GATTC_CLCB* p_clcb);
364 extern void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB* p_clcb,
365 tBTA_GATTC_DATA* p_data);
366 extern void bta_gattc_read(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
367 extern void bta_gattc_write(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
368 extern void bta_gattc_op_cmpl(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
369 extern void bta_gattc_q_cmd(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
370 extern void bta_gattc_search(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
371 extern void bta_gattc_fail(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
372 extern void bta_gattc_confirm(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
373 extern void bta_gattc_execute(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
374 extern void bta_gattc_read_multi(tBTA_GATTC_CLCB* p_clcb,
375 tBTA_GATTC_DATA* p_data);
376 extern void bta_gattc_ci_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
377 extern void bta_gattc_ci_close(tBTA_GATTC_CLCB* p_clcb,
378 tBTA_GATTC_DATA* p_data);
379 extern void bta_gattc_op_cmpl_during_discovery(tBTA_GATTC_CLCB* p_clcb,
380 tBTA_GATTC_DATA* p_data);
381 extern void bta_gattc_restart_discover(tBTA_GATTC_CLCB* p_clcb,
382 tBTA_GATTC_DATA* p_msg);
383 extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN* p_data,
384 tBTA_GATTC_RCB* p_clreg);
385 extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN* p_data);
386 extern void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg,
388 const RawAddress& remote_bda,
389 uint16_t conn_id, tBT_TRANSPORT transport,
391 extern void bta_gattc_process_api_refresh(const RawAddress& remote_bda);
392 extern void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
393 extern void bta_gattc_listen(tBTA_GATTC_DATA* p_msg);
394 extern void bta_gattc_broadcast(tBTA_GATTC_DATA* p_msg);
396 /* utility functions */
397 extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if,
398 const RawAddress& remote_bda,
399 tBT_TRANSPORT transport);
400 extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(uint16_t conn_id);
401 extern tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tGATT_IF client_if,
402 const RawAddress& remote_bda,
403 tBT_TRANSPORT transport);
404 extern void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb);
405 extern tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tGATT_IF client_if,
406 const RawAddress& remote_bda,
407 tBT_TRANSPORT transport);
408 extern tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if);
409 extern tBTA_GATTC_SERV* bta_gattc_find_srcb(const RawAddress& bda);
410 extern tBTA_GATTC_SERV* bta_gattc_srcb_alloc(const RawAddress& bda);
411 extern tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(uint16_t conn_id);
412 extern tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg);
413 extern tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg);
415 extern bool bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
417 extern bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg,
418 tBTA_GATTC_SERV* p_srcb,
419 tBTA_GATTC_NOTIFY* p_notify);
420 extern bool bta_gattc_mark_bg_conn(tGATT_IF client_if,
421 const RawAddress& remote_bda, bool add);
422 extern bool bta_gattc_check_bg_conn(tGATT_IF client_if,
423 const RawAddress& remote_bda, uint8_t role);
424 extern uint8_t bta_gattc_num_reg_app(void);
425 extern void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb,
427 uint16_t start_handle,
428 uint16_t end_handle);
429 extern tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(const RawAddress& bda);
430 extern bool bta_gattc_is_robust_caching_enabled();
432 /* discovery functions */
433 extern void bta_gattc_disc_res_cback(uint16_t conn_id,
434 tGATT_DISC_TYPE disc_type,
435 tGATT_DISC_RES* p_data);
436 extern void bta_gattc_disc_cmpl_cback(uint16_t conn_id,
437 tGATT_DISC_TYPE disc_type,
438 tGATT_STATUS status);
439 extern tGATT_STATUS bta_gattc_discover_pri_service(uint16_t conn_id,
440 tBTA_GATTC_SERV* p_server_cb,
441 tGATT_DISC_TYPE disc_type);
442 extern void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb,
443 bluetooth::Uuid* p_uuid);
444 extern const std::list<gatt::Service>* bta_gattc_get_services(uint16_t conn_id);
445 extern const gatt::Service* bta_gattc_get_service_for_handle(uint16_t conn_id,
447 const gatt::Characteristic* bta_gattc_get_characteristic_srcb(
448 tBTA_GATTC_SERV* p_srcb, uint16_t handle);
449 extern const gatt::Service* bta_gattc_get_service_for_handle_srcb(
450 tBTA_GATTC_SERV* p_srcb, uint16_t handle);
451 extern const gatt::Characteristic* bta_gattc_get_characteristic(
452 uint16_t conn_id, uint16_t handle);
453 extern const gatt::Descriptor* bta_gattc_get_descriptor(uint16_t conn_id,
455 extern const gatt::Characteristic* bta_gattc_get_owning_characteristic(
456 uint16_t conn_id, uint16_t handle);
457 extern void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle,
458 uint16_t end_handle, btgatt_db_element_t** db,
460 extern void bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb);
461 extern void bta_gattc_reset_discover_st(tBTA_GATTC_SERV* p_srcb,
462 tGATT_STATUS status);
464 extern tBTA_GATTC_CONN* bta_gattc_conn_alloc(const RawAddress& remote_bda);
465 extern tBTA_GATTC_CONN* bta_gattc_conn_find(const RawAddress& remote_bda);
466 extern tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(const RawAddress& remote_bda);
467 extern bool bta_gattc_conn_dealloc(const RawAddress& remote_bda);
469 extern bool bta_gattc_cache_load(tBTA_GATTC_SERV* p_srcb);
470 extern void bta_gattc_cache_reset(const RawAddress& server_bda);
472 extern bool bta_gattc_read_db_hash(tBTA_GATTC_CLCB* p_clcb);
474 #endif /* BTA_GATTC_INT_H */