1 /*
2  * Copyright 2016-2023, Cypress Semiconductor Corporation or
3  * an affiliate of Cypress Semiconductor Corporation.  All rights reserved.
4  *
5  * This software, including source code, documentation and related
6  * materials ("Software") is owned by Cypress Semiconductor Corporation
7  * or one of its affiliates ("Cypress") and is protected by and subject to
8  * worldwide patent protection (United States and foreign),
9  * United States copyright laws and international treaty provisions.
10  * Therefore, you may use this Software only as provided in the license
11  * agreement accompanying the software package from which you
12  * obtained this Software ("EULA").
13  * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
14  * non-transferable license to copy, modify, and compile the Software
15  * source code solely for use in connection with Cypress's
16  * integrated circuit products.  Any reproduction, modification, translation,
17  * compilation, or representation of this Software except as specified
18  * above is prohibited without the express written permission of Cypress.
19  *
20  * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
23  * reserves the right to make changes to the Software without notice. Cypress
24  * does not assume any liability arising out of the application or use of the
25  * Software or any product or circuit described in the Software. Cypress does
26  * not authorize its products for use in any products where a malfunction or
27  * failure of the Cypress product may reasonably be expected to result in
28  * significant property damage, injury or death ("High Risk Product"). By
29  * including Cypress's product in a High Risk Product, the manufacturer
30  * of such system or application assumes all risk of such use and in doing
31  * so agrees to indemnify Cypress against all liability.
32  */
33 
34 /** @file
35  *
36  *  WICED Generic Attribute (GATT) Application Programming Interface
37  */
38 #pragma once
39 
40 #include "wiced_result.h"
41 #include "gattdefs.h"
42 #include "wiced_bt_dev.h"
43 #include "wiced_bt_ble.h"
44 #include "l2cdefs.h"
45 #include "wiced_bt_uuid.h"
46 
47 
48 /**
49  * @addtogroup wicedbt_gatt
50  * @{
51  */
52 
53  /**
54  * Default GATT MTU size over LE link
55  */
56 #define GATT_BLE_DEFAULT_MTU_SIZE 23
57 /**
58  * Size of the signature appended to application data in signed write cmd
59  */
60 #define GATT_AUTH_SIGN_LEN   12
61 /**
62  * Max size of application data allowed to be sent using the signed write cmd.
63  */
64 #define GATT_CLIENT_MAX_WRITE_SIGNED_DATA (23 - 3 - GATT_AUTH_SIGN_LEN)
65 
66  /**
67   * GATT Header size (1 byte opcode + 2 byte handle)
68   */
69 #define WICED_GATT_HDR_SIZE  3
70 
71 /** GATT Status Codes*/
72 enum wiced_bt_gatt_status_e
73 {
74     WICED_BT_GATT_SUCCESS                    = 0x00,         /**< Success */
75     WICED_BT_GATT_INVALID_HANDLE             = 0x01,         /**< Invalid Handle */
76     WICED_BT_GATT_READ_NOT_PERMIT            = 0x02,         /**< Read Not Permitted */
77     WICED_BT_GATT_WRITE_NOT_PERMIT           = 0x03,         /**< Write Not permitted */
78     WICED_BT_GATT_INVALID_PDU                = 0x04,         /**< Invalid PDU */
79     WICED_BT_GATT_INSUF_AUTHENTICATION       = 0x05,         /**< Insufficient Authentication */
80     WICED_BT_GATT_REQ_NOT_SUPPORTED          = 0x06,         /**< Request Not Supported */
81     WICED_BT_GATT_INVALID_OFFSET             = 0x07,         /**< Invalid Offset */
82     WICED_BT_GATT_INSUF_AUTHORIZATION        = 0x08,         /**< Insufficient Authorization */
83     WICED_BT_GATT_PREPARE_Q_FULL             = 0x09,         /**< Prepare Queue Full */
84     WICED_BT_GATT_ATTRIBUTE_NOT_FOUND        = 0x0a,         /**< Attribute Not Found */
85     WICED_BT_GATT_NOT_LONG                   = 0x0b,         /**< Not Long Size */
86     WICED_BT_GATT_INSUF_KEY_SIZE             = 0x0c,         /**< Insufficient Key Size */
87     WICED_BT_GATT_INVALID_ATTR_LEN           = 0x0d,         /**< Invalid Attribute Length */
88     WICED_BT_GATT_ERR_UNLIKELY               = 0x0e,         /**< Error Unlikely */
89     WICED_BT_GATT_INSUF_ENCRYPTION           = 0x0f,         /**< Insufficient Encryption */
90     WICED_BT_GATT_UNSUPPORT_GRP_TYPE         = 0x10,         /**< Unsupported Group Type */
91     WICED_BT_GATT_INSUF_RESOURCE             = 0x11,         /**< Insufficient Resource */
92     WICED_BT_GATT_DATABASE_OUT_OF_SYNC       = 0x12,         /**< GATT Database Out of Sync */
93 	WICED_BT_GATT_VALUE_NOT_ALLOWED          = 0x13,         /**< Value Not allowed */
94                                                              /* 0xE0 ~ 0xFB reserved for future use */
95     WICED_BT_GATT_WRITE_REQ_REJECTED         = 0xFC,         /**< Client Write operation rejected */
96     WICED_BT_GATT_CCCD_IMPROPER_CONFIGURED   = 0xFD,         /**< Client Characteristic Configuration Descriptor Improperly Configured */
97     WICED_BT_GATT_BUSY                       = 0xFE,         /**< Busy or Procedure already in progress */
98     WICED_BT_GATT_OUT_OF_RANGE               = 0xFF,         /**< Value Out of Range */
99                                                               /* WICED defined status  */
100     WICED_BT_GATT_ILLEGAL_PARAMETER          = 0x8780,         /**< Illegal Parameter */
101     WICED_BT_GATT_NO_RESOURCES               = 0x8781,         /**< No Resources */
102     WICED_BT_GATT_INTERNAL_ERROR             = 0x8783,         /**< Internal Error */
103     WICED_BT_GATT_WRONG_STATE                = 0x8784,         /**< Wrong State */
104     WICED_BT_GATT_DB_FULL                    = 0x8785,         /**< DB Full */
105     WICED_BT_GATT_UNUSED1                    = 0x8786,         /**< Unused */
106     WICED_BT_GATT_ERROR                      = 0x8787,         /**< Error */
107     WICED_BT_GATT_CMD_STARTED                = 0x8788,         /**< Command Started */
108     WICED_BT_GATT_PENDING                    = 0x8789,         /**< Pending */
109     WICED_BT_GATT_AUTH_FAIL                  = 0x878A,         /**< Authentication Fail */
110     WICED_BT_GATT_MORE                       = 0x878B,         /**< More */
111     WICED_BT_GATT_INVALID_CFG                = 0x878C,         /**< Invalid Configuration */
112     WICED_BT_GATT_SERVICE_STARTED            = 0x878D,         /**< Service Started */
113     WICED_BT_GATT_ENCRYPTED_MITM             = WICED_BT_GATT_SUCCESS, /**< Encrypted MITM */
114     WICED_BT_GATT_ENCRYPTED_NO_MITM          = 0x878E,         /**< Encrypted No MITM */
115     WICED_BT_GATT_NOT_ENCRYPTED              = 0x878F,         /**< Not Encrypted */
116     WICED_BT_GATT_CONGESTED                  = 0x8791,         /**< Congested */
117     WICED_BT_GATT_NOT_ALLOWED                = 0x8792,         /**< Operation not allowed */
118     WICED_BT_GATT_HANDLED                    = 0x8793,         /**< Set by application to indicate it has responded to the message */
119     WICED_BT_GATT_NO_PENDING_OPERATION       = 0x8794,         /**< No pending client operation for the response sent by app */
120     WICED_BT_GATT_INDICATION_RESPONSE_PENDING= 0x8795,         /**< Indication response pending */
121     WICED_BT_GATT_UNUSED2                    = 0x8796,         /**< Unused */
122     WICED_BT_GATT_CCC_CFG_ERR                = 0x8797,         /**< Improper Client Char Configuration */
123     WICED_BT_GATT_PRC_IN_PROGRESS            = 0x8798,         /**< Procedure Already in Progress */
124     WICED_BT_GATT_UNUSED3                    = 0x8799,         /**< Unused */
125     WICED_BT_GATT_BAD_OPCODE                 = 0x879A,         /**< Bad opcode */
126     WICED_BT_GATT_NOT_IMPLEMENTED            = 0x879B,         /**< Not implemented */
127 
128 
129     WICED_BT_GATT_INVALID_CONNECTION_ID      = 0xFFFF,         /**< Invalid connection id */
130 };
131 typedef uint16_t wiced_bt_gatt_status_t;     /**< GATT status (see #wiced_bt_gatt_status_e) */
132 
133 
134 /**
135  * GATT Operation Codes.
136  * All GATT_REQ_xxx are sent by the client and received on the server.
137  * All GATT_RSP_xxx are sent by the server in response to the specific requests from client
138  * All GATT_CMD_xxx are sent by client and received on the server. The server shall not send any response to the received GATT_CMD_xxx
139  *
140  * \p GATT_HANDLE_VALUE_NOTIF, \p GATT_HANDLE_VALUE_IND and \p GATT_HANDLE_VALUE_MULTI_NOTIF are sent by server
141  * to notify/indicate changes to handle values on the server
142  * \p GATT_HANDLE_VALUE_CONF is sent by the client in response to \p GATT_HANDLE_VALUE_IND
143  *
144  */
145 enum wiced_bt_gatt_opcode_e
146 {
147     GATT_RSP_ERROR                  = 0x01,       /**< Error Response */
148     GATT_REQ_MTU                    = 0x02,       /**< Exchange MTU Request */
149     GATT_RSP_MTU                    = 0x03,       /**< Exchange MTU Response */
150     GATT_REQ_FIND_INFO              = 0x04,       /**< Find Information Request */
151     GATT_RSP_FIND_INFO              = 0x05,       /**< Find Information Response */
152     GATT_REQ_FIND_TYPE_VALUE        = 0x06,       /**< Find By Type Value Request */
153     GATT_RSP_FIND_TYPE_VALUE        = 0x07,       /**< Find By Type Value Response */
154     GATT_REQ_READ_BY_TYPE           = 0x08,       /**< Read By Type Request */
155     GATT_RSP_READ_BY_TYPE           = 0x09,       /**< Read By Type Response */
156     GATT_REQ_READ                   = 0x0A,       /**< Read Request */
157     GATT_RSP_READ                   = 0x0B,       /**< Read Response */
158     GATT_REQ_READ_BLOB              = 0x0C,       /**< Read Blob Request */
159     GATT_RSP_READ_BLOB              = 0x0D,       /**< Read Blob Response */
160     GATT_REQ_READ_MULTI             = 0x0E,       /**< Read Multiple Request */
161     GATT_RSP_READ_MULTI             = 0x0F,       /**< Read Multiple Response */
162     GATT_REQ_READ_BY_GRP_TYPE       = 0x10,       /**< Read By Group Type Request */
163     GATT_RSP_READ_BY_GRP_TYPE       = 0x11,       /**< Read By Group Type Response */
164     GATT_REQ_WRITE                  = 0x12,       /**< Write Request */
165     GATT_RSP_WRITE                  = 0x13,       /**< Write Response */
166     GATT_REQ_PREPARE_WRITE          = 0x16,       /**< Prepare Write Request */
167     GATT_RSP_PREPARE_WRITE          = 0x17,       /**< Prepare Write Response */
168     GATT_REQ_EXECUTE_WRITE          = 0x18,       /**< Execute Write Request */
169     GATT_RSP_EXECUTE_WRITE          = 0x19,       /**< Execute Write Response */
170     GATT_HANDLE_VALUE_NOTIF         = 0x1B,       /**< Handle Value Notification */
171     GATT_HANDLE_VALUE_IND           = 0x1D,       /**< Handle Value Indication */
172     GATT_HANDLE_VALUE_CONF          = 0x1E,       /**< Handle Value Confirmation */
173     GATT_REQ_READ_MULTI_VAR_LENGTH  = 0x20,       /**< Read Multiple Variable Length Request */
174     GATT_RSP_READ_MULTI_VAR_LENGTH  = 0x21,       /**< Read Multiple Variable Length Response */
175     GATT_HANDLE_VALUE_MULTI_NOTIF   = 0x23,       /**< Handle Value Multiple Notifications */
176 
177     GATT_CMD_WRITE                  = 0x52,       /**< Write Command */
178     GATT_CMD_SIGNED_WRITE           = 0xD2,       /**< changed in V4.0 1101-0010 (signed write)  see write cmd above*/
179 };
180 
181 typedef uint8_t wiced_bt_gatt_opcode_t;           /**< GATT Opcodes */
182 
183 /**  GATT Disconnection reason */
184 enum wiced_bt_gatt_disconn_reason_e {
185     GATT_CONN_UNKNOWN                       = 0,                                    /**< Unknown reason */
186     GATT_CONN_L2C_FAILURE                   = 1,                                    /**< General L2cap failure  */
187     GATT_CONN_TIMEOUT                       = HCI_ERR_CONNECTION_TOUT,              /**< Connection timeout  */
188     GATT_CONN_TERMINATE_PEER_USER           = HCI_ERR_PEER_USER,                    /**< Connection terminated by peer user  */
189     GATT_CONN_TERMINATE_LOCAL_HOST          = HCI_ERR_CONN_CAUSE_LOCAL_HOST,        /**< Connection terminated by local host  */
190     GATT_CONN_FAIL_ESTABLISH                = HCI_ERR_CONN_FAILED_ESTABLISHMENT,    /**< Connection fail to establish  */
191     GATT_CONN_LMP_TIMEOUT                   = HCI_ERR_LMP_RESPONSE_TIMEOUT,         /**< Connection fail due to LMP response tout */
192     GATT_CONN_CANCEL                        = L2CAP_CONN_CANCEL                     /**< L2CAP connection cancelled  */
193 };
194 typedef uint16_t wiced_bt_gatt_disconn_reason_t;    /**< GATT disconnection reason (see #wiced_bt_gatt_disconn_reason_e) */
195 
196 /** characteristic descriptor: client configuration value */
197 enum wiced_bt_gatt_client_char_config_e
198 {
199     GATT_CLIENT_CONFIG_NONE          = 0x0000,      /**< Does not allow both notifications and indications */
200     GATT_CLIENT_CONFIG_NOTIFICATION  = 0x0001,      /**< Allows notifications  */
201     GATT_CLIENT_CONFIG_INDICATION    = 0x0002       /**< Allows indications  */
202 };
203 typedef uint16_t wiced_bt_gatt_client_char_config_t;     /**< GATT client config (see #wiced_bt_gatt_client_char_config_e) */
204 
205 /** characteristic descriptor: server configuration value */
206 enum wiced_bt_gatt_server_char_config_e
207 {
208     GATT_SERVER_CONFIG_NONE = 0x0000,     /**< No broadcast   */
209     GATT_SERVER_CONFIG_BROADCAST = 0x0001 /**< Broadcast      */
210 };
211 typedef uint16_t wiced_bt_gatt_server_char_config_t; /**< GATT server config (see #wiced_bt_gatt_server_char_config_e) */
212 
213 /**  GATT Characteristic Properties Mask */
214 enum wiced_bt_gatt_char_properties_e {
215     GATT_CHAR_PROPERTIES_BIT_BROADCAST      = (1 << 0),     /**< bit 0: Broadcast */
216     GATT_CHAR_PROPERTIES_BIT_READ           = (1 << 1),     /**< bit 1: Read */
217     GATT_CHAR_PROPERTIES_BIT_WRITE_NR       = (1 << 2),     /**< bit 2: Write (No Response) */
218     GATT_CHAR_PROPERTIES_BIT_WRITE          = (1 << 3),     /**< bit 3: Write */
219     GATT_CHAR_PROPERTIES_BIT_NOTIFY         = (1 << 4),     /**< bit 4: Notify */
220     GATT_CHAR_PROPERTIES_BIT_INDICATE       = (1 << 5),     /**< bit 5: Indicate */
221     GATT_CHAR_PROPERTIES_BIT_AUTH           = (1 << 6),     /**< bit 6: Authenticate */
222     GATT_CHAR_PROPERTIES_BIT_EXT_PROP       = (1 << 7)      /**< bit 7: Extended Properties */
223 };
224 typedef uint8_t wiced_bt_gatt_char_properties_t;            /**< GATT characteristic properties mask (see #wiced_bt_gatt_char_properties_e) */
225 
226 /** Authentication requirement */
227 enum wiced_bt_gatt_auth_req_e {
228     GATT_AUTH_REQ_NONE                  = 0,    /**< No Authentication Required */
229     GATT_AUTH_REQ_NO_MITM               = 1,    /**< Unauthenticated encryption (No MITM) */
230     GATT_AUTH_REQ_MITM                  = 2,    /**< Authenticated encryption (MITM) */
231     GATT_AUTH_REQ_SIGNED_NO_MITM        = 3,    /**< Signed Data (No MITM) */
232     GATT_AUTH_REQ_SIGNED_MITM           = 4     /**< Signed Data (MITM) */
233 };
234 typedef uint8_t wiced_bt_gatt_auth_req_t;   /**< GATT authentication requirement (see #wiced_bt_gatt_auth_req_e)*/
235 
236 
237 /** GATT Write Execute request flags */
238 enum wiced_bt_gatt_exec_flag_e {
239     GATT_PREPARE_WRITE_CANCEL      = 0x00,         /**< GATT_PREP_WRITE_CANCEL */
240     GATT_PREPARE_WRITE_EXEC        = 0x01          /**< GATT_PREP_WRITE_EXEC */
241 };
242 
243 #define GATT_PREP_WRITE_CANCEL GATT_PREPARE_WRITE_CANCEL  /**< See #GATT_PREPARE_WRITE_CANCEL */
244 #define GATT_PREP_WRITE_EXEC   GATT_PREPARE_WRITE_EXEC    /**< See #GATT_PREPARE_WRITE_EXEC */
245 typedef uint8_t   wiced_bt_gatt_exec_flag_t;    /**< GATT execute flag (see #wiced_bt_gatt_exec_flag_e) */
246 
247 /** Attribute read request */
248 /**< Opcode request, can be \ref GATT_REQ_READ, \ref GATT_REQ_READ_BLOB */
249 typedef struct
250 {
251     uint16_t  handle;        /**< Handle of attribute to read */
252     uint16_t  offset;        /**< Offset to read */
253 } wiced_bt_gatt_read_t;
254 
255 /**
256  * Response structure, containing the requested handle list for \ref GATT_RSP_READ_MULTI or \ref GATT_RSP_READ_MULTI_VAR_LENGTH
257  * commands
258  */
259 typedef struct wiced_bt_gatt_read_multiple_req_t_
260 {
261     uint8_t *p_handle_stream;    /**< Stream containing the handles */
262     int      num_handles;        /**< Number of handles pointed to by \p p_handle_stream */
263 } wiced_bt_gatt_read_multiple_req_t;
264 
265 /**
266  * Response structure for read multiple .
267  */
268 typedef struct
269 {
270     wiced_bt_gatt_opcode_t opcode;          /**< Response Opcode, can be \ref GATT_RSP_READ_MULTI or \ref GATT_RSP_READ_MULTI_VAR_LENGTH */
271     wiced_bt_gatt_read_multiple_req_t req;  /**< The request parameters used by client in the \ref wiced_bt_gatt_client_send_read_multiple API */
272     uint8_t *p_multi_rsp;                   /**< The response received from the peer.
273                                                  The response is valid only for \ref GATT_RSP_READ_MULTI.
274                                                  The peer response received for \ref GATT_RSP_READ_MULTI_VAR_LENGTH is split and sent to the app
275                                                  using the \ref GATTC_OPTYPE_READ_HANDLE event.
276                                             */
277     uint16_t rsp_len;                       /**< Length of the data pointed to by \p p_multi_rsp */
278 } wiced_bt_gatt_read_multiple_rsp_t;
279 
280 /** Parameters for GATT_READ_BY_TYPE and GATT_READ_CHAR_VALUE */
281 /**< Opcode request, can be \ref GATT_REQ_READ_MULTI, \ref GATT_REQ_READ_MULTI_VAR_LENGTH*/
282 typedef struct
283 {
284     uint16_t               s_handle;       /**< Starting handle */
285     uint16_t               e_handle;       /**< Ending handle */
286     wiced_bt_uuid_t        uuid;           /**< uuid */
287 } wiced_bt_gatt_read_by_type_t;
288 
289 /** Attribute header, used for GATT write operations, and read response callbacks */
290 typedef struct
291 {
292     uint16_t                    handle;                     /**< Attribute handle */
293     uint16_t                    offset;                     /**< Attribute value offset, ignored if not needed for a command */
294     uint16_t                    len;                        /**< Length of attribute value */
295     wiced_bt_gatt_auth_req_t    auth_req;                   /**< Authentication requirement (see @link wiced_bt_gatt_auth_req_e wiced_bt_gatt_auth_req_t @endlink) */
296 } wiced_bt_gatt_write_hdr_t;
297 
298 /** Attribute write request */
299 typedef struct
300 {
301     uint16_t      handle;     /**< Handle of attribute to write */
302     uint16_t      offset;     /**< Offset to write */
303     uint16_t      val_len;    /**< Value length */
304     uint8_t       *p_val;     /**< Value pointer */
305 } wiced_bt_gatt_write_req_t;
306 
307 /** Attribute handle confirmation, sent to app to indicate completion of server events on the server */
308 typedef struct
309 {
310     uint16_t               handle;    /**< first handle on which the notification or indication was sent */
311     wiced_bt_gatt_opcode_t opcode;    /**< \ref GATT_HANDLE_VALUE_NOTIF, \ref GATT_HANDLE_VALUE_IND, \ref GATT_HANDLE_VALUE_MULTI_NOTIF*/
312 } wiced_bt_gatt_req_conf_t;
313 
314 /** Attribute handle execution write request received on the client */
315 typedef struct
316 {
317     wiced_bt_gatt_exec_flag_t exec_write;   /**< GATT execute flag (see #wiced_bt_gatt_exec_flag_e) */
318 } wiced_bt_gatt_execute_write_req_t;
319 
320 /** Attribute information for GATT attribute requests types received on the server */
321 typedef union
322 {
323     wiced_bt_gatt_read_t              read_req;          /**< Parameters for \ref GATT_REQ_READ and \ref GATT_REQ_READ_BLOB */
324     wiced_bt_gatt_read_multiple_req_t read_multiple_req; /**< Parameters for the \ref GATT_REQ_READ_MULTI and \ref GATT_REQ_READ_MULTI_VAR_LENGTH */
325     wiced_bt_gatt_read_by_type_t      read_by_type;      /**< Parameters for \ref GATT_REQ_READ_BY_TYPE */
326     wiced_bt_gatt_write_req_t         write_req;         /**< Parameters for \ref GATT_REQ_WRITE and \ref GATT_REQ_PREPARE_WRITE*/
327     uint16_t                          remote_mtu;        /**< Parameters for \ref GATT_REQ_MTU */
328     wiced_bt_gatt_req_conf_t          confirm;           /**< Parameters for \ref GATT_HANDLE_VALUE_CONF,
329                                                               \ref GATT_HANDLE_VALUE_NOTIF, \ref GATT_HANDLE_VALUE_MULTI_NOTIF */
330     wiced_bt_gatt_execute_write_req_t exec_write_req;    /**< Parameters for GATT_REQ_EXECUTE_WRITE */
331 } wiced_bt_gatt_request_params_t;
332 
333 /** Discovery types */
334 enum wiced_bt_gatt_discovery_type_e
335 {
336     GATT_DISCOVER_SERVICES_ALL = 1,             /**< discover all services */
337     GATT_DISCOVER_SERVICES_BY_UUID,             /**< discover service by UUID */
338     GATT_DISCOVER_INCLUDED_SERVICES,            /**< discover an included service within a service */
339     GATT_DISCOVER_CHARACTERISTICS,              /**< discover characteristics of a service with/without type requirement */
340     GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS,   /**< discover characteristic descriptors of a character */
341     GATT_DISCOVER_MAX                           /**< maximum discovery types */
342 };
343 typedef uint8_t wiced_bt_gatt_discovery_type_t;    /**< GATT Discovery type (see #wiced_bt_gatt_discovery_type_e) */
344 
345 /** Parameters used in a GATT Discovery */
346 typedef struct
347 {
348     wiced_bt_uuid_t uuid;        /**< Service or Characteristic UUID */
349     uint16_t        s_handle;    /**< Start handle for range to search */
350     uint16_t        e_handle;    /**< End handle for range to search */
351 } wiced_bt_gatt_discovery_param_t;
352 
353 /**  Response data for read operations */
354 typedef struct
355 {
356     uint16_t handle;     /**< handle */
357     uint16_t len;        /**< length of response data or notification or indication data.
358                               In case of multiple notifications, it is total length of
359                               notification tuples pointed by p_data */
360     uint16_t offset;     /**< offset */
361     uint8_t  *p_data;    /**< attribute data. In case of multiple notifications,
362                               it is array of handle length and notification data tuples */
363 } wiced_bt_gatt_data_t;
364 
365 /** Client Operation Complete response data */
366 typedef union
367 {
368     wiced_bt_gatt_data_t    att_value;      /**< Response data for read operations (initiated using \ref wiced_bt_gatt_client_send_read_handle and
369                                                  \ref wiced_bt_gatt_client_send_read_by_type )
370                                                  (or) Notification or Indication data
371                                                  */
372     wiced_bt_gatt_read_multiple_rsp_t multi_rsp; /**< Data Response for \ref GATT_REQ_READ_MULTI  and
373                                                  * \ref GATT_REQ_READ_MULTI_VAR_LENGTH
374                                                  */
375     uint16_t                mtu;            /**< Response data for configuration operations */
376     uint16_t                handle;         /**< Response data for write operations (initiated using #wiced_bt_gatt_client_send_write) */
377 } wiced_bt_gatt_operation_complete_rsp_t;   /**< GATT client operation complete response type */
378 
379 /** GATT client operation type, used in client callback function
380 */
381 enum wiced_bt_gatt_optype_e
382 {
383     GATTC_OPTYPE_NONE  = 0,               /**< None      */
384     GATTC_OPTYPE_DISCOVERY,               /**< Discovery */
385     GATTC_OPTYPE_READ_HANDLE,             /**< Read handle or Read blob */
386     GATTC_OPTYPE_READ_BY_TYPE,            /**< Read by type operation   */
387     GATTC_OPTYPE_READ_MULTIPLE,           /**< Read multiple, or read multiple var length */
388     GATTC_OPTYPE_WRITE_WITH_RSP,          /**< Write with response */
389     GATTC_OPTYPE_WRITE_NO_RSP,            /**< Write no response   */
390     GATTC_OPTYPE_PREPARE_WRITE,           /**< Prepare Write */
391     GATTC_OPTYPE_EXECUTE_WRITE,           /**< Execute Write */
392     GATTC_OPTYPE_CONFIG_MTU,              /**< Configure MTU */
393     GATTC_OPTYPE_NOTIFICATION,            /**< Notification */
394     GATTC_OPTYPE_INDICATION,              /**< Indication */
395 };
396 
397 /** GATT Client Operation Codes */
398 typedef uint8_t wiced_bt_gatt_optype_t; /**< GATT client operation type (see #wiced_bt_gatt_optype_e) */
399 
400 /** GATT caching status of the peer(client) */
401 enum wiced_bt_gatt_caching_status_e
402 {
403     GATT_PEER_CLIENT_CACHE_CHANGE_AWARE   = 0,     /**< Peer client is cache aware   */
404     GATT_PEER_CLIENT_CACHE_CHANGE_UNAWARE = 1,     /**< Peer client is cache unaware */
405     GATT_PEER_CLIENT_CACHE_READY_TO_BE_AWARE = 2   /**< Peer client is reading the database hash */
406 };
407 
408 typedef uint8_t wiced_bt_gatt_caching_status_t; /**< GATT peer caching status (see #wiced_bt_gatt_caching_status_e) */
409 
410 /** characteristic declaration */
411 typedef struct
412 {
413     wiced_bt_gatt_char_properties_t characteristic_properties;  /**< characteristic properties (see @link wiced_bt_gatt_char_properties_e wiced_bt_gatt_char_properties_t @endlink) */
414     uint16_t                        val_handle;                 /**< characteristic value attribute handle */
415     uint16_t                        handle;                     /**< characteristic declaration handle */
416     wiced_bt_uuid_t                 char_uuid;                  /**< characteristic UUID type */
417 } wiced_bt_gatt_char_declaration_t;
418 
419 /** GATT group value */
420 typedef struct
421 {
422     wiced_bt_uuid_t service_type;   /**< group type */
423     uint16_t        s_handle;       /**< starting handle of the group */
424     uint16_t        e_handle;       /**< ending handle of the group */
425 } wiced_bt_gatt_group_value_t;
426 
427 
428 /** included service attribute value */
429 typedef struct
430 {
431     wiced_bt_uuid_t service_type;   /**< included service UUID */
432     uint16_t        handle;         /**< included service handle */
433     uint16_t        s_handle;       /**< included service starting handle */
434     uint16_t        e_handle;       /**< included service ending handle */
435 } wiced_bt_gatt_included_service_t;
436 
437 /** characteristic descriptor information */
438 typedef struct
439 {
440     wiced_bt_uuid_t type;      /**< descriptor UUID type */
441     uint16_t        handle;    /**< descriptor attribute handle */
442 } wiced_bt_gatt_char_descr_info_t;
443 
444 
445 /**
446  * Discovery result data
447  * Use  GATT_DISCOVERY_RESULT_SERVICE_* or GATT_DISCOVERY_RESULT_CHARACTERISTIC_* macros to parse discovery data)
448  */
449 typedef union
450 {
451     wiced_bt_gatt_included_service_t    included_service_type;      /**< Result for GATT_DISCOVER_INCLUDED_SERVICES */
452     wiced_bt_gatt_group_value_t         group_value;                /**< Result for GATT_DISCOVER_SERVICES_ALL or GATT_DISCOVER_SERVICES_BY_UUID  */
453     wiced_bt_gatt_char_declaration_t    characteristic_declaration; /**< Result for GATT_DISCOVER_CHARACTERISTICS */
454     wiced_bt_gatt_char_descr_info_t     char_descr_info;            /**< Result for GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS */
455 } wiced_bt_gatt_discovery_data_t;
456 
457 
458 /*****************************************************************************
459  *  GATT Database Defintions
460  *****************************************************************************/
461 /** Attribute Permission bits (see Core Specification 5.2, Vol 3, Part F, 3.2.5) */
462 #define GATTDB_PERM_NONE                             (0x00)      /**<  No permissions set */
463 #define GATTDB_PERM_VARIABLE_LENGTH                  (0x1 << 0)  /**<  Attribute has variable length (not used by stack) */
464 #define GATTDB_PERM_READABLE                         (0x1 << 1)  /**<  Attribute is readable */
465 #define GATTDB_PERM_WRITE_CMD                        (0x1 << 2)  /**<  Attribute can be written using \ref GATT_CMD_WRITE */
466 #define GATTDB_PERM_WRITE_REQ                        (0x1 << 3)  /**<  Attribute can be written using \ref GATT_REQ_WRITE */
467 #define GATTDB_PERM_AUTH_READABLE                    (0x1 << 4)  /**<  Attribute can be read if the connection is encrypted or
468                                                                        authenticated
469                                                                        */
470 #define GATTDB_PERM_RELIABLE_WRITE                   (0x1 << 5)  /**<  Attribute supports reliable writes */
471 #define GATTDB_PERM_AUTH_WRITABLE                    (0x1 << 6)  /**<  Attribute can be written if the connection is encrypted or
472                                                                        authenticated */
473 
474 /**<  Permission mask for writable characteristics */
475 #define GATTDB_PERM_WRITABLE (GATTDB_PERM_WRITE_CMD | GATTDB_PERM_WRITE_REQ | GATTDB_PERM_AUTH_WRITABLE) /**<Writable permissions*/
476 #define GATTDB_PERM_MASK                             (0x7f)  /**< All the permission bits. */
477 #define GATTDB_PERM_SERVICE_UUID_128                 (0x1 << 7)  /**< Set for 128 bit services/characteristic UUIDs, check
478                                                                        @ref GATT_DB_MACROS "Service and Characteristic macros"
479                                                                       */
480 
481 /**
482  * Attribute permission bit masks.
483  */
484 typedef uint8_t wiced_bt_gatt_permission_t;
485 
486 #define GATTDB_CHAR_PROP_BROADCAST                  (0x1 << 0)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
487 #define GATTDB_CHAR_PROP_READ                       (0x1 << 1)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
488 #define GATTDB_CHAR_PROP_WRITE_NO_RESPONSE          (0x1 << 2)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
489 #define GATTDB_CHAR_PROP_WRITE                      (0x1 << 3)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
490 #define GATTDB_CHAR_PROP_NOTIFY                     (0x1 << 4)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
491 #define GATTDB_CHAR_PROP_INDICATE                   (0x1 << 5)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
492 #define GATTDB_CHAR_PROP_AUTHD_WRITES               (0x1 << 6)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
493 #define GATTDB_CHAR_PROP_EXTENDED                   (0x1 << 7)  /**< GATT Characteristic Properties (see Vol 3, Part G, 3.3.1.1) */
494 
495 #define GATTDB_CHAR_EXTENDED_PROP_RELIABLE             (0x1 << 0)  /**< GATT Characteristic Extended Properties (see Vol 3, Part G, 3.3.3.1) */
496 #define GATTDB_CHAR_EXTENDED_PROP_WRITABLE_AUXILIARIES (0x1 << 1)  /**< GATT Characteristic Extended Properties (see Vol 3, Part G, 3.3.3.1) */
497 
498 /** Conversion macros */
499 #define BIT16_TO_8( val ) \
500     (uint8_t)(  (val)        & 0xff),/* LSB */ \
501     (uint8_t)(( (val) >> 8 ) & 0xff) /* MSB */
502 
503 #define GATTDB_UUID16_SIZE                 2   /**< UUID lengths */
504 #define GATTDB_UUID128_SIZE                16  /**< UUID lengths */
505 
506 /**
507  * @anchor GATT_DB_MACROS
508  * @name Service and Characteristic macros
509  * @{
510  */
511 
512 /** Macro to assist 16 bit primary service declaration */
513 #define PRIMARY_SERVICE_UUID16(handle, service)  \
514     BIT16_TO_8((uint16_t)(handle)), \
515     GATTDB_PERM_READABLE, \
516     4, \
517     BIT16_TO_8((GATT_UUID_PRI_SERVICE)), \
518     BIT16_TO_8((service))
519 
520 /** Macro to assist 128 bit primary service declaration */
521 #define PRIMARY_SERVICE_UUID128(handle, service)  \
522     BIT16_TO_8((uint16_t)(handle)), \
523     GATTDB_PERM_READABLE, \
524     18, \
525     BIT16_TO_8(GATT_UUID_PRI_SERVICE), \
526     service
527 
528 /** Macro to assist 16 bit secondary service declaration */
529 #define SECONDARY_SERVICE_UUID16(handle, service)  \
530     BIT16_TO_8((uint16_t)(handle)), \
531     GATTDB_PERM_READABLE, \
532     4, \
533     BIT16_TO_8((GATT_UUID_SEC_SERVICE)), \
534     BIT16_TO_8((service))
535 
536 /** Macro to assist 128 bit secondary service declaration */
537 #define SECONDARY_SERVICE_UUID128(handle, service)  \
538     BIT16_TO_8((uint16_t)(handle)), \
539     GATTDB_PERM_READABLE, \
540     18, \
541     BIT16_TO_8(GATT_UUID_SEC_SERVICE), \
542     service
543 
544 /** Macro to assist included service declaration */
545 #define INCLUDE_SERVICE_UUID16(handle, service_handle, end_group_handle, service)  \
546     BIT16_TO_8((uint16_t)(handle)), \
547     GATTDB_PERM_READABLE, \
548     8, \
549     BIT16_TO_8(GATT_UUID_INCLUDE_SERVICE), \
550     BIT16_TO_8(service_handle), \
551     BIT16_TO_8(end_group_handle), \
552     BIT16_TO_8(service)
553 
554 /** Macro to assist 128 bit included service declaration */
555 #define INCLUDE_SERVICE_UUID128(handle, service_handle, end_group_handle)\
556     BIT16_TO_8((uint16_t)(handle)), \
557     GATTDB_PERM_READABLE, \
558     6, \
559     BIT16_TO_8(GATT_UUID_INCLUDE_SERVICE), \
560     BIT16_TO_8(service_handle), \
561     BIT16_TO_8(end_group_handle)
562 
563 /** Macro to assist readable 16 bit characteristic declaration */
564 #define CHARACTERISTIC_UUID16(handle, handle_value, uuid, properties, permission) \
565     BIT16_TO_8((uint16_t)(handle)), \
566     GATTDB_PERM_READABLE, \
567     0x07, \
568     BIT16_TO_8(GATT_UUID_CHAR_DECLARE), \
569     (uint8_t)(properties), \
570     BIT16_TO_8((uint16_t)(handle_value)), \
571     BIT16_TO_8(uuid), \
572     BIT16_TO_8((uint16_t)(handle_value)), \
573     (uint8_t)(permission), \
574     (uint8_t)(GATTDB_UUID16_SIZE), \
575     BIT16_TO_8(uuid)
576 
577 /** Macro to assist readable 128 bit characteristic declaration */
578 #define CHARACTERISTIC_UUID128(handle, handle_value, uuid, properties, permission) \
579     BIT16_TO_8((uint16_t)(handle)), \
580     GATTDB_PERM_READABLE, \
581     21, \
582     BIT16_TO_8(GATT_UUID_CHAR_DECLARE), \
583     (uint8_t)(properties), \
584     BIT16_TO_8((uint16_t)(handle_value)), \
585     uuid, \
586     BIT16_TO_8((uint16_t)(handle_value)), \
587     (uint8_t)(permission | GATTDB_PERM_SERVICE_UUID_128), \
588     (uint8_t)(GATTDB_UUID128_SIZE), \
589     uuid
590 
591 /** Macro to assist writable 16 bit characteristic declaration */
592 #define CHARACTERISTIC_UUID16_WRITABLE(handle, handle_value, uuid, properties, permission) \
593     BIT16_TO_8((uint16_t)(handle)), \
594     GATTDB_PERM_READABLE, \
595     0x07, \
596     BIT16_TO_8(GATT_UUID_CHAR_DECLARE), \
597     (uint8_t)(properties), \
598     BIT16_TO_8((uint16_t)(handle_value)), \
599     BIT16_TO_8(uuid), \
600     BIT16_TO_8((uint16_t)(handle_value)), \
601     (uint8_t)(permission), \
602     (uint8_t)(GATTDB_UUID16_SIZE), \
603     (uint8_t)(0), \
604     BIT16_TO_8(uuid)
605 
606 /** Macro to assist writable 128 bit characteristic declaration */
607 #define CHARACTERISTIC_UUID128_WRITABLE(handle, handle_value, uuid, properties, permission) \
608     BIT16_TO_8((uint16_t)(handle)), \
609     GATTDB_PERM_READABLE, \
610     21, \
611     BIT16_TO_8(GATT_UUID_CHAR_DECLARE), \
612     (uint8_t)(properties), \
613     BIT16_TO_8((uint16_t)(handle_value)), \
614     uuid, \
615     BIT16_TO_8((uint16_t)(handle_value)), \
616     (uint8_t)(permission | GATTDB_PERM_SERVICE_UUID_128), \
617     (uint8_t)(GATTDB_UUID128_SIZE), \
618     (uint8_t)(0), \
619     uuid
620 
621 /** Macro to assist writable 16 bit descriptor declaration */
622 #define CHAR_DESCRIPTOR_UUID16_WRITABLE(handle, uuid, permission) \
623     BIT16_TO_8((uint16_t)(handle)), \
624     (uint8_t)(permission), \
625     (uint8_t)(GATTDB_UUID16_SIZE), \
626     (uint8_t)(0), \
627     BIT16_TO_8(uuid)
628 
629 /** Macro to assist readable 16 bit descriptor declaration */
630 #define CHAR_DESCRIPTOR_UUID16(handle, uuid, permission) \
631     BIT16_TO_8((uint16_t)(handle)), \
632     (uint8_t)(permission), \
633     (uint8_t)(GATTDB_UUID16_SIZE), \
634     BIT16_TO_8(uuid)
635 
636 /** Macro to assist writable 128 bit descriptor declaration */
637 #define CHAR_DESCRIPTOR_UUID128_WRITABLE(handle, uuid, permission) \
638     BIT16_TO_8((uint16_t)(handle)), \
639     (uint8_t)(permission | GATTDB_PERM_SERVICE_UUID_128), \
640     (uint8_t)(GATTDB_UUID128_SIZE), \
641     (uint8_t)(0), \
642     uuid
643 
644 /** Macro to assist readable 128 bit descriptor declaration */
645 #define CHAR_DESCRIPTOR_UUID128(handle, uuid, permission) \
646     BIT16_TO_8((uint16_t)(handle)), \
647     (uint8_t)(permission | GATTDB_PERM_SERVICE_UUID_128), \
648     (uint8_t)(GATTDB_UUID128_SIZE), \
649     uuid
650 
651 /** Macro to assist extended properties declaration */
652 #define CHAR_DESCRIPTOR_EXTENDED_PROPERTIES(handle, ext_properties)\
653     BIT16_TO_8((uint16_t)(handle)), \
654     (uint8_t)(GATTDB_PERM_READABLE), \
655     (uint8_t)(GATTDB_UUID16_SIZE + 1), \
656     BIT16_TO_8(GATT_UUID_CHAR_EXT_PROP),\
657     (uint8_t)(ext_properties)
658 
659 /** @} GATT_DB_MACROS */
660 
661 /**
662 * Format of the value of a characteristic.
663 * Enumeration types for the \sa UUID_DESCRIPTOR_CHARACTERISTIC_PRESENTATION_FORMAT descriptor
664 */
665 enum wiced_bt_gatt_format_e
666 {
667     GATT_CHAR_PRESENTATION_FORMAT_RES,            /* rfu */
668     GATT_CHAR_PRESENTATION_FORMAT_BOOL,           /* 0x01 BOOL32 */
669     GATT_CHAR_PRESENTATION_FORMAT_2BITS,          /* 0x02 2 bit */
670     GATT_CHAR_PRESENTATION_FORMAT_NIBBLE,         /* 0x03 nibble */
671     GATT_CHAR_PRESENTATION_FORMAT_UINT8,          /* 0x04 uint8 */
672     GATT_CHAR_PRESENTATION_FORMAT_UINT12,         /* 0x05 uint12 */
673     GATT_CHAR_PRESENTATION_FORMAT_UINT16,         /* 0x06 uint16 */
674     GATT_CHAR_PRESENTATION_FORMAT_UINT24,         /* 0x07 uint24 */
675     GATT_CHAR_PRESENTATION_FORMAT_UINT32,         /* 0x08 uint32 */
676     GATT_CHAR_PRESENTATION_FORMAT_UINT48,         /* 0x09 uint48 */
677     GATT_CHAR_PRESENTATION_FORMAT_UINT64,         /* 0x0a uint64 */
678     GATT_CHAR_PRESENTATION_FORMAT_UINT128,        /* 0x0B uint128 */
679     GATT_CHAR_PRESENTATION_FORMAT_SINT8,          /* 0x0C signed 8 bit integer */
680     GATT_CHAR_PRESENTATION_FORMAT_SINT12,         /* 0x0D signed 12 bit integer */
681     GATT_CHAR_PRESENTATION_FORMAT_SINT16,         /* 0x0E signed 16 bit integer */
682     GATT_CHAR_PRESENTATION_FORMAT_SINT24,         /* 0x0F signed 24 bit integer */
683     GATT_CHAR_PRESENTATION_FORMAT_SINT32,         /* 0x10 signed 32 bit integer */
684     GATT_CHAR_PRESENTATION_FORMAT_SINT48,         /* 0x11 signed 48 bit integer */
685     GATT_CHAR_PRESENTATION_FORMAT_SINT64,         /* 0x12 signed 64 bit integer */
686     GATT_CHAR_PRESENTATION_FORMAT_SINT128,        /* 0x13 signed 128 bit integer */
687     GATT_CHAR_PRESENTATION_FORMAT_FLOAT32,        /* 0x14 float 32 */
688     GATT_CHAR_PRESENTATION_FORMAT_FLOAT64,        /* 0x15 float 64*/
689     GATT_CHAR_PRESENTATION_FORMAT_SFLOAT,         /* 0x16 IEEE-11073 16 bit SFLOAT */
690     GATT_CHAR_PRESENTATION_FORMAT_FLOAT,          /* 0x17 IEEE-11073 32 bit SFLOAT */
691     GATT_CHAR_PRESENTATION_FORMAT_DUINT16,        /* 0x18 IEEE-20601 format */
692     GATT_CHAR_PRESENTATION_FORMAT_UTF8S,          /* 0x19 UTF-8 string */
693     GATT_CHAR_PRESENTATION_FORMAT_UTF16S,         /* 0x1a UTF-16 string */
694     GATT_CHAR_PRESENTATION_FORMAT_STRUCT,         /* 0x1b Opaque structure*/
695     GATT_CHAR_PRESENTATION_FORMAT_MAX             /* 0x1c or above reserved */
696 };
697 typedef uint8_t wiced_bt_gatt_format_t;/**< characteristic format specifiers (see #wiced_bt_gatt_format_e) */
698 
699 /**
700  * .Enumeration of known Client Supported Feature Bit assignments of the \ref wiced_bt_gatt_client_supported_features_t
701  */
702 enum t_gatt_csf_assignments {
703     GATT_CSF_ROBUST_CACHING = 0, /**< Client supports Robust Caching */
704     GATT_CSF_EATT = 1,           /**< Client supports Enhanaced ATT bearers */
705     GATT_CSF_MULTIPLE_HANDLE_VALUE_NOTIFICATIONS = 2 /**< Client supports receiving multiple handle value notifications */
706 };
707 
708 /** macro to determine GATT Client Support features */
709 #define GATT_IS_CSF_FEATURE_SUPPORTED(csf, m) (csf[(m)/8] & (1 << (m)%8))
710 
711 /** GATT Client Support features */
712 typedef uint8_t wiced_bt_gatt_csf_bits_t;
713 
714 /**< Data Receive Buffer
715  *  Data Receive Buffer or DRB is a memory area of type #tDRB and size(channel MTU size + #DRB_OVERHEAD_SIZE).
716  * The DRB buffer is allocated by the application during channel creation.
717  * The allocated DRBs are passed to the GATT layer when establishing an EATT connection.
718  *       a) As a connection initiator with \ref wiced_bt_gatt_eatt_connect
719  *       b) As a connection responder with \ref wiced_bt_gatt_eatt_connection_response_t
720  * Application has to allocate one DRB per GATT bearer of size equal to that of desired local MTU.
721 */
722 typedef tDRB * wiced_bt_eatt_drbs[EATT_CHANNELS_PER_TRANSACTION];                /**< list of Data Receive Blocks */
723 
724 /** Discovery result (used by GATT_DISCOVERY_RESULT_EVT notification) */
725 typedef struct
726 {
727     uint16_t                        conn_id;            /**< ID of the connection */
728     wiced_bt_gatt_discovery_type_t  discovery_type;     /**< Discovery type (see @link wiced_bt_gatt_discovery_type_e wiced_bt_gatt_discovery_type_t @endlink) */
729     wiced_bt_gatt_discovery_data_t  discovery_data;     /**< Discovery data  */
730 } wiced_bt_gatt_discovery_result_t;
731 
732 /** Discovery Complete (used by GATT_DISCOVERY_CPLT_EVT notification) */
733 typedef struct
734 {
735     uint16_t                        conn_id;         /**< ID of the connection */
736     wiced_bt_gatt_discovery_type_t  discovery_type;  /**< Discovery type (see @link wiced_bt_gatt_discovery_type_e wiced_bt_gatt_discovery_type_t @endlink) */
737     wiced_bt_gatt_status_t          status;          /**< Status of the discovery operation */
738 } wiced_bt_gatt_discovery_complete_t;
739 
740 
741 /** Response to read/write/disc/config operations (used by GATT_OPERATION_CPLT_EVT notification) */
742 typedef struct
743 {
744     uint16_t                                conn_id;            /**< ID of the connection */
745     wiced_bt_gatt_optype_t                  op;                 /**< Type of operation completed (see @link wiced_bt_gatt_optype_e wiced_bt_gatt_optype_t @endlink) */
746     wiced_bt_gatt_status_t                  status;             /**< Status of clcb_operation */
747     uint8_t                                 pending_events;     /**< Number of pending events, used to initiate next read */
748     wiced_bt_gatt_operation_complete_rsp_t  response_data;      /**< Response data (dependent on optype) */
749 } wiced_bt_gatt_operation_complete_t;
750 
751 /** GATT connection status (used by GATT_CONNECTION_STATUS_EVT notification) */
752 typedef struct
753 {
754     uint8_t                        *bd_addr;     /**< Remote device address */
755     wiced_bt_ble_address_type_t     addr_type;   /**< Remmote device address type */
756     uint16_t                        conn_id;     /**< ID of the connection */
757     wiced_bool_t                    connected;   /**< TRUE if connected, FALSE if disconnected */
758     wiced_bt_gatt_disconn_reason_t  reason;      /**< Reason code (see @link wiced_bt_gatt_disconn_reason_e wiced_bt_gatt_disconn_reason_t @endlink) */
759     wiced_bt_transport_t            transport;   /**< Transport type of the connection */
760     wiced_bt_dev_role_t             link_role;   /**< Link role on this connection */
761 } wiced_bt_gatt_connection_status_t;
762 
763 /** GATT attribute request (used by GATT_ATTRIBUTE_REQUEST_EVT notification) */
764 typedef struct
765 {
766     uint16_t                        conn_id;       /**< ID of the connection */
767     uint16_t                        len_requested; /**< Max size of data requested as response */
768     wiced_bt_gatt_opcode_t          opcode;        /**< GATT request opcode */
769     wiced_bt_gatt_request_params_t  data;          /**< Information about attribute being request (dependent on request type) */
770 } wiced_bt_gatt_attribute_request_t;
771 
772 /** GATT channel congestion/uncongestion (used by GATT_CONGESTION_EVT notification) */
773 typedef struct
774 {
775     uint16_t       conn_id;    /**< ID of the connection */
776     wiced_bool_t   congested;  /**< congestion state */
777 } wiced_bt_gatt_congestion_event_t;
778 
779 /**
780  * App context is returned back to application in event GATT_APP_BUFFER_TRANSMITTED_EVT on transmission of
781  * application data buffer
782  *
783 */
784 typedef void * wiced_bt_gatt_app_context_t;
785 
786 /**
787  * Structure to return the application buffer and application buffer context given to the stack in various APIs
788  * The application is expected to use this event to free allocated memory used for sending client requests or
789  * responding to client requests.
790  *
791  * On client this is the buffer sent to the stack in
792  *     - \sa wiced_bt_gatt_client_send_write
793  * On server this is the buffer sent to the stack in
794  *     - \sa GATT_GET_RESPONSE_BUFFER_EVT using \ref wiced_bt_gatt_app_response_buffer_t
795  *     - \sa wiced_bt_gatt_server_send_indication
796  *     - \sa wiced_bt_gatt_server_send_notification
797  *     - \sa wiced_bt_gatt_server_send_multiple_notifications
798  *     - \sa wiced_bt_gatt_server_send_read_handle_rsp
799  *     - \sa wiced_bt_gatt_server_send_read_by_type_rsp
800  *     - \sa wiced_bt_gatt_server_send_read_multiple_rsp
801  *     - \sa wiced_bt_gatt_server_send_prepare_write_rsp
802  *
803  * @note Typically the application context is expected to be a function/hint (used by the application)
804  * to allow it to free/deallocate the memory passed to the stack when writing data to the stack.
805  * For e.g, when issueing wiced_bt_gatt_client_send_write, the app can allocate p_app_write_buffer and set the
806  * p_app_ctxt to the appropriate free function. This memory allocated for this transaction has to be freed
807  * when returned by the stack in the GATT_APP_BUFFER_TRANSMITTED_EVT.
808  * The process is made simple since in this case as
809  *  \p p_app_data is the application p_app_write_buffer and
810  *  \p p_app_ctxt is the application free function/hint
811  * @note \ref GATT_APP_BUFFER_TRANSMITTED_EVT is expected to be used only to free app memory as described. The
812  * next transaction should be attempted on the respective \ref GATT_OPERATION_CPLT_EVT
813  */
814 typedef struct
815 {
816     uint8_t *p_app_data;                    /**< Application data buffer    */
817     wiced_bt_gatt_app_context_t p_app_ctxt;  /**< Application context for \p p_app_data */
818 } wiced_bt_gatt_buffer_transmitted_t;
819 
820 
821 /**
822  * Structure to hold the response buffer and the application context for that buffer.
823  * Application fills the structure members with a pointer to send a server response.
824  *
825  * @note The application may free/release/deallocate the \p p_app_rsp_buffer pointer
826  * on receiving a GATT_APP_BUFFER_TRANSMITTED_EVT with
827  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data = \p p_app_rsp_buffer and
828  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt   = \p p_app_ctxt
829  *
830  */
831 typedef struct
832 {
833     uint8_t                    *p_app_rsp_buffer;   /**< Pointer to assign the buffer allocated by the app*/
834     wiced_bt_gatt_app_context_t p_app_ctxt;         /**< Application context for \p p_app_rsp_buffer*/
835 } wiced_bt_gatt_app_response_buffer_t;
836 
837 /**
838  * Structure to get a response buffer of the \p len_requested and the application context for that buffer
839  */
840 typedef struct
841 {
842     uint16_t len_requested;                      /**< Size of the requested buffer to be returned in \p p_buffer */
843     wiced_bt_gatt_app_response_buffer_t buffer;  /**< Structure to be filled by the app to return the allocated buffer */
844 } wiced_bt_gatt_buffer_request_t;
845 
846 
847 /** Structures for GATT event notifications */
848 typedef union
849 {
850     wiced_bt_gatt_discovery_result_t        discovery_result;   /**< Data for GATT_DISCOVERY_RESULT_EVT */
851     wiced_bt_gatt_discovery_complete_t      discovery_complete; /**< Data for GATT_DISCOVERY_CPLT_EVT */
852     wiced_bt_gatt_operation_complete_t      operation_complete; /**< Data for GATT_OPERATION_CPLT_EVT */
853     wiced_bt_gatt_connection_status_t       connection_status;  /**< Data for GATT_CONNECTION_STATUS_EVT */
854     wiced_bt_gatt_attribute_request_t       attribute_request;  /**< Data for GATT_ATTRIBUTE_REQUEST_EVT */
855     wiced_bt_gatt_congestion_event_t        congestion;         /**< Data for GATT_CONGESTION_EVT */
856     wiced_bt_gatt_buffer_request_t          buffer_request;     /**< Data for GATT_GET_RESPONSE_BUFFER_EVT */
857     wiced_bt_gatt_buffer_transmitted_t      buffer_xmitted;     /**< Data for GATT_APP_BUFFER_TRANSMITTED_EVT */
858 } wiced_bt_gatt_event_data_t;
859 
860 /** GATT events */
861 typedef enum
862 {
863     /**
864      * GATT connection status change.
865      * Event data: \ref wiced_bt_gatt_event_data_t.connection_status
866      */
867     GATT_CONNECTION_STATUS_EVT,        /* 0, 0x0*/
868 
869     /**
870      * GATT client events, indication completion of app initiated client operations
871      * Check specific client APIs for more details.
872      * Applications can initiate the next client operation for the specific ATT bearer
873      * on receiving this event.
874      * Event data: \ref wiced_bt_gatt_event_data_t.operation_complete
875      */
876     GATT_OPERATION_CPLT_EVT,           /* 1, 0x1 */
877 
878     /**
879      * GATT attribute discovery result.
880      * Event data: \ref wiced_bt_gatt_event_data_t.discovery_result
881      */
882     GATT_DISCOVERY_RESULT_EVT,         /* 2, 0x2 */
883 
884     /**
885      * GATT attribute discovery complete.
886      * Event data: \ref wiced_bt_gatt_event_data_t.discovery_complete
887      */
888     GATT_DISCOVERY_CPLT_EVT,           /* 3, 0x3 */
889 
890     /**
891      * GATT attribute request (from remote client).
892      * Event data: \ref wiced_bt_gatt_event_data_t.attribute_request
893      */
894     GATT_ATTRIBUTE_REQUEST_EVT,        /* 4, 0x4 */
895 
896     /**
897      * GATT congestion (running low in tx buffers).
898      * Event data: \ref wiced_bt_gatt_event_data_t.congestion
899      * @note Handling #GATT_CONGESTION_EVT
900      * Applications may receive a GATT_CONGESTION_EVT to indicate a congestion
901      * at the GATT layer.
902      * On reception of this event with the #wiced_bt_gatt_congestion_event_t.congested variable set to #WICED_TRUE
903      * the application should not attempt to send any further requests or commands to the GATT layer
904      * as these will return with error code of #WICED_BT_GATT_CONGESTED
905      * On reception of the #GATT_CONGESTION_EVT with the
906      * #wiced_bt_gatt_congestion_event_t.congested variable set to #WICED_FALSE
907      * the application may resume sending any further APIs
908      */
909     GATT_CONGESTION_EVT,               /* 5, 0x5 */
910 
911     /**
912      * GATT buffer request, typically sized to max of bearer mtu - 1,
913      * Event data: \ref wiced_bt_gatt_event_data_t.buffer_request
914      */
915     GATT_GET_RESPONSE_BUFFER_EVT,      /* 6, 0x6 */
916 
917     /**
918     * GATT buffer transmitted event, indicates that the data in
919     * \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data has been transmitted and may
920     * be released/freed by the application using the application provided context in
921     * \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt
922     *
923     * Event data: \ref wiced_bt_gatt_event_data_t.buffer_xmitted
924     */
925     GATT_APP_BUFFER_TRANSMITTED_EVT,   /* 7, 0x7 */
926 } wiced_bt_gatt_evt_t;
927 
928 
929 
930 /**
931  * GATT event notification callback
932  *
933  * Callback for GATT event notifications
934  * Registered using wiced_bt_gatt_register()
935  *
936  * @param[in] event             : Event ID
937  * @param[in] p_event_data      : Event data
938  *
939  * @return Status of event handling
940 */
941 typedef wiced_bt_gatt_status_t wiced_bt_gatt_cback_t(wiced_bt_gatt_evt_t event,
942     wiced_bt_gatt_event_data_t *p_event_data);
943 
944 /** GATT attribute value included in central role DB*/
945 typedef union
946 {
947     uint8_t addr_resolution;  /**< binary value to indicate if addr. resolution is supported */
948 } wiced_bt_gatt_gap_ble_attr_value_t;
949 
950 #pragma pack(1)
951 /**
952  * Structure used by wiced_bt_gattdb APIS, to parse GATTDB
953  */
954 typedef WICED_BT_STRUCT_PACKED wiced_gattdb_entry_s
955 {
956     uint16_t handle;        /**< attribute Handle  */
957     uint8_t  perm;          /**< attribute permission.*/
958     uint8_t  len;           /**< attribute length . It excludes the header.*/
959 } wiced_gattdb_entry_t;
960 #pragma pack()
961 
962 /** GATT connection request (used by GATT_EATT_CONNECTION_INDICATION_EVT notification)
963 * To be returned by application for accepting/rejecting the connection with the
964 * wiced_bt_eatt_connection_response
965 */
966 typedef struct
967 {
968     wiced_bt_device_address_t  bdaddr;                 /**< Bluetooth address of remote device */
969     wiced_bt_transport_t       transport;              /**< Transport type of the connection */
970     wiced_bt_ecrb_cid_list_t    lcids;                  /**< List of the l2cap cids (channel ids) */
971     uint16_t                   mtu;                    /**< Peer MTU */
972     uint8_t                    trans_id;               /**< Transaction id for the connection */
973 } wiced_bt_gatt_eatt_connection_indication_event_t;
974 
975 /**
976  * @brief structure to be used to respond to received EATT connection request
977  *
978  */
979 typedef struct
980 {
981     wiced_bt_gatt_status_t response;    /**< Application response to be sent for the connection request */
982     uint16_t               our_rx_mtu;  /**< Application MTU (Maximum Transmission Unit) to be used for the connection */
983     uint16_t               our_rx_mps;  /**< Application MPS (Maximum Protocol Size) to be used for the connection */
984     wiced_bt_eatt_drbs     ppDRBs;      /**< Data Receive Buffers of our_rx_mtu size to be used for receiving incoming data */
985 } wiced_bt_gatt_eatt_connection_response_t;
986 
987 /**
988 * @brief structure used to deliver the confirmation status of the requested EATT connection
989 */
990 typedef struct
991 {
992     wiced_bt_device_address_t  bdaddr;     /**< Bluetooth address of remote device */
993     wiced_bt_transport_t       transport;  /**< Transport type of the connection */
994     uint16_t                   conn_id;    /**< conn_id of the connection */
995     uint16_t                   mtu;        /**< Peer MTU */
996     uint16_t                   result;     /**< Result of the connection for the conn_id in
997                                            * \ref wiced_bt_gatt_eatt_connection_confirmation_event_t.conn_id
998                                            */
999 } wiced_bt_gatt_eatt_connection_confirmation_event_t;
1000 
1001 
1002 /**
1003  * @brief Function callbacks for EATT.
1004  */
1005 typedef void (*wiced_bt_gatt_eatt_on_connect_ind_t)(wiced_bt_gatt_eatt_connection_indication_event_t *p_ind);   /**< callback upon GATT EATT connection */
1006 typedef void (*wiced_bt_gatt_eatt_on_connect_complete_t)(wiced_bt_gatt_eatt_connection_confirmation_event_t * p_cfm); /**< callback upon GATT EATT connection complete */
1007 typedef void (*wiced_bt_gatt_eatt_on_reconfigure_ind_t)(uint16_t conn_id, uint16_t mtu, uint16_t mps);  /**< callback upon GATT EATT reconnection complete */
1008 typedef void (*wiced_bt_gatt_eatt_release_drb_t)(tDRB *p_drb);   /**< callback upon releasing the DRB */
1009 
1010 /** callbacks for GATT EATT event notifications */
1011 typedef struct
1012 {
1013     wiced_bt_gatt_eatt_on_connect_ind_t      eatt_connect_ind_cb;     /**< callback upon GATT EATT connection */
1014     wiced_bt_gatt_eatt_on_connect_complete_t eatt_connect_cmpl_cb;    /**< callback upon GATT EATT connection complete */
1015     wiced_bt_gatt_eatt_on_reconfigure_ind_t  eatt_reconfigure_ind_cb; /**< callback upon GATT EATT reconnection complete */
1016     wiced_bt_gatt_eatt_release_drb_t         eatt_release_drb;        /**< callback upon releasing the DRB */
1017 } wiced_bt_gatt_eatt_callbacks_t;
1018 
1019 
1020 /**
1021  * @}
1022  */
1023 /*****************************************************************************
1024  *  External Function Declarations
1025  ****************************************************************************/
1026 #ifdef __cplusplus
1027 extern "C"
1028 {
1029 #endif
1030 
1031 /**
1032  * Generic Attribute (GATT) Functions.
1033  * The Generic Attribute Profile (GATT) defines a service framework which enables
1034  * Bluetooth low energy applications to configure themselves as a client or server device.
1035  *
1036  * The profile also provides the capability to perform discovery of services, read, write,
1037  * notification and indication of characteristics defined on a server.
1038  *
1039  * @addtogroup wicedbt_gatt Generic Attribute (GATT)
1040  * @ingroup wicedbt
1041  */
1042 
1043 /**
1044  * BLE (Bluetooth Low Energy) Specific functions.
1045  *
1046  * @if DUAL_MODE
1047  * @addtogroup gatt_le BLE (Bluetooth Low Energy)
1048  * @ingroup gatt_common_api
1049  * @endif
1050  */
1051 
1052 /**
1053  *  BR/EDR (Bluetooth Basic Rate / Enhanced Data Rate) Specific functions.
1054  *
1055  *  @if DUAL_MODE
1056  *  @addtogroup gatt_br BR/EDR (Bluetooth Basic Rate / Enhanced Data Rate)
1057  *  @ingroup gatt_common_api
1058  *  @endif
1059  */
1060 
1061 /**
1062  * GATT Profile Server Functions
1063  *
1064  *  @addtogroup  gatt_server_api_functions       Server API
1065  *  @ingroup wicedbt_gatt
1066  *
1067  *  <b> Server API Functions </b> sub module for @b GATT.
1068  */
1069 
1070 /**
1071  *  GATT Server Data API
1072  *
1073  *  @addtogroup  gattsr_api_functions GATT Server Data API
1074  *  @ingroup gatt_server_api_functions
1075  *
1076  */
1077 
1078 /**
1079  * This API will send a long (1 upto (MTU -3) bytes) indication to the client for the specified
1080  *  handle with a persistent buffer in \p p_app_buffer.
1081  * The indication confirmation from the remote is indicated by an GATT_ATTRIBUTE_REQUEST_EVT
1082  * with wiced_bt_gatt_attribute_request_t.opcode = GATT_HANDLE_VALUE_IND
1083  *
1084  *  @param[in]  conn_id      : connection identifier.
1085  *  @param[in]  attr_handle  : Attribute handle of this handle value indication.
1086  *  @param[in]  val_len      : Length of indication value passed.
1087  *  @param[in]  p_app_buffer : Indication Value, peristent till the data is sent out to the controller
1088  *  @param[in]  p_app_ctxt   : Application context for \p p_app_buffer
1089  *
1090  *  @note The application may free/release/deallocate the \p p_app_buffer pointer
1091  *  on receiving a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1092  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data = \p p_app_buffer and
1093  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt   = \p p_app_ctxt
1094  *
1095  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1096  *  @ingroup gattsr_api_functions
1097  */
1098 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_indication(uint16_t conn_id,
1099     uint16_t attr_handle, uint16_t val_len, uint8_t *p_app_buffer,
1100     wiced_bt_gatt_app_context_t p_app_ctxt);
1101 
1102 /**
1103  * This API will send a long (1 upto (MTU -3) bytes) notification to the client for the
1104  * specified handle with a persistent buffer in \p p_app_buffer.
1105  * The send complete of the notification is indicated by an GATT_ATTRIBUTE_REQUEST_EVT
1106  * with wiced_bt_gatt_attribute_request_t.opcode = GATT_HANDLE_VALUE_NOTIF
1107  *
1108  *  @param[in]  conn_id      : connection identifier.
1109  *  @param[in]  attr_handle  : Attribute handle of this handle value notification.
1110  *  @param[in]  val_len      : Length of notification value passed.
1111  *  @param[in]  p_app_buffer : Notification Value, peristent till the data is sent out to the controller
1112  *  @param[in]  p_app_ctxt   : Application context for \p p_app_buffer
1113  *
1114  *  @note The application may free/release/deallocate the \p p_app_buffer pointer
1115  *  on receiving a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1116  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data = \p p_app_buffer and
1117  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt   = \p p_app_ctxt
1118  *
1119  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1120  *  @ingroup gattsr_api_functions
1121  */
1122 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_notification(uint16_t conn_id, uint16_t attr_handle,
1123     uint16_t val_len, uint8_t *p_app_buffer, wiced_bt_gatt_app_context_t p_app_ctxt);
1124 
1125 /**
1126  * This API will send a long (1 upto (MTU -1) bytes) multiple variable length notification to
1127  * the client with a persistent buffer in \p p_app_buffer.
1128  * The send complete of the notification is indicated by an GATT_ATTRIBUTE_REQUEST_EVT with
1129  * wiced_bt_gatt_attribute_request_t.opcode = GATT_HANDLE_VALUE_MULTI_NOTIF
1130  *
1131  *  @param[in]  conn_id        : connection identifier.
1132  *  @param[in]  app_buffer_len : Length of multiple notification values passed, should not exceed MTU - 1.
1133  *  @param[in]  p_app_buffer   : Notification Values, peristent till the data is sent out to the controller
1134  *                               @note: Notification values are formatted by the application by setting the
1135  *                                      handle(2 octets LE), len(2 octets LE) and data of handle of length for
1136  *                                      atleast 2 handle, len, data pairs.
1137  *  @param[in]  p_app_ctxt     : Application context for \p p_app_buffer
1138  *
1139  *  @note The application may free/release/deallocate the \p p_app_buffer pointer
1140  *  on receiving a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1141  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data = \p p_app_buffer and
1142  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt   = \p p_app_ctxt
1143  *
1144  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1145  *  @ingroup gattsr_api_functions
1146  */
1147 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_multiple_notifications(uint16_t conn_id,
1148     uint16_t app_buffer_len, uint8_t *p_app_buffer, wiced_bt_gatt_app_context_t p_app_ctxt);
1149 
1150 /**
1151  *  GATT Database Access Functions
1152  *
1153  *  @addtogroup  gattdb_api_functions GATT Database
1154  *  @ingroup gatt_server_api_functions
1155  *
1156  */
1157 
1158 /**
1159  * Initialize the GATT database
1160  * (Please refer @ref GATT_DB_MACROS "Service and Characteristic macros" for
1161  * MACRO's to create or add entries to GATT database)
1162  *
1163  * @param[in]   p_gatt_db       : First element in new GATT database array
1164  * @param[in]   gatt_db_size    : Size (in bytes) of GATT database
1165  * @param[out]  hash            : The calculated database hash value. The hash pointer passed to
1166  *                                this function can be NULL incase the application does not support
1167  *                                dynamic databases and does not support database caching
1168  *
1169  * @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1170  *  @ingroup gattdb_api_functions
1171  */
1172 wiced_bt_gatt_status_t wiced_bt_gatt_db_init (const uint8_t *p_gatt_db, uint16_t gatt_db_size, wiced_bt_db_hash_t hash);
1173 
1174 /**
1175  * Add a service module to the database.
1176  * Service modules define the complete service handles (i.e Service, characteristics,
1177  * descriptor, included service declaration handles)
1178  * The handle range in the service modules have to be distinct, i.e, cannot overlap
1179  * with any of the existing service modules in the database
1180  * @note Set the \ref wiced_bt_cfg_gatt_t.max_db_service_modules to the number of additional
1181  * services to be added
1182  *
1183  * @param[in]   p_gatt_db       : First element in GATT database array
1184  * @param[in]   gatt_db_size    : Size (in bytes) of GATT database
1185  * @param[out]  hash            : The calculated database hash value. The hash pointer passed
1186  *                                to this function can be NULL incase the application does not
1187  *                                support dynamic databases and does not support database caching
1188  *
1189  * @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1190  *  @ingroup gattdb_api_functions
1191  */
1192 
1193 wiced_bt_gatt_status_t wiced_bt_gatt_add_services_to_db(const uint8_t* p_gatt_db,
1194     uint16_t gatt_db_size, wiced_bt_db_hash_t hash);
1195 
1196 /**
1197  * Remove the service module from the database.
1198  *
1199  * @param[in]   p_gatt_db       : First element in GATT database array
1200  * @param[out]  hash            : The calculated database hash value. The hash pointer passed
1201  *                                to this function can be NULL incase the application does not
1202  *                                support dynamic databases and does not support database caching
1203  *
1204  * @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1205  *  @ingroup gattdb_api_functions
1206  */
1207 wiced_bt_gatt_status_t wiced_bt_gatt_db_remove_services_from_db(const uint8_t* p_gatt_db,
1208     wiced_bt_db_hash_t hash);
1209 
1210 /**
1211  * @brief Server API to respond to a MTU request from the client
1212  *
1213  * @param[in] conn_id : GATT Connection ID
1214  * @param[in] remote_mtu: The remote MTU received in \ref wiced_bt_gatt_request_params_t.remote_mtu
1215  * @param[in] my_mtu : The response MTU of the application
1216  *
1217  * @note: my_mtu shall be set to <= the configured pdu size in \ref wiced_bt_cfg_settings_t and shall
1218  * be >= \ref GATT_BLE_DEFAULT_MTU_SIZE
1219  */
1220 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_mtu_rsp(uint16_t conn_id, uint16_t remote_mtu, uint16_t my_mtu);
1221 
1222 /**
1223  * @brief Server API to respond to a read request from the client
1224  * This API can be used to respond to \ref GATT_REQ_READ and \ref GATT_REQ_READ_BLOB request.
1225  * The application receivess a callback on the registered \ref wiced_bt_gatt_cback_t with the
1226  * \ref wiced_bt_gatt_attribute_request_t.opcode set to \ref GATT_REQ_READ or \ref GATT_REQ_READ_BLOB
1227  *
1228  * @param[in] conn_id    : GATT Connection ID
1229  * @param[in] opcode     : The opcode received in the event
1230  * @param[in] len        : Actual valid length pointed to by \p p_attr_rsp
1231  * @param[in] p_attr_rsp : The attribute handle \ref wiced_bt_gatt_read_t.handle data as requested,
1232  *                         at an offset of \ref wiced_bt_gatt_read_t.offset
1233  * @param[in] p_app_ctxt : Application context for \p p_attr_rsp
1234  *
1235  *  @note The application may free/release/deallocate the \p p_attr_rsp pointer
1236  *  on receiving a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1237  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data = \p p_app_buffer and
1238  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt   = \p p_attr_rsp
1239  *
1240  *
1241  * @return wiced_bt_gatt_status_t
1242  */
1243 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_read_handle_rsp(uint16_t conn_id,
1244     wiced_bt_gatt_opcode_t opcode,
1245     uint16_t len,
1246     uint8_t *p_attr_rsp,
1247     wiced_bt_gatt_app_context_t p_app_ctxt);
1248 
1249 /**
1250  * @brief Server API to respond to a read by type request from the client
1251  * This API can be used to respond to \ref GATT_REQ_READ_BY_TYPE
1252  * The application receivess a callback on the registered \ref wiced_bt_gatt_cback_t with the
1253  * \ref wiced_bt_gatt_attribute_request_t.opcode set to \ref GATT_REQ_READ_BY_TYPE
1254  *
1255  * @param[in] conn_id : GATT Connection ID
1256  * @param[in] opcode : The opcode received in the event
1257  * @param[in] type_len: The length of the type in the response
1258  * @param[in] data_len : Actual valid length pointed to by \p p_app_rsp_buffer
1259  * @param[in] p_app_rsp_buffer : The formatted data response
1260  * @param[in] p_app_ctxt : Application context for \p p_app_rsp_buffer
1261  *
1262  *  @note The application may free/release/deallocate the \p p_app_rsp_buffer pointer
1263  *  on receiving a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1264  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data = \p p_app_rsp_buffer and
1265  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt   = \p p_app_ctxt
1266  *
1267  * @return wiced_bt_gatt_status_t
1268  */
1269 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_read_by_type_rsp (uint16_t conn_id,
1270     wiced_bt_gatt_opcode_t opcode,
1271     uint8_t type_len,
1272     uint16_t data_len, uint8_t*p_app_rsp_buffer, wiced_bt_gatt_app_context_t p_app_ctxt);
1273 
1274 /**
1275  * @brief Server API to respond to read multiple request from the client
1276  * This API can be used to respond to \ref GATT_REQ_READ_MULTI or \ref GATT_REQ_READ_MULTI_VAR_LENGTH
1277  * The application receivess a callback on the registered \ref wiced_bt_gatt_cback_t with the
1278  * \ref wiced_bt_gatt_attribute_request_t.opcode set to \ref GATT_REQ_READ_MULTI or
1279  * \ref GATT_REQ_READ_MULTI_VAR_LENGTH
1280  *
1281  * @param[in] conn_id : GATT Connection ID
1282  * @param[in] opcode : The opcode received in the event
1283  * @param[in] len : Actual valid length pointed to by \p p_app_rsp_buffer
1284  * @param[in] p_app_rsp_buffer : The formatted data response
1285  * @param[in] p_app_ctxt : Application context for \p p_app_rsp_buffer
1286  *
1287  *  @note The application may free/release/deallocate the \p p_app_rsp_buffer pointer
1288  *  on receiving a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1289  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data = \p p_app_rsp_buffer and
1290  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt   = \p p_app_ctxt
1291  *
1292  * @return wiced_bt_gatt_status_t
1293  */
1294 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_read_multiple_rsp (uint16_t conn_id,
1295     wiced_bt_gatt_opcode_t opcode,
1296     uint16_t len, uint8_t* p_app_rsp_buffer, wiced_bt_gatt_app_context_t p_app_ctxt);
1297 
1298 /**
1299  * @brief Server API to respond to a write request from the client
1300  * This API can be used to respond to \ref GATT_REQ_WRITE
1301  * The application receives a callback on the registered \ref wiced_bt_gatt_cback_t with the
1302  * \ref wiced_bt_gatt_attribute_request_t.opcode set to \ref GATT_REQ_WRITE or
1303  * \ref GATT_CMD_WRITE or \ref GATT_CMD_SIGNED_WRITE
1304  * The command parameters are in \ref wiced_bt_gatt_write_req_t
1305  *
1306  * @param[in] conn_id : GATT Connection ID
1307  * @param[in] opcode : The opcode received in the event
1308  * @param[in] handle : The handle as received in the request
1309  *
1310  * @return wiced_bt_gatt_status_t
1311  */
1312 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_write_rsp(uint16_t conn_id,
1313     wiced_bt_gatt_opcode_t opcode, uint16_t handle);
1314 
1315 /**
1316  * @brief Server API to respond to a prepare write request from the client
1317  * This API can be used to respond to \ref GATT_REQ_PREPARE_WRITE
1318  * The application receives a callback on the registered \ref wiced_bt_gatt_cback_t with the
1319  * \ref wiced_bt_gatt_attribute_request_t.opcode set to \ref GATT_REQ_PREPARE_WRITE
1320  * The application is expected to queue up the writes till it receives a \ref GATT_REQ_EXECUTE_WRITE
1321  * The command parameters are in \ref wiced_bt_gatt_write_req_t
1322  *
1323  * The mechanism of prepare writes is only applicable for attributes which have set the
1324  * \ref GATTDB_PERM_RELIABLE_WRITE
1325  *
1326  * @param[in] conn_id : GATT Connection ID
1327  * @param[in] opcode : The opcode received in the event
1328  * @param[in] handle : The handle as received in the request
1329  * @param[in] offset : The offset as received in the request
1330  * @param[in] len : The actual length of the \p p_data
1331  * @param[in] p_app_rsp_buffer: The data copied into the queue as received in the command.
1332  *  @note: \p p_app_rsp_buffer is a copy of the received data. The application should not
1333  *      send the received \ref wiced_bt_gatt_write_req_t.p_val pointer
1334  * @param[in] p_app_ctxt : Application context for \p p_app_rsp_buffer
1335  *
1336  *  @note The application may free/release/deallocate the \p p_app_rsp_buffer pointer
1337  *  on receiving a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1338  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data set to \p p_app_rsp_buffer and
1339  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt set to \p p_app_ctxt
1340  *
1341  * @return wiced_bt_gatt_status_t
1342  */
1343 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_prepare_write_rsp(uint16_t conn_id,
1344     wiced_bt_gatt_opcode_t opcode,
1345     uint16_t handle, uint16_t offset,
1346     uint16_t len, uint8_t *p_app_rsp_buffer, wiced_bt_gatt_app_context_t p_app_ctxt);
1347 
1348 
1349 /**
1350  * @brief Server API to respond to an execute write request from the client
1351  * The application receives a callback on the registered \ref wiced_bt_gatt_cback_t with the
1352  * \ref wiced_bt_gatt_attribute_request_t.opcode set to \ref GATT_REQ_EXECUTE_WRITE
1353  * The application is expected to write the prepare write queue on receiving this command.
1354  * The command parameters are in \ref wiced_bt_gatt_execute_write_req_t
1355  *
1356  * @param[in] conn_id : GATT Connection ID
1357  * @param[in] opcode : The opcode received in the event
1358  *
1359  * @return wiced_bt_gatt_status_t
1360  */
1361 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_execute_write_rsp(uint16_t conn_id,
1362     wiced_bt_gatt_opcode_t opcode);
1363 
1364 /**
1365  * @brief Server API to send an error response to any of the received requests on
1366  * encountering an error in the command
1367  *
1368  * @param[in] conn_id : GATT Connection ID
1369  * @param[in] opcode : The opcode received in the event
1370  * @param[in] handle : The attribute handle as received in the event
1371  * @param[in] status : The reason code for the error
1372  *
1373  * @return wiced_bt_gatt_status_t
1374  */
1375 wiced_bt_gatt_status_t wiced_bt_gatt_server_send_error_rsp(uint16_t conn_id,
1376     wiced_bt_gatt_opcode_t opcode, uint16_t handle, wiced_bt_gatt_status_t status);
1377 
1378 
1379 /**
1380  * @brief Utility function on the server to find a handle matching a uuid in a range
1381  *
1382  * @param[in] s_handle : Start handle of the range to search
1383  * @param[in] e_handle : End handle of the range to search
1384  * @param[in] p_uuid   : The attribute uuid to match
1385  *
1386  * @return wiced_bt_gatt_status_t
1387  */
1388 
1389 uint16_t wiced_bt_gatt_find_handle_by_type(uint16_t s_handle, uint16_t e_handle, wiced_bt_uuid_t *p_uuid);
1390 
1391 /**
1392  *  GATT Robust Caching  API
1393  *
1394  *  @addtogroup  gatt_robust_caching_api_functions GATT Robust Caching
1395  *  @ingroup gatt_server_api_functions
1396  *
1397  */
1398 
1399 /**
1400  *
1401  * Set the remote client supported features upon write and for bonded device after
1402  *  reconnect as read from NVRAM
1403  *
1404  * @param[in]   conn_id : GATT Connection ID
1405  * @param[in]   csfs : Bit mask as received from the peer
1406  *
1407  * @return None
1408  * @ingroup gatt_robust_caching_api_functions
1409  */
1410 void wiced_bt_gatt_set_client_supported_features(uint16_t conn_id,
1411     wiced_bt_gatt_client_supported_features_t csfs);
1412 
1413 /**
1414  *
1415  * Get the remote client supported features
1416  *
1417  * @param[in]   conn_id : GATT Connection ID
1418  * @param[in]   csfs : Bit mask set in \ref wiced_bt_gatt_set_client_supported_features
1419  *
1420  * @return None
1421  * @ingroup gatt_robust_caching_api_functions
1422  */
1423 void wiced_bt_gatt_get_client_supported_features(uint16_t conn_id,
1424     wiced_bt_gatt_client_supported_features_t csfs);
1425 
1426 
1427 /**
1428  * Function     wiced_bt_gatt_get_peer_caching_status
1429  * Can be used by the server application to read the robust caching status of the peer.
1430  *
1431  * @param[in]       conn_id: connection identifier.
1432  * @param[out]      caching_status: see \ref wiced_bt_gatt_caching_status_t
1433  *
1434  * @return          None
1435  * @ingroup gatt_robust_caching_api_functions
1436  */
1437 void wiced_bt_gatt_get_peer_caching_status(uint16_t conn_id, wiced_bt_gatt_caching_status_t* caching_status);
1438 
1439 /**
1440  *
1441  * This function is used by the application to set peer's caching state.
1442  * The application is expected to store the last #wiced_bt_db_hash_t shared with the bonded peer in NVRAM
1443  * On connection, the application compares the stored #wiced_bt_db_hash_t with the current db_hash.
1444  * If the hashes match, the status is set to #GATT_PEER_CLIENT_CACHE_CHANGE_AWARE
1445  * else #GATT_PEER_CLIENT_CACHE_CHANGE_UNAWARE
1446  *
1447  * @param[in]       conn_id: connection identifier.
1448  * @param[in]       status: CHANGE_AWARE/CHANGE_UNAWARE.
1449  *
1450  * @return          None
1451  * @ingroup gatt_robust_caching_api_functions
1452  */
1453 void wiced_bt_gatt_set_peer_caching_status(uint16_t conn_id, wiced_bt_gatt_caching_status_t status);
1454 
1455 /**
1456  *  GATT Profile Client Functions
1457  *
1458  *  @addtogroup  gatt_client_api_functions   Client API
1459  *  @ingroup wicedbt_gatt
1460  */
1461 
1462 /**
1463  * Configure the ATT MTU size for a connection on an LE transport.
1464  *
1465  *  @param[in]  conn_id             : GATT connection handle
1466  *  @param[in]  mtu                 : New MTU size
1467  *
1468  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1469  *  @ingroup gatt_client_api_functions
1470 
1471  *  @note Allowed mtu range is 23 upto \ref wiced_bt_cfg_ble_t.ble_max_rx_pdu_size for
1472  *  BLE links as configured in #wiced_bt_cfg_settings_t
1473  *
1474 */
1475 wiced_bt_gatt_status_t wiced_bt_gatt_client_configure_mtu (uint16_t conn_id, uint16_t mtu);
1476 
1477 /**
1478  * Start an attribute discovery on an ATT server.
1479  * Discovery results are notified using <b> GATT_DISCOVERY_RESULT_EVT </b>;
1480  * completion is notified using <b> GATT_DISCOVERY_CPLT_EVT </b> of #wiced_bt_gatt_cback_t.
1481  *
1482  *  @param[in]  conn_id             : GATT connection handle
1483  *  @param[in]  discovery_type      : Discover type
1484  *  @param[in]  p_discovery_param   : Discover parameter
1485  *
1486  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1487  *  @ingroup gatt_client_api_functions
1488  */
1489 wiced_bt_gatt_status_t wiced_bt_gatt_client_send_discover (uint16_t conn_id,
1490                                              wiced_bt_gatt_discovery_type_t discovery_type,
1491                                              wiced_bt_gatt_discovery_param_t *p_discovery_param );
1492 
1493 /**
1494  * Read from remote ATT server.
1495  * Result is notified using <b> GATT_OPERATION_CPLT_EVT </b> of #wiced_bt_gatt_cback_t,
1496  * with \ref wiced_bt_gatt_operation_complete_t.op set to GATTC_OPTYPE_READ_HANDLE
1497  *
1498  *  @param[in]  conn_id     : Connection id
1499  *  @param[in]  handle      : Attribute handle to read
1500  *  @param[in]  offset      : Offset to start read from. To read the entire attribute set offset to 0.
1501  *      @note: In case the offset is set to 0, the stack first sends a GATT_REQ_READ and then sends out a
1502  *             series of GATT_REQ_READ_BLOB till the entire attribute is read or until the \p p_read_buf is
1503  *             filled upto \p len
1504  *  @param[out] p_read_buf : The buffer to save the read response
1505  *  @param[in]  len         : Length of the \p p_read_buf
1506  *  @param[in]  auth_req    : Authentication requirements
1507  *
1508  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1509  *  @ingroup gatt_client_api_functions
1510  *
1511  */
1512 wiced_bt_gatt_status_t wiced_bt_gatt_client_send_read_handle(uint16_t conn_id, uint16_t handle,
1513     uint16_t offset,
1514     uint8_t * p_read_buf,
1515     uint16_t len,
1516     wiced_bt_gatt_auth_req_t auth_req);
1517 
1518 /**
1519  * Read by type from remote ATT server.
1520  * The handle information for the read by type request is received via
1521  * <b> GATT_OPERATION_CPLT_EVT </b> of #wiced_bt_gatt_cback_t with
1522  * \ref wiced_bt_gatt_operation_complete_t.op set to \ref GATTC_OPTYPE_READ_HANDLE
1523  * A non-zero \ref wiced_bt_gatt_operation_complete_t.pending_events indicates multiple handle responses
1524  * which may be returned from the server for some UUIDs
1525  *
1526  * The end of the read by type transaction is notified with result via
1527  * <b> GATT_OPERATION_CPLT_EVT </b> of #wiced_bt_gatt_cback_t with
1528  * \ref wiced_bt_gatt_operation_complete_t.op set to GATTC_OPTYPE_READ_BY_TYPE
1529  *
1530  *  @param[in]  conn_id     : Connection id
1531  *  @param[in] s_handle : Start handle of the range to search
1532  *  @param[in] e_handle : End handle of the range to search
1533  *  @param[in] p_uuid   : The attribute uuid to match
1534  *  @param[out] p_read_buf : The buffer to save the read response in case the first
1535  *                           response to the type does not fit into the available MTU.
1536  *  @param[in]  len         : Length of the \p p_read_buf
1537  *  @param[in]  auth_req    : Authentication requirements
1538  *
1539  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1540  *  @ingroup gatt_client_api_functions
1541  *
1542  */
1543 wiced_bt_gatt_status_t wiced_bt_gatt_client_send_read_by_type(uint16_t conn_id,
1544     uint16_t s_handle, uint16_t e_handle, wiced_bt_uuid_t *p_uuid,
1545     uint8_t *p_read_buf, uint16_t len,
1546     wiced_bt_gatt_auth_req_t auth_req);
1547 
1548 /**
1549  * Read multiple from remote ATT server.
1550  * Result is notified using <b> GATT_OPERATION_CPLT_EVT </b> of #wiced_bt_gatt_cback_t, with
1551  * \ref wiced_bt_gatt_operation_complete_t.op
1552  * a) set to GATTC_OPTYPE_READ_MULTIPLE for \p opcode = GATT_REQ_READ_MULTI and
1553  * b) set to GATTC_OPTYPE_READ_HANDLE for \p opcode = GATT_REQ_READ_MULTI_VAR_LENGTH
1554  *
1555  *  @param[in] conn_id     : Connection id
1556  *  @param[in] opcode : Can be set to \ref GATT_REQ_READ_MULTI or \ref GATT_REQ_READ_MULTI_VAR_LENGTH
1557  *  @param[in] num_handles : Number of handles pointed to by \p p_handle_stream
1558  *  @param[in] p_handle_stream : The list of 16 bit handles arranged in Little Endian format
1559  *  @param[in] auth_req   : Authentication requirements
1560  *
1561  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1562  *  @ingroup gatt_client_api_functions
1563  *
1564  */
1565 wiced_bt_gatt_status_t wiced_bt_gatt_client_send_read_multiple(uint16_t conn_id,
1566     wiced_bt_gatt_opcode_t opcode,
1567     int num_handles, uint8_t *p_handle_stream, wiced_bt_gatt_auth_req_t auth_req);
1568 
1569 /**
1570  * Write to remote ATT server.
1571  * Result is notified using <b> GATT_OPERATION_CPLT_EVT </b> of #wiced_bt_gatt_cback_t.
1572  *
1573  *  @param[in] conn_id     : Connection handle
1574  *  @param[in] opcode      : Can be \ref GATT_REQ_WRITE, \ref GATT_CMD_WRITE, \ref GATT_REQ_PREPARE_WRITE
1575  *   @note: When opcode is set to \ref GATT_REQ_WRITE and the length to be written is greater than
1576  *          MTU - 3, then the stack splits the write into a series of \ref GATT_REQ_PREPARE_WRITE and finally
1577  *          sends a \ref GATT_REQ_EXECUTE_WRITE to complete the write.
1578  *  @param[in] p_hdr       : Pointer to the write parameters, with application data in \p p_app_write_buffer
1579  *  @param[in] p_app_write_buffer: Pointer to the application data buffer to be sent. The len of the buffer is in \p p_hdr->len
1580  *  @param[in] p_app_ctxt  : Application context for \p p_app_write_buffer
1581  *
1582  *  @note The application may free/release/deallocate the \p p_app_write_buffer pointer on receiving
1583  *    a \ref GATT_APP_BUFFER_TRANSMITTED_EVT with
1584  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_data set to \p p_app_write_buffer and
1585  *   \ref wiced_bt_gatt_buffer_transmitted_t.p_app_ctxt set to \p p_app_ctxt
1586  *
1587  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1588  *  @ingroup gatt_client_api_functions
1589  */
1590 wiced_bt_gatt_status_t wiced_bt_gatt_client_send_write(uint16_t conn_id,
1591     wiced_bt_gatt_opcode_t opcode,
1592     wiced_bt_gatt_write_hdr_t *p_hdr, uint8_t *p_app_write_buffer,
1593     wiced_bt_gatt_app_context_t p_app_ctxt);
1594 
1595 
1596 /**
1597  * Send Execute Write request to remote ATT server.
1598  *
1599  *  @param[in]  conn_id             : Connection handle
1600  *  @param[in]  is_execute          : <b>WICED_BT_TRUE </b> to execute, <b> WICED_BT_FALSE </b> to cancel
1601  *
1602  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1603  *  @ingroup gatt_client_api_functions
1604  *
1605  */
1606 wiced_bt_gatt_status_t wiced_bt_gatt_client_send_execute_write (uint16_t conn_id, wiced_bool_t is_execute);
1607 
1608 /**
1609  * Send a handle value confirmation to remote ATT server.
1610  * in response to <b>GATTC_OPTYPE_INDICATION </b>  of #wiced_bt_gatt_cback_t
1611  *
1612  *  @param[in]  conn_id             : Connection handle
1613  *  @param[in]  handle              : Attribute handle
1614  *
1615  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1616  *  @ingroup gatt_client_api_functions
1617 */
1618 wiced_bt_gatt_status_t wiced_bt_gatt_client_send_indication_confirm (uint16_t conn_id, uint16_t handle);
1619 
1620 /**
1621  *  GATT Profile Connection Functions
1622  *
1623  *  @addtogroup gatt_common_api Connection API
1624  *  @ingroup wicedbt_gatt
1625  *
1626  */
1627 
1628 /**
1629  * Open GATT over LE connection to a remote device
1630  * Result is notified using <b> GATT_CONNECTION_STATUS_EVT </b> of #wiced_bt_gatt_cback_t.
1631  *
1632  *  @param[in]  bd_addr     : Remote device address
1633  *  @param[in]  bd_addr_type: Public or random address
1634  *  @param[in]  conn_mode   : connection scan mode
1635  *  @param[in]  is_direct   : Is direct connection or not
1636  *
1637  *  @return <b> TRUE </b>            : If connection started
1638  *          <b> FALSE </b>           : If connection start failure
1639  *
1640  * NOTE :  If is_direct = WICED_FALSE, it will create background connection.
1641  *         Default Background connection type is BTM_BLE_CONN_NONE.
1642  *         Before calling wiced_bt_gatt_le_connect please set background connection type (AUTO)
1643  *         using wiced_bt_ble_set_background_connection_type API
1644  *
1645  * @if DUAL_MODE
1646  * @ingroup gatt_le
1647  * @else
1648  * @ingroup gatt_common_api
1649  * @endif
1650  */
1651 wiced_bool_t wiced_bt_gatt_le_connect (wiced_bt_device_address_t bd_addr,
1652                                     wiced_bt_ble_address_type_t bd_addr_type,
1653                                     wiced_bt_ble_conn_mode_t conn_mode,
1654                                     wiced_bool_t is_direct);
1655 /**
1656  * Open GATT over BR/EDR connection to a remote device
1657  * Result is notified using <b> GATT_CONNECTION_STATUS_EVT </b> of #wiced_bt_gatt_cback_t.
1658  *
1659  *  @param[in]  bd_addr     : Remote device address
1660  *
1661  *  @return <b> TRUE </b>            : If connection started
1662  *          <b> FALSE </b>           : If connection start failure
1663  *
1664  *  @ingroup gatt_br
1665  */
1666 wiced_bool_t wiced_bt_gatt_bredr_connect (wiced_bt_device_address_t bd_addr);
1667 
1668 /**
1669  * Register an application callback for GATT.
1670  *
1671  *  @param[in]    p_gatt_cback      : The GATT notification callback
1672  *
1673  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1674  *
1675  * @ingroup gatt_common_api
1676  */
1677 wiced_bt_gatt_status_t wiced_bt_gatt_register (wiced_bt_gatt_cback_t *p_gatt_cback);
1678 
1679 /**
1680  * Cancel initiating GATT connection
1681  *
1682  *  @param[in]  bd_addr     : Remote device address
1683  *  @param[in]  is_direct   : Is direct connection or not
1684  *
1685  *  @return <b> TRUE </b>            : If connection started
1686  *          <b> FALSE </b>           : If connection start failure
1687  *
1688  * @ingroup gatt_common_api
1689  */
1690 wiced_bool_t wiced_bt_gatt_cancel_connect (wiced_bt_device_address_t bd_addr, wiced_bool_t is_direct);
1691 
1692 /**
1693  * Close the specified GATT connection.
1694  * Result is notified using <b> GATT_CONNECTION_STATUS_EVT </b> of #wiced_bt_gatt_cback_t.
1695  *
1696  *  @param[in]   conn_id     : GATT connection ID
1697  *
1698  *  @return @link wiced_bt_gatt_status_e wiced_bt_gatt_status_t @endlink
1699  *
1700  * @ingroup gatt_common_api
1701  */
1702 wiced_bt_gatt_status_t wiced_bt_gatt_disconnect (uint16_t conn_id);
1703 
1704 
1705 /**
1706  * Start or stop LE advertisement and listen for connection.
1707  *
1708  *  @param[in]   start      : TRUE to add device to Filter Accept List / FALSE to remove
1709  *  @param[in]   bd_addr    : Device to add/remove from Filter Accept List
1710  *  @param[in]   type       : Address type of \p bd_addr
1711  *
1712  *  @return <b> TRUE </b>            : Success
1713  *          <b> FALSE </b>           : Failure
1714  *
1715  * @if DUAL_MODE
1716  * @ingroup gatt_le
1717  * @else
1718  * @ingroup gatt_common_api
1719  * @endif
1720  */
1721 wiced_bool_t wiced_bt_gatt_listen (wiced_bool_t start, wiced_bt_device_address_t bd_addr,
1722     wiced_bt_ble_address_type_t type);
1723 
1724 /**
1725  * EATT API
1726  *
1727  *  @addtogroup  gatt_eatt_functions EATT
1728  *  @ingroup gatt_common_api
1729  */
1730 
1731 /**
1732  * Register an application callback for GATT.
1733  *
1734  *  @param[in]    p_gatt_cback      : The GATT notification callback
1735  *  @param[in]    max_eatt_bearers  : Total number of gatt bearers allowed across all devices (legacy + eatt)
1736  *
1737  *  @return Number of (legacy + eatt) bearers allocated
1738  *
1739  *  @ingroup gatt_eatt_functions
1740  */
1741 uint32_t wiced_bt_gatt_eatt_register(wiced_bt_gatt_eatt_callbacks_t *p_gatt_cback, uint32_t max_eatt_bearers);
1742 
1743 /**
1744  * API to create the enhanced gatt channels, using the legacy/unenhanced connection id.
1745  *
1746  *  @param[in]     legacy_conn_id : GATT DB characteristic handle
1747  *  @param[in]     mtu            : EATT bearer Maximum Transmission Unit
1748  *  @param[in]     mps            : EATT bearer Maximum PDU Payload Size
1749  *  @param[in]     num_bearers    : Number of EATT bearers to be established in this call
1750  *  @param[in]     ppDRBs         : Data Receive Buffers, each of MTU size for each of the
1751  *                                  bearers to be established
1752  *  @param[out]    conn_id_list_out : Connection Ids created for the EATT bearers
1753  *
1754  *  @returns #wiced_result_t
1755  *
1756  *  @ingroup gatt_eatt_functions
1757  */
1758 wiced_result_t wiced_bt_gatt_eatt_connect(uint16_t legacy_conn_id, uint32_t mtu,
1759     uint32_t mps, uint32_t num_bearers,
1760     wiced_bt_eatt_drbs ppDRBs, wiced_bt_gatt_eatt_conn_id_list conn_id_list_out);
1761 
1762 /**
1763  * API to create the respond to the enhanced gatt channels connection indication
1764  *
1765  *  @param[in]     p_indication   : #wiced_bt_gatt_eatt_connection_indication_event_t received
1766  *                                  in #wiced_bt_gatt_eatt_callbacks_t
1767  *  @param[in]     p_response     : #wiced_bt_gatt_eatt_connection_response_t response data sent
1768  *                                  by the receiver of #wiced_bt_gatt_eatt_callbacks_t
1769  *  @param[out]    conn_id_list_out : Connection Ids created for the EATT bearers
1770  *
1771  *  @returns characteristic descriptor handle
1772  *
1773  *  @ingroup gatt_eatt_functions
1774  */
1775 wiced_result_t wiced_bt_gatt_eatt_connect_response(wiced_bt_gatt_eatt_connection_indication_event_t * p_indication,
1776     wiced_bt_gatt_eatt_connection_response_t * p_response,
1777     wiced_bt_gatt_eatt_conn_id_list conn_id_list_out);
1778 
1779 /**
1780  * API the reconfigure the enhanced gatt channels
1781  *
1782  *  @param[in]     conn_id_list : #wiced_bt_gatt_eatt_conn_id_list to be reconfigured
1783  *  @param[in]     num_bearers  : Number of bearers in the conn_ids list
1784  *  @param[in]     mtu          : New MTU (Maximum Transmission Unit) value
1785  *  @param[in]     mps          : New MPS (Maximum Protocol Size) value
1786  *  @param[in]     ppDRBs       : New pointers to the DRBs, each of size MTU for each
1787  *                                of bearer in the conn_id_list
1788  *
1789  *  @returns #wiced_result_t
1790  *
1791  *  @ingroup gatt_eatt_functions
1792  */
1793 wiced_result_t wiced_bt_gatt_eatt_reconfigure(wiced_bt_gatt_eatt_conn_id_list conn_id_list,
1794     uint32_t num_bearers, uint32_t mtu, uint32_t mps, wiced_bt_eatt_drbs ppDRBs);
1795 
1796 /**
1797  * API to get the bluetooth device address of the connected gatt conn_id
1798  * @note : The API cannot be used to get the Bluetooth device address in case
1799  *  the device is disconnected
1800  *
1801  *  @param[in]  conn_id    : Connection handle of the gatt bearer
1802  *  @param[out] p_bdaddr   : #wiced_bt_device_address_t
1803  *  @param[out] p_transport: #wiced_bt_transport_t of the bearer
1804  *  @param[out] p_addr_type: #wiced_bt_ble_address_type_t of the bearer.
1805  *                           Valid only if device referred by the conn_id is of type \ref BT_TRANSPORT_LE
1806  *
1807  *  @returns #wiced_bt_gatt_status_t
1808  *
1809  *  @ingroup gatt_common_api
1810  */
1811 wiced_bt_gatt_status_t wiced_bt_gatt_get_device_address(uint16_t conn_id,
1812     wiced_bt_device_address_t* p_bdaddr,
1813     wiced_bt_transport_t* p_transport, wiced_bt_ble_address_type_t* p_addr_type);
1814 
1815 /**
1816  * API to validate connected gatt conn_id
1817  *
1818  *  @param[in]  conn_id    : Connection handle of the gatt bearer
1819  *
1820  *  @returns #wiced_bt_gatt_status_t
1821  *
1822  *  @ingroup gatt_common_api
1823  */
1824 wiced_bt_gatt_status_t wiced_bt_gatt_validate_conn_id(uint16_t conn_id);
1825 
1826 
1827 /**
1828  * @brief Utility function to compare UUIDs
1829  * @param[in] p_left : UUID to compare
1830  * @param[in] p_right : UUID to compare
1831  * @return WICED_TRUE if same, WICED_FALSE otherwise
1832 */
1833 wiced_bool_t wiced_bt_is_same_uuid(const wiced_bt_uuid_t *p_left, const wiced_bt_uuid_t *p_right);
1834 
1835 /**
1836  * @brief Utility function to get the configured ATT bearer mtu
1837  *
1838  * \param conn_id
1839  * \return MTU of the bearer
1840  */
1841 uint16_t wiced_bt_gatt_get_bearer_mtu(uint16_t conn_id);
1842 
1843 /**
1844  * @brief Utility function to check whether the attribute referred to by \p handle
1845  * has the permission to perform actions required by \p opcode received in the
1846  * attribute request from the peer
1847  *
1848  * @param[in] conn_id : Bearer Connection id
1849  * @param[in] handle : Attribute handle to check permission on
1850  * @param[in] opcode : Opcode received from peer.
1851  *
1852  * \return WICED_BT_GATT_SUCCESS if the \p perm_to_check is allowed on the \p handle,
1853  *  else the specific error
1854  */
1855 wiced_bt_gatt_status_t wiced_bt_gatt_server_check_attribute_permission(uint16_t conn_id,
1856     uint16_t handle, wiced_bt_gatt_opcode_t opcode);
1857 
1858 /**
1859  * @brief Utility function to copy a partial read by type response to the buffer
1860  *
1861  * @param[in] p_stream   : Pointer to the buffer to be filled
1862  * @param[in] stream_len : Writable length of buffer pointed to by \p p_stream
1863  * @param[out] p_pair_len : The value length for the read by type rsp.
1864  *           @note: (*p_pair_len) shall be initialized to 0 prior to the first call to this API
1865  * @param[in] attr_handle : Attribute handle to be filled
1866  * @param[in] attr_len : Attribute len to be filled
1867  * @param[in] p_attr : Attribute data
1868  *
1869  * \return length of data filled, 0 on error.
1870  */
1871 
1872 int wiced_bt_gatt_put_read_by_type_rsp_in_stream(uint8_t *p_stream, int stream_len,
1873     uint8_t *p_pair_len, uint16_t attr_handle, uint16_t attr_len, const uint8_t *p_attr);
1874 
1875 
1876 /**
1877  * @brief Utility function to put the handle, data into the multi handle response stream
1878  *
1879  * @param[in] opcode      : Can be either \ref GATT_REQ_READ_MULTI or \ref GATT_REQ_READ_MULTI_VAR_LENGTH
1880  * @param[in] p_stream    : Pointer to the buffer to be filled
1881  * @param[in] stream_len  : Writable length of buffer pointed to by \p p_stream
1882  * @param[in] attr_handle : Attribute handle to be filled
1883  * @param[in] attr_len    : Attribute len to be filled
1884  * @param[in] p_attr      : Attribute data
1885  *
1886  * \return length of data filled, 0 on error.
1887  */
1888 
1889 int wiced_bt_gatt_put_read_multi_rsp_in_stream(wiced_bt_gatt_optype_t opcode,
1890     uint8_t *p_stream, int stream_len,
1891     uint16_t attr_handle, uint16_t attr_len, const uint8_t *p_attr);
1892 
1893 /**
1894  * @brief Utility function to get the variable length handle data from the read multi var length response stream
1895  *
1896  * @param[in] p_stream    : Pointer to the buffer to be read
1897  * @param[in] stream_len  : Readable length of buffer pointed to by \p p_stream
1898  * @param[out] pp_addr  : Addr to the start of the handle data to be filled
1899  * @param[out] p_attr_len : Pointer to fill in the complete attribute length
1900  * @param[out] p_data_in_rsp: Pointer to fill in the actual received length in the rsp at (*pp_addr)
1901  *
1902  * \return size of the \p p_stream read
1903  */
1904 int wiced_bt_gatt_get_multi_handle_data_from_stream(uint8_t *p_stream, int stream_len,
1905     uint8_t **pp_addr, int *p_attr_len, int *p_data_in_rsp);
1906 
1907 /**
1908  * @brief Utility function to get the handle at index from the handle stream
1909  *
1910  * @param[in] p_handle_stream : Stream of handles each of 2 octets
1911  * @param[in] handle_index    : index into the handle stream
1912  *
1913  * \return
1914  */
1915 uint16_t wiced_bt_gatt_get_handle_from_stream(uint8_t *p_handle_stream, uint16_t handle_index);
1916 
1917 /**
1918  * @brief Utility function to put the handle into the handle stream
1919  *
1920  * @param[in] p_handle_stream : Stream of handles each of 2 octets
1921  * @param[in] stream_len      : Length of the stream
1922  * @param[in] handle          : handle
1923  *
1924  * \return
1925  */
1926 int wiced_bt_gatt_put_handle_in_stream(uint8_t *p_handle_stream, int stream_len, uint16_t handle);
1927 
1928 /**
1929  * @brief Utility function to get the handle value and handle value len of internal DB handles
1930  *
1931  * @param[in] handle : Attribute Handle
1932  * @param[out] p_len : pointer to store the length of the handle value
1933  *
1934  * \return pointer to the value of the handle in the db
1935  */
1936 const uint8_t *wiced_bt_gatt_get_handle_value(uint16_t handle, int *p_len);
1937 
1938 /**
1939  * @brief Utility function to get the number of packets queued to tx
1940  *
1941  * @param[in] conn_id : Connection identifier
1942  * @param[out] p_fragments_with_controller: pointer to receive the number of fragments with the controller
1943  *
1944  * \return number of packets queued to tx
1945  */
1946 int wiced_bt_gatt_get_num_queued_tx_packets(uint16_t conn_id, int *p_fragments_with_controller);
1947 
1948 /**
1949 * @brief Utility function to read local registered db by type, by iterating to the next in a loop
1950 * database entry. Initially \p p_db_start is set to NULL, which resets the search to the head of the local database.
1951 * The database is traversed to locate the next entry which matches the intended \p type.
1952 * If an entry is found the data of that entry is written to \p p_disc_data and the entry in the database is returned.
1953 * The returned entry is passed in the subsequent call to this function in \p p_db_start.
1954 * If an entry is not found the function returns a NULL, which indicates that the entire database has been iterated and the calling loop
1955 * can end.
1956 *
1957 * @param[in] type: Discovery type
1958 * @param[in] p_db_start: The database entry to search from. Value of NULL, resets the database pointer to the head
1959 * @param[out] p_disc_data: discovered data, only valid if the return is not NULL
1960 *
1961 * @return wiced_gattdb_entry_t *
1962 */
1963 const wiced_gattdb_entry_t *wiced_bt_gattdb_local_read_data_by_type(wiced_bt_gatt_discovery_type_t type,
1964                                                                     const wiced_gattdb_entry_t *p_db_start,
1965                                                                     wiced_bt_gatt_discovery_data_t *p_disc_data);
1966 
1967 /**
1968  * @brief Utility function to return the 16 bit UUID of the database entry in the local database
1969  *  This will return a attribute UUID in the entry. If the attribute uuid is not 2 bytes. It will return 0x0 which is invalid uuid.
1970  *
1971  * @param[in] p_db_entry: Database entry for which the UUID is to be returned.
1972  *
1973  * @return 16 bit UUID value if available, or 0
1974 */
1975 uint16_t wiced_bt_gattdb_getAttrUUID16(const wiced_gattdb_entry_t *p_db_entry);
1976 
1977 /**
1978  * @brief Utility function to return the data of the database entry in the local database
1979  *
1980  * @param[in] p_db_entry: Database entry of which the data is to be returned.
1981  *
1982  * @return data of the database entry
1983 */
1984 uint8_t *wiced_bt_gattdb_getAttrValue(const wiced_gattdb_entry_t *p_db_entry);
1985 
1986 #ifdef __cplusplus
1987 }
1988 
1989 #endif
1990