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