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