Skip to content

Commit

Permalink
Read config from EEPROM only if no config is in Flash (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
elral authored Sep 5, 2024
1 parent 5967300 commit 1acaa19
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ void resetConfig();
void readConfig();
void _activateConfig();
void readConfigFromMemory(bool configFromFlash);
bool configStoredInFlash()
{
return configLengthFlash > 0;
}
bool configStoredInEEPROM()
{
return configLengthEEPROM > 0;
}

// ************************************************************
// configBuffer handling
Expand Down Expand Up @@ -122,22 +130,21 @@ void OnSetConfig()
#ifdef DEBUG2CMDMESSENGER
cmdMessenger.sendCmd(kDebug, F("Setting config start"));
#endif
// For now a config can only be saved if NO config in flash is available
// otherwise a config from flash would be sent from the connector
// and saved in the EEPROM, so the config get's doubled
if (configLengthFlash == 0) {
char *cfg = cmdMessenger.readStringArg();
uint8_t cfgLen = strlen(cfg);

if (configLengthEEPROM + cfgLen + 1 < MEM_LEN_CONFIG) {
MFeeprom.write_block(MEM_OFFSET_CONFIG + configLengthEEPROM, cfg, cfgLen + 1); // save the received config string including the terminatung NULL (+1) to EEPROM
configLengthEEPROM += cfgLen;
cmdMessenger.sendCmd(kStatus, configLengthEEPROM);
} else
cmdMessenger.sendCmd(kStatus, -1); // last successfull saving block is already NULL terminated, nothing more todo
} else {
// A config can be in flash or in EEPROM, but only one option must be used
// Once a config is in EEPROM, this config will be loaded and reported to the connector
// If no config is in EEPROM, the config from flash will be used if available
// This ensures backwards compatibility if a board gets updated with a config in flash
// but also have a user config in EEPROM
char *cfg = cmdMessenger.readStringArg();
uint8_t cfgLen = strlen(cfg);

bool maxConfigLengthNotExceeded = configLengthEEPROM + cfgLen + 1 < MEM_LEN_CONFIG;
if (maxConfigLengthNotExceeded) {
MFeeprom.write_block(MEM_OFFSET_CONFIG + configLengthEEPROM, cfg, cfgLen + 1); // save the received config string including the terminatung NULL (+1) to EEPROM
configLengthEEPROM += cfgLen;
cmdMessenger.sendCmd(kStatus, configLengthEEPROM);
}
} else
cmdMessenger.sendCmd(kStatus, -1); // last successfull saving block is already NULL terminated, nothing more todo
#ifdef DEBUG2CMDMESSENGER
cmdMessenger.sendCmd(kDebug, F("Setting config end"));
#endif
Expand Down Expand Up @@ -365,19 +372,17 @@ void InitArrays(uint8_t *numberDevices)
void readConfig()
{
uint8_t numberDevices[kTypeMax] = {0};
if (configLengthFlash > 0) {
GetArraySizes(numberDevices, true);
}
if (configLengthEEPROM > 0) {
GetArraySizes(numberDevices, false);

// Early return if no valid configuration is found
if (!configStoredInFlash() && !configStoredInEEPROM()) {
InitArrays(numberDevices);
return;
}

// Determine which configuration to use and proceed
GetArraySizes(numberDevices, configStoredInFlash());
InitArrays(numberDevices);
if (configLengthFlash > 0) {
readConfigFromMemory(true);
}
if (configLengthEEPROM > 0) {
readConfigFromMemory(false);
}
readConfigFromMemory(configStoredInFlash());
}

void readConfigFromMemory(bool configFromFlash)
Expand Down Expand Up @@ -577,23 +582,17 @@ void readConfigFromMemory(bool configFromFlash)

void OnGetConfig()
{
bool sentFromFlash = false;
cmdMessenger.sendCmdStart(kInfo);
if (configLengthFlash > 0) {
if (configStoredInEEPROM()) {
cmdMessenger.sendCmdArg((char)MFeeprom.read_byte(MEM_OFFSET_CONFIG));
for (uint16_t i = 1; i < configLengthEEPROM; i++) {
cmdMessenger.sendArg((char)MFeeprom.read_byte(MEM_OFFSET_CONFIG + i));
}
} else if (configStoredInFlash()) {
cmdMessenger.sendCmdArg((char)pgm_read_byte_near(CustomDeviceConfig));
for (uint16_t i = 1; i < (configLengthFlash - 1); i++) {
cmdMessenger.sendArg((char)pgm_read_byte_near(CustomDeviceConfig + i));
}
sentFromFlash = true;
}
if (configLengthEEPROM > 0) {
if (sentFromFlash)
cmdMessenger.sendArg((char)MFeeprom.read_byte(MEM_OFFSET_CONFIG));
else
cmdMessenger.sendCmdArg((char)MFeeprom.read_byte(MEM_OFFSET_CONFIG));
for (uint16_t i = 1; i < configLengthEEPROM; i++) {
cmdMessenger.sendArg((char)MFeeprom.read_byte(MEM_OFFSET_CONFIG + i));
}
}
cmdMessenger.sendCmdEnd();
}
Expand Down Expand Up @@ -719,8 +718,10 @@ void OnGenNewSerial()
// ************************************************************
void storeName()
{
MFeeprom.write_byte(MEM_OFFSET_NAME, '#');
MFeeprom.write_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1);
if (!configStoredInFlash()) {
MFeeprom.write_byte(MEM_OFFSET_NAME, '#');
MFeeprom.write_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1);
}
}

void restoreName()
Expand All @@ -734,8 +735,10 @@ void restoreName()
void OnSetName()
{
char *cfg = cmdMessenger.readStringArg();
memcpy(name, cfg, MEM_LEN_NAME);
storeName();
if (!configStoredInFlash()) {
memcpy(name, cfg, MEM_LEN_NAME);
storeName();
}
cmdMessenger.sendCmd(kStatus, name);
}

Expand Down

0 comments on commit 1acaa19

Please sign in to comment.