mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 10:49:42 +02:00
Safer array access
This commit is contained in:
parent
c7f4408cef
commit
6cdcb89a83
1 changed files with 20 additions and 10 deletions
|
@ -2,6 +2,14 @@
|
|||
#include "../include.h"
|
||||
#include "BMW-I3-BATTERY.h"
|
||||
|
||||
// Helper function for safe array access
|
||||
static const char* safeArrayAccess(const char* const arr[], size_t arrSize, int idx) {
|
||||
if (idx >= 0 && static_cast<size_t>(idx) < arrSize && arr[idx][0] != '\0') {
|
||||
return arr[idx];
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
String BmwI3HtmlRenderer::get_status_html() {
|
||||
String content;
|
||||
|
||||
|
@ -10,12 +18,12 @@ String BmwI3HtmlRenderer::get_status_html() {
|
|||
content += "<h4>SOC OBD2: " + String(batt.SOC_OBD2()) + "</h4>";
|
||||
static const char* statusText[16] = {
|
||||
"Not evaluated", "OK", "Error!", "Invalid signal", "", "", "", "", "", "", "", "", "", "", "", ""};
|
||||
content += "<h4>Interlock: " + String(statusText[batt.ST_interlock()]) + "</h4>";
|
||||
content += "<h4>Isolation external: " + String(statusText[batt.ST_iso_ext()]) + "</h4>";
|
||||
content += "<h4>Isolation internal: " + String(statusText[batt.ST_iso_int()]) + "</h4>";
|
||||
content += "<h4>Isolation: " + String(statusText[batt.ST_isolation()]) + "</h4>";
|
||||
content += "<h4>Cooling valve: " + String(statusText[batt.ST_valve_cooling()]) + "</h4>";
|
||||
content += "<h4>Emergency: " + String(statusText[batt.ST_EMG()]) + "</h4>";
|
||||
content += "<h4>Interlock: " + String(safeArrayAccess(statusText, 16, batt.ST_interlock())) + "</h4>";
|
||||
content += "<h4>Isolation external: " + String(safeArrayAccess(statusText, 16, batt.ST_iso_ext())) + "</h4>";
|
||||
content += "<h4>Isolation internal: " + String(safeArrayAccess(statusText, 16, batt.ST_iso_int())) + "</h4>";
|
||||
content += "<h4>Isolation: " + String(safeArrayAccess(statusText, 16, batt.ST_isolation())) + "</h4>";
|
||||
content += "<h4>Cooling valve: " + String(safeArrayAccess(statusText, 16, batt.ST_valve_cooling())) + "</h4>";
|
||||
content += "<h4>Emergency: " + String(safeArrayAccess(statusText, 16, batt.ST_EMG())) + "</h4>";
|
||||
static const char* prechargeText[16] = {"Not evaluated",
|
||||
"Not active, closing not blocked",
|
||||
"Error precharge blocked",
|
||||
|
@ -32,7 +40,8 @@ String BmwI3HtmlRenderer::get_status_html() {
|
|||
"",
|
||||
"",
|
||||
""};
|
||||
content += "<h4>Precharge: " + String(prechargeText[batt.ST_precharge()]) + "</h4>"; //Still unclear of enum
|
||||
content += "<h4>Precharge: " + String(safeArrayAccess(prechargeText, 16, batt.ST_precharge())) +
|
||||
"</h4>"; //Still unclear of enum
|
||||
static const char* DCSWText[16] = {"Contactors open",
|
||||
"Precharge ongoing",
|
||||
"Contactors engaged",
|
||||
|
@ -49,7 +58,7 @@ String BmwI3HtmlRenderer::get_status_html() {
|
|||
"",
|
||||
"",
|
||||
""};
|
||||
content += "<h4>Contactor status: " + String(DCSWText[batt.ST_DCSW()]) + "</h4>";
|
||||
content += "<h4>Contactor status: " + String(safeArrayAccess(DCSWText, 16, batt.ST_DCSW())) + "</h4>";
|
||||
static const char* contText[16] = {"Contactors OK",
|
||||
"One contactor welded!",
|
||||
"Two contactors welded!",
|
||||
|
@ -66,7 +75,7 @@ String BmwI3HtmlRenderer::get_status_html() {
|
|||
"",
|
||||
"",
|
||||
""};
|
||||
content += "<h4>Contactor weld: " + String(contText[batt.ST_WELD()]) + "</h4>";
|
||||
content += "<h4>Contactor weld: " + String(safeArrayAccess(contText, 16, batt.ST_WELD())) + "</h4>";
|
||||
static const char* valveText[16] = {"OK",
|
||||
"Short circuit to GND",
|
||||
"Short circuit to 12V",
|
||||
|
@ -83,7 +92,8 @@ String BmwI3HtmlRenderer::get_status_html() {
|
|||
"Stuck",
|
||||
"",
|
||||
"Invalid Signal"};
|
||||
content += "<h4>Cold shutoff valve: " + String(contText[batt.ST_cold_shutoff_valve()]) + "</h4>";
|
||||
content +=
|
||||
"<h4>Cold shutoff valve: " + String(safeArrayAccess(valveText, 16, batt.ST_cold_shutoff_valve())) + "</h4>";
|
||||
|
||||
return content;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue