@@ -192,6 +192,9 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer
192192 const uint8_t * our_priv_key , uint8_t * out_dhkey );
193193#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
194194static void esp_bt_controller_log_interface (uint32_t len , const uint8_t * addr , bool end );
195+ #if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
196+ static void esp_bt_ctrl_log_partition_get_and_erase_first_block (void );
197+ #endif // #if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
195198#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
196199/* Local variable definition
197200 ***************************************************************************
@@ -200,6 +203,236 @@ static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, b
200203static DRAM_ATTR esp_bt_controller_status_t ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE ;
201204#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
202205const static uint32_t log_bufs_size [] = {CONFIG_BT_LE_LOG_CTRL_BUF1_SIZE , CONFIG_BT_LE_LOG_HCI_BUF_SIZE , CONFIG_BT_LE_LOG_CTRL_BUF2_SIZE };
206+ enum log_out_mode {
207+ LOG_DUMP_MEMORY ,
208+ LOG_ASYNC_OUT ,
209+ LOG_STORAGE_TO_FLASH ,
210+ };
211+
212+ bool log_is_inited = false;
213+ #if CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY
214+ uint8_t log_output_mode = LOG_DUMP_MEMORY ;
215+ #else
216+ #if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
217+ uint8_t log_output_mode = LOG_STORAGE_TO_FLASH ;
218+ #else
219+ uint8_t log_output_mode = LOG_ASYNC_OUT ;
220+ #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
221+ #endif // CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY
222+
223+ void esp_bt_log_output_mode_set (uint8_t output_mode )
224+ {
225+ log_output_mode = output_mode ;
226+ }
227+
228+ uint8_t esp_bt_log_output_mode_get (void )
229+ {
230+ return log_output_mode ;
231+ }
232+
233+ esp_err_t esp_bt_controller_log_init (uint8_t log_output_mode )
234+ {
235+ esp_err_t ret = ESP_OK ;
236+ interface_func_t bt_controller_log_interface ;
237+ bt_controller_log_interface = esp_bt_controller_log_interface ;
238+ bool task_create ;
239+ uint8_t buffers = 0 ;
240+
241+ if (log_is_inited ) {
242+ return ret ;
243+ }
244+
245+ #if CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
246+ buffers |= ESP_BLE_LOG_BUF_CONTROLLER ;
247+ #endif // CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
248+ #if CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
249+ buffers |= ESP_BLE_LOG_BUF_HCI ;
250+ #endif // CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
251+
252+ switch (log_output_mode ) {
253+ case LOG_DUMP_MEMORY :
254+ task_create = false;
255+ break ;
256+ case LOG_ASYNC_OUT :
257+ case LOG_STORAGE_TO_FLASH :
258+ task_create = true;
259+ #if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
260+ if (log_output_mode == LOG_STORAGE_TO_FLASH ) {
261+ esp_bt_ctrl_log_partition_get_and_erase_first_block ();
262+ }
263+ #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
264+ break ;
265+ default :
266+ assert (0 );
267+ }
268+
269+ ret = ble_log_init_async (bt_controller_log_interface , task_create , buffers , (uint32_t * )log_bufs_size );
270+ if (ret == ESP_OK ) {
271+ log_is_inited = true;
272+ }
273+
274+ return ret ;
275+ }
276+
277+ void esp_bt_ontroller_log_deinit (void )
278+ {
279+ ble_log_deinit_async ();
280+ log_is_inited = false;
281+ }
282+
283+ #if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
284+ #include "esp_partition.h"
285+ #include "hal/wdt_hal.h"
286+
287+ #define MAX_STORAGE_SIZE (CONFIG_BT_LE_CONTROLLER_LOG_PARTITION_SIZE)
288+ #define BLOCK_SIZE (4096)
289+ #define THRESHOLD (3072)
290+ #define PARTITION_NAME "bt_ctrl_log"
291+
292+ static const esp_partition_t * log_partition ;
293+ static uint32_t write_index = 0 ;
294+ static uint32_t next_erase_index = BLOCK_SIZE ;
295+ static bool block_erased = false;
296+ static bool stop_write = false;
297+ static bool is_filled = false;
298+
299+ static void esp_bt_ctrl_log_partition_get_and_erase_first_block (void )
300+ {
301+ log_partition = NULL ;
302+ assert (MAX_STORAGE_SIZE % BLOCK_SIZE == 0 );
303+ // Find the partition map in the partition table
304+ log_partition = esp_partition_find_first (ESP_PARTITION_TYPE_DATA , ESP_PARTITION_SUBTYPE_ANY , PARTITION_NAME );
305+ assert (log_partition != NULL );
306+ // Prepare data to be read later using the mapped address
307+ ESP_ERROR_CHECK (esp_partition_erase_range (log_partition , 0 , BLOCK_SIZE ));
308+ write_index = 0 ;
309+ next_erase_index = BLOCK_SIZE ;
310+ block_erased = false;
311+ is_filled = false;
312+ stop_write = false;
313+ }
314+
315+ static int esp_bt_controller_log_storage (uint32_t len , const uint8_t * addr , bool end )
316+ {
317+ if (len > MAX_STORAGE_SIZE ) {
318+ return -1 ;
319+ }
320+
321+ if (stop_write ) {
322+ return 0 ;
323+ }
324+
325+ assert (log_partition != NULL );
326+ if (((write_index ) % BLOCK_SIZE ) >= THRESHOLD && !block_erased ) {
327+ // esp_rom_printf("Ers nxt: %d,%d\n", next_erase_index, write_index);
328+ esp_partition_erase_range (log_partition , next_erase_index , BLOCK_SIZE );
329+ next_erase_index = (next_erase_index + BLOCK_SIZE ) % MAX_STORAGE_SIZE ;
330+ block_erased = true;
331+ }
332+
333+ if (((write_index + len ) / BLOCK_SIZE ) > (write_index / BLOCK_SIZE )) {
334+ block_erased = false;
335+ }
336+
337+ if (write_index + len <= MAX_STORAGE_SIZE ) {
338+ esp_partition_write (log_partition , write_index , addr , len );
339+ write_index = (write_index + len ) % MAX_STORAGE_SIZE ;
340+ } else {
341+ uint32_t first_part_len = MAX_STORAGE_SIZE - write_index ;
342+ esp_partition_write (log_partition , write_index , addr , first_part_len );
343+ esp_partition_write (log_partition , 0 , addr + first_part_len , len - first_part_len );
344+ write_index = len - first_part_len ;
345+ is_filled = true;
346+ // esp_rom_printf("old idx: %d,%d\n",next_erase_index, write_index);
347+ }
348+
349+ return 0 ;
350+ }
351+
352+ void esp_bt_read_ctrl_log_from_flash (bool output )
353+ {
354+ esp_partition_mmap_handle_t mmap_handle ;
355+ uint32_t read_index ;
356+ const void * mapped_ptr ;
357+ const uint8_t * buffer ;
358+ uint32_t print_len ;
359+ uint32_t max_print_len ;
360+ esp_err_t err ;
361+
362+ print_len = 0 ;
363+ max_print_len = 4096 ;
364+ err = esp_partition_mmap (log_partition , 0 , MAX_STORAGE_SIZE , ESP_PARTITION_MMAP_DATA , & mapped_ptr , & mmap_handle );
365+ if (err != ESP_OK ) {
366+ ESP_LOGE ("FLASH" , "Mmap failed: %s" , esp_err_to_name (err ));
367+ return ;
368+ }
369+
370+ portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED ;
371+ portENTER_CRITICAL_SAFE (& spinlock );
372+ esp_panic_handler_reconfigure_wdts (5000 );
373+ ble_log_async_output_dump_all (true);
374+ stop_write = true;
375+ esp_bt_ontroller_log_deinit ();
376+ portEXIT_CRITICAL_SAFE (& spinlock );
377+
378+ buffer = (const uint8_t * )mapped_ptr ;
379+ if (is_filled ) {
380+ read_index = next_erase_index ;
381+ } else {
382+ read_index = 0 ;
383+ }
384+
385+ esp_rom_printf ("\r\nREAD_CHECK:%ld,%ld,%d\r\n" ,read_index , write_index , is_filled );
386+ esp_rom_printf ("\r\n[DUMP_START:" );
387+ while (read_index != write_index ) {
388+ esp_rom_printf ("%02x " , buffer [read_index ]);
389+ if (print_len > max_print_len ) {
390+ vTaskDelay (2 );
391+ print_len = 0 ;
392+ }
393+
394+ print_len ++ ;
395+ read_index = (read_index + 1 ) % MAX_STORAGE_SIZE ;
396+ }
397+ esp_rom_printf (":DUMP_END]\r\n" );
398+ esp_partition_munmap (mmap_handle );
399+ err = esp_bt_controller_log_init (log_output_mode );
400+ assert (err == ESP_OK );
401+
402+ }
403+ #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
404+ static void esp_bt_controller_log_interface (uint32_t len , const uint8_t * addr , bool end )
405+ {
406+ if (log_output_mode == LOG_STORAGE_TO_FLASH ) {
407+ #if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
408+ esp_bt_controller_log_storage (len , addr , end );
409+ #endif //CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
410+ } else {
411+ for (int i = 0 ; i < len ; i ++ ) {
412+ esp_rom_printf ("%02x " , addr [i ]);
413+ }
414+
415+ if (end ) {
416+ esp_rom_printf ("\n" );
417+ }
418+ }
419+ }
420+
421+ void esp_ble_controller_log_dump_all (bool output )
422+ {
423+ #if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
424+ esp_bt_read_ctrl_log_from_flash (output );
425+ #else
426+ portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED ;
427+
428+ portENTER_CRITICAL_SAFE (& spinlock );
429+ esp_panic_handler_reconfigure_wdts (5000 );
430+ BT_ASSERT_PRINT ("\r\n[DUMP_START:" );
431+ ble_log_async_output_dump_all (output );
432+ BT_ASSERT_PRINT (":DUMP_END]\r\n" );
433+ portEXIT_CRITICAL_SAFE (& spinlock );
434+ #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
435+ }
203436#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
204437
205438/* This variable tells if BLE is running */
@@ -566,20 +799,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
566799 ESP_LOGI (NIMBLE_PORT_LOG_TAG , "ble rom commit:[%s]" , r_ble_controller_get_rom_compile_version ());
567800
568801#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
569- interface_func_t bt_controller_log_interface ;
570- bt_controller_log_interface = esp_bt_controller_log_interface ;
571- uint8_t buffers = 0 ;
572- #if CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
573- buffers |= ESP_BLE_LOG_BUF_CONTROLLER ;
574- #endif // CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
575- #if CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
576- buffers |= ESP_BLE_LOG_BUF_HCI ;
577- #endif // CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
578- #if CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY
579- ret = ble_log_init_async (bt_controller_log_interface , false, buffers , (uint32_t * )log_bufs_size );
580- #else
581- ret = ble_log_init_async (bt_controller_log_interface , true, buffers , (uint32_t * )log_bufs_size );
582- #endif // CONFIG_BT_CONTROLLER_LOG_DUMP
802+ ret = esp_bt_controller_log_init (log_output_mode );
583803 if (ret != ESP_OK ) {
584804 ESP_LOGW (NIMBLE_PORT_LOG_TAG , "ble_controller_log_init failed %d" , ret );
585805 goto controller_init_err ;
@@ -617,7 +837,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
617837 controller_sleep_deinit ();
618838#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
619839controller_init_err :
620- ble_log_deinit_async ();
840+ esp_bt_ontroller_log_deinit ();
621841#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
622842 ble_controller_deinit ();
623843modem_deint :
@@ -646,7 +866,7 @@ esp_err_t esp_bt_controller_deinit(void)
646866 controller_sleep_deinit ();
647867
648868#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
649- ble_log_deinit_async ();
869+ esp_bt_ontroller_log_deinit ();
650870#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
651871 ble_controller_deinit ();
652872
@@ -996,30 +1216,6 @@ uint8_t esp_ble_get_chip_rev_version(void)
9961216 return efuse_ll_get_chip_wafer_version_minor ();
9971217}
9981218
999- #if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
1000- static void esp_bt_controller_log_interface (uint32_t len , const uint8_t * addr , bool end )
1001- {
1002- for (int i = 0 ; i < len ; i ++ ) {
1003- esp_rom_printf ("%02x " , addr [i ]);
1004- }
1005- if (end ) {
1006- esp_rom_printf ("\n" );
1007- }
1008- }
1009-
1010- void esp_ble_controller_log_dump_all (bool output )
1011- {
1012- portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED ;
1013-
1014- portENTER_CRITICAL_SAFE (& spinlock );
1015- esp_panic_handler_reconfigure_wdts (5000 );
1016- BT_ASSERT_PRINT ("\r\n[DUMP_START:" );
1017- ble_log_async_output_dump_all (output );
1018- BT_ASSERT_PRINT (":DUMP_END]\r\n" );
1019- portEXIT_CRITICAL_SAFE (& spinlock );
1020- }
1021- #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
1022-
10231219#if (!CONFIG_BT_NIMBLE_ENABLED ) && (CONFIG_BT_CONTROLLER_ENABLED )
10241220#if CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC
10251221#define BLE_SM_KEY_ERR 0x17
0 commit comments