Skip to content

Commit 37ca310

Browse files
committed
refactor: clean up unused variables and improve code readability across multiple files
1 parent 15e6758 commit 37ca310

17 files changed

Lines changed: 219 additions & 79 deletions

File tree

firmware/components/cmd_wifi/cmd_wifi.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ static const char* TAG = "cmd_wifi";
3535
// static EventGroupHandle_t wifi_event_group;
3636
// const int CONNECTED_BIT = BIT0;
3737
// static app_callback callback_connection;
38-
static bool disconnect_cb = false;
39-
static int reconnections = 0;
40-
static int connect(int argc, char** argv);
41-
static bool wifi_join(const char* ssid, const char* pass, int timeout_ms);
4238
static void cmd_wifi_handle_credentials(const char* ssid, const char* pass);
4339
static int cmd_wifi_show_aps(int argc, char** argv);
4440

@@ -169,31 +165,6 @@ static int cmd_wifi_show_aps(int argc, char** argv) {
169165
return 0;
170166
}
171167

172-
static int connect(int argc, char** argv) {
173-
int nerrors = arg_parse(argc, argv, (void**) &join_args);
174-
if (nerrors != 0) {
175-
arg_print_errors(stderr, join_args.end, argv[0]);
176-
return 1;
177-
}
178-
ESP_LOGI(__func__, "Connecting to '%s'", join_args.ssid->sval[0]);
179-
180-
/* set default value*/
181-
if (join_args.timeout->count == 0) {
182-
join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
183-
}
184-
printf("timeout: %d\n", join_args.timeout->ival[0]);
185-
printf("ssid: %s\n", join_args.ssid->sval[0]);
186-
bool connected =
187-
wifi_join(join_args.ssid->sval[0], join_args.password->sval[0],
188-
join_args.timeout->ival[0]);
189-
if (!connected) {
190-
ESP_LOGW(__func__, "Connection timed out");
191-
return 1;
192-
}
193-
ESP_LOGI(__func__, "Connected");
194-
return 0;
195-
}
196-
197168
void register_wifi(void) {
198169
#if !defined(CONFIG_CMD_WIFI_DEBUG)
199170
esp_log_level_set(TAG, ESP_LOG_NONE);

firmware/components/wifi_controller/wifi_controller.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ static volatile bool netif_default_created = false;
88
static bool run_once = false;
99
static uint8_t default_ap_mac[6];
1010
static esp_err_t err;
11-
static wifi_config_t wifi_manager_config;
1211

1312
wifi_config_t wifi_driver_access_point_begin() {
1413
// #if !defined(CONFIG_WIFI_CONTROLLER_DEBUG)
@@ -129,6 +128,9 @@ void wifi_driver_init_sta(void) {
129128
}
130129

131130
void wifi_driver_init_null(void) {
131+
if (wifi_driver_initialized) {
132+
wifi_driver_deinit();
133+
}
132134
esp_err_t err = esp_event_loop_create_default();
133135
if (err == ESP_ERR_INVALID_STATE) {
134136
ESP_LOGI(TAG_WIFI_DRIVER, "Event loop already created");

firmware/components/wifi_sniffer/cmd_pcap.c

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
*/
88
#include <stdlib.h>
99
#include <string.h>
10+
#include <dirent.h>
1011
#include "argtable3/argtable3.h"
12+
#include "esp_spiffs.h"
1113
#include "freertos/FreeRTOS.h"
1214
#include "freertos/queue.h"
1315
#include "freertos/semphr.h"
@@ -38,6 +40,9 @@ static const char* TAG = "cmd_pcap";
3840
#define TRACE_TIMER_FLUSH_INT_MS (1000)
3941
#define PCAP_FLUSH_INTERVAL_PACKETS \
4042
(100) // Flush file every N packets to prevent buffer overflow
43+
// Minimum free bytes required in internal flash before starting a new capture.
44+
// Shared logic with cmd_sniffer.c — keep in sync if changed there.
45+
#define PCAP_FLASH_MIN_FREE_BYTES (51200)
4146

4247
#define ANALIZER_SD_CARD "/sdcard"
4348
#define ANALIZER_FLASH_FS "/internal"
@@ -100,6 +105,56 @@ static void create_pcaps_dir() {
100105
}
101106
}
102107

108+
/**
109+
* @brief Remove all analizer*.pcap files from the internal SPIFFS partition.
110+
*
111+
* The analyzer accumulates one .pcap file per session and never deletes old
112+
* ones. On a 512 KB partition this quickly exhausts all available space.
113+
* We only ever need the most-recent capture, so we wipe the stale files
114+
* before opening a new session.
115+
*/
116+
static void cleanup_internal_pcap_files(void) {
117+
DIR* dir = opendir(ANALIZER_FLASH_FS);
118+
if (!dir) {
119+
return;
120+
}
121+
struct dirent* entry;
122+
char filepath[280];
123+
while ((entry = readdir(dir)) != NULL) {
124+
// Match any file that starts with "analizer" and ends with ".pcap"
125+
const char* name = entry->d_name;
126+
size_t len = strlen(name);
127+
if (len > 5 &&
128+
strncmp(name, "analizer", 8) == 0 &&
129+
strcmp(name + len - 5, ".pcap") == 0) {
130+
snprintf(filepath, sizeof(filepath), "%s/%s", ANALIZER_FLASH_FS, name);
131+
ESP_LOGI(TAG, "Removing stale pcap: %s", filepath);
132+
remove(filepath);
133+
}
134+
}
135+
closedir(dir);
136+
}
137+
138+
/**
139+
* @brief Return ESP_ERR_NO_MEM if the internal partition has less than the
140+
* minimum free bytes required to start a new capture session.
141+
*/
142+
static esp_err_t check_internal_free_space(void) {
143+
size_t total = 0, used = 0;
144+
esp_err_t ret = esp_spiffs_info("internal", &total, &used);
145+
if (ret != ESP_OK) {
146+
// Cannot determine free space — optimistically continue
147+
return ESP_OK;
148+
}
149+
size_t free_bytes = (used < total) ? (total - used) : 0;
150+
if (free_bytes < PCAP_FLASH_MIN_FREE_BYTES) {
151+
ESP_LOGW(TAG, "Not enough internal space: %zu free / %zu total (need %d)",
152+
free_bytes, total, PCAP_FLASH_MIN_FREE_BYTES);
153+
return ESP_ERR_NO_MEM;
154+
}
155+
return ESP_OK;
156+
}
157+
103158
void wifi_sniffer_register_summary_cb(summary_cb_t cb) {
104159
summary_cb = cb;
105160
}
@@ -162,8 +217,16 @@ static esp_err_t pcap_open(pcap_cmd_runtime_t* pcap) {
162217
fp = fopen(pcap->filename, "wb+");
163218
} else if (wifi_sniffer_is_destination_internal()) {
164219
flash_fs_mount();
165-
files_ops_incremental_name(ANALIZER_FLASH_FS, "analizer", ".pcap",
166-
pcap->filename);
220+
// Remove leftover .pcap files from previous sessions to reclaim space,
221+
// then verify there is enough room before attempting to create the file.
222+
cleanup_internal_pcap_files();
223+
esp_err_t space_ret = check_internal_free_space();
224+
if (space_ret != ESP_OK) {
225+
return space_ret; // ESP_ERR_NO_MEM → triggers out_of_mem_cb correctly
226+
}
227+
// Always overwrite the same filename so we never accumulate old captures.
228+
snprintf(pcap->filename, PCAP_FILE_NAME_MAX_LEN, "%s/analizer00.pcap",
229+
ANALIZER_FLASH_FS);
167230
fp = fopen(pcap->filename, "wb+");
168231
} else {
169232
ESP_LOGE(TAG, "pcap file destination hasn't specified");
@@ -185,6 +248,12 @@ static esp_err_t pcap_open(pcap_cmd_runtime_t* pcap) {
185248
if (fp) {
186249
fclose(fp);
187250
}
251+
// If the file could not be opened and we don't already have a specific error
252+
// code, promote ESP_FAIL to ESP_ERR_NO_MEM so the caller's out_of_mem_cb
253+
// fires correctly (ESP_FAIL would be silently ignored after our earlier fix).
254+
if (ret == ESP_FAIL) {
255+
ret = ESP_ERR_NO_MEM;
256+
}
188257
return ret;
189258
}
190259

firmware/components/wifi_sniffer/cmd_sniffer.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ static void sniffer_task(void* parameters) {
363363
static esp_err_t sniffer_stop(sniffer_runtime_t* sniffer) {
364364
sniffer->sniffed_packets = 0;
365365
if (sniffer_animation_stop_cb) {
366-
printf("sniffer_animation_stop_cb\n");
367366
sniffer_animation_stop_cb();
368367
}
369368
esp_err_t ret = ESP_OK;
@@ -385,38 +384,30 @@ static esp_err_t sniffer_stop(sniffer_runtime_t* sniffer) {
385384
break;
386385
}
387386
ESP_LOGI(TAG, "stop promiscuous ok");
388-
printf("sniffer->packets_to_sniff\n");
389387

390388
/* stop sniffer local task */
391389
sniffer->is_running = false;
392390
/* wait for task over */
393391
if (sniffer->packets_to_sniff != 0) {
394-
printf("sniffer->packets_to_sniff: %ld \n", sniffer->packets_to_sniff);
395392
xSemaphoreTake(sniffer->sem_task_over, portMAX_DELAY);
396393
}
397-
printf("sniffer->xSemaphoreTake\n");
398394

399395
vSemaphoreDelete(sniffer->sem_task_over);
400-
printf("sniffer->vSemaphoreDelete\n");
401396
sniffer->sem_task_over = NULL;
402397
/* make sure to free all resources in the left items */
403398
UBaseType_t left_items = uxQueueMessagesWaiting(sniffer->work_queue);
404-
printf("sniffer->uxQueueMessagesWaiting\n");
405399

406400
sniffer_packet_info_t packet_info;
407401
while (left_items--) {
408402
xQueueReceive(sniffer->work_queue, &packet_info,
409403
pdMS_TO_TICKS(SNIFFER_PROCESS_PACKET_TIMEOUT_MS));
410404
free(packet_info.payload);
411405
}
412-
printf("sniffer->packet_info\n");
413406
vQueueDelete(sniffer->work_queue);
414-
printf("sniffer->vQueueDelete\n");
415407
sniffer->work_queue = NULL;
416408

417409
/* stop pcap session */
418410
sniff_packet_stop();
419-
printf("sniffer->sniff_packet_stop\n");
420411
err:
421412
return ret;
422413
}
@@ -492,7 +483,12 @@ static esp_err_t sniffer_start(sniffer_runtime_t* sniffer) {
492483
err_queue:
493484
sniffer->is_running = false;
494485
err:
495-
out_of_mem_cb();
486+
// Only trigger the OOM handler for genuine out-of-memory failures.
487+
// Calling it for ESP_ERR_INVALID_STATE or other errors would corrupt
488+
// the sniffer state by calling sniffer_stop() on a half-initialized runtime.
489+
if (out_of_mem_cb && ret == ESP_ERR_NO_MEM) {
490+
out_of_mem_cb();
491+
}
496492
return ret;
497493
}
498494

firmware/main/apps/wifi/captive/captive_cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static int captivecmd_change_name(int argc, char** argv) {
2323
return 1;
2424
}
2525

26-
captive_module_change_ap_name(captivecmd_ap_name_args.name->sval[0]);
26+
captive_module_change_ap_name((char*) captivecmd_ap_name_args.name->sval[0]);
2727

2828
return 0;
2929
}

firmware/main/apps/wifi/deauth/deauth_module.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ static TaskHandle_t scanning_task_handle = NULL;
4848

4949
static void scanning_task();
5050
static void deauth_run_scan_task();
51-
static void deauth_increment_item();
52-
static void deauth_decrement_item();
5351
static void deauth_handle_attacks();
5452

5553
static void scanning_task() {

firmware/main/apps/wifi/deauth_detector/scenes/detector_scenes.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void detector_scenes_show_table(uint16_t* deauth_packets_count_list) {
5050
}
5151

5252
////////////////////////// Main Menu //////////////////////////
53-
static enum { RUN_OPTION, SETTINGS_OPTION, HELP_OPTION } main_menu_options_e;
53+
typedef enum { RUN_OPTION, SETTINGS_OPTION, HELP_OPTION } main_menu_options_e;
5454
static const char* main_menu_options[] = {"Run", "Settings", "Help"};
5555
static void main_menu_handler(uint8_t selection) {
5656
switch (selection) {
@@ -83,7 +83,7 @@ void detector_scenes_main_menu() {
8383
}
8484

8585
////////////////////////// Settings Menu //////////////////////////
86-
static enum { CHANNEL_HOP_OPTION, CHANNEL_OPTION } settings_options_e;
86+
typedef enum { CHANNEL_HOP_OPTION, CHANNEL_OPTION } settings_options_e;
8787
static char* settings_options[] = {"Channel hop", "Channel"};
8888
static void settings_handler(uint8_t scan_mode) {
8989
switch (scan_mode) {
@@ -155,8 +155,8 @@ static void help_exit() {
155155
void detector_scenes_help() {
156156
general_scrolling_text_ctx help;
157157
memset(&help, 0, sizeof(help));
158-
help.banner = "< Back";
159-
help.text = help_text;
158+
help.banner = (char*) "< Back";
159+
help.text = (char*) help_text;
160160
help.scroll_type = GENERAL_SCROLLING_TEXT_CLAMPED;
161161
help.window_type = GENERAL_SCROLLING_TEXT_WINDOW;
162162
help.exit_cb = help_exit;

firmware/main/apps/wifi/modbus_tcp/modbus_dos/cmd/modbus_dos_cmd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static int set_ssid(int argc, char** argv) {
4141
return 1;
4242
}
4343

44-
modbus_dos_prefs_set_ssid(mb_dos_ssid_args.ssid->sval[0]);
44+
modbus_dos_prefs_set_ssid((char*) mb_dos_ssid_args.ssid->sval[0]);
4545
return 0;
4646
}
4747

@@ -52,7 +52,7 @@ static int set_pass(int argc, char** argv) {
5252
return 1;
5353
}
5454

55-
modbus_dos_prefs_set_pass(mb_dos_pass_args.pass->sval[0]);
55+
modbus_dos_prefs_set_pass((char*) mb_dos_pass_args.pass->sval[0]);
5656
return 0;
5757
}
5858

@@ -63,7 +63,7 @@ static int set_ip(int argc, char** argv) {
6363
return 1;
6464
}
6565

66-
modbus_dos_prefs_set_ip(mb_dos_ip_args.ip->sval[0]);
66+
modbus_dos_prefs_set_ip((char*) mb_dos_ip_args.ip->sval[0]);
6767
return 0;
6868
}
6969

firmware/main/apps/wifi/modbus_tcp/modbus_dos/modbus_dos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void reading_task() {
188188
// while (is_running) {
189189
skt = modbus_tcp_connect(port, ip);
190190
if (skt >= 0) {
191-
modbus_tcp_request(skt, request, sizeof(request));
191+
modbus_tcp_request(skt, (uint8_t*) request, sizeof(request));
192192
// vTaskDelay(500);
193193
}
194194
close(skt);

firmware/main/apps/wifi/modbus_tcp/modbus_dos/scenes/modbus_dos_scenes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ static const char* settings_options[] = {"SSID", "Password", "Server IP",
8989

9090
static void show_cmds_help(const char* head, const char* body) {
9191
general_notification_ctx_t notification = {0};
92-
notification.head = head;
93-
notification.body = body;
92+
notification.head = (char*) head;
93+
notification.body = (char*) body;
9494
notification.on_exit = modbus_dos_scenes_settings;
9595

9696
general_notification_handler(notification);
@@ -137,7 +137,7 @@ static const char* help_txt[] = {"You must set", "the server",
137137

138138
void modbus_dos_scenes_help() {
139139
general_scrolling_text_ctx help = {0};
140-
help.banner = "Modbus DOS Help";
140+
help.banner = (char*) "Modbus DOS Help";
141141
help.text_arr = help_txt;
142142
help.text_len = sizeof(help_txt) / sizeof(char*);
143143
help.window_type = GENERAL_SCROLLING_TEXT_WINDOW;

0 commit comments

Comments
 (0)