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 <map>
29 #include <unordered_set>
30 #include <vector>
31 
32 #include "common/circular_buffer.h"
33 #include "common/strings.h"
34 #include "gatt_api.h"
35 #include "internal_include/bt_target.h"
36 #include "macros.h"
37 #include "os/logging/log_adapter.h"
38 #include "osi/include/fixed_queue.h"
39 #include "stack/include/bt_hdr.h"
40 #include "types/bluetooth/uuid.h"
41 #include "types/raw_address.h"
42 
43 #define GATT_TRANS_ID_MAX 0x0fffffff /* 4 MSB is reserved */
44 #define GATT_CL_RCB_MAX 255          /* Maximum number of cl_rcb */
45 
46 /* security action for GATT write and read request */
47 typedef enum : uint8_t {
48   GATT_SEC_NONE = 0,
49   GATT_SEC_OK = 1,
50   GATT_SEC_SIGN_DATA = 2,       /* compute the signature for the write cmd */
51   GATT_SEC_ENCRYPT = 3,         /* encrypt the link with current key */
52   GATT_SEC_ENCRYPT_NO_MITM = 4, /* unauthenticated encryption or better */
53   GATT_SEC_ENCRYPT_MITM = 5,    /* authenticated encryption */
54   GATT_SEC_ENC_PENDING = 6,     /* wait for link encryption pending */
55 } tGATT_SEC_ACTION;
56 
gatt_security_action_text(const tGATT_SEC_ACTION & action)57 inline std::string gatt_security_action_text(const tGATT_SEC_ACTION& action) {
58   switch (action) {
59     CASE_RETURN_TEXT(GATT_SEC_NONE);
60     CASE_RETURN_TEXT(GATT_SEC_OK);
61     CASE_RETURN_TEXT(GATT_SEC_SIGN_DATA);
62     CASE_RETURN_TEXT(GATT_SEC_ENCRYPT);
63     CASE_RETURN_TEXT(GATT_SEC_ENCRYPT_NO_MITM);
64     CASE_RETURN_TEXT(GATT_SEC_ENCRYPT_MITM);
65     CASE_RETURN_TEXT(GATT_SEC_ENC_PENDING);
66     default:
67       return base::StringPrintf("UNKNOWN[%hhu]", action);
68   }
69 }
70 
71 #define GATT_INDEX_INVALID 0xff
72 
73 #define GATT_WRITE_CMD_MASK 0xc0 /*0x1100-0000*/
74 #define GATT_AUTH_SIGN_MASK 0x80 /*0x1000-0000*/
75 #define GATT_AUTH_SIGN_LEN 12
76 
77 #define GATT_HDR_SIZE 3 /* 1B opcode + 2B handle */
78 
79 /* wait for ATT cmd response timeout value */
80 #define GATT_WAIT_FOR_RSP_TIMEOUT_MS (30 * 1000)
81 #define GATT_WAIT_FOR_DISC_RSP_TIMEOUT_MS (5 * 1000)
82 #define GATT_REQ_RETRY_LIMIT 2
83 
84 typedef struct {
85   bool is_link_key_known;
86   bool is_link_key_authed;
87   bool is_encrypted;
88   // whether we connected to the peer, or if it
89   // connected to a discoverable advertisement (affects
90   // GAP permissions)
91   bool can_read_discoverable_characteristics;
92 } tGATT_SEC_FLAG;
93 
94 /* Find Information Response Type
95  */
96 #define GATT_INFO_TYPE_PAIR_16 0x01
97 #define GATT_INFO_TYPE_PAIR_128 0x02
98 
99 constexpr bool kGattConnected = true;
100 constexpr bool kGattDisconnected = !kGattConnected;
101 
102 /*  GATT client FIND_TYPE_VALUE_Request data */
103 typedef struct {
104   bluetooth::Uuid uuid;             /* type of attribute to be found */
105   uint16_t s_handle;                /* starting handle */
106   uint16_t e_handle;                /* ending handle */
107   uint16_t value_len;               /* length of the attribute value */
108   uint8_t value[GATT_MAX_MTU_SIZE]; /* pointer to the attribute value to be found */
109 } tGATT_FIND_TYPE_VALUE;
110 
111 /* client request message to ATT protocol
112  */
113 typedef union {
114   tGATT_READ_BY_TYPE browse;             /* read by type request */
115   tGATT_FIND_TYPE_VALUE find_type_value; /* find by type value */
116   tGATT_READ_MULTI read_multi;           /* read multiple request */
117   tGATT_READ_PARTIAL read_blob;          /* read blob */
118   tGATT_VALUE attr_value;                /* write request */
119                                          /* prepare write */
120   /* write blob */
121   uint16_t handle; /* read,  handle value confirmation */
122   uint16_t mtu;
123   tGATT_EXEC_FLAG exec_write; /* execute write */
124 } tGATT_CL_MSG;
125 
126 /* error response strucutre */
127 typedef struct {
128   uint16_t handle;
129   uint8_t cmd_code;
130   uint8_t reason;
131 } tGATT_ERROR;
132 
133 /* server response message to ATT protocol
134  */
135 typedef union {
136   /* data type            member          event   */
137   tGATT_VALUE attr_value; /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
138                           /* READ_BLOB, READ_BY_TYPE */
139   tGATT_ERROR error;      /* ERROR_RSP */
140   uint16_t handle;        /* WRITE, WRITE_BLOB */
141   uint16_t mtu;           /* exchange MTU request */
142 } tGATT_SR_MSG;
143 
144 /* Characteristic declaration attribute value
145  */
146 typedef struct {
147   tGATT_CHAR_PROP property;
148   uint16_t char_val_handle;
149 } tGATT_CHAR_DECL;
150 
151 /* attribute value maintained in the server database
152  */
153 typedef union {
154   bluetooth::Uuid uuid;        /* service declaration */
155   tGATT_CHAR_DECL char_decl;   /* characteristic declaration */
156   tGATT_INCL_SRVC incl_handle; /* included service */
157   uint16_t char_ext_prop;      /* Characteristic Extended Properties */
158 } tGATT_ATTR_VALUE;
159 
160 /* Attribute UUID type
161  */
162 #define GATT_ATTR_UUID_TYPE_16 0
163 #define GATT_ATTR_UUID_TYPE_128 1
164 #define GATT_ATTR_UUID_TYPE_32 2
165 typedef uint8_t tGATT_ATTR_UUID_TYPE;
166 
167 /* 16 bits UUID Attribute in server database
168  */
169 typedef struct {
170   std::unique_ptr<tGATT_ATTR_VALUE> p_value;
171   tGATT_PERM permission;
172   uint16_t handle;
173   bluetooth::Uuid uuid;
174   bt_gatt_db_attribute_type_t gatt_type;
175 } tGATT_ATTR;
176 
177 /* Service Database definition
178  */
179 typedef struct {
180   std::vector<tGATT_ATTR> attr_list; /* pointer to the attributes */
181   uint16_t end_handle;               /* Last handle number           */
182   uint16_t next_handle;              /* Next usable handle value     */
183 } tGATT_SVC_DB;
184 
185 /* Data Structure used for GATT server */
186 /* An GATT registration record consists of a handle, and 1 or more attributes */
187 /* A service registration information record consists of beginning and ending */
188 /* attribute handle, service UUID and a set of GATT server callback.          */
189 
190 typedef struct {
191   bluetooth::Uuid app_uuid128;
192   tGATT_CBACK app_cb{};
193   tGATT_IF gatt_if{0}; /* one based */
194   bool in_use{false};
195   uint8_t listening{0}; /* if adv for all has been enabled */
196   bool eatt_support{false};
197   std::string name;
198   std::set<RawAddress> direct_connect_request;
199   std::map<RawAddress, uint16_t> mtu_prefs;
200 } tGATT_REG;
201 
202 struct tGATT_CLCB;
203 
204 /* command queue for each connection */
205 typedef struct {
206   BT_HDR* p_cmd;
207   tGATT_CLCB* p_clcb;
208   uint8_t op_code;
209   bool to_send;
210   uint16_t cid;
211 } tGATT_CMD_Q;
212 
213 #if GATT_MAX_SR_PROFILES <= 8
214 typedef uint8_t tGATT_APP_MASK;
215 #elif GATT_MAX_SR_PROFILES <= 16
216 typedef uint16_t tGATT_APP_MASK;
217 #elif GATT_MAX_SR_PROFILES <= 32
218 typedef uint32_t tGATT_APP_MASK;
219 #endif
220 
221 /* command details for each connection */
222 typedef struct {
223   BT_HDR* p_rsp_msg;
224   uint32_t trans_id;
225   tGATT_READ_MULTI multi_req;
226   fixed_queue_t* multi_rsp_q;
227   uint16_t handle;
228   uint8_t op_code;
229   uint8_t status;
230   uint8_t cback_cnt[GATT_MAX_APPS];
231   std::unordered_map<tGATT_IF, uint8_t> cback_cnt_map;
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   std::unordered_map<tGATT_IF, uint8_t> prep_cnt_map;
320   uint8_t ind_count;
321 
322   std::deque<tGATT_CMD_Q> cl_cmd_q;
323   alarm_t* ind_ack_timer; /* local app confirm to indication timer */
324 
325   // TODO(hylo): support byte array data
326   /* Client supported feature*/
327   uint8_t cl_supp_feat;
328   /* Server supported features */
329   uint8_t sr_supp_feat;
330   /* Use for server. if false, should handle database out of sync. */
331   bool is_robust_cache_change_aware;
332 
333   /* SIRK read related data */
334   tGATT_STATUS gatt_status;
335   uint8_t sirk_type;
336   Octet16 sirk;
337 
338   bool in_use;
339   uint8_t tcb_idx;
340 
341   /* ATT Exchange MTU data */
342   uint16_t pending_user_mtu_exchange_value;
343   std::list<tCONN_ID> conn_ids_waiting_for_mtu_exchange;
344   /* Used to set proper TX DATA LEN on the controller*/
345   uint16_t max_user_mtu;
346   uint16_t app_mtu_pref;  // Holds consolidated MTU preference from apps at the time of connection
347 } tGATT_TCB;
348 
349 /* logic channel */
350 typedef struct {
351   uint16_t 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   tCONN_ID 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   tCONN_ID 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   tGATT_IF last_gatt_if; /* last used gatt_if, used to find the next gatt_if easily */
420   std::unordered_map<tGATT_IF, std::unique_ptr<tGATT_REG>> cl_rcb_map;
421 
422   /* list of connection link control blocks.
423    * Since clcbs are also keep in the channels (ATT and EATT) queues while
424    * processing, we want to make sure that references to elements are not
425    * invalidated when elements are added or removed from the list. This is why
426    * std::list is used.
427    */
428   std::list<tGATT_CLCB> clcb_queue;
429 
430 #if (GATT_CONFORMANCE_TESTING == TRUE)
431   bool enable_err_rsp;
432   uint8_t req_op_code;
433   uint8_t err_status;
434   uint16_t handle;
435 #endif
436 
437   tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
438   uint16_t handle_of_h_r; /* Handle of the handles reused characteristic value */
439   uint16_t handle_cl_supported_feat;
440   uint16_t handle_sr_supported_feat;
441   uint8_t gatt_svr_supported_feat_mask; /* Local supported features as a server */
442 
443   /* Supported features as a client. To be written to remote device.
444    * Note this is NOT a value of the characteristic with handle
445    * handle_cl_support_feat, as that one should be written by remote device.
446    */
447   uint8_t gatt_cl_supported_feat_mask;
448 
449   uint16_t handle_of_database_hash;
450   Octet16 database_hash;
451 
452   tGATT_APPL_INFO cb_info;
453 
454   tGATT_HDL_CFG hdl_cfg;
455   bool over_br_enabled;
456 } tGATT_CB;
457 
458 #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
459 
460 /* Global GATT data */
461 extern tGATT_CB gatt_cb;
462 
463 #if (GATT_CONFORMANCE_TESTING == TRUE)
464 void gatt_set_err_rsp(bool enable, uint8_t req_op_code, uint8_t err_status);
465 #endif
466 
467 namespace {
468 constexpr char kTimeFormatString[] = "%Y-%m-%d %H:%M:%S";
469 
470 constexpr unsigned MillisPerSecond = 1000;
EpochMillisToString(uint64_t time_ms)471 inline std::string EpochMillisToString(uint64_t time_ms) {
472   time_t time_sec = time_ms / MillisPerSecond;
473   struct tm tm;
474   localtime_r(&time_sec, &tm);
475   std::string s = bluetooth::common::StringFormatTime(kTimeFormatString, tm);
476   return base::StringPrintf("%s.%03u", s.c_str(),
477                             static_cast<unsigned int>(time_ms % MillisPerSecond));
478 }
479 }  // namespace
480 
481 struct tTCB_STATE_HISTORY {
482   RawAddress address;
483   tBT_TRANSPORT transport;
484   tGATT_CH_STATE state;
485   std::string holders_info;
ToStringtTCB_STATE_HISTORY486   std::string ToString() const {
487     return base::StringPrintf("%s, %s, state: %s, %s", ADDRESS_TO_LOGGABLE_CSTR(address),
488                               bt_transport_text(transport).c_str(),
489                               gatt_channel_state_text(state).c_str(), holders_info.c_str());
490   }
491 };
492 
493 extern bluetooth::common::TimestampedCircularBuffer<tTCB_STATE_HISTORY> tcb_state_history_;
494 
495 /* from gatt_main.cc */
496 bool gatt_disconnect(tGATT_TCB* p_tcb);
497 void gatt_cancel_connect(const RawAddress& bd_addr, tBT_TRANSPORT transport);
498 bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr, tBT_TRANSPORT transport,
499                       int8_t initiating_phys);
500 bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
501                       tBT_TRANSPORT transport, int8_t initiating_phys);
502 void gatt_data_process(tGATT_TCB& p_tcb, uint16_t cid, BT_HDR* p_buf);
503 void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb, bool is_add,
504                                    bool check_acl_link);
505 
506 void gatt_profile_db_init(void);
507 void gatt_set_ch_state(tGATT_TCB* p_tcb, tGATT_CH_STATE ch_state);
508 tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB* p_tcb);
509 void gatt_init_srv_chg(void);
510 void gatt_proc_srv_chg(void);
511 void gatt_send_srv_chg_ind(const RawAddress& peer_bda);
512 void gatt_chk_srv_chg(tGATTS_SRV_CHG* p_srv_chg_clt);
513 void gatt_add_a_bonded_dev_for_srv_chg(const RawAddress& bda);
514 
515 /* from gatt_attr.cc */
516 tCONN_ID gatt_profile_find_conn_id_by_bd_addr(const RawAddress& bda);
517 
518 bool gatt_profile_get_eatt_support(const RawAddress& remote_bda);
519 bool gatt_profile_get_eatt_support_by_conn_id(tCONN_ID conn_id);
520 void gatt_cl_init_sr_status(tGATT_TCB& tcb);
521 bool gatt_cl_read_sr_supp_feat_req(const RawAddress& peer_bda,
522                                    base::OnceCallback<void(const RawAddress&, uint8_t)> cb);
523 bool gatt_cl_read_sirk_req(const RawAddress& peer_bda,
524                            base::OnceCallback<void(tGATT_STATUS status, const RawAddress&,
525                                                    uint8_t sirk_type, Octet16& sirk)>
526                                    cb);
527 bool gatt_sr_is_cl_multi_variable_len_notif_supported(tGATT_TCB& tcb);
528 
529 bool gatt_sr_is_cl_change_aware(tGATT_TCB& tcb);
530 void gatt_sr_init_cl_status(tGATT_TCB& tcb);
531 void gatt_sr_update_cl_status(tGATT_TCB& tcb, bool chg_aware);
532 
533 /* Functions provided by att_protocol.cc */
534 tGATT_STATUS attp_send_cl_confirmation_msg(tGATT_TCB& tcb, uint16_t cid);
535 tGATT_STATUS attp_send_cl_msg(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, uint8_t op_code,
536                               tGATT_CL_MSG* p_msg);
537 BT_HDR* attp_build_sr_msg(tGATT_TCB& tcb, uint8_t op_code, tGATT_SR_MSG* p_msg,
538                           uint16_t payload_size);
539 tGATT_STATUS attp_send_sr_msg(tGATT_TCB& tcb, uint16_t cid, BT_HDR* p_msg);
540 tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB& tcb, uint16_t cid, BT_HDR* p_toL2CAP);
541 
542 /* utility functions */
543 uint16_t gatt_get_local_mtu(void);
544 char const* gatt_dbg_op_name(uint8_t op_code);
545 uint32_t gatt_add_sdp_record(const bluetooth::Uuid& uuid, uint16_t start_hdl, uint16_t end_hdl);
546 bool gatt_parse_uuid_from_cmd(bluetooth::Uuid* p_uuid, uint16_t len, uint8_t** p_data);
547 uint8_t gatt_build_uuid_to_stream_len(const bluetooth::Uuid& uuid);
548 uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst, const bluetooth::Uuid& uuid);
549 void gatt_sr_get_sec_info(const RawAddress& rem_bda, tBT_TRANSPORT transport,
550                           tGATT_SEC_FLAG* p_sec_flag, uint8_t* p_key_size);
551 void gatt_start_rsp_timer(tGATT_CLCB* p_clcb);
552 void gatt_stop_rsp_timer(tGATT_CLCB* p_clcb);
553 void gatt_start_conf_timer(tGATT_TCB* p_tcb, uint16_t cid);
554 void gatt_stop_conf_timer(tGATT_TCB& tcb, uint16_t cid);
555 void gatt_rsp_timeout(void* data);
556 void gatt_indication_confirmation_timeout(void* data);
557 void gatt_ind_ack_timeout(void* data);
558 void gatt_start_ind_ack_timer(tGATT_TCB& tcb, uint16_t cid);
559 void gatt_stop_ind_ack_timer(tGATT_TCB* p_tcb, uint16_t cid);
560 tGATT_STATUS gatt_send_error_rsp(tGATT_TCB& tcb, uint16_t cid, uint8_t err_code, uint8_t op_code,
561                                  uint16_t handle, bool deq);
562 
563 bool gatt_is_srv_chg_ind_pending(tGATT_TCB* p_tcb);
564 tGATTS_SRV_CHG* gatt_is_bda_in_the_srv_chg_clt_list(const RawAddress& bda);
565 
566 bool gatt_find_the_connected_bda(uint8_t start_idx, RawAddress& bda, uint8_t* p_found_idx,
567                                  tBT_TRANSPORT* p_transport);
568 void gatt_set_srv_chg(void);
569 void gatt_delete_dev_from_srv_chg_clt_list(const RawAddress& bd_addr);
570 void gatt_add_pending_ind(tGATT_TCB* p_tcb, tGATT_VALUE* p_ind);
571 void gatt_free_srvc_db_buffer_app_id(const bluetooth::Uuid& app_id);
572 bool gatt_cl_send_next_cmd_inq(tGATT_TCB& tcb);
573 tCONN_ID gatt_create_conn_id(tTCB_IDX tcb_idx, tGATT_IF gatt_if);
574 tTCB_IDX gatt_get_tcb_idx(tCONN_ID conn_id);
575 tGATT_IF gatt_get_gatt_if(tCONN_ID conn_id);
576 
577 /* reserved handle list */
578 std::list<tGATT_HDL_LIST_ELEM>::iterator gatt_find_hdl_buffer_by_app_id(
579         const bluetooth::Uuid& app_uuid128, bluetooth::Uuid* p_svc_uuid, uint16_t svc_inst);
580 tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle);
581 tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg);
582 
583 /* for background connection */
584 bool gatt_auto_connect_dev_remove(tGATT_IF gatt_if, const RawAddress& bd_addr);
585 
586 /* server function */
587 std::list<tGATT_SRV_LIST_ELEM>::iterator gatt_sr_find_i_rcb_by_handle(uint16_t handle);
588 tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if, uint32_t trans_id,
589                                      uint8_t op_code, tGATT_STATUS status, tGATTS_RSP* p_msg,
590                                      tGATT_SR_CMD* sr_res_p);
591 void gatt_server_handle_client_req(tGATT_TCB& p_tcb, uint16_t cid, uint8_t op_code, uint16_t len,
592                                    uint8_t* p_data);
593 void gatt_sr_send_req_callback(tCONN_ID conn_id, uint32_t trans_id, uint8_t op_code,
594                                tGATTS_DATA* p_req_data);
595 uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code, uint16_t handle);
596 bool gatt_cancel_open(tGATT_IF gatt_if, const RawAddress& bda);
597 void gatt_notify_phy_updated(tHCI_STATUS status, uint16_t handle, uint8_t tx_phy, uint8_t rx_phy);
598 void gatt_notify_subrate_change(uint16_t handle, uint16_t subrate_factor, uint16_t latency,
599                                 uint16_t cont_num, uint16_t timeout, uint8_t status);
600 /*   */
601 
602 bool gatt_tcb_is_cid_busy(tGATT_TCB& tcb, uint16_t cid);
603 
604 tGATT_REG* gatt_get_regcb(tGATT_IF gatt_if);
605 bool gatt_is_clcb_allocated(tCONN_ID conn_id);
606 tGATT_CLCB* gatt_clcb_alloc(tCONN_ID conn_id);
607 
608 bool gatt_tcb_get_cid_available_for_indication(tGATT_TCB* p_tcb, bool eatt_support,
609                                                uint16_t** indicate_handle_p, uint16_t* cid_p);
610 bool gatt_tcb_find_indicate_handle(tGATT_TCB& tcb, uint16_t cid, uint16_t* indicated_handle_p);
611 uint16_t gatt_tcb_get_att_cid(tGATT_TCB& tcb, bool eatt_support);
612 uint16_t gatt_tcb_get_payload_size(tGATT_TCB& tcb, uint16_t cid);
613 std::string gatt_tcb_get_holders_info_string(const tGATT_TCB* p_tcb);
614 void gatt_clcb_invalidate(tGATT_TCB* p_tcb, const tGATT_CLCB* p_clcb);
615 uint16_t gatt_get_mtu(const RawAddress& bda, tBT_TRANSPORT transport);
616 bool gatt_is_pending_mtu_exchange(tGATT_TCB* p_tcb);
617 void gatt_set_conn_id_waiting_for_mtu_exchange(tGATT_TCB* p_tcb, tCONN_ID conn_id);
618 
619 void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB& p_tcb);
620 bool gatt_sr_is_cback_cnt_zero(tGATT_TCB& p_tcb);
621 bool gatt_sr_is_prep_cnt_zero(tGATT_TCB& p_tcb);
622 void gatt_sr_reset_cback_cnt(tGATT_TCB& p_tcb, uint16_t cid);
623 void gatt_sr_reset_prep_cnt(tGATT_TCB& tcb);
624 tGATT_SR_CMD* gatt_sr_get_cmd_by_trans_id(tGATT_TCB* p_tcb, uint32_t trans_id);
625 tGATT_SR_CMD* gatt_sr_get_cmd_by_cid(tGATT_TCB& tcb, uint16_t cid);
626 tGATT_READ_MULTI* gatt_sr_get_read_multi(tGATT_TCB& tcb, uint16_t cid);
627 void gatt_sr_update_cback_cnt(tGATT_TCB& p_tcb, uint16_t cid, tGATT_IF gatt_if, bool is_inc,
628                               bool is_reset_first);
629 void gatt_sr_update_prep_cnt(tGATT_TCB& tcb, tGATT_IF gatt_if, bool is_inc, bool is_reset_first);
630 
631 tGATT_TCB* gatt_find_tcb_by_cid(uint16_t lcid);
632 tGATT_TCB* gatt_allocate_tcb_by_bdaddr(const RawAddress& bda, tBT_TRANSPORT transport);
633 tGATT_TCB* gatt_get_tcb_by_idx(uint8_t tcb_idx);
634 tGATT_TCB* gatt_find_tcb_by_addr(const RawAddress& bda, tBT_TRANSPORT transport);
635 bool gatt_send_ble_burst_data(const RawAddress& remote_bda, BT_HDR* p_buf);
636 uint16_t gatt_get_mtu_pref(const tGATT_REG* p_reg, const RawAddress& bda);
637 uint16_t gatt_get_apps_preferred_mtu(const RawAddress& bda);
638 void gatt_remove_apps_mtu_prefs(const RawAddress& bda);
639 
640 /* GATT client functions */
641 void gatt_dequeue_sr_cmd(tGATT_TCB& tcb, uint16_t cid);
642 tGATT_STATUS gatt_send_write_msg(tGATT_TCB& p_tcb, tGATT_CLCB* p_clcb, uint8_t op_code,
643                                  uint16_t handle, uint16_t len, uint16_t offset, uint8_t* p_data);
644 void gatt_cleanup_upon_disc(const RawAddress& bda, tGATT_DISCONN_REASON reason,
645                             tBT_TRANSPORT transport);
646 void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status, void* p_data);
647 
648 void gatt_act_discovery(tGATT_CLCB* p_clcb);
649 void gatt_act_read(tGATT_CLCB* p_clcb, uint16_t offset);
650 void gatt_act_write(tGATT_CLCB* p_clcb, uint8_t sec_act);
651 tGATT_CLCB* gatt_cmd_dequeue(tGATT_TCB& tcb, uint16_t cid, uint8_t* p_opcode);
652 bool gatt_cmd_enq(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, bool to_send, uint8_t op_code, BT_HDR* p_buf);
653 void gatt_client_handle_server_rsp(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code, uint16_t len,
654                                    uint8_t* p_data);
655 void gatt_send_queue_write_cancel(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, tGATT_EXEC_FLAG flag);
656 bool gatt_is_outstanding_msg_in_att_send_queue(const tGATT_TCB& tcb);
657 
658 /* gatt_auth.cc */
659 bool gatt_security_check_start(tGATT_CLCB* p_clcb);
660 void gatt_verify_signature(tGATT_TCB& tcb, uint16_t cid, BT_HDR* p_buf);
661 tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB& tcb);
662 tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb);
663 void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act);
664 
665 /* gatt_db.cc */
666 void gatts_init_service_db(tGATT_SVC_DB& db, const bluetooth::Uuid& service, bool is_pri,
667                            uint16_t s_hdl, uint16_t num_handle);
668 uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle, uint16_t e_handle,
669                                     const bluetooth::Uuid& service);
670 uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm, tGATT_CHAR_PROP property,
671                                   const bluetooth::Uuid& char_uuid);
672 uint16_t gatts_add_char_ext_prop_descr(tGATT_SVC_DB& db, uint16_t extended_properties);
673 uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm, const bluetooth::Uuid& dscp_uuid);
674 tGATT_STATUS gatts_db_read_attr_value_by_type(tGATT_TCB& tcb, uint16_t cid, tGATT_SVC_DB* p_db,
675                                               uint8_t op_code, BT_HDR* p_rsp, uint16_t s_handle,
676                                               uint16_t e_handle, const bluetooth::Uuid& type,
677                                               uint16_t* p_len, tGATT_SEC_FLAG sec_flag,
678                                               uint8_t key_size, uint32_t trans_id,
679                                               uint16_t* p_cur_handle);
680 tGATT_STATUS gatts_read_attr_value_by_handle(tGATT_TCB& tcb, uint16_t cid, tGATT_SVC_DB* p_db,
681                                              uint8_t op_code, uint16_t handle, uint16_t offset,
682                                              uint8_t* p_value, uint16_t* p_len, uint16_t mtu,
683                                              tGATT_SEC_FLAG sec_flag, uint8_t key_size,
684                                              uint32_t trans_id);
685 tGATT_STATUS gatts_write_attr_perm_check(tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle,
686                                          uint16_t offset, uint8_t* p_data, uint16_t len,
687                                          tGATT_SEC_FLAG sec_flag, uint8_t key_size);
688 tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB* p_db, bool is_long, uint16_t handle,
689                                         tGATT_SEC_FLAG sec_flag, uint8_t key_size);
690 bluetooth::Uuid* gatts_get_service_uuid(tGATT_SVC_DB* p_db);
691 void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb);
692 
693 /* gatt_sr_hash.cc */
694 Octet16 gatts_calculate_database_hash(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr);
695 
696 namespace bluetooth {
697 namespace legacy {
698 namespace testing {
699 BT_HDR* attp_build_value_cmd(uint16_t payload_size, uint8_t op_code, uint16_t handle,
700                              uint16_t offset, uint16_t len, uint8_t* p_data);
701 }  // namespace testing
702 }  // namespace legacy
703 }  // namespace bluetooth
704 
705 namespace std {
706 template <>
707 struct formatter<tGATT_CH_STATE> : enum_formatter<tGATT_CH_STATE> {};
708 }  // namespace std
709 
710 #endif
711