From 2f580a37a28e3086af3b12c8348c151e7cf11b33 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 8 Dec 2021 17:49:16 +0300 Subject: [PATCH] terminal: context is a temporary Make sure it could be moved further along, and the values inside of it can be safely moved as well. Also fixup commands that were missing ctx in OK / Error. --- code/espurna/button.cpp | 2 +- code/espurna/crash.cpp | 2 +- code/espurna/debug.cpp | 2 +- code/espurna/homeassistant.cpp | 2 +- code/espurna/i2c.cpp | 4 +-- code/espurna/ifan.cpp | 2 +- code/espurna/influxdb.cpp | 4 +-- code/espurna/ir.cpp | 2 +- code/espurna/led.cpp | 2 +- code/espurna/light.cpp | 14 +++++----- code/espurna/lightfox.cpp | 4 +-- code/espurna/mqtt.cpp | 6 ++--- code/espurna/nofuss.cpp | 2 +- code/espurna/ntp.cpp | 8 +++--- code/espurna/ota_asynctcp.cpp | 2 +- code/espurna/ota_httpupdate.cpp | 2 +- code/espurna/relay.cpp | 2 +- code/espurna/rfbridge.cpp | 16 +++++------ code/espurna/rpnrules.cpp | 12 ++++----- code/espurna/rtcmem.cpp | 4 +-- code/espurna/sensor.cpp | 8 +++--- code/espurna/sensors/PZEM004TSensor.h | 6 ++--- code/espurna/sensors/PZEM004TV30Sensor.h | 2 +- code/espurna/settings.cpp | 16 +++++------ code/espurna/storage_eeprom.cpp | 16 +++++------ code/espurna/telnet.cpp | 6 ++--- code/espurna/terminal.cpp | 34 ++++++++++++------------ code/espurna/terminal_commands.h | 2 +- code/espurna/terminal_parsing.cpp | 25 ++++++++--------- code/espurna/tuya.cpp | 4 +-- code/espurna/wifi.cpp | 14 +++++----- code/test/unit/terminal/terminal.cpp | 22 +++++++-------- 32 files changed, 125 insertions(+), 124 deletions(-) diff --git a/code/espurna/button.cpp b/code/espurna/button.cpp index a4c7d1bd..54f5129a 100644 --- a/code/espurna/button.cpp +++ b/code/espurna/button.cpp @@ -1257,7 +1257,7 @@ void buttonSetup() { DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), count); #if TERMINAL_SUPPORT - terminalRegisterCommand(F("BUTTON"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("BUTTON"), [](::terminal::CommandContext&& ctx) { unsigned index { 0u }; for (auto& button : _buttons) { ctx.output.printf_P(PSTR("%u - "), index++); diff --git a/code/espurna/crash.cpp b/code/espurna/crash.cpp index 5a95c0c8..0b87e8b3 100644 --- a/code/espurna/crash.cpp +++ b/code/espurna/crash.cpp @@ -324,7 +324,7 @@ void crashSetup() { } #if TERMINAL_SUPPORT - terminalRegisterCommand(F("CRASH"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("CRASH"), [](::terminal::CommandContext&& ctx) { debug::crash::forceDump(ctx.output); terminalOK(ctx); }); diff --git a/code/espurna/debug.cpp b/code/espurna/debug.cpp index 47f02b1d..925299ef 100644 --- a/code/espurna/debug.cpp +++ b/code/espurna/debug.cpp @@ -665,7 +665,7 @@ void debugSetup() { #if DEBUG_LOG_BUFFER_SUPPORT #if TERMINAL_SUPPORT - terminalRegisterCommand(F("DEBUG.BUFFER"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("DEBUG.BUFFER"), [](::terminal::CommandContext&& ctx) { debug::buffer::disable(); if (!debug::buffer::size()) { terminalError(ctx, F("buffer is empty\n")); diff --git a/code/espurna/homeassistant.cpp b/code/espurna/homeassistant.cpp index f0cdbc69..f6e640d3 100644 --- a/code/espurna/homeassistant.cpp +++ b/code/espurna/homeassistant.cpp @@ -1007,7 +1007,7 @@ void haSetup() { mqttRegister(homeassistant::mqttCallback); #if TERMINAL_SUPPORT - terminalRegisterCommand(F("HA.SEND"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("HA.SEND"), [](::terminal::CommandContext&& ctx) { homeassistant::internal::state = homeassistant::internal::State::Pending; homeassistant::publishDiscovery(); terminalOK(ctx); diff --git a/code/espurna/i2c.cpp b/code/espurna/i2c.cpp index 2323990f..a1fbebe9 100644 --- a/code/espurna/i2c.cpp +++ b/code/espurna/i2c.cpp @@ -231,7 +231,7 @@ void init() { #if TERMINAL_SUPPORT void initTerminalCommands() { - terminalRegisterCommand(F("I2C.SCAN"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("I2C.SCAN"), [](::terminal::CommandContext&& ctx) { unsigned char devices { 0 }; i2c::scan([&](unsigned char address) { ++devices; @@ -246,7 +246,7 @@ void initTerminalCommands() { terminalError(ctx, F("No devices found")); }); - terminalRegisterCommand(F("I2C.CLEAR"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("I2C.CLEAR"), [](::terminal::CommandContext&& ctx) { ctx.output.printf("result: %d\n", i2c::clear()); terminalOK(ctx); }); diff --git a/code/espurna/ifan.cpp b/code/espurna/ifan.cpp index f6eb6ea2..00a23bda 100644 --- a/code/espurna/ifan.cpp +++ b/code/espurna/ifan.cpp @@ -346,7 +346,7 @@ void setup() { #endif #if TERMINAL_SUPPORT - terminalRegisterCommand(F("SPEED"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("SPEED"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() == 2) { updateSpeedFromPayload(ctx.argv[1]); } diff --git a/code/espurna/influxdb.cpp b/code/espurna/influxdb.cpp index 0f4749fd..78e11cac 100644 --- a/code/espurna/influxdb.cpp +++ b/code/espurna/influxdb.cpp @@ -305,9 +305,9 @@ void idbSetup() { espurnaRegisterLoop(_idbFlush); #if TERMINAL_SUPPORT - terminalRegisterCommand(F("IDB.SEND"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("IDB.SEND"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() != 4) { - terminalError(F("idb.send ")); + terminalError(ctx, F("idb.send ")); return; } diff --git a/code/espurna/ir.cpp b/code/espurna/ir.cpp index 8f0fe2b6..f5106a7f 100644 --- a/code/espurna/ir.cpp +++ b/code/espurna/ir.cpp @@ -1648,7 +1648,7 @@ void process(rx::DecodeResult& result) { } void setup() { - terminalRegisterCommand(F("IR.SEND"), [](const ::terminal::CommandContext& ctx) { + terminalRegisterCommand(F("IR.SEND"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() == 2) { auto view = StringView{ctx.argv[1]}; diff --git a/code/espurna/led.cpp b/code/espurna/led.cpp index 98124613..2bb2e8c4 100644 --- a/code/espurna/led.cpp +++ b/code/espurna/led.cpp @@ -1029,7 +1029,7 @@ void onConnected(JsonObject& root) { namespace terminal { void setup() { - terminalRegisterCommand(F("LED"), [](const ::terminal::CommandContext& ctx) { + terminalRegisterCommand(F("LED"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() > 1) { size_t id; if (!tryParseId(ctx.argv[1].c_str(), ledCount, id)) { diff --git a/code/espurna/light.cpp b/code/espurna/light.cpp index a53bb837..96e1b60f 100644 --- a/code/espurna/light.cpp +++ b/code/espurna/light.cpp @@ -2378,7 +2378,7 @@ namespace { void _lightInitCommands() { - terminalRegisterCommand(F("LIGHT"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("LIGHT"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() > 1) { if (!_lightParsePayload(ctx.argv[1].c_str())) { terminalError(ctx, F("Invalid payload")); @@ -2391,7 +2391,7 @@ void _lightInitCommands() { terminalOK(ctx); }); - terminalRegisterCommand(F("BRIGHTNESS"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("BRIGHTNESS"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() > 1) { _lightAdjustBrightness(ctx.argv[1]); lightUpdate(); @@ -2400,7 +2400,7 @@ void _lightInitCommands() { terminalOK(ctx); }); - terminalRegisterCommand(F("CHANNEL"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("CHANNEL"), [](::terminal::CommandContext&& ctx) { const size_t Channels { _light_channels.size() }; if (!Channels) { terminalError(ctx, F("No channels configured")); @@ -2435,7 +2435,7 @@ void _lightInitCommands() { terminalOK(ctx); }); - terminalRegisterCommand(F("RGB"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("RGB"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() > 1) { _lightFromRgbPayload(ctx.argv[1].c_str()); lightUpdate(); @@ -2444,7 +2444,7 @@ void _lightInitCommands() { terminalOK(ctx); }); - terminalRegisterCommand(F("HSV"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("HSV"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() > 1) { _lightFromHsvPayload(ctx.argv[1].c_str()); lightUpdate(); @@ -2453,7 +2453,7 @@ void _lightInitCommands() { terminalOK(ctx); }); - terminalRegisterCommand(F("KELVIN"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("KELVIN"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() > 1) { _lightAdjustKelvin(ctx.argv[1]); lightUpdate(); @@ -2462,7 +2462,7 @@ void _lightInitCommands() { terminalOK(ctx); }); - terminalRegisterCommand(F("MIRED"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("MIRED"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() > 1) { _lightAdjustMireds(ctx.argv[1]); lightUpdate(); diff --git a/code/espurna/lightfox.cpp b/code/espurna/lightfox.cpp index 1612d201..4a15c4c4 100644 --- a/code/espurna/lightfox.cpp +++ b/code/espurna/lightfox.cpp @@ -159,12 +159,12 @@ void _lightfoxWebSocketOnAction(uint32_t client_id, const char * action, JsonObj void _lightfoxInitCommands() { - terminalRegisterCommand(F("LIGHTFOX.LEARN"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("LIGHTFOX.LEARN"), [](::terminal::CommandContext&& ctx) { lightfoxLearn(); terminalOK(ctx); }); - terminalRegisterCommand(F("LIGHTFOX.CLEAR"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("LIGHTFOX.CLEAR"), [](::terminal::CommandContext&& ctx) { lightfoxClear(); terminalOK(ctx); }); diff --git a/code/espurna/mqtt.cpp b/code/espurna/mqtt.cpp index bb48417e..9c334bb3 100644 --- a/code/espurna/mqtt.cpp +++ b/code/espurna/mqtt.cpp @@ -788,18 +788,18 @@ namespace { #if TERMINAL_SUPPORT void _mqttInitCommands() { - terminalRegisterCommand(F("MQTT.RESET"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("MQTT.RESET"), [](::terminal::CommandContext&& ctx) { _mqttConfigure(); mqttDisconnect(); terminalOK(ctx); }); - terminalRegisterCommand(F("MQTT.INFO"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("MQTT.INFO"), [](::terminal::CommandContext&& ctx) { _mqttInfo(); terminalOK(ctx); }); - terminalRegisterCommand(F("MQTT.SEND"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("MQTT.SEND"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() == 3) { if (mqttSend(ctx.argv[1].c_str(), ctx.argv[2].c_str(), false, false)) { terminalOK(ctx); diff --git a/code/espurna/nofuss.cpp b/code/espurna/nofuss.cpp index c96fed9b..545a4c48 100644 --- a/code/espurna/nofuss.cpp +++ b/code/espurna/nofuss.cpp @@ -94,7 +94,7 @@ void _nofussLoop() { #if TERMINAL_SUPPORT void _nofussInitCommands() { - terminalRegisterCommand(F("NOFUSS"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("NOFUSS"), [](::terminal::CommandContext&& ctx) { terminalOK(ctx); nofussRun(); }); diff --git a/code/espurna/ntp.cpp b/code/espurna/ntp.cpp index 23025772..526223e3 100644 --- a/code/espurna/ntp.cpp +++ b/code/espurna/ntp.cpp @@ -607,7 +607,7 @@ void ntpSetup() { #endif #if TERMINAL_SUPPORT - terminalRegisterCommand(F("NTP"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("NTP"), [](::terminal::CommandContext&& ctx) { if (ntpSynced()) { _ntpReportTerminal(ctx.output); terminalOK(ctx); @@ -617,7 +617,7 @@ void ntpSetup() { terminalError(ctx, F("NTP not synced")); }); - terminalRegisterCommand(F("NTP.SYNC"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("NTP.SYNC"), [](::terminal::CommandContext&& ctx) { if (_ntp_synced) { sntp_stop(); sntp_init(); @@ -630,7 +630,7 @@ void ntpSetup() { // TODO: strptime & mktime is around ~3.7Kb #if 1 - terminalRegisterCommand(F("NTP.SETTIME"), [](const terminal::CommandContext& ctx) { + terminalRegisterCommand(F("NTP.SETTIME"), [](::terminal::CommandContext&& ctx) { if (ctx.argv.size() != 2) { terminalError(ctx, F("NTP.SETTIME