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