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