Skip to content

Commit

Permalink
refactor(spamssid_cmd): Clean code and delete funct
Browse files Browse the repository at this point in the history
  • Loading branch information
JahazielLem committed Jan 23, 2025
1 parent 1e74aab commit 2d551ed
Showing 1 changed file with 91 additions and 19 deletions.
110 changes: 91 additions & 19 deletions firmware/main/modules/cmd_control/spamssid_cmd/spamssid_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,118 @@
#include "general_flash_storage.h"
#include "menus_module.h"

static const char* TAG = "spamssid_cmd";

static struct {
struct arg_str* name;
struct arg_str* value;
struct arg_end* end;
} ssdi_args;
} ssdi_save_args;

static struct {
struct arg_str* idx;
struct arg_end* end;
} ssdi_delete_args;

static int save_ssid_cmd(int argc, char** argv);
static int delete_ssid_cmd(int argc, char** argv);
static int show_ssid_cmd(int argc, char** argv);
static void show_ssid();

static void show_ssid() {
storage_contex_t list[99];
uint8_t list_count = 0;

flash_storage_get_list(GENFLASH_STORAGE_SPAM, list, &list_count);
for (int i = 0; i < list_count; i++) {
printf("[%d] %s\n", i, list[i].item_storage_name);
}
}

static int save_ssid(int argc, char** argv);
static int show_ssid_cmd(int argc, char** argv) {
show_ssid();
return 0;
}

static int save_ssid(int argc, char** argv) {
int nerrors = arg_parse(argc, argv, (void**) &ssdi_args);
static int delete_ssid_cmd(int argc, char** argv) {
ESP_LOGI("ssid", "Here 0");
int nerrors = arg_parse(argc, argv, (void**) &ssdi_delete_args);
if (nerrors != 0) {
arg_print_errors(stderr, ssdi_args.end, argv[0]);
arg_print_errors(stderr, ssdi_delete_args.end, argv[0]);
return 1;
}
ESP_LOGI("ssid", "Here 1");
int index = atoi(ssdi_delete_args.idx->sval[0]);
char* ssids_list[99] = {NULL};

storage_contex_t list[99];
uint8_t list_count = 0;
ESP_LOGI("ssid", "Here 2");
flash_storage_get_list(GENFLASH_STORAGE_SPAM, list, &list_count);
for (int i = 0; i < list_count; i++) {
ssids_list[i] = list[i].item_storage_name;
}
ESP_LOGI("ssid", "Here 1");
if (index < 0 || index >= list_count) {
ESP_LOGW(__func__, "Index out of range");
return 1;
}
ESP_LOGI("ssid", "Delete index %d %s", index, ssids_list[index]);

flash_storage_delete_list_item(GENFLASH_STORAGE_SPAM, ssids_list[index]);
show_ssid();
return 0;
}

static int save_ssid_cmd(int argc, char** argv) {
int nerrors = arg_parse(argc, argv, (void**) &ssdi_save_args);
if (nerrors != 0) {
arg_print_errors(stderr, ssdi_save_args.end, argv[0]);
return 1;
}
storage_contex_t new_ssid;
new_ssid.main_storage_name = GENFLASH_STORAGE_SPAM;
new_ssid.item_storage_name = ssdi_args.name->sval[0];
new_ssid.item_storage_name = ssdi_save_args.name->sval[0];
new_ssid.items_storage_value = malloc(GENFLASH_STORAGE_MAX_LEN_STR);
strcpy(new_ssid.items_storage_value, ssdi_args.value->sval[0]);
strcpy(new_ssid.items_storage_value, ssdi_save_args.value->sval[0]);
flash_storage_save_list_items(&new_ssid);
return 0;
}

void cmd_spamssid_register_commands() {
ssdi_args.name = arg_str0(NULL, "name", "<n>",
"Name of the SSID spam (Max 17 characteres)");
ssdi_args.value = arg_str1(NULL, NULL, "<value>",
"Words of the spam comma separed for each SSID");
ssdi_args.end = arg_end(2);
ssdi_save_args.name = arg_str0(NULL, "name", "<n>",
"Name of the SSID spam (Max 17 characteres)");
ssdi_save_args.value = arg_str1(
NULL, NULL, "<value>", "Words of the spam comma separed for each SSID");
ssdi_save_args.end = arg_end(2);

ssdi_delete_args.idx = arg_str1(NULL, NULL, "<idx>", "Index of the SSID");
ssdi_delete_args.end = arg_end(1);

const esp_console_cmd_t save_cmd = {
esp_console_cmd_t spamssid_save_cmd = {
.command = "spam_save",
.help = "Save SSID for spam",
.category = "spam",
.category = GENFLASH_STORAGE_SPAM,
.hint = NULL,
.func = &save_ssid_cmd,
.argtable = &ssdi_save_args,
};
esp_console_cmd_t spamssid_delete_cmd = {
.command = "spam_delete",
.help = "Delete SSID for spam",
.category = GENFLASH_STORAGE_SPAM,
.hint = NULL,
.func = &delete_ssid_cmd,
.argtable = &ssdi_delete_args,
};
esp_console_cmd_t spamssid_show_cmd = {
.command = "spam_show",
.help = "Show SSID's list",
.category = GENFLASH_STORAGE_SPAM,
.hint = NULL,
.func = &save_ssid,
.argtable = &ssdi_args,
.func = &show_ssid_cmd,
.argtable = NULL,
};

ESP_ERROR_CHECK(esp_console_cmd_register(&save_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&spamssid_save_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&spamssid_delete_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&spamssid_show_cmd));
}

0 comments on commit 2d551ed

Please sign in to comment.