From e0b9568e56c7debd4164b7c338b2581c1a866ae5 Mon Sep 17 00:00:00 2001
From: jazzpi <jasper@mezzo.de>
Date: Sat, 30 Jul 2022 22:23:05 +0200
Subject: [PATCH] Check number of broken temperature sensors

---
 Core/Inc/AMS_Errorcodes.h   |  1 +
 Core/Inc/Slave_Monitoring.h |  2 ++
 Core/Src/Slave_Monitoring.c | 14 ++++++++++++++
 3 files changed, 17 insertions(+)

diff --git a/Core/Inc/AMS_Errorcodes.h b/Core/Inc/AMS_Errorcodes.h
index c96572f..63b3bb5 100644
--- a/Core/Inc/AMS_Errorcodes.h
+++ b/Core/Inc/AMS_Errorcodes.h
@@ -13,5 +13,6 @@
 #define SlavesTimeoutError 1
 #define SlavesErrorFrameError 2
 #define SLAVES_FRAME_TIMEOUT_ERROR 3
+#define SLAVES_TOO_FEW_TEMPS 4
 
 #endif /* INC_AMS_ERRORCODES_H_ */
diff --git a/Core/Inc/Slave_Monitoring.h b/Core/Inc/Slave_Monitoring.h
index a64437f..bc0524c 100644
--- a/Core/Inc/Slave_Monitoring.h
+++ b/Core/Inc/Slave_Monitoring.h
@@ -20,6 +20,8 @@
 
 #define SLAVETIMEOUT 500
 #define SLAVE_HEARTBEAT_FRAMES 11
+// 30% * 90 = 27, each sensor measures 2 cells
+#define SLAVE_MIN_TEMP_SENSORS 14
 
 typedef struct {
 
diff --git a/Core/Src/Slave_Monitoring.c b/Core/Src/Slave_Monitoring.c
index 6b8b7be..d261874 100644
--- a/Core/Src/Slave_Monitoring.c
+++ b/Core/Src/Slave_Monitoring.c
@@ -78,6 +78,20 @@ uint8_t checkSlaveTimeout() {
         return 1;
       }
     }
+
+    int working_cell_temps = 0;
+    for (int i = 0; i < NUMBEROFTEMPS; i++) {
+      if (slaves[n].cellTemps[i] != 0) {
+        working_cell_temps++;
+      }
+    }
+    if (working_cell_temps < SLAVE_MIN_TEMP_SENSORS) {
+      AMSErrorHandle temperror;
+      temperror.errorcode = SLAVES_TOO_FEW_TEMPS;
+      temperror.errorarg[0] = n;
+      AMS_Error_Handler(&temperror);
+      return 1;
+    }
   }
   return 0;
 }