• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-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 #ifndef  GATT_INT_H
20 #define  GATT_INT_H
21 
22 #include "bt_target.h"
23 
24 #if BLE_INCLUDED == TRUE
25 
26 
27 #include "bt_trace.h"
28 #include "gatt_api.h"
29 #include "btm_ble_api.h"
30 #include "btu.h"
31 
32 #include <string.h>
33 
34 
35 #define GATT_CREATE_CONN_ID(tcb_idx, gatt_if)  ((UINT16) ((((UINT8)(tcb_idx) ) << 8) | ((UINT8) (gatt_if))))
36 #define GATT_GET_TCB_IDX(conn_id)  ((UINT8) (((UINT16) (conn_id)) >> 8))
37 #define GATT_GET_GATT_IF(conn_id)  ((tGATT_IF)((UINT8) (conn_id)))
38 
39 #define GATT_GET_SR_REG_PTR(index) (&gatt_cb.sr_reg[(UINT8) (index)]);
40 #define GATT_TRANS_ID_MAX                   0x0fffffff      /* 4 MSB is reserved */
41 
42 /* security action for GATT write and read request */
43 #define GATT_SEC_NONE              0
44 #define GATT_SEC_OK                1
45 #define GATT_SEC_ENCRYPT           2    /* encrypt the link with current key */
46 #define GATT_SEC_ENCRYPT_NO_MITM   3    /* unauthenticated encryption or better */
47 #define GATT_SEC_ENCRYPT_MITM      4    /* authenticated encryption */
48 #define GATT_SEC_SIGN_DATA         5   /* compute the signature for the write cmd */
49 typedef UINT8 tGATT_SEC_ACTION;
50 
51 
52 #define GATT_ATTR_OP_SPT_MTU               (0x00000001 << 0)
53 #define GATT_ATTR_OP_SPT_FIND_INFO         (0x00000001 << 1)
54 #define GATT_ATTR_OP_SPT_FIND_BY_TYPE      (0x00000001 << 2)
55 #define GATT_ATTR_OP_SPT_READ_BY_TYPE      (0x00000001 << 3)
56 #define GATT_ATTR_OP_SPT_READ              (0x00000001 << 4)
57 #define GATT_ATTR_OP_SPT_MULT_READ         (0x00000001 << 5)
58 #define GATT_ATTR_OP_SPT_READ_BLOB         (0x00000001 << 6)
59 #define GATT_ATTR_OP_SPT_READ_BY_GRP_TYPE  (0x00000001 << 7)
60 #define GATT_ATTR_OP_SPT_WRITE             (0x00000001 << 8)
61 #define GATT_ATTR_OP_SPT_WRITE_CMD         (0x00000001 << 9)
62 #define GATT_ATTR_OP_SPT_PREP_WRITE        (0x00000001 << 10)
63 #define GATT_ATTR_OP_SPT_EXE_WRITE         (0x00000001 << 11)
64 #define GATT_ATTR_OP_SPT_HDL_VALUE_CONF    (0x00000001 << 12)
65 #define GATT_ATTR_OP_SP_SIGN_WRITE        (0x00000001 << 13)
66 
67 #define GATT_INDEX_INVALID  0xff
68 
69 #define GATT_PENDING_REQ_NONE    0
70 
71 
72 #define GATT_WRITE_CMD_MASK  0xc0  /*0x1100-0000*/
73 #define GATT_AUTH_SIGN_MASK  0x80  /*0x1000-0000*/
74 #define GATT_AUTH_SIGN_LEN   12
75 
76 #define GATT_HDR_SIZE           3 /* 1B opcode + 2B handle */
77 
78 /* wait for ATT cmd response timeout value */
79 #define GATT_WAIT_FOR_RSP_TOUT       30
80 
81 /* characteristic descriptor type */
82 #define GATT_DESCR_EXT_DSCPTOR   1    /* Characteristic Extended Properties */
83 #define GATT_DESCR_USER_DSCPTOR  2    /* Characteristic User Description    */
84 #define GATT_DESCR_CLT_CONFIG    3    /* Client Characteristic Configuration */
85 #define GATT_DESCR_SVR_CONFIG    4    /* Server Characteristic Configuration */
86 #define GATT_DESCR_PRES_FORMAT   5    /* Characteristic Presentation Format */
87 #define GATT_DESCR_AGGR_FORMAT   6    /* Characteristic Aggregate Format */
88 #define GATT_DESCR_VALID_RANGE   7    /* Characteristic Valid Range */
89 #define GATT_DESCR_UNKNOWN       0xff
90 
91 #define GATT_SEC_FLAG_LKEY_UNAUTHED     BTM_SEC_FLAG_LKEY_KNOWN
92 #define GATT_SEC_FLAG_LKEY_AUTHED       BTM_SEC_FLAG_LKEY_AUTHED
93 #define GATT_SEC_FLAG_ENCRYPTED         BTM_SEC_FLAG_ENCRYPTED
94 typedef UINT8 tGATT_SEC_FLAG;
95 
96 /* Find Information Response Type
97 */
98 #define GATT_INFO_TYPE_PAIR_16      0x01
99 #define GATT_INFO_TYPE_PAIR_128     0x02
100 
101 /*  GATT client FIND_TYPE_VALUE_Request data */
102 typedef struct
103 {
104     tBT_UUID        uuid;           /* type of attribute to be found */
105     UINT16          s_handle;       /* starting handle */
106     UINT16          e_handle;       /* ending handle */
107     UINT16          value_len;      /* length of the attribute value */
108     UINT8           value[GATT_MAX_MTU_SIZE];       /* pointer to the attribute value to be found */
109 } tGATT_FIND_TYPE_VALUE;
110 
111 /* client request message to ATT protocol
112 */
113 typedef union
114 {
115     tGATT_READ_BY_TYPE      browse;     /* read by type request */
116     tGATT_FIND_TYPE_VALUE   find_type_value;/* find by type value */
117     tGATT_READ_MULTI        read_multi;   /* read multiple request */
118     tGATT_READ_PARTIAL      read_blob;    /* read blob */
119     tGATT_VALUE             attr_value;   /* write request */
120                                           /* prepare write */
121     /* write blob */
122     UINT16                  handle;        /* read,  handle value confirmation */
123     UINT16                  mtu;
124     tGATT_EXEC_FLAG         exec_write;    /* execute write */
125 }tGATT_CL_MSG;
126 
127 /* error response strucutre */
128 typedef struct
129 {
130     UINT16  handle;
131     UINT8   cmd_code;
132     UINT8   reason;
133 }tGATT_ERROR;
134 
135 /* server response message to ATT protocol
136 */
137 typedef union
138 {
139     /* data type            member          event   */
140     tGATT_VALUE             attr_value;     /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
141                                             /* READ_BLOB, READ_BY_TYPE */
142     tGATT_ERROR             error;          /* ERROR_RSP */
143     UINT16                  handle;         /* WRITE, WRITE_BLOB */
144     UINT16                  mtu;            /* exchange MTU request */
145 } tGATT_SR_MSG;
146 
147 /* Characteristic declaration attribute value
148 */
149 typedef struct
150 {
151     tGATT_CHAR_PROP             property;
152     UINT16                      char_val_handle;
153 } tGATT_CHAR_DECL;
154 
155 /* attribute value maintained in the server database
156 */
157 typedef union
158 {
159     tBT_UUID                uuid;               /* service declaration */
160     tGATT_CHAR_DECL         char_decl;          /* characteristic declaration */
161     tGATT_INCL_SRVC         incl_handle;        /* included service */
162 
163 } tGATT_ATTR_VALUE;
164 
165 /* Attribute UUID type
166 */
167 #define GATT_ATTR_UUID_TYPE_16      0
168 #define GATT_ATTR_UUID_TYPE_128     1
169 typedef UINT8   tGATT_ATTR_UUID_TYPE;
170 
171 /* 16 bits UUID Attribute in server database
172 */
173 typedef struct
174 {
175     void                                *p_next;  /* pointer to the next attribute,
176                                                     either tGATT_ATTR16 or tGATT_ATTR128 */
177     tGATT_ATTR_VALUE                    *p_value;
178     tGATT_ATTR_UUID_TYPE                uuid_type;
179     tGATT_PERM                          permission;
180     UINT16                              handle;
181     UINT16                              uuid;
182 } tGATT_ATTR16;
183 
184 /* 128 bits UUID Attribute in server database
185 */
186 typedef struct
187 {
188     void                                *p_next;  /* pointer to the next attribute,
189                                                     either tGATT_ATTR16 or tGATT_ATTR128 */
190     tGATT_ATTR_VALUE                    *p_value;
191     tGATT_ATTR_UUID_TYPE                uuid_type;
192     tGATT_PERM                          permission;
193     UINT16                              handle;
194     UINT8                               uuid[LEN_UUID_128];
195 } tGATT_ATTR128;
196 
197 /* Service Database definition
198 */
199 typedef struct
200 {
201     void            *p_attr_list;               /* pointer to the first attribute,
202                                                   either tGATT_ATTR16 or tGATT_ATTR128 */
203     UINT8           *p_free_mem;                /* Pointer to free memory       */
204     BUFFER_Q        svc_buffer;                 /* buffer queue used for service database */
205     UINT32          mem_free;                   /* Memory still available       */
206     UINT16          end_handle;                 /* Last handle number           */
207     UINT16          next_handle;                /* Next usable handle value     */
208 } tGATT_SVC_DB;
209 
210 /* Data Structure used for GATT server                                        */
211 /* A GATT registration record consists of a handle, and 1 or more attributes  */
212 /* A service registration information record consists of beginning and ending */
213 /* attribute handle, service UUID and a set of GATT server callback.          */
214 typedef struct
215 {
216     tGATT_SVC_DB    *p_db;      /* pointer to the service database */
217     //tGATT_SR_CBACK  sr_cb;      /* server callback functions */
218     tBT_UUID        app_uuid;           /* applicatino UUID */
219     UINT32          sdp_handle; /* primamry service SDP handle */
220     UINT16          service_instance;   /* service instance number */
221     UINT16          type;       /* service type UUID, primary or secondary */
222     UINT16          s_hdl;      /* service starting handle */
223     UINT16          e_hdl;      /* service ending handle */
224     tGATT_IF        gatt_if;    /* this service is belong to which application */
225     BOOLEAN         in_use;
226 } tGATT_SR_REG;
227 
228 
229 /* Data Structure used for GATT server */
230 /* An GATT registration record consists of a handle, and 1 or more attributes */
231 /* A service registration information record consists of beginning and ending */
232 /* attribute handle, service UUID and a set of GATT server callback.          */
233 
234 typedef struct
235 {
236     tBT_UUID     app_uuid128;
237     tGATT_CBACK  app_cb;
238     tGATT_IF     gatt_if; /* one based */
239     BOOLEAN      in_use;
240 } tGATT_REG;
241 
242 
243 
244 
245 /* command queue for each connection */
246 typedef struct
247 {
248     BT_HDR      *p_cmd;
249     UINT16      clcb_idx;
250     UINT8       op_code;
251     BOOLEAN     to_send;
252 }tGATT_CMD_Q;
253 
254 
255 #if GATT_MAX_SR_PROFILES <= 8
256 typedef UINT8 tGATT_APP_MASK;
257 #elif GATT_MAX_SR_PROFILES <= 16
258 typedef UINT16 tGATT_APP_MASK;
259 #elif GATT_MAX_SR_PROFILES <= 32
260 typedef UINT32 tGATT_APP_MASK;
261 #endif
262 
263 /* command details for each connection */
264 typedef struct
265 {
266     BT_HDR          *p_rsp_msg;
267     UINT32           trans_id;
268     tGATT_READ_MULTI multi_req;
269     BUFFER_Q         multi_rsp_q;
270     UINT16           handle;
271     UINT8            op_code;
272     UINT8            status;
273     UINT8            cback_cnt[GATT_MAX_APPS];
274 } tGATT_SR_CMD;
275 
276 #define     GATT_CH_CLOSE               0
277 #define     GATT_CH_CLOSING             1
278 #define     GATT_CH_CONN                2
279 #define     GATT_CH_CFG                 3
280 #define     GATT_CH_OPEN                4
281 #define     GATT_CH_W4_SEC_COMP         5
282 #define     GATT_CH_W4_DATA_SIGN_COMP   6
283 
284 typedef UINT8 tGATT_CH_STATE;
285 
286 #define GATT_GATT_START_HANDLE  1
287 #define GATT_GAP_START_HANDLE   20
288 #define GATT_APP_START_HANDLE   40
289 
290 typedef struct hdl_cfg
291 {
292     UINT16               gatt_start_hdl;
293     UINT16               gap_start_hdl;
294     UINT16               app_start_hdl;
295 }tGATT_HDL_CFG;
296 
297 typedef struct hdl_list_elem
298 {
299     struct              hdl_list_elem *p_next;
300     struct              hdl_list_elem *p_prev;
301     tGATTS_HNDL_RANGE   asgn_range; /* assigned handle range */
302     tGATT_SVC_DB        svc_db;
303     BOOLEAN             in_use;
304 }tGATT_HDL_LIST_ELEM;
305 
306 typedef struct
307 {
308     tGATT_HDL_LIST_ELEM  *p_first;
309     tGATT_HDL_LIST_ELEM  *p_last;
310     UINT16               count;
311 }tGATT_HDL_LIST_INFO;
312 
313 
314 typedef struct srv_list_elem
315 {
316     struct              srv_list_elem *p_next;
317     struct              srv_list_elem *p_prev;
318     UINT16              s_hdl;
319     UINT8               i_sreg;
320     BOOLEAN             in_use;
321     BOOLEAN             is_primary;
322 }tGATT_SRV_LIST_ELEM;
323 
324 
325 typedef struct
326 {
327     tGATT_SRV_LIST_ELEM  *p_last_primary;
328     tGATT_SRV_LIST_ELEM  *p_first;
329     tGATT_SRV_LIST_ELEM  *p_last;
330     UINT16               count;
331 }tGATT_SRV_LIST_INFO;
332 
333 typedef struct
334 {
335     void            *p_clcb;            /* which clcb is doing encryption */
336     tGATT_SEC_ACTION sec_act;
337     BD_ADDR         peer_bda;
338     UINT32          trans_id;
339 
340     UINT16          att_lcid;           /* L2CAP channel ID for ATT */
341     UINT16          payload_size;
342 
343     tGATT_CH_STATE  ch_state;
344     UINT8           ch_flags;
345 
346     tGATT_IF         app_hold_link[GATT_MAX_APPS];
347 
348     /* server needs */
349     /* server response data */
350     tGATT_SR_CMD    sr_cmd;
351     UINT16          indicate_handle;
352     BUFFER_Q        pending_ind_q;
353 
354     TIMER_LIST_ENT  conf_timer_ent;     /* peer confirm to indication timer */
355 
356     UINT8            prep_cnt[GATT_MAX_APPS];
357     UINT8            ind_count;
358 
359     tGATT_CMD_Q       cl_cmd_q[GATT_CL_MAX_LCB];
360     TIMER_LIST_ENT    rsp_timer_ent;        /* peer response timer */
361     TIMER_LIST_ENT    ind_ack_timer_ent;    /* local app confirm to indication timer */
362     UINT8             pending_cl_req;
363     UINT8             next_slot_inq;    /* index of next available slot in queue */
364 
365     BOOLEAN         in_use;
366     UINT8           tcb_idx;
367 } tGATT_TCB;
368 
369 /* logic channel */
370 typedef struct
371 {
372     UINT16                  next_disc_start_hdl;   /* starting handle for the next inc srvv discovery */
373     tGATT_DISC_RES          result;
374     BOOLEAN                 wait_for_read_rsp;
375 } tGATT_READ_INC_UUID128;
376 typedef struct
377 {
378     tGATT_TCB               *p_tcb;         /* associated TCB of this CLCB */
379     tGATT_REG               *p_reg;        /* owner of this CLCB */
380     UINT8                   sccb_idx;
381     UINT8                   *p_attr_buf;    /* attribute buffer for read multiple, prepare write */
382     tBT_UUID                uuid;
383     UINT16                  conn_id;        /* connection handle */
384     UINT16                  clcb_idx;
385     UINT16                  s_handle;       /* starting handle of the active request */
386     UINT16                  e_handle;       /* ending handle of the active request */
387     UINT16                  counter;        /* used as offset, attribute length, num of prepare write */
388     UINT16                  start_offset;
389     tGATT_AUTH_REQ          auth_req;       /* authentication requirement */
390     UINT8                   operation;      /* one logic channel can have one operation active */
391     UINT8                   op_subtype;     /* operation subtype */
392     UINT8                   status;         /* operation status */
393     BOOLEAN                 first_read_blob_after_read;
394     tGATT_READ_INC_UUID128  read_uuid128;
395     BOOLEAN                 in_use;
396 } tGATT_CLCB;
397 
398 #define GATT_SIGN_WRITE             1
399 #define GATT_VERIFY_SIGN_DATA       2
400 
401 typedef struct
402 {
403     BT_HDR      hdr;
404     tGATT_CLCB  *p_clcb;
405 }tGATT_SIGN_WRITE_OP;
406 
407 typedef struct
408 {
409     BT_HDR      hdr;
410     tGATT_TCB   *p_tcb;
411     BT_HDR      *p_data;
412 
413 }tGATT_VERIFY_SIGN_OP;
414 
415 
416 typedef struct
417 {
418     UINT16                  clcb_idx;
419     BOOLEAN                 in_use;
420 } tGATT_SCCB;
421 
422 typedef struct
423 {
424     UINT16      handle;
425     UINT16      uuid;
426     UINT32      service_change;
427 }tGATT_SVC_CHG;
428 
429 typedef struct
430 {
431     tGATT_IF        gatt_if[GATT_MAX_APPS];
432     BD_ADDR         remote_bda;
433     BOOLEAN         in_use;
434 }tGATT_BG_CONN_DEV;
435 
436 
437 typedef struct
438 {
439     UINT16  conn_id;
440     BOOLEAN in_use;
441     BOOLEAN connected;
442     BD_ADDR bda;
443 }tGATT_PROFILE_CLCB;
444 
445 typedef struct
446 {
447     tGATT_TCB           tcb[GATT_MAX_PHY_CHANNEL];
448     BUFFER_Q            sign_op_queue;
449 
450     tGATT_SR_REG        sr_reg[GATT_MAX_SR_PROFILES];
451     UINT16              next_handle;    /* next available handle */
452     tGATT_SVC_CHG       gattp_attr;     /* GATT profile attribute service change */
453     tGATT_IF            gatt_if;
454     tGATT_HDL_LIST_INFO hdl_list_info;
455     tGATT_HDL_LIST_ELEM hdl_list[GATT_MAX_SR_PROFILES];
456     tGATT_SRV_LIST_INFO srv_list_info;
457     tGATT_SRV_LIST_ELEM srv_list[GATT_MAX_SR_PROFILES];
458 
459     BUFFER_Q            srv_chg_clt_q;   /* service change clients queue */
460     BUFFER_Q            pending_new_srv_start_q; /* pending new service start queue */
461     tGATT_REG           cl_rcb[GATT_MAX_APPS];
462     tGATT_CLCB          clcb[GATT_CL_MAX_LCB];  /* connection link control block*/
463     tGATT_SCCB          sccb[GATT_MAX_SCCB];    /* sign complete callback function GATT_MAX_SCCB <= GATT_CL_MAX_LCB */
464     UINT8               trace_level;
465     UINT16              def_mtu_size;
466 
467 #if GATT_CONFORMANCE_TESTING == TRUE
468     BOOLEAN             enable_err_rsp;
469     UINT8               req_op_code;
470     UINT8               err_status;
471 #endif
472 
473     tGATT_PROFILE_CLCB  profile_clcb[GATT_MAX_APPS];
474     UINT16              handle_of_h_r;          /* Handle of the handles reused characteristic value */
475 
476     tGATT_APPL_INFO       cb_info;
477 
478 
479 
480     tGATT_HDL_CFG           hdl_cfg;
481     tGATT_BG_CONN_DEV       bgconn_dev[GATT_MAX_BG_CONN_DEV];
482 
483 } tGATT_CB;
484 
485 
486 #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
487 
488 #ifdef __cplusplus
489 extern "C" {
490 #endif
491 
492 /* Global GATT data */
493 #if GATT_DYNAMIC_MEMORY == FALSE
494 GATT_API extern tGATT_CB  gatt_cb;
495 #else
496 GATT_API extern tGATT_CB *gatt_cb_ptr;
497 #define gatt_cb (*gatt_cb_ptr)
498 #endif
499 
500 #if GATT_CONFORMANCE_TESTING == TRUE
501 GATT_API extern void gatt_set_err_rsp(BOOLEAN enable, UINT8 req_op_code, UINT8 err_status);
502 #endif
503 
504 #ifdef __cplusplus
505 }
506 #endif
507 
508 /* internal functions */
509 extern void gatt_init (void);
510 
511 /* from gatt_main.c */
512 extern BOOLEAN gatt_disconnect (BD_ADDR rem_bda);
513 extern BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr);
514 extern BOOLEAN gatt_connect (BD_ADDR rem_bda,  tGATT_TCB *p_tcb);
515 extern void gatt_data_process (tGATT_TCB *p_tcb, BT_HDR *p_buf);
516 extern void gatt_update_app_use_link_flag ( tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add, BOOLEAN check_acl_link);
517 
518 extern void gatt_profile_db_init(void);
519 extern void gatt_set_ch_state(tGATT_TCB *p_tcb, tGATT_CH_STATE ch_state);
520 extern tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB *p_tcb);
521 extern void gatt_init_srv_chg(void);
522 extern void gatt_proc_srv_chg (void);
523 extern void gatt_send_srv_chg_ind (BD_ADDR peer_bda);
524 extern void gatt_chk_srv_chg(tGATTS_SRV_CHG *p_srv_chg_clt);
525 extern void gatt_add_a_bonded_dev_for_srv_chg (BD_ADDR bda);
526 
527 /* from gatt_attr.c */
528 extern UINT16 gatt_profile_find_conn_id_by_bd_addr(BD_ADDR bda);
529 extern tGATT_PROFILE_CLCB *gatt_profile_find_clcb_by_bd_addr(BD_ADDR bda);
530 extern BOOLEAN gatt_profile_clcb_dealloc (UINT16 conn_id);
531 extern tGATT_PROFILE_CLCB *gatt_profile_clcb_alloc (UINT16 conn_id, BD_ADDR bda);
532 
533 
534 /* Functions provided by att_protocol.c */
535 extern tGATT_STATUS attp_send_cl_msg (tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 op_code, tGATT_CL_MSG *p_msg);
536 extern BT_HDR *attp_build_sr_msg(tGATT_TCB *p_tcb, UINT8 op_code, tGATT_SR_MSG *p_msg);
537 extern tGATT_STATUS attp_send_sr_msg (tGATT_TCB *p_tcb, BT_HDR *p_msg);
538 extern BOOLEAN  attp_send_msg_to_L2CAP(tGATT_TCB *p_tcb, BT_HDR *p_toL2CAP);
539 
540 /* utility functions */
541 extern UINT8 * gatt_dbg_op_name(UINT8 op_code);
542 extern UINT32 gatt_add_sdp_record (tBT_UUID *p_uuid, UINT16 start_hdl, UINT16 end_hdl);
543 extern BOOLEAN gatt_parse_uuid_from_cmd(tBT_UUID *p_uuid, UINT16 len, UINT8 **p_data);
544 extern UINT8 gatt_build_uuid_to_stream(UINT8 **p_dst, tBT_UUID uuid);
545 extern BOOLEAN gatt_uuid_compare(tBT_UUID src, tBT_UUID tar);
546 extern void gatt_sr_get_sec_info(BD_ADDR rem_bda, BOOLEAN le_conn, UINT8 *p_sec_flag, UINT8 *p_key_size);
547 extern void gatt_start_rsp_timer(tGATT_TCB    *p_tcb);
548 extern void gatt_start_conf_timer(tGATT_TCB    *p_tcb);
549 extern void gatt_rsp_timeout(TIMER_LIST_ENT *p_tle);
550 extern void gatt_ind_ack_timeout(TIMER_LIST_ENT *p_tle);
551 extern void gatt_start_ind_ack_timer(tGATT_TCB *p_tcb);
552 extern tGATT_STATUS gatt_send_error_rsp(tGATT_TCB *p_tcb, UINT8 err_code, UINT8 op_code, UINT16 handle, BOOLEAN deq);
553 extern void gatt_dbg_display_uuid(tBT_UUID bt_uuid);
554 
555 extern tGATTS_PENDING_NEW_SRV_START *gatt_sr_is_new_srv_chg(tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst);
556 
557 extern BOOLEAN gatt_is_srv_chg_ind_pending (tGATT_TCB *p_tcb);
558 extern tGATTS_SRV_CHG *gatt_is_bda_in_the_srv_chg_clt_list (BD_ADDR bda);
559 
560 extern BOOLEAN gatt_find_the_connected_bda(UINT8 start_idx, BD_ADDR bda, UINT8 *p_found_idx);
561 extern void gatt_set_srv_chg(void);
562 extern void gatt_delete_dev_from_srv_chg_clt_list(BD_ADDR bd_addr);
563 extern tGATT_VALUE *gatt_add_pending_ind(tGATT_TCB  *p_tcb, tGATT_VALUE *p_ind);
564 extern tGATTS_PENDING_NEW_SRV_START *gatt_add_pending_new_srv_start( tGATTS_HNDL_RANGE *p_new_srv_start);
565 extern void gatt_free_srvc_db_buffer_app_id(tBT_UUID *p_app_id);
566 
567 /* reserved handle list */
568 extern tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_app_id (tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst);
569 extern tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_handle(UINT16 handle);
570 extern tGATT_HDL_LIST_ELEM *gatt_alloc_hdl_buffer(void);
571 extern void gatt_free_hdl_buffer(tGATT_HDL_LIST_ELEM *p);
572 extern BOOLEAN gatt_is_last_attribute(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_start, tBT_UUID value);
573 extern void gatt_update_last_pri_srv_info(tGATT_SRV_LIST_INFO *p_list);
574 extern BOOLEAN gatt_add_a_srv_to_list(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_new);
575 extern BOOLEAN gatt_remove_a_srv_from_list(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_remove);
576 extern BOOLEAN gatt_add_an_item_to_list(tGATT_HDL_LIST_INFO *p_list, tGATT_HDL_LIST_ELEM *p_new);
577 extern BOOLEAN gatt_remove_an_item_from_list(tGATT_HDL_LIST_INFO *p_list, tGATT_HDL_LIST_ELEM *p_remove);
578 extern tGATTS_SRV_CHG *gatt_add_srv_chg_clt(tGATTS_SRV_CHG *p_srv_chg);
579 
580 /* for background connection */
581 extern BOOLEAN gatt_update_auto_connect_dev (tGATT_IF gatt_if, BOOLEAN add, BD_ADDR bd_addr);
582 extern BOOLEAN gatt_add_bg_dev_list(tGATT_IF gatt_if, BD_ADDR bd_addr);
583 extern BOOLEAN gatt_is_bg_dev_for_app(tGATT_BG_CONN_DEV *p_dev, tGATT_IF gatt_if);
584 extern BOOLEAN gatt_remove_bg_dev_for_app(tGATT_IF gatt_if, BD_ADDR bd_addr);
585 extern UINT8 gatt_get_num_apps_for_bg_dev(BD_ADDR bd_addr);
586 extern BOOLEAN gatt_find_app_for_bg_dev(BD_ADDR bd_addr, tGATT_IF *p_gatt_if);
587 extern BOOLEAN gatt_remove_bg_dev_from_list(tGATT_IF gatt_if, BD_ADDR bd_addr);
588 extern tGATT_BG_CONN_DEV * gatt_find_bg_dev(BD_ADDR remote_bda);
589 extern void gatt_deregister_bgdev_list(tGATT_IF gatt_if);
590 extern void gatt_reset_bgdev_list(void);
591 
592 /* server function */
593 extern UINT8 gatt_sr_find_i_rcb_by_handle(UINT16 handle);
594 extern UINT8 gatt_sr_find_i_rcb_by_app_id(tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst);
595 extern UINT8 gatt_sr_alloc_rcb(tGATT_HDL_LIST_ELEM *p_list);
596 extern tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if, UINT32 trans_id, UINT8 op_code, tGATT_STATUS status, tGATTS_RSP *p_msg);
597 extern void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
598                                            UINT16 len, UINT8 *p_data);
599 extern void gatt_sr_send_req_callback(UINT16 conn_id,  UINT32 trans_id,
600                                       UINT8 op_code, tGATTS_DATA *p_req_data);
601 extern UINT32 gatt_sr_enqueue_cmd (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 handle);
602 extern BOOLEAN gatt_cancel_open(tGATT_IF gatt_if, BD_ADDR bda);
603 
604 /*   */
605 
606 extern tGATT_REG *gatt_get_regcb (tGATT_IF gatt_if);
607 extern BOOLEAN gatt_is_clcb_allocated (UINT16 conn_id);
608 extern tGATT_CLCB *gatt_clcb_alloc (UINT16 conn_id);
609 extern void gatt_clcb_dealloc (tGATT_CLCB *p_clcb);
610 
611 extern void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB *p_tcb );
612 extern BOOLEAN gatt_sr_is_cback_cnt_zero(tGATT_TCB *p_tcb );
613 extern BOOLEAN gatt_sr_is_prep_cnt_zero(tGATT_TCB *p_tcb );
614 extern void gatt_sr_reset_cback_cnt(tGATT_TCB *p_tcb );
615 extern void gatt_sr_reset_prep_cnt(tGATT_TCB *p_tcb );
616 extern void gatt_sr_update_cback_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first);
617 extern void gatt_sr_update_prep_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first);
618 
619 extern BOOLEAN gatt_find_app_hold_link(tGATT_TCB *p_tcb, UINT8 start_idx, UINT8 *p_found_idx, tGATT_IF *p_gatt_if);
620 extern UINT8 gatt_num_apps_hold_link(tGATT_TCB *p_tcb);
621 extern UINT8 gatt_num_clcb_by_bd_addr(BD_ADDR bda);
622 extern tGATT_TCB * gatt_find_tcb_by_cid(UINT16 lcid);
623 extern tGATT_TCB * gatt_allocate_tcb_by_bdaddr(BD_ADDR bda);
624 extern tGATT_TCB * gatt_get_tcb_by_idx(UINT8 tcb_idx);
625 extern tGATT_TCB * gatt_find_tcb_by_addr(BD_ADDR bda);
626 
627 
628 /* GATT client functions */
629 extern void gatt_dequeue_sr_cmd (tGATT_TCB *p_tcb);
630 extern UINT8 gatt_send_write_msg(tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 op_code, UINT16 handle,
631                                  UINT16 len, UINT16 offset, UINT8 *p_data);
632 extern void gatt_cleanup_upon_disc(BD_ADDR bda, UINT16 reason);
633 extern void gatt_end_operation(tGATT_CLCB *p_clcb, tGATT_STATUS status, void *p_data);
634 
635 extern void gatt_act_discovery(tGATT_CLCB *p_clcb);
636 extern void gatt_act_read(tGATT_CLCB *p_clcb, UINT16 offset);
637 extern void gatt_act_write(tGATT_CLCB *p_clcb);
638 extern UINT8 gatt_act_send_browse(tGATT_TCB *p_tcb, UINT16 index, UINT8 op, UINT16 s_handle, UINT16 e_handle,
639                                   tBT_UUID uuid);
640 extern tGATT_CLCB *gatt_cmd_dequeue(tGATT_TCB *p_tcb, UINT8 *p_opcode);
641 extern BOOLEAN gatt_cmd_enq(tGATT_TCB *p_tcb, UINT16 clcb_idx, BOOLEAN to_send, UINT8 op_code, BT_HDR *p_buf);
642 extern void gatt_client_handle_server_rsp (tGATT_TCB *p_tcb, UINT8 op_code,
643                                            UINT16 len, UINT8 *p_data);
644 extern void gatt_send_queue_write_cancel (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, tGATT_EXEC_FLAG flag);
645 
646 /* gatt_auth.c */
647 extern BOOLEAN gatt_security_check_start(tGATT_CLCB *p_clcb);
648 extern void gatt_verify_signature(tGATT_TCB *p_tcb, BT_HDR *p_buf);
649 extern tGATT_SEC_ACTION gatt_determine_sec_act(tGATT_CLCB *p_clcb );
650 extern tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB *p_tcb);
651 extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB *p_tcb);
652 extern void gatt_set_sec_act(tGATT_TCB *p_tcb, tGATT_SEC_ACTION sec_act);
653 
654 /* gatt_db.c */
655 extern BOOLEAN gatts_init_service_db (tGATT_SVC_DB *p_db, tBT_UUID service, BOOLEAN is_pri, UINT16 s_hdl, UINT16 num_handle);
656 extern UINT16 gatts_add_included_service (tGATT_SVC_DB *p_db, UINT16 s_handle, UINT16 e_handle, tBT_UUID service);
657 extern UINT16 gatts_add_characteristic (tGATT_SVC_DB *p_db, tGATT_PERM perm, tGATT_CHAR_PROP property, tBT_UUID *p_char_uuid);
658 extern UINT16 gatts_add_char_descr (tGATT_SVC_DB *p_db, tGATT_PERM perm, tBT_UUID *p_dscp_uuid);
659 extern tGATT_STATUS gatts_db_read_attr_value_by_type (tGATT_TCB *p_tcb, tGATT_SVC_DB *p_db, UINT8 op_code, BT_HDR *p_rsp, UINT16 s_handle,
660                                                       UINT16 e_handle, tBT_UUID type, UINT16 *p_len, tGATT_SEC_FLAG sec_flag, UINT8 key_size,UINT32 trans_id, UINT16 *p_cur_handle);
661 extern tGATT_STATUS gatts_read_attr_value_by_handle(tGATT_TCB *p_tcb,tGATT_SVC_DB *p_db, UINT8 op_code, UINT16 handle, UINT16 offset,
662                                                     UINT8 *p_value, UINT16 *p_len, UINT16 mtu,tGATT_SEC_FLAG sec_flag,UINT8 key_size,UINT32 trans_id);
663 extern tGATT_STATUS gatts_write_attr_perm_check (tGATT_SVC_DB *p_db, UINT8 op_code,UINT16 handle, UINT16 offset, UINT8 *p_data,
664                                                  UINT16 len, tGATT_SEC_FLAG sec_flag, UINT8 key_size);
665 extern tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB *p_db, BOOLEAN is_long, UINT16 handle, tGATT_SEC_FLAG sec_flag,UINT8 key_size);
666 extern void gatts_update_srv_list_elem(UINT8 i_sreg, UINT16 handle, BOOLEAN is_primary);
667 extern tBT_UUID * gatts_get_service_uuid (tGATT_SVC_DB *p_db);
668 
669 #endif
670 
671 #endif /* BLE_INCLUDED */
672