class constructors are a great way to have simple initialization sequence

This commit is contained in:
rusefi 2020-05-03 10:58:52 -04:00
parent 244d1ea7e8
commit 11a02b6395
5 changed files with 30 additions and 31 deletions

View File

@ -8,15 +8,33 @@
#include "engine.h" #include "engine.h"
#include "os_access.h" #include "os_access.h"
static char warningBuffer[ERROR_BUFFER_SIZE];
static critical_msg_t criticalErrorMessageBuffer;
#if EFI_SIMULATOR || EFI_PROD_CODE #if EFI_SIMULATOR || EFI_PROD_CODE
//todo: move into simulator global //todo: move into simulator global
#include "memstreams.h" #include "memstreams.h"
static MemoryStream warningStream; class ErrorState {
static MemoryStream firmwareErrorMessageStream; public:
#endif /* EFI_SIMULATOR || EFI_PROD_CODE */ /**
* Class constructors are a great way to have simple initialization sequence
*/
ErrorState();
MemoryStream warningStream;
MemoryStream firmwareErrorMessageStream;
};
static char warningBuffer[ERROR_BUFFER_SIZE]; ErrorState::ErrorState() {
static volatile bool isWarningStreamInitialized = false; /**
* these methods only change RAM state of data structures without any HAL access thus safe in contructor
*/
msObjectInit(&warningStream, (uint8_t *) warningBuffer, ERROR_BUFFER_SIZE, 0);
msObjectInit(&firmwareErrorMessageStream, criticalErrorMessageBuffer, sizeof(criticalErrorMessageBuffer), 0);
}
static ErrorState errorState;
#endif /* EFI_SIMULATOR || EFI_PROD_CODE */
#if EFI_HD44780_LCD #if EFI_HD44780_LCD
#include "lcd_HD44780.h" #include "lcd_HD44780.h"
@ -31,7 +49,6 @@ EXTERN_ENGINE;
extern int warningEnabled; extern int warningEnabled;
extern bool main_loop_started; extern bool main_loop_started;
static critical_msg_t criticalErrorMessageBuffer;
bool hasFirmwareErrorFlag = false; bool hasFirmwareErrorFlag = false;
const char *dbg_panic_file; const char *dbg_panic_file;
@ -105,7 +122,7 @@ static void printWarning(const char *fmt, va_list ap) {
logger.append(WARNING_PREFIX); logger.append(WARNING_PREFIX);
printToStream(&warningStream, fmt, ap); printToStream(&errorState.warningStream, fmt, ap);
if (CONFIG(showHumanReadableWarning)) { if (CONFIG(showHumanReadableWarning)) {
#if EFI_TUNER_STUDIO #if EFI_TUNER_STUDIO
@ -141,10 +158,6 @@ bool warning(obd_code_e code, const char *fmt, ...) {
#endif /* EFI_SIMULATOR */ #endif /* EFI_SIMULATOR */
#if EFI_SIMULATOR || EFI_PROD_CODE #if EFI_SIMULATOR || EFI_PROD_CODE
if (!isWarningStreamInitialized) {
firmwareError(CUSTOM_ERR_ASSERT, "warn stream not initialized for %d", code);
return false;
}
engine->engineState.warnings.addWarningCode(code); engine->engineState.warnings.addWarningCode(code);
// todo: move this logic into WarningCodeState? // todo: move this logic into WarningCodeState?
@ -209,20 +222,6 @@ void onUnlockHook(void) {
#endif /* EFI_CLOCK_LOCKS */ #endif /* EFI_CLOCK_LOCKS */
/**
* This method should be invoked really early in firmware initialization cycle.
*
* Implementation can only do trivial things like changing memory state. No hardware or OS access allowed
* within this method.
*/
void initErrorHandlingDataStructures(void) {
#if EFI_SIMULATOR || EFI_PROD_CODE
msObjectInit(&warningStream, (uint8_t *) warningBuffer, ERROR_BUFFER_SIZE, 0);
msObjectInit(&firmwareErrorMessageStream, criticalErrorMessageBuffer, sizeof(criticalErrorMessageBuffer), 0);
#endif
isWarningStreamInitialized = true;
}
void firmwareError(obd_code_e code, const char *fmt, ...) { void firmwareError(obd_code_e code, const char *fmt, ...) {
#if EFI_PROD_CODE #if EFI_PROD_CODE
if (hasFirmwareErrorFlag) if (hasFirmwareErrorFlag)
@ -246,13 +245,13 @@ void firmwareError(obd_code_e code, const char *fmt, ...) {
criticalErrorMessageBuffer[sizeof(criticalErrorMessageBuffer) - 1] = 0; // just to be sure criticalErrorMessageBuffer[sizeof(criticalErrorMessageBuffer) - 1] = 0; // just to be sure
} else { } else {
// todo: look into chsnprintf once on Chibios 3 // todo: look into chsnprintf once on Chibios 3
firmwareErrorMessageStream.eos = 0; // reset errorState.firmwareErrorMessageStream.eos = 0; // reset
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
chvprintf((BaseSequentialStream *) &firmwareErrorMessageStream, fmt, ap); chvprintf((BaseSequentialStream *) &errorState.firmwareErrorMessageStream, fmt, ap);
va_end(ap); va_end(ap);
// todo: reuse warning buffer helper method // todo: reuse warning buffer helper method
firmwareErrorMessageStream.buffer[firmwareErrorMessageStream.eos] = 0; // need to terminate explicitly errorState.firmwareErrorMessageStream.buffer[errorState.firmwareErrorMessageStream.eos] = 0; // need to terminate explicitly
} }
#else #else

View File

@ -40,7 +40,6 @@ extern bool hasFirmwareErrorFlag;
// todo: rename to getCriticalErrorMessage // todo: rename to getCriticalErrorMessage
char *getFirmwareError(void); char *getFirmwareError(void);
void initErrorHandlingDataStructures(void);
// todo: rename to getWarningMessage? // todo: rename to getWarningMessage?
char *getWarningMessage(void); char *getWarningMessage(void);

View File

@ -18,6 +18,9 @@
class PinRepository { class PinRepository {
public: public:
/**
* Class constructors are a great way to have simple initialization sequence
*/
PinRepository(); PinRepository();
int totalPinsUsed = 0; int totalPinsUsed = 0;
}; };

View File

@ -158,7 +158,6 @@ static void scheduleReboot(void) {
} }
void runRusEfi(void) { void runRusEfi(void) {
initErrorHandlingDataStructures();
efiAssertVoid(CUSTOM_RM_STACK_1, getCurrentRemainingStack() > 512, "init s"); efiAssertVoid(CUSTOM_RM_STACK_1, getCurrentRemainingStack() > 512, "init s");
assertEngineReference(); assertEngineReference();
engine->setConfig(config); engine->setConfig(config);

View File

@ -90,7 +90,6 @@ static void runChprintfTest() {
void rusEfiFunctionalTest(void) { void rusEfiFunctionalTest(void) {
printToConsole("Running rusEfi simulator version:"); printToConsole("Running rusEfi simulator version:");
initErrorHandlingDataStructures();
static char versionBuffer[20]; static char versionBuffer[20];
itoa10(versionBuffer, (int)getRusEfiVersion()); itoa10(versionBuffer, (int)getRusEfiVersion());
printToConsole(versionBuffer); printToConsole(versionBuffer);