add version comparison function and check for outdated versions before updates
This commit is contained in:
		| @@ -10,6 +10,8 @@ | |||||||
|  |  | ||||||
| [common] | [common] | ||||||
| version = "1.3.99" | version = "1.3.99" | ||||||
|  | to_old_version = "1.4.0" | ||||||
|  |  | ||||||
| ## | ## | ||||||
| [env:esp32dev] | [env:esp32dev] | ||||||
| platform = espressif32 | platform = espressif32 | ||||||
| @@ -48,6 +50,7 @@ build_flags = | |||||||
|     #-DNDEBUG |     #-DNDEBUG | ||||||
|     -mtext-section-literals |     -mtext-section-literals | ||||||
|     -DVERSION=\"${common.version}\" |     -DVERSION=\"${common.version}\" | ||||||
|  |     -DTOOLDVERSION=\"${common.to_old_version}\" | ||||||
|     -DASYNCWEBSERVER_REGEX |     -DASYNCWEBSERVER_REGEX | ||||||
|     #-DCORE_DEBUG_LEVEL=3 |     #-DCORE_DEBUG_LEVEL=3 | ||||||
|     -DCONFIG_ARDUHAL_LOG_COLORS=1 |     -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 size_t updateWritten = 0; | ||||||
| static bool isSpiffsUpdate = false; | 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() { | void backupJsonConfigs() { | ||||||
|     // Bambu Credentials backup |     // Bambu Credentials backup | ||||||
| @@ -111,6 +139,16 @@ void handleUpdate(AsyncWebServer &server) { | |||||||
|     updateHandler->setUri("/update"); |     updateHandler->setUri("/update"); | ||||||
|     updateHandler->setMethod(HTTP_POST); |     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, |     updateHandler->onUpload([](AsyncWebServerRequest *request, String filename, | ||||||
|                              size_t index, uint8_t *data, size_t len, bool final) { |                              size_t index, uint8_t *data, size_t len, bool final) { | ||||||
|         if (!index) { |         if (!index) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user