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 <bluetooth/log.h>
25 
26 #include <deque>
27 #include <list>
28 #include <unordered_set>
29 #include <vector>
30 
31 #include "common/init_flags.h"
32 #include "gatt_api.h"
33 #include "internal_include/bt_target.h"
34 #include "macros.h"
35 #include "osi/include/fixed_queue.h"
36 #include "stack/include/bt_hdr.h"
37 #include "types/bluetooth/uuid.h"
38 #include "types/raw_address.h"
39 
40 #define GATT_CREATE_CONN_ID(tcb_idx, gatt_if) \
41   ((uint16_t)((((uint8_t)(tcb_idx)) << 8) | ((uint8_t)(gatt_if))))
42 #define GATT_GET_TCB_IDX(conn_id) ((uint8_t)(((uint16_t)(conn_id)) >> 8))
43 #define GATT_GET_GATT_IF(conn_id) ((tGATT_IF)((uint8_t)(conn_id)))
44 
45 #define GATT_TRANS_ID_MAX 0x0fffffff /* 4 MSB is reserved */
46 
47 /* security action for GATT write and read request */
48 typedef enum : uint8_t {
49   GATT_SEC_NONE = 0,
50   GATT_SEC_OK = 1,
51   GATT_SEC_SIGN_DATA = 2,       /* compute the signature for the write cmd */
52   GATT_SEC_ENCRYPT = 3,         /* encrypt the link with current key */
53   GATT_SEC_ENCRYPT_NO_MITM = 4, /* unauthenticated encryption or better */
54   GATT_SEC_ENCRYPT_MITM = 5,    /* authenticated encryption */
55   GATT_SEC_ENC_PENDING = 6,     /* wait for link encryption pending */
56 } tGATT_SEC_ACTION;
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 base::StringPrintf("UNKNOWN[%hhu]", action);
69   }
70 }
71 
72 #define GATT_INDEX_INVALID 0xff
73 
74 #define GATT_WRITE_CMD_MASK 0xc0 /*0x1100-0000*/
75 #define GATT_AUTH_SIGN_MASK 0x80 /*0x1000-0000*/
76 #define GATT_AUTH_SIGN_LEN 12
77 
78 #define GATT_HDR_SIZE 3 /* 1B opcode + 2B handle */
79 
80 /* wait for ATT cmd response timeout value */
81 #define GATT_WAIT_FOR_RSP_TIMEOUT_MS (30 * 1000)
82 #define GATT_WAIT_FOR_DISC_RSP_TIMEOUT_MS (5 * 1000)
83 #define GATT_REQ_RETRY_LIMIT 2
84 
85 typedef struct {
86   bool is_link_key_known;
87   bool is_link_key_authed;
88   bool is_encrypted;
89   // whether we connected to the peer, or if it
90   // connected to a discoverable advertisement (affects
91   // GAP permissions)
92   bool can_read_discoverable_characteristics;
93 } tGATT_SEC_FLAG;
94 
95 /* Find Information Response Type
96 */
97 #define GATT_INFO_TYPE_PAIR_16 0x01
98 #define GATT_INFO_TYPE_PAIR_128 0x02
99 
100 constexpr bool kGattConnected = true;
101 constexpr bool kGattDisconnected = !kGattConnected;
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   uint16_t char_ext_prop;      /* Characteristic Extended Properties */
160 } tGATT_ATTR_VALUE;
161 
162 /* Attribute UUID type
163 */
164 #define GATT_ATTR_UUID_TYPE_16 0
165 #define GATT_ATTR_UUID_TYPE_128 1
166 #define GATT_ATTR_UUID_TYPE_32 2
167 typedef uint8_t tGATT_ATTR_UUID_TYPE;
168 
169 /* 16 bits UUID Attribute in server database
170 */
171 typedef struct {
172   std::unique_ptr<tGATT_ATTR_VALUE> p_value;
173   tGATT_PERM permission;
174   uint16_t handle;
175   bluetooth::Uuid uuid;
176   bt_gatt_db_attribute_type_t gatt_type;
177 } tGATT_ATTR;
178 
179 /* Service Database definition
180 */
181 typedef struct {
182   std::vector<tGATT_ATTR> attr_list; /* pointer to the attributes */
183   uint16_t end_handle;       /* Last handle number           */
184   uint16_t next_handle;      /* Next usable handle value     */
185 } tGATT_SVC_DB;
186 
187 /* Data Structure used for GATT server */
188 /* An GATT registration record consists of a handle, and 1 or more attributes */
189 /* A service registration information record consists of beginning and ending */
190 /* attribute handle, service UUID and a set of GATT server callback.          */
191 
192 typedef struct {
193   bluetooth::Uuid app_uuid128;
194   tGATT_CBACK app_cb{};
195   tGATT_IF gatt_if{0}; /* one based */
196   bool in_use{false};
197   uint8_t listening{0}; /* if adv for all has been enabled */
198   bool eatt_support{false};
199   std::string name;
200   std::set<RawAddress> direct_connect_request;
201 } tGATT_REG;
202 
203 struct tGATT_CLCB;
204 
205 /* command queue for each connection */
206 typedef struct {
207   BT_HDR* p_cmd;
208   tGATT_CLCB* p_clcb;
209   uint8_t op_code;
210   bool to_send;
211   uint16_t cid;
212 } tGATT_CMD_Q;
213 
214 #if GATT_MAX_SR_PROFILES <= 8
215 typedef uint8_t tGATT_APP_MASK;
216 #elif GATT_MAX_SR_PROFILES <= 16
217 typedef uint16_t tGATT_APP_MASK;
218 #elif GATT_MAX_SR_PROFILES <= 32
219 typedef uint32_t tGATT_APP_MASK;
220 #endif
221 
222 /* command details for each connection */
223 typedef struct {
224   BT_HDR* p_rsp_msg;
225   uint32_t trans_id;
226   tGATT_READ_MULTI multi_req;
227   fixed_queue_t* multi_rsp_q;
228   uint16_t handle;
229   uint8_t op_code;
230   uint8_t status;
231   uint8_t cback_cnt[GATT_MAX_APPS];
232   uint16_t cid;
233 } tGATT_SR_CMD;
234 
235 typedef enum : uint8_t {
236   GATT_CH_CLOSE = 0,
237   GATT_CH_CLOSING = 1,
238   GATT_CH_CONN = 2,
239   GATT_CH_CFG = 3,
240   GATT_CH_OPEN = 4,
241 } tGATT_CH_STATE;
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 base::StringPrintf("UNKNOWN[%hhu]", state);
252   }
253 }
254 
255 // If you change these values make sure to look at b/262219144 before.
256 // Some platform rely on this to never changes
257 #define GATT_GATT_START_HANDLE 1
258 #define GATT_GAP_START_HANDLE 20
259 #define GATT_GMCS_START_HANDLE 40
260 #define GATT_GTBS_START_HANDLE 90
261 #define GATT_TMAS_START_HANDLE 130
262 #define GATT_APP_START_HANDLE 134
263 
264 typedef struct hdl_cfg {
265   uint16_t gatt_start_hdl;
266   uint16_t gap_start_hdl;
267   uint16_t gmcs_start_hdl;
268   uint16_t gtbs_start_hdl;
269   uint16_t tmas_start_hdl;
270   uint16_t app_start_hdl;
271 } tGATT_HDL_CFG;
272 
273 typedef struct hdl_list_elem {
274   tGATTS_HNDL_RANGE asgn_range; /* assigned handle range */
275   tGATT_SVC_DB svc_db;
276 } tGATT_HDL_LIST_ELEM;
277 
278 /* Data Structure used for GATT server                                        */
279 /* A GATT registration record consists of a handle, and 1 or more attributes  */
280 /* A service registration information record consists of beginning and ending */
281 /* attribute handle, service UUID and a set of GATT server callback.          */
282 typedef struct {
283   tGATT_SVC_DB* p_db;  /* pointer to the service database */
284   bluetooth::Uuid app_uuid; /* application UUID */
285   uint32_t sdp_handle; /* primamry service SDP handle */
286   uint16_t type;       /* service type UUID, primary or secondary */
287   uint16_t s_hdl;      /* service starting handle */
288   uint16_t e_hdl;      /* service ending handle */
289   tGATT_IF gatt_if;    /* this service is belong to which application */
290   bool is_primary;
291 } tGATT_SRV_LIST_ELEM;
292 
293 typedef struct {
294   std::deque<tGATT_CLCB*> pending_enc_clcb; /* pending encryption channel q */
295   tGATT_SEC_ACTION sec_act;
296   RawAddress peer_bda;
297   tBT_TRANSPORT transport;
298   uint32_t trans_id;
299 
300   /* Indicates number of available eatt channels */
301   uint8_t eatt;
302 
303   uint16_t att_lcid; /* L2CAP channel ID for ATT */
304   uint16_t payload_size;
305 
306   tGATT_CH_STATE ch_state;
307 
308   std::unordered_set<tGATT_IF> app_hold_link;
309 
310   /* server needs */
311   /* server response data */
312   tGATT_SR_CMD sr_cmd;
313   uint16_t indicate_handle;
314   fixed_queue_t* pending_ind_q;
315 
316   alarm_t* conf_timer; /* peer confirm to indication timer */
317 
318   uint8_t prep_cnt[GATT_MAX_APPS];
319   uint8_t ind_count;
320 
321   std::deque<tGATT_CMD_Q> cl_cmd_q;
322   alarm_t* ind_ack_timer; /* local app confirm to indication timer */
323 
324   // TODO(hylo): support byte array data
325   /* Client supported feature*/
326   uint8_t cl_supp_feat;
327   /* Server supported features */
328   uint8_t sr_supp_feat;
329   /* Use for server. if false, should handle database out of sync. */
330   bool is_robust_cache_change_aware;
331 
332   /* SIRK read related data */
333   tGATT_STATUS gatt_status;
334   uint8_t sirk_type;
335   Octet16 sirk;
336 
337   bool in_use;
338   uint8_t tcb_idx;
339 
340   /* ATT Exchange MTU data */
341   uint16_t pending_user_mtu_exchange_value;
342   std::list<uint16_t> conn_ids_waiting_for_mtu_exchange;
343   /* Used to set proper TX DATA LEN on the controller*/
344   uint16_t max_user_mtu;
345 
346 } tGATT_TCB;
347 
348 /* logic channel */
349 typedef struct {
350   uint16_t
351       next_disc_start_hdl; /* starting handle for the next inc srvv discovery */
352   tGATT_DISC_RES result;
353   bool wait_for_read_rsp;
354 } tGATT_READ_INC_UUID128;
355 struct tGATT_CLCB {
356   tGATT_TCB* p_tcb; /* associated TCB of this CLCB */
357   tGATT_REG* p_reg; /* owner of this CLCB */
358   uint8_t sccb_idx;
359   uint8_t* p_attr_buf; /* attribute buffer for read multiple, prepare write */
360   bluetooth::Uuid uuid;
361   uint16_t conn_id; /* connection handle */
362   uint16_t s_handle; /* starting handle of the active request */
363   uint16_t e_handle; /* ending handle of the active request */
364   uint16_t counter; /* used as offset, attribute length, num of prepare write */
365   uint16_t start_offset;
366   tGATT_AUTH_REQ auth_req; /* authentication requirement */
367   tGATTC_OPTYPE operation; /* one logic channel can have one operation active */
368   uint8_t op_subtype;      /* operation subtype */
369   tGATT_STATUS status;     /* operation status */
370   bool first_read_blob_after_read;
371   tGATT_READ_INC_UUID128 read_uuid128;
372   alarm_t* gatt_rsp_timer_ent; /* peer response timer */
373   uint8_t retry_count;
374   uint16_t read_req_current_mtu; /* This is the MTU value that the read was
375                                     initiated with */
376   uint16_t cid;
377 };
378 
379 typedef struct {
380   uint16_t handle;
381   uint16_t uuid;
382   uint32_t service_change;
383 } tGATT_SVC_CHG;
384 
385 #define GATT_SVC_CHANGED_CONNECTING 1     /* wait for connection */
386 #define GATT_SVC_CHANGED_SERVICE 2        /* GATT service discovery */
387 #define GATT_SVC_CHANGED_CHARACTERISTIC 3 /* service change char discovery */
388 #define GATT_SVC_CHANGED_DESCRIPTOR 4     /* service change CCC discoery */
389 #define GATT_SVC_CHANGED_CONFIGURE_CCCD 5 /* config CCC */
390 
391 typedef struct {
392   uint16_t conn_id;
393   bool in_use;
394   bool connected;
395   RawAddress bda;
396   tBT_TRANSPORT transport;
397 
398   /* GATT service change CCC related variables */
399   uint8_t ccc_stage;
400   uint8_t ccc_result;
401   uint16_t s_handle;
402   uint16_t e_handle;
403 } tGATT_PROFILE_CLCB;
404 
405 typedef struct {
406   tGATT_TCB tcb[GATT_MAX_PHY_CHANNEL];
407   fixed_queue_t* sign_op_queue;
408 
409   uint16_t next_handle;     /* next available handle */
410   uint16_t last_service_handle; /* handle of last service */
411   tGATT_SVC_CHG gattp_attr; /* GATT profile attribute service change */
412   tGATT_IF gatt_if;
413   std::list<tGATT_HDL_LIST_ELEM>* hdl_list_info;
414   std::list<tGATT_SRV_LIST_ELEM>* srv_list_info;
415 
416   fixed_queue_t* srv_chg_clt_q; /* service change clients queue */
417   tGATT_REG cl_rcb[GATT_MAX_APPS];
418 
419   /* list of connection link control blocks.
420    * Since clcbs are also keep in the channels (ATT and EATT) queues while
421    * processing, we want to make sure that references to elements are not
422    * invalidated when elements are added or removed from the list. This is why
423    * std::list is used.
424    */
425   std::list<tGATT_CLCB> clcb_queue;
426 
427 #if (GATT_CONFORMANCE_TESTING == TRUE)
428   bool enable_err_rsp;
429   uint8_t req_op_code;
430   uint8_t err_status;
431   uint16_t handle;
432 #endif
433 
434   tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
435   uint16_t
436       handle_of_h_r; /* Handle of the handles reused characteristic value */
437   uint16_t handle_cl_supported_feat;
438   uint16_t handle_sr_supported_feat;
439   uint8_t
440       gatt_svr_supported_feat_mask; /* Local supported features as a server */
441 
442   /* Supported features as a client. To be written to remote device.
443    * Note this is NOT a value of the characteristic with handle
444    * handle_cl_support_feat, as that one should be written by remote device.
445    */
446   uint8_t gatt_cl_supported_feat_mask;
447 
448   uint16_t handle_of_database_hash;
449   Octet16 database_hash;
450 
451   tGATT_APPL_INFO cb_info;
452 
453   tGATT_HDL_CFG hdl_cfg;
454   bool over_br_enabled;
455 } tGATT_CB;
456 
457 #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
458 
459 /* Global GATT data */
460 extern tGATT_CB gatt_cb;
461 
462 #if (GATT_CONFORMANCE_TESTING == TRUE)
463 void gatt_set_err_rsp(bool enable, uint8_t req_op_code, uint8_t err_status);
464 #endif
465 
466 /* from gatt_main.cc */
467 bool gatt_disconnect(tGATT_TCB* p_tcb);
468 bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
469                       tBT_TRANSPORT transport, int8_t initiating_phys);
470 bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
471                       tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
472                       int8_t initiating_phys);
473 bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
474                   tBT_TRANSPORT transport, uint8_t initiating_phys,
475                   tGATT_IF gatt_if);
476 bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
477                   tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
478                   uint8_t initiating_phys, tGATT_IF gatt_if);
479 void gatt_data_process(tGATT_TCB& p_tcb, uint16_t cid, BT_HDR* p_buf);
480 void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
481                                    bool is_add, bool check_acl_link);
482 
483 void gatt_profile_db_init(void);
484 void gatt_set_ch_state(tGATT_TCB* p_tcb, tGATT_CH_STATE ch_state);
485 tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB* p_tcb);
486 void gatt_init_srv_chg(void);
487 void gatt_proc_srv_chg(void);
488 void gatt_send_srv_chg_ind(const RawAddress& peer_bda);
489 void gatt_chk_srv_chg(tGATTS_SRV_CHG* p_srv_chg_clt);
490 void gatt_add_a_bonded_dev_for_srv_chg(const RawAddress& bda);
491 
492 /* from gatt_attr.cc */
493 uint16_t gatt_profile_find_conn_id_by_bd_addr(const RawAddress& bda);
494 
495 bool gatt_profile_get_eatt_support(const RawAddress& remote_bda);
496 void gatt_cl_init_sr_status(tGATT_TCB& tcb);
497 bool gatt_cl_read_sr_supp_feat_req(
498     const RawAddress& peer_bda,
499     base::OnceCallback<void(const RawAddress&, uint8_t)> cb);
500 bool gatt_cl_read_sirk_req(
501     const RawAddress& peer_bda,
502     base::OnceCallback<void(tGATT_STATUS status, const RawAddress&,
503                             uint8_t sirk_type, Octet16& sirk)>
504         cb);
505 bool gatt_sr_is_cl_multi_variable_len_notif_supported(tGATT_TCB& tcb);
506 
507 bool gatt_sr_is_cl_change_aware(tGATT_TCB& tcb);
508 void gatt_sr_init_cl_status(tGATT_TCB& tcb);
509 void gatt_sr_update_cl_status(tGATT_TCB& tcb, bool chg_aware);
510 
511 /* Functions provided by att_protocol.cc */
512 tGATT_STATUS attp_send_cl_confirmation_msg(tGATT_TCB& tcb, uint16_t cid);
513 tGATT_STATUS attp_send_cl_msg(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
514                               uint8_t op_code, tGATT_CL_MSG* p_msg);
515 BT_HDR* attp_build_sr_msg(tGATT_TCB& tcb, uint8_t op_code, tGATT_SR_MSG* p_msg,
516                           uint16_t payload_size);
517 tGATT_STATUS attp_send_sr_msg(tGATT_TCB& tcb, uint16_t cid, BT_HDR* p_msg);
518 tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB& tcb, uint16_t cid,
519                                     BT_HDR* p_toL2CAP);
520 
521 /* utility functions */
522 uint16_t gatt_get_local_mtu(void);
523 char const* 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(tHCI_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(tGATT_TCB& tcb, uint16_t cid);
602 void gatt_clcb_invalidate(tGATT_TCB* p_tcb, const tGATT_CLCB* p_clcb);
603 uint16_t gatt_get_mtu(const RawAddress& bda, tBT_TRANSPORT transport);
604 bool gatt_is_pending_mtu_exchange(tGATT_TCB* p_tcb);
605 void gatt_set_conn_id_waiting_for_mtu_exchange(tGATT_TCB* p_tcb,
606                                                uint16_t conn_id);
607 
608 void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB& p_tcb);
609 bool gatt_sr_is_cback_cnt_zero(tGATT_TCB& p_tcb);
610 bool gatt_sr_is_prep_cnt_zero(tGATT_TCB& p_tcb);
611 void gatt_sr_reset_cback_cnt(tGATT_TCB& p_tcb, uint16_t cid);
612 void gatt_sr_reset_prep_cnt(tGATT_TCB& tcb);
613 tGATT_SR_CMD* gatt_sr_get_cmd_by_trans_id(tGATT_TCB* p_tcb, uint32_t trans_id);
614 tGATT_SR_CMD* gatt_sr_get_cmd_by_cid(tGATT_TCB& tcb, uint16_t cid);
615 tGATT_READ_MULTI* gatt_sr_get_read_multi(tGATT_TCB& tcb, uint16_t cid);
616 void gatt_sr_update_cback_cnt(tGATT_TCB& p_tcb, uint16_t cid, tGATT_IF gatt_if,
617                               bool is_inc, bool is_reset_first);
618 void gatt_sr_update_prep_cnt(tGATT_TCB& tcb, tGATT_IF gatt_if, bool is_inc,
619                              bool is_reset_first);
620 
621 tGATT_TCB* gatt_find_tcb_by_cid(uint16_t lcid);
622 tGATT_TCB* gatt_allocate_tcb_by_bdaddr(const RawAddress& bda,
623                                        tBT_TRANSPORT transport);
624 tGATT_TCB* gatt_get_tcb_by_idx(uint8_t tcb_idx);
625 tGATT_TCB* gatt_find_tcb_by_addr(const RawAddress& bda,
626                                  tBT_TRANSPORT transport);
627 bool gatt_send_ble_burst_data(const RawAddress& remote_bda, BT_HDR* p_buf);
628 
629 /* GATT client functions */
630 void gatt_dequeue_sr_cmd(tGATT_TCB& tcb, uint16_t cid);
631 tGATT_STATUS gatt_send_write_msg(tGATT_TCB& p_tcb, tGATT_CLCB* p_clcb,
632                                  uint8_t op_code, uint16_t handle, uint16_t len,
633                                  uint16_t offset, uint8_t* p_data);
634 void gatt_cleanup_upon_disc(const RawAddress& bda, tGATT_DISCONN_REASON reason,
635                             tBT_TRANSPORT transport);
636 void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status, void* p_data);
637 
638 void gatt_act_discovery(tGATT_CLCB* p_clcb);
639 void gatt_act_read(tGATT_CLCB* p_clcb, uint16_t offset);
640 void gatt_act_write(tGATT_CLCB* p_clcb, uint8_t sec_act);
641 tGATT_CLCB* gatt_cmd_dequeue(tGATT_TCB& tcb, uint16_t cid, uint8_t* p_opcode);
642 bool gatt_cmd_enq(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, bool to_send,
643                   uint8_t op_code, BT_HDR* p_buf);
644 void gatt_client_handle_server_rsp(tGATT_TCB& tcb, uint16_t cid,
645                                    uint8_t op_code, uint16_t len,
646                                    uint8_t* p_data);
647 void gatt_send_queue_write_cancel(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
648                                   tGATT_EXEC_FLAG flag);
649 bool gatt_is_outstanding_msg_in_att_send_queue(const tGATT_TCB& tcb);
650 
651 /* gatt_auth.cc */
652 bool gatt_security_check_start(tGATT_CLCB* p_clcb);
653 void gatt_verify_signature(tGATT_TCB& tcb, uint16_t cid, BT_HDR* p_buf);
654 tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB& tcb);
655 tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb);
656 void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act);
657 
658 /* gatt_db.cc */
659 void gatts_init_service_db(tGATT_SVC_DB& db, const bluetooth::Uuid& service,
660                            bool is_pri, uint16_t s_hdl, uint16_t num_handle);
661 uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle,
662                                     uint16_t e_handle,
663                                     const bluetooth::Uuid& service);
664 uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
665                                   tGATT_CHAR_PROP property,
666                                   const bluetooth::Uuid& char_uuid);
667 uint16_t gatts_add_char_ext_prop_descr(tGATT_SVC_DB& db,
668                                        uint16_t extended_properties);
669 uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm,
670                               const bluetooth::Uuid& dscp_uuid);
671 tGATT_STATUS gatts_db_read_attr_value_by_type(
672     tGATT_TCB& tcb, uint16_t cid, tGATT_SVC_DB* p_db, uint8_t op_code,
673     BT_HDR* p_rsp, uint16_t s_handle, uint16_t e_handle,
674     const bluetooth::Uuid& type, uint16_t* p_len, tGATT_SEC_FLAG sec_flag,
675     uint8_t key_size, uint32_t trans_id, uint16_t* p_cur_handle);
676 tGATT_STATUS gatts_read_attr_value_by_handle(
677     tGATT_TCB& tcb, uint16_t cid, tGATT_SVC_DB* p_db, uint8_t op_code,
678     uint16_t handle, uint16_t offset, uint8_t* p_value, uint16_t* p_len,
679     uint16_t mtu, tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id);
680 tGATT_STATUS gatts_write_attr_perm_check(tGATT_SVC_DB* p_db, uint8_t op_code,
681                                          uint16_t handle, uint16_t offset,
682                                          uint8_t* p_data, uint16_t len,
683                                          tGATT_SEC_FLAG sec_flag,
684                                          uint8_t key_size);
685 tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB* p_db, bool is_long,
686                                         uint16_t handle,
687                                         tGATT_SEC_FLAG sec_flag,
688                                         uint8_t key_size);
689 bluetooth::Uuid* gatts_get_service_uuid(tGATT_SVC_DB* p_db);
690 
691 /* gatt_sr_hash.cc */
692 Octet16 gatts_calculate_database_hash(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr);
693 
694 namespace fmt {
695 template <>
696 struct formatter<tGATT_CH_STATE> : enum_formatter<tGATT_CH_STATE> {};
697 }  // namespace fmt
698 
699 #endif
700