1 /*****************************************************************************
2  * @file    ble_bufsize.h
3  * @author  MDG
4  * @brief   Definition of BLE stack buffers size
5  *****************************************************************************
6  * @attention
7  *
8  * Copyright (c) 2018-2023 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 #define BLE_MEM_BLOCK_X_TX(mtu) \
53           (DIVC((mtu) + 4U, BLE_MEM_BLOCK_SIZE) + 1U)
54 
55 #define BLE_MEM_BLOCK_X_RX(mtu, n_link) \
56           ((DIVC((mtu) + 4U, BLE_MEM_BLOCK_SIZE) + 2U) * (n_link) + 1)
57 
58 #define BLE_MEM_BLOCK_X_MTU(mtu, n_link) \
59           (BLE_MEM_BLOCK_X_TX(mtu) + BLE_MEM_BLOCK_X_RX(mtu, n_link))
60 
61 /*
62  * BLE_MBLOCKS_SECURE_CONNECTIONS: minimum number of blocks required for
63  * secure connections
64  */
65 #define BLE_MBLOCKS_SECURE_CONNECTIONS        4
66 
67 /*
68  * BLE_MBLOCKS_CALC: minimum number of buffers needed by the stack.
69  * This is the minimum racomanded value and depends on:
70  *  - pw: size of Prepare Write List
71  *  - mtu: ATT_MTU size
72  *  - n_link: maximum number of simultaneous connections
73  */
74 #define BLE_MBLOCKS_CALC(pw, mtu, n_link) \
75           ((pw) + MAX(BLE_MEM_BLOCK_X_MTU(mtu, n_link), \
76                       BLE_MBLOCKS_SECURE_CONNECTIONS))
77 
78 /*
79  * BLE_FIXED_BUFFER_SIZE_BYTES:
80  * A part of the RAM, is dynamically allocated by initializing all the pointers
81  * defined in a global context variable "mem_alloc_ctx_p".
82  * This initialization is made in the Dynamic_allocator functions, which
83  * assign a portion of RAM given by the external application to the above
84  * mentioned "global pointers".
85  *
86  * The size of this Dynamic RAM is made of 2 main components:
87  * - a part that is parameters-dependent (num of links, GATT buffers, ...),
88  *   and which value is made explicit by the following macro;
89  * - a part, that may be considered "fixed", i.e. independent from the above
90  *   mentioned parameters.
91 */
92 #if (BEACON_ONLY != 0)
93 #define BLE_FIXED_BUFFER_SIZE_BYTES  4092   /* Beacon only */
94 #elif (LL_ONLY_BASIC != 0)
95 #define BLE_FIXED_BUFFER_SIZE_BYTES  5788   /* LL only Basic*/
96 #elif (LL_ONLY != 0)
97 #define BLE_FIXED_BUFFER_SIZE_BYTES  6036   /* LL only Full */
98 #elif (SLAVE_ONLY != 0)
99 #define BLE_FIXED_BUFFER_SIZE_BYTES  6300   /* Peripheral only */
100 #elif (BASIC_FEATURES != 0)
101 #define BLE_FIXED_BUFFER_SIZE_BYTES  6632   /* Basic Features */
102 #else
103 #define BLE_FIXED_BUFFER_SIZE_BYTES  7152   /* Full stack */
104 #endif
105 
106 /*
107  * BLE_PER_LINK_SIZE_BYTES: additional memory size used per link
108  */
109 #if (BEACON_ONLY != 0)
110 #define BLE_PER_LINK_SIZE_BYTES       112   /* Beacon only */
111 #elif (LL_ONLY_BASIC != 0)
112 #define BLE_PER_LINK_SIZE_BYTES       244   /* LL only Basic */
113 #elif (LL_ONLY != 0)
114 #define BLE_PER_LINK_SIZE_BYTES       244   /* LL only Full */
115 #elif (SLAVE_ONLY != 0)
116 #define BLE_PER_LINK_SIZE_BYTES       344   /* Peripheral only */
117 #elif (BASIC_FEATURES != 0)
118 #define BLE_PER_LINK_SIZE_BYTES       420   /* Basic Features */
119 #else
120 #define BLE_PER_LINK_SIZE_BYTES       432   /* Full stack */
121 #endif
122 
123 /*
124  * BLE_TOTAL_BUFFER_SIZE: this macro returns the amount of memory, in bytes,
125  * needed for the storage of data structures (except GATT database elements)
126  * whose size depends on the number of supported connections.
127  *
128  * @param n_link: Maximum number of simultaneous connections that the device
129  * will support. Valid values are from 1 to 8.
130  *
131  * @param mblocks_count: Number of memory blocks allocated for packets.
132  */
133 #define BLE_TOTAL_BUFFER_SIZE(n_link, mblocks_count) \
134           (16 + BLE_FIXED_BUFFER_SIZE_BYTES + \
135            (BLE_PER_LINK_SIZE_BYTES * (n_link)) + \
136            ((BLE_MEM_BLOCK_SIZE + 12) * (mblocks_count)))
137 
138 /*
139  * BLE_EXT_ADV_BUFFER_SIZE
140  * additional memory size used for Extended advertising;
141  * It has to be added to BLE_TOTAL_BUFFER_SIZE() if the Extended advertising
142  * feature is used.
143  *
144  * @param set_nbr: Maximum number of advertising sets.
145  * Valid values are from 1 to 8.
146  *
147  * @param data_len: Maximum size of advertising data.
148  * Valid values are from 31 to 1650.
149  */
150 #define BLE_EXT_ADV_BUFFER_SIZE(set_nbr, data_len) \
151           (2512 + ((892 + (DIVC(data_len, 207) * 244)) * (set_nbr)))
152 
153 /*
154  * BLE_TOTAL_BUFFER_SIZE_GATT: this macro returns the amount of memory,
155  * in bytes, needed for the storage of GATT database elements.
156  *
157  * @param num_gatt_attributes: Maximum number of Attributes (i.e. the number
158  * of characteristic + the number of characteristic values + the number of
159  * descriptors, excluding the services) that can be stored in the GATT
160  * database. Note that certain characteristics and relative descriptors are
161  * added automatically during device initialization so this parameters should
162  * be 9 plus the number of user Attributes
163  *
164  * @param num_gatt_services: Maximum number of Services that can be stored in
165  * the GATT database. Note that the GAP and GATT services are automatically
166  * added so this parameter should be 2 plus the number of user services
167  *
168  * @param att_value_array_size: Size of the storage area for Attribute values.
169   */
170 #define BLE_TOTAL_BUFFER_SIZE_GATT(num_gatt_attributes, num_gatt_services, att_value_array_size) \
171           (((((att_value_array_size) - 1) | 3) + 1) + \
172            (40 * (num_gatt_attributes)) + (48 * (num_gatt_services)))
173 
174 
175 #endif /* BLE_BUFSIZE_H__ */
176