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