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