diff --git a/data/web/index.html b/data/web/index.html index 91667ae..5923e95 100644 --- a/data/web/index.html +++ b/data/web/index.html @@ -469,6 +469,16 @@ case 'debug': logToDebug(data.message); break; + case 'radio_config': + if (data.config) { + loraPresetSelector.value = 'custom'; + document.getElementById('radio-freq').value = data.config.frequency || 869.25; + loraBw.value = data.config.bandwidth || 250.0; + loraSf.value = data.config.spreading_factor || 11; + loraCr.value = data.config.coding_rate || 5; + document.getElementById('radio-power').value = data.config.power || 20; + break; + } default: logToDebug(`Unknown message type: ${data.type}`); } diff --git a/main/main.cpp b/main/main.cpp index dd58ddd..72ebba5 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -15,6 +15,7 @@ #include "MeshtasticCompact.hpp" #include "webserver.h" #include "TMAttack.hpp" +#include "settings.hpp" static const char* TAG = "DarkMesh"; @@ -25,16 +26,17 @@ extern "C" { void app_main(); } TMAttack tmAttack; +TConfig config; Radio_PINS radio_pins = {9, 11, 10, 8, 14, 12, 13}; // Default radio pins for Heltec WSL V3. LoraConfig lora_config = { - .frequency = 869.25, - .bandwidth = 250.0, - .spreading_factor = 11, - .coding_rate = 5, + .frequency = 869.25, // config + .bandwidth = 250.0, // config + .spreading_factor = 11, // config + .coding_rate = 5, // config .sync_word = 0x2b, .preamble_length = 16, - .output_power = 22, + .output_power = 22, // config .tcxo_voltage = 1.8, .use_regulator_ldo = false, }; // default LoRa configuration for EU LONGFAST 433 @@ -87,6 +89,8 @@ void app_main(void) { ESP_LOGI(TAG, "WiFi AP initialized. SSID: %s, Password: %s", ssid, password); init_httpd(); + ESP_LOGI(TAG, "Loading radio config."); + config.load_radio(lora_config); ESP_LOGI(TAG, "Radio initializing..."); meshtasticCompact.RadioInit(RadioType::SX1262, radio_pins, lora_config); @@ -235,6 +239,12 @@ void handle_set_config(JSON_Object* params) { meshtasticCompact.setRadioSpreadingFactor(static_cast(sf)); meshtasticCompact.setRadioCodingRate(static_cast(cr)); meshtasticCompact.setRadioPower(static_cast(power)); + lora_config.frequency = frequency; + lora_config.bandwidth = bandwidth; + lora_config.spreading_factor = static_cast(sf); + lora_config.coding_rate = static_cast(cr); + lora_config.output_power = static_cast(power); + config.save_radio(lora_config); sendDebugMessage("Radio configuration updated."); } @@ -266,5 +276,7 @@ void handle_initme() { std::string attack_json = "{ \"type\": \"status_update\", \"current_attack\": \"" + tmAttack.getCurrentAttackTypeString() + "\" }"; ws_sendall((uint8_t*)attack_json.c_str(), attack_json.length(), true); vTaskDelay(pdMS_TO_TICKS(20)); - // todo send radio config + // send radio config + std::string config_json = "{ \"type\": \"radio_config\", \"config\": { \"frequency\": " + std::to_string(lora_config.frequency) + ", \"bandwidth\": " + std::to_string(lora_config.bandwidth) + ", \"spreading_factor\": " + std::to_string(lora_config.spreading_factor) + ", \"coding_rate\": " + std::to_string(lora_config.coding_rate) + ", \"power\": " + std::to_string(lora_config.output_power) + " } }"; + ws_sendall((uint8_t*)config_json.c_str(), config_json.length(), true); } diff --git a/main/settings.hpp b/main/settings.hpp new file mode 100644 index 0000000..d60890e --- /dev/null +++ b/main/settings.hpp @@ -0,0 +1,62 @@ +#pragma once + +#include +#include +#include +#include "MeshasticCompactStructs.hpp" + +class TConfig { + public: + std::string ssid; + std::string password; + int channel; + + TConfig() : ssid(""), password(""), channel(1) {} + + bool load_radio(LoraConfig& lora_config) { + nvs_handle_t handle; + esp_err_t err = nvs_open("radio_cfg", NVS_READONLY, &handle); + if (err != ESP_OK) return false; + + char tmpstr[64] = {0}; + size_t tmpstrlen = sizeof(tmpstr); + err = nvs_get_str(handle, "freqstr", tmpstr, &tmpstrlen); + if (err == ESP_OK) { + // Parse frequency string + std::string freq_string(tmpstr); + lora_config.frequency = std::stof(freq_string); + } + err = nvs_get_str(handle, "bwstr", tmpstr, &tmpstrlen); + if (err == ESP_OK) { + // Parse bandwidth string + std::string bw_string(tmpstr); + lora_config.bandwidth = std::stof(bw_string); + } + err = nvs_get_u8(handle, "sf", &lora_config.spreading_factor); + if (err != ESP_OK) lora_config.spreading_factor = 11; + err = nvs_get_u8(handle, "cr", &lora_config.coding_rate); + if (err != ESP_OK) lora_config.coding_rate = 5; + err = nvs_get_i8(handle, "outpower", &lora_config.output_power); + if (err != ESP_OK) lora_config.output_power = 20; + + nvs_close(handle); + return true; + } + + bool + save_radio(LoraConfig& lora_config) { + nvs_handle_t handle; + esp_err_t err = nvs_open("radio_cfg", NVS_READWRITE, &handle); + if (err != ESP_OK) return false; + + err = nvs_set_str(handle, "freqstr", std::to_string(lora_config.frequency).c_str()); + err = nvs_set_str(handle, "bwstr", std::to_string(lora_config.bandwidth).c_str()); + err = nvs_set_u8(handle, "sf", lora_config.spreading_factor); + err = nvs_set_u8(handle, "cr", lora_config.coding_rate); + err = nvs_set_i8(handle, "outpower", lora_config.output_power); + + nvs_commit(handle); + nvs_close(handle); + return true; + } +}; \ No newline at end of file