mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-04 23:44:20 +01:00
Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
148 lines
3.9 KiB
C++
148 lines
3.9 KiB
C++
/*
|
|
|
|
EEPROM MODULE
|
|
|
|
*/
|
|
|
|
#include "storage_eeprom.h"
|
|
#include "settings.h"
|
|
|
|
EEPROM_Rotate EEPROMr;
|
|
bool _eeprom_commit = false;
|
|
|
|
uint32_t _eeprom_commit_count = 0;
|
|
bool _eeprom_last_commit_result = false;
|
|
|
|
void eepromRotate(bool value) {
|
|
// Enable/disable EEPROM rotation only if we are using more sectors than the
|
|
// reserved by the memory layout
|
|
if (EEPROMr.size() > EEPROMr.reserved()) {
|
|
if (value) {
|
|
DEBUG_MSG_P(PSTR("[EEPROM] Reenabling EEPROM rotation\n"));
|
|
} else {
|
|
DEBUG_MSG_P(PSTR("[EEPROM] Disabling EEPROM rotation\n"));
|
|
}
|
|
EEPROMr.rotate(value);
|
|
|
|
// Because .rotate(false) marks EEPROM as dirty, this is equivalent to the .backup(0)
|
|
eepromCommit();
|
|
}
|
|
}
|
|
|
|
uint32_t eepromCurrent() {
|
|
return EEPROMr.current();
|
|
}
|
|
|
|
String eepromSectors() {
|
|
String response;
|
|
for (uint32_t i = 0; i < EEPROMr.size(); i++) {
|
|
if (i > 0) response = response + String(", ");
|
|
response = response + String(EEPROMr.base() - i);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
void eepromSectorsDebug() {
|
|
DEBUG_MSG_P(PSTR("[MAIN] EEPROM sectors: %s\n"), (char *) eepromSectors().c_str());
|
|
DEBUG_MSG_P(PSTR("[MAIN] EEPROM current: %lu\n"), eepromCurrent());
|
|
}
|
|
|
|
bool _eepromCommit() {
|
|
_eeprom_commit_count++;
|
|
_eeprom_last_commit_result = EEPROMr.commit();
|
|
return _eeprom_last_commit_result;
|
|
}
|
|
|
|
void eepromCommit() {
|
|
_eeprom_commit = true;
|
|
}
|
|
|
|
void eepromBackup(uint32_t index){
|
|
EEPROMr.backup(index);
|
|
}
|
|
|
|
#if TERMINAL_SUPPORT
|
|
|
|
void _eepromInitCommands() {
|
|
|
|
terminalRegisterCommand(F("EEPROM"), [](const terminal::CommandContext&) {
|
|
infoMemory("EEPROM", SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE - settingsSize());
|
|
eepromSectorsDebug();
|
|
if (_eeprom_commit_count > 0) {
|
|
DEBUG_MSG_P(PSTR("[MAIN] Commits done: %lu\n"), _eeprom_commit_count);
|
|
DEBUG_MSG_P(PSTR("[MAIN] Last result: %s\n"), _eeprom_last_commit_result ? "OK" : "ERROR");
|
|
}
|
|
terminalOK();
|
|
});
|
|
|
|
terminalRegisterCommand(F("EEPROM.COMMIT"), [](const terminal::CommandContext&) {
|
|
const bool res = _eepromCommit();
|
|
if (res) {
|
|
terminalOK();
|
|
} else {
|
|
DEBUG_MSG_P(PSTR("-ERROR\n"));
|
|
}
|
|
});
|
|
|
|
terminalRegisterCommand(F("EEPROM.DUMP"), [](const terminal::CommandContext& ctx) {
|
|
// XXX: like Update::printError, dump only accepts Stream
|
|
// this should be safe, since we expect read-only stream
|
|
EEPROMr.dump(reinterpret_cast<Stream&>(ctx.output));
|
|
terminalOK(ctx.output);
|
|
});
|
|
|
|
terminalRegisterCommand(F("FLASH.DUMP"), [](const terminal::CommandContext& ctx) {
|
|
if (ctx.argc < 2) {
|
|
terminalError(F("Wrong arguments"));
|
|
return;
|
|
}
|
|
uint32_t sector = ctx.argv[1].toInt();
|
|
uint32_t max = ESP.getFlashChipSize() / SPI_FLASH_SEC_SIZE;
|
|
if (sector >= max) {
|
|
terminalError(F("Sector out of range"));
|
|
return;
|
|
}
|
|
EEPROMr.dump(reinterpret_cast<Stream&>(ctx.output), sector);
|
|
terminalOK(ctx.output);
|
|
});
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
void eepromLoop() {
|
|
if (_eeprom_commit) {
|
|
_eepromCommit();
|
|
_eeprom_commit = false;
|
|
}
|
|
}
|
|
|
|
void eepromSetup() {
|
|
|
|
#ifdef EEPROM_ROTATE_SECTORS
|
|
EEPROMr.size(EEPROM_ROTATE_SECTORS);
|
|
#else
|
|
// If the memory layout has more than one sector reserved use those,
|
|
// otherwise calculate pool size based on memory size.
|
|
if (EEPROMr.size() == 1) {
|
|
if (EEPROMr.last() > 1000) { // 4Mb boards
|
|
EEPROMr.size(4);
|
|
} else if (EEPROMr.last() > 250) { // 1Mb boards
|
|
EEPROMr.size(2);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
EEPROMr.offset(EEPROM_ROTATE_DATA);
|
|
EEPROMr.begin(EEPROM_SIZE);
|
|
|
|
#if TERMINAL_SUPPORT
|
|
_eepromInitCommands();
|
|
#endif
|
|
|
|
espurnaRegisterLoop(eepromLoop);
|
|
|
|
}
|