1/******************************************************************************
2 *
3 *  Copyright (C) 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 "bt_target.h"
23
24#include "btm_ble_api.h"
25#include "btu.h"
26#include "gatt_api.h"
27#include "osi/include/fixed_queue.h"
28
29#include <base/strings/stringprintf.h>
30#include <string.h>
31#include <list>
32#include <unordered_set>
33#include <vector>
34
35#define GATT_CREATE_CONN_ID(tcb_idx, gatt_if) \
36  ((uint16_t)((((uint8_t)(tcb_idx)) << 8) | ((uint8_t)(gatt_if))))
37#define GATT_GET_TCB_IDX(conn_id) ((uint8_t)(((uint16_t)(conn_id)) >> 8))
38#define GATT_GET_GATT_IF(conn_id) ((tGATT_IF)((uint8_t)(conn_id)))
39
40#define GATT_TRANS_ID_MAX 0x0fffffff /* 4 MSB is reserved */
41
42/* security action for GATT write and read request */
43#define GATT_SEC_NONE 0
44#define GATT_SEC_OK 1
45#define GATT_SEC_SIGN_DATA 2       /* compute the signature for the write cmd */
46#define GATT_SEC_ENCRYPT 3         /* encrypt the link with current key */
47#define GATT_SEC_ENCRYPT_NO_MITM 4 /* unauthenticated encryption or better */
48#define GATT_SEC_ENCRYPT_MITM 5    /* authenticated encryption */
49#define GATT_SEC_ENC_PENDING 6     /* wait for link encryption pending */
50typedef uint8_t tGATT_SEC_ACTION;
51
52#define GATT_ATTR_OP_SPT_MTU (0x00000001 << 0)
53#define GATT_ATTR_OP_SPT_FIND_INFO (0x00000001 << 1)
54#define GATT_ATTR_OP_SPT_FIND_BY_TYPE (0x00000001 << 2)
55#define GATT_ATTR_OP_SPT_READ_BY_TYPE (0x00000001 << 3)
56#define GATT_ATTR_OP_SPT_READ (0x00000001 << 4)
57#define GATT_ATTR_OP_SPT_MULT_READ (0x00000001 << 5)
58#define GATT_ATTR_OP_SPT_READ_BLOB (0x00000001 << 6)
59#define GATT_ATTR_OP_SPT_READ_BY_GRP_TYPE (0x00000001 << 7)
60#define GATT_ATTR_OP_SPT_WRITE (0x00000001 << 8)
61#define GATT_ATTR_OP_SPT_WRITE_CMD (0x00000001 << 9)
62#define GATT_ATTR_OP_SPT_PREP_WRITE (0x00000001 << 10)
63#define GATT_ATTR_OP_SPT_EXE_WRITE (0x00000001 << 11)
64#define GATT_ATTR_OP_SPT_HDL_VALUE_CONF (0x00000001 << 12)
65#define GATT_ATTR_OP_SP_SIGN_WRITE (0x00000001 << 13)
66
67#define GATT_INDEX_INVALID 0xff
68
69#define GATT_PENDING_REQ_NONE 0
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/* characteristic descriptor type */
83#define GATT_DESCR_EXT_DSCPTOR 1  /* Characteristic Extended Properties */
84#define GATT_DESCR_USER_DSCPTOR 2 /* Characteristic User Description    */
85#define GATT_DESCR_CLT_CONFIG 3   /* Client Characteristic Configuration */
86#define GATT_DESCR_SVR_CONFIG 4   /* Server Characteristic Configuration */
87#define GATT_DESCR_PRES_FORMAT 5  /* Characteristic Presentation Format */
88#define GATT_DESCR_AGGR_FORMAT 6  /* Characteristic Aggregate Format */
89#define GATT_DESCR_VALID_RANGE 7  /* Characteristic Valid Range */
90#define GATT_DESCR_UNKNOWN 0xff
91
92#define GATT_SEC_FLAG_LKEY_UNAUTHED BTM_SEC_FLAG_LKEY_KNOWN
93#define GATT_SEC_FLAG_LKEY_AUTHED BTM_SEC_FLAG_LKEY_AUTHED
94#define GATT_SEC_FLAG_ENCRYPTED BTM_SEC_FLAG_ENCRYPTED
95typedef uint8_t tGATT_SEC_FLAG;
96
97/* Find Information Response Type
98*/
99#define GATT_INFO_TYPE_PAIR_16 0x01
100#define GATT_INFO_TYPE_PAIR_128 0x02
101
102/*  GATT client FIND_TYPE_VALUE_Request data */
103typedef struct {
104  tBT_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
109      value[GATT_MAX_MTU_SIZE]; /* pointer to the attribute value to be found */
110} tGATT_FIND_TYPE_VALUE;
111
112/* client request message to ATT protocol
113*/
114typedef union {
115  tGATT_READ_BY_TYPE browse;             /* read by type request */
116  tGATT_FIND_TYPE_VALUE find_type_value; /* find by type value */
117  tGATT_READ_MULTI read_multi;           /* read multiple request */
118  tGATT_READ_PARTIAL read_blob;          /* read blob */
119  tGATT_VALUE attr_value;                /* write request */
120                                         /* prepare write */
121  /* write blob */
122  uint16_t handle; /* read,  handle value confirmation */
123  uint16_t mtu;
124  tGATT_EXEC_FLAG exec_write; /* execute write */
125} tGATT_CL_MSG;
126
127/* error response strucutre */
128typedef struct {
129  uint16_t handle;
130  uint8_t cmd_code;
131  uint8_t reason;
132} tGATT_ERROR;
133
134/* server response message to ATT protocol
135*/
136typedef union {
137  /* data type            member          event   */
138  tGATT_VALUE attr_value; /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
139                          /* READ_BLOB, READ_BY_TYPE */
140  tGATT_ERROR error;      /* ERROR_RSP */
141  uint16_t handle;        /* WRITE, WRITE_BLOB */
142  uint16_t mtu;           /* exchange MTU request */
143} tGATT_SR_MSG;
144
145/* Characteristic declaration attribute value
146*/
147typedef struct {
148  tGATT_CHAR_PROP property;
149  uint16_t char_val_handle;
150} tGATT_CHAR_DECL;
151
152/* attribute value maintained in the server database
153*/
154typedef union {
155  tBT_UUID uuid;               /* service declaration */
156  tGATT_CHAR_DECL char_decl;   /* characteristic declaration */
157  tGATT_INCL_SRVC incl_handle; /* included service */
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
165typedef uint8_t tGATT_ATTR_UUID_TYPE;
166
167/* 16 bits UUID Attribute in server database
168*/
169typedef struct {
170  std::unique_ptr<tGATT_ATTR_VALUE> p_value;
171  tGATT_PERM permission;
172  uint16_t handle;
173  tBT_UUID uuid;
174  bt_gatt_db_attribute_type_t gatt_type;
175} tGATT_ATTR;
176
177/* Service Database definition
178*/
179typedef 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
190typedef struct {
191  tBT_UUID app_uuid128;
192  tGATT_CBACK app_cb;
193  tGATT_IF gatt_if; /* one based */
194  bool in_use;
195  uint8_t listening; /* if adv for all has been enabled */
196} tGATT_REG;
197
198struct tGATT_CLCB;
199
200/* command queue for each connection */
201typedef struct {
202  BT_HDR* p_cmd;
203  tGATT_CLCB* p_clcb;
204  uint8_t op_code;
205  bool to_send;
206} tGATT_CMD_Q;
207
208#if GATT_MAX_SR_PROFILES <= 8
209typedef uint8_t tGATT_APP_MASK;
210#elif GATT_MAX_SR_PROFILES <= 16
211typedef uint16_t tGATT_APP_MASK;
212#elif GATT_MAX_SR_PROFILES <= 32
213typedef uint32_t tGATT_APP_MASK;
214#endif
215
216/* command details for each connection */
217typedef struct {
218  BT_HDR* p_rsp_msg;
219  uint32_t trans_id;
220  tGATT_READ_MULTI multi_req;
221  fixed_queue_t* multi_rsp_q;
222  uint16_t handle;
223  uint8_t op_code;
224  uint8_t status;
225  uint8_t cback_cnt[GATT_MAX_APPS];
226} tGATT_SR_CMD;
227
228#define GATT_CH_CLOSE 0
229#define GATT_CH_CLOSING 1
230#define GATT_CH_CONN 2
231#define GATT_CH_CFG 3
232#define GATT_CH_OPEN 4
233
234typedef uint8_t tGATT_CH_STATE;
235
236#define GATT_GATT_START_HANDLE 1
237#define GATT_GAP_START_HANDLE 20
238#define GATT_APP_START_HANDLE 40
239
240typedef struct hdl_cfg {
241  uint16_t gatt_start_hdl;
242  uint16_t gap_start_hdl;
243  uint16_t app_start_hdl;
244} tGATT_HDL_CFG;
245
246typedef struct hdl_list_elem {
247  tGATTS_HNDL_RANGE asgn_range; /* assigned handle range */
248  tGATT_SVC_DB svc_db;
249} tGATT_HDL_LIST_ELEM;
250
251/* Data Structure used for GATT server                                        */
252/* A GATT registration record consists of a handle, and 1 or more attributes  */
253/* A service registration information record consists of beginning and ending */
254/* attribute handle, service UUID and a set of GATT server callback.          */
255typedef struct {
256  tGATT_SVC_DB* p_db;  /* pointer to the service database */
257  tBT_UUID app_uuid;   /* applicatino UUID */
258  uint32_t sdp_handle; /* primamry service SDP handle */
259  uint16_t type;       /* service type UUID, primary or secondary */
260  uint16_t s_hdl;      /* service starting handle */
261  uint16_t e_hdl;      /* service ending handle */
262  tGATT_IF gatt_if;    /* this service is belong to which application */
263  bool is_primary;
264} tGATT_SRV_LIST_ELEM;
265
266typedef struct {
267  std::queue<tGATT_CLCB*> pending_enc_clcb; /* pending encryption channel q */
268  tGATT_SEC_ACTION sec_act;
269  RawAddress peer_bda;
270  tBT_TRANSPORT transport;
271  uint32_t trans_id;
272
273  uint16_t att_lcid; /* L2CAP channel ID for ATT */
274  uint16_t payload_size;
275
276  tGATT_CH_STATE ch_state;
277  uint8_t ch_flags;
278
279  std::unordered_set<uint8_t> app_hold_link;
280
281  /* server needs */
282  /* server response data */
283  tGATT_SR_CMD sr_cmd;
284  uint16_t indicate_handle;
285  fixed_queue_t* pending_ind_q;
286
287  alarm_t* conf_timer; /* peer confirm to indication timer */
288
289  uint8_t prep_cnt[GATT_MAX_APPS];
290  uint8_t ind_count;
291
292  std::queue<tGATT_CMD_Q> cl_cmd_q;
293  alarm_t* ind_ack_timer; /* local app confirm to indication timer */
294
295  bool in_use;
296  uint8_t tcb_idx;
297} tGATT_TCB;
298
299/* logic channel */
300typedef struct {
301  uint16_t
302      next_disc_start_hdl; /* starting handle for the next inc srvv discovery */
303  tGATT_DISC_RES result;
304  bool wait_for_read_rsp;
305} tGATT_READ_INC_UUID128;
306struct tGATT_CLCB {
307  tGATT_TCB* p_tcb; /* associated TCB of this CLCB */
308  tGATT_REG* p_reg; /* owner of this CLCB */
309  uint8_t sccb_idx;
310  uint8_t* p_attr_buf; /* attribute buffer for read multiple, prepare write */
311  tBT_UUID uuid;
312  uint16_t conn_id; /* connection handle */
313  uint16_t s_handle; /* starting handle of the active request */
314  uint16_t e_handle; /* ending handle of the active request */
315  uint16_t counter; /* used as offset, attribute length, num of prepare write */
316  uint16_t start_offset;
317  tGATT_AUTH_REQ auth_req; /* authentication requirement */
318  uint8_t operation;       /* one logic channel can have one operation active */
319  uint8_t op_subtype;      /* operation subtype */
320  uint8_t status;          /* operation status */
321  bool first_read_blob_after_read;
322  tGATT_READ_INC_UUID128 read_uuid128;
323  bool in_use;
324  alarm_t* gatt_rsp_timer_ent; /* peer response timer */
325  uint8_t retry_count;
326};
327
328typedef struct {
329  uint16_t handle;
330  uint16_t uuid;
331  uint32_t service_change;
332} tGATT_SVC_CHG;
333
334typedef struct {
335  std::unordered_set<tGATT_IF> gatt_if;
336  RawAddress remote_bda;
337} tGATT_BG_CONN_DEV;
338
339#define GATT_SVC_CHANGED_CONNECTING 1     /* wait for connection */
340#define GATT_SVC_CHANGED_SERVICE 2        /* GATT service discovery */
341#define GATT_SVC_CHANGED_CHARACTERISTIC 3 /* service change char discovery */
342#define GATT_SVC_CHANGED_DESCRIPTOR 4     /* service change CCC discoery */
343#define GATT_SVC_CHANGED_CONFIGURE_CCCD 5 /* config CCC */
344
345typedef struct {
346  uint16_t conn_id;
347  bool in_use;
348  bool connected;
349  RawAddress bda;
350  tBT_TRANSPORT transport;
351
352  /* GATT service change CCC related variables */
353  uint8_t ccc_stage;
354  uint8_t ccc_result;
355  uint16_t s_handle;
356  uint16_t e_handle;
357} tGATT_PROFILE_CLCB;
358
359typedef struct {
360  tGATT_TCB tcb[GATT_MAX_PHY_CHANNEL];
361  fixed_queue_t* sign_op_queue;
362
363  uint16_t next_handle;     /* next available handle */
364  uint16_t last_primary_s_handle; /* handle of last primary service */
365  tGATT_SVC_CHG gattp_attr; /* GATT profile attribute service change */
366  tGATT_IF gatt_if;
367  std::list<tGATT_HDL_LIST_ELEM>* hdl_list_info;
368  std::list<tGATT_SRV_LIST_ELEM>* srv_list_info;
369
370  fixed_queue_t* srv_chg_clt_q; /* service change clients queue */
371  tGATT_REG cl_rcb[GATT_MAX_APPS];
372  tGATT_CLCB clcb[GATT_CL_MAX_LCB]; /* connection link control block*/
373  uint16_t def_mtu_size;
374
375#if (GATT_CONFORMANCE_TESTING == TRUE)
376  bool enable_err_rsp;
377  uint8_t req_op_code;
378  uint8_t err_status;
379  uint16_t handle;
380#endif
381
382  tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
383  uint16_t
384      handle_of_h_r; /* Handle of the handles reused characteristic value */
385
386  tGATT_APPL_INFO cb_info;
387
388  tGATT_HDL_CFG hdl_cfg;
389  std::list<tGATT_BG_CONN_DEV> bgconn_dev;
390} tGATT_CB;
391
392#define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
393
394/* Global GATT data */
395extern tGATT_CB gatt_cb;
396
397#if (GATT_CONFORMANCE_TESTING == TRUE)
398extern void gatt_set_err_rsp(bool enable, uint8_t req_op_code,
399                             uint8_t err_status);
400#endif
401
402/* from gatt_main.cc */
403extern bool gatt_disconnect(tGATT_TCB* p_tcb);
404extern bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
405                             tBT_TRANSPORT transport, bool opportunistic,
406                             int8_t initiating_phys);
407extern bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
408                         tBT_TRANSPORT transport, uint8_t initiating_phys);
409extern void gatt_data_process(tGATT_TCB& p_tcb, BT_HDR* p_buf);
410extern void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
411                                          bool is_add, bool check_acl_link);
412
413extern void gatt_profile_db_init(void);
414extern void gatt_set_ch_state(tGATT_TCB* p_tcb, tGATT_CH_STATE ch_state);
415extern tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB* p_tcb);
416extern void gatt_init_srv_chg(void);
417extern void gatt_proc_srv_chg(void);
418extern void gatt_send_srv_chg_ind(const RawAddress& peer_bda);
419extern void gatt_chk_srv_chg(tGATTS_SRV_CHG* p_srv_chg_clt);
420extern void gatt_add_a_bonded_dev_for_srv_chg(const RawAddress& bda);
421
422/* from gatt_attr.cc */
423extern uint16_t gatt_profile_find_conn_id_by_bd_addr(const RawAddress& bda);
424
425/* Functions provided by att_protocol.cc */
426extern tGATT_STATUS attp_send_cl_msg(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
427                                     uint8_t op_code, tGATT_CL_MSG* p_msg);
428extern BT_HDR* attp_build_sr_msg(tGATT_TCB& tcb, uint8_t op_code,
429                                 tGATT_SR_MSG* p_msg);
430extern tGATT_STATUS attp_send_sr_msg(tGATT_TCB& tcb, BT_HDR* p_msg);
431extern tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB& tcb, BT_HDR* p_toL2CAP);
432
433/* utility functions */
434extern uint8_t* gatt_dbg_op_name(uint8_t op_code);
435extern uint32_t gatt_add_sdp_record(tBT_UUID* p_uuid, uint16_t start_hdl,
436                                    uint16_t end_hdl);
437extern bool gatt_parse_uuid_from_cmd(tBT_UUID* p_uuid, uint16_t len,
438                                     uint8_t** p_data);
439extern uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst, tBT_UUID uuid);
440extern bool gatt_uuid_compare(tBT_UUID src, tBT_UUID tar);
441extern void gatt_convert_uuid32_to_uuid128(uint8_t uuid_128[LEN_UUID_128],
442                                           uint32_t uuid_32);
443extern void gatt_sr_get_sec_info(const RawAddress& rem_bda,
444                                 tBT_TRANSPORT transport, uint8_t* p_sec_flag,
445                                 uint8_t* p_key_size);
446extern void gatt_start_rsp_timer(tGATT_CLCB* p_clcb);
447extern void gatt_start_conf_timer(tGATT_TCB* p_tcb);
448extern void gatt_rsp_timeout(void* data);
449extern void gatt_indication_confirmation_timeout(void* data);
450extern void gatt_ind_ack_timeout(void* data);
451extern void gatt_start_ind_ack_timer(tGATT_TCB& tcb);
452extern tGATT_STATUS gatt_send_error_rsp(tGATT_TCB& tcb, uint8_t err_code,
453                                        uint8_t op_code, uint16_t handle,
454                                        bool deq);
455extern void gatt_dbg_display_uuid(tBT_UUID bt_uuid);
456
457extern bool gatt_is_srv_chg_ind_pending(tGATT_TCB* p_tcb);
458extern tGATTS_SRV_CHG* gatt_is_bda_in_the_srv_chg_clt_list(
459    const RawAddress& bda);
460
461extern bool gatt_find_the_connected_bda(uint8_t start_idx, RawAddress& bda,
462                                        uint8_t* p_found_idx,
463                                        tBT_TRANSPORT* p_transport);
464extern void gatt_set_srv_chg(void);
465extern void gatt_delete_dev_from_srv_chg_clt_list(const RawAddress& bd_addr);
466extern tGATT_VALUE* gatt_add_pending_ind(tGATT_TCB* p_tcb, tGATT_VALUE* p_ind);
467extern void gatt_free_srvc_db_buffer_app_id(tBT_UUID* p_app_id);
468extern bool gatt_cl_send_next_cmd_inq(tGATT_TCB& tcb);
469
470/* reserved handle list */
471extern std::list<tGATT_HDL_LIST_ELEM>::iterator gatt_find_hdl_buffer_by_app_id(
472    tBT_UUID* p_app_uuid128, tBT_UUID* p_svc_uuid, uint16_t svc_inst);
473extern tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle);
474extern tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg);
475
476/* for background connection */
477extern bool gatt_update_auto_connect_dev(tGATT_IF gatt_if, bool add,
478                                         const RawAddress& bd_addr);
479extern bool gatt_is_bg_dev_for_app(tGATT_BG_CONN_DEV* p_dev, tGATT_IF gatt_if);
480extern bool gatt_remove_bg_dev_for_app(tGATT_IF gatt_if,
481                                       const RawAddress& bd_addr);
482extern uint8_t gatt_clear_bg_dev_for_addr(const RawAddress& bd_addr);
483extern tGATT_BG_CONN_DEV* gatt_find_bg_dev(const RawAddress& remote_bda);
484extern void gatt_deregister_bgdev_list(tGATT_IF gatt_if);
485
486/* server function */
487extern std::list<tGATT_SRV_LIST_ELEM>::iterator gatt_sr_find_i_rcb_by_handle(
488    uint16_t handle);
489extern bool gatt_sr_find_i_rcb_by_app_id(tBT_UUID* p_app_uuid128,
490                                         tBT_UUID* p_svc_uuid,
491                                         uint16_t svc_inst);
492extern tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
493                                            uint32_t trans_id, uint8_t op_code,
494                                            tGATT_STATUS status,
495                                            tGATTS_RSP* p_msg);
496extern void gatt_server_handle_client_req(tGATT_TCB& p_tcb, uint8_t op_code,
497                                          uint16_t len, uint8_t* p_data);
498extern void gatt_sr_send_req_callback(uint16_t conn_id, uint32_t trans_id,
499                                      uint8_t op_code, tGATTS_DATA* p_req_data);
500extern uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code,
501                                    uint16_t handle);
502extern bool gatt_cancel_open(tGATT_IF gatt_if, const RawAddress& bda);
503extern void gatt_notify_phy_updated(tGATT_TCB* p_tcb, uint8_t tx_phy,
504                                    uint8_t rx_phy, uint8_t status);
505
506/*   */
507
508extern tGATT_REG* gatt_get_regcb(tGATT_IF gatt_if);
509extern bool gatt_is_clcb_allocated(uint16_t conn_id);
510extern tGATT_CLCB* gatt_clcb_alloc(uint16_t conn_id);
511extern void gatt_clcb_dealloc(tGATT_CLCB* p_clcb);
512
513extern void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB& p_tcb);
514extern bool gatt_sr_is_cback_cnt_zero(tGATT_TCB& p_tcb);
515extern bool gatt_sr_is_prep_cnt_zero(tGATT_TCB& p_tcb);
516extern void gatt_sr_reset_cback_cnt(tGATT_TCB& p_tcb);
517extern void gatt_sr_reset_prep_cnt(tGATT_TCB& tcb);
518extern void gatt_sr_update_cback_cnt(tGATT_TCB& p_tcb, tGATT_IF gatt_if,
519                                     bool is_inc, bool is_reset_first);
520extern void gatt_sr_update_prep_cnt(tGATT_TCB& tcb, tGATT_IF gatt_if,
521                                    bool is_inc, bool is_reset_first);
522
523extern uint8_t gatt_num_clcb_by_bd_addr(const RawAddress& bda);
524extern tGATT_TCB* gatt_find_tcb_by_cid(uint16_t lcid);
525extern tGATT_TCB* gatt_allocate_tcb_by_bdaddr(const RawAddress& bda,
526                                              tBT_TRANSPORT transport);
527extern tGATT_TCB* gatt_get_tcb_by_idx(uint8_t tcb_idx);
528extern tGATT_TCB* gatt_find_tcb_by_addr(const RawAddress& bda,
529                                        tBT_TRANSPORT transport);
530extern bool gatt_send_ble_burst_data(const RawAddress& remote_bda,
531                                     BT_HDR* p_buf);
532
533/* GATT client functions */
534extern void gatt_dequeue_sr_cmd(tGATT_TCB& tcb);
535extern uint8_t gatt_send_write_msg(tGATT_TCB& p_tcb, tGATT_CLCB* p_clcb,
536                                   uint8_t op_code, uint16_t handle,
537                                   uint16_t len, uint16_t offset,
538                                   uint8_t* p_data);
539extern void gatt_cleanup_upon_disc(const RawAddress& bda, uint16_t reason,
540                                   tBT_TRANSPORT transport);
541extern void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status,
542                               void* p_data);
543
544extern void gatt_act_discovery(tGATT_CLCB* p_clcb);
545extern void gatt_act_read(tGATT_CLCB* p_clcb, uint16_t offset);
546extern void gatt_act_write(tGATT_CLCB* p_clcb, uint8_t sec_act);
547extern tGATT_CLCB* gatt_cmd_dequeue(tGATT_TCB& tcb, uint8_t* p_opcode);
548extern void gatt_cmd_enq(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, bool to_send,
549                         uint8_t op_code, BT_HDR* p_buf);
550extern void gatt_client_handle_server_rsp(tGATT_TCB& tcb, uint8_t op_code,
551                                          uint16_t len, uint8_t* p_data);
552extern void gatt_send_queue_write_cancel(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
553                                         tGATT_EXEC_FLAG flag);
554
555/* gatt_auth.cc */
556extern void gatt_security_check_start(tGATT_CLCB* p_clcb);
557extern void gatt_verify_signature(tGATT_TCB& tcb, BT_HDR* p_buf);
558extern tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB& tcb);
559extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb);
560extern void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act);
561
562/* gatt_db.cc */
563extern void gatts_init_service_db(tGATT_SVC_DB& db, tBT_UUID* p_service,
564                                  bool is_pri, uint16_t s_hdl,
565                                  uint16_t num_handle);
566extern uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle,
567                                           uint16_t e_handle, tBT_UUID service);
568extern uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm,
569                                         tGATT_CHAR_PROP property,
570                                         tBT_UUID& char_uuid);
571extern uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm,
572                                     tBT_UUID& dscp_uuid);
573extern tGATT_STATUS gatts_db_read_attr_value_by_type(
574    tGATT_TCB& tcb, tGATT_SVC_DB* p_db, uint8_t op_code, BT_HDR* p_rsp,
575    uint16_t s_handle, uint16_t e_handle, tBT_UUID type, uint16_t* p_len,
576    tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id,
577    uint16_t* p_cur_handle);
578extern tGATT_STATUS gatts_read_attr_value_by_handle(
579    tGATT_TCB& tcb, tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle,
580    uint16_t offset, uint8_t* p_value, uint16_t* p_len, uint16_t mtu,
581    tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id);
582extern tGATT_STATUS gatts_write_attr_perm_check(
583    tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle, uint16_t offset,
584    uint8_t* p_data, uint16_t len, tGATT_SEC_FLAG sec_flag, uint8_t key_size);
585extern tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB* p_db, bool is_long,
586                                               uint16_t handle,
587                                               tGATT_SEC_FLAG sec_flag,
588                                               uint8_t key_size);
589extern tBT_UUID* gatts_get_service_uuid(tGATT_SVC_DB* p_db);
590
591#endif
592