1 /*****************************************************************************
2  * @file    ble_bufsize.h
3  *
4  * @brief   Definition of BLE stack buffers size
5  *****************************************************************************
6  * @attention
7  *
8  * Copyright (c) 2018-2024 STMicroelectronics.
9  * All rights reserved.
10  *
11  * This software is licensed under terms that can be found in the LICENSE file
12  * in the root directory of this software component.
13  * If no LICENSE file comes with this software, it is provided AS-IS.
14  *
15  *****************************************************************************
16  */
17 
18 #ifndef BLE_BUFSIZE_H__
19 #define BLE_BUFSIZE_H__
20 
21 
22 /*
23  * BLE_DEFAULT_ATT_MTU: minimum MTU value that GATT must support.
24  */
25 #define BLE_DEFAULT_ATT_MTU                  23
26 
27 /*
28  * BLE_DEFAULT_MAX_ATT_SIZE: maximum attribute size.
29  */
30 #define BLE_DEFAULT_MAX_ATT_SIZE            512
31 
32 /*
33  * BLE_PREP_WRITE_X_ATT: compute how many Prepare Write Request are needed to
34  * write a characteristic with size 'max_att' when the used ATT_MTU value is
35  * equal to BLE_DEFAULT_ATT_MTU (23).
36  */
37 #define BLE_PREP_WRITE_X_ATT(max_att) \
38         (DIVC(max_att, BLE_DEFAULT_ATT_MTU - 5) * 2)
39 
40 /*
41  * BLE_DEFAULT_PREP_WRITE_LIST_SIZE: default minimum Prepare Write List size.
42  */
43 #define BLE_DEFAULT_PREP_WRITE_LIST_SIZE \
44         BLE_PREP_WRITE_X_ATT(BLE_DEFAULT_MAX_ATT_SIZE)
45 
46 /*
47  * BLE_MEM_BLOCK_X_MTU: compute how many memory blocks are needed to compose
48  * an ATT packet with ATT_MTU=mtu.
49  */
50 #define BLE_MEM_BLOCK_SIZE                   32
51 
52 #if (SLAVE_ONLY != 0) || (BASIC_FEATURES != 0)
53 #define BLE_MEM_BLOCK_X_PTX(n_link)           0
54 #else
55 #define BLE_MEM_BLOCK_X_PTX(n_link)           (n_link)
56 #endif
57 
58 #define BLE_MEM_BLOCK_X_TX(mtu) \
59         (DIVC((mtu) + 4U, BLE_MEM_BLOCK_SIZE) + 1)
60 
61 #define BLE_MEM_BLOCK_X_RX(mtu, n_link) \
62         ((DIVC((mtu) + 4U, BLE_MEM_BLOCK_SIZE) + 2U) * (n_link) + 1)
63 
64 #define BLE_MEM_BLOCK_X_MTU(mtu, n_link) \
65         (BLE_MEM_BLOCK_X_TX(mtu) + BLE_MEM_BLOCK_X_PTX(n_link) + \
66          BLE_MEM_BLOCK_X_RX(mtu, n_link))
67 
68 /*
69  * BLE_MBLOCKS_SECURE_CONNECTIONS: minimum number of blocks required for
70  * secure connections
71  */
72 #define BLE_MBLOCKS_SECURE_CONNECTIONS        4
73 
74 /*
75  * BLE_MBLOCKS_CALC: minimum number of buffers needed by the stack.
76  * This is the minimum racomanded value and depends on:
77  *  - pw: size of Prepare Write List
78  *  - mtu: ATT_MTU size
79  *  - n_link: maximum number of simultaneous connections
80  */
81 #define BLE_MBLOCKS_CALC(pw, mtu, n_link) \
82         ((pw) + MAX(BLE_MEM_BLOCK_X_MTU(mtu, n_link), \
83                     BLE_MBLOCKS_SECURE_CONNECTIONS))
84 
85 /*
86  * BLE_FIXED_BUFFER_SIZE_BYTES:
87  * A part of the RAM, is dynamically allocated by initializing all the pointers
88  * defined in a global context variable "mem_alloc_ctx_p".
89  * This initialization is made in the Dynamic_allocator functions, which
90  * assign a portion of RAM given by the external application to the above
91  * mentioned "global pointers".
92  *
93  * The size of this Dynamic RAM is made of 2 main components:
94  * - a part that is parameters-dependent (num of links, GATT buffers, ...),
95  *   and which value is made explicit by the following macro;
96  * - a part, that may be considered "fixed", i.e. independent from the above
97  *   mentioned parameters.
98 */
99 #if (BEACON_ONLY != 0)
100 #define BLE_FIXED_BUFFER_SIZE_BYTES  4100   /* Beacon only */
101 #elif (LL_ONLY_BASIC != 0)
102 #define BLE_FIXED_BUFFER_SIZE_BYTES  6040   /* LL only Basic*/
103 #elif (LL_ONLY != 0)
104 #define BLE_FIXED_BUFFER_SIZE_BYTES  6288   /* LL only Full */
105 #elif (SLAVE_ONLY != 0)
106 #define BLE_FIXED_BUFFER_SIZE_BYTES  6408   /* Peripheral only */
107 #elif (BASIC_FEATURES != 0)
108 #define BLE_FIXED_BUFFER_SIZE_BYTES  7184   /* Basic Features */
109 #else
110 #define BLE_FIXED_BUFFER_SIZE_BYTES  7468   /* Full stack */
111 #endif
112 
113 /*
114  * BLE_PER_LINK_SIZE_BYTES: additional memory size used per link
115  */
116 #if (BEACON_ONLY != 0)
117 #define BLE_PER_LINK_SIZE_BYTES       108   /* Beacon only */
118 #elif (LL_ONLY_BASIC != 0)
119 #define BLE_PER_LINK_SIZE_BYTES       244   /* LL only Basic */
120 #elif (LL_ONLY != 0)
121 #define BLE_PER_LINK_SIZE_BYTES       244   /* LL only Full */
122 #elif (SLAVE_ONLY != 0)
123 #define BLE_PER_LINK_SIZE_BYTES       392   /* Peripheral only */
124 #elif (BASIC_FEATURES != 0)
125 #define BLE_PER_LINK_SIZE_BYTES       420   /* Basic Features */
126 #else
127 #define BLE_PER_LINK_SIZE_BYTES       432   /* Full stack */
128 #endif
129 
130 /*
131  * BLE_TOTAL_BUFFER_SIZE: this macro returns the amount of memory, in bytes,
132  * needed for the storage of data structures (except GATT database elements)
133  * whose size depends on the number of supported connections.
134  *
135  * @param n_link: Maximum number of simultaneous connections that the device
136  * will support. Valid values are from 1 to 8.
137  *
138  * @param mblocks_count: Number of memory blocks allocated for packets.
139  */
140 #define BLE_TOTAL_BUFFER_SIZE(n_link, mblocks_count) \
141         (16 + BLE_FIXED_BUFFER_SIZE_BYTES + \
142          (BLE_PER_LINK_SIZE_BYTES * (n_link)) + \
143          ((BLE_MEM_BLOCK_SIZE + 8) * (mblocks_count)))
144 
145 /*
146  * BLE_EXT_ADV_BUFFER_SIZE
147  * additional memory size used for Extended advertising;
148  * It has to be added to BLE_TOTAL_BUFFER_SIZE() if the Extended advertising
149  * feature is used.
150  *
151  * @param set_nbr: Maximum number of advertising sets.
152  * Valid values are from 1 to 8.
153  *
154  * @param data_len: Maximum size of advertising data.
155  * Valid values are from 31 to 1650.
156  */
157 #define BLE_EXT_ADV_BUFFER_SIZE(set_nbr, data_len) \
158         (2512 + ((892 + (DIVC(data_len, 207) * 244)) * (set_nbr)))
159 
160 /*
161  * BLE_TOTAL_BUFFER_SIZE_GATT: this macro returns the amount of memory,
162  * in bytes, needed for the storage of GATT database elements.
163  *
164  * @param num_gatt_attributes: Maximum number of Attributes (i.e. the number
165  * of characteristic + the number of characteristic values + the number of
166  * descriptors, excluding the services) that can be stored in the GATT
167  * database. Note that certain characteristics and relative descriptors are
168  * added automatically during device initialization so this parameters should
169  * be 9 plus the number of user Attributes
170  *
171  * @param num_gatt_services: Maximum number of Services that can be stored in
172  * the GATT database. Note that the GAP and GATT services are automatically
173  * added so this parameter should be 2 plus the number of user services
174  *
175  * @param att_value_array_size: Size of the storage area for Attribute values.
176   */
177 #define BLE_TOTAL_BUFFER_SIZE_GATT(num_gatt_attributes, num_gatt_services, att_value_array_size) \
178         (((((att_value_array_size) - 1) | 3) + 1) + \
179          (40 * (num_gatt_attributes)) + (48 * (num_gatt_services)))
180 
181 
182 #endif /* BLE_BUFSIZE_H__ */
183