1 /**
2   ******************************************************************************
3   * @file    ble_stack.h
4   * @author  GPM WBL Application team
5   * @brief   Header file for Bluetooth LE stack configuration
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   /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef BLE_STACK_H
21 #define BLE_STACK_H
22 
23 /* Includes ------------------------------------------------------------------*/
24 #include <stdint.h>
25 #include "ble_status.h"
26 #include "ble_stack_user_cfg.h"
27 #include "ble_api.h"
28 #include "ble_events.h"
29 
30 
31 #ifndef MIN
32 #define MIN(a,b)                        (((a) < (b))? (a) : (b))
33 #endif
34 #ifndef MAX
35 #define MAX(a,b)                        (((a) > (b))? (a) : (b))
36 #endif
37 #define DIV_CEIL(x, y)                  (((x) + (y) - 1U) / (y))
38 #define ALIGN_32(x)                     ((((x) - 1U) | 3U) + 1U)
39 
40 /**
41  * BLE_STACK_DEFAULT_ATT_MTU: minimum mtu value that GATT must support.
42  * 5.2.1 ATT_MTU, BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G]
43  */
44 #define BLE_STACK_DEFAULT_ATT_MTU                 (23U)
45 
46 #define BLE_STACK_MEM_BLOCK_SIZE                  (32U)
47 
48 #define BLE_STACK_DEFAULT_NUM_EATT_CHANNELS       (0U)
49 
50 /**
51  * @name  BLE_STACK_L2CAP_MPS_OCTETS
52  * @brief Minimum and Maximum supported MPS values for L2CAP component.
53  * @see   BLE Spec. v.5.2, Vol.3, Part A, Sec.4.1, Table 4.1
54  * @{
55  */
56 #define BLE_STACK_L2CAP_MPS_OCTETS_MIN             (23U)
57 #define BLE_STACK_L2CAP_MPS_OCTETS_MAX             (1024U) /* MPS limited to 1KB */
58 /**
59  * @}
60  */
61 
62 
63 /**
64  * BLE_STACK_SM_SECURE_CONN_MTU: mtu size needed for Security Manager Channel Configuration
65  * Parameters with LE Secure Connections.
66  * 3.2 SECURITY MANAGER CHANNEL OVER L2CAP, BLUETOOTH SPECIFICATION Version 4.2
67  * [Vol 3, Part H]
68  */
69 #define BLE_STACK_SM_SECURE_CONN_MTU              (65U)
70 
71 /*
72  * BLE_STACK_MAX_ATT_MTU: maximum supported ATT_MTU size.
73  */
74 #define BLE_STACK_MAX_ATT_MTU                     (1024U - 6U)  // (6 bytes for L2CAP header)
75 
76 /**
77  * BLE_STACK_DEFAULT_MAX_ATT_MTU: default max ATT_MTU size.
78  */
79 #define BLE_STACK_DEFAULT_MAX_ATT_MTU             (251U - 6U) // (6 bytes for L2CAP header)
80 
81 /**
82  * BLE_STACK_DEFAULT_MAX_ATT_SIZE: maximum attribute size.
83  */
84 #define BLE_STACK_DEFAULT_MAX_ATT_SIZE            (512U)
85 
86 /**
87  * BLE_STACK_MEM_BLOCK_X_MTU(mtu, n_links): compute how many memory blocks are needed to
88  * support a maximum number (n_links) of simultaneous connections that use ATT
89  * packet with ATT_MTU=mtu.
90  * 7.2 FRAGMENTATION AND RECOMBINATION, BLUETOOTH SPECIFICATION Version 4.2
91  * [Vol 3, Part A]
92  */
93 
94 #define BLE_STACK_MEM_BLOCK_X_TX(mtu)             (DIV_CEIL((mtu) + 6U, BLE_STACK_MEM_BLOCK_SIZE) + 1U)
95 #define BLE_STACK_MEM_BLOCK_X_RX(mtu, n_link)     ((DIV_CEIL((mtu) + 6U, BLE_STACK_MEM_BLOCK_SIZE) + 2U) * (n_link) + 1U)
96 #define BLE_STACK_MEM_BLOCK_X_MTU(mtu, n_link)    (BLE_STACK_MEM_BLOCK_X_TX(mtu) + BLE_STACK_MEM_BLOCK_X_RX(mtu, n_link))
97 
98 /**
99  * Minimum number of blocks required for secure connections
100  */
101 #define BLE_STACK_MBLOCKS_SECURE_CONNECTIONS      (4U)
102 
103 /**
104  * BLE_STACK_MBLOCKS_CALC(mtu, n_link): minimum number of buffers needed by the stack.
105  * This is the minimum racomanded value and depends on:
106  *  - mtu: ATT_MTU size
107  *  - n_link: maximum number of simultaneous connections
108  *  - n_eatt_ch: number of EATT channels
109  */
110 #define BLE_STACK_MBLOCKS_CALC(mtu, n_link, n_eatt_ch) MAX(BLE_STACK_MEM_BLOCK_X_MTU(mtu, (n_link) + (n_eatt_ch)), \
111                                                            BLE_STACK_MBLOCKS_SECURE_CONNECTIONS)
112 
113 /**
114  * Default memory blocks count
115  */
116 #define BLE_STACK_DEFAULT_MBLOCKS_COUNT BLE_STACK_MBLOCKS_CALC(BLE_STACK_DEFAULT_MAX_ATT_MTU, \
117                                                                BLE_STACK_NUM_LINKS, \
118                                                                BLE_STACK_DEFAULT_NUM_EATT_CHANNELS)
119 
120 /**
121  * Amount of memory used for each GATT attribute entry.
122  */
123 #define BLE_STACK_GATT_ATTRIBUTE_SIZE (8U)
124 
125 /**
126  * Number of GATT attributes used for mandatory profiles (GATT and GAP).
127  */
128 #define BLE_STACK_NUM_GATT_MANDATORY_ATTRIBUTES (19U)
129 
130 /**
131  * Amount of memory needed by each GATT Client procedure context
132  */
133 #define COEFF_NUM_GATT_CLIENT_PROCS (36U)
134 
135 /**
136  * Amount of memory needed by each EATT channel.
137  */
138 #define COEFF_NUM_EATT_CHANNELS (32U)
139 
140 #define LL_TX_MAX_BUFFER_SIZE      (292UL)
141 #define LL_TX_DEFAULT_BUFFER_SIZE  (68UL)
142 #define LL_RX_MAX_BUFFER_SIZE      (260UL)
143 #define LL_RX_DEFAULT_BUFFER_SIZE  (44UL)
144 #define BLE_STACK_TX_BUFFER_SIZE(EXT_ADV_SCAN_EN, DL_EXT_EN, NUM_BRC_BIG, NUM_SYNC_BIG, NUM_CIG) \
145                                     ((((EXT_ADV_SCAN_EN) == 1U) || ((DL_EXT_EN) == 1U) || ((NUM_CIG) > 0U) || ((NUM_BRC_BIG) > 0U) || ((NUM_SYNC_BIG) > 0U)) ? LL_TX_MAX_BUFFER_SIZE : LL_TX_DEFAULT_BUFFER_SIZE)
146 #define BLE_STACK_RX_BUFFER_SIZE(EXT_ADV_SCAN_EN, DL_EXT_EN, NUM_BRC_BIG, NUM_SYNC_BIG, NUM_CIG) \
147                                     ((((EXT_ADV_SCAN_EN) == 1U) || ((DL_EXT_EN) == 1U) || ((NUM_CIG) > 0U) || ((NUM_BRC_BIG) > 0U) || ((NUM_SYNC_BIG) > 0U)) ? LL_RX_MAX_BUFFER_SIZE : LL_RX_DEFAULT_BUFFER_SIZE)
148 
149 /**
150  * Amount of memory needed to support ISO feature
151  */
152 #define BLE_STACK_ISO_CMN_SIZE(NUM_BRC_BIG, NUM_SYNC_BIG, NUM_CIG)    ((((NUM_BRC_BIG) + (NUM_SYNC_BIG) + (NUM_CIG)) > 0U) ?                \
153                                                                         (12U + 4U)                                                          \
154                                                                        : 0U)
155 
156 #define BLE_STACK_BIG_SIZE(NUM_BRC_BIG, NUM_BRC_BIS, NUM_SYNC_BIG, NUM_SYNC_BIS)     ((((NUM_BRC_BIG) + (NUM_SYNC_BIG)) > 0U) ?             \
157                                                                                         (248U * (NUM_BRC_BIG) +                             \
158                                                                                            8U * (NUM_BRC_BIG) * (NUM_BRC_BIS) +             \
159                                                                                          296U * (NUM_SYNC_BIG) +                            \
160                                                                                           20U * (NUM_SYNC_BIS) * (NUM_SYNC_BIG))            \
161                                                                                         : 0U)
162 
163 #define BLE_STACK_CIG_SIZE(NUM_CIG, NUM_CIS)    (((NUM_CIG) > 0U) ?                                                                         \
164                                                   (16U + 56U +                                                                              \
165                                                   168U * (NUM_CIG) +                                                                        \
166                                                   192U * (NUM_CIS))                                                                         \
167                                                  : 0U)
168 
169 #define BLE_STACK_ISOAL_SIZE(NUM_TX_STREAMS, NUM_RX_STREAMS)    ((((NUM_TX_STREAMS) + (NUM_RX_STREAMS)) > 0U) ?                             \
170                                                                     (20U +                                                                  \
171                                                                      12U * ((NUM_TX_STREAMS) + (NUM_RX_STREAMS)) +                          \
172                                                                      52U * (NUM_TX_STREAMS) +                                               \
173                                                                      28U * (NUM_RX_STREAMS))                                                \
174                                                                    : 0U)
175 
176 #define BLE_STACK_ISO_SIZE(NUM_BRC_BIG, NUM_BRC_BIS, NUM_SYNC_BIG, NUM_SYNC_BIS, NUM_CIG, NUM_CIS)                                          \
177                                            (BLE_STACK_ISO_CMN_SIZE(NUM_BRC_BIG, NUM_SYNC_BIG, NUM_CIG) +                                    \
178                                             BLE_STACK_BIG_SIZE(NUM_BRC_BIG, NUM_BRC_BIS, NUM_SYNC_BIG, NUM_SYNC_BIS) +                      \
179                                             BLE_STACK_CIG_SIZE(NUM_CIG, NUM_CIS) +                                                          \
180                                             BLE_STACK_ISOAL_SIZE((NUM_BRC_BIG) * (NUM_BRC_BIS) + (NUM_CIS),                                 \
181                                                                  (NUM_SYNC_BIG) * (NUM_SYNC_BIS) + (NUM_CIS)))
182 
183 
184 #if (BLESTACK_CONTROLLER_ONLY == 0)
185 
186 /**
187  * A part of the RAM, is dynamically allocated by initializing all the pointers
188  * defined in a global context variable "mem_alloc_ctx_p".
189  * This initialization is made in the Dynamic_allocator functions, which
190  * assign a portion of RAM given by the external application to the above
191  * mentioned "global pointers".
192  *
193  * The size of this Dynamic RAM is made of 2 main components:
194  * - a part that is parameters-dependent (num of links, GATT buffers, ...),
195  *   and whose value is made explicit by the following macros BLE_STACK_*_SIZE;
196  * - a part, that may be considered "fixed", i.e., independent from the above
197  *   mentioned parameters.
198 */
199 #   define BLE_STACK_FIXED_BUFFER_SIZE_BYTES (970U)
200 
201 /**
202  * Amount of memory needed by each radio link
203  */
204 #   define COEFF_CONN_SUPP_EN   (840U + BLE_STACK_GATT_ATTRIBUTE_SIZE * BLE_STACK_NUM_GATT_MANDATORY_ATTRIBUTES)
205 #   define COEFF_NUM_OF_LINKS_0 (48U)
206 #   define COEFF_NUM_OF_LINKS_1 (656U)
207 #   define BLE_STACK_LINKS_SIZE(CONN_SUPP_EN, NUM_OF_LINKS) \
208         (((CONN_SUPP_EN) == 1U) || ((NUM_OF_LINKS) > 0U) ? COEFF_CONN_SUPP_EN * (CONN_SUPP_EN) + COEFF_NUM_OF_LINKS_0 * (NUM_OF_LINKS) + COEFF_NUM_OF_LINKS_1 * (CONN_SUPP_EN) * (NUM_OF_LINKS) : 0U)
209 
210 /**
211  * Amount of memory needed for mem. blocks allocated for connections
212  */
213 #   define BLE_STACK_MBLOCK_SIZE(MEM_BLOCK_COUNT) ((4U + BLE_STACK_MEM_BLOCK_SIZE) * (MEM_BLOCK_COUNT))
214 
215 /**
216  * Amount of memory needed to support PHY Update feature
217  */
218 #   define BLE_STACK_PHY_UPD_SIZE(CONN_SUPP_EN, PHY_UPD_EN, NUM_OF_LINKS) (((PHY_UPD_EN) == 1U) ? 8U + 48U * (CONN_SUPP_EN) * (NUM_OF_LINKS) : 0U)
219 
220 /**
221  * Amount of memory needed to support extended advertising and scanning feature. Note that if
222  * extended advertising is enabled the memory required by the legacy advertising set must be
223  * subtracted from the total
224  */
225 #   define BLE_STACK_CONTROLLER_EXT_ADV_SIZE(ENABLED, NUM_ADV_SET, NUM_AUX_EVENT, NUM_OF_LINKS) \
226         (((ENABLED) == 1U) ? -276U + 368U * (NUM_ADV_SET) + 0U * (NUM_AUX_EVENT) + 0U * (NUM_OF_LINKS) : 0U)
227 
228 /**
229  * Amount of memory needed for the Filter Accept List and, if connections are supported, also for connection ID list
230  */
231 #   define BLE_STACK_FILTER_ACCEPT_LIST_SIZE(WL_SIZE_LOG2) (8U * (1U << (WL_SIZE_LOG2)))
232 #   define BLE_STACK_CONN_ID_LIST_SIZE(CONN_SUPP_EN, WL_SIZE_LOG2) (((CONN_SUPP_EN) == 1U) ? BLE_STACK_FILTER_ACCEPT_LIST_SIZE(WL_SIZE_LOG2) : 0U)
233 
234 /**
235  * Amount of memory needed to support L2CAP COCs (Connection Oriented Channels)
236  */
237 #   define BLE_STACK_L2C_COCS_SIZE(CONN_SUPP_EN, L2C_COS_EN, NUM_OF_COCS) (((CONN_SUPP_EN) == 1U) && ((L2C_COS_EN) == 1U) ? 84U * (NUM_OF_COCS) : 0U)
238 
239 /**
240  * Amount of memory needed to support Data Length Extension feature
241  */
242 #   define BLE_STACK_D_LEN_EXT_SIZE(LEN_EXT_ENABLED, ADV_EXT_ENABLED) ((((LEN_EXT_ENABLED) == 1U) || ((ADV_EXT_ENABLED) == 1U)) ? 0U : 0U)
243 
244 /**
245  * Amount of memory needed to support periodic advertising and synchronizing feature
246  */
247 #   define BLE_STACK_CONTROLLER_PERIODIC_ADV_WR_SIZE(PER_ADV_WR_EN, CONN_SUPP_EN, NUM_ADV_SET, NUM_SUBEVENTS_PAWR, MAX_SUBEVENT_DATA_COUNT_PAWR) \
248         ((((PER_ADV_WR_EN) == 1U) && ((CONN_SUPP_EN) == 1U)) ? (132U + 8U * (NUM_SUBEVENTS_PAWR) + 16U * (MAX_SUBEVENT_DATA_COUNT_PAWR)) * (NUM_ADV_SET) : 0U)
249 
250 #   define BLE_STACK_CONTROLLER_PERIODIC_ADV_SIZE(PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_LINKS, NUM_ADV_SET, NUM_SUBEVENTS_PAWR, MAX_SUBEVENT_DATA_COUNT_PAWR) \
251         (((PER_ADV_EN) == 1U) ? (156U * (NUM_ADV_SET) + 32U * (NUM_LINKS) + \
252                                  BLE_STACK_CONTROLLER_PERIODIC_ADV_WR_SIZE(PER_ADV_WR_EN, CONN_SUPP_EN, NUM_ADV_SET, NUM_SUBEVENTS_PAWR, MAX_SUBEVENT_DATA_COUNT_PAWR)) : \
253                                 0U)
254 
255 /**
256  * Amount of memory needed to support the scan feature
257  */
258 #   define BLE_STACK_BASE_SIZE(CONN_SUPP_EN) (((CONN_SUPP_EN) == 1U) ? 32U : 20U)
259 
260 #   define BLE_STACK_PAST_SIZE(CONN_SUPP_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) ? 24U * (NUM_OF_LINKS) : 0U)
261 
262 #   define BLE_STACK_PER_SYNC_SIZE(PER_ADV_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_OF_LINKS, ADV_LIST_LIZE)\
263         (((PER_ADV_EN) == 1U) ? BLE_STACK_BASE_SIZE(CONN_SUPP_EN) + 144U * (NUM_SYNC_SLOTS) + 8U * (1U << (ADV_LIST_LIZE)) + BLE_STACK_PAST_SIZE(CONN_SUPP_EN, NUM_OF_LINKS) : 0U)
264 
265 #   define BLE_STACK_PER_SYNC_WR_SIZE(PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_SYNC_SUBEVENTS)\
266         (((PER_ADV_WR_EN & PER_ADV_EN & CONN_SUPP_EN) == 1U) ? 176U * (NUM_SYNC_SLOTS) + ALIGN_32(NUM_SYNC_SUBEVENTS) : 0U)
267 
268 #   define BLE_STACK_CONTROLLER_SCAN_SIZE(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_AUX_EVENT,\
269                                           NUM_SYNC_SLOTS, NUM_OF_LINKS, ADV_LIST_LIZE, NUM_SYNC_SUBEVENTS) \
270         (((SCAN_EN) == 1U) ? 320U + (192U + 48U * (NUM_AUX_EVENT) + \
271             BLE_STACK_PER_SYNC_SIZE(PER_ADV_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_OF_LINKS, ADV_LIST_LIZE) + \
272             BLE_STACK_PER_SYNC_WR_SIZE(PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_SYNC_SUBEVENTS)) * (EXT_ADV_EN) : 0U)
273 
274 
275 /**
276  * Amount of memory needed to support controller privacy feature
277  */
278 #   define BLE_STACK_CONTROLLER_PRIV_SIZE(ENABLED, FILTER_ACCEPT_LIST_SIZE_LOG2) (((ENABLED) == 1U) ? 104U + 80U * (1U << (FILTER_ACCEPT_LIST_SIZE_LOG2)) : 0U)
279 
280 /**
281  * Amount of memory needed to support CTE feature
282  */
283 #   define BLE_STACK_CTE_NUM_OF_LINKS(CONN_SUPP_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) ? 8U * (NUM_OF_LINKS) : 0U)
284 #   define BLE_STACK_CTE_NUM_OF_ANT_IDS(CONN_SUPP_EN, NUM_OF_LINKS, NUM_OF_ANT_IDS)\
285         (((NUM_OF_ANT_IDS) > 0U) ? (1U + 0U * (CONN_SUPP_EN) * (NUM_OF_LINKS)) * ALIGN_32(NUM_OF_ANT_IDS) : 0U)
286     /** In the macro above, note that:
287      *  - the contribution associated with the nr. of links is 0 because the macro CONN_ANT_PATT is not defined
288      *  - the contribution associated with the nr. of antenna IDs is adapted to the word alignment
289      */
290 #   define BLE_STACK_CTE_NUM_OF_IQ_SAMPLES(NUM_OF_IQSAMPLES) (4U * (NUM_OF_IQSAMPLES))
291 #   define BLE_STACK_CTE_TIMER(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, NUM_OF_IQSAMPLES) \
292         ((((SCAN_EN) == 1U) && ((EXT_ADV_EN) == 1U) && ((PER_ADV_EN) == 1U) && ((NUM_OF_IQSAMPLES) > 0U)) ? 24U : 0U)
293 
294 #   define BLE_STACK_CTE_SIZE(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, CONN_SUPP_EN, CTE_EN, NUM_OF_LINKS, NUM_OF_ANT_IDS, NUM_OF_IQSAMPLES) \
295         (((CTE_EN) == 1U) ? 12U + \
296                             BLE_STACK_CTE_NUM_OF_LINKS(CONN_SUPP_EN, NUM_OF_LINKS) + \
297                             BLE_STACK_CTE_NUM_OF_ANT_IDS(CONN_SUPP_EN, NUM_OF_LINKS, NUM_OF_ANT_IDS) + \
298                             BLE_STACK_CTE_NUM_OF_IQ_SAMPLES(NUM_OF_IQSAMPLES) + \
299                             BLE_STACK_CTE_TIMER(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, NUM_OF_IQSAMPLES) : \
300                             0U)
301 
302 /**
303  * Amount of memory needed to support PCL feature
304  */
305 #   define BLE_STACK_PCL_NUM_OF_LINKS(NUM_OF_LINKS)  (32U * (NUM_OF_LINKS))
306 #   define BLE_STACK_PCL_SIZE(CONN_SUPP_EN, PCL_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) && ((PCL_EN) == 1U) ? 20U + BLE_STACK_PCL_NUM_OF_LINKS(NUM_OF_LINKS) : 0U)
307 
308 /**
309  * Amount of memory needed to support the LE Channel Classification feature
310  */
311 #   define BLE_STACK_CHC_NUM_OF_LINKS(NUM_OF_LINKS) (28U * (NUM_OF_LINKS))
312 #   define BLE_STACK_CHC_SIZE(CONN_SUPP_EN, CHC_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) && ((CHC_EN) == 1U) ? BLE_STACK_CHC_NUM_OF_LINKS(NUM_OF_LINKS) : 0U)
313 
314 /**
315  * Amount of memory needed by FIFOs
316  */
317 #   define CALC_FIFO_SIZE(FS) ((((FS) + 3U) >> 2U) << 2U)
318 #   define BLE_STACK_FIFO_SIZE(ISR0_FS, ISR1_FS, USER_FS) (CALC_FIFO_SIZE(ISR0_FS) + CALC_FIFO_SIZE(ISR1_FS) + CALC_FIFO_SIZE(USER_FS))
319 
320 /**
321  * Amount of memory needed by GATT when connection support is enabled
322  */
323 #   define BLE_STACK_GATT_SIZE(CONN_SUPP_EN, GATT_ATTRIBUTES, GATT_CLIENT_PROCS, GATT_NUM_EATT_CHANNELS) \
324         (((CONN_SUPP_EN) == 1U) ? (BLE_STACK_GATT_ATTRIBUTE_SIZE * (GATT_ATTRIBUTES) + \
325                                 COEFF_NUM_GATT_CLIENT_PROCS * (GATT_CLIENT_PROCS) + \
326                                 COEFF_NUM_EATT_CHANNELS * (GATT_NUM_EATT_CHANNELS)) \
327                                 : 0U)
328 
329 
330 #else // (BLESTACK_CONTROLLER_ONLY == 1)
331 
332 #   define BLE_STACK_FIXED_BUFFER_SIZE_BYTES (828U)
333 
334 /**
335  * Amount of memory needed by each radio link
336  */
337 #   define COEFF_CONN_SUPP_EN   (180U)
338 #   define COEFF_NUM_OF_LINKS_0 (48U)  // [DB] NOTE: scheduler's tasks
339 #   define COEFF_NUM_OF_LINKS_1 (484U)
340 #   define BLE_STACK_LINKS_SIZE(CONN_SUPP_EN, NUM_OF_LINKS) \
341         (((CONN_SUPP_EN) == 1U) || ((NUM_OF_LINKS) > 0U) ? COEFF_CONN_SUPP_EN * (CONN_SUPP_EN) + COEFF_NUM_OF_LINKS_0 * (NUM_OF_LINKS) + COEFF_NUM_OF_LINKS_1 * (CONN_SUPP_EN) * (NUM_OF_LINKS) : 0U)
342 
343 /**
344  * Amount of memory needed for mem. blocks allocated for connections
345  */
346 #   define BLE_STACK_MBLOCK_SIZE(MEM_BLOCK_COUNT) ((4U + BLE_STACK_MEM_BLOCK_SIZE) * (MEM_BLOCK_COUNT))
347 
348 /**
349  * Amount of memory needed to support PHY Update feature
350  */
351 #   define BLE_STACK_PHY_UPD_SIZE(CONN_SUPP_EN, PHY_UPD_EN, NUM_OF_LINKS) (((PHY_UPD_EN) == 1U) ? 16U * (CONN_SUPP_EN) * (NUM_OF_LINKS) : 0U)
352 
353 /**
354  * Amount of memory needed to support extended advertising and scanning feature. Note that if
355  * extended advertising is enabled the memory required by the legacy advertising set must be
356  * subtracted from the total
357  */
358 #   define BLE_STACK_CONTROLLER_EXT_ADV_SIZE(ENABLED, NUM_ADV_SET, NUM_AUX_EVENT, NUM_OF_LINKS) \
359         (((ENABLED) == 1U) ? -256U + 348U * (NUM_ADV_SET) + 0U * (NUM_AUX_EVENT) + 0U * (NUM_OF_LINKS) : 0U)
360 
361 /**
362  * Amount of memory needed for the Filter Accept List and, if connections are supported, also for connection ID list
363  */
364 #   define BLE_STACK_FILTER_ACCEPT_LIST_SIZE(WL_SIZE_LOG2) (8U * (1U << (WL_SIZE_LOG2)))
365 #   define BLE_STACK_CONN_ID_LIST_SIZE(CONN_SUPP_EN, WL_SIZE_LOG2) (((CONN_SUPP_EN) == 1U) ? BLE_STACK_FILTER_ACCEPT_LIST_SIZE(WL_SIZE_LOG2) : 0U)
366 
367 /**
368  * Amount of memory needed to support L2CAP COCs (Connection Oriented Channels)
369  */
370 #   define BLE_STACK_L2C_COCS_SIZE(CONN_SUPP_EN, L2C_COS_EN, NUM_OF_COCS) (0U)
371 
372 /**
373  * Amount of memory needed to support Data Length Extension feature
374  */
375 #   define BLE_STACK_D_LEN_EXT_SIZE(LEN_EXT_ENABLED, ADV_EXT_ENABLED) ((((LEN_EXT_ENABLED) == 1U) || ((ADV_EXT_ENABLED) == 1U)) ? 0U : 0U)
376 
377 /**
378  * Amount of memory needed to support periodic advertising and synchronizing feature
379  */
380 #   define BLE_STACK_CONTROLLER_PERIODIC_ADV_WR_SIZE(PER_ADV_WR_EN, CONN_SUPP_EN, NUM_ADV_SET, NUM_SUBEVENTS_PAWR, MAX_SUBEVENT_DATA_COUNT_PAWR) \
381         ((((PER_ADV_WR_EN) == 1U) && ((CONN_SUPP_EN) == 1U)) ? (132U + 8U * (NUM_SUBEVENTS_PAWR) + 16U * (MAX_SUBEVENT_DATA_COUNT_PAWR)) * (NUM_ADV_SET) : 0U)
382 
383 #   define BLE_STACK_CONTROLLER_PERIODIC_ADV_SIZE(PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_LINKS, NUM_ADV_SET, NUM_SUBEVENTS_PAWR, MAX_SUBEVENT_DATA_COUNT_PAWR) \
384         (((PER_ADV_EN) == 1U) ? (156U * (NUM_ADV_SET) + 32U * (NUM_LINKS) + \
385                                  BLE_STACK_CONTROLLER_PERIODIC_ADV_WR_SIZE(PER_ADV_WR_EN, CONN_SUPP_EN, NUM_ADV_SET, NUM_SUBEVENTS_PAWR, MAX_SUBEVENT_DATA_COUNT_PAWR)) : \
386                                 0U)
387 
388 /**
389  * Amount of memory needed to support the scan feature
390  */
391 #   define BLE_STACK_BASE_SIZE(CONN_SUPP_EN) (((CONN_SUPP_EN) == 1U) ? 32U : 20U)
392 
393 #   define BLE_STACK_PAST_SIZE(CONN_SUPP_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) ? 24U * (NUM_OF_LINKS) : 0U)
394 
395 #   define BLE_STACK_PER_SYNC_SIZE(PER_ADV_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_OF_LINKS, ADV_LIST_LIZE)\
396     (((PER_ADV_EN) == 1U) ? BLE_STACK_BASE_SIZE(CONN_SUPP_EN) + 144U * (NUM_SYNC_SLOTS) + 8U * (1U << (ADV_LIST_LIZE)) + BLE_STACK_PAST_SIZE(CONN_SUPP_EN, NUM_OF_LINKS) : 0U)
397 
398 #   define BLE_STACK_PER_SYNC_WR_SIZE(PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_SYNC_SUBEVENTS)\
399         (((PER_ADV_WR_EN & PER_ADV_EN & CONN_SUPP_EN) == 1U) ? 176U * (NUM_SYNC_SLOTS) + ALIGN_32(NUM_SYNC_SUBEVENTS) : 0U)
400 
401 #   define BLE_STACK_CONTROLLER_SCAN_SIZE(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_AUX_EVENT,\
402                                           NUM_SYNC_SLOTS, NUM_OF_LINKS, ADV_LIST_LIZE, NUM_SYNC_SUBEVENTS) \
403         (((SCAN_EN) == 1U) ? 320U + (164U + 48U * (NUM_AUX_EVENT) + \
404                              BLE_STACK_PER_SYNC_SIZE(PER_ADV_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_OF_LINKS, ADV_LIST_LIZE) + \
405                              BLE_STACK_PER_SYNC_WR_SIZE(PER_ADV_EN, PER_ADV_WR_EN, CONN_SUPP_EN, NUM_SYNC_SLOTS, NUM_SYNC_SUBEVENTS)) * (EXT_ADV_EN) : 0U)
406 
407 /**
408  * Amount of memory needed to support controller privacy feature
409  */
410 #   define BLE_STACK_CONTROLLER_PRIV_SIZE(ENABLED, FILTER_ACCEPT_LIST_SIZE_LOG2) (((ENABLED) == 1U) ? 104U + 80U * (1U << (FILTER_ACCEPT_LIST_SIZE_LOG2)) : 0U)
411 
412 /**
413  * Amount of memory needed to support CTE feature
414  */
415 #   define BLE_STACK_CTE_NUM_OF_LINKS(CONN_SUPP_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) ? 8U * (NUM_OF_LINKS) : 0U)
416 #   define BLE_STACK_CTE_NUM_OF_ANT_IDS(CONN_SUPP_EN, NUM_OF_LINKS, NUM_OF_ANT_IDS)\
417         (((NUM_OF_ANT_IDS) > 0U) ? (1U + 0U * (CONN_SUPP_EN) * (NUM_OF_LINKS)) * (1U + (((NUM_OF_ANT_IDS) - 1U) | 3U)) : 0U)
418     /** In the macro above, note that:
419      *  - the contribution associated with the nr. of links is 0 because the macro CONN_ANT_PATT is not defined
420      *  - the contribution associated with the nr. of antenna IDs is adapted to the word alignment
421      */
422 #   define BLE_STACK_CTE_NUM_OF_IQ_SAMPLES(NUM_OF_IQSAMPLES) (4U * (NUM_OF_IQSAMPLES))
423 
424 #   define BLE_STACK_CTE_TIMER(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, NUM_OF_IQSAMPLES) \
425         ((((SCAN_EN) == 1U) && ((EXT_ADV_EN) == 1U) && ((PER_ADV_EN) == 1U) && ((NUM_OF_IQSAMPLES) > 0U)) ? 24U : 0U)
426 
427 #   define BLE_STACK_CTE_SIZE(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, CONN_SUPP_EN, CTE_EN, NUM_OF_LINKS, NUM_OF_ANT_IDS, NUM_OF_IQSAMPLES) \
428         (((CTE_EN) == 1U) ? 12U + \
429                             BLE_STACK_CTE_NUM_OF_LINKS(CONN_SUPP_EN, NUM_OF_LINKS) + \
430                             BLE_STACK_CTE_NUM_OF_ANT_IDS(CONN_SUPP_EN, NUM_OF_LINKS, NUM_OF_ANT_IDS) + \
431                             BLE_STACK_CTE_NUM_OF_IQ_SAMPLES(NUM_OF_IQSAMPLES) + \
432                             BLE_STACK_CTE_TIMER(SCAN_EN, EXT_ADV_EN, PER_ADV_EN, NUM_OF_IQSAMPLES) : \
433                             0U)
434 
435 /**
436  * Amount of memory needed to support PCL feature
437  */
438 #   define BLE_STACK_PCL_NUM_OF_LINKS(NUM_OF_LINKS)  (32U * (NUM_OF_LINKS))
439 #   define BLE_STACK_PCL_SIZE(CONN_SUPP_EN, PCL_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) && ((PCL_EN) == 1U) ? 20U + BLE_STACK_PCL_NUM_OF_LINKS(NUM_OF_LINKS) : 0U)
440 
441 /**
442  * Amount of memory needed to support the LE Channel Classification feature
443  */
444 #   define BLE_STACK_CHC_NUM_OF_LINKS(NUM_OF_LINKS) (28U * (NUM_OF_LINKS))
445 #   define BLE_STACK_CHC_SIZE(CONN_SUPP_EN, CHC_EN, NUM_OF_LINKS) (((CONN_SUPP_EN) == 1U) && ((CHC_EN) == 1U) ? BLE_STACK_CHC_NUM_OF_LINKS(NUM_OF_LINKS) : 0U)
446 
447 /**
448  * Amount of memory needed by FIFOs
449  */
450 #   define CALC_FIFO_SIZE(FS) ((((FS) + 3U) >> 2U) << 2U)
451 #   define BLE_STACK_FIFO_SIZE(ISR0_FS, ISR1_FS, USER_FS) (CALC_FIFO_SIZE(ISR0_FS) + CALC_FIFO_SIZE(ISR1_FS) + CALC_FIFO_SIZE(USER_FS))
452 
453 /**
454  * Amount of memory needed by GATT when connection support is enabled
455  */
456 #   define BLE_STACK_GATT_SIZE(CONN_SUPP_EN, GATT_ATTRIBUTES, GATT_CLIENT_PROCS, NUM_EATT_CHANNELS) (0U)
457 
458 #endif // BLESTACK_CONTROLLER_ONLY
459 
460 /**
461  *
462  * This macro returns the amount of memory, in bytes, needed for the storage of GATT database elements
463  * and other data structures whose size depends on the number of supported connections.
464  *
465  * @param BLE_STACK_NUM_LINKS: Maximum number of simultaneous connections that the device will support.
466  * @param BLE_STACK_NUM_EATT_CHANNELS: Maximum number of simultaneous EATT active channels that the device will support.
467  * @param BLE_STACK_NUM_GATT_ATTRIBUTES: Maximum number of Attributes (i.e. the number of characteristic + the number of characteristic values + the number of descriptors, excluding the services) that can be stored in the GATT database. Note that certain characteristics and relative descriptors are added automatically during device initialization so this parameters should be 9 plus the number of user Attributes
468  * @param BLE_STACK_NUM_GATT_CLIENT_PROCS: Maximum number of concurrent GATT Client Procedures.
469  * @param BLE_STACK_MBLOCKS_COUNT: Number of memory blocks allocated for packets.
470  * @param BLE_STACK_D_LEN_EXT_EN: Enable or disable the Extended Packet length feature. Valid values are 0 or 1.
471  * @param BLE_STACK_PHY_UPD_EN: Enable or disable the PHY Update feature. Valid values are 0 or 1.
472  * @param BLE_STACK_EXT_ADV_SCAN_EN: Enable or disable the Extended Advertising and Scanning feature. Valid values are 0 or 1.
473  * @param BLE_STACK_CTRL_PRIV_EN: Enable or disable the Controller privacy feature. Valid values are 0 or 1.
474  * @param BLE_STACK_SEC_CONN_EN: Enable or disable the secure connection feature. Valid values are 0 or 1.
475  * @param BLE_STACK_SCAN_EN: Enable or disable the Scan feature. Valid values are 0 or 1.
476  * @param NUM_ADV_SET: Number of advertising sets. Valid values are > 0.
477  * @param NUM_SUBEVENTS_PAWR: Number of periodic advertising with responses (PAwR) subevents. Valid values are > 0 if PAwR is supported.
478  * @param MAX_SUBEVENT_DATA_COUNT_PAWR: Maximum number of periodic advertising with responses (PAwR) subevents that data can be requested for. Valid values are > 0 if PAwR is supported.
479  * @param NUM_SUBEVENTS_PAWR: Maximum number of PAwR subevents.
480  * @param NUM_AUX_EVENT: Number of radio tasks to allocate for secondary channels scanning (i.e., auxiliary events).
481  * @param FILTER_ACCEPT_LIST_SIZE_LOG2: log2 ceiling of Filter Accept List size.
482  * @param BLE_STACK_L2CAP_COS_EN: Enable or disable the L2CAP Channel Oriented Stream feature. Valid values are 0 or 1.
483  * @param NUM_L2CAP_COCS: Number of L2CAP COS channels.
484  * @param CONTROLLER_PERIODIC_ADV_EN: Enable or disable the Periodic Advertising and Synchronization feature. Valid values are 0 or 1.
485  * @param CONTROLLER_PERIODIC_ADV_WR_EN: Enable or disable the Periodic Advertising and Synchronization with Responses feature. Valid values are 0 or 1.
486  * @param NUM_SYNC_SLOTS: Number of radio tasks to allocate for synchronizing to periodic advertisements.
487  * @param BLE_STACK_CTE_EN: Enable or disable the CTE feature. Valid values are 0 or 1.
488  * @param NUM_CTE_ANT_IDS: Maximum antenna switching pattern length
489  * @param NUM_CTE_IQSAMPLES: Maximum number of IQ-Samples in the buffer
490  * @param BLE_STACK_PCL_EN: Enable or disable the Power Control & Path Loss Monitoring feature. Valid values are 0 or 1.
491  * @param BLE_STACK_CONN_SUPP_EN: Enable or disable the support of ACL connections.
492  * @param BLE_STACK_CHC_EN: Enable or disable the LE Channel Classification feature. Valid values are 0 or 1.
493  * @param BLE_STACK_NUM_SYNC_BIG: Number of BIG Synchronous groups to support.
494  * @param BLE_STACK_NUM_BRC_BIG: Number of BIG Broadcaster groups to support.
495  * @param BLE_STACK_NUM_SYNC_BIS: Number of BIG Synchronous streams per group to support.
496  * @param BLE_STACK_NUM_BRC_BIS: Number of BIG Broadcaster streams per group to support.
497  * @param BLE_STACK_NUM_CIG: Number of CIG groups to support.
498  * @param BLE_STACK_NUM_CIS: Number of CIS streams to support.
499  * @param BLE_STACK_CIS_EN: Enable or disable the Connected Isochronous Streams feature. Valid values are 0 or 1.
500  * @param ISR0_FIFO_SIZE: Size of the internal FIFO used for critical controller events produced by the ISR (e.g. rx data packets)
501  * @param ISR1_FIFO_SIZE: Size of the internal FIFO used for non-critical controller events produced by the ISR (e.g. advertising or IQ sampling reports)
502  * @param USER_FIFO_SIZE: Size of the internal FIFO used for controller and host events produced outside the ISR
503  */
504 #define BLE_STACK_TOTAL_BUFFER_SIZE_EXT(\
505     BLE_STACK_NUM_LINKS,\
506     BLE_STACK_NUM_EATT_CHANNELS,\
507     BLE_STACK_NUM_GATT_ATTRIBUTES,\
508     BLE_STACK_NUM_GATT_CLIENT_PROCS,\
509     BLE_STACK_MBLOCKS_COUNT,\
510     BLE_STACK_D_LEN_EXT_EN,\
511     BLE_STACK_PHY_UPD_EN,\
512     BLE_STACK_EXT_ADV_SCAN_EN,\
513     BLE_STACK_CTRL_PRIV_EN,\
514     BLE_STACK_SEC_CONN_EN,\
515     BLE_STACK_SCAN_EN,\
516     NUM_ADV_SET,\
517     NUM_SUBEVENTS_PAWR,\
518     MAX_SUBEVENT_DATA_COUNT_PAWR,\
519     NUM_AUX_EVENT,\
520     FILTER_ACCEPT_LIST_SIZE_LOG2,\
521     BLE_STACK_L2CAP_COS_EN,\
522     NUM_L2CAP_COCS,\
523     CONTROLLER_PERIODIC_ADV_EN,\
524     CONTROLLER_PERIODIC_ADV_WR_EN,\
525     NUM_SYNC_SLOTS,\
526     BLE_STACK_CTE_EN,\
527     NUM_CTE_ANT_IDS,\
528     NUM_CTE_IQSAMPLES,\
529     BLE_STACK_PCL_EN,\
530     BLE_STACK_CONN_SUPP_EN,\
531     BLE_STACK_CHC_EN,\
532     BLE_STACK_NUM_SYNC_BIG,\
533     BLE_STACK_NUM_BRC_BIG,\
534     BLE_STACK_NUM_SYNC_BIS,\
535     BLE_STACK_NUM_BRC_BIS,\
536     BLE_STACK_NUM_CIG,\
537     BLE_STACK_NUM_CIS,\
538     ISR0_FIFO_SIZE,\
539     ISR1_FIFO_SIZE,\
540     USER_FIFO_SIZE)\
541 (\
542   BLE_STACK_FIXED_BUFFER_SIZE_BYTES                                                                             + \
543   BLE_STACK_LINKS_SIZE(BLE_STACK_CONN_SUPP_EN, BLE_STACK_NUM_LINKS)                                             + \
544   BLE_STACK_GATT_SIZE(BLE_STACK_CONN_SUPP_EN, BLE_STACK_NUM_GATT_ATTRIBUTES, BLE_STACK_NUM_GATT_CLIENT_PROCS,     \
545                       BLE_STACK_NUM_EATT_CHANNELS)                                                              + \
546   BLE_STACK_MBLOCK_SIZE(BLE_STACK_MBLOCKS_COUNT)                                                                + \
547   BLE_STACK_D_LEN_EXT_SIZE(BLE_STACK_D_LEN_EXT_EN, BLE_STACK_EXT_ADV_SCAN_EN)                                   + \
548   BLE_STACK_PHY_UPD_SIZE(BLE_STACK_CONN_SUPP_EN, BLE_STACK_PHY_UPD_EN, BLE_STACK_NUM_LINKS)                     + \
549   BLE_STACK_TX_BUFFER_SIZE(BLE_STACK_EXT_ADV_SCAN_EN, BLE_STACK_D_LEN_EXT_EN, BLE_STACK_NUM_SYNC_BIG,             \
550                              BLE_STACK_NUM_BRC_BIG, BLE_STACK_NUM_CIG)                                          + \
551   BLE_STACK_RX_BUFFER_SIZE(BLE_STACK_EXT_ADV_SCAN_EN, BLE_STACK_D_LEN_EXT_EN, BLE_STACK_NUM_SYNC_BIG,             \
552                              BLE_STACK_NUM_BRC_BIG, BLE_STACK_NUM_CIG)                                          + \
553   BLE_STACK_CONTROLLER_EXT_ADV_SIZE(BLE_STACK_EXT_ADV_SCAN_EN, NUM_ADV_SET, NUM_AUX_EVENT, BLE_STACK_NUM_LINKS) + \
554   BLE_STACK_FILTER_ACCEPT_LIST_SIZE(FILTER_ACCEPT_LIST_SIZE_LOG2)                                               + \
555   BLE_STACK_CONN_ID_LIST_SIZE(BLE_STACK_CONN_SUPP_EN, FILTER_ACCEPT_LIST_SIZE_LOG2)                             + \
556   BLE_STACK_CONTROLLER_PRIV_SIZE(BLE_STACK_CTRL_PRIV_EN, FILTER_ACCEPT_LIST_SIZE_LOG2)                          + \
557   (0U * (BLE_STACK_SEC_CONN_EN))                                                                                + \
558   BLE_STACK_CONTROLLER_SCAN_SIZE(                                                                                 \
559     BLE_STACK_SCAN_EN, BLE_STACK_EXT_ADV_SCAN_EN, CONTROLLER_PERIODIC_ADV_EN, CONTROLLER_PERIODIC_ADV_WR_EN,      \
560     BLE_STACK_CONN_SUPP_EN, NUM_AUX_EVENT, NUM_SYNC_SLOTS, BLE_STACK_NUM_LINKS, FILTER_ACCEPT_LIST_SIZE_LOG2,     \
561     NUM_SUBEVENTS_PAWR)                                                                                         + \
562   BLE_STACK_L2C_COCS_SIZE(BLE_STACK_CONN_SUPP_EN, BLE_STACK_L2CAP_COS_EN, NUM_L2CAP_COCS)                       + \
563   BLE_STACK_CONTROLLER_PERIODIC_ADV_SIZE(                                                                         \
564     CONTROLLER_PERIODIC_ADV_EN, CONTROLLER_PERIODIC_ADV_WR_EN, BLE_STACK_CONN_SUPP_EN,                            \
565     BLE_STACK_NUM_LINKS, NUM_ADV_SET, NUM_SUBEVENTS_PAWR, MAX_SUBEVENT_DATA_COUNT_PAWR)                         + \
566   BLE_STACK_CTE_SIZE(                                                                                             \
567     BLE_STACK_SCAN_EN, BLE_STACK_EXT_ADV_SCAN_EN, CONTROLLER_PERIODIC_ADV_EN, BLE_STACK_CONN_SUPP_EN,             \
568     BLE_STACK_CTE_EN, BLE_STACK_NUM_LINKS, NUM_CTE_ANT_IDS, NUM_CTE_IQSAMPLES)                                  + \
569   BLE_STACK_PCL_SIZE(BLE_STACK_CONN_SUPP_EN, BLE_STACK_PCL_EN, BLE_STACK_NUM_LINKS)                             + \
570   BLE_STACK_CHC_SIZE(BLE_STACK_CONN_SUPP_EN, BLE_STACK_CHC_EN, BLE_STACK_NUM_LINKS)                             + \
571   BLE_STACK_ISO_SIZE(BLE_STACK_NUM_BRC_BIG, BLE_STACK_NUM_BRC_BIS,                                                \
572                      BLE_STACK_NUM_SYNC_BIG, BLE_STACK_NUM_SYNC_BIS,                                              \
573                      BLE_STACK_NUM_CIG, BLE_STACK_NUM_CIS)                                                      + \
574   BLE_STACK_FIFO_SIZE(ISR0_FIFO_SIZE, ISR1_FIFO_SIZE, USER_FIFO_SIZE)                                             \
575 )
576 
577 /**
578  *
579  * This macro acts like the BLE_STACK_TOTAL_BUFFER_SIZE_EXT macro while makes use of the
580  * modularity parameters defined in the app_conf.h file
581  *
582  * @param BLE_STACK_NUM_LINKS: Maximum number of simultaneous radio tasks that the device will support (Radio controller supports up to 128 simultaneous radio tasks, but actual usable max value depends
583 on the available device RAM)
584  * @param BLE_STACK_NUM_EATT_CHANNELS: Maximum number of simultaneous EATT active channels that the device will support.
585  * @param BLE_STACK_NUM_GATT_ATTRIBUTES: Maximum number of Attributes (i.e. the number of characteristic + the number of characteristic values + the number of descriptors, excluding the services) that can be stored in the GATT database. Note that certain characteristics and relative descriptors are added automatically during device initialization so this parameters should be 9 plus the number of user Attributes
586  * @param BLE_STACK_NUM_GATT_CLIENT_PROCS: Maximum number of concurrent GATT Client Procedures.
587  * @param BLE_STACK_MBLOCKS_COUNT: Number of memory blocks allocated for packets.
588  * @param BLE_STACK_NUM_ADV_SET: Number of advertising sets.
589  * @param BLE_STACK_NUM_ADV_SET_PAWR: Number of advertising sets for periodic advertising with responses.
590  * @param BLE_STACK_NUM_SUBEVENTS_PAWR: Number of periodic advertising with responses subevents.
591  * @param BLE_STACK_SUBEVENT_DATA_COUNT_PAWR: Maximum number of periodic advertising with responses subevents that data can be requested for
592  * @param BLE_STACK_NUM_AUX_EVENT: Number of auxiliary scan slots.
593  * @param BLE_STACK_FILTER_ACCEPT_LIST_SIZE_LOG2: log2 ceiling of Filter Accept List size.
594  * @param BLE_STACK_NUM_L2CAP_COCS: number of L2CAP Connection Oriented Channels supported.
595  * @param NUM_SYNC_SLOTS: Number of radio tasks to allocate for synchronizing to periodic advertisements.
596  * @param BLE_STACK_NUM_CTE_ANT_IDS: Maximum antenna switching pattern length .
597  * @param BLE_STACK_NUM_IQSAMPLES: Maximum number of IQ-Samples in the buffer.
598  * @param ISR0_FIFO_SIZE: Size of the internal FIFO used for critical controller events produced by the ISR (e.g. rx data packets).
599  * @param ISR1_FIFO_SIZE: Size of the internal FIFO used for non-critical controller events produced by the ISR (e.g. advertising or IQ sampling reports).
600  * @param USER_FIFO_SIZE: Size of the internal FIFO used for controller and host events produced outside the ISR.
601  */
602 #define BLE_STACK_TOTAL_BUFFER_SIZE(\
603     BLE_STACK_NUM_LINKS,\
604     BLE_STACK_NUM_EATT_CHANNELS,\
605     BLE_STACK_NUM_GATT_ATTRIBUTES,\
606     BLE_STACK_NUM_GATT_CLIENT_PROCS,\
607     BLE_STACK_MBLOCKS_COUNT,\
608     BLE_STACK_NUM_ADV_SET,\
609     BLE_STACK_NUM_SUBEVENTS_PAWR,\
610     BLE_STACK_SUBEVENT_DATA_COUNT_PAWR,\
611     BLE_STACK_NUM_AUX_EVENT,\
612     BLE_STACK_FILTER_ACCEPT_LIST_SIZE_LOG2,\
613     BLE_STACK_NUM_L2CAP_COCS,\
614     NUM_SYNC_SLOTS,\
615     BLE_STACK_NUM_CTE_ANT_IDS,\
616     BLE_STACK_NUM_IQSAMPLES,\
617     BLE_STACK_NUM_SYNC_BIG,\
618     BLE_STACK_NUM_BRC_BIG,\
619     BLE_STACK_NUM_SYNC_BIS,\
620     BLE_STACK_NUM_BRC_BIS,\
621     BLE_STACK_NUM_CIG,\
622     BLE_STACK_NUM_CIS,\
623     ISR0_FIFO_SIZE,\
624     ISR1_FIFO_SIZE,\
625     USER_FIFO_SIZE)\
626 (\
627     BLE_STACK_TOTAL_BUFFER_SIZE_EXT(\
628         BLE_STACK_NUM_LINKS,\
629         BLE_STACK_NUM_EATT_CHANNELS,\
630         BLE_STACK_NUM_GATT_ATTRIBUTES,\
631         BLE_STACK_NUM_GATT_CLIENT_PROCS,\
632         BLE_STACK_MBLOCKS_COUNT,\
633         CONTROLLER_DATA_LENGTH_EXTENSION_ENABLED,\
634         CONTROLLER_2M_CODED_PHY_ENABLED,\
635         CONTROLLER_EXT_ADV_SCAN_ENABLED,\
636         CONTROLLER_PRIVACY_ENABLED,\
637         SECURE_CONNECTIONS_ENABLED,\
638         CONTROLLER_SCAN_ENABLED,\
639         BLE_STACK_NUM_ADV_SET,\
640         BLE_STACK_NUM_SUBEVENTS_PAWR,\
641         BLE_STACK_SUBEVENT_DATA_COUNT_PAWR,\
642         BLE_STACK_NUM_AUX_EVENT,\
643         BLE_STACK_FILTER_ACCEPT_LIST_SIZE_LOG2,\
644         L2CAP_COS_ENABLED,\
645         BLE_STACK_NUM_L2CAP_COCS,\
646         CONTROLLER_PERIODIC_ADV_ENABLED,\
647         CONTROLLER_PERIODIC_ADV_WR_ENABLED,\
648         NUM_SYNC_SLOTS,\
649         CONTROLLER_CTE_ENABLED,\
650         BLE_STACK_NUM_CTE_ANT_IDS,\
651         BLE_STACK_NUM_IQSAMPLES,\
652         CONTROLLER_POWER_CONTROL_ENABLED,\
653         CONNECTION_ENABLED,\
654         CONTROLLER_CHAN_CLASS_ENABLED,\
655         BLE_STACK_NUM_SYNC_BIG,\
656         BLE_STACK_NUM_BRC_BIG,\
657         BLE_STACK_NUM_SYNC_BIS,\
658         BLE_STACK_NUM_BRC_BIS,\
659         BLE_STACK_NUM_CIG,\
660         BLE_STACK_NUM_CIS,\
661         ISR0_FIFO_SIZE,\
662         ISR1_FIFO_SIZE,\
663         USER_FIFO_SIZE\
664 ))
665 
666 /**
667 *
668 * This structure contains memory and low level hardware configuration data for the device
669 */
670 typedef struct {
671   uint8_t* BLEStartRamAddress;      /**< Start address of the RAM buffer required by the Bluetooth stack. It must be 32-bit aligned. Use BLE_STACK_TOTAL_BUFFER_SIZE to calculate the correct size. */
672   uint32_t TotalBufferSize;         /**< BLE_STACK_TOTAL_BUFFER_SIZE return value, used to check the MACRO correctness*/
673   uint16_t NumAttrRecords;          /**< Maximum number of attributes that can be stored in the GATT database. */
674   uint8_t MaxNumOfClientProcs;      /**< Maximum number of concurrent client's procedures. This value shall be less or equal to NumOfRadioTasks. */
675   uint8_t NumOfRadioTasks;          /**< Maximum number of simultaneous radio tasks. Radio controller supports up to 128 simultaneous radio tasks, but actual usable max value depends on the available device RAM (NUM_LINKS used in the calculation of BLE_STACK_TOTAL_BUFFER_SIZE). */
676   uint8_t NumOfEATTChannels;        /**< Maximum number of simultaneous EATT active channels */
677   uint16_t NumBlockCount;           /**< Number of allocated memory blocks */
678   uint16_t ATT_MTU;                 /**< Maximum supported ATT_MTU size [23-1020]*/
679   uint32_t MaxConnEventLength;      /**< Maximum duration of the connection event when the device is peripheral, in units of 625/256 us (~2.44 us) */
680   uint16_t SleepClockAccuracy;      /**< Sleep clock accuracy (ppm value)*/
681   uint8_t NumOfAdvDataSet;          /**< Maximum number of advertising data sets, valid only when Advertising Extension Feature is enabled  */
682   uint8_t NumOfSubeventsPAwR;       /**< Maximum number of Periodic Advertising with Responses subevents */
683   uint8_t MaxPAwRSubeventDataCount; /**< Maximum number of Periodic Advertising with Responses subevents that data can be requested for */
684   uint8_t NumOfAuxScanSlots;        /**< Maximum number of slots for scanning on the secondary advertising channel, valid only when Advertising Extension Feature is enabled  */
685   uint8_t NumOfSyncSlots;           /**< Maximum number of slots for synchronizing to a periodic advertising train, valid only when Periodic Advertising and Synchronizing Feature is enabled  */
686   uint8_t FilterAcceptListSizeLog2; /**< Two's logarithm of Filter Accept, Resolving and advertiser list size. */
687   uint16_t L2CAP_MPS;               /**< The maximum size of payload data in octets that the L2CAP layer entity is capable of accepting [0-1024].*/
688   uint8_t L2CAP_NumChannels;        /**< Maximum number of channels in LE Credit Based Flow Control mode [0-255].*/
689   uint8_t CTE_MaxNumAntennaIDs;     /**< Maximum number of Antenna IDs in the antenna pattern used in CTE connection oriented mode. */
690   uint8_t CTE_MaxNumIQSamples;      /**< Maximum number of IQ samples in the buffer used in CTE connection oriented mode. */
691   uint8_t NumOfSyncBIG;             /**< Maximum number of ISO Synchronizer groups. */
692   uint8_t NumOfBrcBIG;              /**< Maximum number of ISO Broadcaster groups. */
693   uint8_t NumOfSyncBIS;             /**< Maximum number of ISO Synchronizer streams. */
694   uint8_t NumOfBrcBIS;              /**< Maximum number of ISO Broadcaster streams. */
695   uint8_t NumOfCIG;                 /**< Maximum number of Connected Isochronous Groups. */
696   uint8_t NumOfCIS;                 /**< Maximum number of Connected Isochronous Streams. */
697   uint16_t isr0_fifo_size;          /**< Size of the internal FIFO used for critical controller events produced by the ISR (e.g. rx data packets)*/
698   uint16_t isr1_fifo_size;          /**< Size of the internal FIFO used for non-critical controller events produced by the ISR (e.g. advertising or IQ sampling reports)*/
699   uint16_t user_fifo_size;          /**< Size of the internal FIFO used for controller and host events produced outside the ISR */
700 } BLE_STACK_InitTypeDef;
701 
702 
703 /**
704  * @brief This function executes the processing of all Host Stack layers.
705  *
706  * The BLE Stack Tick function has to be executed regularly to process incoming Link Layer packets and to process Host Layers procedures. All
707  * stack callbacks are called by this function.
708  *
709  * If Low Speed Ring Oscillator is used instead of the LS Crystal oscillator this function performs also the LS RO calibration and hence must
710  * be called at least once at every system wake-up in order to keep the 500 ppm accuracy (at least 500 ppm accuracy is mandatory if acting as a Central).
711  *
712  * No BLE stack function must be called while the BLE_STACK_Tick is running. For example, if a BLE stack function may be called inside an
713  * interrupt routine, that interrupt must be disabled during the execution of BLE_STACK_Tick(). Example (if a stack function may be called inside
714  * UART ISR):
715  * @code
716  * NVIC_DisableIRQ(UART_IRQn);
717  * BLE_STACK_Tick();
718  * NVIC_EnableIRQ(UART_IRQn);
719  * @endcode
720 
721  * @note The API name and parameters are subject to change in future releases.
722  * @return None
723  */
724 void BLE_STACK_Tick(void);
725 
726 /**
727  * @brief The BLE Stack initialization routine
728  *
729  * @note The API name and parameters are subject to change in future releases.
730  *
731  * @param[in]  BLE_STACK_InitStruct      pointer to the const structure containing memory and low level
732  *                                              hardware configuration data for the device
733  *
734  * @return Value indicating success or error code.
735  *
736  */
737 tBleStatus BLE_STACK_Init(const BLE_STACK_InitTypeDef *BLE_STACK_InitStruct);
738 
739 /**
740  * @brief This function is called when an event is coming from the BLE stack
741  *
742  * @param  hci_pckt: The user event received from the BLE stack
743  * @param  length:   Length of the event
744  * @retval None
745  */
746 void BLE_STACK_Event(hci_pckt *hci_pckt, uint16_t length);
747 
748 
749 /**
750  * @brief Returns the BLE Stack matching sleep mode
751  *
752  * @note The API name and parameters are subject to change in future releases.
753  *
754  * @return
755  *  SLEEPMODE_RUNNING       = 0,
756  *  SLEEPMODE_NOTIMER       = 3,
757  */
758 uint8_t BLE_STACK_SleepCheck(void);
759 
760 /**
761  *
762  * @brief Radio ISR routine.
763  *
764  * This is the base function called for any radio ISR.
765  *
766  * @param[in]  BlueInterrupt    value of the radio interrupt register
767  *
768  * @return None
769  */
770 void BLE_STACK_RadioHandler(uint32_t BlueInterrupt);
771 
772 /**
773   * @brief This function provide information when a new radio activity will be performed.
774 Information provided includes type of radio activity and absolute time in system ticks when a new radio activity is schedule, if any.
775   * @param NextStateSysTime 32bit absolute current time expressed in internal time units.
776   * @retval Value indicating the next state:
777   - 0x00: Idle
778   - 0x01: Advertising
779   - 0x02: Connection event Peripheral
780   - 0x03: Scanning
781   - 0x04: Connection request
782   - 0x05: Connection event Central
783   - 0x06: TX test mode
784   - 0x07: RX test mode
785   */
786 uint8_t BLE_STACK_ReadNextRadioActivity(uint32_t *NextStateSysTime);
787 
788 /* Statistics per link */
789 typedef struct llc_conn_per_statistic_s {
790 
791     uint16_t num_pkts;          /**< The number of received packets, valid or with CRC errors. */
792     uint16_t num_crc_err;       /**< The number of packets received with CRC errors. */
793     uint16_t num_evts;          /**< The number of past connection events, including missed ones. */
794     uint16_t num_miss_evts;     /**< The number of missed RX packets, because of RX timeout or invalid packet length. */
795 
796 } llc_conn_per_statistic_st;
797 
798 /**
799  * @brief     LLC function to collect statistics per link:
800  *             - statistics are stored in a buffer allocated by the application
801  *             - counters are reset every time the function is called
802  *             - counters are stopped when the function is called with a pointer to NULL
803  *
804  * @param[in] conn_handle Connection handle that identifies the connection
805  * @param[in] statistics_p pointer to the structure where statistics will be noted
806  * @return    BLE_ERROR_UNKNOWN_CONNECTION_ID in case of invalid conn_handle
807  *            BLE_STATUS_SUCCESS otherwise
808  */
809 tBleStatus llc_conn_per_statistic(uint16_t conn_handle,
810                                   llc_conn_per_statistic_st *statistics_p);
811 
812 
813 /* Statistics per link and per channel */
814 #if !defined(LLC_MAX_NUM_DATA_CHAN)
815 #   define LLC_MAX_NUM_DATA_CHAN (37U)
816 #endif
817 
818 typedef struct llc_conn_per_statistic_by_channel_s {
819     uint16_t num_pkts[LLC_MAX_NUM_DATA_CHAN];              /**< The number of received packets, valid or with CRC errors. */
820     uint16_t num_crc_err[LLC_MAX_NUM_DATA_CHAN];           /**< The number of packets received with CRC errors. */
821     uint16_t num_miss_evts[LLC_MAX_NUM_DATA_CHAN];         /**< The number of missed RX events, because of RX timeout or invalid packet length. */
822 } llc_conn_per_statistic_by_channel_st;
823 
824 /**
825  * @brief     LLC function to collect statistics per link and per channel:
826  *             - statistics are stored in a buffer allocated by the application
827  *             - counters are reset every time the function is called
828  *             - counters are stopped when the function is called with a pointer to NULL
829  *
830  * @param[in] conn_handle - connection handle that identifies the connection
831  * @param[in] statistics_p - pointer to the structure where statistics will be noted
832  * @return    BLE_ERROR_UNKNOWN_CONNECTION_ID in case of invalid conn_handle
833  *            BLE_STATUS_SUCCESS otherwise
834  */
835 tBleStatus llc_conn_per_statistic_by_channel(uint16_t conn_handle,
836                                              llc_conn_per_statistic_by_channel_st *statistics_p);
837 
838 
839 #endif /* BLE_STACK_H */
840 
841