1 /**
2  ******************************************************************************
3   * @file    ble_gatt.h
4   * @author  GPM WBL Application team
5   * @brief   Header file for Bluetooth LE GATT
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2024 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 #ifndef BLE_GATT_H
20 #define BLE_GATT_H
21 
22 /******************************************************************************
23  * Includes
24  *****************************************************************************/
25 #include <stdint.h>
26 
27 /******************************************************************************
28  * CONSTANT SYMBOLS
29  *****************************************************************************/
30 /**
31  *@addtogroup GATT GATT
32  *@brief Generic Attribute Profile
33  *@{
34  */
35 
36 /**
37  *@addtogroup GATT_constants GATT Constants
38  *@brief Constants for GATT layer
39  *@{
40  */
41 
42 /**
43  *@name BLE ATT
44  * Constants for connection handles
45  *@{
46  */
47 #define BLE_ATT_INVALID_ATTR_HANDLE                     (0x0000U) /**< Invalid attribute handle */
48 #define BLE_ATT_MIN_ATTR_HANDLE                         (0x0001U) /**< Minimum valid attribute handle */
49 #define BLE_ATT_MAX_ATTR_HANDLE                         (0xFFFFU) /**< Maximum valid attribute handle */
50 #define BLE_ATT_ATTR_HANDLE_SIZE                        (2U) /**< Size of an attribute handle value */
51 #define BLE_GATT_MAX_ATTR_VALUE_SIZE                    (512U) /**< Max size of an attribute value. @see 3.2.9 Long
52                                                                 *   attribute values - BLUETOOTH CORE SPECIFICATION
53                                                                 *   Version 5.1 | Vol 3, Part F page 2297 */
54 /**
55  *@}
56  */
57 
58 /**
59  *@anchor GATT_SRV_TYPE
60  *@name GATT_SRV_TYPE
61  *@{
62  */
63 #define BLE_GATT_SRV_PRIMARY_SRV_TYPE                   (1U)
64 #define BLE_GATT_SRV_SECONDARY_SRV_TYPE                 (2U)
65 /**
66  *@}
67  */
68 
69 /**
70  *@anchor GATT_SRV_KEY_SIZE
71  *@name GATT_SRV_KEY_SIZE
72  *@{
73  */
74 #define BLE_GATT_SRV_MIN_ENCRY_KEY_SIZE                 (7U)
75 #define BLE_GATT_SRV_MAX_ENCRY_KEY_SIZE                 (16U)
76 /**
77  *@}
78  */
79 
80 /**
81  *@name GATT_SRV_UUID
82  *@{
83  */
84 #define BLE_GATT_SRV_PRIM_SERV_DECL_TYPE_UUID           (0x2800U)
85 #define BLE_GATT_SRV_SEC_SERV_DECL_TYPE_UUID            (0x2801U)
86 #define BLE_GATT_SRV_INCL_SERV_DECL_TYPE_UUID           (0x2802U)
87 #define BLE_GATT_SRV_CHAR_DECL_TYPE_UUID                (0x2803U)
88 #define BLE_GATT_SRV_CHAR_EXT_PROP_DESCR_TYPE_UUID      (0x2900U)
89 #define BLE_GATT_SRV_CHAR_USER_DESCR_TYPE_UUID          (0x2901U)
90 #define BLE_GATT_SRV_CLIENT_CHAR_CONF_DESCR_TYPE_UUID   (0x2902U)
91 #define BLE_GATT_SRV_SERVER_CHAR_CONF_DESCR_TYPE_UUID   (0x2903U)
92 #define BLE_GATT_SRV_CHAR_PRES_FORMAT_DESCR_TYPE_UUID   (0x2904U)
93 #define BLE_GATT_SRV_CHAR_AGGR_FORMAT_DESCR_TYPE_UUID   (0x2905U)
94 #define BLE_GATT_SRV_DEVICE_NAME_CHR_UUID               (0x2A00U)
95 #define BLE_GATT_SRV_APPEARANCE_CHR_UUID                (0x2A01U)
96 #define BLE_GATT_SRV_PERIPHERAL_PREFERRED_CONN_PARAMS_UUID   (0x2A04U)
97 #define BLE_GATT_SRV_SERVICE_CHANGE_CHR_UUID            (0x2A05U)
98 #define BLE_GATT_SRV_CENTRAL_ADDRESS_RESOLUTION_UUID    (0x2AA6U)
99 #define BLE_GATT_SRV_CLIENT_SUPP_FEATURE_CHR_UUID       (0x2B29U)
100 #define BLE_GATT_SRV_DB_HASH_CHR_UUID                   (0x2B2AU)
101 #define BLE_GATT_SRV_SUPPORTED_FEATURES_CHR_UUID        (0x2B3AU)
102 #define BLE_GATT_SRV_ENCRYPTED_DATA_KEY_MATERIAL_UUID   (0x2B88U)
103 
104 #define BLE_GATT_SRV_GATT_SERVICE_UUID                  (0x1801U)
105 #define BLE_GATT_SRV_GAP_SERVICE_UUID                   (0x1800U)
106 
107 /**
108  *@}
109  */
110 
111 /**
112  *@name GATT_SRV_CHAR_PROP
113  * @brief The following macro are used to define the characteristic properties as
114  * defined in BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G, 3.3.1.1
115  * Table 3.5.
116  *@{
117  */
118 #define BLE_GATT_SRV_CHAR_PROP_NONE                     (0x00U)
119 /** If set, it permits broadcasts of the
120  * Characteristic Value using Server Characteristic Configuration Descriptor.
121  * If set, the Server Characteristic Configuration Descriptor shall exist. */
122 #define BLE_GATT_SRV_CHAR_PROP_BROADCAST                (0x01U)
123 /** If set, it permits reads of the Characteristic Value. */
124 #define BLE_GATT_SRV_CHAR_PROP_READ                     (0x02U)
125 /** If set, it permit writes of the Characteristic Value without response. */
126 #define BLE_GATT_SRV_CHAR_PROP_WRITE_NO_RESP            (0x04U)
127 /** If set, it permits writes of the Characteristic Value with response. */
128 #define BLE_GATT_SRV_CHAR_PROP_WRITE                    (0x08U)
129 /** If set, it permits notifications of a
130  * Characteristic Value without acknowledgment. If set, the Client
131  * Characteristic Configuration Descriptor shall exist. */
132 #define BLE_GATT_SRV_CHAR_PROP_NOTIFY                   (0x10U)
133 /** If set, it permits indications of a
134  * Characteristic Value with acknowledgment. If set, the Client Characteristic
135  * Configuration Descriptor shall exist. */
136 #define BLE_GATT_SRV_CHAR_PROP_INDICATE                 (0x20U)
137 /** If set, it permits signed writes to the
138  * Characteristic Value. */
139 #define BLE_GATT_SRV_CHAR_PROP_AUTH_SIGN_WRITE          (0x40U)
140 /** If set, additional characteristic
141  * properties are defined in the Characteristic Extended Properties Descriptor.
142  * If set, the Characteristic Extended Properties Descriptor shall exist. */
143 #define BLE_GATT_SRV_CHAR_PROP_EXTENDED_PROP            (0x80U)
144 
145 #define BLE_GATT_SRV_CHAR_PROP_ACCESS_PERM_MASK         (BLE_GATT_SRV_CHAR_PROP_READ | \
146                                                          BLE_GATT_SRV_CHAR_PROP_WRITE | \
147                                                          BLE_GATT_SRV_CHAR_PROP_WRITE_NO_RESP | \
148                                                          BLE_GATT_SRV_CHAR_PROP_AUTH_SIGN_WRITE)
149 /**
150  *@}
151  */
152 
153 /**
154  *@anchor GATT_SRV_DESCR_PROP
155  *@name GATT_SRV_DESCR_PROP constants
156  * The following macro are used to define the descriptor access properties.
157  *@{
158  */
159 #define BLE_GATT_SRV_DESCR_PROP_READ                    (0x01U)
160 #define BLE_GATT_SRV_DESCR_PROP_WRITE                   (0x02U)
161 /**
162  *@}
163  */
164 
165 /**
166  *@anchor SRV_VALBUFFER_OP_FLAGS
167  *@name SRV_VALBUFFER_OP_FLAGS
168  * The following flags enable some behaviour on how to handle the value
169  * buffers used in the profile definition structures.
170  *@{
171  */
172 #define BLE_GATT_SRV_OP_MODIFIED_EVT_ENABLE_FLAG        (0x01U)
173 #define BLE_GATT_SRV_OP_VALUE_VAR_LENGTH_FLAG           (0x02U)
174 /**
175  *@}
176  */
177 
178 /**
179  *@name ATT_ERROR_CODES
180  * Available Error Codes as reported in:
181  * Table 3.3 | Vol 3, Part F.
182  *@{
183  */
184 #define BLE_ATT_ERR_NONE                                (0x00U) /**< Not an error! */
185 #define BLE_ATT_ERR_INVALID_HANDLE                      (0x01U) /**< The attribute handle given was not valid on this server. */
186 #define BLE_ATT_ERR_READ_NOT_PERM                       (0x02U) /**< The attribute cannot be read. */
187 #define BLE_ATT_ERR_WRITE_NOT_PERM                      (0x03U) /**< The attribute cannot be written. */
188 #define BLE_ATT_ERR_INVALID_PDU                         (0x04U) /**< The attribute PDU was invalid. */
189 #define BLE_ATT_ERR_INSUFF_AUTHENTICATION               (0x05U) /**< The attribute requires authentication before it can be read or written. */
190 #define BLE_ATT_ERR_REQ_NOT_SUPP                        (0x06U) /**< Attribute server does not support the request received from the client. */
191 #define BLE_ATT_ERR_INVALID_OFFSET                      (0x07U) /**< Offset specified was past the end of the attribute. */
192 #define BLE_ATT_ERR_INSUFF_AUTHORIZATION                (0x08U) /**< The attribute requires authorization before it can be read or written. */
193 #define BLE_ATT_ERR_PREP_QUEUE_FULL                     (0x09U) /**< Too many prepare writes have been queued. */
194 #define BLE_ATT_ERR_ATTR_NOT_FOUND                      (0x0AU) /**< No attribute found within the given attribute handle range. */
195 #define BLE_ATT_ERR_ATTR_NOT_LONG                       (0x0BU) /**< The attribute cannot be read using the Read Blob Request. */
196 #define BLE_ATT_ERR_INSUFF_ENCR_KEY_SIZE                (0x0CU) /**< The Encryption Key Size used for encrypting this link is insufficient. */
197 #define BLE_ATT_ERR_INVAL_ATTR_VALUE_LEN                (0x0DU) /**< The attribute value length is invalid for the operation. */
198 #define BLE_ATT_ERR_UNLIKELY                            (0x0EU) /**< The attribute request that was requested has encountered an error
199                                                                  *   that was unlikely, and therefore could not be completed as requested. */
200 #define BLE_ATT_ERR_INSUFF_ENCRYPT                      (0x0FU) /**< The attribute requires encryption before it can be read or written. */
201 #define BLE_ATT_ERR_UNSUPP_GRP_TYPE                     (0x10U) /**< The attribute type is not a supported grouping attribute as defined by
202                                                                  *   a higher layer specification. */
203 #define BLE_ATT_ERR_INSUFF_RESOURCES                    (0x11U) /**< Insufficient Resources to complete the request. */
204 #define BLE_ATT_ERR_DB_OUT_OF_SYNC                      (0x12U) /**< The server requests the client to rediscover the database. */
205 #define BLE_ATT_ERR_VALUE_NOT_ALLOWED                   (0x13U) /**< The attribute parameter value was not allowed. */
206 #define BLE_ATT_ERR_APPL_MIN                            (0x80U) /**< Application error code defined by a higher layer specification.
207                                                                  *   Lower error value.
208                                                                  */
209 #define BLE_ATT_ERR_APPL_MAX                            (0x9FU) /**< Application error code defined by a higher layer specification.
210                                                                  *   Higher error value.
211                                                                  */
212 #define BLE_ATT_ERR_CMN_PROF_SRVC_MIN                   (0xE0U) /**< Common profile and service error codes defined in [Core Specification
213                                                                  *   Supplement], Part B.
214                                                                  *   Lower error value.
215                                                                  */
216 #define BLE_ATT_ERR_CCCD_IMPROPERLY_CONFIGURED          (0xFDU) /**< The Client Characteristic Configuration Descriptor Improperly Configured error
217                                                                  *   code is used when a Client Characteristic Configuration descriptor is not
218                                                                  *   configured according to the requirements of the profile or service.
219                                                                  *   2.3 CLIENT CHARACTERISTIC CONFIGURATION DESCRIPTOR IMPROPERLY CONFIGURED (0xFD)
220                                                                  *   Supplement to the Bluetooth Core Specification | CSS v10, Part B page 34
221                                                                  */
222 #define BLE_ATT_ERR_CMN_PROF_SRVC_MAX                   (0xFFU) /**< Common profile and service error codes defined in [Core Specification
223                                                                  *   Supplement], Part B.
224                                                                  *   Higher error value. */
225 /**
226  *@}
227  */
228 
229 /**
230  *@anchor GATT_SRV_PERMS
231  *@name GATT_SRV_PERMS
232  * The following macro define the attribute permissions.
233  *@{
234  */
235 /** Read and Write can be performed on the attribute with no requirements. */
236 #define BLE_GATT_SRV_PERM_NONE                          (0x00U)
237 /** Attribute can be read only on authenticated links (MITM on). */
238 #define BLE_GATT_SRV_PERM_AUTHEN_READ                   (0x01U)
239 /** Attribute can be read only on encrypted links. */
240 #define BLE_GATT_SRV_PERM_ENCRY_READ                    (0x04U)
241 /** Attribute can be written only on authenticated links (MITM on). */
242 #define BLE_GATT_SRV_PERM_AUTHEN_WRITE                  (0x08U)
243 /** Attribute can be written only on encrypted links. */
244 #define BLE_GATT_SRV_PERM_ENCRY_WRITE                   (0x20U)
245 #define BLE_GATT_SRV_PERM_ALL                           (BLE_GATT_SRV_PERM_AUTHEN_READ | \
246                                                          BLE_GATT_SRV_PERM_ENCRY_READ | \
247                                                          BLE_GATT_SRV_PERM_AUTHEN_WRITE | \
248                                                          BLE_GATT_SRV_PERM_ENCRY_WRITE)
249 /**
250  *@}
251  */
252 
253 /**
254  *@name GATT_SRV_CCCD
255  *@brief The Client Characteristic Configuration is a special descriptor that
256  *       has to store the subscription of each client to receive notification
257  *       and indication value.
258  *@{
259  */
260 #define BLE_GATT_SRV_CCCD_VALUE_LEN                     (2U)
261 #define BLE_GATT_SRV_CCCD_NOTIFICATION_BIT              (0U)
262 #define BLE_GATT_SRV_CCCD_INDICATION_BIT                (1U)
263 #define BLE_GATT_SRV_CCCD_NOTIFICATION                  (1U << BLE_GATT_SRV_CCCD_NOTIFICATION_BIT)
264 #define BLE_GATT_SRV_CCCD_INDICATION                    (1U << BLE_GATT_SRV_CCCD_INDICATION_BIT)
265 
266 #define BLE_GATT_SRV_CCCD_NUM_BITS_PER_CONN             (2U)
267 #define BLE_GATT_SRV_CCCD_VAL_MASK                      (BLE_GATT_SRV_CCCD_NUM_BITS_PER_CONN | \
268                                                          (BLE_GATT_SRV_CCCD_NUM_BITS_PER_CONN - 1))
269 #define BLE_GATT_SRV_CCCD_NUM_CONN_PER_BYTE             (8U / BLE_GATT_SRV_CCCD_NUM_BITS_PER_CONN)
270 /** Return the number of bytes needed to store a CCCD giving the number of connections. */
271 #define BLE_GATT_SRV_CCCD_BUFF_SIZE(NUM_CONN)           (((NUM_CONN) / BLE_GATT_SRV_CCCD_NUM_CONN_PER_BYTE) + 1U)
272 /** Declare a storage buffer with the giving name to store the CCCD. */
273 #define BLE_GATT_SRV_CCCD_BUFF_DECLARE(NAME, NUM_CONN)   uint8_t(NAME)[BLE_GATT_SRV_CCCD_BUFF_SIZE(NUM_CONN)]
274 
275 #define BLE_GATT_SRV_CCCD_BUFFER_NAME(NAME)             NAME##_cccd_buffer
276 #define BLE_GATT_SRV_CCCD_VAL_BUFFER_NAME(NAME)         NAME##_cccd_val_buffer
277 
278 #define BLE_GATT_SRV_CCCD_BUFFER_DECLARE(NAME, NUM_CONN, OP_FLAGS)                  \
279     static uint8_t BLE_GATT_SRV_CCCD_BUFFER_NAME(NAME)[BLE_GATT_SRV_CCCD_BUFF_SIZE(NUM_CONN)]; \
280     static const ble_gatt_val_buffer_def_t BLE_GATT_SRV_CCCD_VAL_BUFFER_NAME(NAME) = {   \
281         .op_flags = (OP_FLAGS & BLE_GATT_SRV_OP_MODIFIED_EVT_ENABLE_FLAG),          \
282         .buffer_len = BLE_GATT_SRV_CCCD_BUFF_SIZE(NUM_CONN),                        \
283         .buffer_p = BLE_GATT_SRV_CCCD_BUFFER_NAME(NAME),                            \
284     }
285 
286 #define BLE_GATT_SRV_CCCD_DEF_NAME(NAME)                NAME##_cccd
287 #define BLE_GATT_SRV_CCCD_DEF_STR_FIELDS(NAME, NUM_CONN, PERM)                      \
288     .val_buffer_p = (ble_gatt_val_buffer_def_t *)&BLE_GATT_SRV_CCCD_VAL_BUFFER_NAME(NAME), \
289     .properties = BLE_GATT_SRV_DESCR_PROP_WRITE | BLE_GATT_SRV_DESCR_PROP_READ,     \
290     .permissions = (PERM),                                                          \
291     .min_key_size = BLE_GATT_SRV_MIN_ENCRY_KEY_SIZE,                                \
292     .uuid = BLE_UUID_INIT_16(BLE_GATT_SRV_CLIENT_CHAR_CONF_DESCR_TYPE_UUID)
293 
294 #define BLE_GATT_SRV_CCCD_DEF_STR_DECLARE(NAME, NUM_CONN, PERM)                     \
295     ble_gatt_descr_def_t BLE_GATT_SRV_CCCD_DEF_NAME(NAME) = {                       \
296         BLE_GATT_SRV_CCCD_DEF_STR_FIELDS(NAME, NUM_CONN, PERM),                     \
297     }
298 
299 #define BLE_GATT_SRV_CCCD_PERM_DEFAULT                 (BLE_GATT_SRV_PERM_NONE)
300 #define BLE_GATT_SRV_CCCD_DECLARE(NAME, NUM_CONN, PERM, OP_FLAGS)                   \
301     BLE_GATT_SRV_CCCD_BUFFER_DECLARE(NAME, NUM_CONN, OP_FLAGS);                     \
302     BLE_GATT_SRV_CCCD_DEF_STR_DECLARE(NAME, NUM_CONN, PERM)
303 /**
304  *@}
305  */
306 
307 /**
308  *@name GATT_SRV_CLIENT_SUP_FEATURE
309  * Use 1 bit for each Client to store Client Supported Feature, since just one
310  * bit is defined in the spec.:
311  * @see Table 7.6: Client Supported Features bit assignments, 7.2 CLIENT
312  * SUPPORTED FEATURES, BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G
313  *@{
314  */
315 
316 /**
317  * Bit assigned for Robust Caching Support
318  *
319  * 7.2 CLIENT SUPPORTED FEATURES, Table 7.6: Client Supported Features bit
320  * assignments, BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2404
321  */
322 #define BLE_GATT_SRV_CSF_ROBUST_CACHING                 (0x01U)
323 
324 /**
325  * Bit assigned for Enhanced ATT bearer
326  *
327  * Table 7.6: Client Supported Features bit assignments
328  * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part G page 1602
329  */
330 #define BLE_GATT_SRV_CSF_EATT                           (0x02U)
331 
332 /**
333  * Bit assigned for Multiple Handle Value Notifications
334  *
335  * Table 7.6: Client Supported Features bit assignments
336  * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part G page 1603
337  */
338 #define BLE_GATT_SRV_CSF_MULTI_HANDLE_VAL_NOTIFICATION  (0x04U)
339 
340 #define BLE_GATT_SRV_CLIENT_SUP_FEATURE_NUM_BITS_PER_CONN    (4U)
341 
342 #define BLE_GATT_SRV_CLIENT_SUP_FEATURE_VAL_MASK        (0x07U)
343 
344 #define BLE_GATT_SRV_CLIENT_SUP_FEATURE_VALUE_LEN       (1U)
345 #define BLE_GATT_SRV_CLIENT_SUP_FEATURE_NUM_CONN_PER_BYTE    (8U / BLE_GATT_SRV_CLIENT_SUP_FEATURE_NUM_BITS_PER_CONN)
346 #define BLE_GATT_SRV_CLIENT_SUP_FEATURE_SIZE_X_CONN(NUM_CONN)  (((NUM_CONN) / \
347                                                                  BLE_GATT_SRV_CLIENT_SUP_FEATURE_NUM_CONN_PER_BYTE) + 1U)
348 /**
349  *@}
350  */
351 
352 /**
353  *@name GATT_SRV_SUPPORTED_FEATURES
354  * Server Supported Features Characteristic is used to indicate support for
355  * server features. The server shall set a bit only if the corresponding
356  * feature is supported.
357  *
358  * Table 7.11: Server Supported Features bit assignments
359  * 7.4 SERVER SUPPORTED FEATURES
360  * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part G page 1605
361  *@{
362  */
363 #define BLE_GATT_SRV_SUPPORTED_FEATURES_EATT            (1U) /**< Enhanced ATT bearer supported. */
364 
365 #define BLE_GATT_SRV_SUPPORTED_FEATURES_VAL_LEN         (1U)
366 /**
367  *@}
368  */
369 
370 /**
371  *@name GATT_SRV_CLIENT_SUP_FEATURE
372  * Flags for aci_gatt_srv_notify
373  *@{
374  */
375 #define BLE_GATT_SRV_NOTIFY_FLAG_NOTIFICATION           (0x00U)
376 #define BLE_GATT_SRV_NOTIFY_FLAG_FLUSHABLE_NOTIFICATION (0x01U)
377 #define BLE_GATT_SRV_NOTIFY_FLAG_INDICATION             (0x02U)
378 /**
379  *@}
380  */
381 
382 #define BLE_GATT_UNENHANCED_ATT_L2CAP_CID               (0x0004)
383 
384 /**
385  *@name UUID_TYPE
386  * The following macro are used to define the type of an UUID:
387  * @def BLE_UUID_TYPE_16BITS used for short 16 bits UUIDs. These are SIG defined.
388  * @def BLE_UUID_TYPE_32BITS used for 32 bits UUIDs. These are SIG defined.
389  * @def BLE_UUID_TYPE_128BITS used for long 128 bits UUIDs.
390  *@{
391  */
392 /** Used for short 16 bits UUIDs. These are SIG defined. */
393 #define BLE_UUID_TYPE_16BITS                            (16U)
394 /** Used for 32 bits UUIDs. These are SIG defined. */
395 #define BLE_UUID_TYPE_32BITS                            (32U)
396 /** Used for long 128 bits UUIDs. */
397 #define BLE_UUID_TYPE_128BITS                           (128U)
398 /**
399  *@}
400  */
401 
402 /**
403  *@name UUID_LENGTH
404  *@{
405  */
406 #define BLE_UUID_128_LENGTH                             (16U)
407 #define BLE_UUID_16_LENGTH                              (2U)
408 /**
409  *@}
410  */
411 
412 /**
413  *@name UUID_BASE
414  *@{
415  */
416 #define BLE_UUID_BASE_LENGTH                            (12U)
417 #define BLE_UUID_BASE                                   0xFB, 0x34, 0x9B, 0x5F, \
418                                                         0x80, 0x00, 0x00, 0x80, \
419                                                         0x00, 0x10, 0x00, 0x00
420 /**
421  *@}
422  */
423 
424 /**
425  *@anchor BLE_UUID_INIT
426  *@name BLE_UUID_INIT Macros
427  *@{
428  */
429 #define BLE_UUID_INIT_128(UUID)     {                                           \
430         .v_128.v = { UUID },                                                    \
431         .uuid_type = BLE_UUID_TYPE_128BITS,                                     \
432 }
433 #define BLE_UUID_INIT_32(UUID)      {                                           \
434         .v_32.base = { BLE_UUID_BASE },                                         \
435         .v_32.v = UUID,                                                         \
436         .uuid_type = BLE_UUID_TYPE_128BITS,                                     \
437 }
438 #define BLE_UUID_INIT_16(UUID)      {                                           \
439         .v_16.base = { BLE_UUID_BASE },                                         \
440         .v_16.v = UUID,                                                         \
441         .v_16.empty = 0x0000U,                                                  \
442         .uuid_type = BLE_UUID_TYPE_16BITS,                                      \
443 }
444 
445 #define BLE_COPY_UUID_16(p, UUID)      do{                                      \
446         const uint8_t uuid_base[] = {BLE_UUID_BASE};                            \
447         memcpy((p).v_16.base,uuid_base,sizeof((p).v_16.base));                  \
448         (p).v_16.v = UUID;                                                      \
449         (p).v_16.empty = 0x0000U;                                               \
450         (p).uuid_type = BLE_UUID_TYPE_16BITS;                                   \
451 }while(0);
452 /**
453  *@}
454  */
455 /**
456  *@}
457  */
458 /******************************************************************************
459  * TYPES
460  *****************************************************************************/
461 typedef struct ble_uuid_s {
462 #if defined(__CC_ARM)
463     #pragma anon_unions
464 #endif
465     union
466     {
467         struct {
468             uint8_t v[BLE_UUID_128_LENGTH];
469         } v_128;
470         struct {
471             uint8_t base[BLE_UUID_BASE_LENGTH];
472             uint32_t v;
473         } v_32;
474         struct {
475             uint8_t base[BLE_UUID_BASE_LENGTH];
476             uint16_t v;
477             uint16_t empty;
478         } v_16;
479     };
480     uint8_t uuid_type;
481 } ble_uuid_t;
482 
483 /**
484  * @brief The following structure hold the information needed to write an
485  * attribute. These information shall be valid for all the time the procedure is
486  * in execution and can be freed on the reception of PROCEDURE COMPLETE event.
487  */
488 typedef struct {
489     uint16_t attr_h; /**< Attribute handle to write. */
490     uint16_t attr_offset; /**< Offset from which start to write the attribute value */
491     uint16_t data_len; /**< Length of data to write. */
492     uint8_t *data_p; /**< Pointer to the data to write. */
493 } ble_gatt_clt_write_ops_t;
494 
495 typedef struct ble_gatt_val_buffer_def_s {
496     uint8_t op_flags; /**< See @ref SRV_VALBUFFER_OP_FLAGS */
497     uint16_t val_len; /**< Actual value length. It can differ from buffer_len if BLE_GATT_SRV_PERM_ENCRY_READ the BLE_GATT_SRV_OP_VALUE_VAR_LENGTH_FLAG is set in op_flags */
498     uint16_t buffer_len; /**< Buffer length. */
499     uint8_t *buffer_p; /**< Pointer to the storage buffer. */
500 } ble_gatt_val_buffer_def_t;
501 
502 typedef struct ble_gatt_descr_def_s {
503     uint8_t properties; /**< See @ref GATT_SRV_DESCR_PROP. */
504     /** Minimum key size required to access descriptor value. Valid only if permissions are
505      *  BLE_GATT_SRV_PERM_ENCRY_READ or BLE_GATT_SRV_PERM_ENCRY_WRITE, otherwise ignored.
506      *  See @ref GATT_SRV_KEY_SIZE for minimum and maximum values.
507      */
508     uint8_t min_key_size;
509     uint8_t permissions; /**< Access permissions. See @ref GATT_SRV_PERMS. */
510     ble_uuid_t uuid; /**< UUID for this descriptor */
511     ble_gatt_val_buffer_def_t *val_buffer_p; /**< Pointer to the value buffer structure. */
512 } ble_gatt_descr_def_t;
513 
514 typedef struct ble_gatt_chr_def_s {
515     uint8_t properties; /**< @see GATT_SRV_CHAR_PROP */
516     /** Minimum key size required to access characteristic value. Valid only if permissions are
517      *  BLE_GATT_SRV_PERM_ENCRY_READ or BLE_GATT_SRV_PERM_ENCRY_WRITE, otherwise ignored.
518      *  @see GATT_SRV_KEY_SIZE for minimum and maximum values.
519      */
520     uint8_t min_key_size;
521     uint8_t permissions; /**< Access permissions. See @ref GATT_SRV_PERMS. */
522     ble_uuid_t uuid; /**< UUID for this characteristic. Macros can be used to initialize the UUID: see @ref BLE_UUID_INIT. */
523     struct {
524         uint8_t descr_count; /**< Number of descriptors. */
525         ble_gatt_descr_def_t *descrs_p; /**< Pointer to the descriptors vector. */
526     } descrs; /**< List of descriptors. */
527     ble_gatt_val_buffer_def_t *val_buffer_p; /**< Pointer to the value buffer structure. */
528 } ble_gatt_chr_def_t;
529 
530 typedef struct ble_gatt_srv_def_s {
531     ble_uuid_t uuid; /**< UUID for this service. Macros can be used to initialize the UUID: see @ref BLE_UUID_INIT. */
532     uint8_t type; /**< primary/secondary. See @ref GATT_SRV_TYPE */
533     uint16_t group_size; /**< Define the number of attribute handles reserved for this service. */
534     struct {
535         uint8_t incl_srv_count; /**< Number of services. */
536         struct ble_gatt_srv_def_s **included_srv_pp; /**< List of pointers to the included service definition. */
537     } included_srv;
538     struct {
539         uint8_t chr_count; /**< Number of characteristics. */
540         ble_gatt_chr_def_t *chrs_p; /**< Pointer to characteristics vector. */
541     } chrs; /**< List of characteristics. */
542 } ble_gatt_srv_def_t;
543 
544 /******************************************************************************
545  * FUNCTION PROTOTYPES
546  *****************************************************************************/
547 
548 /**
549  *@}
550  */
551 
552 #endif /* BLE_GATT_H */
553