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