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 } tGATT_DISCONN_REASON;
283
gatt_disconnection_reason_text(const tGATT_DISCONN_REASON & reason)284 inline std::string gatt_disconnection_reason_text(const tGATT_DISCONN_REASON& reason) {
285 switch (reason) {
286 CASE_RETURN_TEXT(GATT_CONN_OK);
287 CASE_RETURN_TEXT(GATT_CONN_L2C_FAILURE);
288 CASE_RETURN_TEXT(GATT_CONN_TIMEOUT);
289 CASE_RETURN_TEXT(GATT_CONN_TERMINATE_PEER_USER);
290 CASE_RETURN_TEXT(GATT_CONN_TERMINATE_LOCAL_HOST);
291 CASE_RETURN_TEXT(GATT_CONN_LMP_TIMEOUT);
292 CASE_RETURN_TEXT(GATT_CONN_FAILED_ESTABLISHMENT);
293 CASE_RETURN_TEXT(BTA_GATT_CONN_NONE);
294 CASE_RETURN_TEXT(GATT_CONN_TERMINATED_POWER_OFF);
295 default:
296 return base::StringPrintf("UNKNOWN[%hu]", reason);
297 }
298 }
299
300 /* LE PHY bits */
301 constexpr uint8_t LE_PHY_1M_BIT = 0;
302 constexpr uint8_t LE_PHY_2M_BIT = 1;
303 constexpr uint8_t LE_PHY_CODED_BIT = 2;
304
305 /* LE PHY bit mask values */
306 constexpr uint8_t LE_PHY_1M = (1 << LE_PHY_1M_BIT);
307 constexpr uint8_t LE_PHY_2M = (1 << LE_PHY_2M_BIT);
308 constexpr uint8_t LE_PHY_CODED = (1 << LE_PHY_CODED_BIT);
309
310 /* MAX GATT MTU size
311 */
312 #ifndef GATT_MAX_MTU_SIZE
313 #define GATT_MAX_MTU_SIZE 517
314 #endif
315
316 /* default GATT MTU size over LE link
317 */
318 #define GATT_DEF_BLE_MTU_SIZE 23
319
320 /* invalid connection ID
321 */
322 #define GATT_INVALID_CONN_ID ((tCONN_ID)0xFFFF)
323
324 /* GATT notification caching timer, default to be three seconds
325 */
326 #ifndef GATTC_NOTIF_TIMEOUT
327 #define GATTC_NOTIF_TIMEOUT 3
328 #endif
329
330 /*****************************************************************************
331 * GATT Structure Definition
332 ****************************************************************************/
333
334 /* Attribute permissions
335 */
336 #define GATT_PERM_READ (1 << 0) /* bit 0 */
337 #define GATT_PERM_READ_ENCRYPTED (1 << 1) /* bit 1 */
338 #define GATT_PERM_READ_ENC_MITM (1 << 2) /* bit 2 */
339 #define GATT_PERM_WRITE (1 << 4) /* bit 4 */
340 #define GATT_PERM_WRITE_ENCRYPTED (1 << 5) /* bit 5 */
341 #define GATT_PERM_WRITE_ENC_MITM (1 << 6) /* bit 6 */
342 #define GATT_PERM_WRITE_SIGNED (1 << 7) /* bit 7 */
343 #define GATT_PERM_WRITE_SIGNED_MITM (1 << 8) /* bit 8 */
344 #define GATT_PERM_READ_IF_ENCRYPTED_OR_DISCOVERABLE (1 << 9) /* bit 9 */
345 typedef uint16_t tGATT_PERM;
346
347 /* the MS nibble of tGATT_PERM; key size 7=0; size 16=9 */
348 #define GATT_ENCRYPT_KEY_SIZE_MASK (0xF000)
349
350 #define GATT_READ_ALLOWED \
351 (GATT_PERM_READ | GATT_PERM_READ_ENCRYPTED | GATT_PERM_READ_ENC_MITM | \
352 GATT_PERM_READ_IF_ENCRYPTED_OR_DISCOVERABLE)
353 #define GATT_READ_AUTH_REQUIRED (GATT_PERM_READ_ENCRYPTED)
354 #define GATT_READ_MITM_REQUIRED (GATT_PERM_READ_ENC_MITM)
355 #define GATT_READ_ENCRYPTED_REQUIRED (GATT_PERM_READ_ENCRYPTED | GATT_PERM_READ_ENC_MITM)
356
357 #define GATT_WRITE_ALLOWED \
358 (GATT_PERM_WRITE | GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE_ENC_MITM | \
359 GATT_PERM_WRITE_SIGNED | GATT_PERM_WRITE_SIGNED_MITM)
360
361 #define GATT_WRITE_AUTH_REQUIRED (GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE_SIGNED)
362
363 #define GATT_WRITE_MITM_REQUIRED (GATT_PERM_WRITE_ENC_MITM | GATT_PERM_WRITE_SIGNED_MITM)
364
365 #define GATT_WRITE_ENCRYPTED_PERM (GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE_ENC_MITM)
366
367 #define GATT_WRITE_SIGNED_PERM (GATT_PERM_WRITE_SIGNED | GATT_PERM_WRITE_SIGNED_MITM)
368
369 /* Characteristic properties
370 */
371 #define GATT_CHAR_PROP_BIT_BROADCAST (1 << 0)
372 #define GATT_CHAR_PROP_BIT_READ (1 << 1)
373 #define GATT_CHAR_PROP_BIT_WRITE_NR (1 << 2)
374 #define GATT_CHAR_PROP_BIT_WRITE (1 << 3)
375 #define GATT_CHAR_PROP_BIT_NOTIFY (1 << 4)
376 #define GATT_CHAR_PROP_BIT_INDICATE (1 << 5)
377 #define GATT_CHAR_PROP_BIT_AUTH (1 << 6)
378 #define GATT_CHAR_PROP_BIT_EXT_PROP (1 << 7)
379 typedef uint8_t tGATT_CHAR_PROP;
380
381 /* Format of the value of a characteristic. enumeration type
382 */
383 enum {
384 GATT_FORMAT_RES, /* rfu */
385 GATT_FORMAT_BOOL, /* 0x01 boolean */
386 GATT_FORMAT_2BITS, /* 0x02 2 bit */
387 GATT_FORMAT_NIBBLE, /* 0x03 nibble */
388 GATT_FORMAT_UINT8, /* 0x04 uint8 */
389 GATT_FORMAT_UINT12, /* 0x05 uint12 */
390 GATT_FORMAT_UINT16, /* 0x06 uint16 */
391 GATT_FORMAT_UINT24, /* 0x07 uint24 */
392 GATT_FORMAT_UINT32, /* 0x08 uint32 */
393 GATT_FORMAT_UINT48, /* 0x09 uint48 */
394 GATT_FORMAT_UINT64, /* 0x0a uint64 */
395 GATT_FORMAT_UINT128, /* 0x0B uint128 */
396 GATT_FORMAT_SINT8, /* 0x0C signed 8 bit integer */
397 GATT_FORMAT_SINT12, /* 0x0D signed 12 bit integer */
398 GATT_FORMAT_SINT16, /* 0x0E signed 16 bit integer */
399 GATT_FORMAT_SINT24, /* 0x0F signed 24 bit integer */
400 GATT_FORMAT_SINT32, /* 0x10 signed 32 bit integer */
401 GATT_FORMAT_SINT48, /* 0x11 signed 48 bit integer */
402 GATT_FORMAT_SINT64, /* 0x12 signed 64 bit integer */
403 GATT_FORMAT_SINT128, /* 0x13 signed 128 bit integer */
404 GATT_FORMAT_FLOAT32, /* 0x14 float 32 */
405 GATT_FORMAT_FLOAT64, /* 0x15 float 64*/
406 GATT_FORMAT_SFLOAT, /* 0x16 IEEE-11073 16 bit SFLOAT */
407 GATT_FORMAT_FLOAT, /* 0x17 IEEE-11073 32 bit SFLOAT */
408 GATT_FORMAT_DUINT16, /* 0x18 IEEE-20601 format */
409 GATT_FORMAT_UTF8S, /* 0x19 UTF-8 string */
410 GATT_FORMAT_UTF16S, /* 0x1a UTF-16 string */
411 GATT_FORMAT_STRUCT, /* 0x1b Opaque structure*/
412 GATT_FORMAT_MAX /* 0x1c or above reserved */
413 };
414 typedef uint8_t tGATT_FORMAT;
415
416 /* Characteristic Presentation Format Descriptor value
417 */
418 typedef struct {
419 uint16_t unit; /* as UUIUD defined by SIG */
420 uint16_t descr; /* as UUID as defined by SIG */
421 tGATT_FORMAT format;
422 int8_t exp;
423 uint8_t name_spc; /* The name space of the description */
424 } tGATT_CHAR_PRES;
425
426 /* Characteristic Report reference Descriptor format
427 */
428 typedef struct {
429 uint8_t rpt_id; /* report ID */
430 uint8_t rpt_type; /* report type */
431 } tGATT_CHAR_RPT_REF;
432
433 #define GATT_VALID_RANGE_MAX_SIZE 16
434 typedef struct {
435 uint8_t format;
436 uint16_t len;
437 uint8_t lower_range[GATT_VALID_RANGE_MAX_SIZE]; /* in little endian format */
438 uint8_t upper_range[GATT_VALID_RANGE_MAX_SIZE];
439 } tGATT_VALID_RANGE;
440
441 /* Characteristic Aggregate Format attribute value
442 */
443 #define GATT_AGGR_HANDLE_NUM_MAX 10
444 typedef struct {
445 uint8_t num_handle;
446 uint16_t handle_list[GATT_AGGR_HANDLE_NUM_MAX];
447 } tGATT_CHAR_AGGRE;
448
449 /* Characteristic descriptor: Extended Properties value
450 */
451 /* permits reliable writes of the Characteristic Value */
452 #define GATT_CHAR_BIT_REL_WRITE 0x0001
453 /* permits writes to the characteristic descriptor */
454 #define GATT_CHAR_BIT_WRITE_AUX 0x0002
455
456 /* characteristic descriptor: client configuration value
457 */
458 #define GATT_CLT_CONFIG_NONE 0x0000
459 #define GATT_CLT_CONFIG_NOTIFICATION 0x0001
460 #define GATT_CLT_CONFIG_INDICATION 0x0002
461
462 /* characteristic descriptor: server configuration value
463 */
464 #define GATT_SVR_CONFIG_NONE 0x0000
465 #define GATT_SVR_CONFIG_BROADCAST 0x0001
466 typedef uint16_t tGATT_SVR_CHAR_CONFIG;
467
468 /* Characteristic descriptor: Extended Properties value
469 */
470 /* permits reliable writes of the Characteristic Value */
471 #define GATT_CHAR_BIT_REL_WRITE 0x0001
472 /* permits writes to the characteristic descriptor */
473 #define GATT_CHAR_BIT_WRITE_AUX 0x0002
474
475 /* authentication requirement
476 */
477 #define GATT_AUTH_REQ_NONE 0
478 #define GATT_AUTH_REQ_NO_MITM 1 /* unauthenticated encryption */
479 #define GATT_AUTH_REQ_MITM 2 /* authenticated encryption */
480 #define GATT_AUTH_REQ_SIGNED_NO_MITM 3
481 #define GATT_AUTH_REQ_SIGNED_MITM 4
482 typedef uint8_t tGATT_AUTH_REQ;
483
484 /* Attribute Value structure
485 */
486 typedef struct {
487 tCONN_ID conn_id;
488 uint16_t handle; /* attribute handle */
489 uint16_t offset; /* attribute value offset, if no offset is needed for the
490 command, ignore it */
491 uint16_t len; /* length of attribute value */
492 tGATT_AUTH_REQ auth_req; /* authentication request */
493 uint8_t value[GATT_MAX_ATTR_LEN]; /* the actual attribute value */
494 } tGATT_VALUE;
495
496 /* Union of the event data which is used in the server respond API to carry the
497 * server response information
498 */
499 typedef union {
500 /* data type member event */
501 tGATT_VALUE attr_value; /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
502 /* READ_BLOB, READ_BY_TYPE */
503 uint16_t handle; /* WRITE, WRITE_BLOB */
504 } tGATTS_RSP;
505
506 #define GATT_PREP_WRITE_CANCEL 0x00
507 #define GATT_PREP_WRITE_EXEC 0x01
508 typedef uint8_t tGATT_EXEC_FLAG;
509
510 /* read request always based on UUID */
511 typedef struct {
512 uint16_t handle;
513 uint16_t offset;
514 bool is_long;
515 bt_gatt_db_attribute_type_t gatt_type; /* are we writing characteristic or descriptor */
516 } tGATT_READ_REQ;
517
518 /* write request data */
519 typedef struct {
520 uint16_t handle; /* attribute handle */
521 uint16_t offset; /* attribute value offset, if no offset is needed for the
522 command, ignore it */
523 uint16_t len; /* length of attribute value */
524 uint8_t value[GATT_MAX_ATTR_LEN]; /* the actual attribute value */
525 bool need_rsp; /* need write response */
526 bool is_prep; /* is prepare write */
527 bt_gatt_db_attribute_type_t 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 10 /* Max attributes to read in one request */
593 typedef struct {
594 tGATT_AUTH_REQ auth_req;
595 uint16_t num_handles; /* number of handles to read */
596 uint16_t handles[GATT_MAX_READ_MULTI_HANDLES]; /* handles list to be read */
597 bool variable_len;
598 } tGATT_READ_MULTI;
599
600 /* Read By Handle Request (GATT_READ_BY_HANDLE) data */
601 typedef struct {
602 tGATT_AUTH_REQ auth_req;
603 uint16_t handle;
604 } tGATT_READ_BY_HANDLE;
605
606 /* READ_BT_HANDLE_Request data */
607 typedef struct {
608 tGATT_AUTH_REQ auth_req;
609 uint16_t handle;
610 uint16_t offset;
611 } tGATT_READ_PARTIAL;
612
613 /* Read Request Data
614 */
615 typedef union {
616 tGATT_READ_BY_TYPE service;
617 tGATT_READ_BY_TYPE char_type; /* characteristic type */
618 tGATT_READ_MULTI read_multiple;
619 tGATT_READ_BY_HANDLE by_handle;
620 tGATT_READ_PARTIAL partial;
621 } tGATT_READ_PARAM;
622
623 /* GATT write type enumeration */
624 enum { GATT_WRITE_NO_RSP = 1, GATT_WRITE, GATT_WRITE_PREPARE };
625 typedef uint8_t tGATT_WRITE_TYPE;
626
627 /* Client Operation Complete Callback Data
628 */
629 typedef union {
630 tGATT_VALUE att_value;
631 uint16_t mtu;
632 uint16_t handle;
633 uint16_t cid;
634 } tGATT_CL_COMPLETE;
635
636 /* GATT client operation type, used in client callback function
637 */
638 typedef enum : uint8_t {
639 GATTC_OPTYPE_NONE = 0,
640 GATTC_OPTYPE_DISCOVERY = 1,
641 GATTC_OPTYPE_READ = 2,
642 GATTC_OPTYPE_WRITE = 3,
643 GATTC_OPTYPE_EXE_WRITE = 4,
644 GATTC_OPTYPE_CONFIG = 5,
645 GATTC_OPTYPE_NOTIFICATION = 6,
646 GATTC_OPTYPE_INDICATION = 7,
647 } tGATTC_OPTYPE;
648
649 /* characteristic declaration
650 */
651 typedef struct {
652 tGATT_CHAR_PROP char_prop; /* characteristic properties */
653 uint16_t val_handle; /* characteristic value attribute handle */
654 bluetooth::Uuid char_uuid; /* characteristic UUID type */
655 } tGATT_CHAR_DCLR_VAL;
656
657 /* primary service group data
658 */
659 typedef struct {
660 uint16_t e_handle; /* ending handle of the group */
661 bluetooth::Uuid service_type; /* group type */
662 } tGATT_GROUP_VALUE;
663
664 /* included service attribute value
665 */
666 typedef struct {
667 bluetooth::Uuid service_type; /* included service UUID */
668 uint16_t s_handle; /* starting handle */
669 uint16_t e_handle; /* ending handle */
670 } tGATT_INCL_SRVC;
671
672 typedef union {
673 tGATT_INCL_SRVC incl_service; /* include service value */
674 tGATT_GROUP_VALUE group_value; /* Service UUID type.
675 This field is used with GATT_DISC_SRVC_ALL
676 or GATT_DISC_SRVC_BY_UUID
677 type of discovery result callback. */
678
679 uint16_t handle; /* When used with GATT_DISC_INC_SRVC type discovery result,
680 it is the included service starting handle.*/
681
682 tGATT_CHAR_DCLR_VAL dclr_value; /* Characteristic declaration value.
683 This field is used with GATT_DISC_CHAR type discovery.*/
684 } tGATT_DISC_VALUE;
685
686 /* discover result record
687 */
688 typedef struct {
689 bluetooth::Uuid type;
690 uint16_t handle;
691 tGATT_DISC_VALUE value;
692 } tGATT_DISC_RES;
693
694 #define GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP \
695 1 /* start a idle timer for this duration \
696 when no application need to use the link */
697
698 #define GATT_LINK_NO_IDLE_TIMEOUT 0xFFFF
699
700 #define GATT_INVALID_ACL_HANDLE 0xFFFF
701 /* discover result callback function */
702 typedef void(tGATT_DISC_RES_CB)(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
703 tGATT_DISC_RES* p_data);
704
705 /* discover complete callback function */
706 typedef void(tGATT_DISC_CMPL_CB)(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
707
708 /* Define a callback function for when read/write/disc/config operation is
709 * completed. */
710 typedef void(tGATT_CMPL_CBACK)(tCONN_ID conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
711 tGATT_CL_COMPLETE* p_data);
712
713 /* Define a callback function when an initialized connection is established. */
714 typedef void(tGATT_CONN_CBACK)(tGATT_IF gatt_if, const RawAddress& bda, tCONN_ID conn_id,
715 bool connected, tGATT_DISCONN_REASON reason,
716 tBT_TRANSPORT transport);
717
718 /* attribute request callback for ATT server */
719 typedef void(tGATT_REQ_CBACK)(tCONN_ID conn_id, uint32_t trans_id, tGATTS_REQ_TYPE type,
720 tGATTS_DATA* p_data);
721
722 /* channel congestion/uncongestion callback */
723 typedef void(tGATT_CONGESTION_CBACK)(tCONN_ID conn_id, bool congested);
724
725 /* Define a callback function when encryption is established. */
726 typedef void(tGATT_ENC_CMPL_CB)(tGATT_IF gatt_if, const RawAddress& bda);
727
728 /* Define a callback function when phy is updated. */
729 typedef void(tGATT_PHY_UPDATE_CB)(tGATT_IF gatt_if, tCONN_ID conn_id, uint8_t tx_phy,
730 uint8_t rx_phy, tGATT_STATUS status);
731
732 /* Define a callback function when connection parameters are updated */
733 typedef void(tGATT_CONN_UPDATE_CB)(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t interval,
734 uint16_t latency, uint16_t timeout, tGATT_STATUS status);
735
736 /* Define a callback function when subrate change event is received */
737 typedef void(tGATT_SUBRATE_CHG_CB)(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t subrate_factor,
738 uint16_t latency, uint16_t cont_num, uint16_t timeout,
739 tGATT_STATUS status);
740
741 /* Define the structure that applications use to register with
742 * GATT. This structure includes callback functions. All functions
743 * MUST be provided.
744 */
745 typedef struct {
746 tGATT_CONN_CBACK* p_conn_cb{nullptr};
747 tGATT_CMPL_CBACK* p_cmpl_cb{nullptr};
748 tGATT_DISC_RES_CB* p_disc_res_cb{nullptr};
749 tGATT_DISC_CMPL_CB* p_disc_cmpl_cb{nullptr};
750 tGATT_REQ_CBACK* p_req_cb{nullptr};
751 tGATT_ENC_CMPL_CB* p_enc_cmpl_cb{nullptr};
752 tGATT_CONGESTION_CBACK* p_congestion_cb{nullptr};
753 tGATT_PHY_UPDATE_CB* p_phy_update_cb{nullptr};
754 tGATT_CONN_UPDATE_CB* p_conn_update_cb{nullptr};
755 tGATT_SUBRATE_CHG_CB* p_subrate_chg_cb{nullptr};
756 } tGATT_CBACK;
757
758 /***************** Start Handle Management Definitions *********************/
759
760 typedef struct {
761 bluetooth::Uuid app_uuid128;
762 bluetooth::Uuid svc_uuid;
763 uint16_t s_handle;
764 uint16_t e_handle;
765 bool is_primary; /* primary service or secondary */
766 } tGATTS_HNDL_RANGE;
767
768 #define GATTS_SRV_CHG_CMD_ADD_CLIENT 1
769 #define GATTS_SRV_CHG_CMD_UPDATE_CLIENT 2
770 #define GATTS_SRV_CHG_CMD_REMOVE_CLIENT 3
771 #define GATTS_SRV_CHG_CMD_READ_NUM_CLENTS 4
772 #define GATTS_SRV_CHG_CMD_READ_CLENT 5
773 typedef uint8_t tGATTS_SRV_CHG_CMD;
774
775 typedef struct {
776 RawAddress bda;
777 bool srv_changed;
778 } tGATTS_SRV_CHG;
779
780 typedef union {
781 tGATTS_SRV_CHG srv_chg;
782 uint8_t client_read_index; /* only used for sequential reading client srv chg
783 info */
784 } tGATTS_SRV_CHG_REQ;
785
786 typedef union {
787 tGATTS_SRV_CHG srv_chg;
788 uint8_t num_clients;
789 } tGATTS_SRV_CHG_RSP;
790
791 /* Attribute server handle ranges NV storage callback functions
792 */
793 typedef void(tGATTS_NV_SAVE_CBACK)(bool is_saved, tGATTS_HNDL_RANGE* p_hndl_range);
794 typedef bool(tGATTS_NV_SRV_CHG_CBACK)(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ* p_req,
795 tGATTS_SRV_CHG_RSP* p_rsp);
796
797 typedef struct {
798 tGATTS_NV_SAVE_CBACK* p_nv_save_callback;
799 tGATTS_NV_SRV_CHG_CBACK* p_srv_chg_callback;
800 } tGATT_APPL_INFO;
801
802 /******************** End Handle Management Definitions ********************/
803
804 /*******************************************************************************
805 * External Function Declarations
806 ******************************************************************************/
807
808 /******************************************************************************/
809 /* GATT Profile API Functions */
810 /******************************************************************************/
811 /* GATT Profile Server Functions */
812 /******************************************************************************/
813
814 /*******************************************************************************
815 *
816 * Function GATTS_NVRegister
817 *
818 * Description Application manager calls this function to register for
819 * NV save callback function. There can be one and only one
820 * NV save callback function.
821 *
822 * Parameter p_cb_info : callback information
823 *
824 * Returns true if registered OK, else false
825 *
826 ******************************************************************************/
827 [[nodiscard]] bool GATTS_NVRegister(tGATT_APPL_INFO* p_cb_info);
828
829 /*******************************************************************************
830 *
831 * Function BTA_GATTS_AddService
832 *
833 * Description Add a service. When service is ready, a callback
834 * event BTA_GATTS_ADD_SRVC_EVT is called to report status
835 * and handles to the profile.
836 *
837 * Parameters server_if: server interface.
838 * service: pointer array describing service.
839 * count: number of elements in service array.
840 *
841 * Returns on success GATT_SERVICE_STARTED is returned, and
842 * attribute_handle field inside service elements are filled.
843 * on error error status is returned.
844 *
845 ******************************************************************************/
846 [[nodiscard]] tGATT_STATUS GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service,
847 int count);
848
849 /*******************************************************************************
850 *
851 * Function GATTS_DeleteService
852 *
853 * Description This function is called to delete a service.
854 *
855 * Parameter gatt_if : application interface
856 * p_svc_uuid : service UUID
857 * svc_inst : instance of the service inside the
858 * application
859 *
860 * Returns true if operation succeed, else false
861 *
862 ******************************************************************************/
863 [[nodiscard]] bool GATTS_DeleteService(tGATT_IF gatt_if, bluetooth::Uuid* p_svc_uuid,
864 uint16_t svc_inst);
865
866 /*******************************************************************************
867 *
868 * Function GATTS_StopService
869 *
870 * Description This function is called to stop a service
871 *
872 * Parameter service_handle : this is the start handle of a service
873 *
874 * Returns None.
875 *
876 ******************************************************************************/
877 void GATTS_StopService(uint16_t service_handle);
878
879 /*******************************************************************************
880 *
881 * Function GATTs_HandleValueIndication
882 *
883 * Description This function sends a handle value indication to a client.
884 *
885 * Parameter conn_id: connection identifier.
886 * attr_handle: Attribute handle of this handle value
887 * indication.
888 * val_len: Length of the indicated attribute value.
889 * p_val: Pointer to the indicated attribute value data.
890 *
891 * Returns GATT_SUCCESS if successfully sent or queued; otherwise error
892 * code.
893 *
894 ******************************************************************************/
895 [[nodiscard]] tGATT_STATUS GATTS_HandleValueIndication(tCONN_ID conn_id, uint16_t attr_handle,
896 uint16_t val_len, uint8_t* p_val);
897
898 /*******************************************************************************
899 *
900 * Function GATTS_HandleValueNotification
901 *
902 * Description This function sends a handle value notification to a client.
903 *
904 * Parameter conn_id: connection identifier.
905 * attr_handle: Attribute handle of this handle value
906 * indication.
907 * val_len: Length of the indicated attribute value.
908 * p_val: Pointer to the indicated attribute value data.
909 *
910 * Returns GATT_SUCCESS if successfully sent; otherwise error code.
911 *
912 ******************************************************************************/
913 [[nodiscard]] tGATT_STATUS GATTS_HandleValueNotification(tCONN_ID conn_id, uint16_t attr_handle,
914 uint16_t val_len, uint8_t* p_val);
915
916 /*******************************************************************************
917 *
918 * Function GATTS_SendRsp
919 *
920 * Description This function sends the server response to client.
921 *
922 * Parameter conn_id: connection identifier.
923 * trans_id: transaction id
924 * status: response status
925 * p_msg: pointer to message parameters structure.
926 *
927 * Returns GATT_SUCCESS if successfully sent; otherwise error code.
928 *
929 ******************************************************************************/
930 [[nodiscard]] tGATT_STATUS GATTS_SendRsp(tCONN_ID conn_id, uint32_t trans_id, tGATT_STATUS status,
931 tGATTS_RSP* p_msg);
932
933 /******************************************************************************/
934 /* GATT Profile Client Functions */
935 /******************************************************************************/
936
937 /*******************************************************************************
938 *
939 * Function GATTC_ConfigureMTU
940 *
941 * Description This function is called to configure the ATT MTU size for
942 * a connection on an LE transport.
943 *
944 * Parameters conn_id: connection identifier.
945 * mtu - attribute MTU size..
946 *
947 * Returns GATT_SUCCESS if command started successfully.
948 *
949 ******************************************************************************/
950 [[nodiscard]] tGATT_STATUS GATTC_ConfigureMTU(tCONN_ID conn_id, uint16_t mtu);
951
952 /*******************************************************************************
953 * Function GATTC_UpdateUserAttMtuIfNeeded
954 *
955 * Description This function to be called when user requested MTU after
956 * MTU Exchange has been already done. This will update data
957 * length in the controller.
958 *
959 * Parameters remote_bda : peer device address. (input)
960 * transport : physical transport of the GATT connection
961 * (BR/EDR or LE) (input)
962 * user_mtu: user request mtu
963 *
964 ******************************************************************************/
965 void GATTC_UpdateUserAttMtuIfNeeded(const RawAddress& remote_bda, tBT_TRANSPORT transport,
966 uint16_t user_mtu);
967
968 /******************************************************************************
969 *
970 * Function GATTC_TryMtuRequest
971 *
972 * Description This function shall be called before calling
973 * GATTC_ConfigureMTU in order to check if operation is
974 * available to do.
975 *
976 * Parameters remote_bda : peer device address. (input)
977 * transport : physical transport of the GATT connection
978 * (BR/EDR or LE) (input)
979 * conn_id : connection id (input)
980 * current_mtu: current mtu on the link (output)
981 *
982 * Returns tGATTC_TryMtuRequestResult:
983 * - MTU_EXCHANGE_NOT_DONE_YET: There was no MTU Exchange
984 * procedure on the link. User can call GATTC_ConfigureMTU
985 * now.
986 * - MTU_EXCHANGE_NOT_ALLOWED : Not allowed for BR/EDR or if
987 * link does not exist
988 * - MTU_EXCHANGE_ALREADY_DONE: MTU Exchange is done. MTU
989 * should be taken from current_mtu
990 * - MTU_EXCHANGE_IN_PROGRESS : Other use is doing MTU
991 * Exchange. Conn_id is stored for result.
992 *
993 ******************************************************************************/
994 [[nodiscard]] tGATTC_TryMtuRequestResult GATTC_TryMtuRequest(const RawAddress& remote_bda,
995 tBT_TRANSPORT transport,
996 tCONN_ID conn_id,
997 uint16_t* current_mtu);
998
999 [[nodiscard]] std::list<tCONN_ID> GATTC_GetAndRemoveListOfConnIdsWaitingForMtuRequest(
1000 const RawAddress& remote_bda);
1001 /*******************************************************************************
1002 *
1003 * Function GATTC_Discover
1004 *
1005 * Description This function is called to do a discovery procedure on ATT
1006 * server.
1007 *
1008 * Parameters conn_id: connection identifier.
1009 * disc_type:discovery type.
1010 * start_handle and end_handle: range of handles for discovery
1011 * uuid: uuid to discovery. set to Uuid::kEmpty for requests
1012 * that don't need it
1013 *
1014 * Returns GATT_SUCCESS if command received/sent successfully.
1015 *
1016 ******************************************************************************/
1017 [[nodiscard]] tGATT_STATUS GATTC_Discover(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
1018 uint16_t start_handle, uint16_t end_handle,
1019 const bluetooth::Uuid& uuid);
1020 [[nodiscard]] tGATT_STATUS GATTC_Discover(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
1021 uint16_t start_handle, uint16_t end_handle);
1022
1023 /*******************************************************************************
1024 *
1025 * Function GATTC_Read
1026 *
1027 * Description This function is called to read the value of an attribute
1028 * from the server.
1029 *
1030 * Parameters conn_id: connection identifier.
1031 * type - attribute read type.
1032 * p_read - read operation parameters.
1033 *
1034 * Returns GATT_SUCCESS if command started successfully.
1035 *
1036 ******************************************************************************/
1037 [[nodiscard]] tGATT_STATUS GATTC_Read(tCONN_ID conn_id, tGATT_READ_TYPE type,
1038 tGATT_READ_PARAM* p_read);
1039
1040 /*******************************************************************************
1041 *
1042 * Function GATTC_Write
1043 *
1044 * Description This function is called to read the value of an attribute
1045 * from the server.
1046 *
1047 * Parameters conn_id: connection identifier.
1048 * type - attribute write type.
1049 * p_write - write operation parameters.
1050 *
1051 * Returns GATT_SUCCESS if command started successfully.
1052 *
1053 ******************************************************************************/
1054 [[nodiscard]] tGATT_STATUS GATTC_Write(tCONN_ID conn_id, tGATT_WRITE_TYPE type,
1055 tGATT_VALUE* p_write);
1056
1057 /*******************************************************************************
1058 *
1059 * Function GATTC_ExecuteWrite
1060 *
1061 * Description This function is called to send an Execute write request to
1062 * the server.
1063 *
1064 * Parameters conn_id: connection identifier.
1065 * is_execute - to execute or cancel the prepare write
1066 * request(s)
1067 *
1068 * Returns GATT_SUCCESS if command started successfully.
1069 *
1070 ******************************************************************************/
1071 [[nodiscard]] tGATT_STATUS GATTC_ExecuteWrite(tCONN_ID conn_id, bool is_execute);
1072
1073 /*******************************************************************************
1074 *
1075 * Function GATTC_SendHandleValueConfirm
1076 *
1077 * Description This function is called to send a handle value confirmation
1078 * as response to a handle value notification from server.
1079 *
1080 * Parameters conn_id: connection identifier.
1081 * handle: the handle of the attribute confirmation.
1082 *
1083 * Returns GATT_SUCCESS if command started successfully.
1084 *
1085 ******************************************************************************/
1086 [[nodiscard]] tGATT_STATUS GATTC_SendHandleValueConfirm(tCONN_ID conn_id, uint16_t handle);
1087
1088 /*******************************************************************************
1089 *
1090 * Function GATT_SetIdleTimeout
1091 *
1092 * Description This function (common to both client and server) sets the
1093 * idle timeout for a transport connection
1094 *
1095 * Parameter bd_addr: target device bd address.
1096 * idle_tout: timeout value in seconds.
1097 * transport: transport option.
1098 * is_active: whether we should use this as a signal that an
1099 * active client now exists (which changes link
1100 * timeout logic, see
1101 * t_l2c_linkcb.with_active_local_clients for
1102 * details).
1103 *
1104 * Returns void
1105 *
1106 ******************************************************************************/
1107 void GATT_SetIdleTimeout(const RawAddress& bd_addr, uint16_t idle_tout, tBT_TRANSPORT transport,
1108 bool is_active);
1109
1110 /*******************************************************************************
1111 *
1112 * Function GATT_Register
1113 *
1114 * Description This function is called to register an application
1115 * with GATT
1116 *
1117 * Parameter p_app_uuid128: Application UUID
1118 * p_cb_info: callback functions.
1119 * eatt_support: set support for eatt
1120 *
1121 * Returns 0 for error, otherwise the index of the client registered
1122 * with GATT
1123 *
1124 ******************************************************************************/
1125 [[nodiscard]] tGATT_IF GATT_Register(const bluetooth::Uuid& p_app_uuid128, const std::string& name,
1126 tGATT_CBACK* p_cb_info, bool eatt_support);
1127
1128 /*******************************************************************************
1129 *
1130 * Function GATT_Deregister
1131 *
1132 * Description This function deregistered the application from GATT.
1133 *
1134 * Parameters gatt_if: application interface.
1135 *
1136 * Returns None.
1137 *
1138 ******************************************************************************/
1139 void GATT_Deregister(tGATT_IF gatt_if);
1140
1141 /*******************************************************************************
1142 *
1143 * Function GATT_StartIf
1144 *
1145 * Description This function is called after registration to start
1146 * receiving callbacks for registered interface. Function may
1147 * call back with connection status and queued notifications
1148 *
1149 * Parameter gatt_if: application interface.
1150 *
1151 * Returns None
1152 *
1153 ******************************************************************************/
1154 void GATT_StartIf(tGATT_IF gatt_if);
1155
1156 /*******************************************************************************
1157 *
1158 * Function GATT_Connect
1159 *
1160 * Description This function initiate a connection to a remote device on
1161 * GATT channel.
1162 *
1163 * Parameters gatt_if: application interface
1164 * bd_addr: peer device address
1165 * addr_type: peer device address type
1166 * connection_type: connection type
1167 * transport : Physical transport for GATT connection
1168 * (BR/EDR or LE)
1169 * opportunistic: will not keep device connected if other apps
1170 * disconnect, will not update connected apps counter, when
1171 * disconnected won't cause physical disconnection.
1172 *
1173 * Returns true if connection started; else false
1174 *
1175 ******************************************************************************/
1176 [[nodiscard]] bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr,
1177 tBLE_ADDR_TYPE addr_type, tBTM_BLE_CONN_TYPE connection_type,
1178 tBT_TRANSPORT transport, bool opportunistic,
1179 uint8_t initiating_phys, uint16_t preferred_transport);
1180 [[nodiscard]] bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr,
1181 tBTM_BLE_CONN_TYPE connection_type, tBT_TRANSPORT transport,
1182 bool opportunistic);
1183
1184 /*******************************************************************************
1185 *
1186 * Function GATT_CancelConnect
1187 *
1188 * Description Terminate the connection initiation to a remote device on a
1189 * GATT channel.
1190 *
1191 * Parameters gatt_if: client interface. If 0 used as unconditionally
1192 * disconnect, typically used for direct connection
1193 * cancellation.
1194 * bd_addr: peer device address.
1195 * is_direct: is a direct connection or a background auto
1196 * connection
1197 *
1198 * Returns true if connection started; else false
1199 *
1200 ******************************************************************************/
1201 [[nodiscard]] bool GATT_CancelConnect(tGATT_IF gatt_if, const RawAddress& bd_addr, bool is_direct);
1202
1203 /*******************************************************************************
1204 *
1205 * Function GATT_Disconnect
1206 *
1207 * Description Disconnect the GATT channel for this registered application.
1208 *
1209 * Parameters conn_id: connection identifier.
1210 *
1211 * Returns GATT_SUCCESS if disconnected.
1212 *
1213 ******************************************************************************/
1214 [[nodiscard]] tGATT_STATUS GATT_Disconnect(tCONN_ID conn_id);
1215
1216 /*******************************************************************************
1217 *
1218 * Function GATT_GetConnectionInfor
1219 *
1220 * Description Use conn_id to find its associated BD address and
1221 * application interface
1222 *
1223 * Parameters conn_id: connection id (input)
1224 * p_gatt_if: application interface (output)
1225 * bd_addr: peer device address. (output)
1226 * transport : physical transport of the GATT connection
1227 * (BR/EDR or LE)
1228 *
1229 * Returns true the logical link information is found for conn_id
1230 *
1231 ******************************************************************************/
1232 [[nodiscard]] bool GATT_GetConnectionInfor(tCONN_ID conn_id, tGATT_IF* p_gatt_if,
1233 RawAddress& bd_addr, tBT_TRANSPORT* p_transport);
1234
1235 /*******************************************************************************
1236 *
1237 * Function GATT_GetConnIdIfConnected
1238 *
1239 * Description Find the conn_id if the logical link for a BD address
1240 * and application interface is connected
1241 *
1242 * Parameters gatt_if: application interface (input)
1243 * bd_addr: peer device address. (input)
1244 * p_conn_id: connection id (output)
1245 * transport : physical transport of the GATT connection
1246 * (BR/EDR or LE)
1247 *
1248 * Returns true the logical link is connected
1249 *
1250 ******************************************************************************/
1251 [[nodiscard]] bool GATT_GetConnIdIfConnected(tGATT_IF gatt_if, const RawAddress& bd_addr,
1252 tCONN_ID* p_conn_id, tBT_TRANSPORT transport);
1253
1254 /*******************************************************************************
1255 *
1256 * Function GATT_ConfigServiceChangeCCC
1257 *
1258 * Description Configure service change indication on remote device
1259 *
1260 * Returns None.
1261 *
1262 ******************************************************************************/
1263 void GATT_ConfigServiceChangeCCC(const RawAddress& remote_bda, bool enable,
1264 tBT_TRANSPORT transport);
1265
1266 // Enables the GATT profile on the device.
1267 // It clears out the control blocks, and registers with L2CAP.
1268 void gatt_init(void);
1269
1270 // Frees resources used by the GATT profile.
1271 void gatt_free(void);
1272
1273 void gatt_consolidate(const RawAddress& identity_addr, const RawAddress& rpa);
1274 void gatt_notify_conn_update(const RawAddress& remote, uint16_t interval, uint16_t latency,
1275 uint16_t timeout, tHCI_STATUS status);
1276
1277 // Link encryption complete notification for all encryption process
1278 // initiated outside GATT.
1279 void gatt_notify_enc_cmpl(const RawAddress& bd_addr);
1280
1281 /** Reset bg device list. If called after controller reset, set |after_reset| to
1282 * true, as there is no need to wipe controller acceptlist in this case. */
1283 void gatt_reset_bgdev_list(bool after_reset);
1284
1285 // Initialize GATTS list of bonded device service change updates.
1286 void gatt_load_bonded(void);
1287
1288 void gatt_tcb_dump(int fd);
1289
1290 namespace std {
1291 template <>
1292 struct formatter<GattStatus> : enum_formatter<GattStatus> {};
1293 template <>
1294 struct formatter<tGATT_DISCONN_REASON> : enum_formatter<tGATT_DISCONN_REASON> {};
1295 template <>
1296 struct formatter<tGATTC_OPTYPE> : enum_formatter<tGATTC_OPTYPE> {};
1297 template <>
1298 struct formatter<tGATT_OP_CODE> : enum_formatter<tGATT_OP_CODE> {};
1299 template <>
1300 struct formatter<tGATT_DISC_TYPE> : enum_formatter<tGATT_DISC_TYPE> {};
1301 } // namespace std
1302
1303 #endif /* GATT_API_H */
1304