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