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