xref: /btstack/port/stm32-wb55xx-nucleo-freertos/Middlewares/STM32_WPAN/ble/core/ble_bufsize.h (revision 0561b2d8d5dba972c7daa57d5e677f7a1327edfd)
1 /*****************************************************************************
2  * @file    ble_bufsize.h
3  * @author  MCD Application Team
4  * @brief   Definition of BLE stack buffers size
5  *****************************************************************************
6  * @attention
7  *
8  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
9  * All rights reserved.</center></h2>
10  *
11  * This software component is licensed by ST under Ultimate Liberty license
12  * SLA0044, the "License"; You may not use this file except in compliance with
13  * the License. You may obtain a copy of the License at:
14  *                             www.st.com/SLA0044
15  *
16  *****************************************************************************
17  */
18 
19 #ifndef BLE_BUFSIZE_H__
20 #define BLE_BUFSIZE_H__
21 
22 
23 /*
24  * BLE_DEFAULT_ATT_MTU: minimum MTU value that GATT must support.
25  */
26 #define BLE_DEFAULT_ATT_MTU                  23
27 
28 /*
29  * BLE_DEFAULT_MAX_ATT_MTU: maximum supported ATT MTU size.
30  */
31 #define BLE_DEFAULT_MAX_ATT_MTU             158
32 
33 /*
34  * BLE_DEFAULT_MAX_ATT_SIZE: maximum attribute size.
35  */
36 #define BLE_DEFAULT_MAX_ATT_SIZE            512
37 
38 /*
39  * BLE_PREP_WRITE_X_ATT: compute how many Prepare Write Request are needed to
40  * write a characteristic with size 'max_att' when the used ATT_MTU value is
41  * equal to BLE_DEFAULT_ATT_MTU (23).
42  */
43 #define BLE_PREP_WRITE_X_ATT(max_att) \
44           (DIVC(max_att, BLE_DEFAULT_ATT_MTU - 5) * 2)
45 
46 /*
47  * BLE_DEFAULT_PREP_WRITE_LIST_SIZE: default minimum Prepare Write List size.
48  */
49 #define BLE_DEFAULT_PREP_WRITE_LIST_SIZE \
50           BLE_PREP_WRITE_X_ATT(BLE_DEFAULT_MAX_ATT_SIZE)
51 
52 /*
53  * BLE_MEM_BLOCK_X_MTU: compute how many memory blocks are needed to compose
54  * an ATT packet with ATT_MTU=mtu.
55  */
56 #define BLE_MEM_BLOCK_SIZE                   32
57 
58 #define BLE_MEM_BLOCK_X_TX(mtu) \
59           (DIVC((mtu) + 4U, BLE_MEM_BLOCK_SIZE) + 1U)
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_RX(mtu, n_link))
66 
67 /*
68  * BLE_MBLOCKS_SECURE_CONNECTIONS: minimum number of blocks required for
69  * secure connections
70  */
71 #define BLE_MBLOCKS_SECURE_CONNECTIONS        4
72 
73 /*
74  * BLE_MBLOCKS_CALC: minimum number of buffers needed by the stack.
75  * This is the minimum racomanded value and depends on:
76  *  - pw: size of Prepare Write List
77  *  - mtu: ATT_MTU size
78  *  - n_link: maximum number of simultaneous connections
79  */
80 #define BLE_MBLOCKS_CALC(pw, mtu, n_link) \
81           ((pw) + MAX(BLE_MEM_BLOCK_X_MTU(mtu, n_link), \
82                       BLE_MBLOCKS_SECURE_CONNECTIONS))
83 
84 /*
85  * BLE_DEFAULT_MBLOCKS_COUNT: default memory blocks count
86  */
87 #define BLE_DEFAULT_MBLOCKS_COUNT(n_link) \
88           BLE_MBLOCKS_CALC(BLE_DEFAULT_PREP_WRITE_LIST_SIZE, \
89                            BLE_DEFAULT_MAX_ATT_MTU, n_link)
90 
91 /*
92  * BLE_FIXED_BUFFER_SIZE_BYTES:
93  * A part of the RAM, is dinamically allocated by initilizing all the pointers
94  * defined in a global context variable "mem_alloc_ctx_p".
95  * This initialization is made in the Dynamic_allocator functions, which
96  * assing a portion of RAM given by the external application to the above
97  * mentioned "global pointers".
98  *
99  * The size of this Dynamic RAM is made of 2 main components:
100  * - a part that is parameters-dependent (num of links, GATT buffers, ...),
101  *   and which value is explicited by the following macro;
102  * - a part, that may be considered "fixed", i.e. independent from the above
103  *   mentioned parameters.
104 */
105 #define BLE_FIXED_BUFFER_SIZE_BYTES(llo)  ((llo) ? 6064 : 6504)
106 
107 /*
108  * BLE_PER_LINK_SIZE_BYTES: additional memory size used per link
109  */
110 #define BLE_PER_LINK_SIZE_BYTES(llo)      ((llo) ? 208 : 376)
111 
112 /*
113  * BLE_DLEN_EXT_SIZE: amount of memory needed to support Data Length
114  * Extension feature.
115  */
116 #define BLE_DLEN_EXT_SIZE(en)             ((en) ? 436 : 0)
117 
118 /*
119  * BLE_TOTAL_BUFFER_SIZE: this macro returns the amount of memory, in bytes,
120  * needed for the storage of data structures (except GATT database elements)
121  * whose size depends on the number of supported connections.
122  *
123  * @param num_links: Maximum number of simultaneous connections that the device
124  * will support. Valid values are from 1 to 8.
125  *
126  * @param mblocks_count: Number of memory blocks allocated for packets.
127  *
128  * @param dlen_ext_en: Enable or disable the Extended Packet length feature.
129  * Valid values are 0 or 1.
130   */
131 #define BLE_TOTAL_BUFFER_SIZE(n_link, mblocks_count, dlen_ext_en) \
132           (BLE_FIXED_BUFFER_SIZE_BYTES(LL_ONLY) + \
133            (BLE_PER_LINK_SIZE_BYTES(LL_ONLY) * (n_link)) + \
134            ((BLE_MEM_BLOCK_SIZE + 12) * (mblocks_count)) + \
135            BLE_DLEN_EXT_SIZE(dlen_ext_en))
136 
137 /*
138  * BLE_TOTAL_BUFFER_SIZE_GATT: this macro returns the amount of memory,
139  * in bytes, needed for the storage of GATT database elements.
140  *
141  * @param num_gatt_attributes: Maximum number of Attributes (i.e. the number
142  * of characteristic + the number of characteristic values + the number of
143  * descriptors, excluding the services) that can be stored in the GATT
144  * database. Note that certain characteristics and relative descriptors are
145  * added automatically during device initialization so this parameters should
146  * be 9 plus the number of user Attributes
147  *
148  * @param num_gatt_services: Maximum number of Services that can be stored in
149  * the GATT database. Note that the GAP and GATT services are automatically
150  * added so this parameter should be 2 plus the number of user services
151  *
152  * @param att_value_array_size: Size of the storage area for Attribute values.
153   */
154 #define BLE_TOTAL_BUFFER_SIZE_GATT(num_gatt_attributes, num_gatt_services, att_value_array_size) \
155           (((((att_value_array_size) - 1) | 3) + 1) + \
156            (40 * (num_gatt_attributes)) + (48 * (num_gatt_services)))
157 
158 
159 #endif /* ! BLE_BUFSIZE_H__ */
160