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