minor optimizations, use EMSESP_Version, only call esp_image_verify() and store the entry for partitions that actually have a value

This commit is contained in:
proddy
2026-04-18 17:40:21 +02:00
parent 2d7c8f0863
commit 2fbfdf94ab
3 changed files with 39 additions and 36 deletions

View File

@@ -26,13 +26,12 @@
#include <esp_mac.h> #include <esp_mac.h>
#include "esp_efuse.h" #include "esp_efuse.h"
#include <nvs.h> #include <nvs.h>
#include <mbedtls/base64.h>
#endif #endif
#include <HTTPClient.h> #include <HTTPClient.h>
#include <map> #include <map>
#include <semver200.h> #include "EMSESP_Version.h"
#if defined(EMSESP_TEST) #if defined(EMSESP_TEST)
#include "../test/test.h" #include "../test/test.h"
@@ -338,6 +337,7 @@ void System::get_partition_info() {
strftime(time_string, sizeof(time_string), "%FT%T", localtime(&d)); strftime(time_string, sizeof(time_string), "%FT%T", localtime(&d));
p_info.install_date = d > 1500000000L ? time_string : ""; p_info.install_date = d > 1500000000L ? time_string : "";
if (!p_info.version.empty()) {
esp_image_metadata_t meta = {}; esp_image_metadata_t meta = {};
esp_partition_pos_t part_pos = {.offset = part->address, .size = part->size}; esp_partition_pos_t part_pos = {.offset = part->address, .size = part->size};
if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &part_pos, &meta) == ESP_OK) { if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &part_pos, &meta) == ESP_OK) {
@@ -345,9 +345,9 @@ void System::get_partition_info() {
} else { } else {
p_info.size = 0; p_info.size = 0;
} }
partition_info_[part->label] = p_info; partition_info_[part->label] = p_info;
} }
}
it = esp_partition_next(it); // loop to next partition it = esp_partition_next(it); // loop to next partition
} }
@@ -1533,8 +1533,8 @@ bool System::check_upgrade() {
settingsVersion = "3.5.0"; // this was the last stable version without version info settingsVersion = "3.5.0"; // this was the last stable version without version info
} }
version::Semver200_version settings_version(settingsVersion); version::EMSESP_Version settings_version(settingsVersion);
version::Semver200_version this_version(EMSESP_APP_VERSION); version::EMSESP_Version this_version(EMSESP_APP_VERSION);
std::string settings_version_type = settings_version.prerelease().empty() ? "" : ("-" + settings_version.prerelease()); std::string settings_version_type = settings_version.prerelease().empty() ? "" : ("-" + settings_version.prerelease());
std::string this_version_type = this_version.prerelease().empty() ? "" : ("-" + this_version.prerelease()); std::string this_version_type = this_version.prerelease().empty() ? "" : ("-" + this_version.prerelease());
@@ -1685,18 +1685,17 @@ void System::exportSettings(const std::string & type, const char * filename, Jso
File settingsFile = LittleFS.open(filename); File settingsFile = LittleFS.open(filename);
if (settingsFile) { if (settingsFile) {
{
JsonDocument jsonDocument; JsonDocument jsonDocument;
DeserializationError error = deserializeJson(jsonDocument, settingsFile); DeserializationError error = deserializeJson(jsonDocument, settingsFile);
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) { settingsFile.close(); // close early, we no longer need the file
JsonObject node = output[section].to<JsonObject>(); if (error || !jsonDocument.is<JsonObject>()) {
for (JsonPair kvp : jsonDocument.as<JsonObject>()) {
node[kvp.key()] = kvp.value();
}
} else {
LOG_ERROR("Failed to deserialize settings file %s", filename); LOG_ERROR("Failed to deserialize settings file %s", filename);
return;
}
output[section].set(jsonDocument.as<JsonObject>());
} }
LOG_DEBUG("Exported %s settings from file %s", section, filename); LOG_DEBUG("Exported %s settings from file %s", section, filename);
settingsFile.close();
} else { } else {
LOG_ERROR("No settings file for %s found", filename); LOG_ERROR("No settings file for %s found", filename);
} }
@@ -1748,20 +1747,24 @@ void System::exportSystemBackup(JsonObject output) {
if (file) { if (file) {
JsonDocument jsonDocument; JsonDocument jsonDocument;
DeserializationError error = deserializeJson(jsonDocument, file); DeserializationError error = deserializeJson(jsonDocument, file);
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) { file.close(); // close early, we no longer need the file
JsonObject node = nodes.add<JsonObject>(); if (!error && jsonDocument.is<JsonObject>()) {
node["type"] = "customSupport"; JsonObject support_node = nodes.add<JsonObject>();
node["data"] = jsonDocument.as<JsonObject>(); support_node["type"] = "customSupport";
} support_node["data"].set(jsonDocument.as<JsonObject>());
file.close();
LOG_DEBUG("Exported custom support file %s", EMSESP_CUSTOMSUPPORT_FILE); LOG_DEBUG("Exported custom support file %s", EMSESP_CUSTOMSUPPORT_FILE);
} else {
LOG_ERROR("Failed to deserialize custom support file");
}
} }
// Backup NVS values // Backup NVS values
node = nodes.add<JsonObject>(); node = nodes.add<JsonObject>();
node["type"] = "nvs"; node["type"] = "nvs";
const char * nvs_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, "nvs1") ? "nvs1" : "nvs"; // nvs1 is on 16MBs // EMSESP::nvs_ is opened on "nvs" (see EMSESP::start), so always read the backup from there.
// The old "nvs1" partition may still exist on 16MB boards but has been migrated and emptied.
const char * nvs_part = "nvs";
nvs_iterator_t it = nullptr; nvs_iterator_t it = nullptr;
#if ESP_IDF_VERSION_MAJOR < 5 #if ESP_IDF_VERSION_MAJOR < 5
it = nvs_entry_find(nvs_part, "ems-esp", NVS_TYPE_ANY); it = nvs_entry_find(nvs_part, "ems-esp", NVS_TYPE_ANY);

View File

@@ -253,7 +253,7 @@ uint8_t WebStatusService::upgradeImportantMessages(std::string & version) {
// it's a filename with a .bin or .md extension, try and extract the version from it // it's a filename with a .bin or .md extension, try and extract the version from it
// e.g. EMS-ESP-3_8_2-dev_13-ESP32-16MB+.bin -> major=3 minor=8 patch=2 // e.g. EMS-ESP-3_8_2-dev_13-ESP32-16MB+.bin -> major=3 minor=8 patch=2
version::Semver200_version latest_version; version::EMSESP_Version latest_version;
if ((version.find(".bin") != std::string::npos) || (version.find(".md") != std::string::npos)) { if ((version.find(".bin") != std::string::npos) || (version.find(".md") != std::string::npos)) {
std::string filename = version; std::string filename = version;
auto pos = filename.find("EMS-ESP-"); auto pos = filename.find("EMS-ESP-");
@@ -274,18 +274,18 @@ uint8_t WebStatusService::upgradeImportantMessages(std::string & version) {
std::string major_version = filename.substr(pos, underscore1 - pos); std::string major_version = filename.substr(pos, underscore1 - pos);
std::string minor_version = filename.substr(underscore1 + 1, underscore2 - underscore1 - 1); std::string minor_version = filename.substr(underscore1 + 1, underscore2 - underscore1 - 1);
std::string patch_version = filename.substr(underscore2 + 1, dash - underscore2 - 1); std::string patch_version = filename.substr(underscore2 + 1, dash - underscore2 - 1);
latest_version = version::Semver200_version(major_version + "." + minor_version + "." + patch_version); latest_version = version::EMSESP_Version(major_version + "." + minor_version + "." + patch_version);
} else { } else {
// if it's .json file exit // if it's .json file exit
if (version.find(".json") != std::string::npos) { if (version.find(".json") != std::string::npos) {
return 0; return 0;
} else { } else {
// treat it like a version string like "3.9.0" // treat it like a version string like "3.9.0"
latest_version = version::Semver200_version(version); latest_version = version::EMSESP_Version(version);
} }
} }
version::Semver200_version current_version(current_version_s); // get current version version::EMSESP_Version current_version(current_version_s); // get current version
if (latest_version > current_version && current_version.minor() < latest_version.minor()) { if (latest_version > current_version && current_version.minor() < latest_version.minor()) {
return 0; // if it's just a minor version upgrade return 0 return 0; // if it's just a minor version upgrade return 0
@@ -306,9 +306,9 @@ uint8_t WebStatusService::upgradeImportantMessages(std::string & version) {
// versions holds the latest development version and stable version in one string, comma separated // versions holds the latest development version and stable version in one string, comma separated
bool WebStatusService::checkUpgrade(JsonObject root, std::string & version) { bool WebStatusService::checkUpgrade(JsonObject root, std::string & version) {
if (!version.empty()) { if (!version.empty()) {
version::Semver200_version current_version(current_version_s); version::EMSESP_Version current_version(current_version_s);
version::Semver200_version latest_dev_version(version.substr(0, version.find(','))); version::EMSESP_Version latest_dev_version(version.substr(0, version.find(',')));
version::Semver200_version latest_stable_version(version.substr(version.find(',') + 1)); version::EMSESP_Version latest_stable_version(version.substr(version.find(',') + 1));
bool dev_upgradeable = latest_dev_version > current_version; bool dev_upgradeable = latest_dev_version > current_version;
bool stable_upgradeable = latest_stable_version > current_version; bool stable_upgradeable = latest_stable_version > current_version;

View File

@@ -4,7 +4,7 @@
#define EMSESP_SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus" #define EMSESP_SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus"
#define EMSESP_ACTION_SERVICE_PATH "/rest/action" #define EMSESP_ACTION_SERVICE_PATH "/rest/action"
#include <semver200.h> // for version checking #include "../core/EMSESP_Version.h"
#include "../emsesp_version.h" #include "../emsesp_version.h"
namespace emsesp { namespace emsesp {