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