1 /*****************************************************************************
2  * @file    blestack.h
3  * @author  MDG
4  * @brief   Header file for BLE stack
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 BLESTACK_H__
19 #define BLESTACK_H__
20 
21 
22 #include "auto/ble_types.h"
23 #include "ble_bufsize.h"
24 
25 
26 /*
27  * Definitions for return value of BleStack_Process( )
28  */
29 enum
30 {
31   BLE_SLEEPMODE_RUNNING   = 0,
32   BLE_SLEEPMODE_CPU_HALT  = 1,
33   BLE_SLEEPMODE_WAKETIMER = 2,
34   BLE_SLEEPMODE_NOTIMER   = 3,
35 };
36 
37 /*
38  * Definitions for 'options' parameter
39  */
40 enum
41 {
42   BLE_OPTIONS_LL_ONLY             = 0x0001U,
43   BLE_OPTIONS_NO_SVC_CHANGE_DESC  = 0x0002U,
44   BLE_OPTIONS_DEV_NAME_READ_ONLY  = 0x0004U,
45   BLE_OPTIONS_EXTENDED_ADV        = 0x0008U,
46   BLE_OPTIONS_REDUCED_DB_IN_NVM   = 0x0020U,
47   BLE_OPTIONS_GATT_CACHING        = 0x0040U,
48   BLE_OPTIONS_POWER_CLASS_1       = 0x0080U,
49   BLE_OPTIONS_APPEARANCE_WRITABLE = 0x0100U,
50   BLE_OPTIONS_ENHANCED_ATT        = 0x0200U,
51 };
52 
53 /*
54  * Definitions for 'debug' parameter
55  */
56 enum
57 {
58   BLE_DEBUG_RAND_ADDR_INIT  = 0x00000010UL,
59 };
60 
61 /*
62  * This structure contains memory and low level hardware configuration data
63  * for the device
64 */
65 typedef struct
66 {
67 
68   /* Start address of the RAM buffer allocated for BLE stack library.
69    * It must be a 32bit aligned RAM area.
70    */
71   uint8_t* bleStartRamAddress;
72 
73   /* Size of the RAM buffer allocated for BLE stack library.
74    * (could be filled with BLE_TOTAL_BUFFER_SIZE return value)
75    */
76   uint32_t total_buffer_size;
77 
78   /* Start address of the RAM buffer allocated for GATT database.
79    * It must be a 32bit aligned RAM area.
80    */
81   uint8_t* bleStartRamAddress_GATT;
82 
83   /* Size of the RAM buffer allocated for GATT database.
84    * (could be filled with BLE_TOTAL_BUFFER_SIZE_GATT return value)
85    */
86   uint32_t total_buffer_size_GATT;
87 
88   /* Maximum number of Attributes (i.e. the number of characteristic + the
89    * number of characteristic values + the number of descriptors, excluding the
90    * services) that can be stored in the GATT database.
91    * Note that certain characteristics and relative descriptors are added
92    * automatically during device initialization so this parameters should be 9
93    * (or 6 w.r.t. options) plus the number of user Attributes (NUM_GATT_
94    * ATTRIBUTES used in the calculation of BLE TOTAL_BUFFER_SIZE_GATT)
95    */
96   uint16_t numAttrRecord;
97 
98   /* Maximum number of Services that can be stored in the GATT database.
99    * Note that the GAP and GATT services are automatically added so this
100    * parameter should be 2 plus the number of user services (NUM_GATT_SERVICES
101    * used in the calculation of BLE_TOTAL_BUFFER_SIZE_GATT)
102    */
103   uint16_t numAttrServ;
104 
105   /* Size of the storage area for Attribute values (ATT_VALUE_ARRAY_SIZE used
106    * in the calculation of BLE_TOTAL_BUFFER_SIZE_GATT)
107    * This value depends on the number of attributes used by application. In
108    * particular the sum of the following quantities (in octets) should be made
109    * for each attribute:
110    * - attribute value length
111    * - 5, if UUID is 16 bit; 19, if UUID is 128 bit
112    * - 2, if server configuration descriptor is used
113    * - 2*numOfLinks, if client configuration descriptor is used
114    * - 2, if extended properties is used
115    * The total amount of memory needed is the sum of the above quantities for
116    * each attribute.
117    */
118   uint16_t attrValueArrSize;
119 
120   /* Maximum number of simultaneous connections that the device will support.
121    * Valid values are from 1 to 8 (NUM_LINKS used in the calculation of
122    * BLE_TOTAL_BUFFER_SIZE).
123    */
124   uint8_t numOfLinks;
125 
126   /* Prepare Write List size in terms of number of packet with ATT_MTU=23 bytes
127    */
128   uint8_t prWriteListSize;
129 
130   /* Number of allocated memory blocks
131    */
132   uint16_t mblockCount;
133 
134   /* Maximum supported ATT_MTU size
135    */
136   uint16_t attMtu;
137 
138   /* Maximum value of the connection-oriented channel Maximum Payload Size
139    * Range: 23 .. (BLE_EVT_MAX_PARAM_LEN - 7)
140    */
141   uint16_t max_coc_mps;
142 
143   /* Maximum number of connection-oriented channels.
144    * Range: 0 .. 64
145    */
146   uint8_t max_coc_nbr;
147 
148   /* Maximum number of connection-oriented channels in initiator mode.
149    * Range: 0 .. max_coc_nbr
150    */
151   uint8_t max_coc_initiator_nbr;
152 
153   /* Options flags
154    * Bitmap of the "BLE_OPTIONS_..." definitions (see above).
155    * - bit 0:   1: LL only                   0: LL + host
156    * - bit 1:   1: no service change desc.   0: with service change desc.
157    * - bit 2:   1: device name Read-Only     0: device name R/W
158    * - bit 3:   1: extended adv supported    0: extended adv not supported
159    * - bit 5:   1: Reduced GATT db in NVM    0: Full GATT db in NVM
160    * - bit 6:   1: GATT caching is used      0: GATT caching is not used
161    * - bit 7:   1: LE Power Class 1          0: LE Power Class 2-3
162    * - bit 8:   1: appearance Writable       0: appearance Read-Only
163    * - bit 9:   1: Enhanced ATT supported    0: Enhanced ATT not supported
164    * - other bits: reserved
165    */
166   uint16_t options;
167 
168   /* Debug flags
169    * Bitmap of the "BLE_DEBUG_..." definitions (see above).
170    */
171   uint32_t debug;
172 
173 } BleStack_init_t;
174 
175 /*
176  * BleStack_Init
177  *
178  * @brief The BLE Stack initialization routine
179  *
180  * @param[in]  Init_params_p      pointer to the const structure containing
181  *                                memory and low level  hardware configuration
182  *                                data for the device
183  *
184  * @return Value indicating success or error code.
185  */
186 extern tBleStatus BleStack_Init( const BleStack_init_t* init_params_p );
187 
188 /*
189  * BleStack_Process
190  *
191  * @brief This function executes the processing of all Host Stack layers.
192  * It has to be executed regularly to process incoming Link Layer packets and
193  * to process Host Layers procedures. All stack callbacks are called by this
194  * function.
195  *
196  * No BLE stack function must be called while the BleStack_Process is running.
197  * For example, if a BLE stack function may be called inside an
198  * interrupt routine, that interrupt must be disabled during the execution of
199  * BleStack_Process().
200  *
201  * @return
202  *  BLE_SLEEPMODE_RUNNING   (0) -> BLE Stack Process has to be executed,
203  *  BLE_SLEEPMODE_CPU_HALT  (1) -> BLE Stack Process does not have to be
204  *                                 executed,
205  */
206 extern uint8_t BleStack_Process( void );
207 
208 /*
209  * BleStack_Request
210  *
211  * @brief This function gives a request from application to the BLE stack.
212  * The input parameter is a buffer of bytes in the BLE standard format:
213  * HCI/ACI command packet or ACL data packet.
214  * The response packet is returned in the same buffer and the size (in bytes)
215  * of the response is given by the function return value.
216  */
217 extern uint16_t BleStack_Request( uint8_t* buffer );
218 
219 /*
220  * BLECB_Indication
221  *
222  * @brief Callback called by the BLE stack (from BleStack_Process() context)
223  * to send an indication to the application. The indication is a BLE standard
224  * packet that can be either an ACI/HCI event or an ACL data.
225  */
226 extern uint8_t BLECB_Indication( const uint8_t* data,
227                                  uint16_t length,
228                                  const uint8_t* ext_data,
229                                  uint16_t ext_length );
230 
231 
232 #endif /* BLESTACK_H__ */
233