Updated Debugging-Stuff and WebUI Backend

This commit is contained in:
Marcel Peterkau 2023-04-13 00:35:24 +02:00
parent 4507c80eba
commit aeab80d5a8
11 changed files with 489 additions and 46 deletions

View File

@ -40,28 +40,10 @@
#define I2C_POWER_ADDRESS 0x40
#define I2C_EEPROM_ADDRESS 0x50
#define SW_VERSION 1.0
#define FLASH_FS_VERSION 1.0
#ifndef OTA_DELAY
#define OTA_DELAY 50 // ticks -> 10ms / tick
#endif
#ifndef ADMIN_PASSWORD
#error "You need to define ADMIN_PASSWORD for OTA-Update"
#endif
#ifndef WIFI_PASSWORD
#error "You must define an WIFI_PASSWORD for OTA-Update"
#endif
#ifndef WIFI_SSID
#error "You must define an WIFI_SSID for OTA-Update"
#endif
#ifndef WIFI_AP_SSID
#warning "No WIFI_AP_SSID defined. Using DeviceName"
#define WIFI_AP_SSID DEVICE_NAME
#endif
#ifndef WIFI_AP_PASSWORD
#error "You must define an WIFI_AP_PASSWORD for Standalone AP-Mode"
#endif
#endif

View File

@ -43,6 +43,8 @@ const char BatteryString[][10]{
"LiPo 3S"
};
const size_t BatteryString_Elements = sizeof(BatteryString) / sizeof(BatteryString[0]);
typedef struct
{
uint8_t EEPROM_Version = 1;

View File

@ -0,0 +1,46 @@
#ifndef _DEBUGGER_H_
#define _DEBUGGER_H_
#include <Arduino.h>
#include "webui.h"
const char PROGMEM helpCmd[] = "sysinfo - System Info\n"
"netinfo - WiFi Info\n"
"formatPDS - Format Persistence EEPROM Data\n"
"formatCFG - Format Configuration EEPROM Data\n"
"checkEE - Check EEPROM with checksum\n"
"dumpEE1k - dump the first 1kb of EEPROM to Serial\n"
"dumpEE - dump the whole EPPROM to Serial\n"
"resetPageEE - Reset the PersistenceData Page\n"
"dumpCFG - print Config struct\n"
"dumpPDS - print PersistanceStruct\n"
"saveEE - save EE-Data\n"
"showdtc - Show all DTCs\n"
"dumpGlobals - print globals\n";
typedef enum DebugStatus_e
{
disabled,
enabled
} DebugStatus_t;
typedef enum DebugPorts_e
{
dbg_Serial,
dbg_Webui,
dbg_cntElements
} DebugPorts_t;
const char sDebugPorts[dbg_cntElements][7] = {
"Serial",
"WebUI"};
extern DebugStatus_t DebuggerStatus[dbg_cntElements];
void initDebugger();
void pushCANDebug(uint32_t id, uint8_t dlc, uint8_t *data);
void Debug_pushMessage(const char *format, ...);
void SetDebugportStatus(DebugPorts_t port, DebugStatus_t status);
void Debug_Process();
#endif

View File

@ -17,14 +17,16 @@ typedef enum DTCNums_e
DTC_EEPROM_CFG_SANITY,
DTC_NO_LORA_FOUND,
DTC_NO_BATMNON_FOUND,
DTC_BAT_LOW,
DTC_BAT_CRITICAL,
DTC_LAST_DTC
} DTCNums_t;
typedef enum DTCActive_e
{
DTC_NONE,
DTC_ACTIVE,
DTC_PREVIOUS,
DTC_NONE
DTC_PREVIOUS
} DTCActive_t;
typedef enum DTCSeverity_e
@ -48,6 +50,7 @@ void ClearDTC(DTCNums_t DTC_no);
void ClearAllDTC();
DTCNums_t getlastDTC(boolean only_active);
DTCNums_t getlastDTC_Severity(boolean only_active, DTCSeverity_t severity);
void DTC_Process();
extern DTCEntry_s DTCStorage[MAX_DTC_STORAGE];
#endif

View File

@ -41,5 +41,21 @@ typedef struct Globals_s
} Globals_t;
extern Globals_t globals;
typedef struct Constants_s
{
uint8_t FW_Version_major;
uint8_t FW_Version_minor;
uint8_t Required_Flash_Version_major;
uint8_t Required_Flash_Version_minor;
char GitHash[11];
} Constants_t;
const Constants_t constants PROGMEM = {
1,1, // Firmware_Version
1,1, // Required Flash Version
GIT_REV // Git-Hash-String
};
void initGlobals();
#endif

View File

@ -0,0 +1,21 @@
#ifndef _SANITYCHECK_H_
#define _SANITYCHECK_H_
#ifndef ADMIN_PASSWORD
#error "You need to define ADMIN_PASSWORD for OTA-Update"
#endif
#ifndef WIFI_PASSWORD
#error "You must define an WIFI_PASSWORD for OTA-Update"
#endif
#ifndef WIFI_SSID
#error "You must define an WIFI_SSID for OTA-Update"
#endif
#ifndef WIFI_AP_SSID
#warning "No WIFI_AP_SSID defined. Using DeviceName"
#define WIFI_AP_SSID DEVICE_NAME
#endif
#ifndef WIFI_AP_PASSWORD
#error "You must define an WIFI_AP_PASSWORD for Standalone AP-Mode"
#endif
#endif //_SANITYCHECK_H_

View File

@ -15,7 +15,10 @@
#include "globals.h"
#include "dtc.h"
#include "common.h"
#include "debugger.h"
void initWebUI();
void Webserver_Process();
void Websocket_PushLiveDebug(String Message);
#endif

330
Software/src/debugger.cpp Normal file
View File

@ -0,0 +1,330 @@
#include "debugger.h"
DebugStatus_t DebuggerStatus[dbg_cntElements];
String IpAddress2String(const IPAddress &ipAddress);
void processCmdDebug(String command);
void Debug_formatCFG();
void Debug_formatPersistence();
void Debug_printSystemInfo();
void Debug_printWifiInfo();
void Debug_CheckEEPOM();
void Debug_dumpConfig();
void Debug_dumpPersistance();
void Debug_ShowDTCs();
void Debug_dumpGlobals();
void Debug_printHelp();
void initDebugger()
{
DebuggerStatus[dbg_Serial] = disabled;
DebuggerStatus[dbg_Webui] = disabled;
Serial.setDebugOutput(false);
}
void Debug_Process()
{
typedef enum InputProcessed_e
{
IDLE,
CMD_COMPLETE,
CMD_ABORT,
CMD_OVERFLOW
} InputProcessed_t;
static int inputCnt = 0;
static char inputBuffer[32];
InputProcessed_t InputProcessed = IDLE;
if (Serial.available())
{
char inputChar = Serial.read();
switch (inputChar)
{
case '\n':
inputBuffer[inputCnt] = 0; // terminate the String
inputCnt = 0;
InputProcessed = CMD_COMPLETE;
break;
case 0x1B: // Esc
inputBuffer[0] = 0;
inputCnt = 0;
InputProcessed = CMD_ABORT;
break;
case 0x21 ... 0x7E: // its a real letter or sign and not some control-chars
inputBuffer[inputCnt] = inputChar;
inputCnt++;
break;
default:
break;
}
if (inputCnt > sizeof(inputBuffer))
{
inputCnt = 0;
inputBuffer[sizeof(inputBuffer) - 1] = 0; // terminate the String
InputProcessed = CMD_OVERFLOW;
}
}
switch (InputProcessed)
{
case CMD_ABORT:
Debug_pushMessage("Abort\n");
break;
case CMD_COMPLETE:
processCmdDebug(String(inputBuffer));
break;
case CMD_OVERFLOW:
Debug_pushMessage("input Buffer overflow\n");
break;
default:
break;
}
InputProcessed = IDLE;
}
void SetDebugportStatus(DebugPorts_t port, DebugStatus_t status)
{
if (status == disabled)
Debug_pushMessage("disable DebugPort %s\n", sDebugPorts[port]);
DebuggerStatus[port] = status;
if (status == enabled)
Debug_pushMessage("enabled DebugPort %s\n", sDebugPorts[port]);
}
void Debug_pushMessage(const char *format, ...)
{
if ((DebuggerStatus[dbg_Serial] == enabled) || (DebuggerStatus[dbg_Webui] == enabled))
{
char buff[64];
va_list arg;
va_start(arg, format);
vsnprintf(buff, sizeof(buff), format, arg);
va_end(arg);
if (DebuggerStatus[dbg_Serial] == enabled)
{
Serial.print(buff);
}
if (DebuggerStatus[dbg_Webui] == enabled)
{
Websocket_PushLiveDebug(String(buff));
}
}
}
void pushCANDebug(uint32_t id, uint8_t dlc, uint8_t *data)
{
if ((DebuggerStatus[dbg_Serial] == enabled) || (DebuggerStatus[dbg_Webui] == enabled))
{
char buff[100];
char *p = buff;
p += snprintf(p, sizeof(buff), "CAN: 0x%08X | %d | ", id, dlc);
for (int i = 0; i < dlc; i++)
{
p += snprintf(p, sizeof(buff) - (p - buff), "%02X ", data[i]);
}
*(p++) = '\n';
*p = '\0';
if (DebuggerStatus[dbg_Serial] == enabled)
{
Serial.print(buff);
}
if (DebuggerStatus[dbg_Webui] == enabled)
{
Websocket_PushLiveDebug(String(buff));
}
}
}
void processCmdDebug(String command)
{
if (command == "help")
Debug_printHelp();
else if (command == "sysinfo")
Debug_printSystemInfo();
else if (command == "netinfo")
Debug_printWifiInfo();
else if (command == "formatCFG")
Debug_formatCFG();
else if (command == "formatPDS")
Debug_formatPersistence();
else if (command == "checkEE")
Debug_CheckEEPOM();
else if (command == "dumpEE1k")
dumpEEPROM(0, 1024);
else if (command == "dumpEE")
dumpEEPROM(0, EEPROM_SIZE_BYTES);
else if (command == "resetPageEE")
MovePersistencePage_EEPROM(true);
else if (command == "dumpCFG")
Debug_dumpConfig();
else if (command == "dumpPDS")
Debug_dumpPersistance();
else if (command == "saveEE")
globals.requestEEAction = EE_ALL_SAVE;
else if (command == "showdtc")
Debug_ShowDTCs();
else if (command == "dumpGlobals")
Debug_dumpGlobals();
else if (command == "sdbg")
SetDebugportStatus(dbg_Serial, enabled);
else
Debug_pushMessage("unknown Command\n");
}
void Debug_formatCFG()
{
Debug_pushMessage("Formatting Config-EEPROM and reseting to default\n");
FormatConfig_EEPROM();
}
void Debug_formatPersistence()
{
Debug_pushMessage("Formatting Persistence-EEPROM and reseting to default\n");
FormatPersistence_EEPROM();
}
void Debug_printSystemInfo()
{
Debug_pushMessage("Souko's ChainOiler Mk1\n");
Debug_pushMessage("Hostname: %s\n", globals.DeviceName);
FlashMode_t ideMode = ESP.getFlashChipMode();
Debug_pushMessage("Sdk version: %s\n", ESP.getSdkVersion());
Debug_pushMessage("Core Version: %s\n", ESP.getCoreVersion().c_str());
Debug_pushMessage("Boot Version: %u\n", ESP.getBootVersion());
Debug_pushMessage("Boot Mode: %u\n", ESP.getBootMode());
Debug_pushMessage("CPU Frequency: %u MHz\n", ESP.getCpuFreqMHz());
Debug_pushMessage("Reset reason: %s\n", ESP.getResetReason().c_str());
Debug_pushMessage("Flash Size: %d\n", ESP.getFlashChipRealSize());
Debug_pushMessage("Flash Size IDE: %d\n", ESP.getFlashChipSize());
Debug_pushMessage("Flash ide mode: %s\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT"
: ideMode == FM_DIO ? "DIO"
: ideMode == FM_DOUT ? "DOUT"
: "UNKNOWN"));
Debug_pushMessage("OTA-Pass: %s\n", QUOTE(ADMIN_PASSWORD));
Debug_pushMessage("Git-Revison: %s\n", constants.GitHash);
Debug_pushMessage("Sw-Version: %d.%02d\n", constants.FW_Version_major, constants.FW_Version_minor);
}
void Debug_dumpConfig()
{
Debug_pushMessage("batteryType: %d\n", ConfigData.batteryType);
Debug_pushMessage("EEPROM_Version: %d\n", ConfigData.EEPROM_Version);
Debug_pushMessage("checksum: 0x%08X\n", ConfigData.checksum);
}
void Debug_dumpGlobals()
{
Debug_pushMessage("systemStatus: %d\n", globals.systemStatus);
Debug_pushMessage("resumeStatus: %d\n", globals.resumeStatus);
Debug_pushMessage("systemStatustxt: %s\n", globals.systemStatustxt);
Debug_pushMessage("battery_level: %d\n", globals.battery_level);
Debug_pushMessage("loadvoltage_mV: %d\n", globals.loadvoltage_mV);
Debug_pushMessage("requestEEAction: %d\n", globals.requestEEAction);
Debug_pushMessage("DeviceName: %s\n", globals.DeviceName);
Debug_pushMessage("DeviceName_ID: %s\n", globals.DeviceName_ID);
Debug_pushMessage("FlashVersion: %s\n", globals.FlashVersion);
Debug_pushMessage("eePersistanceAdress: %d\n", globals.eePersistanceAdress);
Debug_pushMessage("hasDTC: %d\n", globals.hasDTC);
}
void Debug_dumpPersistance()
{
Debug_pushMessage("writeCycleCounter: %d\n", PersistenceData.writeCycleCounter);
Debug_pushMessage("activeFaction: %d\n", PersistenceData.activeFaction);
Debug_pushMessage("faction_1_timer: %d\n", PersistenceData.faction_1_timer);
Debug_pushMessage("faction_2_timer: %d\n", PersistenceData.faction_2_timer);
Debug_pushMessage("faction_3_timer: %d\n", PersistenceData.faction_3_timer);
Debug_pushMessage("checksum: %d\n", PersistenceData.checksum);
Debug_pushMessage("PSD Adress: 0x%04X\n", globals.eePersistanceAdress);
}
void Debug_printWifiInfo()
{
}
void Debug_CheckEEPOM()
{
uint32_t checksum = PersistenceData.checksum;
PersistenceData.checksum = 0;
if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) == checksum)
{
Debug_pushMessage("PersistenceData EEPROM Checksum OK\n");
}
else
{
Debug_pushMessage("PersistenceData EEPROM Checksum BAD\n");
}
PersistenceData.checksum = checksum;
checksum = ConfigData.checksum;
ConfigData.checksum = 0;
if (Checksum_EEPROM((uint8_t *)&ConfigData, sizeof(ConfigData)) == checksum)
{
Debug_pushMessage("ConfigData EEPROM Checksum OK\n");
}
else
{
Debug_pushMessage("ConfigData EEPROM Checksum BAD\n");
}
ConfigData.checksum = checksum;
}
void Debug_ShowDTCs()
{
char buff_timestamp[16]; // Format: DD-hh:mm:ss:xxx
char buff_active[9];
Debug_pushMessage("\n timestamp | DTC-Nr. | status | severity\n");
for (uint32_t i = 0; i < MAX_DTC_STORAGE; i++)
{
if (DTCStorage[i].Number < DTC_LAST_DTC)
{
sprintf(buff_timestamp, "%02d-%02d:%02d:%02d:%03d",
DTCStorage[i].timestamp / 86400000, // Days
DTCStorage[i].timestamp / 360000 % 24, // Hours
DTCStorage[i].timestamp / 60000 % 60, // Minutes
DTCStorage[i].timestamp / 1000 % 60, // Seconds
DTCStorage[i].timestamp % 1000); // milliseconds
if (DTCStorage[i].active == DTC_ACTIVE)
strcpy(buff_active, "active");
else if (DTCStorage[i].active == DTC_PREVIOUS)
strcpy(buff_active, "previous");
else
strcpy(buff_active, "none");
Debug_pushMessage("%s %7d %8s %8d\n", buff_timestamp, DTCStorage[i].Number, buff_active, DTCStorage[i].severity);
}
}
}
void Debug_printHelp()
{
char buff[64];
for (int i = sizeof(helpCmd) / 63; i < sizeof(helpCmd) / 63; i++)
{
memcpy_P(buff, (helpCmd + (i * 63)), 63);
buff[63] = 0;
Debug_pushMessage(buff);
}
}

View File

@ -1,4 +1,5 @@
#include "dtc.h"
#include "debugger.h"
DTCEntry_s DTCStorage[MAX_DTC_STORAGE];
@ -10,7 +11,7 @@ void MaintainDTC(DTCNums_t DTC_no, DTCSeverity_t DTC_severity, boolean active, u
{
if (active && DTCStorage[i].active != DTC_ACTIVE)
{
Serial.printf("DTC gone active: %d, DebugVal: %d\n", DTC_no, DebugValue);
Debug_pushMessage("DTC gone active: %d, DebugVal: %d\n", DTC_no, DebugValue);
DTCStorage[i].timestamp = millis();
DTCStorage[i].active = DTC_ACTIVE;
DTCStorage[i].severity = DTC_severity;
@ -18,7 +19,7 @@ void MaintainDTC(DTCNums_t DTC_no, DTCSeverity_t DTC_severity, boolean active, u
}
if (!active && DTCStorage[i].active == DTC_ACTIVE)
{
Serial.printf("DTC gone previous: %d\n", DTC_no);
Debug_pushMessage("DTC gone previous: %d\n", DTC_no);
DTCStorage[i].active = DTC_PREVIOUS;
}
return;
@ -33,11 +34,12 @@ void MaintainDTC(DTCNums_t DTC_no, DTCSeverity_t DTC_severity, boolean active, u
{
if (DTCStorage[i].Number == DTC_LAST_DTC)
{
Serial.printf("new DTC registered: %d, DebugVal: %d\n", DTC_no, DebugValue);
Debug_pushMessage("new DTC registered: %d, DebugVal: %d\n", DTC_no, DebugValue);
DTCStorage[i].Number = DTC_no;
DTCStorage[i].timestamp = millis();
DTCStorage[i].active = DTC_ACTIVE;
DTCStorage[i].debugVal = DebugValue;
DTCStorage[i].severity = DTC_severity;
return;
}
}
@ -105,4 +107,33 @@ DTCNums_t getlastDTC_Severity(boolean only_active, DTCSeverity_t severity)
}
return pointer >= 0 ? DTCStorage[pointer].Number : DTC_LAST_DTC;
}
void DTC_Process()
{
static tSystem_Status preserverSysStatusError;
if (getlastDTC(false) < DTC_LAST_DTC)
{
globals.hasDTC = true;
if (getlastDTC_Severity(true, DTC_CRITICAL) < DTC_LAST_DTC && globals.systemStatus != sysStat_Shutdown)
{
if (globals.systemStatus != sysStat_Error)
{
preserverSysStatusError = globals.systemStatus;
}
globals.systemStatus = sysStat_Error;
}
else
{
if (globals.systemStatus == sysStat_Error)
{
globals.systemStatus = preserverSysStatusError;
}
}
}
else
{
globals.hasDTC = false;
}
}

10
Software/src/globals.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "globals.h"
Globals_t globals;
void initGlobals()
{
globals.systemStatus = sysStat_Startup;
globals.resumeStatus = sysStat_Normal;
globals.requestEEAction = EE_IDLE;
}

View File

@ -11,19 +11,18 @@
#include <ArduinoJson.h>
// local includes
#include "common.h"
#include "sanitycheck.h"
#include "defaults.h"
#include "webui.h"
#include "config.h"
#include "globals.h"
#include "dtc.h"
#include "common.h"
#include "debugger.h"
#ifdef FEATURE_ENABLE_LORA
#include "lora_net.h"
#endif
#ifdef WIFI_CLIENT
#include <WiFiMulti.h>
#ifdef FEATURE_ENABLE_WIFI_CLIENT
#include <ESP8266WiFiMulti.h>
@ -72,14 +71,6 @@ Ticker tmrWiFiMaintainConnection(tmrCallback_WiFiMaintainConnection, 1000, 0, MI
uint32_t DisplayOverrideFlag = 0;
char DisplayOverrideValue[5] = {0};
Globals_t globals;
void initGlobals()
{
globals.requestEEAction = EE_IDLE;
globals.resumeStatus = sysStat_Normal;
globals.systemStatus = sysStat_Startup;
}
#ifdef FEATURE_ENABLE_LORA
void setMPins_Helper(int pin, int status)
{
@ -106,6 +97,7 @@ void setup()
InitEEPROM();
GetConfig_EEPROM();
GetPersistence_EEPROM();
Serial.print("\nEE-Init done");
if (i2c_io.begin())
{
@ -191,15 +183,17 @@ void setup()
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed"); });
ArduinoOTA.begin();
ArduinoOTA.begin();
Serial.print("\nOTA-Init done");
initWebUI();
Serial.print("\nWebUI-Init done");
initGlobals();
Serial.print("\nglobals-Init done");
#ifdef CAPTIVE
dnsServer.start(53, "*", WiFi.softAPIP());
#endif
initWebUI();
initGlobals();
disp_FAC_1.init();
disp_FAC_1.setBrightness(5);
disp_FAC_2.init();
@ -211,7 +205,7 @@ void setup()
tmrFactionTicker.start();
tmrInputGetter.start();
Serial.println("Setup Done");
Serial.print("\nSetup Done\n");
}
void loop()
@ -220,12 +214,14 @@ void loop()
tmrEEPROMCyclicPDS.update();
tmrFactionTicker.update();
tmrInputGetter.update();
tmrStatusSender.update();
tmrPowerMonitor.update();
ArduinoOTA.handle();
SevenSeg_Output();
EEPROM_Process();
Webserver_Process();
DTC_Process();
Debug_Process();
#ifdef FEATURE_ENABLE_LORA
tmrStatusSender.update();
@ -416,6 +412,9 @@ void tmrCallback_PowerMonitor()
break;
}
MaintainDTC(DTC_BAT_CRITICAL, DTC_CRITICAL, (battery_level < 5 ? true : false), battery_level);
MaintainDTC(DTC_BAT_LOW, DTC_WARN, (battery_level < 15 ? true : false), battery_level);
// Serial.printf("Battery Level: %d %%\n", globals.battery_level);
// Serial.printf("Bus Voltage: %f V\n", busvoltage);
// Serial.printf("Shunt Voltage: %f mV\n", shuntvoltage);
@ -429,7 +428,7 @@ void tmrCallback_EEPROMCyclicPDS()
StorePersistence_EEPROM();
}
#ifdef WIFI_CLIENT
#ifdef FEATURE_ENABLE_WIFI_CLIENT
void tmrCallback_WiFiMaintainConnection()
{
static uint32_t WiFiFailCount = 0;
@ -459,7 +458,7 @@ void toggleWiFiAP(boolean shutdown)
if (WiFi.getMode() != WIFI_OFF)
{
WiFi.mode(WIFI_OFF);
debugV("WiFi turned off");
Serial.println("WiFi turned off");
#ifdef FEATURE_ENABLE_WIFI_CLIENT
WiFiMaintainConnectionTicker.stop();
#endif
@ -471,9 +470,9 @@ void toggleWiFiAP(boolean shutdown)
WiFi.softAP(QUOTE(WIFI_AP_SSID), QUOTE(WIFI_AP_PASSWORD));
#ifdef FEATURE_ENABLE_WIFI_CLIENT
WiFiMaintainConnectionTicker.stop();
debugV("WiFi AP started, stopped Maintain-Timer");
Serial.println("WiFi AP started, stopped Maintain-Timer");
#else
debugV("WiFi AP started");
Serial.println("WiFi AP started");
#endif
}
}