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 "bt_trace.h"
25 #include "btm_ble_api.h"
26 #include "btu.h"
27 #include "gatt_api.h"
28 #include "osi/include/fixed_queue.h"
29 
30 #include <string.h>
31 #include <list>
32 #include <vector>
33 
34 #define GATT_CREATE_CONN_ID(tcb_idx, gatt_if) \
35   ((uint16_t)((((uint8_t)(tcb_idx)) << 8) | ((uint8_t)(gatt_if))))
36 #define GATT_GET_TCB_IDX(conn_id) ((uint8_t)(((uint16_t)(conn_id)) >> 8))
37 #define GATT_GET_GATT_IF(conn_id) ((tGATT_IF)((uint8_t)(conn_id)))
38 
39 #define GATT_TRANS_ID_MAX 0x0fffffff /* 4 MSB is reserved */
40 
41 /* security action for GATT write and read request */
42 #define GATT_SEC_NONE 0
43 #define GATT_SEC_OK 1
44 #define GATT_SEC_SIGN_DATA 2       /* compute the signature for the write cmd */
45 #define GATT_SEC_ENCRYPT 3         /* encrypt the link with current key */
46 #define GATT_SEC_ENCRYPT_NO_MITM 4 /* unauthenticated encryption or better */
47 #define GATT_SEC_ENCRYPT_MITM 5    /* authenticated encryption */
48 #define GATT_SEC_ENC_PENDING 6     /* wait for link encryption pending */
49 typedef uint8_t tGATT_SEC_ACTION;
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 #define GATT_WRITE_CMD_MASK 0xc0 /*0x1100-0000*/
71 #define GATT_AUTH_SIGN_MASK 0x80 /*0x1000-0000*/
72 #define GATT_AUTH_SIGN_LEN 12
73 
74 #define GATT_HDR_SIZE 3 /* 1B opcode + 2B handle */
75 
76 /* wait for ATT cmd response timeout value */
77 #define GATT_WAIT_FOR_RSP_TIMEOUT_MS (30 * 1000)
78 #define GATT_WAIT_FOR_DISC_RSP_TIMEOUT_MS (5 * 1000)
79 #define GATT_REQ_RETRY_LIMIT 2
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_t 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   tBT_UUID uuid;      /* type of attribute to be found */
104   uint16_t s_handle;  /* starting handle */
105   uint16_t e_handle;  /* ending handle */
106   uint16_t value_len; /* length of the attribute value */
107   uint8_t
108       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   tGATT_READ_BY_TYPE browse;             /* read by type request */
115   tGATT_FIND_TYPE_VALUE find_type_value; /* find by type value */
116   tGATT_READ_MULTI read_multi;           /* read multiple request */
117   tGATT_READ_PARTIAL read_blob;          /* read blob */
118   tGATT_VALUE attr_value;                /* write request */
119                                          /* prepare write */
120   /* write blob */
121   uint16_t handle; /* read,  handle value confirmation */
122   uint16_t mtu;
123   tGATT_EXEC_FLAG exec_write; /* execute write */
124 } tGATT_CL_MSG;
125 
126 /* error response strucutre */
127 typedef struct {
128   uint16_t handle;
129   uint8_t cmd_code;
130   uint8_t reason;
131 } tGATT_ERROR;
132 
133 /* server response message to ATT protocol
134 */
135 typedef union {
136   /* data type            member          event   */
137   tGATT_VALUE attr_value; /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
138                           /* READ_BLOB, READ_BY_TYPE */
139   tGATT_ERROR error;      /* ERROR_RSP */
140   uint16_t handle;        /* WRITE, WRITE_BLOB */
141   uint16_t mtu;           /* exchange MTU request */
142 } tGATT_SR_MSG;
143 
144 /* Characteristic declaration attribute value
145 */
146 typedef struct {
147   tGATT_CHAR_PROP property;
148   uint16_t char_val_handle;
149 } tGATT_CHAR_DECL;
150 
151 /* attribute value maintained in the server database
152 */
153 typedef union {
154   tBT_UUID uuid;               /* service declaration */
155   tGATT_CHAR_DECL char_decl;   /* characteristic declaration */
156   tGATT_INCL_SRVC incl_handle; /* included service */
157 } tGATT_ATTR_VALUE;
158 
159 /* Attribute UUID type
160 */
161 #define GATT_ATTR_UUID_TYPE_16 0
162 #define GATT_ATTR_UUID_TYPE_128 1
163 #define GATT_ATTR_UUID_TYPE_32 2
164 typedef uint8_t tGATT_ATTR_UUID_TYPE;
165 
166 /* 16 bits UUID Attribute in server database
167 */
168 typedef struct {
169   std::unique_ptr<tGATT_ATTR_VALUE> p_value;
170   tGATT_PERM permission;
171   uint16_t handle;
172   tBT_UUID uuid;
173   bt_gatt_db_attribute_type_t gatt_type;
174 } tGATT_ATTR;
175 
176 /* Service Database definition
177 */
178 typedef struct {
179   std::vector<tGATT_ATTR> attr_list; /* pointer to the attributes */
180   uint16_t end_handle;       /* Last handle number           */
181   uint16_t next_handle;      /* Next usable handle value     */
182 } tGATT_SVC_DB;
183 
184 /* Data Structure used for GATT server */
185 /* An GATT registration record consists of a handle, and 1 or more attributes */
186 /* A service registration information record consists of beginning and ending */
187 /* attribute handle, service UUID and a set of GATT server callback.          */
188 
189 typedef struct {
190   tBT_UUID app_uuid128;
191   tGATT_CBACK app_cb;
192   tGATT_IF gatt_if; /* one based */
193   bool in_use;
194   uint8_t listening; /* if adv for all has been enabled */
195 } tGATT_REG;
196 
197 /* command queue for each connection */
198 typedef struct {
199   BT_HDR* p_cmd;
200   uint16_t clcb_idx;
201   uint8_t op_code;
202   bool to_send;
203 } tGATT_CMD_Q;
204 
205 #if GATT_MAX_SR_PROFILES <= 8
206 typedef uint8_t tGATT_APP_MASK;
207 #elif GATT_MAX_SR_PROFILES <= 16
208 typedef uint16_t tGATT_APP_MASK;
209 #elif GATT_MAX_SR_PROFILES <= 32
210 typedef uint32_t tGATT_APP_MASK;
211 #endif
212 
213 /* command details for each connection */
214 typedef struct {
215   BT_HDR* p_rsp_msg;
216   uint32_t trans_id;
217   tGATT_READ_MULTI multi_req;
218   fixed_queue_t* multi_rsp_q;
219   uint16_t handle;
220   uint8_t op_code;
221   uint8_t status;
222   uint8_t cback_cnt[GATT_MAX_APPS];
223 } tGATT_SR_CMD;
224 
225 #define GATT_CH_CLOSE 0
226 #define GATT_CH_CLOSING 1
227 #define GATT_CH_CONN 2
228 #define GATT_CH_CFG 3
229 #define GATT_CH_OPEN 4
230 
231 typedef uint8_t tGATT_CH_STATE;
232 
233 #define GATT_GATT_START_HANDLE 1
234 #define GATT_GAP_START_HANDLE 20
235 #define GATT_APP_START_HANDLE 40
236 
237 typedef struct hdl_cfg {
238   uint16_t gatt_start_hdl;
239   uint16_t gap_start_hdl;
240   uint16_t app_start_hdl;
241 } tGATT_HDL_CFG;
242 
243 typedef struct hdl_list_elem {
244   tGATTS_HNDL_RANGE asgn_range; /* assigned handle range */
245   tGATT_SVC_DB svc_db;
246 } tGATT_HDL_LIST_ELEM;
247 
248 /* Data Structure used for GATT server                                        */
249 /* A GATT registration record consists of a handle, and 1 or more attributes  */
250 /* A service registration information record consists of beginning and ending */
251 /* attribute handle, service UUID and a set of GATT server callback.          */
252 typedef struct {
253   tGATT_SVC_DB* p_db;  /* pointer to the service database */
254   tBT_UUID app_uuid;   /* applicatino UUID */
255   uint32_t sdp_handle; /* primamry service SDP handle */
256   uint16_t type;       /* service type UUID, primary or secondary */
257   uint16_t s_hdl;      /* service starting handle */
258   uint16_t e_hdl;      /* service ending handle */
259   tGATT_IF gatt_if;    /* this service is belong to which application */
260   bool is_primary;
261 } tGATT_SRV_LIST_ELEM;
262 
263 typedef struct {
264   fixed_queue_t* pending_enc_clcb; /* pending encryption channel q */
265   tGATT_SEC_ACTION sec_act;
266   BD_ADDR peer_bda;
267   tBT_TRANSPORT transport;
268   uint32_t trans_id;
269 
270   uint16_t att_lcid; /* L2CAP channel ID for ATT */
271   uint16_t payload_size;
272 
273   tGATT_CH_STATE ch_state;
274   uint8_t ch_flags;
275 
276   tGATT_IF app_hold_link[GATT_MAX_APPS];
277 
278   /* server needs */
279   /* server response data */
280   tGATT_SR_CMD sr_cmd;
281   uint16_t indicate_handle;
282   fixed_queue_t* pending_ind_q;
283 
284   alarm_t* conf_timer; /* peer confirm to indication timer */
285 
286   uint8_t prep_cnt[GATT_MAX_APPS];
287   uint8_t ind_count;
288 
289   tGATT_CMD_Q cl_cmd_q[GATT_CL_MAX_LCB];
290   alarm_t* ind_ack_timer; /* local app confirm to indication timer */
291   uint8_t pending_cl_req;
292   uint8_t next_slot_inq; /* index of next available slot in queue */
293 
294   bool in_use;
295   uint8_t tcb_idx;
296 } tGATT_TCB;
297 
298 /* logic channel */
299 typedef struct {
300   uint16_t
301       next_disc_start_hdl; /* starting handle for the next inc srvv discovery */
302   tGATT_DISC_RES result;
303   bool wait_for_read_rsp;
304 } tGATT_READ_INC_UUID128;
305 typedef struct {
306   tGATT_TCB* p_tcb; /* associated TCB of this CLCB */
307   tGATT_REG* p_reg; /* owner of this CLCB */
308   uint8_t sccb_idx;
309   uint8_t* p_attr_buf; /* attribute buffer for read multiple, prepare write */
310   tBT_UUID uuid;
311   uint16_t conn_id; /* connection handle */
312   uint16_t clcb_idx;
313   uint16_t s_handle; /* starting handle of the active request */
314   uint16_t e_handle; /* ending handle of the active request */
315   uint16_t counter; /* used as offset, attribute length, num of prepare write */
316   uint16_t start_offset;
317   tGATT_AUTH_REQ auth_req; /* authentication requirement */
318   uint8_t operation;       /* one logic channel can have one operation active */
319   uint8_t op_subtype;      /* operation subtype */
320   uint8_t status;          /* operation status */
321   bool first_read_blob_after_read;
322   tGATT_READ_INC_UUID128 read_uuid128;
323   bool in_use;
324   alarm_t* gatt_rsp_timer_ent; /* peer response timer */
325   uint8_t retry_count;
326 
327 } tGATT_CLCB;
328 
329 typedef struct { tGATT_CLCB* p_clcb; } tGATT_PENDING_ENC_CLCB;
330 
331 typedef struct {
332   uint16_t clcb_idx;
333   bool in_use;
334 } tGATT_SCCB;
335 
336 typedef struct {
337   uint16_t handle;
338   uint16_t uuid;
339   uint32_t service_change;
340 } tGATT_SVC_CHG;
341 
342 typedef struct {
343   tGATT_IF gatt_if[GATT_MAX_APPS];
344   BD_ADDR remote_bda;
345   bool in_use;
346 } tGATT_BG_CONN_DEV;
347 
348 #define GATT_SVC_CHANGED_CONNECTING 1     /* wait for connection */
349 #define GATT_SVC_CHANGED_SERVICE 2        /* GATT service discovery */
350 #define GATT_SVC_CHANGED_CHARACTERISTIC 3 /* service change char discovery */
351 #define GATT_SVC_CHANGED_DESCRIPTOR 4     /* service change CCC discoery */
352 #define GATT_SVC_CHANGED_CONFIGURE_CCCD 5 /* config CCC */
353 
354 typedef struct {
355   uint16_t conn_id;
356   bool in_use;
357   bool connected;
358   BD_ADDR bda;
359   tBT_TRANSPORT transport;
360 
361   /* GATT service change CCC related variables */
362   uint8_t ccc_stage;
363   uint8_t ccc_result;
364   uint16_t s_handle;
365   uint16_t e_handle;
366 } tGATT_PROFILE_CLCB;
367 
368 typedef struct {
369   tGATT_TCB tcb[GATT_MAX_PHY_CHANNEL];
370   fixed_queue_t* sign_op_queue;
371 
372   uint16_t next_handle;     /* next available handle */
373   uint16_t last_primary_s_handle; /* handle of last primary service */
374   tGATT_SVC_CHG gattp_attr; /* GATT profile attribute service change */
375   tGATT_IF gatt_if;
376   std::list<tGATT_HDL_LIST_ELEM>* hdl_list_info;
377   std::list<tGATT_SRV_LIST_ELEM>* srv_list_info;
378 
379   fixed_queue_t* srv_chg_clt_q; /* service change clients queue */
380   tGATT_REG cl_rcb[GATT_MAX_APPS];
381   tGATT_CLCB clcb[GATT_CL_MAX_LCB]; /* connection link control block*/
382   tGATT_SCCB sccb[GATT_MAX_SCCB];   /* sign complete callback function
383                                        GATT_MAX_SCCB <= GATT_CL_MAX_LCB */
384   uint8_t trace_level;
385   uint16_t def_mtu_size;
386 
387 #if (GATT_CONFORMANCE_TESTING == TRUE)
388   bool enable_err_rsp;
389   uint8_t req_op_code;
390   uint8_t err_status;
391   uint16_t handle;
392 #endif
393 
394   tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
395   uint16_t
396       handle_of_h_r; /* Handle of the handles reused characteristic value */
397 
398   tGATT_APPL_INFO cb_info;
399 
400   tGATT_HDL_CFG hdl_cfg;
401   tGATT_BG_CONN_DEV bgconn_dev[GATT_MAX_BG_CONN_DEV];
402 
403 } tGATT_CB;
404 
405 #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
406 
407 /* Global GATT data */
408 extern tGATT_CB gatt_cb;
409 
410 #if (GATT_CONFORMANCE_TESTING == TRUE)
411 extern void gatt_set_err_rsp(bool enable, uint8_t req_op_code,
412                              uint8_t err_status);
413 #endif
414 
415 /* from gatt_main.cc */
416 extern bool gatt_disconnect(tGATT_TCB* p_tcb);
417 extern bool gatt_act_connect(tGATT_REG* p_reg, BD_ADDR bd_addr,
418                              tBT_TRANSPORT transport, bool opportunistic,
419                              int8_t initiating_phys);
420 extern bool gatt_connect(BD_ADDR rem_bda, tGATT_TCB* p_tcb,
421                          tBT_TRANSPORT transport, uint8_t initiating_phys);
422 extern void gatt_data_process(tGATT_TCB* p_tcb, BT_HDR* p_buf);
423 extern void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
424                                           bool is_add, bool check_acl_link);
425 
426 extern void gatt_profile_db_init(void);
427 extern void gatt_set_ch_state(tGATT_TCB* p_tcb, tGATT_CH_STATE ch_state);
428 extern tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB* p_tcb);
429 extern void gatt_init_srv_chg(void);
430 extern void gatt_proc_srv_chg(void);
431 extern void gatt_send_srv_chg_ind(BD_ADDR peer_bda);
432 extern void gatt_chk_srv_chg(tGATTS_SRV_CHG* p_srv_chg_clt);
433 extern void gatt_add_a_bonded_dev_for_srv_chg(BD_ADDR bda);
434 
435 /* from gatt_attr.cc */
436 extern uint16_t gatt_profile_find_conn_id_by_bd_addr(BD_ADDR bda);
437 
438 /* Functions provided by att_protocol.cc */
439 extern tGATT_STATUS attp_send_cl_msg(tGATT_TCB* p_tcb, uint16_t clcb_idx,
440                                      uint8_t op_code, tGATT_CL_MSG* p_msg);
441 extern BT_HDR* attp_build_sr_msg(tGATT_TCB* p_tcb, uint8_t op_code,
442                                  tGATT_SR_MSG* p_msg);
443 extern tGATT_STATUS attp_send_sr_msg(tGATT_TCB* p_tcb, BT_HDR* p_msg);
444 extern tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB* p_tcb, BT_HDR* p_toL2CAP);
445 
446 /* utility functions */
447 extern uint8_t* gatt_dbg_op_name(uint8_t op_code);
448 extern uint32_t gatt_add_sdp_record(tBT_UUID* p_uuid, uint16_t start_hdl,
449                                     uint16_t end_hdl);
450 extern bool gatt_parse_uuid_from_cmd(tBT_UUID* p_uuid, uint16_t len,
451                                      uint8_t** p_data);
452 extern uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst, tBT_UUID uuid);
453 extern bool gatt_uuid_compare(tBT_UUID src, tBT_UUID tar);
454 extern void gatt_convert_uuid32_to_uuid128(uint8_t uuid_128[LEN_UUID_128],
455                                            uint32_t uuid_32);
456 extern void gatt_sr_get_sec_info(BD_ADDR rem_bda, tBT_TRANSPORT transport,
457                                  uint8_t* p_sec_flag, uint8_t* p_key_size);
458 extern void gatt_start_rsp_timer(uint16_t clcb_idx);
459 extern void gatt_start_conf_timer(tGATT_TCB* p_tcb);
460 extern void gatt_rsp_timeout(void* data);
461 extern void gatt_indication_confirmation_timeout(void* data);
462 extern void gatt_ind_ack_timeout(void* data);
463 extern void gatt_start_ind_ack_timer(tGATT_TCB* p_tcb);
464 extern tGATT_STATUS gatt_send_error_rsp(tGATT_TCB* p_tcb, uint8_t err_code,
465                                         uint8_t op_code, uint16_t handle,
466                                         bool deq);
467 extern void gatt_dbg_display_uuid(tBT_UUID bt_uuid);
468 extern tGATT_PENDING_ENC_CLCB* gatt_add_pending_enc_channel_clcb(
469     tGATT_TCB* p_tcb, tGATT_CLCB* p_clcb);
470 
471 extern bool gatt_is_srv_chg_ind_pending(tGATT_TCB* p_tcb);
472 extern tGATTS_SRV_CHG* gatt_is_bda_in_the_srv_chg_clt_list(BD_ADDR bda);
473 
474 extern bool gatt_find_the_connected_bda(uint8_t start_idx, BD_ADDR bda,
475                                         uint8_t* p_found_idx,
476                                         tBT_TRANSPORT* p_transport);
477 extern void gatt_set_srv_chg(void);
478 extern void gatt_delete_dev_from_srv_chg_clt_list(BD_ADDR bd_addr);
479 extern tGATT_VALUE* gatt_add_pending_ind(tGATT_TCB* p_tcb, tGATT_VALUE* p_ind);
480 extern void gatt_free_srvc_db_buffer_app_id(tBT_UUID* p_app_id);
481 extern bool gatt_cl_send_next_cmd_inq(tGATT_TCB* p_tcb);
482 
483 /* reserved handle list */
484 extern std::list<tGATT_HDL_LIST_ELEM>::iterator gatt_find_hdl_buffer_by_app_id(
485     tBT_UUID* p_app_uuid128, tBT_UUID* p_svc_uuid, uint16_t svc_inst);
486 extern tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle);
487 extern tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg);
488 
489 /* for background connection */
490 extern bool gatt_update_auto_connect_dev(tGATT_IF gatt_if, bool add,
491                                          BD_ADDR bd_addr);
492 extern bool gatt_is_bg_dev_for_app(tGATT_BG_CONN_DEV* p_dev, tGATT_IF gatt_if);
493 extern bool gatt_remove_bg_dev_for_app(tGATT_IF gatt_if, BD_ADDR bd_addr);
494 extern uint8_t gatt_get_num_apps_for_bg_dev(BD_ADDR bd_addr);
495 extern bool gatt_find_app_for_bg_dev(BD_ADDR bd_addr, tGATT_IF* p_gatt_if);
496 extern tGATT_BG_CONN_DEV* gatt_find_bg_dev(BD_ADDR remote_bda);
497 extern void gatt_deregister_bgdev_list(tGATT_IF gatt_if);
498 
499 /* server function */
500 extern std::list<tGATT_SRV_LIST_ELEM>::iterator gatt_sr_find_i_rcb_by_handle(
501     uint16_t handle);
502 extern bool gatt_sr_find_i_rcb_by_app_id(tBT_UUID* p_app_uuid128,
503                                          tBT_UUID* p_svc_uuid,
504                                          uint16_t svc_inst);
505 extern tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB* p_tcb, tGATT_IF gatt_if,
506                                             uint32_t trans_id, uint8_t op_code,
507                                             tGATT_STATUS status,
508                                             tGATTS_RSP* p_msg);
509 extern void gatt_server_handle_client_req(tGATT_TCB* p_tcb, uint8_t op_code,
510                                           uint16_t len, uint8_t* p_data);
511 extern void gatt_sr_send_req_callback(uint16_t conn_id, uint32_t trans_id,
512                                       uint8_t op_code, tGATTS_DATA* p_req_data);
513 extern uint32_t gatt_sr_enqueue_cmd(tGATT_TCB* p_tcb, uint8_t op_code,
514                                     uint16_t handle);
515 extern bool gatt_cancel_open(tGATT_IF gatt_if, BD_ADDR bda);
516 extern void gatt_notify_phy_updated(tGATT_TCB* p_tcb, uint8_t tx_phy,
517                                     uint8_t rx_phy, uint8_t status);
518 
519 /*   */
520 
521 extern tGATT_REG* gatt_get_regcb(tGATT_IF gatt_if);
522 extern bool gatt_is_clcb_allocated(uint16_t conn_id);
523 extern tGATT_CLCB* gatt_clcb_alloc(uint16_t conn_id);
524 extern void gatt_clcb_dealloc(tGATT_CLCB* p_clcb);
525 
526 extern void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB* p_tcb);
527 extern bool gatt_sr_is_cback_cnt_zero(tGATT_TCB* p_tcb);
528 extern bool gatt_sr_is_prep_cnt_zero(tGATT_TCB* p_tcb);
529 extern void gatt_sr_reset_cback_cnt(tGATT_TCB* p_tcb);
530 extern void gatt_sr_reset_prep_cnt(tGATT_TCB* p_tcb);
531 extern void gatt_sr_update_cback_cnt(tGATT_TCB* p_tcb, tGATT_IF gatt_if,
532                                      bool is_inc, bool is_reset_first);
533 extern void gatt_sr_update_prep_cnt(tGATT_TCB* p_tcb, tGATT_IF gatt_if,
534                                     bool is_inc, bool is_reset_first);
535 
536 extern bool gatt_find_app_hold_link(tGATT_TCB* p_tcb, uint8_t start_idx,
537                                     uint8_t* p_found_idx, tGATT_IF* p_gatt_if);
538 extern uint8_t gatt_num_apps_hold_link(tGATT_TCB* p_tcb);
539 extern uint8_t gatt_num_clcb_by_bd_addr(BD_ADDR bda);
540 extern tGATT_TCB* gatt_find_tcb_by_cid(uint16_t lcid);
541 extern tGATT_TCB* gatt_allocate_tcb_by_bdaddr(BD_ADDR bda,
542                                               tBT_TRANSPORT transport);
543 extern tGATT_TCB* gatt_get_tcb_by_idx(uint8_t tcb_idx);
544 extern tGATT_TCB* gatt_find_tcb_by_addr(BD_ADDR bda, tBT_TRANSPORT transport);
545 extern bool gatt_send_ble_burst_data(BD_ADDR remote_bda, BT_HDR* p_buf);
546 
547 /* GATT client functions */
548 extern void gatt_dequeue_sr_cmd(tGATT_TCB* p_tcb);
549 extern uint8_t gatt_send_write_msg(tGATT_TCB* p_tcb, uint16_t clcb_idx,
550                                    uint8_t op_code, uint16_t handle,
551                                    uint16_t len, uint16_t offset,
552                                    uint8_t* p_data);
553 extern void gatt_cleanup_upon_disc(BD_ADDR bda, uint16_t reason,
554                                    tBT_TRANSPORT transport);
555 extern void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status,
556                                void* p_data);
557 
558 extern void gatt_act_discovery(tGATT_CLCB* p_clcb);
559 extern void gatt_act_read(tGATT_CLCB* p_clcb, uint16_t offset);
560 extern void gatt_act_write(tGATT_CLCB* p_clcb, uint8_t sec_act);
561 extern uint8_t gatt_act_send_browse(tGATT_TCB* p_tcb, uint16_t index,
562                                     uint8_t op, uint16_t s_handle,
563                                     uint16_t e_handle, tBT_UUID uuid);
564 extern tGATT_CLCB* gatt_cmd_dequeue(tGATT_TCB* p_tcb, uint8_t* p_opcode);
565 extern bool gatt_cmd_enq(tGATT_TCB* p_tcb, uint16_t clcb_idx, bool to_send,
566                          uint8_t op_code, BT_HDR* p_buf);
567 extern void gatt_client_handle_server_rsp(tGATT_TCB* p_tcb, uint8_t op_code,
568                                           uint16_t len, uint8_t* p_data);
569 extern void gatt_send_queue_write_cancel(tGATT_TCB* p_tcb, tGATT_CLCB* p_clcb,
570                                          tGATT_EXEC_FLAG flag);
571 
572 /* gatt_auth.cc */
573 extern bool gatt_security_check_start(tGATT_CLCB* p_clcb);
574 extern void gatt_verify_signature(tGATT_TCB* p_tcb, BT_HDR* p_buf);
575 extern tGATT_SEC_ACTION gatt_determine_sec_act(tGATT_CLCB* p_clcb);
576 extern tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB* p_tcb);
577 extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb);
578 extern void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act);
579 
580 /* gatt_db.cc */
581 extern void gatts_init_service_db(tGATT_SVC_DB& db, tBT_UUID* p_service,
582                                   bool is_pri, uint16_t s_hdl,
583                                   uint16_t num_handle);
584 extern uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle,
585                                            uint16_t e_handle, tBT_UUID service);
586 extern uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
587                                          tGATT_CHAR_PROP property,
588                                          tBT_UUID& char_uuid);
589 extern uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm,
590                                      tBT_UUID& dscp_uuid);
591 extern tGATT_STATUS gatts_db_read_attr_value_by_type(
592     tGATT_TCB* p_tcb, tGATT_SVC_DB* p_db, uint8_t op_code, BT_HDR* p_rsp,
593     uint16_t s_handle, uint16_t e_handle, tBT_UUID type, uint16_t* p_len,
594     tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id,
595     uint16_t* p_cur_handle);
596 extern tGATT_STATUS gatts_read_attr_value_by_handle(
597     tGATT_TCB* p_tcb, tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle,
598     uint16_t offset, uint8_t* p_value, uint16_t* p_len, uint16_t mtu,
599     tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id);
600 extern tGATT_STATUS gatts_write_attr_perm_check(
601     tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle, uint16_t offset,
602     uint8_t* p_data, uint16_t len, tGATT_SEC_FLAG sec_flag, uint8_t key_size);
603 extern tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB* p_db, bool is_long,
604                                                uint16_t handle,
605                                                tGATT_SEC_FLAG sec_flag,
606                                                uint8_t key_size);
607 extern tBT_UUID* gatts_get_service_uuid(tGATT_SVC_DB* p_db);
608 
609 #endif
610