Refactor semantic version comparison for OTA updates (#216)
## Summary * **What is the goal of this PR?** (e.g., Fixes a bug in the user authentication module, Implements the new feature for file uploading.) This PR refactors the semantic version comparison logic used during OTA update checks. Memory stats before : RAM: [=== ] 30.8% (used 101068 bytes from 327680 bytes) Flash: [========= ] 85.7% (used **5617830** bytes from 6553600 bytes) Memory stats before : RAM: [=== ] 30.8% (used 101068 bytes from 327680 bytes) Flash: [========= ] 85.7% (used **5616870** bytes from 6553600 bytes) * **What changes are included?** Replaced std::string::substr() and std::stoi() based parsing with a lightweight, heap-free approach. Version parsing is now done in a single pass without creating temporary std::string objects. Behavior remains identical: versions are still compared as MAJOR.MINOR.PATCH. ## Additional Context `std::string::substr() ` creates a new string and performs heap allocation * Add any other information that might be helpful for the reviewer (e.g., performance implications, potential risks, specific areas to focus on).
This commit is contained in:
@@ -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", ¤tMajor, ¤tMinor, ¤tPatch);
|
||||||
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()) {
|
||||||
|
|||||||
@@ -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);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user