add version comparison function and check for outdated versions before updates
This commit is contained in:
parent
bec2c91331
commit
eb2a8dc128
@ -10,6 +10,8 @@
|
||||
|
||||
[common]
|
||||
version = "1.3.99"
|
||||
to_old_version = "1.4.0"
|
||||
|
||||
##
|
||||
[env:esp32dev]
|
||||
platform = espressif32
|
||||
@ -48,6 +50,7 @@ build_flags =
|
||||
#-DNDEBUG
|
||||
-mtext-section-literals
|
||||
-DVERSION=\"${common.version}\"
|
||||
-DTOOLDVERSION=\"${common.to_old_version}\"
|
||||
-DASYNCWEBSERVER_REGEX
|
||||
#-DCORE_DEBUG_LEVEL=3
|
||||
-DCONFIG_ARDUHAL_LOG_COLORS=1
|
||||
|
38
src/ota.cpp
38
src/ota.cpp
@ -14,6 +14,34 @@ static size_t updateTotalSize = 0;
|
||||
static size_t updateWritten = 0;
|
||||
static bool isSpiffsUpdate = false;
|
||||
|
||||
/**
|
||||
* Compares two version strings and determines if version1 is less than version2
|
||||
*
|
||||
* @param version1 First version string (format: x.y.z)
|
||||
* @param version2 Second version string (format: x.y.z)
|
||||
* @return true if version1 is less than version2
|
||||
*/
|
||||
bool isVersionLessThan(const String& version1, const String& version2) {
|
||||
int major1 = 0, minor1 = 0, patch1 = 0;
|
||||
int major2 = 0, minor2 = 0, patch2 = 0;
|
||||
|
||||
// Parse version1
|
||||
sscanf(version1.c_str(), "%d.%d.%d", &major1, &minor1, &patch1);
|
||||
|
||||
// Parse version2
|
||||
sscanf(version2.c_str(), "%d.%d.%d", &major2, &minor2, &patch2);
|
||||
|
||||
// Compare major version
|
||||
if (major1 < major2) return true;
|
||||
if (major1 > major2) return false;
|
||||
|
||||
// Major versions equal, compare minor
|
||||
if (minor1 < minor2) return true;
|
||||
if (minor1 > minor2) return false;
|
||||
|
||||
// Minor versions equal, compare patch
|
||||
return patch1 < patch2;
|
||||
}
|
||||
|
||||
void backupJsonConfigs() {
|
||||
// Bambu Credentials backup
|
||||
@ -111,6 +139,16 @@ void handleUpdate(AsyncWebServer &server) {
|
||||
updateHandler->setUri("/update");
|
||||
updateHandler->setMethod(HTTP_POST);
|
||||
|
||||
// Check if current version is less than defined TOOLVERSION before proceeding with update
|
||||
if (isVersionLessThan(VERSION, TOOLDVERSION)) {
|
||||
updateHandler->onRequest([](AsyncWebServerRequest *request) {
|
||||
request->send(400, "application/json",
|
||||
"{\"success\":false,\"message\":\"Your current version is too old. Please perform a full upgrade.\"}");
|
||||
});
|
||||
server.addHandler(updateHandler);
|
||||
return;
|
||||
}
|
||||
|
||||
updateHandler->onUpload([](AsyncWebServerRequest *request, String filename,
|
||||
size_t index, uint8_t *data, size_t len, bool final) {
|
||||
if (!index) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user