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_efuse.h"
#include <nvs.h>
#include <mbedtls/base64.h>
#endif
#include <HTTPClient.h>
#include <map>
#include <semver200.h>
#include "EMSESP_Version.h"
#if defined(EMSESP_TEST)
#include "../test/test.h"
@@ -338,15 +337,16 @@ void System::get_partition_info() {
strftime(time_string, sizeof(time_string), "%FT%T", localtime(&d));
p_info.install_date = d > 1500000000L ? time_string : "";
esp_image_metadata_t meta = {};
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) {
p_info.size = meta.image_len / 1024; // actual firmware size in KB
} else {
p_info.size = 0;
if (!p_info.version.empty()) {
esp_image_metadata_t meta = {};
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) {
p_info.size = meta.image_len / 1024; // actual firmware size in KB
} else {
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
@@ -1533,8 +1533,8 @@ bool System::check_upgrade() {
settingsVersion = "3.5.0"; // this was the last stable version without version info
}
version::Semver200_version settings_version(settingsVersion);
version::Semver200_version this_version(EMSESP_APP_VERSION);
version::EMSESP_Version settings_version(settingsVersion);
version::EMSESP_Version this_version(EMSESP_APP_VERSION);
std::string settings_version_type = settings_version.prerelease().empty() ? "" : ("-" + settings_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);
if (settingsFile) {
JsonDocument jsonDocument;
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
JsonObject node = output[section].to<JsonObject>();
for (JsonPair kvp : jsonDocument.as<JsonObject>()) {
node[kvp.key()] = kvp.value();
{
JsonDocument jsonDocument;
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
settingsFile.close(); // close early, we no longer need the file
if (error || !jsonDocument.is<JsonObject>()) {
LOG_ERROR("Failed to deserialize settings file %s", filename);
return;
}
} else {
LOG_ERROR("Failed to deserialize settings file %s", filename);
output[section].set(jsonDocument.as<JsonObject>());
}
LOG_DEBUG("Exported %s settings from file %s", section, filename);
settingsFile.close();
} else {
LOG_ERROR("No settings file for %s found", filename);
}
@@ -1748,20 +1747,24 @@ void System::exportSystemBackup(JsonObject output) {
if (file) {
JsonDocument jsonDocument;
DeserializationError error = deserializeJson(jsonDocument, file);
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
JsonObject node = nodes.add<JsonObject>();
node["type"] = "customSupport";
node["data"] = jsonDocument.as<JsonObject>();
file.close(); // close early, we no longer need the file
if (!error && jsonDocument.is<JsonObject>()) {
JsonObject support_node = nodes.add<JsonObject>();
support_node["type"] = "customSupport";
support_node["data"].set(jsonDocument.as<JsonObject>());
LOG_DEBUG("Exported custom support file %s", EMSESP_CUSTOMSUPPORT_FILE);
} else {
LOG_ERROR("Failed to deserialize custom support file");
}
file.close();
LOG_DEBUG("Exported custom support file %s", EMSESP_CUSTOMSUPPORT_FILE);
}
// Backup NVS values
node = nodes.add<JsonObject>();
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;
#if ESP_IDF_VERSION_MAJOR < 5
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
// 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)) {
std::string filename = version;
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 minor_version = filename.substr(underscore1 + 1, underscore2 - underscore1 - 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 {
// if it's .json file exit
if (version.find(".json") != std::string::npos) {
return 0;
} else {
// 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()) {
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
bool WebStatusService::checkUpgrade(JsonObject root, std::string & version) {
if (!version.empty()) {
version::Semver200_version current_version(current_version_s);
version::Semver200_version latest_dev_version(version.substr(0, version.find(',')));
version::Semver200_version latest_stable_version(version.substr(version.find(',') + 1));
version::EMSESP_Version current_version(current_version_s);
version::EMSESP_Version latest_dev_version(version.substr(0, version.find(',')));
version::EMSESP_Version latest_stable_version(version.substr(version.find(',') + 1));
bool dev_upgradeable = latest_dev_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_ACTION_SERVICE_PATH "/rest/action"
#include <semver200.h> // for version checking
#include "../core/EMSESP_Version.h"
#include "../emsesp_version.h"
namespace emsesp {