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#include "bta_sys.h"
29#include "bta_gatt_api.h"
30#include "bta_gattc_ci.h"
31#include "bta_gattc_co.h"
32
33#include "gki.h"
34
35/*****************************************************************************
36**  Constants and data types
37*****************************************************************************/
38enum
39{
40    BTA_GATTC_API_OPEN_EVT   = BTA_SYS_EVT_START(BTA_ID_GATTC),
41    BTA_GATTC_INT_OPEN_FAIL_EVT,
42    BTA_GATTC_API_CANCEL_OPEN_EVT,
43    BTA_GATTC_INT_CANCEL_OPEN_OK_EVT,
44
45    BTA_GATTC_API_READ_EVT,
46    BTA_GATTC_API_WRITE_EVT,
47    BTA_GATTC_API_EXEC_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
61    /* for cache loading/saving */
62    BTA_GATTC_START_CACHE_EVT,
63    BTA_GATTC_CI_CACHE_OPEN_EVT,
64    BTA_GATTC_CI_CACHE_LOAD_EVT,
65    BTA_GATTC_CI_CACHE_SAVE_EVT,
66
67    BTA_GATTC_INT_START_IF_EVT,
68    BTA_GATTC_API_REG_EVT,
69    BTA_GATTC_API_DEREG_EVT,
70    BTA_GATTC_INT_DEREG_EVT
71
72};
73typedef UINT16 tBTA_GATTC_INT_EVT;
74
75/* max client application GATTC can support */
76#ifndef     BTA_GATTC_CL_MAX
77#define     BTA_GATTC_CL_MAX    4
78#endif
79
80/* max known devices GATTC can support */
81#ifndef     BTA_GATTC_KNOWN_SR_MAX
82#define     BTA_GATTC_KNOWN_SR_MAX    4
83#endif
84
85#ifndef BTA_GATTC_CLCB_MAX
86    #define BTA_GATTC_CLCB_MAX      GATT_CL_MAX_LCB
87#endif
88
89#define BTA_GATTC_WRITE_PREPARE          GATT_WRITE_PREPARE
90
91/* internal strucutre for GATTC register API  */
92typedef struct
93{
94    BT_HDR                  hdr;
95    tBT_UUID                app_uuid;
96    tBTA_GATTC_CBACK        *p_cback;
97}tBTA_GATTC_API_REG;
98
99typedef struct
100{
101    BT_HDR                  hdr;
102    tBTA_GATTC_IF           client_if;
103}tBTA_GATTC_INT_START_IF;
104
105typedef tBTA_GATTC_INT_START_IF tBTA_GATTC_API_DEREG;
106typedef tBTA_GATTC_INT_START_IF tBTA_GATTC_INT_DEREG;
107
108typedef struct
109{
110    BT_HDR                  hdr;
111    BD_ADDR                 remote_bda;
112    tBTA_GATTC_IF           client_if;
113    BOOLEAN                 is_direct;
114} tBTA_GATTC_API_OPEN;
115
116typedef tBTA_GATTC_API_OPEN tBTA_GATTC_API_CANCEL_OPEN;
117
118typedef struct
119{
120    BT_HDR                  hdr;
121    tBTA_GATT_AUTH_REQ      auth_req;
122    tBTA_GATT_SRVC_ID       srvc_id;
123    tBTA_GATT_ID            char_id;
124    tBT_UUID                descr_type;
125} tBTA_GATTC_API_READ;
126
127typedef struct
128{
129    BT_HDR                  hdr;
130    tBTA_GATT_AUTH_REQ      auth_req;
131    tBTA_GATT_SRVC_ID       srvc_id;
132    tBTA_GATT_ID            char_id;
133    tBT_UUID                descr_type;
134    tBTA_GATTC_WRITE_TYPE   write_type;
135    UINT16                  offset;
136    UINT16                  len;
137    UINT8                   *p_value;
138}tBTA_GATTC_API_WRITE;
139
140typedef struct
141{
142    BT_HDR                  hdr;
143    BOOLEAN                 is_execute;
144}tBTA_GATTC_API_EXEC;
145
146typedef struct
147{
148    BT_HDR                  hdr;
149    tBTA_GATT_SRVC_ID       srvc_id;
150    tBTA_GATT_ID            char_id;
151} tBTA_GATTC_API_CONFIRM;
152
153typedef tGATT_CL_COMPLETE tBTA_GATTC_CMPL;
154
155typedef struct
156{
157    BT_HDR                  hdr;
158    UINT8                   op_code;
159    tGATT_STATUS            status;
160    tBTA_GATTC_CMPL         *p_cmpl;
161}tBTA_GATTC_OP_CMPL;
162
163typedef struct
164{
165    BT_HDR              hdr;
166    tBT_UUID            srvc_uuid;
167}tBTA_GATTC_API_SEARCH;
168
169typedef struct
170{
171    BT_HDR                  hdr;
172    tBTA_GATT_AUTH_REQ      auth_req;
173    UINT8                   num_attr;
174    tBTA_GATTC_ATTR_ID      *p_id_list;
175}tBTA_GATTC_API_READ_MULTI;
176
177typedef union
178{
179    BT_HDR                      hdr;
180    tBTA_GATTC_API_REG          api_reg;
181    tBTA_GATTC_API_DEREG        api_dereg;
182    tBTA_GATTC_API_OPEN         api_conn;
183    tBTA_GATTC_API_CANCEL_OPEN  api_cancel_conn;
184    tBTA_GATTC_API_READ         api_read;
185    tBTA_GATTC_API_SEARCH       api_search;
186    tBTA_GATTC_API_WRITE        api_write;
187    tBTA_GATTC_API_CONFIRM      api_confirm;
188    tBTA_GATTC_API_EXEC         api_exec;
189    tBTA_GATTC_API_READ_MULTI   api_read_multi;
190    tBTA_GATTC_OP_CMPL          op_cmpl;
191    tBTA_GATTC_CI_EVT           ci_open;
192    tBTA_GATTC_CI_EVT           ci_save;
193    tBTA_GATTC_CI_LOAD          ci_load;
194
195    tBTA_GATTC_INT_START_IF     int_start_if;
196    tBTA_GATTC_INT_DEREG        int_dereg;
197
198} tBTA_GATTC_DATA;
199
200
201/* GATT server cache on the client */
202typedef union
203{
204    UINT8               uuid128[LEN_UUID_128];
205    UINT16              uuid16;
206}tBTA_GATTC_UUID;
207
208typedef struct gattc_attr_cache
209{
210    tBTA_GATTC_UUID         *p_uuid;
211    struct                  gattc_attr_cache *p_next;
212    UINT16                  uuid_len;
213    UINT16                  attr_handle;
214    UINT8                   inst_id;
215    tBTA_GATT_CHAR_PROP     property; /* if characteristic, it is char property;
216                                         if included service, flag primary,
217                                         if descriptor, not used */
218    tBTA_GATTC_ATTR_TYPE    attr_type;
219// btla-specific ++
220} __attribute__((packed)) tBTA_GATTC_CACHE_ATTR;
221// btla-specific --
222
223typedef struct gattc_svc_cache
224{
225    tBTA_GATT_SRVC_ID       service_uuid;
226    tBTA_GATTC_CACHE_ATTR   *p_attr;
227    tBTA_GATTC_CACHE_ATTR   *p_last_attr;
228    UINT16                  s_handle;
229    UINT16                  e_handle;
230    struct                  gattc_svc_cache *p_next;
231// btla-specific ++
232} __attribute__((packed)) tBTA_GATTC_CACHE;
233// btla-specific --
234
235typedef struct
236{
237    tBT_UUID            uuid;
238    UINT16              s_handle;
239    UINT16              e_handle;
240    BOOLEAN             is_primary;
241    UINT8               srvc_inst_id;
242    tBTA_GATT_CHAR_PROP property;
243}tBTA_GATTC_ATTR_REC;
244
245
246#define BTA_GATTC_MAX_CACHE_CHAR    40
247#define BTA_GATTC_ATTR_LIST_SIZE    (BTA_GATTC_MAX_CACHE_CHAR * sizeof(tBTA_GATTC_ATTR_REC))
248
249#ifndef BTA_GATTC_CACHE_SRVR_SIZE
250    #define BTA_GATTC_CACHE_SRVR_SIZE   600
251#endif
252
253enum
254{
255    BTA_GATTC_IDLE_ST = 0,      /* Idle  */
256    BTA_GATTC_W4_CONN_ST,       /* Wait for connection -  (optional) */
257    BTA_GATTC_CONN_ST,          /* connected state */
258    BTA_GATTC_DISCOVER_ST       /* discover is in progress */
259};
260typedef UINT8 tBTA_GATTC_STATE;
261
262typedef struct
263{
264    BOOLEAN             in_use;
265    BD_ADDR             server_bda;
266    BOOLEAN             connected;
267
268#define BTA_GATTC_SERV_IDLE     0
269#define BTA_GATTC_SERV_LOAD     1
270#define BTA_GATTC_SERV_SAVE     2
271
272    UINT8               state;
273
274    tBTA_GATTC_CACHE    *p_srvc_cache;
275    tBTA_GATTC_CACHE    *p_cur_srvc;
276    BUFFER_Q            cache_buffer;   /* buffer queue used for storing the cache data */
277    UINT8               *p_free;        /* starting point to next available byte */
278    UINT16              free_byte;      /* number of available bytes in server cache buffer */
279    UINT8               update_count;   /* indication received */
280    UINT8               num_clcb;       /* number of associated CLCB */
281
282
283    tBTA_GATTC_ATTR_REC *p_srvc_list;
284    UINT8               cur_srvc_idx;
285    UINT8               cur_char_idx;
286    UINT8               next_avail_idx;
287    UINT8               total_srvc;
288    UINT8               total_char;
289
290    UINT8               srvc_hdl_chg;   /* service handle change indication pending */
291    UINT16              attr_index;     /* cahce NV saving/loading attribute index */
292
293} tBTA_GATTC_SERV;
294
295#ifndef BTA_GATTC_NOTIF_REG_MAX
296#define BTA_GATTC_NOTIF_REG_MAX     4
297#endif
298
299typedef struct
300{
301    BOOLEAN             in_use;
302    BD_ADDR             remote_bda;
303    tBTA_GATTC_CHAR_ID  char_id;
304}tBTA_GATTC_NOTIF_REG;
305
306typedef struct
307{
308    tBTA_GATTC_CBACK        *p_cback;
309    BOOLEAN                 in_use;
310    tBTA_GATTC_IF           client_if;      /* client interface with BTE stack for this application */
311    UINT8                   num_clcb;       /* number of associated CLCB */
312    BOOLEAN                 dereg_pending;
313    tBT_UUID                app_uuid;
314    tBTA_GATTC_NOTIF_REG    notif_reg[BTA_GATTC_NOTIF_REG_MAX];
315}tBTA_GATTC_RCB;
316
317/* client channel is a mapping between a BTA client(cl_id) and a remote BD address */
318typedef struct
319{
320    UINT16              bta_conn_id;    /* client channel ID, unique for clcb */
321    BD_ADDR             bda;
322    tBTA_GATTC_RCB      *p_rcb;         /* pointer to the registration CB */
323    tBTA_GATTC_SERV     *p_srcb;    /* server cache CB */
324    tBTA_GATTC_DATA     *p_q_cmd;   /* command in queue waiting for execution */
325
326#define BTA_GATTC_NO_SCHEDULE       0
327#define BTA_GATTC_DISC_WAITING      0x01
328#define BTA_GATTC_REQ_WAITING       0x10
329
330    UINT8               auto_update; /* auto update is waiting */
331    BOOLEAN             in_use;
332    tBTA_GATTC_STATE    state;
333    tBTA_GATT_STATUS    status;
334    UINT16              reason;
335} tBTA_GATTC_CLCB;
336
337/* back ground connection tracking information */
338#if GATT_MAX_APPS <= 8
339typedef UINT8 tBTA_GATTC_CIF_MASK ;
340#elif GATT_MAX_APPS <= 16
341typedef UINT16 tBTA_GATTC_CIF_MASK;
342#elif GATT_MAX_APPS <= 32
343typedef UINT32 tBTA_GATTC_CIF_MASK;
344#endif
345
346typedef struct
347{
348    BOOLEAN                 in_use;
349    BD_ADDR                 remote_bda;
350    tBTA_GATTC_CIF_MASK     cif_mask;
351
352}tBTA_GATTC_BG_TCK;
353
354typedef struct
355{
356    tBTA_GATTC_BG_TCK   bg_track[BTA_GATTC_KNOWN_SR_MAX];
357    tBTA_GATTC_RCB      cl_rcb[BTA_GATTC_CL_MAX];
358
359    tBTA_GATTC_CLCB     clcb[BTA_GATTC_CLCB_MAX];
360    tBTA_GATTC_SERV     known_server[BTA_GATTC_KNOWN_SR_MAX];
361
362    tSDP_DISCOVERY_DB   *p_sdp_db;
363    UINT16              sdp_conn_id;
364}tBTA_GATTC_CB;
365
366/*****************************************************************************
367**  Global data
368*****************************************************************************/
369
370/* GATTC control block */
371#if BTA_DYNAMIC_MEMORY == FALSE
372extern tBTA_GATTC_CB  bta_gattc_cb;
373#else
374extern tBTA_GATTC_CB *bta_gattc_cb_ptr;
375#define bta_gattc_cb (*bta_gattc_cb_ptr)
376#endif
377
378/*****************************************************************************
379**  Function prototypes
380*****************************************************************************/
381extern BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg);
382extern void bta_gattc_sm_execute(tBTA_GATTC_CLCB *p_clcb, UINT16 event, tBTA_GATTC_DATA *p_data);
383
384/* function processed outside SM */
385extern void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
386extern void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
387extern void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
388extern void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
389extern void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
390extern void bta_gattc_int_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
391
392/* function within state machine */
393extern void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
394extern void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
395extern void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
396
397extern void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
398extern void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
399extern void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
400
401extern void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
402
403extern void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
404extern void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
405
406extern void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
407extern void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
408extern void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
409extern void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
410extern void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
411extern void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
412extern void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
413extern void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
414extern void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
415extern void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
416extern void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
417extern void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
418extern void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
419extern void bta_gattc_ci_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
420extern void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
421extern void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
422extern void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
423extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg);
424extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data);
425extern void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
426                                       BD_ADDR remote_bda, UINT16 conn_id);
427
428/* utility functions */
429extern tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda); //todo
430extern tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id);
431extern tBTA_GATTC_CLCB * bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda);
432extern void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb);
433extern tBTA_GATTC_CLCB * bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda);
434extern tBTA_GATTC_RCB * bta_gattc_cl_get_regcb(UINT8 client_if);
435extern tBTA_GATTC_SERV * bta_gattc_find_srcb(BD_ADDR bda);
436extern tBTA_GATTC_SERV * bta_gattc_srcb_alloc(BD_ADDR bda);
437extern tBTA_GATTC_SERV * bta_gattc_find_scb_by_cid (UINT16 conn_id);
438extern BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
439
440extern UINT16 bta_gattc_id2handle(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_SRVC_ID *p_service_id, tBTA_GATT_ID *p_char_id, tBT_UUID descr_uuid);
441extern BOOLEAN bta_gattc_handle2id(tBTA_GATTC_SERV *p_srcb, UINT16 handle, tBTA_GATT_SRVC_ID *service_id, tBTA_GATT_ID *char_id, tBT_UUID *p_type);
442extern BOOLEAN bta_gattc_uuid_compare (tBT_UUID src, tBT_UUID tar, BOOLEAN is_precise);
443extern void bta_gattc_pack_attr_uuid(tBTA_GATTC_CACHE_ATTR   *p_attr, tBT_UUID *p_uuid);
444extern BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB  *p_clreg, tBTA_GATTC_SERV *p_srcb, tBTA_GATTC_NOTIFY  *p_notify);
445extern tBTA_GATT_STATUS bta_gattc_pack_read_cb_data(tBTA_GATTC_SERV *p_srcb, tBT_UUID descr_uuid, tGATT_VALUE *p_attr, tBTA_GATT_READ_VAL *p_value);
446extern BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if,  BD_ADDR remote_bda, BOOLEAN add);
447extern BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if,  BD_ADDR remote_bda);
448extern UINT8 bta_gattc_num_reg_app(void);
449extern void bta_gattc_clear_notif_registration(UINT16 conn_id);
450
451/* discovery functions */
452extern void bta_gattc_disc_res_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data);
453extern void bta_gattc_disc_cmpl_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
454extern tBTA_GATT_STATUS bta_gattc_discover_procedure(UINT16 conn_id, tBTA_GATTC_SERV *p_server_cb, UINT8 disc_type);
455extern tBTA_GATT_STATUS bta_gattc_discover_pri_service(UINT16 conn_id, tBTA_GATTC_SERV *p_server_cb, UINT8 disc_type);
456extern void bta_gattc_search_service(tBTA_GATTC_CLCB *p_clcb, tBT_UUID uuid);
457extern tBTA_GATT_STATUS bta_gattc_query_cache(UINT16 conn_id, UINT8 query_type, tBTA_GATT_SRVC_ID *p_srvc_id,
458                                              tBTA_GATT_ID *p_start_rec,tBT_UUID *p_uuid_cond,
459                                              tBTA_GATT_ID *p_output, void *p_property);
460extern tBTA_GATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV *p_srvc_cb);
461extern void bta_gattc_rebuild_cache(tBTA_GATTC_SERV *p_srcv, UINT16 num_attr, tBTA_GATTC_NV_ATTR *p_attr, UINT16 attr_index);
462extern BOOLEAN bta_gattc_cache_save(tBTA_GATTC_SERV *p_srvc_cb, UINT16 conn_id);
463
464#endif /* BTA_GATTC_INT_H */
465