fix merge issues

This commit is contained in:
proddy
2026-04-15 20:37:33 +02:00
parent 6b31fef1af
commit def5173692
2 changed files with 3959 additions and 3877 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -16,13 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "emsesp.h" #include "emsesp.h"
namespace emsesp { namespace emsesp {
uint8_t WebSettings::flags_ = 0; uint8_t WebSettings::flags_ = 0;
WebSettingsService::WebSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager) WebSettingsService::WebSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(WebSettings::read, WebSettings::update, this, server, EMSESP_SETTINGS_SERVICE_PATH, securityManager) : _httpEndpoint(WebSettings::read, WebSettings::update, this, server, EMSESP_SETTINGS_SERVICE_PATH, securityManager)
, _fsPersistence(WebSettings::read, WebSettings::update, this, fs, EMSESP_SETTINGS_FILE) { , _fsPersistence(WebSettings::read, WebSettings::update, this, fs, EMSESP_SETTINGS_FILE) {
securityManager->addEndpoint(server, EMSESP_BOARD_PROFILE_SERVICE_PATH, AuthenticationPredicates::IS_AUTHENTICATED, [this](AsyncWebServerRequest * request) { securityManager->addEndpoint(server, EMSESP_BOARD_PROFILE_SERVICE_PATH, AuthenticationPredicates::IS_AUTHENTICATED, [this](AsyncWebServerRequest * request) {
@@ -30,9 +30,9 @@ WebSettingsService::WebSettingsService(AsyncWebServer * server, FS * fs, Securit
}); });
addUpdateHandler([this] { onUpdate(); }, false); addUpdateHandler([this] { onUpdate(); }, false);
} }
void WebSettings::read(WebSettings & settings, JsonObject root) { void WebSettings::read(WebSettings & settings, JsonObject root) {
root["version"] = settings.version; root["version"] = settings.version;
root["board_profile"] = settings.board_profile; root["board_profile"] = settings.board_profile;
root["platform"] = EMSESP_PLATFORM; root["platform"] = EMSESP_PLATFORM;
@@ -83,11 +83,25 @@ void WebSettings::read(WebSettings & settings, JsonObject root) {
root["modbus_max_clients"] = settings.modbus_max_clients; root["modbus_max_clients"] = settings.modbus_max_clients;
root["modbus_timeout"] = settings.modbus_timeout; root["modbus_timeout"] = settings.modbus_timeout;
root["developer_mode"] = settings.developer_mode; root["developer_mode"] = settings.developer_mode;
} #ifndef NO_TLS_SUPPORT
root["email_enabled"] = settings.email_enabled;
#else
root["email_enabled"] = false;
#endif
root["email_ssl"] = settings.email_ssl;
root["email_starttls"] = settings.email_starttls;
root["email_server"] = settings.email_server;
root["email_port"] = settings.email_port;
root["email_login"] = settings.email_login;
root["email_pass"] = settings.email_pass;
root["email_sender"] = settings.email_sender;
root["email_recp"] = settings.email_recp;
root["email_subject"] = settings.email_subject;
}
// call on initialization and also when settings are updated/saved via web or console // call on initialization and also when settings are updated/saved via web or console
// note, settings is empty when the service starts // note, settings is empty when the service starts
StateUpdateResult WebSettings::update(JsonObject root, WebSettings & settings) { StateUpdateResult WebSettings::update(JsonObject root, WebSettings & settings) {
// make a copy of the settings to compare to later // make a copy of the settings to compare to later
const WebSettings original_settings(settings); const WebSettings original_settings(settings);
@@ -202,12 +216,12 @@ StateUpdateResult WebSettings::update(JsonObject root, WebSettings & settings) {
settings.syslog_port = root["syslog_port"] | EMSESP_DEFAULT_SYSLOG_PORT; settings.syslog_port = root["syslog_port"] | EMSESP_DEFAULT_SYSLOG_PORT;
check_flag(original_settings.syslog_port, settings.syslog_port, ChangeFlags::SYSLOG); check_flag(original_settings.syslog_port, settings.syslog_port, ChangeFlags::SYSLOG);
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
settings.syslog_host = root["syslog_host"] | EMSESP_DEFAULT_SYSLOG_HOST; settings.syslog_host = root["syslog_host"] | EMSESP_DEFAULT_SYSLOG_HOST;
if (original_settings.syslog_host != settings.syslog_host) { if (original_settings.syslog_host != settings.syslog_host) {
add_flags(ChangeFlags::SYSLOG); add_flags(ChangeFlags::SYSLOG);
} }
#endif #endif
// temperature sensor // temperature sensor
settings.dallas_parasite = root["dallas_parasite"] | EMSESP_DEFAULT_DALLAS_PARASITE; settings.dallas_parasite = root["dallas_parasite"] | EMSESP_DEFAULT_DALLAS_PARASITE;
@@ -243,13 +257,9 @@ StateUpdateResult WebSettings::update(JsonObject root, WebSettings & settings) {
// Modbus settings // Modbus settings
settings.modbus_enabled = root["modbus_enabled"] | EMSESP_DEFAULT_MODBUS_ENABLED; settings.modbus_enabled = root["modbus_enabled"] | EMSESP_DEFAULT_MODBUS_ENABLED;
check_flag(original_settings.modbus_enabled, settings.modbus_enabled, ChangeFlags::RESTART);
settings.modbus_port = root["modbus_port"] | EMSESP_DEFAULT_MODBUS_PORT; settings.modbus_port = root["modbus_port"] | EMSESP_DEFAULT_MODBUS_PORT;
check_flag(original_settings.modbus_port, settings.modbus_port, ChangeFlags::RESTART);
settings.modbus_max_clients = root["modbus_max_clients"] | EMSESP_DEFAULT_MODBUS_MAX_CLIENTS; settings.modbus_max_clients = root["modbus_max_clients"] | EMSESP_DEFAULT_MODBUS_MAX_CLIENTS;
check_flag(original_settings.modbus_max_clients, settings.modbus_max_clients, ChangeFlags::RESTART);
settings.modbus_timeout = root["modbus_timeout"] | EMSESP_DEFAULT_MODBUS_TIMEOUT; settings.modbus_timeout = root["modbus_timeout"] | EMSESP_DEFAULT_MODBUS_TIMEOUT;
check_flag(original_settings.modbus_timeout, settings.modbus_timeout, ChangeFlags::RESTART);
// //
// these may need mqtt restart to rebuild HA discovery topics // these may need mqtt restart to rebuild HA discovery topics
@@ -300,6 +310,20 @@ StateUpdateResult WebSettings::update(JsonObject root, WebSettings & settings) {
settings.weblog_level = root["weblog_level"] | EMSESP_DEFAULT_WEBLOG_LEVEL; settings.weblog_level = root["weblog_level"] | EMSESP_DEFAULT_WEBLOG_LEVEL;
settings.weblog_compact = root["weblog_compact"] | EMSESP_DEFAULT_WEBLOG_COMPACT; settings.weblog_compact = root["weblog_compact"] | EMSESP_DEFAULT_WEBLOG_COMPACT;
settings.email_enabled = root["email_enabled"] | FACTORY_EMAIL_ENABLE;
settings.email_ssl = root["email_ssl"] | FACTORY_EMAIL_SSL;
settings.email_starttls = root["email_starttls"] | FACTORY_EMAIL_STARTTLS;
settings.email_server = root["email_server"] | FACTORY_EMAIL_SERVER;
settings.email_port = root["email_port"] | FACTORY_EMAIL_PORT;
settings.email_login = root["email_login"] | FACTORY_EMAIL_LOGIN;
settings.email_pass = root["email_pass"] | FACTORY_EMAIL_PASSWORD;
settings.email_sender = root["email_sender"] | FACTORY_EMAIL_FROM;
settings.email_recp = root["email_recp"] | FACTORY_EMAIL_TO;
settings.email_subject = root["email_subject"] | FACTORY_EMAIL_SUBJECT;
if (settings.email_ssl && settings.email_starttls) {
settings.email_ssl = false;
}
// if no psram limit weblog buffer to 25 messages // if no psram limit weblog buffer to 25 messages
if (EMSESP::system_.PSram() > 0) { if (EMSESP::system_.PSram() > 0) {
settings.weblog_buffer = root["weblog_buffer"] | EMSESP_DEFAULT_WEBLOG_BUFFER; settings.weblog_buffer = root["weblog_buffer"] | EMSESP_DEFAULT_WEBLOG_BUFFER;
@@ -329,11 +353,11 @@ StateUpdateResult WebSettings::update(JsonObject root, WebSettings & settings) {
} }
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
} }
// this is called after any of the settings have been persisted to the filesystem // this is called after any of the settings have been persisted to the filesystem
// either via the Web UI or via the Console // either via the Web UI or via the Console
void WebSettingsService::onUpdate() { void WebSettingsService::onUpdate() {
// skip if we're restarting anyway // skip if we're restarting anyway
if (WebSettings::has_flags(WebSettings::ChangeFlags::RESTART)) { if (WebSettings::has_flags(WebSettings::ChangeFlags::RESTART)) {
@@ -373,18 +397,18 @@ void WebSettingsService::onUpdate() {
} }
WebSettings::reset_flags(); WebSettings::reset_flags();
} }
void WebSettingsService::begin() { void WebSettingsService::begin() {
_fsPersistence.readFromFS(); _fsPersistence.readFromFS();
} }
void WebSettingsService::save() { void WebSettingsService::save() {
_fsPersistence.writeToFS(); _fsPersistence.writeToFS();
} }
// send the board profile as JSON // send the board profile as JSON
void WebSettingsService::board_profile(AsyncWebServerRequest * request) { void WebSettingsService::board_profile(AsyncWebServerRequest * request) {
if (request->hasParam("boardProfile")) { if (request->hasParam("boardProfile")) {
std::string board_profile = request->getParam("boardProfile")->value().c_str(); std::string board_profile = request->getParam("boardProfile")->value().c_str();
@@ -413,11 +437,11 @@ void WebSettingsService::board_profile(AsyncWebServerRequest * request) {
AsyncWebServerResponse * response = request->beginResponse(200); AsyncWebServerResponse * response = request->beginResponse(200);
request->send(response); request->send(response);
} }
// loads the board profile to set the gpios // loads the board profile to set the gpios
// if the board profile is not found, or default, it will try to autodetect the board profile // if the board profile is not found, or default, it will try to autodetect the board profile
void WebSettings::set_board_profile(WebSettings & settings) { void WebSettings::set_board_profile(WebSettings & settings) {
// The optional NVS boot value has priority and overrides any board_profile setting. // The optional NVS boot value has priority and overrides any board_profile setting.
// This is only done for BBQKees boards // This is only done for BBQKees boards
// Note 1: we never set the NVS boot value in the code - this is done on initial pre-loading // Note 1: we never set the NVS boot value in the code - this is done on initial pre-loading
@@ -425,9 +449,9 @@ void WebSettings::set_board_profile(WebSettings & settings) {
if (EMSESP::system_.getBBQKeesGatewayDetails(FUSE_VALUE::MFG).startsWith("BBQKees")) { if (EMSESP::system_.getBBQKeesGatewayDetails(FUSE_VALUE::MFG).startsWith("BBQKees")) {
String bbq_board = EMSESP::system_.getBBQKeesGatewayDetails(FUSE_VALUE::BOARD); String bbq_board = EMSESP::system_.getBBQKeesGatewayDetails(FUSE_VALUE::BOARD);
if (!bbq_board.isEmpty() && settings.board_profile != "CUSTOM") { if (!bbq_board.isEmpty() && settings.board_profile != "CUSTOM") {
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
EMSESP::logger().info("Overriding board profile with fuse value %s", bbq_board.c_str()); EMSESP::logger().info("Overriding board profile with fuse value %s", bbq_board.c_str());
#endif #endif
settings.board_profile = bbq_board; settings.board_profile = bbq_board;
} }
} }
@@ -445,9 +469,9 @@ void WebSettings::set_board_profile(WebSettings & settings) {
std::vector<int8_t> data(10, 99); // initialize with 99 for all values, just as a safe guard to catch bad gpios std::vector<int8_t> data(10, 99); // initialize with 99 for all values, just as a safe guard to catch bad gpios
if (settings.board_profile != "default") { if (settings.board_profile != "default") {
if (!System::load_board_profile(data, settings.board_profile.c_str())) { if (!System::load_board_profile(data, settings.board_profile.c_str())) {
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
EMSESP::logger().debug("Unable to identify board profile %s", settings.board_profile.c_str()); EMSESP::logger().debug("Unable to identify board profile %s", settings.board_profile.c_str());
#endif #endif
settings.board_profile = "default"; // can't find profile, fallback to "default" settings.board_profile = "default"; // can't find profile, fallback to "default"
} }
} }
@@ -455,26 +479,17 @@ void WebSettings::set_board_profile(WebSettings & settings) {
// we still don't have a valid board profile. Let's see if we can determine one from the build config or hardware // we still don't have a valid board profile. Let's see if we can determine one from the build config or hardware
if (settings.board_profile == "default") { if (settings.board_profile == "default") {
EMSESP::logger().info("Autodetecting board profile"); EMSESP::logger().info("Autodetecting board profile");
#if CONFIG_IDF_TARGET_ESP32 #if CONFIG_IDF_TARGET_ESP32
// check for no PSRAM, could be a E32 or S32? // check for no PSRAM, could be a E32 or S32?
if (!ESP.getPsramSize()) { if (!ESP.getPsramSize()) {
#if ESP_ARDUINO_VERSION_MAJOR < 3
if (ETH.begin(1, 16, 23, 18, ETH_PHY_LAN8720, ETH_CLOCK_GPIO0_IN)) {
#else
if (ETH.begin(ETH_PHY_LAN8720, 1, 23, 18, 16, ETH_CLOCK_GPIO0_IN)) { if (ETH.begin(ETH_PHY_LAN8720, 1, 23, 18, 16, ETH_CLOCK_GPIO0_IN)) {
#endif
settings.board_profile = "E32"; // Ethernet without PSRAM settings.board_profile = "E32"; // Ethernet without PSRAM
} else { } else {
settings.board_profile = "S32"; // ESP32 standard WiFi without PSRAM settings.board_profile = "S32"; // ESP32 standard WiFi without PSRAM
} }
} else { } else {
// check for boards with PSRAM, could be a E32V2 otherwise default back to the S32 // check for boards with PSRAM, could be a E32V2 otherwise default back to the S32
#if ESP_ARDUINO_VERSION_MAJOR < 3
if (ETH.begin(0, 15, 23, 18, ETH_PHY_LAN8720, ETH_CLOCK_GPIO0_OUT)) {
#else
if (ETH.begin(ETH_PHY_LAN8720, 0, 23, 18, 15, ETH_CLOCK_GPIO0_OUT)) { if (ETH.begin(ETH_PHY_LAN8720, 0, 23, 18, 15, ETH_CLOCK_GPIO0_OUT)) {
#endif
if (analogReadMilliVolts(39) > 700) { // core voltage > 2.6V if (analogReadMilliVolts(39) > 700) { // core voltage > 2.6V
settings.board_profile = "E32V2_2"; // Ethernet, PSRAM, internal sensors settings.board_profile = "E32V2_2"; // Ethernet, PSRAM, internal sensors
} else { } else {
@@ -484,29 +499,29 @@ void WebSettings::set_board_profile(WebSettings & settings) {
settings.board_profile = "S32"; // ESP32 standard WiFi with PSRAM settings.board_profile = "S32"; // ESP32 standard WiFi with PSRAM
} }
} }
// override if we know the target from the build config like C3, S2, S3 etc.. // override if we know the target from the build config like C3, S2, S3 etc..
#elif CONFIG_IDF_TARGET_ESP32C3 #elif CONFIG_IDF_TARGET_ESP32C3
settings.board_profile = "C3MINI"; settings.board_profile = "C3MINI";
#elif CONFIG_IDF_TARGET_ESP32S2 #elif CONFIG_IDF_TARGET_ESP32S2
settings.board_profile = "S2MINI"; settings.board_profile = "S2MINI";
#elif CONFIG_IDF_TARGET_ESP32S3 #elif CONFIG_IDF_TARGET_ESP32S3
settings.board_profile = "S32S3"; // BBQKees Gateway S3 settings.board_profile = "S32S3"; // BBQKees Gateway S3
#elif CONFIG_IDF_TARGET_ESP32C6 #elif CONFIG_IDF_TARGET_ESP32C6
settings.board_profile = "CUSTOM"; settings.board_profile = "CUSTOM";
#endif #endif
// apply the new board profile setting // apply the new board profile setting
System::load_board_profile(data, settings.board_profile.c_str()); System::load_board_profile(data, settings.board_profile.c_str());
} }
// log board profile and PSRAM info // log board profile and PSRAM info
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
uint32_t psram_size = ESP.getPsramSize() / 1024; // in KB uint32_t psram_size = ESP.getPsramSize() / 1024; // in KB
if (psram_size > 0) { if (psram_size > 0) {
EMSESP::logger().info("Loaded board profile %s (PSRAM: %lu KB)", settings.board_profile.c_str(), psram_size); EMSESP::logger().info("Loaded board profile %s (PSRAM: %lu KB)", settings.board_profile.c_str(), psram_size);
} else { } else {
EMSESP::logger().info("Loaded board profile %s (PSRAM: not available)", settings.board_profile.c_str()); EMSESP::logger().info("Loaded board profile %s (PSRAM: not available)", settings.board_profile.c_str());
} }
#endif #endif
// apply the new board profile settings // apply the new board profile settings
// 0=led, 1=dallas, 2=rx, 3=tx, 4=button, 5=phy_type, 6=eth_power, 7=eth_phy_addr, 8=eth_clock_mode, 9=led_type // 0=led, 1=dallas, 2=rx, 3=tx, 4=button, 5=phy_type, 6=eth_power, 7=eth_phy_addr, 8=eth_clock_mode, 9=led_type
@@ -520,34 +535,35 @@ void WebSettings::set_board_profile(WebSettings & settings) {
settings.eth_phy_addr = data[7]; // Ethernet PHY Address settings.eth_phy_addr = data[7]; // Ethernet PHY Address
settings.eth_clock_mode = data[8]; // Ethernet Clock Mode settings.eth_clock_mode = data[8]; // Ethernet Clock Mode
settings.led_type = data[9]; // LED Type settings.led_type = data[9]; // LED Type
} }
// returns true if the value was changed // returns true if the value was changed
bool WebSettings::check_flag(int prev_v, int new_v, uint8_t flag) { bool WebSettings::check_flag(int prev_v, int new_v, uint8_t flag) {
if (prev_v != new_v) { if (prev_v != new_v) {
add_flags(flag); add_flags(flag);
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
// EMSESP::logger().debug("check_flag: flag %d, prev_v=%d, new_v=%d", flag, prev_v, new_v); // EMSESP::logger().debug("check_flag: flag %d, prev_v=%d, new_v=%d", flag, prev_v, new_v);
#endif #endif
return true; return true;
} }
return false; return false;
} }
void WebSettings::add_flags(uint8_t flags) { void WebSettings::add_flags(uint8_t flags) {
flags_ |= flags; flags_ |= flags;
} }
bool WebSettings::has_flags(uint8_t flags) { bool WebSettings::has_flags(uint8_t flags) {
return (flags_ & flags) == flags; return (flags_ & flags) == flags;
} }
void WebSettings::reset_flags() { void WebSettings::reset_flags() {
flags_ = ChangeFlags::NONE; flags_ = ChangeFlags::NONE;
} }
uint8_t WebSettings::get_flags() { uint8_t WebSettings::get_flags() {
return flags_; return flags_;
} }
} // namespace emsesp
} // namespace emsesp