Refactor semantic version comparison for OTA updates

This commit is contained in:
embedded4ever 2026-01-03 00:19:06 +00:00
parent 062d69dc2a
commit 6da03de54c
2 changed files with 27 additions and 31 deletions

View File

@ -3,7 +3,6 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include <Update.h> #include <Update.h>
#include <WiFiClientSecure.h>
namespace { namespace {
constexpr char latestReleaseUrl[] = "https://api.github.com/repos/daveallie/crosspoint-reader/releases/latest"; constexpr char latestReleaseUrl[] = "https://api.github.com/repos/daveallie/crosspoint-reader/releases/latest";
@ -69,44 +68,41 @@ OtaUpdater::OtaUpdaterError OtaUpdater::checkForUpdate() {
return OK; return OK;
} }
bool OtaUpdater::isUpdateNewer() { bool OtaUpdater::isUpdateNewer() const {
if (!updateAvailable || latestVersion.empty() || latestVersion == CROSSPOINT_VERSION) { if (!updateAvailable || latestVersion.empty() || latestVersion == CROSSPOINT_VERSION) {
return false; return false;
} }
int currentMajor, currentMinor, currentPatch;
int latestMajor, latestMinor, latestPatch;
const auto currentVersion = CROSSPOINT_VERSION;
// semantic version check (only match on 3 segments) // semantic version check (only match on 3 segments)
const auto updateMajor = stoi(latestVersion.substr(0, latestVersion.find('.'))); sscanf(latestVersion.c_str(), "%d.%d.%d", &latestMajor, &latestMinor, &latestPatch);
const auto updateMinor = stoi( sscanf(currentVersion, "%d.%d.%d", &currentMajor, &currentMinor, &currentPatch);
latestVersion.substr(latestVersion.find('.') + 1, latestVersion.find_last_of('.') - latestVersion.find('.') - 1));
const auto updatePatch = stoi(latestVersion.substr(latestVersion.find_last_of('.') + 1));
std::string currentVersion = CROSSPOINT_VERSION; /*
const auto currentMajor = stoi(currentVersion.substr(0, currentVersion.find('.'))); * Compare major versions.
const auto currentMinor = stoi(currentVersion.substr( * If they differ, return true if latest major version greater than current major version
currentVersion.find('.') + 1, currentVersion.find_last_of('.') - currentVersion.find('.') - 1)); * otherwise return false.
const auto currentPatch = stoi(currentVersion.substr(currentVersion.find_last_of('.') + 1)); */
if (latestMajor != currentMajor) return latestMajor > currentMajor;
if (updateMajor > currentMajor) { /*
return true; * Compare minor versions.
} * If they differ, return true if latest minor version greater than current minor version
if (updateMajor < currentMajor) { * otherwise return false.
return false; */
} if (latestMinor != currentMinor) return latestMinor > currentMinor;
if (updateMinor > currentMinor) { /*
return true; * Check patch versions.
} */
if (updateMinor < currentMinor) { return latestPatch > currentPatch;
return false;
}
if (updatePatch > currentPatch) {
return true;
}
return false;
} }
const std::string& OtaUpdater::getLatestVersion() { return latestVersion; } const std::string& OtaUpdater::getLatestVersion() const { return latestVersion; }
OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate(const std::function<void(size_t, size_t)>& onProgress) { OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate(const std::function<void(size_t, size_t)>& onProgress) {
if (!isUpdateNewer()) { if (!isUpdateNewer()) {

View File

@ -23,8 +23,8 @@ class OtaUpdater {
size_t totalSize = 0; size_t totalSize = 0;
OtaUpdater() = default; OtaUpdater() = default;
bool isUpdateNewer(); bool isUpdateNewer() const;
const std::string& getLatestVersion(); const std::string& getLatestVersion() const;
OtaUpdaterError checkForUpdate(); OtaUpdaterError checkForUpdate();
OtaUpdaterError installUpdate(const std::function<void(size_t, size_t)>& onProgress); OtaUpdaterError installUpdate(const std::function<void(size_t, size_t)>& onProgress);
}; };