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