11#include "probe_monitor.h"
22#include "spi_bridge.h"
3+ #include "esp_log.h"
34#include <string.h>
5+ #include <stdlib.h>
46
57static probe_record_t cached_probe ;
8+ static probe_record_t * cached_results = NULL ;
9+ static uint16_t cached_count = 0 ;
10+ static uint16_t cached_capacity = 0 ;
11+ static probe_record_t empty_record ;
12+
13+ #define PROBE_MONITOR_MAX_RESULTS 300
614
715bool probe_monitor_start (void ) {
16+ probe_monitor_free_results ();
817 return (spi_bridge_send_command (SPI_ID_WIFI_APP_PROBE_MON , NULL , 0 , NULL , NULL , 2000 ) == ESP_OK );
918}
1019
1120void probe_monitor_stop (void ) {
1221 spi_bridge_send_command (SPI_ID_WIFI_APP_ATTACK_STOP , NULL , 0 , NULL , NULL , 2000 );
22+ probe_monitor_free_results ();
1323}
1424
1525probe_record_t * probe_monitor_get_results (uint16_t * count ) {
1626 spi_header_t resp ;
1727 uint8_t payload [2 ];
18- uint16_t magic_count = 0xFFFF ;
19- if (spi_bridge_send_command (SPI_ID_SYSTEM_DATA , (uint8_t * )& magic_count , 2 , & resp , payload , 1000 ) == ESP_OK ) {
20- memcpy (count , payload , 2 );
28+ uint16_t magic_count = SPI_DATA_INDEX_COUNT ;
29+ if (spi_bridge_send_command (SPI_ID_SYSTEM_DATA , (uint8_t * )& magic_count , 2 , & resp , payload , 1000 ) != ESP_OK ) {
30+ if (count ) * count = cached_count ;
31+ return cached_results ? cached_results : & empty_record ;
2132 }
22- return NULL ;
33+
34+ uint16_t remote_count = 0 ;
35+ memcpy (& remote_count , payload , 2 );
36+ if (remote_count > PROBE_MONITOR_MAX_RESULTS ) {
37+ remote_count = PROBE_MONITOR_MAX_RESULTS ;
38+ }
39+
40+ if (remote_count < cached_count ) {
41+ cached_count = 0 ;
42+ }
43+
44+ if (remote_count > cached_capacity ) {
45+ probe_record_t * new_buf = (probe_record_t * )realloc (cached_results , remote_count * sizeof (probe_record_t ));
46+ if (!new_buf ) {
47+ if (count ) * count = cached_count ;
48+ return cached_results ? cached_results : & empty_record ;
49+ }
50+ cached_results = new_buf ;
51+ cached_capacity = remote_count ;
52+ }
53+
54+ uint16_t fetched = cached_count ;
55+ for (uint16_t i = cached_count ; i < remote_count ; i ++ ) {
56+ if (spi_bridge_send_command (SPI_ID_SYSTEM_DATA , (uint8_t * )& i , 2 , & resp , (uint8_t * )& cached_results [i ], 1000 ) != ESP_OK ) {
57+ break ;
58+ }
59+ fetched = i + 1 ;
60+ }
61+
62+ cached_count = fetched ;
63+ if (count ) * count = cached_count ;
64+ return cached_results ? cached_results : & empty_record ;
2365}
2466
2567probe_record_t * probe_monitor_get_result_by_index (uint16_t index ) {
@@ -30,6 +72,17 @@ probe_record_t* probe_monitor_get_result_by_index(uint16_t index) {
3072 return NULL ;
3173}
3274
33- void probe_monitor_free_results (void ) {}
34- bool probe_monitor_save_results_to_internal_flash (void ) { return true; }
35- bool probe_monitor_save_results_to_sd_card (void ) { return true; }
75+ void probe_monitor_free_results (void ) {
76+ if (cached_results ) {
77+ free (cached_results );
78+ cached_results = NULL ;
79+ }
80+ cached_count = 0 ;
81+ cached_capacity = 0 ;
82+ }
83+ bool probe_monitor_save_results_to_internal_flash (void ) {
84+ return (spi_bridge_send_command (SPI_ID_WIFI_PROBE_SAVE_FLASH , NULL , 0 , NULL , NULL , 5000 ) == ESP_OK );
85+ }
86+ bool probe_monitor_save_results_to_sd_card (void ) {
87+ return (spi_bridge_send_command (SPI_ID_WIFI_PROBE_SAVE_SD , NULL , 0 , NULL , NULL , 5000 ) == ESP_OK );
88+ }
0 commit comments