• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define GATT_SVC_CHANGED_CONNECTING 1     /* wait for connection */
335 #define GATT_SVC_CHANGED_SERVICE 2        /* GATT service discovery */
336 #define GATT_SVC_CHANGED_CHARACTERISTIC 3 /* service change char discovery */
337 #define GATT_SVC_CHANGED_DESCRIPTOR 4     /* service change CCC discoery */
338 #define GATT_SVC_CHANGED_CONFIGURE_CCCD 5 /* config CCC */
339 
340 typedef struct {
341   uint16_t conn_id;
342   bool in_use;
343   bool connected;
344   RawAddress bda;
345   tBT_TRANSPORT transport;
346 
347   /* GATT service change CCC related variables */
348   uint8_t ccc_stage;
349   uint8_t ccc_result;
350   uint16_t s_handle;
351   uint16_t e_handle;
352 } tGATT_PROFILE_CLCB;
353 
354 typedef struct {
355   tGATT_TCB tcb[GATT_MAX_PHY_CHANNEL];
356   fixed_queue_t* sign_op_queue;
357 
358   uint16_t next_handle;     /* next available handle */
359   uint16_t last_service_handle; /* handle of last service */
360   tGATT_SVC_CHG gattp_attr; /* GATT profile attribute service change */
361   tGATT_IF gatt_if;
362   std::list<tGATT_HDL_LIST_ELEM>* hdl_list_info;
363   std::list<tGATT_SRV_LIST_ELEM>* srv_list_info;
364 
365   fixed_queue_t* srv_chg_clt_q; /* service change clients queue */
366   tGATT_REG cl_rcb[GATT_MAX_APPS];
367   tGATT_CLCB clcb[GATT_CL_MAX_LCB]; /* connection link control block*/
368   uint16_t def_mtu_size;
369 
370 #if (GATT_CONFORMANCE_TESTING == TRUE)
371   bool enable_err_rsp;
372   uint8_t req_op_code;
373   uint8_t err_status;
374   uint16_t handle;
375 #endif
376 
377   tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
378   uint16_t
379       handle_of_h_r; /* Handle of the handles reused characteristic value */
380 
381   tGATT_APPL_INFO cb_info;
382 
383   tGATT_HDL_CFG hdl_cfg;
384 } tGATT_CB;
385 
386 #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
387 
388 /* Global GATT data */
389 extern tGATT_CB gatt_cb;
390 
391 #if (GATT_CONFORMANCE_TESTING == TRUE)
392 extern void gatt_set_err_rsp(bool enable, uint8_t req_op_code,
393                              uint8_t err_status);
394 #endif
395 
396 /* from gatt_main.cc */
397 extern bool gatt_disconnect(tGATT_TCB* p_tcb);
398 extern bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
399                              tBT_TRANSPORT transport, int8_t initiating_phys);
400 extern bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
401                          tBT_TRANSPORT transport, uint8_t initiating_phys,
402                          tGATT_IF gatt_if);
403 extern void gatt_data_process(tGATT_TCB& p_tcb, BT_HDR* p_buf);
404 extern void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
405                                           bool is_add, bool check_acl_link);
406 
407 extern void gatt_profile_db_init(void);
408 extern void gatt_set_ch_state(tGATT_TCB* p_tcb, tGATT_CH_STATE ch_state);
409 extern tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB* p_tcb);
410 extern void gatt_init_srv_chg(void);
411 extern void gatt_proc_srv_chg(void);
412 extern void gatt_send_srv_chg_ind(const RawAddress& peer_bda);
413 extern void gatt_chk_srv_chg(tGATTS_SRV_CHG* p_srv_chg_clt);
414 extern void gatt_add_a_bonded_dev_for_srv_chg(const RawAddress& bda);
415 
416 /* from gatt_attr.cc */
417 extern uint16_t gatt_profile_find_conn_id_by_bd_addr(const RawAddress& bda);
418 
419 /* Functions provided by att_protocol.cc */
420 extern tGATT_STATUS attp_send_cl_msg(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
421                                      uint8_t op_code, tGATT_CL_MSG* p_msg);
422 extern BT_HDR* attp_build_sr_msg(tGATT_TCB& tcb, uint8_t op_code,
423                                  tGATT_SR_MSG* p_msg);
424 extern tGATT_STATUS attp_send_sr_msg(tGATT_TCB& tcb, BT_HDR* p_msg);
425 extern tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB& tcb, BT_HDR* p_toL2CAP);
426 
427 /* utility functions */
428 extern uint8_t* gatt_dbg_op_name(uint8_t op_code);
429 extern uint32_t gatt_add_sdp_record(const bluetooth::Uuid& uuid,
430                                     uint16_t start_hdl, uint16_t end_hdl);
431 extern bool gatt_parse_uuid_from_cmd(bluetooth::Uuid* p_uuid, uint16_t len,
432                                      uint8_t** p_data);
433 extern uint8_t gatt_build_uuid_to_stream_len(const bluetooth::Uuid& uuid);
434 extern uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst,
435                                          const bluetooth::Uuid& uuid);
436 extern void gatt_sr_get_sec_info(const RawAddress& rem_bda,
437                                  tBT_TRANSPORT transport, uint8_t* p_sec_flag,
438                                  uint8_t* p_key_size);
439 extern void gatt_start_rsp_timer(tGATT_CLCB* p_clcb);
440 extern void gatt_start_conf_timer(tGATT_TCB* p_tcb);
441 extern void gatt_rsp_timeout(void* data);
442 extern void gatt_indication_confirmation_timeout(void* data);
443 extern void gatt_ind_ack_timeout(void* data);
444 extern void gatt_start_ind_ack_timer(tGATT_TCB& tcb);
445 extern tGATT_STATUS gatt_send_error_rsp(tGATT_TCB& tcb, uint8_t err_code,
446                                         uint8_t op_code, uint16_t handle,
447                                         bool deq);
448 
449 extern bool gatt_is_srv_chg_ind_pending(tGATT_TCB* p_tcb);
450 extern tGATTS_SRV_CHG* gatt_is_bda_in_the_srv_chg_clt_list(
451     const RawAddress& bda);
452 
453 extern bool gatt_find_the_connected_bda(uint8_t start_idx, RawAddress& bda,
454                                         uint8_t* p_found_idx,
455                                         tBT_TRANSPORT* p_transport);
456 extern void gatt_set_srv_chg(void);
457 extern void gatt_delete_dev_from_srv_chg_clt_list(const RawAddress& bd_addr);
458 extern void gatt_add_pending_ind(tGATT_TCB* p_tcb, tGATT_VALUE* p_ind);
459 extern void gatt_free_srvc_db_buffer_app_id(const bluetooth::Uuid& app_id);
460 extern bool gatt_cl_send_next_cmd_inq(tGATT_TCB& tcb);
461 
462 /* reserved handle list */
463 extern std::list<tGATT_HDL_LIST_ELEM>::iterator gatt_find_hdl_buffer_by_app_id(
464     const bluetooth::Uuid& app_uuid128, bluetooth::Uuid* p_svc_uuid,
465     uint16_t svc_inst);
466 extern tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle);
467 extern tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg);
468 
469 /* for background connection */
470 extern bool gatt_auto_connect_dev_remove(tGATT_IF gatt_if,
471                                          const RawAddress& bd_addr);
472 
473 /* server function */
474 extern std::list<tGATT_SRV_LIST_ELEM>::iterator gatt_sr_find_i_rcb_by_handle(
475     uint16_t handle);
476 extern tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
477                                             uint32_t trans_id, uint8_t op_code,
478                                             tGATT_STATUS status,
479                                             tGATTS_RSP* p_msg);
480 extern void gatt_server_handle_client_req(tGATT_TCB& p_tcb, uint8_t op_code,
481                                           uint16_t len, uint8_t* p_data);
482 extern void gatt_sr_send_req_callback(uint16_t conn_id, uint32_t trans_id,
483                                       uint8_t op_code, tGATTS_DATA* p_req_data);
484 extern uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code,
485                                     uint16_t handle);
486 extern bool gatt_cancel_open(tGATT_IF gatt_if, const RawAddress& bda);
487 extern void gatt_notify_phy_updated(uint8_t status, uint16_t handle,
488                                     uint8_t tx_phy, uint8_t rx_phy);
489 /*   */
490 
491 extern tGATT_REG* gatt_get_regcb(tGATT_IF gatt_if);
492 extern bool gatt_is_clcb_allocated(uint16_t conn_id);
493 extern tGATT_CLCB* gatt_clcb_alloc(uint16_t conn_id);
494 extern void gatt_clcb_dealloc(tGATT_CLCB* p_clcb);
495 
496 extern void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB& p_tcb);
497 extern bool gatt_sr_is_cback_cnt_zero(tGATT_TCB& p_tcb);
498 extern bool gatt_sr_is_prep_cnt_zero(tGATT_TCB& p_tcb);
499 extern void gatt_sr_reset_cback_cnt(tGATT_TCB& p_tcb);
500 extern void gatt_sr_reset_prep_cnt(tGATT_TCB& tcb);
501 extern void gatt_sr_update_cback_cnt(tGATT_TCB& p_tcb, tGATT_IF gatt_if,
502                                      bool is_inc, bool is_reset_first);
503 extern void gatt_sr_update_prep_cnt(tGATT_TCB& tcb, tGATT_IF gatt_if,
504                                     bool is_inc, bool is_reset_first);
505 
506 extern uint8_t gatt_num_clcb_by_bd_addr(const RawAddress& bda);
507 extern tGATT_TCB* gatt_find_tcb_by_cid(uint16_t lcid);
508 extern tGATT_TCB* gatt_allocate_tcb_by_bdaddr(const RawAddress& bda,
509                                               tBT_TRANSPORT transport);
510 extern tGATT_TCB* gatt_get_tcb_by_idx(uint8_t tcb_idx);
511 extern tGATT_TCB* gatt_find_tcb_by_addr(const RawAddress& bda,
512                                         tBT_TRANSPORT transport);
513 extern bool gatt_send_ble_burst_data(const RawAddress& remote_bda,
514                                      BT_HDR* p_buf);
515 
516 /* GATT client functions */
517 extern void gatt_dequeue_sr_cmd(tGATT_TCB& tcb);
518 extern uint8_t gatt_send_write_msg(tGATT_TCB& p_tcb, tGATT_CLCB* p_clcb,
519                                    uint8_t op_code, uint16_t handle,
520                                    uint16_t len, uint16_t offset,
521                                    uint8_t* p_data);
522 extern void gatt_cleanup_upon_disc(const RawAddress& bda, uint16_t reason,
523                                    tBT_TRANSPORT transport);
524 extern void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status,
525                                void* p_data);
526 
527 extern void gatt_act_discovery(tGATT_CLCB* p_clcb);
528 extern void gatt_act_read(tGATT_CLCB* p_clcb, uint16_t offset);
529 extern void gatt_act_write(tGATT_CLCB* p_clcb, uint8_t sec_act);
530 extern tGATT_CLCB* gatt_cmd_dequeue(tGATT_TCB& tcb, uint8_t* p_opcode);
531 extern void gatt_cmd_enq(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, bool to_send,
532                          uint8_t op_code, BT_HDR* p_buf);
533 extern void gatt_client_handle_server_rsp(tGATT_TCB& tcb, uint8_t op_code,
534                                           uint16_t len, uint8_t* p_data);
535 extern void gatt_send_queue_write_cancel(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
536                                          tGATT_EXEC_FLAG flag);
537 
538 /* gatt_auth.cc */
539 extern bool gatt_security_check_start(tGATT_CLCB* p_clcb);
540 extern void gatt_verify_signature(tGATT_TCB& tcb, BT_HDR* p_buf);
541 extern tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB& tcb);
542 extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb);
543 extern void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act);
544 
545 /* gatt_db.cc */
546 extern void gatts_init_service_db(tGATT_SVC_DB& db,
547                                   const bluetooth::Uuid& service, bool is_pri,
548                                   uint16_t s_hdl, uint16_t num_handle);
549 extern uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle,
550                                            uint16_t e_handle,
551                                            const bluetooth::Uuid& service);
552 extern uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
553                                          tGATT_CHAR_PROP property,
554                                          const bluetooth::Uuid& char_uuid);
555 extern uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm,
556                                      const bluetooth::Uuid& dscp_uuid);
557 extern tGATT_STATUS gatts_db_read_attr_value_by_type(
558     tGATT_TCB& tcb, tGATT_SVC_DB* p_db, uint8_t op_code, BT_HDR* p_rsp,
559     uint16_t s_handle, uint16_t e_handle, const bluetooth::Uuid& type,
560     uint16_t* p_len, tGATT_SEC_FLAG sec_flag, uint8_t key_size,
561     uint32_t trans_id, uint16_t* p_cur_handle);
562 extern tGATT_STATUS gatts_read_attr_value_by_handle(
563     tGATT_TCB& tcb, tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle,
564     uint16_t offset, uint8_t* p_value, uint16_t* p_len, uint16_t mtu,
565     tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id);
566 extern tGATT_STATUS gatts_write_attr_perm_check(
567     tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle, uint16_t offset,
568     uint8_t* p_data, uint16_t len, tGATT_SEC_FLAG sec_flag, uint8_t key_size);
569 extern tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB* p_db, bool is_long,
570                                                uint16_t handle,
571                                                tGATT_SEC_FLAG sec_flag,
572                                                uint8_t key_size);
573 extern bluetooth::Uuid* gatts_get_service_uuid(tGATT_SVC_DB* p_db);
574 
575 #endif
576