• 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 #ifndef GATT_API_H
19 #define GATT_API_H
20 
21 #include <bluetooth/log.h>
22 
23 #include <cstdint>
24 #include <list>
25 #include <string>
26 
27 #include "btm_ble_api.h"
28 #include "gattdefs.h"
29 #include "hardware/bt_gatt_types.h"
30 #include "include/hardware/bt_common_types.h"
31 #include "internal_include/bt_target.h"
32 #include "macros.h"
33 #include "stack/include/btm_ble_api_types.h"
34 #include "stack/include/hci_error_code.h"
35 #include "types/bluetooth/uuid.h"
36 #include "types/bt_transport.h"
37 #include "types/raw_address.h"
38 
39 /*****************************************************************************
40  *  Constants
41  ****************************************************************************/
42 /* Success code and error codes */
43 typedef enum GattStatus : uint8_t {
44   GATT_SUCCESS = 0x00,
45   GATT_INVALID_HANDLE = 0x01,
46   GATT_READ_NOT_PERMIT = 0x02,
47   GATT_WRITE_NOT_PERMIT = 0x03,
48   GATT_INVALID_PDU = 0x04,
49   GATT_INSUF_AUTHENTICATION = 0x05,
50   GATT_REQ_NOT_SUPPORTED = 0x06,
51   GATT_INVALID_OFFSET = 0x07,
52   GATT_INSUF_AUTHORIZATION = 0x08,
53   GATT_PREPARE_Q_FULL = 0x09,
54   GATT_NOT_FOUND = 0x0a,
55   GATT_NOT_LONG = 0x0b,
56   GATT_INSUF_KEY_SIZE = 0x0c,
57   GATT_INVALID_ATTR_LEN = 0x0d,
58   GATT_ERR_UNLIKELY = 0x0e,
59   GATT_INSUF_ENCRYPTION = 0x0f,
60   GATT_UNSUPPORT_GRP_TYPE = 0x10,
61   GATT_INSUF_RESOURCE = 0x11,
62   GATT_DATABASE_OUT_OF_SYNC = 0x12,
63   GATT_VALUE_NOT_ALLOWED = 0x13,
64   GATT_ILLEGAL_PARAMETER = 0x87,
65   GATT_NO_RESOURCES = 0x80,
66   GATT_INTERNAL_ERROR = 0x81,
67   GATT_WRONG_STATE = 0x82,
68   GATT_DB_FULL = 0x83,
69   GATT_BUSY = 0x84,
70   GATT_ERROR = 0x85,
71   GATT_CMD_STARTED = 0x86,
72   GATT_PENDING = 0x88,
73   GATT_AUTH_FAIL = 0x89,
74   GATT_INVALID_CFG = 0x8b,
75   GATT_SERVICE_STARTED = 0x8c,
76   GATT_ENCRYPED_MITM = GATT_SUCCESS,
77   GATT_ENCRYPED_NO_MITM = 0x8d,
78   GATT_NOT_ENCRYPTED = 0x8e,
79   GATT_CONGESTED = 0x8f,
80   GATT_DUP_REG = 0x90,      /* 0x90 */
81   GATT_ALREADY_OPEN = 0x91, /* 0x91 */
82   GATT_CANCEL = 0x92,       /* 0x92 */
83   GATT_CONNECTION_TIMEOUT = 0x93,
84   /* = 0xE0 ~ 0xFC reserved for future use */
85 
86   /* Client Characteristic Configuration Descriptor Improperly Configured */
87   GATT_CCC_CFG_ERR = 0xFD,
88   /* Procedure Already in progress */
89   GATT_PRC_IN_PROGRESS = 0xFE,
90   /* Attribute value out of range */
91   GATT_OUT_OF_RANGE = 0xFF,
92 } tGATT_STATUS;
93 
gatt_status_text(const tGATT_STATUS & status)94 inline std::string gatt_status_text(const tGATT_STATUS& status) {
95   switch (status) {
96     CASE_RETURN_TEXT(GATT_SUCCESS);  // Also GATT_ENCRYPED_MITM
97     CASE_RETURN_TEXT(GATT_INVALID_HANDLE);
98     CASE_RETURN_TEXT(GATT_READ_NOT_PERMIT);
99     CASE_RETURN_TEXT(GATT_WRITE_NOT_PERMIT);
100     CASE_RETURN_TEXT(GATT_INVALID_PDU);
101     CASE_RETURN_TEXT(GATT_INSUF_AUTHENTICATION);
102     CASE_RETURN_TEXT(GATT_REQ_NOT_SUPPORTED);
103     CASE_RETURN_TEXT(GATT_INVALID_OFFSET);
104     CASE_RETURN_TEXT(GATT_INSUF_AUTHORIZATION);
105     CASE_RETURN_TEXT(GATT_PREPARE_Q_FULL);
106     CASE_RETURN_TEXT(GATT_NOT_FOUND);
107     CASE_RETURN_TEXT(GATT_NOT_LONG);
108     CASE_RETURN_TEXT(GATT_INSUF_KEY_SIZE);
109     CASE_RETURN_TEXT(GATT_INVALID_ATTR_LEN);
110     CASE_RETURN_TEXT(GATT_ERR_UNLIKELY);
111     CASE_RETURN_TEXT(GATT_INSUF_ENCRYPTION);
112     CASE_RETURN_TEXT(GATT_UNSUPPORT_GRP_TYPE);
113     CASE_RETURN_TEXT(GATT_INSUF_RESOURCE);
114     CASE_RETURN_TEXT(GATT_DATABASE_OUT_OF_SYNC);
115     CASE_RETURN_TEXT(GATT_VALUE_NOT_ALLOWED);
116     CASE_RETURN_TEXT(GATT_ILLEGAL_PARAMETER);
117     CASE_RETURN_TEXT(GATT_NO_RESOURCES);
118     CASE_RETURN_TEXT(GATT_INTERNAL_ERROR);
119     CASE_RETURN_TEXT(GATT_WRONG_STATE);
120     CASE_RETURN_TEXT(GATT_DB_FULL);
121     CASE_RETURN_TEXT(GATT_BUSY);
122     CASE_RETURN_TEXT(GATT_ERROR);
123     CASE_RETURN_TEXT(GATT_CMD_STARTED);
124     CASE_RETURN_TEXT(GATT_PENDING);
125     CASE_RETURN_TEXT(GATT_AUTH_FAIL);
126     CASE_RETURN_TEXT(GATT_INVALID_CFG);
127     CASE_RETURN_TEXT(GATT_SERVICE_STARTED);
128     CASE_RETURN_TEXT(GATT_ENCRYPED_NO_MITM);
129     CASE_RETURN_TEXT(GATT_NOT_ENCRYPTED);
130     CASE_RETURN_TEXT(GATT_CONGESTED);
131     CASE_RETURN_TEXT(GATT_DUP_REG);
132     CASE_RETURN_TEXT(GATT_ALREADY_OPEN);
133     CASE_RETURN_TEXT(GATT_CANCEL);
134     CASE_RETURN_TEXT(GATT_CONNECTION_TIMEOUT);
135     CASE_RETURN_TEXT(GATT_CCC_CFG_ERR);
136     CASE_RETURN_TEXT(GATT_PRC_IN_PROGRESS);
137     CASE_RETURN_TEXT(GATT_OUT_OF_RANGE);
138     default:
139       return std::format("UNKNOWN[{}]", static_cast<uint8_t>(status));
140   }
141 }
142 
143 typedef enum : uint8_t {
144   GATT_RSP_ERROR = 0x01,
145   GATT_REQ_MTU = 0x02,
146   GATT_RSP_MTU = 0x03,
147   GATT_REQ_FIND_INFO = 0x04,
148   GATT_RSP_FIND_INFO = 0x05,
149   GATT_REQ_FIND_TYPE_VALUE = 0x06,
150   GATT_RSP_FIND_TYPE_VALUE = 0x07,
151   GATT_REQ_READ_BY_TYPE = 0x08,
152   GATT_RSP_READ_BY_TYPE = 0x09,
153   GATT_REQ_READ = 0x0A,
154   GATT_RSP_READ = 0x0B,
155   GATT_REQ_READ_BLOB = 0x0C,
156   GATT_RSP_READ_BLOB = 0x0D,
157   GATT_REQ_READ_MULTI = 0x0E,
158   GATT_RSP_READ_MULTI = 0x0F,
159   GATT_REQ_READ_BY_GRP_TYPE = 0x10,
160   GATT_RSP_READ_BY_GRP_TYPE = 0x11,
161   /*                 0001-0010 (write)*/
162   GATT_REQ_WRITE = 0x12,
163   GATT_RSP_WRITE = 0x13,
164   /* changed in V4.0 01001-0010(write cmd)*/
165   GATT_CMD_WRITE = 0x52,
166   GATT_REQ_PREPARE_WRITE = 0x16,
167   GATT_RSP_PREPARE_WRITE = 0x17,
168   GATT_REQ_EXEC_WRITE = 0x18,
169   GATT_RSP_EXEC_WRITE = 0x19,
170   GATT_HANDLE_VALUE_NOTIF = 0x1B,
171   GATT_HANDLE_VALUE_IND = 0x1D,
172   GATT_HANDLE_VALUE_CONF = 0x1E,
173 
174   GATT_REQ_READ_MULTI_VAR = 0x20,
175   GATT_RSP_READ_MULTI_VAR = 0x21,
176   GATT_HANDLE_MULTI_VALUE_NOTIF = 0x23,
177 
178   /* changed in V4.0 1101-0010 (signed write)  see write cmd above*/
179   GATT_SIGN_CMD_WRITE = 0xD2,
180   /* 0x1E = 30 + 1 = 31*/
181   GATT_OP_CODE_MAX = (GATT_HANDLE_MULTI_VALUE_NOTIF + 1),
182 } tGATT_OP_CODE;
183 
184 typedef enum : uint8_t {
185   MTU_EXCHANGE_DEVICE_DISCONNECTED = 0x00,
186   MTU_EXCHANGE_NOT_ALLOWED,
187   MTU_EXCHANGE_NOT_DONE_YET,
188   MTU_EXCHANGE_IN_PROGRESS,
189   MTU_EXCHANGE_ALREADY_DONE,
190 } tGATTC_TryMtuRequestResult;
191 
gatt_op_code_text(const tGATT_OP_CODE & op_code)192 inline std::string gatt_op_code_text(const tGATT_OP_CODE& op_code) {
193   switch (op_code) {
194     case GATT_RSP_ERROR:
195       return std::string("GATT_RSP_ERROR");
196     case GATT_REQ_MTU:
197       return std::string("GATT_REQ_MTU");
198     case GATT_RSP_MTU:
199       return std::string("GATT_RSP_MTU");
200     case GATT_REQ_FIND_INFO:
201       return std::string("GATT_REQ_FIND_INFO");
202     case GATT_RSP_FIND_INFO:
203       return std::string("GATT_RSP_FIND_INFO");
204     case GATT_REQ_FIND_TYPE_VALUE:
205       return std::string("GATT_REQ_FIND_TYPE_VALUE");
206     case GATT_RSP_FIND_TYPE_VALUE:
207       return std::string("GATT_RSP_FIND_TYPE_VALUE");
208     case GATT_REQ_READ_BY_TYPE:
209       return std::string("GATT_REQ_READ_BY_TYPE");
210     case GATT_RSP_READ_BY_TYPE:
211       return std::string("GATT_RSP_READ_BY_TYPE");
212     case GATT_REQ_READ:
213       return std::string("GATT_REQ_READ");
214     case GATT_RSP_READ:
215       return std::string("GATT_RSP_READ");
216     case GATT_REQ_READ_BLOB:
217       return std::string("GATT_REQ_READ_BLOB");
218     case GATT_RSP_READ_BLOB:
219       return std::string("GATT_RSP_READ_BLOB");
220     case GATT_REQ_READ_MULTI:
221       return std::string("GATT_REQ_READ_MULTI");
222     case GATT_RSP_READ_MULTI:
223       return std::string("GATT_RSP_READ_MULTI");
224     case GATT_REQ_READ_BY_GRP_TYPE:
225       return std::string("GATT_REQ_READ_BY_GRP_TYPE");
226     case GATT_RSP_READ_BY_GRP_TYPE:
227       return std::string("GATT_RSP_READ_BY_GRP_TYPE");
228     case GATT_REQ_WRITE:
229       return std::string("GATT_REQ_WRITE");
230     case GATT_RSP_WRITE:
231       return std::string("GATT_RSP_WRITE");
232     case GATT_CMD_WRITE:
233       return std::string("GATT_CMD_WRITE");
234     case GATT_REQ_PREPARE_WRITE:
235       return std::string("GATT_REQ_PREPARE_WRITE");
236     case GATT_RSP_PREPARE_WRITE:
237       return std::string("GATT_RSP_PREPARE_WRITE");
238     case GATT_REQ_EXEC_WRITE:
239       return std::string("GATT_REQ_EXEC_WRITE");
240     case GATT_RSP_EXEC_WRITE:
241       return std::string("GATT_RSP_EXEC_WRITE");
242     case GATT_HANDLE_VALUE_NOTIF:
243       return std::string("GATT_HANDLE_VALUE_NOTIF");
244     case GATT_HANDLE_VALUE_IND:
245       return std::string("GATT_HANDLE_VALUE_IND");
246     case GATT_HANDLE_VALUE_CONF:
247       return std::string("GATT_HANDLE_VALUE_CONF");
248     case GATT_REQ_READ_MULTI_VAR:
249       return std::string("GATT_REQ_READ_MULTI_VAR");
250     case GATT_RSP_READ_MULTI_VAR:
251       return std::string("GATT_RSP_READ_MULTI_VAR");
252     case GATT_HANDLE_MULTI_VALUE_NOTIF:
253       return std::string("GATT_HANDLE_MULTI_VALUE_NOTIF");
254     case GATT_SIGN_CMD_WRITE:
255       return std::string("GATT_SIGN_CMD_WRITE");
256     case GATT_OP_CODE_MAX:
257       return std::string("GATT_OP_CODE_MAX");
258   };
259 }
260 
261 #define GATT_HANDLE_IS_VALID(x) ((x) != 0)
262 
263 typedef enum : uint16_t {
264   GATT_CONN_OK = 0,
265   /* general L2cap failure  */
266   GATT_CONN_L2C_FAILURE = 1,
267   /* 0x08 connection timeout  */
268   GATT_CONN_TIMEOUT = HCI_ERR_CONNECTION_TOUT,
269   /* 0x13 connection terminate by peer user  */
270   GATT_CONN_TERMINATE_PEER_USER = HCI_ERR_PEER_USER,
271   /* 0x16 connection terminated by local host  */
272   GATT_CONN_TERMINATE_LOCAL_HOST = HCI_ERR_CONN_CAUSE_LOCAL_HOST,
273   /* 0x22 connection fail for LMP response tout */
274   GATT_CONN_LMP_TIMEOUT = HCI_ERR_LMP_RESPONSE_TIMEOUT,
275 
276   GATT_CONN_FAILED_ESTABLISHMENT = HCI_ERR_CONN_FAILED_ESTABLISHMENT,
277 
278   GATT_CONN_TERMINATED_POWER_OFF = HCI_ERR_REMOTE_POWER_OFF,
279 
280   BTA_GATT_CONN_NONE = 0x0101, /* 0x0101 no connection to cancel  */
281 } tGATT_DISCONN_REASON;
282 
gatt_disconnection_reason_text(const tGATT_DISCONN_REASON & reason)283 inline std::string gatt_disconnection_reason_text(const tGATT_DISCONN_REASON& reason) {
284   switch (reason) {
285     CASE_RETURN_TEXT(GATT_CONN_OK);
286     CASE_RETURN_TEXT(GATT_CONN_L2C_FAILURE);
287     CASE_RETURN_TEXT(GATT_CONN_TIMEOUT);
288     CASE_RETURN_TEXT(GATT_CONN_TERMINATE_PEER_USER);
289     CASE_RETURN_TEXT(GATT_CONN_TERMINATE_LOCAL_HOST);
290     CASE_RETURN_TEXT(GATT_CONN_LMP_TIMEOUT);
291     CASE_RETURN_TEXT(GATT_CONN_FAILED_ESTABLISHMENT);
292     CASE_RETURN_TEXT(BTA_GATT_CONN_NONE);
293     CASE_RETURN_TEXT(GATT_CONN_TERMINATED_POWER_OFF);
294     default:
295       return std::format("UNKNOWN[{}]", static_cast<uint16_t>(reason));
296   }
297 }
298 
299 /* LE PHY bits */
300 constexpr uint8_t LE_PHY_1M_BIT = 0;
301 constexpr uint8_t LE_PHY_2M_BIT = 1;
302 constexpr uint8_t LE_PHY_CODED_BIT = 2;
303 
304 /* LE PHY bit mask values */
305 constexpr uint8_t LE_PHY_1M = (1 << LE_PHY_1M_BIT);
306 constexpr uint8_t LE_PHY_2M = (1 << LE_PHY_2M_BIT);
307 constexpr uint8_t LE_PHY_CODED = (1 << LE_PHY_CODED_BIT);
308 
309 /* MAX GATT MTU size
310  */
311 #ifndef GATT_MAX_MTU_SIZE
312 #define GATT_MAX_MTU_SIZE 517
313 #endif
314 
315 /* default GATT MTU size over LE link
316  */
317 #define GATT_DEF_BLE_MTU_SIZE 23
318 
319 /* invalid connection ID
320  */
321 #define GATT_INVALID_CONN_ID ((tCONN_ID)0xFFFF)
322 
323 /* GATT notification caching timer, default to be three seconds
324  */
325 #ifndef GATTC_NOTIF_TIMEOUT
326 #define GATTC_NOTIF_TIMEOUT 3
327 #endif
328 
329 /*****************************************************************************
330  * GATT Structure Definition
331  ****************************************************************************/
332 
333 /* Attribute permissions
334  */
335 #define GATT_PERM_READ (1 << 0)                              /* bit 0 */
336 #define GATT_PERM_READ_ENCRYPTED (1 << 1)                    /* bit 1 */
337 #define GATT_PERM_READ_ENC_MITM (1 << 2)                     /* bit 2 */
338 #define GATT_PERM_WRITE (1 << 4)                             /* bit 4 */
339 #define GATT_PERM_WRITE_ENCRYPTED (1 << 5)                   /* bit 5 */
340 #define GATT_PERM_WRITE_ENC_MITM (1 << 6)                    /* bit 6 */
341 #define GATT_PERM_WRITE_SIGNED (1 << 7)                      /* bit 7 */
342 #define GATT_PERM_WRITE_SIGNED_MITM (1 << 8)                 /* bit 8 */
343 #define GATT_PERM_READ_IF_ENCRYPTED_OR_DISCOVERABLE (1 << 9) /* bit 9 */
344 typedef uint16_t tGATT_PERM;
345 
346 /* the MS nibble of tGATT_PERM; key size 7=0; size 16=9 */
347 #define GATT_ENCRYPT_KEY_SIZE_MASK (0xF000)
348 
349 #define GATT_READ_ALLOWED                                                \
350   (GATT_PERM_READ | GATT_PERM_READ_ENCRYPTED | GATT_PERM_READ_ENC_MITM | \
351    GATT_PERM_READ_IF_ENCRYPTED_OR_DISCOVERABLE)
352 #define GATT_READ_AUTH_REQUIRED (GATT_PERM_READ_ENCRYPTED)
353 #define GATT_READ_MITM_REQUIRED (GATT_PERM_READ_ENC_MITM)
354 #define GATT_READ_ENCRYPTED_REQUIRED (GATT_PERM_READ_ENCRYPTED | GATT_PERM_READ_ENC_MITM)
355 
356 #define GATT_WRITE_ALLOWED                                                  \
357   (GATT_PERM_WRITE | GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE_ENC_MITM | \
358    GATT_PERM_WRITE_SIGNED | GATT_PERM_WRITE_SIGNED_MITM)
359 
360 #define GATT_WRITE_AUTH_REQUIRED (GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE_SIGNED)
361 
362 #define GATT_WRITE_MITM_REQUIRED (GATT_PERM_WRITE_ENC_MITM | GATT_PERM_WRITE_SIGNED_MITM)
363 
364 #define GATT_WRITE_ENCRYPTED_PERM (GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE_ENC_MITM)
365 
366 #define GATT_WRITE_SIGNED_PERM (GATT_PERM_WRITE_SIGNED | GATT_PERM_WRITE_SIGNED_MITM)
367 
368 /* Characteristic properties
369  */
370 #define GATT_CHAR_PROP_BIT_BROADCAST (1 << 0)
371 #define GATT_CHAR_PROP_BIT_READ (1 << 1)
372 #define GATT_CHAR_PROP_BIT_WRITE_NR (1 << 2)
373 #define GATT_CHAR_PROP_BIT_WRITE (1 << 3)
374 #define GATT_CHAR_PROP_BIT_NOTIFY (1 << 4)
375 #define GATT_CHAR_PROP_BIT_INDICATE (1 << 5)
376 #define GATT_CHAR_PROP_BIT_AUTH (1 << 6)
377 #define GATT_CHAR_PROP_BIT_EXT_PROP (1 << 7)
378 typedef uint8_t tGATT_CHAR_PROP;
379 
380 /* Format of the value of a characteristic. enumeration type
381  */
382 enum {
383   GATT_FORMAT_RES,     /* rfu */
384   GATT_FORMAT_BOOL,    /* 0x01 boolean */
385   GATT_FORMAT_2BITS,   /* 0x02 2 bit */
386   GATT_FORMAT_NIBBLE,  /* 0x03 nibble */
387   GATT_FORMAT_UINT8,   /* 0x04 uint8 */
388   GATT_FORMAT_UINT12,  /* 0x05 uint12 */
389   GATT_FORMAT_UINT16,  /* 0x06 uint16 */
390   GATT_FORMAT_UINT24,  /* 0x07 uint24 */
391   GATT_FORMAT_UINT32,  /* 0x08 uint32 */
392   GATT_FORMAT_UINT48,  /* 0x09 uint48 */
393   GATT_FORMAT_UINT64,  /* 0x0a uint64 */
394   GATT_FORMAT_UINT128, /* 0x0B uint128 */
395   GATT_FORMAT_SINT8,   /* 0x0C signed 8 bit integer */
396   GATT_FORMAT_SINT12,  /* 0x0D signed 12 bit integer */
397   GATT_FORMAT_SINT16,  /* 0x0E signed 16 bit integer */
398   GATT_FORMAT_SINT24,  /* 0x0F signed 24 bit integer */
399   GATT_FORMAT_SINT32,  /* 0x10 signed 32 bit integer */
400   GATT_FORMAT_SINT48,  /* 0x11 signed 48 bit integer */
401   GATT_FORMAT_SINT64,  /* 0x12 signed 64 bit integer */
402   GATT_FORMAT_SINT128, /* 0x13 signed 128 bit integer */
403   GATT_FORMAT_FLOAT32, /* 0x14 float 32 */
404   GATT_FORMAT_FLOAT64, /* 0x15 float 64*/
405   GATT_FORMAT_SFLOAT,  /* 0x16 IEEE-11073 16 bit SFLOAT */
406   GATT_FORMAT_FLOAT,   /* 0x17 IEEE-11073 32 bit SFLOAT */
407   GATT_FORMAT_DUINT16, /* 0x18 IEEE-20601 format */
408   GATT_FORMAT_UTF8S,   /* 0x19 UTF-8 string */
409   GATT_FORMAT_UTF16S,  /* 0x1a UTF-16 string */
410   GATT_FORMAT_STRUCT,  /* 0x1b Opaque structure*/
411   GATT_FORMAT_MAX      /* 0x1c or above reserved */
412 };
413 typedef uint8_t tGATT_FORMAT;
414 
415 /* Characteristic Presentation Format Descriptor value
416  */
417 typedef struct {
418   uint16_t unit;  /* as UUIUD defined by SIG */
419   uint16_t descr; /* as UUID as defined by SIG */
420   tGATT_FORMAT format;
421   int8_t exp;
422   uint8_t name_spc; /* The name space of the description */
423 } tGATT_CHAR_PRES;
424 
425 /* Characteristic Report reference Descriptor format
426  */
427 typedef struct {
428   uint8_t rpt_id;   /* report ID */
429   uint8_t rpt_type; /* report type */
430 } tGATT_CHAR_RPT_REF;
431 
432 #define GATT_VALID_RANGE_MAX_SIZE 16
433 typedef struct {
434   uint8_t format;
435   uint16_t len;
436   uint8_t lower_range[GATT_VALID_RANGE_MAX_SIZE]; /* in little endian format */
437   uint8_t upper_range[GATT_VALID_RANGE_MAX_SIZE];
438 } tGATT_VALID_RANGE;
439 
440 /* Characteristic Aggregate Format attribute value
441  */
442 #define GATT_AGGR_HANDLE_NUM_MAX 10
443 typedef struct {
444   uint8_t num_handle;
445   uint16_t handle_list[GATT_AGGR_HANDLE_NUM_MAX];
446 } tGATT_CHAR_AGGRE;
447 
448 /* Characteristic descriptor: Extended Properties value
449  */
450 /* permits reliable writes of the Characteristic Value */
451 #define GATT_CHAR_BIT_REL_WRITE 0x0001
452 /* permits writes to the characteristic descriptor */
453 #define GATT_CHAR_BIT_WRITE_AUX 0x0002
454 
455 /* characteristic descriptor: client configuration value
456  */
457 #define GATT_CLT_CONFIG_NONE 0x0000
458 #define GATT_CLT_CONFIG_NOTIFICATION 0x0001
459 #define GATT_CLT_CONFIG_INDICATION 0x0002
460 
461 /* characteristic descriptor: server configuration value
462  */
463 #define GATT_SVR_CONFIG_NONE 0x0000
464 #define GATT_SVR_CONFIG_BROADCAST 0x0001
465 typedef uint16_t tGATT_SVR_CHAR_CONFIG;
466 
467 /* Characteristic descriptor: Extended Properties value
468  */
469 /* permits reliable writes of the Characteristic Value */
470 #define GATT_CHAR_BIT_REL_WRITE 0x0001
471 /* permits writes to the characteristic descriptor */
472 #define GATT_CHAR_BIT_WRITE_AUX 0x0002
473 
474 /* authentication requirement
475  */
476 #define GATT_AUTH_REQ_NONE 0
477 #define GATT_AUTH_REQ_NO_MITM 1 /* unauthenticated encryption */
478 #define GATT_AUTH_REQ_MITM 2    /* authenticated encryption */
479 #define GATT_AUTH_REQ_SIGNED_NO_MITM 3
480 #define GATT_AUTH_REQ_SIGNED_MITM 4
481 typedef uint8_t tGATT_AUTH_REQ;
482 
483 /* Attribute Value structure
484  */
485 typedef struct {
486   tCONN_ID conn_id;
487   uint16_t handle;                  /* attribute handle */
488   uint16_t offset;                  /* attribute value offset, if no offset is needed for the
489                                        command, ignore it */
490   uint16_t len;                     /* length of attribute value */
491   tGATT_AUTH_REQ auth_req;          /*  authentication request */
492   uint8_t value[GATT_MAX_ATTR_LEN]; /* the actual attribute value */
493 } tGATT_VALUE;
494 
495 /* Union of the event data which is used in the server respond API to carry the
496  * server response information
497  */
498 typedef union {
499   /* data type            member          event   */
500   tGATT_VALUE attr_value; /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
501                           /* READ_BLOB, READ_BY_TYPE */
502   uint16_t handle;        /* WRITE, WRITE_BLOB */
503 } tGATTS_RSP;
504 
505 #define GATT_PREP_WRITE_CANCEL 0x00
506 #define GATT_PREP_WRITE_EXEC 0x01
507 typedef uint8_t tGATT_EXEC_FLAG;
508 
509 /* read request always based on UUID */
510 typedef struct {
511   uint16_t handle;
512   uint16_t offset;
513   bool is_long;
514   bt_gatt_db_attribute_type_t gatt_type; /* are we writing characteristic or descriptor */
515 } tGATT_READ_REQ;
516 
517 /* write request data */
518 typedef struct {
519   uint16_t handle;                       /* attribute handle */
520   uint16_t offset;                       /* attribute value offset, if no offset is needed for the
521                                             command, ignore it */
522   uint16_t len;                          /* length of attribute value */
523   uint8_t value[GATT_MAX_ATTR_LEN];      /* the actual attribute value */
524   bool need_rsp;                         /* need write response */
525   bool is_prep;                          /* is prepare write */
526   bt_gatt_db_attribute_type_t gatt_type; /* are we writing characteristic or descriptor */
527 } tGATT_WRITE_REQ;
528 
529 /* callback data for server access request from client */
530 typedef union {
531   tGATT_READ_REQ read_req; /* read request, read by Type, read blob */
532 
533   tGATT_WRITE_REQ write_req;  /* write */
534                               /* prepare write */
535                               /* write blob */
536   uint16_t handle;            /* handle value confirmation */
537   uint16_t mtu;               /* MTU exchange request */
538   tGATT_EXEC_FLAG exec_write; /* execute write */
539 } tGATTS_DATA;
540 
541 typedef uint8_t tGATT_SERV_IF; /* GATT Service Interface */
542 
543 enum {
544   GATTS_REQ_TYPE_READ_CHARACTERISTIC = 1, /* Char read request */
545   GATTS_REQ_TYPE_READ_DESCRIPTOR,         /* Desc read request */
546   GATTS_REQ_TYPE_WRITE_CHARACTERISTIC,    /* Char write request */
547   GATTS_REQ_TYPE_WRITE_DESCRIPTOR,        /* Desc write request */
548   GATTS_REQ_TYPE_WRITE_EXEC,              /* Execute write */
549   GATTS_REQ_TYPE_MTU,                     /* MTU exchange information */
550   GATTS_REQ_TYPE_CONF                     /* handle value confirmation */
551 };
552 typedef uint8_t tGATTS_REQ_TYPE;
553 
554 /* Client Used Data Structure
555  */
556 /* definition of different discovery types */
557 typedef enum : uint8_t {
558   GATT_DISC_SRVC_ALL = 1, /* discover all services */
559   GATT_DISC_SRVC_BY_UUID, /* discover service of a special type */
560   GATT_DISC_INC_SRVC,     /* discover the included service within a service */
561   GATT_DISC_CHAR,         /* discover characteristics of a service with/without type
562                              requirement */
563   GATT_DISC_CHAR_DSCPT,   /* discover characteristic descriptors of a character */
564   GATT_DISC_MAX           /* maximum discover type */
565 } tGATT_DISC_TYPE;
566 
567 /* GATT read type enumeration
568  */
569 enum {
570   GATT_READ_BY_TYPE = 1,
571   GATT_READ_BY_HANDLE,
572   GATT_READ_MULTIPLE,
573   GATT_READ_MULTIPLE_VAR_LEN,
574   GATT_READ_CHAR_VALUE,
575   GATT_READ_PARTIAL,
576   GATT_READ_MAX
577 };
578 typedef uint8_t tGATT_READ_TYPE;
579 
580 /* Read By Type Request (GATT_READ_BY_TYPE) Data
581  */
582 typedef struct {
583   tGATT_AUTH_REQ auth_req;
584   uint16_t s_handle;
585   uint16_t e_handle;
586   bluetooth::Uuid uuid;
587 } tGATT_READ_BY_TYPE;
588 
589 /*   GATT_READ_MULTIPLE request data
590  */
591 #define GATT_MAX_READ_MULTI_HANDLES 10 /* Max attributes to read in one request */
592 typedef struct {
593   tGATT_AUTH_REQ auth_req;
594   uint16_t num_handles;                          /* number of handles to read */
595   uint16_t handles[GATT_MAX_READ_MULTI_HANDLES]; /* handles list to be read */
596   bool variable_len;
597 } tGATT_READ_MULTI;
598 
599 /*   Read By Handle Request (GATT_READ_BY_HANDLE) data */
600 typedef struct {
601   tGATT_AUTH_REQ auth_req;
602   uint16_t handle;
603 } tGATT_READ_BY_HANDLE;
604 
605 /*   READ_BT_HANDLE_Request data */
606 typedef struct {
607   tGATT_AUTH_REQ auth_req;
608   uint16_t handle;
609   uint16_t offset;
610 } tGATT_READ_PARTIAL;
611 
612 /* Read Request Data
613  */
614 typedef union {
615   tGATT_READ_BY_TYPE service;
616   tGATT_READ_BY_TYPE char_type; /* characteristic type */
617   tGATT_READ_MULTI read_multiple;
618   tGATT_READ_BY_HANDLE by_handle;
619   tGATT_READ_PARTIAL partial;
620 } tGATT_READ_PARAM;
621 
622 /* GATT write type enumeration */
623 enum { GATT_WRITE_NO_RSP = 1, GATT_WRITE, GATT_WRITE_PREPARE };
624 typedef uint8_t tGATT_WRITE_TYPE;
625 
626 /* Client Operation Complete Callback Data
627  */
628 typedef union {
629   tGATT_VALUE att_value;
630   uint16_t mtu;
631   uint16_t handle;
632   uint16_t cid;
633 } tGATT_CL_COMPLETE;
634 
635 /* GATT client operation type, used in client callback function
636  */
637 typedef enum : uint8_t {
638   GATTC_OPTYPE_NONE = 0,
639   GATTC_OPTYPE_DISCOVERY = 1,
640   GATTC_OPTYPE_READ = 2,
641   GATTC_OPTYPE_WRITE = 3,
642   GATTC_OPTYPE_EXE_WRITE = 4,
643   GATTC_OPTYPE_CONFIG = 5,
644   GATTC_OPTYPE_NOTIFICATION = 6,
645   GATTC_OPTYPE_INDICATION = 7,
646 } tGATTC_OPTYPE;
647 
648 /* characteristic declaration
649  */
650 typedef struct {
651   tGATT_CHAR_PROP char_prop; /* characteristic properties */
652   uint16_t val_handle;       /* characteristic value attribute handle */
653   bluetooth::Uuid char_uuid; /* characteristic UUID type */
654 } tGATT_CHAR_DCLR_VAL;
655 
656 /* primary service group data
657  */
658 typedef struct {
659   uint16_t e_handle;            /* ending handle of the group */
660   bluetooth::Uuid service_type; /* group type */
661 } tGATT_GROUP_VALUE;
662 
663 /* included service attribute value
664  */
665 typedef struct {
666   bluetooth::Uuid service_type; /* included service UUID */
667   uint16_t s_handle;            /* starting handle */
668   uint16_t e_handle;            /* ending handle */
669 } tGATT_INCL_SRVC;
670 
671 typedef union {
672   tGATT_INCL_SRVC incl_service;  /* include service value */
673   tGATT_GROUP_VALUE group_value; /* Service UUID type.
674                                     This field is used with GATT_DISC_SRVC_ALL
675                                     or GATT_DISC_SRVC_BY_UUID
676                                     type of discovery result callback. */
677 
678   uint16_t handle; /* When used with GATT_DISC_INC_SRVC type discovery result,
679                       it is the included service starting handle.*/
680 
681   tGATT_CHAR_DCLR_VAL dclr_value; /* Characteristic declaration value.
682                                      This field is used with GATT_DISC_CHAR type discovery.*/
683 } tGATT_DISC_VALUE;
684 
685 /* discover result record
686  */
687 typedef struct {
688   bluetooth::Uuid type;
689   uint16_t handle;
690   tGATT_DISC_VALUE value;
691 } tGATT_DISC_RES;
692 
693 #define GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP  \
694   1 /* start a idle timer for this duration \
695      when no application need to use the link */
696 
697 #define GATT_LINK_NO_IDLE_TIMEOUT 0xFFFF
698 
699 #define GATT_INVALID_ACL_HANDLE 0xFFFF
700 /* discover result callback function */
701 typedef void(tGATT_DISC_RES_CB)(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
702                                 tGATT_DISC_RES* p_data);
703 
704 /* discover complete callback function */
705 typedef void(tGATT_DISC_CMPL_CB)(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
706 
707 /* Define a callback function for when read/write/disc/config operation is
708  * completed. */
709 typedef void(tGATT_CMPL_CBACK)(tCONN_ID conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
710                                tGATT_CL_COMPLETE* p_data);
711 
712 /* Define a callback function when an initialized connection is established. */
713 typedef void(tGATT_CONN_CBACK)(tGATT_IF gatt_if, const RawAddress& bda, tCONN_ID conn_id,
714                                bool connected, tGATT_DISCONN_REASON reason,
715                                tBT_TRANSPORT transport);
716 
717 /* attribute request callback for ATT server */
718 typedef void(tGATT_REQ_CBACK)(tCONN_ID conn_id, uint32_t trans_id, tGATTS_REQ_TYPE type,
719                               tGATTS_DATA* p_data);
720 
721 /* channel congestion/uncongestion callback */
722 typedef void(tGATT_CONGESTION_CBACK)(tCONN_ID conn_id, bool congested);
723 
724 /* Define a callback function when encryption is established. */
725 typedef void(tGATT_ENC_CMPL_CB)(tGATT_IF gatt_if, const RawAddress& bda);
726 
727 /* Define a callback function when phy is updated. */
728 typedef void(tGATT_PHY_UPDATE_CB)(tGATT_IF gatt_if, tCONN_ID conn_id, uint8_t tx_phy,
729                                   uint8_t rx_phy, tGATT_STATUS status);
730 
731 /* Define a callback function when connection parameters are updated */
732 typedef void(tGATT_CONN_UPDATE_CB)(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t interval,
733                                    uint16_t latency, uint16_t timeout, tGATT_STATUS status);
734 
735 /* Define a callback function when subrate change event is received */
736 typedef void(tGATT_SUBRATE_CHG_CB)(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t subrate_factor,
737                                    uint16_t latency, uint16_t cont_num, uint16_t timeout,
738                                    tGATT_STATUS status);
739 
740 /* Define the structure that applications use to register with
741  * GATT. This structure includes callback functions. All functions
742  * MUST be provided.
743  */
744 typedef struct {
745   tGATT_CONN_CBACK* p_conn_cb{nullptr};
746   tGATT_CMPL_CBACK* p_cmpl_cb{nullptr};
747   tGATT_DISC_RES_CB* p_disc_res_cb{nullptr};
748   tGATT_DISC_CMPL_CB* p_disc_cmpl_cb{nullptr};
749   tGATT_REQ_CBACK* p_req_cb{nullptr};
750   tGATT_ENC_CMPL_CB* p_enc_cmpl_cb{nullptr};
751   tGATT_CONGESTION_CBACK* p_congestion_cb{nullptr};
752   tGATT_PHY_UPDATE_CB* p_phy_update_cb{nullptr};
753   tGATT_CONN_UPDATE_CB* p_conn_update_cb{nullptr};
754   tGATT_SUBRATE_CHG_CB* p_subrate_chg_cb{nullptr};
755 } tGATT_CBACK;
756 
757 /*****************  Start Handle Management Definitions   *********************/
758 
759 typedef struct {
760   bluetooth::Uuid app_uuid128;
761   bluetooth::Uuid svc_uuid;
762   uint16_t s_handle;
763   uint16_t e_handle;
764   bool is_primary; /* primary service or secondary */
765 } tGATTS_HNDL_RANGE;
766 
767 #define GATTS_SRV_CHG_CMD_ADD_CLIENT 1
768 #define GATTS_SRV_CHG_CMD_UPDATE_CLIENT 2
769 #define GATTS_SRV_CHG_CMD_REMOVE_CLIENT 3
770 #define GATTS_SRV_CHG_CMD_READ_NUM_CLENTS 4
771 #define GATTS_SRV_CHG_CMD_READ_CLENT 5
772 typedef uint8_t tGATTS_SRV_CHG_CMD;
773 
774 typedef struct {
775   RawAddress bda;
776   bool srv_changed;
777 } tGATTS_SRV_CHG;
778 
779 typedef union {
780   tGATTS_SRV_CHG srv_chg;
781   uint8_t client_read_index; /* only used for sequential reading client srv chg
782                                 info */
783 } tGATTS_SRV_CHG_REQ;
784 
785 typedef union {
786   tGATTS_SRV_CHG srv_chg;
787   uint8_t num_clients;
788 } tGATTS_SRV_CHG_RSP;
789 
790 /* Attribute server handle ranges NV storage callback functions
791  */
792 typedef void(tGATTS_NV_SAVE_CBACK)(bool is_saved, tGATTS_HNDL_RANGE* p_hndl_range);
793 typedef bool(tGATTS_NV_SRV_CHG_CBACK)(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ* p_req,
794                                       tGATTS_SRV_CHG_RSP* p_rsp);
795 
796 typedef struct {
797   tGATTS_NV_SAVE_CBACK* p_nv_save_callback;
798   tGATTS_NV_SRV_CHG_CBACK* p_srv_chg_callback;
799 } tGATT_APPL_INFO;
800 
801 /********************  End Handle Management Definitions   ********************/
802 
803 /*******************************************************************************
804  *  External Function Declarations
805  ******************************************************************************/
806 
807 /******************************************************************************/
808 /* GATT Profile API Functions */
809 /******************************************************************************/
810 /* GATT Profile Server Functions */
811 /******************************************************************************/
812 
813 /*******************************************************************************
814  *
815  * Function         GATTS_NVRegister
816  *
817  * Description      Application manager calls this function to register for
818  *                  NV save callback function.  There can be one and only one
819  *                  NV save callback function.
820  *
821  * Parameter        p_cb_info : callback information
822  *
823  * Returns          true if registered OK, else false
824  *
825  ******************************************************************************/
826 [[nodiscard]] bool GATTS_NVRegister(tGATT_APPL_INFO* p_cb_info);
827 
828 /*******************************************************************************
829  *
830  * Function         BTA_GATTS_AddService
831  *
832  * Description      Add a service. When service is ready, a callback
833  *                  event BTA_GATTS_ADD_SRVC_EVT is called to report status
834  *                  and handles to the profile.
835  *
836  * Parameters       server_if: server interface.
837  *                  service: pointer array describing service.
838  *                  count: number of elements in service array.
839  *
840  * Returns          on success GATT_SERVICE_STARTED is returned, and
841  *                  attribute_handle field inside service elements are filled.
842  *                  on error error status is returned.
843  *
844  ******************************************************************************/
845 [[nodiscard]] tGATT_STATUS GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service,
846                                             int count);
847 
848 /*******************************************************************************
849  *
850  * Function         GATTS_DeleteService
851  *
852  * Description      This function is called to delete a service.
853  *
854  * Parameter        gatt_if       : application interface
855  *                  p_svc_uuid    : service UUID
856  *                  svc_inst      : instance of the service inside the
857  *                                  application
858  *
859  * Returns          true if operation succeed, else false
860  *
861  ******************************************************************************/
862 [[nodiscard]] bool GATTS_DeleteService(tGATT_IF gatt_if, bluetooth::Uuid* p_svc_uuid,
863                                        uint16_t svc_inst);
864 
865 /*******************************************************************************
866  *
867  * Function         GATTS_StopService
868  *
869  * Description      This function is called to stop a service
870  *
871  * Parameter         service_handle : this is the start handle of a service
872  *
873  * Returns          None.
874  *
875  ******************************************************************************/
876 void GATTS_StopService(uint16_t service_handle);
877 
878 /*******************************************************************************
879  *
880  * Function         GATTs_HandleValueIndication
881  *
882  * Description      This function sends a handle value indication to a client.
883  *
884  * Parameter        conn_id: connection identifier.
885  *                  attr_handle: Attribute handle of this handle value
886  *                               indication.
887  *                  val_len: Length of the indicated attribute value.
888  *                  p_val: Pointer to the indicated attribute value data.
889  *
890  * Returns          GATT_SUCCESS if successfully sent or queued; otherwise error
891  *                               code.
892  *
893  ******************************************************************************/
894 [[nodiscard]] tGATT_STATUS GATTS_HandleValueIndication(tCONN_ID conn_id, uint16_t attr_handle,
895                                                        uint16_t val_len, uint8_t* p_val);
896 
897 /*******************************************************************************
898  *
899  * Function         GATTS_HandleValueNotification
900  *
901  * Description      This function sends a handle value notification to a client.
902  *
903  * Parameter       conn_id: connection identifier.
904  *                  attr_handle: Attribute handle of this handle value
905  *                               indication.
906  *                  val_len: Length of the indicated attribute value.
907  *                  p_val: Pointer to the indicated attribute value data.
908  *
909  * Returns          GATT_SUCCESS if successfully sent; otherwise error code.
910  *
911  ******************************************************************************/
912 [[nodiscard]] tGATT_STATUS GATTS_HandleValueNotification(tCONN_ID conn_id, uint16_t attr_handle,
913                                                          uint16_t val_len, uint8_t* p_val);
914 
915 /*******************************************************************************
916  *
917  * Function         GATTS_SendRsp
918  *
919  * Description      This function sends the server response to client.
920  *
921  * Parameter        conn_id: connection identifier.
922  *                  trans_id: transaction id
923  *                  status: response status
924  *                  p_msg: pointer to message parameters structure.
925  *
926  * Returns          GATT_SUCCESS if successfully sent; otherwise error code.
927  *
928  ******************************************************************************/
929 [[nodiscard]] tGATT_STATUS GATTS_SendRsp(tCONN_ID conn_id, uint32_t trans_id, tGATT_STATUS status,
930                                          tGATTS_RSP* p_msg);
931 
932 /******************************************************************************/
933 /* GATT Profile Client Functions */
934 /******************************************************************************/
935 
936 /*******************************************************************************
937  *
938  * Function         GATTC_ConfigureMTU
939  *
940  * Description      This function is called to configure the ATT MTU size for
941  *                  a connection on an LE transport.
942  *
943  * Parameters       conn_id: connection identifier.
944  *                  mtu    - attribute MTU size..
945  *
946  * Returns          GATT_SUCCESS if command started successfully.
947  *
948  ******************************************************************************/
949 [[nodiscard]] tGATT_STATUS GATTC_ConfigureMTU(tCONN_ID conn_id, uint16_t mtu);
950 
951 /*******************************************************************************
952  * Function         GATTC_UpdateUserAttMtuIfNeeded
953  *
954  * Description      This function to be called when user requested MTU after
955  *                  MTU Exchange has been already done. This will update data
956  *                  length in the controller.
957  *
958  * Parameters        remote_bda : peer device address. (input)
959  *                   transport  : physical transport of the GATT connection
960  *                                 (BR/EDR or LE) (input)
961  *                   user_mtu: user request mtu
962  *
963  ******************************************************************************/
964 void GATTC_UpdateUserAttMtuIfNeeded(const RawAddress& remote_bda, tBT_TRANSPORT transport,
965                                     uint16_t user_mtu);
966 
967 /******************************************************************************
968  *
969  * Function         GATTC_TryMtuRequest
970  *
971  * Description      This function shall be called before calling
972  *                  GATTC_ConfigureMTU in order to check if operation is
973  *                  available to do.
974  *
975  * Parameters        remote_bda : peer device address. (input)
976  *                   transport  : physical transport of the GATT connection
977  *                                 (BR/EDR or LE) (input)
978  *                   conn_id    : connection id  (input)
979  *                   current_mtu: current mtu on the link (output)
980  *
981  * Returns          tGATTC_TryMtuRequestResult:
982  *                  - MTU_EXCHANGE_NOT_DONE_YET: There was no MTU Exchange
983  *                      procedure on the link. User can call GATTC_ConfigureMTU
984  *                      now.
985  *                  - MTU_EXCHANGE_NOT_ALLOWED : Not allowed for BR/EDR or if
986  *                      link does not exist
987  *                  - MTU_EXCHANGE_ALREADY_DONE: MTU Exchange is done. MTU
988  *                      should be taken from current_mtu
989  *                  - MTU_EXCHANGE_IN_PROGRESS : Other use is doing MTU
990  *                      Exchange. Conn_id is stored for result.
991  *
992  ******************************************************************************/
993 [[nodiscard]] tGATTC_TryMtuRequestResult GATTC_TryMtuRequest(const RawAddress& remote_bda,
994                                                              tBT_TRANSPORT transport,
995                                                              tCONN_ID conn_id,
996                                                              uint16_t* current_mtu);
997 
998 [[nodiscard]] std::list<tCONN_ID> GATTC_GetAndRemoveListOfConnIdsWaitingForMtuRequest(
999         const RawAddress& remote_bda);
1000 /*******************************************************************************
1001  *
1002  * Function         GATTC_Discover
1003  *
1004  * Description      This function is called to do a discovery procedure on ATT
1005  *                  server.
1006  *
1007  * Parameters       conn_id: connection identifier.
1008  *                  disc_type:discovery type.
1009  *                  start_handle and end_handle: range of handles for discovery
1010  *                  uuid: uuid to discovery. set to Uuid::kEmpty for requests
1011  *                        that don't need it
1012  *
1013  * Returns          GATT_SUCCESS if command received/sent successfully.
1014  *
1015  ******************************************************************************/
1016 [[nodiscard]] tGATT_STATUS GATTC_Discover(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
1017                                           uint16_t start_handle, uint16_t end_handle,
1018                                           const bluetooth::Uuid& uuid);
1019 [[nodiscard]] tGATT_STATUS GATTC_Discover(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
1020                                           uint16_t start_handle, uint16_t end_handle);
1021 
1022 /*******************************************************************************
1023  *
1024  * Function         GATTC_Read
1025  *
1026  * Description      This function is called to read the value of an attribute
1027  *                  from the server.
1028  *
1029  * Parameters       conn_id: connection identifier.
1030  *                  type    - attribute read type.
1031  *                  p_read  - read operation parameters.
1032  *
1033  * Returns          GATT_SUCCESS if command started successfully.
1034  *
1035  ******************************************************************************/
1036 [[nodiscard]] tGATT_STATUS GATTC_Read(tCONN_ID conn_id, tGATT_READ_TYPE type,
1037                                       tGATT_READ_PARAM* p_read);
1038 
1039 /*******************************************************************************
1040  *
1041  * Function         GATTC_Write
1042  *
1043  * Description      This function is called to read the value of an attribute
1044  *                  from the server.
1045  *
1046  * Parameters       conn_id: connection identifier.
1047  *                  type    - attribute write type.
1048  *                  p_write  - write operation parameters.
1049  *
1050  * Returns          GATT_SUCCESS if command started successfully.
1051  *
1052  ******************************************************************************/
1053 [[nodiscard]] tGATT_STATUS GATTC_Write(tCONN_ID conn_id, tGATT_WRITE_TYPE type,
1054                                        tGATT_VALUE* p_write);
1055 
1056 /*******************************************************************************
1057  *
1058  * Function         GATTC_ExecuteWrite
1059  *
1060  * Description      This function is called to send an Execute write request to
1061  *                  the server.
1062  *
1063  * Parameters       conn_id: connection identifier.
1064  *                  is_execute - to execute or cancel the prepare write
1065  *                               request(s)
1066  *
1067  * Returns          GATT_SUCCESS if command started successfully.
1068  *
1069  ******************************************************************************/
1070 [[nodiscard]] tGATT_STATUS GATTC_ExecuteWrite(tCONN_ID conn_id, bool is_execute);
1071 
1072 /*******************************************************************************
1073  *
1074  * Function         GATTC_SendHandleValueConfirm
1075  *
1076  * Description      This function is called to send a handle value confirmation
1077  *                  as response to a handle value notification from server.
1078  *
1079  * Parameters       conn_id: connection identifier.
1080  *                  handle: the handle of the attribute confirmation.
1081  *
1082  * Returns          GATT_SUCCESS if command started successfully.
1083  *
1084  ******************************************************************************/
1085 [[nodiscard]] tGATT_STATUS GATTC_SendHandleValueConfirm(tCONN_ID conn_id, uint16_t handle);
1086 
1087 /*******************************************************************************
1088  *
1089  * Function         GATT_SetIdleTimeout
1090  *
1091  * Description      This function (common to both client and server) sets the
1092  *                  idle timeout for a transport connection
1093  *
1094  * Parameter        bd_addr:   target device bd address.
1095  *                  idle_tout: timeout value in seconds.
1096  *                  transport: transport option.
1097  *                  is_active: whether we should use this as a signal that an
1098  *                             active client now exists (which changes link
1099  *                             timeout logic, see
1100  *                             t_l2c_linkcb.with_active_local_clients for
1101  *                             details).
1102  *
1103  * Returns          void
1104  *
1105  ******************************************************************************/
1106 void GATT_SetIdleTimeout(const RawAddress& bd_addr, uint16_t idle_tout, tBT_TRANSPORT transport,
1107                          bool is_active);
1108 
1109 /*******************************************************************************
1110  *
1111  * Function         GATT_Register
1112  *
1113  * Description      This function is called to register an  application
1114  *                  with GATT
1115  *
1116  * Parameter        p_app_uuid128: Application UUID
1117  *                  p_cb_info: callback functions.
1118  *                  eatt_support: set support for eatt
1119  *
1120  * Returns          0 for error, otherwise the index of the client registered
1121  *                  with GATT
1122  *
1123  ******************************************************************************/
1124 [[nodiscard]] tGATT_IF GATT_Register(const bluetooth::Uuid& p_app_uuid128, const std::string& name,
1125                                      tGATT_CBACK* p_cb_info, bool eatt_support);
1126 
1127 /*******************************************************************************
1128  *
1129  * Function         GATT_Deregister
1130  *
1131  * Description      This function deregistered the application from GATT.
1132  *
1133  * Parameters       gatt_if: application interface.
1134  *
1135  * Returns          None.
1136  *
1137  ******************************************************************************/
1138 void GATT_Deregister(tGATT_IF gatt_if);
1139 
1140 /*******************************************************************************
1141  *
1142  * Function         GATT_StartIf
1143  *
1144  * Description      This function is called after registration to start
1145  *                  receiving callbacks for registered interface.  Function may
1146  *                  call back with connection status and queued notifications
1147  *
1148  * Parameter        gatt_if: application interface.
1149  *
1150  * Returns          None
1151  *
1152  ******************************************************************************/
1153 void GATT_StartIf(tGATT_IF gatt_if);
1154 
1155 /*******************************************************************************
1156  *
1157  * Function         GATT_Connect
1158  *
1159  * Description      This function initiate a connection to a remote device on
1160  *                  GATT channel.
1161  *
1162  * Parameters       gatt_if: application interface
1163  *                  bd_addr: peer device address
1164  *                  addr_type: peer device address type
1165  *                  connection_type: connection type
1166  *                  transport : Physical transport for GATT connection
1167  *                              (BR/EDR or LE)
1168  *                  opportunistic: will not keep device connected if other apps
1169  *                      disconnect, will not update connected apps counter, when
1170  *                      disconnected won't cause physical disconnection.
1171  *
1172  * Returns          true if connection started; else false
1173  *
1174  ******************************************************************************/
1175 [[nodiscard]] bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr,
1176                                 tBLE_ADDR_TYPE addr_type, tBTM_BLE_CONN_TYPE connection_type,
1177                                 tBT_TRANSPORT transport, bool opportunistic,
1178                                 uint8_t initiating_phys, uint16_t preferred_transport);
1179 [[nodiscard]] bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr,
1180                                 tBTM_BLE_CONN_TYPE connection_type, tBT_TRANSPORT transport,
1181                                 bool opportunistic);
1182 
1183 /*******************************************************************************
1184  *
1185  * Function         GATT_CancelConnect
1186  *
1187  * Description      Terminate the connection initiation to a remote device on a
1188  *                  GATT channel.
1189  *
1190  * Parameters       gatt_if: client interface. If 0 used as unconditionally
1191  *                           disconnect, typically used for direct connection
1192  *                           cancellation.
1193  *                  bd_addr: peer device address.
1194  *                  is_direct: is a direct connection or a background auto
1195  *                             connection
1196  *
1197  * Returns          true if connection started; else false
1198  *
1199  ******************************************************************************/
1200 [[nodiscard]] bool GATT_CancelConnect(tGATT_IF gatt_if, const RawAddress& bd_addr, bool is_direct);
1201 
1202 /*******************************************************************************
1203  *
1204  * Function         GATT_Disconnect
1205  *
1206  * Description      Disconnect the GATT channel for this registered application.
1207  *
1208  * Parameters       conn_id: connection identifier.
1209  *
1210  * Returns          GATT_SUCCESS if disconnected.
1211  *
1212  ******************************************************************************/
1213 [[nodiscard]] tGATT_STATUS GATT_Disconnect(tCONN_ID conn_id);
1214 
1215 /*******************************************************************************
1216  *
1217  * Function         GATT_GetConnectionInfor
1218  *
1219  * Description      Use conn_id to find its associated BD address and
1220  *                  application interface
1221  *
1222  * Parameters        conn_id: connection id  (input)
1223  *                   p_gatt_if: application interface (output)
1224  *                   bd_addr: peer device address. (output)
1225  *                   transport : physical transport of the GATT connection
1226  *                                (BR/EDR or LE)
1227  *
1228  * Returns          true the logical link information is found for conn_id
1229  *
1230  ******************************************************************************/
1231 [[nodiscard]] bool GATT_GetConnectionInfor(tCONN_ID conn_id, tGATT_IF* p_gatt_if,
1232                                            RawAddress& bd_addr, tBT_TRANSPORT* p_transport);
1233 
1234 /*******************************************************************************
1235  *
1236  * Function         GATT_GetConnIdIfConnected
1237  *
1238  * Description      Find the conn_id if the logical link for a BD address
1239  *                  and application interface is connected
1240  *
1241  * Parameters        gatt_if: application interface (input)
1242  *                   bd_addr: peer device address. (input)
1243  *                   p_conn_id: connection id  (output)
1244  *                   transport :  physical transport of the GATT connection
1245  *                               (BR/EDR or LE)
1246  *
1247  * Returns          true the logical link is connected
1248  *
1249  ******************************************************************************/
1250 [[nodiscard]] bool GATT_GetConnIdIfConnected(tGATT_IF gatt_if, const RawAddress& bd_addr,
1251                                              tCONN_ID* p_conn_id, tBT_TRANSPORT transport);
1252 
1253 /*******************************************************************************
1254  *
1255  * Function         GATT_ConfigServiceChangeCCC
1256  *
1257  * Description      Configure service change indication on remote device
1258  *
1259  * Returns          None.
1260  *
1261  ******************************************************************************/
1262 void GATT_ConfigServiceChangeCCC(const RawAddress& remote_bda, bool enable,
1263                                  tBT_TRANSPORT transport);
1264 
1265 // Enables the GATT profile on the device.
1266 // It clears out the control blocks, and registers with L2CAP.
1267 void gatt_init(void);
1268 
1269 // Frees resources used by the GATT profile.
1270 void gatt_free(void);
1271 
1272 void gatt_consolidate(const RawAddress& identity_addr, const RawAddress& rpa);
1273 void gatt_notify_conn_update(const RawAddress& remote, uint16_t interval, uint16_t latency,
1274                              uint16_t timeout, tHCI_STATUS status);
1275 
1276 // Link encryption complete notification for all encryption process
1277 // initiated outside GATT.
1278 void gatt_notify_enc_cmpl(const RawAddress& bd_addr);
1279 
1280 /** Reset bg device list. If called after controller reset, set |after_reset| to
1281  * true, as there is no need to wipe controller acceptlist in this case. */
1282 void gatt_reset_bgdev_list(bool after_reset);
1283 
1284 // Initialize GATTS list of bonded device service change updates.
1285 void gatt_load_bonded(void);
1286 
1287 void gatt_tcb_dump(int fd);
1288 
1289 namespace std {
1290 template <>
1291 struct formatter<GattStatus> : enum_formatter<GattStatus> {};
1292 template <>
1293 struct formatter<tGATT_DISCONN_REASON> : enum_formatter<tGATT_DISCONN_REASON> {};
1294 template <>
1295 struct formatter<tGATTC_OPTYPE> : enum_formatter<tGATTC_OPTYPE> {};
1296 template <>
1297 struct formatter<tGATT_OP_CODE> : enum_formatter<tGATT_OP_CODE> {};
1298 template <>
1299 struct formatter<tGATT_DISC_TYPE> : enum_formatter<tGATT_DISC_TYPE> {};
1300 }  // namespace std
1301 
1302 #endif /* GATT_API_H */
1303