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