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