From 6ffe239cae4b02199e5874401a08de69e194a1ae Mon Sep 17 00:00:00 2001
From: Marcel Peterkau <marcel@peterkau.de>
Date: Mon, 22 Aug 2022 14:26:37 +0200
Subject: [PATCH] Added DTC for LittleFS-Image Version mismatch

---
 Software/src/common.h  |  2 ++
 Software/src/dtc.h     |  1 +
 Software/src/globals.h |  5 ++++-
 Software/src/webui.cpp | 34 ++++++++++++++++++++--------------
 4 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/Software/src/common.h b/Software/src/common.h
index ca769fe..822f39d 100644
--- a/Software/src/common.h
+++ b/Software/src/common.h
@@ -29,6 +29,8 @@
 #define SW_VERSION_MAJOR 1
 #define SW_VERSION_MINOR 1
 
+#define FLASH_FS_VERSION 1.1
+
 #ifndef OTA_DELAY
 #define OTA_DELAY 50 // ticks -> 10ms / tick
 #endif
diff --git a/Software/src/dtc.h b/Software/src/dtc.h
index a3de2a0..f00fe55 100644
--- a/Software/src/dtc.h
+++ b/Software/src/dtc.h
@@ -15,6 +15,7 @@ typedef enum DTCNums_e
   DTC_EEPROM_PDSADRESS_BAD,
   DTC_EEPROM_VERSION_BAD,
   DTC_FLASHFS_ERROR,
+  DTC_FLASHFS_VERSION_ERROR,
 #ifdef FEATURE_ENABLE_GPS
   DTC_NO_GPS_SERIAL,
 #endif
diff --git a/Software/src/globals.h b/Software/src/globals.h
index a131760..03323c6 100644
--- a/Software/src/globals.h
+++ b/Software/src/globals.h
@@ -18,9 +18,12 @@ typedef enum eEERequest
   EE_IDLE,
   EE_CFG_SAVE,
   EE_CFG_LOAD,
+  EE_CFG_FORMAT,
   EE_PDS_SAVE,
   EE_PDS_LOAD,
+  EE_PDS_FORMAT,
   EE_ALL_SAVE
+
 } tEERequest;
 
 typedef struct Globals_s
@@ -31,7 +34,7 @@ typedef struct Globals_s
   uint16_t purgePulses = 0;
   eEERequest requestEEAction = EE_IDLE;
   char DeviceName[33];
-  uint32_t FlashVersion;
+  char FlashVersion[5];
   uint16_t eePersistanceAdress;
   uint8_t TankPercentage;
   bool hasDTC;
diff --git a/Software/src/webui.cpp b/Software/src/webui.cpp
index e18dcd3..d6a4ab5 100644
--- a/Software/src/webui.cpp
+++ b/Software/src/webui.cpp
@@ -20,7 +20,12 @@ void initWebUI()
     return;
   }
 
-  globals.FlashVersion = GetFlashVersion();
+  GetFlashVersion(globals.FlashVersion, sizeof(globals.FlashVersion));
+
+  if (!strcmp(globals.FlashVersion, QUOTE(FLASH_FS_VERSION))
+  {
+    MaintainDTC(DTC_FLASHFS_VERSION_ERROR, DTC_WARN, true);
+  }
 
   MDNS.begin(globals.DeviceName);
   MDNS.addService("telnet", "tcp", 23);
@@ -312,20 +317,21 @@ void WebserverNotFound_Callback(AsyncWebServerRequest *request)
   request->send(404, "text/html", "Not found");
 }
 
-uint32_t GetFlashVersion()
+void GetFlashVersion(char *buff, size_t buff_size)
 {
-    char buffer[20];
-    File this_file = LittleFS.open("version", "r");
-    if (!this_file)
-    { // failed to open the file, retrn empty result
-      return 0;
-    }
-    if (this_file.available())
-    {
-      this_file.readBytesUntil('\r', buffer, sizeof(buffer));
-    }
-    this_file.close(); 
-  return atoi(buffer);
+  File this_file = LittleFS.open("version", "r");
+  if (!this_file)
+  { // failed to open the file, retrn empty result
+    buff[0] = '\0';
+    return;
+  }
+  if (this_file.available())
+  {
+    int bytes_read;
+    bytes_read = this_file.readBytesUntil('\r', buff, buff_size-1);
+    buff[bytes_read] = '\0';
+  }
+  this_file.close();
 }
 
 void WebserverFirmwareUpdate_Callback(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)