1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    dtm_cmds.c
5   * @author  GPM WBL Application Team
6   * @brief   DTM specific commands to be implemented on DTM context (not present on BLE stack)
7   ******************************************************************************
8   * @attention
9   *
10   * Copyright (c) 2024 STMicroelectronics.
11   * All rights reserved.
12   *
13   * This software is licensed under terms that can be found in the LICENSE file
14   * in the root directory of this software component.
15   * If no LICENSE file comes with this software, it is provided AS-IS.
16   *
17   ******************************************************************************
18   */
19 /* USER CODE END Header */
20 
21 #include "stm32wb0x.h"
22 #include "ble_stack.h"
23 #include "dtm_cmd_db.h"
24 #include "transport_layer.h"
25 #include "adv_buff_alloc.h"
26 #include "adv_buff_alloc_tiny.h"
27 #include "ble_const.h"
28 #include "dtm_burst.h"
29 #include "app_ble.h"
30 #include "app_common.h"
31 
32 #ifndef MIN
33 #   define MIN(a,b) (((a) < (b)) ? (a) : (b))
34 #endif
35 
36 #ifdef WATCHDOG
37 #include "rf_driver_ll_iwdg.h"
38 #endif
39 
40 /* Private macro -------------------------------------------------------------*/
41 /** @name BLE stack v3.1 stack modular configurations bitmap
42   * @{
43   */
44 #define CONTROLLER_PRIVACY_BIT                 ((uint32_t)0x00000001)  /*!< Bit  0 selected */
45 #define SECURE_CONNECTIONS_BIT                 ((uint32_t)0x00000002)  /*!< Bit  1 selected */
46 /* Observer (scanner) or Central role support:
47    -  Observer disabled (CONTROLLER_SCAN_BIT = 0) or enabled (CONTROLLER_SCAN_BIT = 1) if CONNECTION_BIT = 0;
48    -  Observer and Central disabled (CONTROLLER_SCAN_BIT = 0) or enabled (CONTROLLER_SCAN_BIT = 1) if CONNECTION_BIT = 1.
49 */
50 #define CONTROLLER_SCAN_BIT                  ((uint32_t)0x00000004)  /*!< Bit  2 selected */
51 #define CONTROLLER_DATA_LENGTH_EXTENSION_BIT   ((uint32_t)0x00000008)  /*!< Bit  3 selected */
52 #define LINK_LAYER_ONLY_BIT                    ((uint32_t)0x00000010)  /*!< Bit  4 selected */
53 #define CONTROLLER_2M_CODED_PHY_BIT            ((uint32_t)0x00000020)  /*!< Bit  5 selected */
54 #define CONTROLLER_EXT_ADV_SCAN_BIT            ((uint32_t)0x00000040)  /*!< Bit  6 selected */
55 #define L2CAP_COS_BIT                          ((uint32_t)0x00000080)  /*!< Bit  7 selected */
56 #define CONTROLLER_PERIODIC_ADV_BIT            ((uint32_t)0x00000100)  /*!< Bit  8 selected */
57 #define CONTROLLER_CTE_BIT                     ((uint32_t)0x00000200)  /*!< Bit  9 selected */
58 #define CONTROLLER_POWER_CONTROL_BIT           ((uint32_t)0x00000400)  /*!< Bit 10 selected */
59 /* Support to connections:
60    - If CONNECTION_BIT = 0, connections are not supported;
61      device is a broadcaster-only if CONTROLLER_SCAN_BIT = 0,
62      or a broadcaster and observer if CONTROLLER_SCAN_BIT = 1.
63    - If CONNECTION_BIT = 1, connections are supported;
64      device can only act as broadcaster or peripheral if CONTROLLER_SCAN_BIT = 0,
65      or any role (broadcaster, observer, peripheral, and central) if CONTROLLER_SCAN_BIT = 1.
66 */
67 #define CONNECTION_BIT                         ((uint32_t)0x00000800)  /*!< Bit 11 selected */
68 #define CONTROLLER_ONLY_BIT                    ((uint32_t)0x00001000)  /*!< Bit 12 selected */
69 #define CONTROLLER_CHAN_CLASS_BIT              ((uint32_t)0x00010000)  /*!< Bit 16 selected */
70 #define CONTROLLER_BIS_BIT                     ((uint32_t)0x00020000)  /*!< Bit 17 selected */
71 #define CONNECTION_SUBRATING_BIT               ((uint32_t)0x00080000)  /*!< Bit 19 selected */
72 #define CONTROLLER_CIS_BIT                     ((uint32_t)0x00100000)  /*!< Bit 20 selected */
73 #define CONTROLLER_PERIODIC_ADV_WR_BIT         ((uint32_t)0x00400000)  /*!< Bit 22 selected */
74 /**
75   * @}
76   */
77 
78 /** @brief Link Layer Enabled or not based on LL_ONLY and BLESTACK_CONTROLLER_ONLY preprocessor options */
79 #ifndef BLESTACK_CONTROLLER_ONLY
80 #define BLESTACK_CONTROLLER_ONLY (0U)
81 #endif
82 #if (BLESTACK_CONTROLLER_ONLY == 1U)
83 #define LINK_LAYER_ONLY_ENABLED (1U)
84 #else
85 #define LINK_LAYER_ONLY_ENABLED (0U)
86 #endif
87 
88 /** @brief Define BLE stack configurations variant bitmap value based on enabled BLE stack options and associated bits (LSB 5 bits) */
89 #define BLE_STACK_CONFIGURATIONS_VARIANT (                                                          \
90     ((uint32_t)(CONTROLLER_PRIVACY_ENABLED * CONTROLLER_PRIVACY_BIT))                             | \
91     ((uint32_t)(SECURE_CONNECTIONS_ENABLED * SECURE_CONNECTIONS_BIT))                             | \
92     ((uint32_t)(CONTROLLER_SCAN_ENABLED * CONTROLLER_SCAN_BIT))                               | \
93     ((uint32_t)(CONTROLLER_DATA_LENGTH_EXTENSION_ENABLED * CONTROLLER_DATA_LENGTH_EXTENSION_BIT)) | \
94     ((uint32_t)(LINK_LAYER_ONLY_ENABLED * LINK_LAYER_ONLY_BIT))                                   | \
95     ((uint32_t)(CONTROLLER_2M_CODED_PHY_ENABLED * CONTROLLER_2M_CODED_PHY_BIT))                   | \
96     ((uint32_t)(CONTROLLER_EXT_ADV_SCAN_ENABLED * CONTROLLER_EXT_ADV_SCAN_BIT))                   | \
97     ((uint32_t)(L2CAP_COS_ENABLED * L2CAP_COS_BIT))                                               | \
98     ((uint32_t)(CONTROLLER_PERIODIC_ADV_ENABLED * CONTROLLER_PERIODIC_ADV_BIT))                   | \
99     ((uint32_t)(CONTROLLER_CTE_ENABLED * CONTROLLER_CTE_BIT))                                     | \
100     ((uint32_t)(CONTROLLER_POWER_CONTROL_ENABLED * CONTROLLER_POWER_CONTROL_BIT))                 | \
101     ((uint32_t)(CONNECTION_ENABLED * CONNECTION_BIT))                                             | \
102     ((uint32_t)(BLESTACK_CONTROLLER_ONLY * CONTROLLER_ONLY_BIT))                                  | \
103     ((uint32_t)(CONTROLLER_CHAN_CLASS_ENABLED * CONTROLLER_CHAN_CLASS_BIT))                       | \
104     ((uint32_t)(CONTROLLER_BIS_ENABLED * CONTROLLER_BIS_BIT))                                     | \
105     ((uint32_t)(CONNECTION_SUBRATING_ENABLED * CONNECTION_SUBRATING_BIT))                         | \
106     ((uint32_t)(CONTROLLER_CIS_ENABLED * CONTROLLER_CIS_BIT))                                     | \
107     ((uint32_t)(CONTROLLER_PERIODIC_ADV_WR_ENABLED * CONTROLLER_PERIODIC_ADV_WR_BIT))               \
108 )
109 
aci_hal_get_firmware_details(uint8_t * DTM_version_major,uint8_t * DTM_version_minor,uint8_t * DTM_version_patch,uint8_t * DTM_variant,uint16_t * DTM_Build_Number,uint8_t * BTLE_Stack_version_major,uint8_t * BTLE_Stack_version_minor,uint8_t * BTLE_Stack_version_patch,uint8_t * BTLE_Stack_development,uint16_t * BTLE_Stack_variant,uint16_t * BTLE_Stack_Build_Number)110 tBleStatus aci_hal_get_firmware_details(uint8_t *DTM_version_major,
111                                         uint8_t *DTM_version_minor,
112                                         uint8_t *DTM_version_patch,
113                                         uint8_t *DTM_variant,
114                                         uint16_t *DTM_Build_Number,
115                                         uint8_t *BTLE_Stack_version_major,
116                                         uint8_t *BTLE_Stack_version_minor,
117                                         uint8_t *BTLE_Stack_version_patch,
118                                         uint8_t *BTLE_Stack_development,
119                                         uint16_t *BTLE_Stack_variant,
120                                         uint16_t *BTLE_Stack_Build_Number)
121 {
122   tBleStatus ret;
123   uint32_t BTLE_Stack_variant_ext;
124 
125   ret = aci_hal_get_firmware_details_v2(DTM_version_major,DTM_version_minor,DTM_version_patch,
126                                          DTM_variant,DTM_Build_Number,BTLE_Stack_version_major,
127                                          BTLE_Stack_version_minor,BTLE_Stack_version_patch,
128                                          BTLE_Stack_development,&BTLE_Stack_variant_ext,
129                                          BTLE_Stack_Build_Number);
130   *BTLE_Stack_variant = BTLE_Stack_variant_ext;
131 
132   return ret;
133 }
134 
aci_hal_get_firmware_details_v2(uint8_t * DTM_version_major,uint8_t * DTM_version_minor,uint8_t * DTM_version_patch,uint8_t * DTM_variant,uint16_t * DTM_Build_Number,uint8_t * BTLE_Stack_version_major,uint8_t * BTLE_Stack_version_minor,uint8_t * BTLE_Stack_version_patch,uint8_t * BTLE_Stack_development,uint32_t * BTLE_Stack_variant,uint16_t * BTLE_Stack_Build_Number)135 tBleStatus aci_hal_get_firmware_details_v2(uint8_t *DTM_version_major,
136                                            uint8_t *DTM_version_minor,
137                                            uint8_t *DTM_version_patch,
138                                            uint8_t *DTM_variant,
139                                            uint16_t *DTM_Build_Number,
140                                            uint8_t *BTLE_Stack_version_major,
141                                            uint8_t *BTLE_Stack_version_minor,
142                                            uint8_t *BTLE_Stack_version_patch,
143                                            uint8_t *BTLE_Stack_development,
144                                            uint32_t *BTLE_Stack_variant,
145                                            uint16_t *BTLE_Stack_Build_Number)
146 {
147 
148     aci_hal_get_fw_build_number(BTLE_Stack_Build_Number);
149     uint8_t HCI_Version = 0;
150     uint16_t HCI_Revision = 0;
151     uint8_t LMP_PAL_Version = 0;
152     uint16_t Manufacturer_Name = 0;
153     uint16_t LMP_PAL_Subversion = 0;
154 
155     hci_read_local_version_information(&HCI_Version, &HCI_Revision, &LMP_PAL_Version,
156                                        &Manufacturer_Name, &LMP_PAL_Subversion);
157 
158     *DTM_version_major  = DTM_FW_VERSION_MAJOR;
159     *DTM_version_minor  = DTM_FW_VERSION_MINOR;
160     *DTM_version_patch  = DTM_FW_VERSION_PATCH;
161     *DTM_variant = DTM_VARIANT;
162     *DTM_Build_Number = 0;
163     *BTLE_Stack_version_major = HCI_Revision&0x0F;
164     *BTLE_Stack_version_minor = (LMP_PAL_Subversion>>4)&0x0F;
165     *BTLE_Stack_version_patch = LMP_PAL_Subversion&0x0F;
166     *BTLE_Stack_development = (LMP_PAL_Subversion>>15)&0x01;
167 
168     /* Set the stack configurations variant bitmap value:
169        first LSB 7 bits are reserved for BLE stack modular options + Link Layer only*/
170     *BTLE_Stack_variant = BLE_STACK_CONFIGURATIONS_VARIANT;
171 
172     return (BLE_STATUS_SUCCESS);
173 
174 }
175 
176 /**
177  * @brief  This API implements the hci le transmitter test with
178  *         the capability to set the number of packets to be sent.
179  * @param  TX_Frequency: TX frequency
180  * @param  Length_Of_Test_Data: length of test data
181  * @param  Packet_Payload: packet payload
182  * @param  Number_Of_Packets: number pf packets to be sent on test
183  * @param  PHY: PHY to be used by the transmitter
184  * @retval status
185 */
aci_hal_transmitter_test_packets(uint8_t TX_Frequency,uint8_t Length_Of_Test_Data,uint8_t Packet_Payload,uint16_t Number_Of_Packets,uint8_t PHY)186 tBleStatus aci_hal_transmitter_test_packets(uint8_t TX_Frequency,
187                                             uint8_t Length_Of_Test_Data,
188                                             uint8_t Packet_Payload,
189                                             uint16_t Number_Of_Packets,
190                                             uint8_t PHY)
191 {
192   extern uint16_t num_packets;
193   tBleStatus status;
194 
195   if(Number_Of_Packets == 0)
196   {
197     return BLE_ERROR_INVALID_HCI_CMD_PARAMS;
198   }
199 
200 #if CONTROLLER_2M_CODED_PHY_ENABLED
201 
202   status =  hci_le_transmitter_test_v2(TX_Frequency,
203                                        Length_Of_Test_Data,
204                                        Packet_Payload,
205                                        PHY);
206 
207 #else
208 
209   status = hci_le_transmitter_test(TX_Frequency /* 1 */,
210                                    Length_Of_Test_Data /* 1 */,
211                                    Packet_Payload /* 1 */);
212 #endif
213 
214   if(status == 0x00)
215   {
216     num_packets = Number_Of_Packets;
217   }
218 
219   return status;
220 }
221 
222 #if CONTROLLER_CTE_ENABLED
223 /**
224  * @brief  This API implements the hci le transmitter test v2 with
225  *         the capability to set the number of packets to be sent.
226  * @param  TX_Channel: TX channel
227  * @param  Test_Data_Length: length of test data
228  * @param  Packet_Payload: packet payload
229  * @param  Number_Of_Packets: number of packets to be sent on test
230  * @param  PHY: PHY to be used by the transmitter
231  * @param  CTE_Length: CTE length
232  * @param  CTE_Type: CTE type
233  * @param  Switching_Pattern_Length: switching pattern length
234  * @param  Antenna_IDs: antenna ids
235  * @param  Transmit_Power_Level: tx power level
236  * @retval status
237 */
aci_hal_transmitter_test_packets_v2(uint8_t TX_Channel,uint8_t Test_Data_Length,uint8_t Packet_Payload,uint16_t Number_Of_Packets,uint8_t PHY,uint8_t CTE_Length,uint8_t CTE_Type,uint8_t Switching_Pattern_Length,uint8_t Antenna_IDs[])238 tBleStatus aci_hal_transmitter_test_packets_v2(uint8_t TX_Channel,
239                                                uint8_t Test_Data_Length,
240                                                uint8_t Packet_Payload,
241                                                uint16_t Number_Of_Packets,
242                                                uint8_t PHY,
243                                                uint8_t CTE_Length,
244                                                uint8_t CTE_Type,
245                                                uint8_t Switching_Pattern_Length,
246                                                uint8_t Antenna_IDs[])
247 {
248   extern uint16_t num_packets;
249   tBleStatus status;
250 
251   if(Number_Of_Packets == 0)
252   {
253     return BLE_ERROR_INVALID_HCI_CMD_PARAMS;
254   }
255 
256   status =  hci_le_transmitter_test_v3(TX_Channel,
257                                        Test_Data_Length,
258                                        Packet_Payload,
259                                        PHY,
260                                        CTE_Length,
261                                        CTE_Type,
262                                        Switching_Pattern_Length,
263                                        Antenna_IDs);
264 
265   if(status == 0x00)
266   {
267     num_packets = Number_Of_Packets;
268   }
269 
270   return status;
271 }
272 #endif
273 
274 #if (CONNECTION_ENABLED == 1) && (BLESTACK_CONTROLLER_ONLY==0)
275 
aci_test_tx_notification_start(uint16_t Connection_Handle,uint16_t Service_Handle,uint16_t Char_Handle,uint16_t Value_Length)276 tBleStatus aci_test_tx_notification_start(uint16_t Connection_Handle, uint16_t Service_Handle, uint16_t Char_Handle, uint16_t Value_Length)
277 {
278   return BURST_TXNotificationStart(Connection_Handle, Service_Handle, Char_Handle, Value_Length);
279 }
280 
aci_test_tx_write_command_start(uint16_t Connection_Handle,uint16_t Attr_Handle,uint16_t Value_Length)281 tBleStatus aci_test_tx_write_command_start(uint16_t Connection_Handle, uint16_t Attr_Handle, uint16_t Value_Length)
282 {
283   return BURST_TXWriteCommandStart(Connection_Handle, Attr_Handle, Value_Length);
284 }
285 
aci_test_rx_start(uint16_t Connection_Handle,uint16_t Attribute_Handle,uint8_t Notifications_WriteCmds)286 tBleStatus aci_test_rx_start(uint16_t Connection_Handle, uint16_t Attribute_Handle, uint8_t Notifications_WriteCmds)
287 {
288   return BURST_RXStart(Connection_Handle, Attribute_Handle, Notifications_WriteCmds);
289 }
290 
aci_test_stop(uint8_t TX_RX)291 tBleStatus aci_test_stop(uint8_t TX_RX)
292 {
293   switch(TX_RX){
294   case 0:
295     BURST_TXStop();
296     break;
297   case 1:
298     BURST_RXStop();
299     break;
300   default:
301     return BLE_ERROR_INVALID_HCI_CMD_PARAMS;
302   }
303 
304   return BLE_STATUS_SUCCESS;
305 }
306 
aci_test_report(uint32_t * TX_Packets,uint32_t * RX_Packets,uint16_t * RX_Data_Length,uint32_t * RX_Sequence_Errors)307 tBleStatus aci_test_report(uint32_t *TX_Packets, uint32_t *RX_Packets, uint16_t *RX_Data_Length, uint32_t *RX_Sequence_Errors)
308 {
309   *TX_Packets = BURST_TXReport();
310   *RX_Packets = BURST_RXReport(RX_Data_Length, RX_Sequence_Errors);
311 
312   return BLE_STATUS_SUCCESS;
313 }
314 
315 #endif
316 
aci_hal_write_radio_reg(uint32_t Start_Address,uint8_t Num_Bytes,uint8_t Data[])317 tBleStatus aci_hal_write_radio_reg(uint32_t Start_Address,
318                                    uint8_t Num_Bytes,
319                                    uint8_t Data[])
320 {
321     uint32_t *address = (uint32_t *)Start_Address;
322 
323     if (((Num_Bytes & 0x03U) != 0x00U) ||
324         ((Start_Address & 0x03U) != 0x00U))
325     {
326         return BLE_ERROR_INVALID_HCI_CMD_PARAMS;
327     }
328 
329     for (uint32_t word = 0; word < ((uint32_t)Num_Bytes >> 2); word++)
330     {
331         uint32_t tmp_word = 0x00000000;
332         for (int byte = 3; byte >= 0; byte--)
333         {
334             tmp_word <<= 8;
335             tmp_word |= Data[word * 4U + (uint32_t)byte];
336         }
337         address[word] = tmp_word;
338     }
339 
340     return BLE_STATUS_SUCCESS;
341 }
342 
aci_hal_read_radio_reg(uint32_t Start_Address,uint8_t Num_Bytes,uint8_t * Data_Length,uint8_t Data[])343 tBleStatus aci_hal_read_radio_reg(uint32_t Start_Address,
344                                   uint8_t Num_Bytes,
345                                   uint8_t *Data_Length,
346                                   uint8_t Data[])
347 {
348     uint32_t *address = (uint32_t *)Start_Address;
349 
350     if (((Num_Bytes & 0x03U) != 0x00U) ||
351         ((Start_Address & 0x03U) != 0x00U))
352     {
353         return BLE_ERROR_INVALID_HCI_CMD_PARAMS;
354     }
355 
356     ATOMIC_SECTION_BEGIN();
357     for (uint32_t word = 0; word < ((uint32_t)Num_Bytes >> 2); word++)
358     {
359         uint32_t tmp_word = address[word];
360         for (int byte = 0; byte < 4; byte++)
361         {
362             Data[word * 4U + (uint32_t)byte] = (uint8_t)(tmp_word & 0x000000FFU);
363             tmp_word >>= 8;
364         }
365     }
366     ATOMIC_SECTION_END();
367     * Data_Length = Num_Bytes;
368 
369     return BLE_STATUS_SUCCESS;
370 }
371