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