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 typedef enum : uint8_t {
45   GATT_SEC_NONE = 0,
46   GATT_SEC_OK = 1,
47   GATT_SEC_SIGN_DATA = 2,       /* compute the signature for the write cmd */
48   GATT_SEC_ENCRYPT = 3,         /* encrypt the link with current key */
49   GATT_SEC_ENCRYPT_NO_MITM = 4, /* unauthenticated encryption or better */
50   GATT_SEC_ENCRYPT_MITM = 5,    /* authenticated encryption */
51   GATT_SEC_ENC_PENDING = 6,     /* wait for link encryption pending */
52 } tGATT_SEC_ACTION;
53 
54 #define CASE_RETURN_TEXT(code) \
55   case code:                   \
56     return #code
57 
gatt_security_action_text(const tGATT_SEC_ACTION & action)58 inline std::string gatt_security_action_text(const tGATT_SEC_ACTION& action) {
59   switch (action) {
60     CASE_RETURN_TEXT(GATT_SEC_NONE);
61     CASE_RETURN_TEXT(GATT_SEC_OK);
62     CASE_RETURN_TEXT(GATT_SEC_SIGN_DATA);
63     CASE_RETURN_TEXT(GATT_SEC_ENCRYPT);
64     CASE_RETURN_TEXT(GATT_SEC_ENCRYPT_NO_MITM);
65     CASE_RETURN_TEXT(GATT_SEC_ENCRYPT_MITM);
66     CASE_RETURN_TEXT(GATT_SEC_ENC_PENDING);
67     default:
68       return std::string("UNKNOWN[%hhu]", action);
69   }
70 }
71 
72 #undef CASE_RETURN_TEXT
73 
74 #define GATT_INDEX_INVALID 0xff
75 
76 #define GATT_WRITE_CMD_MASK 0xc0 /*0x1100-0000*/
77 #define GATT_AUTH_SIGN_MASK 0x80 /*0x1000-0000*/
78 #define GATT_AUTH_SIGN_LEN 12
79 
80 #define GATT_HDR_SIZE 3 /* 1B opcode + 2B handle */
81 
82 /* wait for ATT cmd response timeout value */
83 #define GATT_WAIT_FOR_RSP_TIMEOUT_MS (30 * 1000)
84 #define GATT_WAIT_FOR_DISC_RSP_TIMEOUT_MS (5 * 1000)
85 #define GATT_REQ_RETRY_LIMIT 2
86 
87 #define GATT_SEC_FLAG_LKEY_UNAUTHED BTM_SEC_FLAG_LKEY_KNOWN
88 #define GATT_SEC_FLAG_LKEY_AUTHED BTM_SEC_FLAG_LKEY_AUTHED
89 #define GATT_SEC_FLAG_ENCRYPTED BTM_SEC_FLAG_ENCRYPTED
90 typedef uint8_t tGATT_SEC_FLAG;
91 
92 /* Find Information Response Type
93 */
94 #define GATT_INFO_TYPE_PAIR_16 0x01
95 #define GATT_INFO_TYPE_PAIR_128 0x02
96 
97 constexpr bool kGattConnected = true;
98 constexpr bool kGattDisconnected = !kGattConnected;
99 
100 /*  GATT client FIND_TYPE_VALUE_Request data */
101 typedef struct {
102   bluetooth::Uuid uuid; /* type of attribute to be found */
103   uint16_t s_handle;  /* starting handle */
104   uint16_t e_handle;  /* ending handle */
105   uint16_t value_len; /* length of the attribute value */
106   uint8_t
107       value[GATT_MAX_MTU_SIZE]; /* pointer to the attribute value to be found */
108 } tGATT_FIND_TYPE_VALUE;
109 
110 /* client request message to ATT protocol
111 */
112 typedef union {
113   tGATT_READ_BY_TYPE browse;             /* read by type request */
114   tGATT_FIND_TYPE_VALUE find_type_value; /* find by type value */
115   tGATT_READ_MULTI read_multi;           /* read multiple request */
116   tGATT_READ_PARTIAL read_blob;          /* read blob */
117   tGATT_VALUE attr_value;                /* write request */
118                                          /* prepare write */
119   /* write blob */
120   uint16_t handle; /* read,  handle value confirmation */
121   uint16_t mtu;
122   tGATT_EXEC_FLAG exec_write; /* execute write */
123 } tGATT_CL_MSG;
124 
125 /* error response strucutre */
126 typedef struct {
127   uint16_t handle;
128   uint8_t cmd_code;
129   uint8_t reason;
130 } tGATT_ERROR;
131 
132 /* server response message to ATT protocol
133 */
134 typedef union {
135   /* data type            member          event   */
136   tGATT_VALUE attr_value; /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
137                           /* READ_BLOB, READ_BY_TYPE */
138   tGATT_ERROR error;      /* ERROR_RSP */
139   uint16_t handle;        /* WRITE, WRITE_BLOB */
140   uint16_t mtu;           /* exchange MTU request */
141 } tGATT_SR_MSG;
142 
143 /* Characteristic declaration attribute value
144 */
145 typedef struct {
146   tGATT_CHAR_PROP property;
147   uint16_t char_val_handle;
148 } tGATT_CHAR_DECL;
149 
150 /* attribute value maintained in the server database
151 */
152 typedef union {
153   bluetooth::Uuid uuid;        /* service declaration */
154   tGATT_CHAR_DECL char_decl;   /* characteristic declaration */
155   tGATT_INCL_SRVC incl_handle; /* included service */
156   uint16_t char_ext_prop;      /* Characteristic Extended Properties */
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   bluetooth::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   bluetooth::Uuid app_uuid128;
191   tGATT_CBACK app_cb{};
192   tGATT_IF gatt_if{0}; /* one based */
193   bool in_use{false};
194   uint8_t listening{0}; /* if adv for all has been enabled */
195   bool eatt_support{false};
196   std::string name;
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   uint16_t cid;
208 } tGATT_CMD_Q;
209 
210 #if GATT_MAX_SR_PROFILES <= 8
211 typedef uint8_t tGATT_APP_MASK;
212 #elif GATT_MAX_SR_PROFILES <= 16
213 typedef uint16_t tGATT_APP_MASK;
214 #elif GATT_MAX_SR_PROFILES <= 32
215 typedef uint32_t tGATT_APP_MASK;
216 #endif
217 
218 /* command details for each connection */
219 typedef struct {
220   BT_HDR* p_rsp_msg;
221   uint32_t trans_id;
222   tGATT_READ_MULTI multi_req;
223   fixed_queue_t* multi_rsp_q;
224   uint16_t handle;
225   uint8_t op_code;
226   uint8_t status;
227   uint8_t cback_cnt[GATT_MAX_APPS];
228   uint16_t cid;
229 } tGATT_SR_CMD;
230 
231 typedef enum : uint8_t {
232   GATT_CH_CLOSE = 0,
233   GATT_CH_CLOSING = 1,
234   GATT_CH_CONN = 2,
235   GATT_CH_CFG = 3,
236   GATT_CH_OPEN = 4,
237 } tGATT_CH_STATE;
238 
239 #define CASE_RETURN_TEXT(code) \
240   case code:                   \
241     return #code
242 
gatt_channel_state_text(const tGATT_CH_STATE & state)243 inline std::string gatt_channel_state_text(const tGATT_CH_STATE& state) {
244   switch (state) {
245     CASE_RETURN_TEXT(GATT_CH_CLOSE);
246     CASE_RETURN_TEXT(GATT_CH_CLOSING);
247     CASE_RETURN_TEXT(GATT_CH_CONN);
248     CASE_RETURN_TEXT(GATT_CH_CFG);
249     CASE_RETURN_TEXT(GATT_CH_OPEN);
250     default:
251       return std::string("UNKNOWN[%hhu]", state);
252   }
253 }
254 #undef CASE_RETURN_TEXT
255 
256 #define GATT_GATT_START_HANDLE 1
257 #define GATT_GAP_START_HANDLE 20
258 #define GATT_APP_START_HANDLE 40
259 
260 typedef struct hdl_cfg {
261   uint16_t gatt_start_hdl;
262   uint16_t gap_start_hdl;
263   uint16_t app_start_hdl;
264 } tGATT_HDL_CFG;
265 
266 typedef struct hdl_list_elem {
267   tGATTS_HNDL_RANGE asgn_range; /* assigned handle range */
268   tGATT_SVC_DB svc_db;
269 } tGATT_HDL_LIST_ELEM;
270 
271 /* Data Structure used for GATT server                                        */
272 /* A GATT registration record consists of a handle, and 1 or more attributes  */
273 /* A service registration information record consists of beginning and ending */
274 /* attribute handle, service UUID and a set of GATT server callback.          */
275 typedef struct {
276   tGATT_SVC_DB* p_db;  /* pointer to the service database */
277   bluetooth::Uuid app_uuid; /* application UUID */
278   uint32_t sdp_handle; /* primamry service SDP handle */
279   uint16_t type;       /* service type UUID, primary or secondary */
280   uint16_t s_hdl;      /* service starting handle */
281   uint16_t e_hdl;      /* service ending handle */
282   tGATT_IF gatt_if;    /* this service is belong to which application */
283   bool is_primary;
284 } tGATT_SRV_LIST_ELEM;
285 
286 typedef struct {
287   std::queue<tGATT_CLCB*> pending_enc_clcb; /* pending encryption channel q */
288   tGATT_SEC_ACTION sec_act;
289   RawAddress peer_bda;
290   tBT_TRANSPORT transport;
291   uint32_t trans_id;
292 
293   /* Indicates number of available eatt channels */
294   uint8_t eatt;
295 
296   uint16_t att_lcid; /* L2CAP channel ID for ATT */
297   uint16_t payload_size;
298 
299   tGATT_CH_STATE ch_state;
300 
301   std::unordered_set<uint8_t> app_hold_link;
302 
303   /* server needs */
304   /* server response data */
305   tGATT_SR_CMD sr_cmd;
306   uint16_t indicate_handle;
307   fixed_queue_t* pending_ind_q;
308 
309   alarm_t* conf_timer; /* peer confirm to indication timer */
310 
311   uint8_t prep_cnt[GATT_MAX_APPS];
312   uint8_t ind_count;
313 
314   std::queue<tGATT_CMD_Q> cl_cmd_q;
315   alarm_t* ind_ack_timer; /* local app confirm to indication timer */
316 
317   // TODO(hylo): support byte array data
318   /* Client supported feature*/
319   uint8_t cl_supp_feat;
320   /* Server supported features */
321   uint8_t sr_supp_feat;
322   /* Use for server. if false, should handle database out of sync. */
323   bool is_robust_cache_change_aware;
324 
325   bool in_use;
326   uint8_t tcb_idx;
327 } tGATT_TCB;
328 
329 /* logic channel */
330 typedef struct {
331   uint16_t
332       next_disc_start_hdl; /* starting handle for the next inc srvv discovery */
333   tGATT_DISC_RES result;
334   bool wait_for_read_rsp;
335 } tGATT_READ_INC_UUID128;
336 struct tGATT_CLCB {
337   tGATT_TCB* p_tcb; /* associated TCB of this CLCB */
338   tGATT_REG* p_reg; /* owner of this CLCB */
339   uint8_t sccb_idx;
340   uint8_t* p_attr_buf; /* attribute buffer for read multiple, prepare write */
341   bluetooth::Uuid uuid;
342   uint16_t conn_id; /* connection handle */
343   uint16_t s_handle; /* starting handle of the active request */
344   uint16_t e_handle; /* ending handle of the active request */
345   uint16_t counter; /* used as offset, attribute length, num of prepare write */
346   uint16_t start_offset;
347   tGATT_AUTH_REQ auth_req; /* authentication requirement */
348   tGATTC_OPTYPE operation; /* one logic channel can have one operation active */
349   uint8_t op_subtype;      /* operation subtype */
350   tGATT_STATUS status;     /* operation status */
351   bool first_read_blob_after_read;
352   tGATT_READ_INC_UUID128 read_uuid128;
353   bool in_use;
354   alarm_t* gatt_rsp_timer_ent; /* peer response timer */
355   uint8_t retry_count;
356   uint16_t read_req_current_mtu; /* This is the MTU value that the read was
357                                     initiated with */
358   uint16_t cid;
359 };
360 
361 typedef struct {
362   uint16_t handle;
363   uint16_t uuid;
364   uint32_t service_change;
365 } tGATT_SVC_CHG;
366 
367 #define GATT_SVC_CHANGED_CONNECTING 1     /* wait for connection */
368 #define GATT_SVC_CHANGED_SERVICE 2        /* GATT service discovery */
369 #define GATT_SVC_CHANGED_CHARACTERISTIC 3 /* service change char discovery */
370 #define GATT_SVC_CHANGED_DESCRIPTOR 4     /* service change CCC discoery */
371 #define GATT_SVC_CHANGED_CONFIGURE_CCCD 5 /* config CCC */
372 
373 typedef struct {
374   uint16_t conn_id;
375   bool in_use;
376   bool connected;
377   RawAddress bda;
378   tBT_TRANSPORT transport;
379 
380   /* GATT service change CCC related variables */
381   uint8_t ccc_stage;
382   uint8_t ccc_result;
383   uint16_t s_handle;
384   uint16_t e_handle;
385 } tGATT_PROFILE_CLCB;
386 
387 typedef struct {
388   tGATT_TCB tcb[GATT_MAX_PHY_CHANNEL];
389   fixed_queue_t* sign_op_queue;
390 
391   uint16_t next_handle;     /* next available handle */
392   uint16_t last_service_handle; /* handle of last service */
393   tGATT_SVC_CHG gattp_attr; /* GATT profile attribute service change */
394   tGATT_IF gatt_if;
395   std::list<tGATT_HDL_LIST_ELEM>* hdl_list_info;
396   std::list<tGATT_SRV_LIST_ELEM>* srv_list_info;
397 
398   fixed_queue_t* srv_chg_clt_q; /* service change clients queue */
399   tGATT_REG cl_rcb[GATT_MAX_APPS];
400   tGATT_CLCB clcb[GATT_CL_MAX_LCB]; /* connection link control block*/
401 
402 #if (GATT_CONFORMANCE_TESTING == TRUE)
403   bool enable_err_rsp;
404   uint8_t req_op_code;
405   uint8_t err_status;
406   uint16_t handle;
407 #endif
408 
409   tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
410   uint16_t
411       handle_of_h_r; /* Handle of the handles reused characteristic value */
412   uint16_t handle_cl_supported_feat;
413   uint16_t handle_sr_supported_feat;
414   uint8_t
415       gatt_svr_supported_feat_mask; /* Local supported features as a server */
416 
417   /* Supported features as a client. To be written to remote device.
418    * Note this is NOT a value of the characteristic with handle
419    * handle_cl_support_feat, as that one should be written by remote device.
420    */
421   uint8_t gatt_cl_supported_feat_mask;
422 
423   uint16_t handle_of_database_hash;
424   Octet16 database_hash;
425 
426   tGATT_APPL_INFO cb_info;
427 
428   tGATT_HDL_CFG hdl_cfg;
429 } tGATT_CB;
430 
431 #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
432 
433 /* Global GATT data */
434 extern tGATT_CB gatt_cb;
435 
436 #if (GATT_CONFORMANCE_TESTING == TRUE)
437 extern void gatt_set_err_rsp(bool enable, uint8_t req_op_code,
438                              uint8_t err_status);
439 #endif
440 
441 /* from gatt_main.cc */
442 extern bool gatt_disconnect(tGATT_TCB* p_tcb);
443 extern bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
444                              tBT_TRANSPORT transport, int8_t initiating_phys);
445 extern bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
446                          tBT_TRANSPORT transport, uint8_t initiating_phys,
447                          tGATT_IF gatt_if);
448 extern void gatt_data_process(tGATT_TCB& p_tcb, uint16_t cid, BT_HDR* p_buf);
449 extern void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
450                                           bool is_add, bool check_acl_link);
451 
452 extern void gatt_profile_db_init(void);
453 extern void gatt_set_ch_state(tGATT_TCB* p_tcb, tGATT_CH_STATE ch_state);
454 extern tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB* p_tcb);
455 extern void gatt_init_srv_chg(void);
456 extern void gatt_proc_srv_chg(void);
457 extern void gatt_send_srv_chg_ind(const RawAddress& peer_bda);
458 extern void gatt_chk_srv_chg(tGATTS_SRV_CHG* p_srv_chg_clt);
459 extern void gatt_add_a_bonded_dev_for_srv_chg(const RawAddress& bda);
460 
461 /* from gatt_attr.cc */
462 extern uint16_t gatt_profile_find_conn_id_by_bd_addr(const RawAddress& bda);
463 
464 extern bool gatt_profile_get_eatt_support(const RawAddress& remote_bda);
465 extern void gatt_cl_init_sr_status(tGATT_TCB& tcb);
466 extern bool gatt_cl_read_sr_supp_feat_req(
467     const RawAddress& peer_bda,
468     base::OnceCallback<void(const RawAddress&, uint8_t)> cb);
469 
470 extern bool gatt_sr_is_cl_change_aware(tGATT_TCB& tcb);
471 extern void gatt_sr_init_cl_status(tGATT_TCB& tcb);
472 extern void gatt_sr_update_cl_status(tGATT_TCB& tcb, bool chg_unaware);
473 
474 /* Functions provided by att_protocol.cc */
475 extern tGATT_STATUS attp_send_cl_confirmation_msg(tGATT_TCB& tcb, uint16_t cid);
476 extern tGATT_STATUS attp_send_cl_msg(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
477                                      uint8_t op_code, tGATT_CL_MSG* p_msg);
478 extern BT_HDR* attp_build_sr_msg(tGATT_TCB& tcb, uint8_t op_code,
479                                  tGATT_SR_MSG* p_msg);
480 extern tGATT_STATUS attp_send_sr_msg(tGATT_TCB& tcb, uint16_t cid,
481                                      BT_HDR* p_msg);
482 extern tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB& tcb, uint16_t cid,
483                                            BT_HDR* p_toL2CAP);
484 
485 /* utility functions */
486 extern uint8_t* gatt_dbg_op_name(uint8_t op_code);
487 extern uint32_t gatt_add_sdp_record(const bluetooth::Uuid& uuid,
488                                     uint16_t start_hdl, uint16_t end_hdl);
489 extern bool gatt_parse_uuid_from_cmd(bluetooth::Uuid* p_uuid, uint16_t len,
490                                      uint8_t** p_data);
491 extern uint8_t gatt_build_uuid_to_stream_len(const bluetooth::Uuid& uuid);
492 extern uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst,
493                                          const bluetooth::Uuid& uuid);
494 extern void gatt_sr_get_sec_info(const RawAddress& rem_bda,
495                                  tBT_TRANSPORT transport, uint8_t* p_sec_flag,
496                                  uint8_t* p_key_size);
497 extern void gatt_start_rsp_timer(tGATT_CLCB* p_clcb);
498 extern void gatt_stop_rsp_timer(tGATT_CLCB* p_clcb);
499 extern void gatt_start_conf_timer(tGATT_TCB* p_tcb, uint16_t cid);
500 extern void gatt_stop_conf_timer(tGATT_TCB& tcb, uint16_t cid);
501 extern void gatt_rsp_timeout(void* data);
502 extern void gatt_indication_confirmation_timeout(void* data);
503 extern void gatt_ind_ack_timeout(void* data);
504 extern void gatt_start_ind_ack_timer(tGATT_TCB& tcb, uint16_t cid);
505 extern void gatt_stop_ind_ack_timer(tGATT_TCB* p_tcb, uint16_t cid);
506 extern tGATT_STATUS gatt_send_error_rsp(tGATT_TCB& tcb, uint16_t cid,
507                                         uint8_t err_code, uint8_t op_code,
508                                         uint16_t handle, bool deq);
509 
510 extern bool gatt_is_srv_chg_ind_pending(tGATT_TCB* p_tcb);
511 extern tGATTS_SRV_CHG* gatt_is_bda_in_the_srv_chg_clt_list(
512     const RawAddress& bda);
513 
514 extern bool gatt_find_the_connected_bda(uint8_t start_idx, RawAddress& bda,
515                                         uint8_t* p_found_idx,
516                                         tBT_TRANSPORT* p_transport);
517 extern void gatt_set_srv_chg(void);
518 extern void gatt_delete_dev_from_srv_chg_clt_list(const RawAddress& bd_addr);
519 extern void gatt_add_pending_ind(tGATT_TCB* p_tcb, tGATT_VALUE* p_ind);
520 extern void gatt_free_srvc_db_buffer_app_id(const bluetooth::Uuid& app_id);
521 extern bool gatt_cl_send_next_cmd_inq(tGATT_TCB& tcb);
522 
523 /* reserved handle list */
524 extern std::list<tGATT_HDL_LIST_ELEM>::iterator gatt_find_hdl_buffer_by_app_id(
525     const bluetooth::Uuid& app_uuid128, bluetooth::Uuid* p_svc_uuid,
526     uint16_t svc_inst);
527 extern tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle);
528 extern tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg);
529 
530 /* for background connection */
531 extern bool gatt_auto_connect_dev_remove(tGATT_IF gatt_if,
532                                          const RawAddress& bd_addr);
533 
534 /* server function */
535 extern std::list<tGATT_SRV_LIST_ELEM>::iterator gatt_sr_find_i_rcb_by_handle(
536     uint16_t handle);
537 extern tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
538                                             uint32_t trans_id, uint8_t op_code,
539                                             tGATT_STATUS status,
540                                             tGATTS_RSP* p_msg,
541                                             tGATT_SR_CMD* sr_res_p);
542 extern void gatt_server_handle_client_req(tGATT_TCB& p_tcb, uint16_t cid,
543                                           uint8_t op_code, uint16_t len,
544                                           uint8_t* p_data);
545 extern void gatt_sr_send_req_callback(uint16_t conn_id, uint32_t trans_id,
546                                       uint8_t op_code, tGATTS_DATA* p_req_data);
547 extern uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint16_t cid,
548                                     uint8_t op_code, uint16_t handle);
549 extern bool gatt_cancel_open(tGATT_IF gatt_if, const RawAddress& bda);
550 extern void gatt_notify_phy_updated(tGATT_STATUS status, uint16_t handle,
551                                     uint8_t tx_phy, uint8_t rx_phy);
552 /*   */
553 
554 extern bool gatt_tcb_is_cid_busy(tGATT_TCB& tcb, uint16_t cid);
555 
556 extern tGATT_REG* gatt_get_regcb(tGATT_IF gatt_if);
557 extern bool gatt_is_clcb_allocated(uint16_t conn_id);
558 extern tGATT_CLCB* gatt_clcb_alloc(uint16_t conn_id);
559 
560 extern bool gatt_tcb_get_cid_available_for_indication(
561     tGATT_TCB* p_tcb, bool eatt_support, uint16_t** indicate_handle_p,
562     uint16_t* cid_p);
563 extern bool gatt_tcb_find_indicate_handle(tGATT_TCB& tcb, uint16_t cid,
564                                           uint16_t* indicated_handle_p);
565 extern uint16_t gatt_tcb_get_att_cid(tGATT_TCB& tcb, bool eatt_support);
566 extern uint16_t gatt_tcb_get_payload_size_tx(tGATT_TCB& tcb, uint16_t cid);
567 extern uint16_t gatt_tcb_get_payload_size_rx(tGATT_TCB& tcb, uint16_t cid);
568 extern void gatt_clcb_dealloc(tGATT_CLCB* p_clcb);
569 
570 extern void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB& p_tcb);
571 extern bool gatt_sr_is_cback_cnt_zero(tGATT_TCB& p_tcb);
572 extern bool gatt_sr_is_prep_cnt_zero(tGATT_TCB& p_tcb);
573 extern void gatt_sr_reset_cback_cnt(tGATT_TCB& p_tcb, uint16_t cid);
574 extern void gatt_sr_reset_prep_cnt(tGATT_TCB& tcb);
575 extern tGATT_SR_CMD* gatt_sr_get_cmd_by_trans_id(tGATT_TCB* p_tcb,
576                                                  uint32_t trans_id);
577 extern tGATT_SR_CMD* gatt_sr_get_cmd_by_cid(tGATT_TCB& tcb, uint16_t cid);
578 extern tGATT_READ_MULTI* gatt_sr_get_read_multi(tGATT_TCB& tcb, uint16_t cid);
579 extern void gatt_sr_update_cback_cnt(tGATT_TCB& p_tcb, uint16_t cid,
580                                      tGATT_IF gatt_if, bool is_inc,
581                                      bool is_reset_first);
582 extern void gatt_sr_update_prep_cnt(tGATT_TCB& tcb, tGATT_IF gatt_if,
583                                     bool is_inc, bool is_reset_first);
584 
585 extern uint8_t gatt_num_clcb_by_bd_addr(const RawAddress& bda);
586 extern tGATT_TCB* gatt_find_tcb_by_cid(uint16_t lcid);
587 extern tGATT_TCB* gatt_allocate_tcb_by_bdaddr(const RawAddress& bda,
588                                               tBT_TRANSPORT transport);
589 extern tGATT_TCB* gatt_get_tcb_by_idx(uint8_t tcb_idx);
590 extern tGATT_TCB* gatt_find_tcb_by_addr(const RawAddress& bda,
591                                         tBT_TRANSPORT transport);
592 extern bool gatt_send_ble_burst_data(const RawAddress& remote_bda,
593                                      BT_HDR* p_buf);
594 
595 /* GATT client functions */
596 extern void gatt_dequeue_sr_cmd(tGATT_TCB& tcb, uint16_t cid);
597 extern tGATT_STATUS gatt_send_write_msg(tGATT_TCB& p_tcb, tGATT_CLCB* p_clcb,
598                                         uint8_t op_code, uint16_t handle,
599                                         uint16_t len, uint16_t offset,
600                                         uint8_t* p_data);
601 extern void gatt_cleanup_upon_disc(const RawAddress& bda,
602                                    tGATT_DISCONN_REASON reason,
603                                    tBT_TRANSPORT transport);
604 extern void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status,
605                                void* p_data);
606 
607 extern void gatt_act_discovery(tGATT_CLCB* p_clcb);
608 extern void gatt_act_read(tGATT_CLCB* p_clcb, uint16_t offset);
609 extern void gatt_act_write(tGATT_CLCB* p_clcb, uint8_t sec_act);
610 extern tGATT_CLCB* gatt_cmd_dequeue(tGATT_TCB& tcb, uint16_t cid,
611                                     uint8_t* p_opcode);
612 extern void gatt_cmd_enq(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, bool to_send,
613                          uint8_t op_code, BT_HDR* p_buf);
614 extern void gatt_client_handle_server_rsp(tGATT_TCB& tcb, uint16_t cid,
615                                           uint8_t op_code, uint16_t len,
616                                           uint8_t* p_data);
617 extern void gatt_send_queue_write_cancel(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
618                                          tGATT_EXEC_FLAG flag);
619 
620 /* gatt_auth.cc */
621 extern bool gatt_security_check_start(tGATT_CLCB* p_clcb);
622 extern void gatt_verify_signature(tGATT_TCB& tcb, uint16_t cid, BT_HDR* p_buf);
623 extern tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB& tcb);
624 extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb);
625 extern void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act);
626 
627 /* gatt_db.cc */
628 extern void gatts_init_service_db(tGATT_SVC_DB& db,
629                                   const bluetooth::Uuid& service, bool is_pri,
630                                   uint16_t s_hdl, uint16_t num_handle);
631 extern uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle,
632                                            uint16_t e_handle,
633                                            const bluetooth::Uuid& service);
634 extern uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
635                                          tGATT_CHAR_PROP property,
636                                          const bluetooth::Uuid& char_uuid);
637 extern uint16_t gatts_add_char_ext_prop_descr(tGATT_SVC_DB& db,
638                                               uint16_t extended_properties);
639 extern uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm,
640                                      const bluetooth::Uuid& dscp_uuid);
641 extern tGATT_STATUS gatts_db_read_attr_value_by_type(
642     tGATT_TCB& tcb, uint16_t cid, tGATT_SVC_DB* p_db, uint8_t op_code,
643     BT_HDR* p_rsp, uint16_t s_handle, uint16_t e_handle,
644     const bluetooth::Uuid& type, uint16_t* p_len, tGATT_SEC_FLAG sec_flag,
645     uint8_t key_size, uint32_t trans_id, uint16_t* p_cur_handle);
646 extern tGATT_STATUS gatts_read_attr_value_by_handle(
647     tGATT_TCB& tcb, uint16_t cid, tGATT_SVC_DB* p_db, uint8_t op_code,
648     uint16_t handle, uint16_t offset, uint8_t* p_value, uint16_t* p_len,
649     uint16_t mtu, tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id);
650 extern tGATT_STATUS gatts_write_attr_perm_check(
651     tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle, uint16_t offset,
652     uint8_t* p_data, uint16_t len, tGATT_SEC_FLAG sec_flag, uint8_t key_size);
653 extern tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB* p_db, bool is_long,
654                                                uint16_t handle,
655                                                tGATT_SEC_FLAG sec_flag,
656                                                uint8_t key_size);
657 extern bluetooth::Uuid* gatts_get_service_uuid(tGATT_SVC_DB* p_db);
658 
659 /* gatt_sr_hash.cc */
660 extern Octet16 gatts_calculate_database_hash(
661     std::list<tGATT_SRV_LIST_ELEM>* lst_ptr);
662 
663 #endif
664