diff --git a/include/cmd_lora.h b/include/cmd_lora.h index fde5066..acb4d93 100644 --- a/include/cmd_lora.h +++ b/include/cmd_lora.h @@ -11,6 +11,7 @@ void cmd_sf_register(); void cmd_preamble_register(); void cmd_send_register(); void cmd_dump_register(); +void cmd_hostname_register(); #ifdef __cplusplus } diff --git a/include/main.h b/include/main.h index e8d2cf1..ba7a841 100644 --- a/include/main.h +++ b/include/main.h @@ -9,8 +9,10 @@ extern uint8_t mac[6]; extern lora32_cfg_t lora; struct lorcomm_cfg { + char *hostname; + lora32_cfg_t *lora; - nvs_handle_t *nvs_handle; + nvs_handle_t nvs_handle; } lorcomm_cfg; diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 7d31325..ebf5145 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -9,6 +9,7 @@ set(COMPONENT_SRCS "\ cmd_preamble.c\ cmd_send.c\ cmd_dump.c\ + cmd_hostname.c\ ") set(COMPONENT_ADD_INCLUDEDIRS ". ../include") diff --git a/main/cmd_hostname.c b/main/cmd_hostname.c index 5edf802..5a0107a 100644 --- a/main/cmd_hostname.c +++ b/main/cmd_hostname.c @@ -3,6 +3,8 @@ #include "esp_log.h" #include "esp_console.h" #include "argtable3/argtable3.h" +#include "nvs_flash.h" +#include "esp_vfs_dev.h" #include "esp32-lora.h" #include "lorcomm.h" @@ -27,6 +29,10 @@ int hostname(int argc, char **argv) { printf(" > %s\n", lorcomm_cfg.hostname); } else { asprintf(&lorcomm_cfg.hostname, "%s", hostname_args.hostname->sval[0]); + + nvs_set_str(lorcomm_cfg.nvs_handle, "hostname", hostname_args.hostname->sval[0]); + esp_err_t err = nvs_commit(lorcomm_cfg.nvs_handle); + printf((err != ESP_OK) ? "Failed!\n" : "Done\n"); } return 0; @@ -37,6 +43,17 @@ void cmd_hostname_register(lora32_cfg_t *lora) { hostname_args.end = arg_end(1); hostname_args.lora = lora; + /* + size_t hostname_len; + + printf("nvs_handle: %d\n\n\n", lorcomm_cfg.nvs_handle); + nvs_get_str(lorcomm_cfg.nvs_handle, "hostname", NULL, &hostname_len); + nvs_get_str(lorcomm_cfg.nvs_handle, "hostname", lorcomm_cfg.hostname, &hostname_len); + printf("hostname: %s\n", lorcomm_cfg.hostname); + */ + + lorcomm_cfg.hostname = "lorcomm"; + const esp_console_cmd_t hostname_cmd = { .command = "hostname", .help = "Set/Get Hostname", diff --git a/main/console.c b/main/console.c index b78f9e4..5e06104 100644 --- a/main/console.c +++ b/main/console.c @@ -56,13 +56,17 @@ void console_task(void *args) { cmd_preamble_register(lorcomm_cfg.lora); cmd_send_register(lorcomm_cfg.lora); cmd_dump_register(lorcomm_cfg.lora); + cmd_hostname_register(lorcomm_cfg.lora); char *prompt; while(true) { - asprintf(&prompt, LOG_COLOR_I "%s > " LOG_RESET_COLOR, "lorcomm"); + asprintf(&prompt, LOG_COLOR_I "%s > " LOG_RESET_COLOR, lorcomm_cfg.hostname == NULL ? "lorcomm" : lorcomm_cfg.hostname); + char* line = linenoise(prompt); + free(prompt); + if (line == NULL) { /* Ignore empty lines */ continue; } @@ -84,6 +88,3 @@ void console_task(void *args) { linenoiseFree(line); } } - -void console_init() { -} diff --git a/main/main.c b/main/main.c index 894dc9e..9b0ab6e 100644 --- a/main/main.c +++ b/main/main.c @@ -101,9 +101,10 @@ void app_main(void) { if (err != ESP_OK) { printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err)); - lorcomm_cfg.nvs_handle = NULL; + //lorcomm_cfg.nvs_handle = NULL; } else { - lorcomm_cfg.nvs_handle = &nvs_handle; + lorcomm_cfg.nvs_handle = nvs_handle; + ESP_LOGI(TAG, "nvs_handle: %d", nvs_handle); } esp_efuse_mac_get_default((uint8_t*)&mac);