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