From c7f26bee32432b21800595a01ff1e86fd86a0026 Mon Sep 17 00:00:00 2001
From: Marcel Peterkau <marcel@peterkau.de>
Date: Mon, 22 Aug 2022 14:49:14 +0200
Subject: [PATCH] made EEPROM-Format on WebUI more "secure"

---
 Software/data/index.htm |  8 ++++----
 Software/src/config.cpp | 21 +++++++++++++++------
 Software/src/globals.h  |  1 +
 Software/src/webui.cpp  | 20 +++++++++++++-------
 4 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/Software/data/index.htm b/Software/data/index.htm
index d9bac0a..c416523 100644
--- a/Software/data/index.htm
+++ b/Software/data/index.htm
@@ -312,14 +312,14 @@
           <div class="form-group">
             <div class="col-xs-offset-5 col-xs-7">
               <div class="form-check">
-                <input class="form-check-input" type="checkbox" value="" id="reset_ee_cfg_check">
-                <label class="form-check-label" for="reset_ee_cfg_check">
+                <input class="form-check-input" type="checkbox" value="" id="reset_ee_cfg">
+                <label class="form-check-label" for="reset_ee_cfg">
                   JA, EEPROM-Bereich "CFG" formatieren und Konfiguration zurück setzen
                 </label>
               </div>
               <div class="form-check">
-                <input class="form-check-input" type="checkbox" value="" id="reset_ee_pds_check">
-                <label class="form-check-label" for="reset_ee_pds_check">
+                <input class="form-check-input" type="checkbox" value="" id="reset_ee_pds">
+                <label class="form-check-label" for="reset_ee_pds">
                   JA, EEPROM-Bereich "PDS" formatieren und Betriebsdaten zurück setzen
                 </label>
               </div>
diff --git a/Software/src/config.cpp b/Software/src/config.cpp
index ce62867..c3af149 100644
--- a/Software/src/config.cpp
+++ b/Software/src/config.cpp
@@ -15,15 +15,11 @@ boolean checkEEPROMavailable();
 void InitEEPROM()
 {
   ee.begin();
-  if (!checkEEPROMavailable())
-  {
-    return;
-  }
+  checkEEPROMavailable();
 }
 
 void EEPROM_Process()
 {
-
   switch (globals.requestEEAction)
   {
   case EE_CFG_SAVE:
@@ -34,6 +30,11 @@ void EEPROM_Process()
     GetConfig_EEPROM();
     globals.requestEEAction = EE_IDLE;
     break;
+  case EE_CFG_FORMAT:
+    FormatConfig_EEPROM();
+    globals.requestEEAction = EE_IDLE;
+    globals.systemStatus = sysStat_Shutdown;
+    break;
   case EE_PDS_SAVE:
     StorePersistence_EEPROM();
     globals.requestEEAction = EE_IDLE;
@@ -42,6 +43,15 @@ void EEPROM_Process()
     GetPersistence_EEPROM();
     globals.requestEEAction = EE_IDLE;
     break;
+  case EE_PDS_FORMAT:
+    FormatPersistence_EEPROM();
+    globals.requestEEAction = EE_IDLE;
+    break;
+  case EE_FORMAT_ALL:
+    FormatConfig_EEPROM();
+    FormatPersistence_EEPROM();
+    globals.requestEEAction = EE_IDLE;
+    break;
   case EE_ALL_SAVE:
     StorePersistence_EEPROM();
     StoreConfig_EEPROM();
@@ -76,7 +86,6 @@ void GetConfig_EEPROM()
   if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) != checksum)
   {
     MaintainDTC(DTC_EEPROM_CFG_BAD, DTC_CRITICAL, true);
-    FormatConfig_EEPROM();
   }
   LubeConfig.checksum = checksum;
 }
diff --git a/Software/src/globals.h b/Software/src/globals.h
index 03323c6..bba362f 100644
--- a/Software/src/globals.h
+++ b/Software/src/globals.h
@@ -22,6 +22,7 @@ typedef enum eEERequest
   EE_PDS_SAVE,
   EE_PDS_LOAD,
   EE_PDS_FORMAT,
+  EE_FORMAT_ALL,
   EE_ALL_SAVE
 
 } tEERequest;
diff --git a/Software/src/webui.cpp b/Software/src/webui.cpp
index 9ea05a5..529b7cd 100644
--- a/Software/src/webui.cpp
+++ b/Software/src/webui.cpp
@@ -308,15 +308,21 @@ void WebserverPOST_Callback(AsyncWebServerRequest *request)
       PersistenceData.tankRemain_µl = LubeConfig.tankCapacity_ml * 1000;
       globals.requestEEAction = EE_PDS_SAVE;
     }
-    if (p->name() == "reset_ee_cfg")
+    if (p->name() == "reset_ee_btn")
     {
-      globals.requestEEAction = EE_CFG_FORMAT;
+      if (request->hasParam("reset_ee_pds"))
+      {
+        AsyncWebParameter *param = request->getParam("reset_ee_pds");
+        if (param->value() == "checked")
+          globals.requestEEAction = globals.requestEEAction == EE_CFG_FORMAT ? EE_FORMAT_ALL : EE_PDS_FORMAT;
+      }
+      if (request->hasParam("reset_ee_cfg"))
+      {
+        AsyncWebParameter *param = request->getParam("reset_ee_cfg");
+        if (param->value() == "checked")
+          globals.requestEEAction = globals.requestEEAction == EE_PDS_FORMAT ? EE_FORMAT_ALL : EE_CFG_FORMAT;
+      }
     }
-    if (p->name() == "reset_ee_pds")
-    {
-      globals.requestEEAction = EE_PDS_FORMAT;
-    }
-   
     // end: POST Form Maintenance
   }
 }