Browse Source

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.
pull/2490/head
Maxim Prokhorov 2 years ago
parent
commit
2f580a37a2
32 changed files with 125 additions and 124 deletions
  1. +1
    -1
      code/espurna/button.cpp
  2. +1
    -1
      code/espurna/crash.cpp
  3. +1
    -1
      code/espurna/debug.cpp
  4. +1
    -1
      code/espurna/homeassistant.cpp
  5. +2
    -2
      code/espurna/i2c.cpp
  6. +1
    -1
      code/espurna/ifan.cpp
  7. +2
    -2
      code/espurna/influxdb.cpp
  8. +1
    -1
      code/espurna/ir.cpp
  9. +1
    -1
      code/espurna/led.cpp
  10. +7
    -7
      code/espurna/light.cpp
  11. +2
    -2
      code/espurna/lightfox.cpp
  12. +3
    -3
      code/espurna/mqtt.cpp
  13. +1
    -1
      code/espurna/nofuss.cpp
  14. +4
    -4
      code/espurna/ntp.cpp
  15. +1
    -1
      code/espurna/ota_asynctcp.cpp
  16. +1
    -1
      code/espurna/ota_httpupdate.cpp
  17. +1
    -1
      code/espurna/relay.cpp
  18. +8
    -8
      code/espurna/rfbridge.cpp
  19. +6
    -6
      code/espurna/rpnrules.cpp
  20. +2
    -2
      code/espurna/rtcmem.cpp
  21. +4
    -4
      code/espurna/sensor.cpp
  22. +3
    -3
      code/espurna/sensors/PZEM004TSensor.h
  23. +1
    -1
      code/espurna/sensors/PZEM004TV30Sensor.h
  24. +8
    -8
      code/espurna/settings.cpp
  25. +8
    -8
      code/espurna/storage_eeprom.cpp
  26. +3
    -3
      code/espurna/telnet.cpp
  27. +17
    -17
      code/espurna/terminal.cpp
  28. +1
    -1
      code/espurna/terminal_commands.h
  29. +13
    -12
      code/espurna/terminal_parsing.cpp
  30. +2
    -2
      code/espurna/tuya.cpp
  31. +7
    -7
      code/espurna/wifi.cpp
  32. +11
    -11
      code/test/unit/terminal/terminal.cpp

+ 1
- 1
code/espurna/button.cpp View File

@ -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++);


+ 1
- 1
code/espurna/crash.cpp View File

@ -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);
});


+ 1
- 1
code/espurna/debug.cpp View File

@ -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"));


+ 1
- 1
code/espurna/homeassistant.cpp View File

@ -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);


+ 2
- 2
code/espurna/i2c.cpp View File

@ -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);
});


+ 1
- 1
code/espurna/ifan.cpp View File

@ -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]);
}


+ 2
- 2
code/espurna/influxdb.cpp View File

@ -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 <topic> <id> <value>"));
terminalError(ctx, F("idb.send <topic> <id> <value>"));
return;
}


+ 1
- 1
code/espurna/ir.cpp View File

@ -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]};


+ 1
- 1
code/espurna/led.cpp View File

@ -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)) {


+ 7
- 7
code/espurna/light.cpp View File

@ -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();


+ 2
- 2
code/espurna/lightfox.cpp View File

@ -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);
});


+ 3
- 3
code/espurna/mqtt.cpp View File

@ -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);


+ 1
- 1
code/espurna/nofuss.cpp View File

@ -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();
});


+ 4
- 4
code/espurna/ntp.cpp View File

@ -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 <TIME>"));
return;
@ -646,7 +646,7 @@ void ntpSetup() {
terminalError(ctx, F("Invalid timestamp"));
});
#else
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 <TIME>"));
return;


+ 1
- 1
code/espurna/ota_asynctcp.cpp View File

@ -242,7 +242,7 @@ void clientFromUrl(const String& string) {
#if TERMINAL_SUPPORT
void terminalCommands() {
terminalRegisterCommand(F("OTA"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("OTA"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() == 2) {
clientFromUrl(ctx.argv[1]);
terminalOK(ctx);


+ 1
- 1
code/espurna/ota_httpupdate.cpp View File

@ -144,7 +144,7 @@ void clientFromUrl(const String& url) {
#if TERMINAL_SUPPORT
void terminalCommands() {
terminalRegisterCommand(F("OTA"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("OTA"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() == 2) {
clientFromUrl(ctx.argv[1]);
terminalOK(ctx);


+ 1
- 1
code/espurna/relay.cpp View File

@ -2112,7 +2112,7 @@ namespace {
void _relayInitCommands() {
terminalRegisterCommand(F("RELAY"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RELAY"), [](::terminal::CommandContext&& ctx) {
auto showRelays = [&](size_t start, size_t stop, bool full = true) {
for (size_t index = start; index < stop; ++index) {
auto& relay = _relays[index];


+ 8
- 8
code/espurna/rfbridge.cpp View File

@ -1076,8 +1076,8 @@ void _rfbApiSetup() {
#if TERMINAL_SUPPORT
void _rfbCommandStatusDispatch(const terminal::CommandContext& ctx, size_t id, const String& payload, RelayStatusCallback callback) {
auto parsed = rpcParsePayload(payload.c_str());
void _rfbCommandStatusDispatch(::terminal::CommandContext&& ctx, size_t id, RelayStatusCallback callback) {
auto parsed = rpcParsePayload(ctx.argv[2].c_str());
switch (parsed) {
case PayloadStatus::On:
case PayloadStatus::Off:
@ -1092,7 +1092,7 @@ void _rfbCommandStatusDispatch(const terminal::CommandContext& ctx, size_t id, c
void _rfbInitCommands() {
terminalRegisterCommand(F("RFB.SEND"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RFB.SEND"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() == 2) {
rfbSend(ctx.argv[1]);
return;
@ -1102,7 +1102,7 @@ void _rfbInitCommands() {
});
#if RELAY_SUPPORT
terminalRegisterCommand(F("RFB.LEARN"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RFB.LEARN"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 3) {
terminalError(ctx, F("RFB.LEARN <ID> <STATUS>"));
return;
@ -1114,10 +1114,10 @@ void _rfbInitCommands() {
return;
}
_rfbCommandStatusDispatch(ctx, id, ctx.argv[2], rfbLearn);
_rfbCommandStatusDispatch(std::move(ctx), id, rfbLearn);
});
terminalRegisterCommand(F("RFB.FORGET"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RFB.FORGET"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() < 2) {
terminalError(ctx, F("RFB.FORGET <ID> [<STATUS>]"));
return;
@ -1130,7 +1130,7 @@ void _rfbInitCommands() {
}
if (ctx.argv.size() == 3) {
_rfbCommandStatusDispatch(ctx, id, ctx.argv[2], rfbForget);
_rfbCommandStatusDispatch(std::move(ctx), id, rfbForget);
return;
}
@ -1142,7 +1142,7 @@ void _rfbInitCommands() {
#endif // if RELAY_SUPPORT
#if RFB_PROVIDER == RFB_PROVIDER_EFM8BB1
terminalRegisterCommand(F("RFB.WRITE"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RFB.WRITE"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 2) {
terminalError(ctx, F("RFB.WRITE <PAYLOAD>"));
return;


+ 6
- 6
code/espurna/rpnrules.cpp View File

@ -263,7 +263,7 @@ void showStack(Print& output) {
}
void setup() {
terminalRegisterCommand(F("RPN.RUNNERS"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RPN.RUNNERS"), [](::terminal::CommandContext&& ctx) {
if (internal::runners.empty()) {
terminalError(ctx, F("No active runners"));
return;
@ -280,7 +280,7 @@ void setup() {
terminalOK(ctx);
});
terminalRegisterCommand(F("RPN.VARS"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RPN.VARS"), [](::terminal::CommandContext&& ctx) {
rpn_variables_foreach(internal::context, [&ctx](const String& name, const rpn_value& value) {
char buffer[256] = {0};
snprintf_P(buffer, sizeof(buffer), PSTR(" %s: %s\n"), name.c_str(), valueToString(value).c_str());
@ -289,7 +289,7 @@ void setup() {
terminalOK(ctx);
});
terminalRegisterCommand(F("RPN.OPS"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RPN.OPS"), [](::terminal::CommandContext&& ctx) {
rpn_operators_foreach(internal::context, [&ctx](const String& name, size_t argc, rpn_operator::callback_type) {
char buffer[128] = {0};
snprintf_P(buffer, sizeof(buffer), PSTR(" %s (%d)\n"), name.c_str(), argc);
@ -298,9 +298,9 @@ void setup() {
terminalOK(ctx);
});
terminalRegisterCommand(F("RPN.TEST"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RPN.TEST"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 2) {
terminalError(F("Wrong arguments"));
terminalError(ctx, F("Wrong arguments"));
return;
}
@ -963,7 +963,7 @@ void init(rpn_context& context) {
internal::stale_delay = getSetting("rfbStaleDelay", internal::StaleDelay);
#if TERMINAL_SUPPORT
terminalRegisterCommand(F("RFB.CODES"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RFB.CODES"), [](::terminal::CommandContext&& ctx) {
for (auto& code : internal::codes) {
char buffer[128] = {0};
snprintf_P(buffer, sizeof(buffer),


+ 2
- 2
code/espurna/rtcmem.cpp View File

@ -51,12 +51,12 @@ bool _rtcmemStatus() {
#if TERMINAL_SUPPORT
void _rtcmemInitCommands() {
terminalRegisterCommand(F("RTCMEM.REINIT"), [](const terminal::CommandContext&) {
terminalRegisterCommand(F("RTCMEM.REINIT"), [](::terminal::CommandContext&&) {
_rtcmemInit();
});
#if DEBUG_SUPPORT
terminalRegisterCommand(F("RTCMEM.DUMP"), [](const terminal::CommandContext&) {
terminalRegisterCommand(F("RTCMEM.DUMP"), [](::terminal::CommandContext&&) {
DEBUG_MSG_P(PSTR("[RTCMEM] boot_status=%u status=%u blocks_used=%u\n"),
_rtcmem_status, _rtcmemStatus(), RtcmemSize);


+ 4
- 4
code/espurna/sensor.cpp View File

@ -2189,7 +2189,7 @@ void _sensorMqttCallback(unsigned int type, const char* topic, char* payload) {
namespace {
void _sensorInitCommands() {
terminalRegisterCommand(F("MAGNITUDES"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("MAGNITUDES"), [](::terminal::CommandContext&& ctx) {
char last[64];
char reported[64];
for (size_t index = 0; index < _magnitudes.size(); ++index) {
@ -2203,7 +2203,7 @@ void _sensorInitCommands() {
terminalOK();
});
terminalRegisterCommand(F("EXPECTED"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("EXPECTED"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() == 3) {
const auto id = settings::internal::convert<size_t>(ctx.argv[1]);
if (id < _magnitudes.size()) {
@ -2222,12 +2222,12 @@ void _sensorInitCommands() {
terminalError(ctx, F("EXPECTED <ID> <VALUE>"));
});
terminalRegisterCommand(F("RESET.RATIOS"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RESET.RATIOS"), [](::terminal::CommandContext&& ctx) {
_sensorApiEmonResetRatios();
terminalOK(ctx);
});
terminalRegisterCommand(F("ENERGY"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("ENERGY"), [](::terminal::CommandContext&& ctx) {
using IndexType = decltype(sensor_magnitude_t::index_global);
if (ctx.argv.size() == 3) {


+ 3
- 3
code/espurna/sensors/PZEM004TSensor.h View File

@ -466,14 +466,14 @@ constexpr BaseEmonSensor::Magnitude PZEM004TSensor::Magnitudes[];
void PZEM004TSensor::registerTerminalCommands() {
#if TERMINAL_SUPPORT
terminalRegisterCommand(F("PZ.DEVICES"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("PZ.DEVICES"), [](::terminal::CommandContext&& ctx) {
foreach([&](const PZEM004TSensor& device) {
ctx.output.printf("%s\n", device._address.toString().c_str());
});
terminalOK(ctx);
});
terminalRegisterCommand(F("PZ.PORTS"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("PZ.PORTS"), [](::terminal::CommandContext&& ctx) {
auto it = _ports.begin();
auto end = _ports.end();
@ -519,7 +519,7 @@ void PZEM004TSensor::registerTerminalCommands() {
// Set the *currently connected* device address
// (ref. comment at the top, shouldn't do this when multiple devices are connected)
terminalRegisterCommand(F("PZ.ADDRESS"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("PZ.ADDRESS"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 3) {
terminalError(ctx, F("PZ.ADDRESS <PORT> <ADDRESS>"));
return;


+ 1
- 1
code/espurna/sensors/PZEM004TV30Sensor.h View File

@ -659,7 +659,7 @@ PZEM004TV30Sensor::PortPtr PZEM004TV30Sensor::_port{};
void PZEM004TV30Sensor::registerTerminalCommands() {
#if TERMINAL_SUPPORT
terminalRegisterCommand(F("PZ.ADDRESS"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("PZ.ADDRESS"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 2) {
terminalError(ctx.output, F("PZ.ADDRESS <ADDRESS>"));
return;


+ 8
- 8
code/espurna/settings.cpp View File

@ -494,7 +494,7 @@ void settingsGetJson(JsonObject& root) {
namespace {
void _settingsInitCommands() {
terminalRegisterCommand(F("CONFIG"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("CONFIG"), [](::terminal::CommandContext&& ctx) {
// TODO: enough of a buffer?
DynamicJsonBuffer jsonBuffer(1024);
JsonObject& root = jsonBuffer.createObject();
@ -503,7 +503,7 @@ void _settingsInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("KEYS"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("KEYS"), [](::terminal::CommandContext&& ctx) {
auto keys = settingsKeys();
ctx.output.print(F("Current settings:"));
@ -522,7 +522,7 @@ void _settingsInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("DEL"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("DEL"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() < 2) {
terminalError(ctx, F("del <key> [<key>...]"));
return;
@ -540,7 +540,7 @@ void _settingsInitCommands() {
}
});
terminalRegisterCommand(F("SET"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("SET"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 3) {
terminalError(ctx, F("set <key> <value>"));
return;
@ -554,7 +554,7 @@ void _settingsInitCommands() {
terminalError(ctx, F("could not set the key"));
});
terminalRegisterCommand(F("GET"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("GET"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() < 2) {
terminalError(ctx, F("get <key> [<key>...]"));
return;
@ -580,18 +580,18 @@ void _settingsInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("RELOAD"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RELOAD"), [](::terminal::CommandContext&& ctx) {
espurnaReload();
terminalOK(ctx);
});
terminalRegisterCommand(F("FACTORY.RESET"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("FACTORY.RESET"), [](::terminal::CommandContext&& ctx) {
factoryReset();
terminalOK(ctx);
});
#if not SETTINGS_AUTOSAVE
terminalRegisterCommand(F("SAVE"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("SAVE"), [](::terminal::CommandContext&& ctx) {
eepromCommit();
terminalOK(ctx);
});


+ 8
- 8
code/espurna/storage_eeprom.cpp View File

@ -74,7 +74,7 @@ void eepromBackup(uint32_t index){
void _eepromInitCommands() {
terminalRegisterCommand(F("EEPROM"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("EEPROM"), [](::terminal::CommandContext&& ctx) {
ctx.output.printf_P(PSTR("Sectors: %s, current: %lu\n"),
eepromSectors().c_str(), eepromCurrent());
if (_eeprom_commit_count > 0) {
@ -84,29 +84,29 @@ void _eepromInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("EEPROM.COMMIT"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("EEPROM.COMMIT"), [](::terminal::CommandContext&& ctx) {
_eepromCommit();
terminalOK(ctx);
});
terminalRegisterCommand(F("EEPROM.DUMP"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("EEPROM.DUMP"), [](::terminal::CommandContext&& ctx) {
EEPROMr.dump(static_cast<Stream&>(ctx.output)); // XXX: only Print interface is used
terminalOK(ctx.output);
terminalOK(ctx);
});
terminalRegisterCommand(F("FLASH.DUMP"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("FLASH.DUMP"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() < 2) {
terminalError(F("Wrong arguments"));
terminalError(ctx, 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"));
terminalError(ctx, F("Sector out of range"));
return;
}
EEPROMr.dump(static_cast<Stream&>(ctx.output), sector); // XXX: only Print interface is used
terminalOK(ctx.output);
terminalOK(ctx);
});
}


+ 3
- 3
code/espurna/telnet.cpp View File

@ -561,14 +561,14 @@ void telnetSetup() {
#endif
#if TERMINAL_SUPPORT
terminalRegisterCommand(F("TELNET.REVERSE"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("TELNET.REVERSE"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() < 3) {
terminalError(F("Wrong arguments. Usage: TELNET.REVERSE <host> <port>"));
terminalError(ctx, F("Wrong arguments. Usage: TELNET.REVERSE <host> <port>"));
return;
}
terminalOK();
_telnetReverse(ctx.argv[1].c_str(), ctx.argv[2].toInt());
terminalOK(ctx);
});
#endif
#endif


+ 17
- 17
code/espurna/terminal.cpp View File

@ -159,7 +159,7 @@ unsigned char _serial_rx_pointer = 0;
// Commands
// -----------------------------------------------------------------------------
void _terminalHelpCommand(const terminal::CommandContext& ctx) {
void _terminalHelpCommand(::terminal::CommandContext&& ctx) {
auto names = _terminal.names();
// XXX: Core's ..._P funcs only allow 2nd pointer to be in PROGMEM,
@ -350,13 +350,13 @@ void _terminalInitCommands() {
terminalRegisterCommand(F("COMMANDS"), _terminalHelpCommand);
terminalRegisterCommand(F("HELP"), _terminalHelpCommand);
terminalRegisterCommand(F("ERASE.CONFIG"), [](const terminal::CommandContext&) {
terminalRegisterCommand(F("ERASE.CONFIG"), [](::terminal::CommandContext&&) {
terminalOK();
customResetReason(CustomResetReason::Terminal);
forceEraseSDKConfig();
});
terminalRegisterCommand(F("ADC"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("ADC"), [](::terminal::CommandContext&& ctx) {
const int pin = (ctx.argv.size() == 2)
? ctx.argv[1].toInt()
: A0;
@ -365,7 +365,7 @@ void _terminalInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("GPIO"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("GPIO"), [](::terminal::CommandContext&& ctx) {
const int pin = (ctx.argv.size() >= 2)
? ctx.argv[1].toInt()
: -1;
@ -404,7 +404,7 @@ void _terminalInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("HEAP"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("HEAP"), [](::terminal::CommandContext&& ctx) {
const auto stats = systemHeapStats();
ctx.output.printf_P(PSTR("initial: %lu available: %lu contiguous: %hu\n"),
systemInitialFreeHeap(), stats.available, stats.usable);
@ -412,13 +412,13 @@ void _terminalInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("STACK"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("STACK"), [](::terminal::CommandContext&& ctx) {
ctx.output.printf_P(PSTR("continuation stack initial: %d, free: %u\n"),
CONT_STACKSIZE, systemFreeStack());
terminalOK(ctx);
});
terminalRegisterCommand(F("INFO"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("INFO"), [](::terminal::CommandContext&& ctx) {
ctx.output.printf_P(PSTR("%s %s built %s\n"), getAppName(), getVersion(), buildTime().c_str());
ctx.output.printf_P(PSTR("mcu: esp8266 chipid: %s freq: %hhumhz\n"), getFullChipId().c_str(), system_get_cpu_freq());
ctx.output.printf_P(PSTR("sdk: %s core: %s\n"),
@ -438,7 +438,7 @@ void _terminalInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("STORAGE"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("STORAGE"), [](::terminal::CommandContext&& ctx) {
ctx.output.printf_P(PSTR("flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
ctx.output.printf_P(PSTR("speed: %u\n"), ESP.getFlashChipSpeed());
ctx.output.printf_P(PSTR("mode: %s\n"), getFlashChipMode());
@ -476,7 +476,7 @@ void _terminalInitCommands() {
terminalOK(ctx);
});
terminalRegisterCommand(F("RESET"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("RESET"), [](::terminal::CommandContext&& ctx) {
auto count = 1;
if (ctx.argv.size() == 2) {
count = ctx.argv[1].toInt();
@ -489,19 +489,19 @@ void _terminalInitCommands() {
deferredReset(100, CustomResetReason::Terminal);
});
terminalRegisterCommand(F("UPTIME"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("UPTIME"), [](::terminal::CommandContext&& ctx) {
ctx.output.printf_P(PSTR("uptime %s\n"), getUptime().c_str());
terminalOK(ctx);
});
#if SECURE_CLIENT == SECURE_CLIENT_BEARSSL
terminalRegisterCommand(F("MFLN.PROBE"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("MFLN.PROBE"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 3) {
terminalError(ctx, F("<url> <value>"));
return;
}
URL _url(ctx.argv[1]);
URL _url(std::move(ctx.argv[1]));
uint16_t requested_mfln = atol(ctx.argv[2].c_str());
auto client = std::make_unique<BearSSL::WiFiClientSecure>();
@ -516,13 +516,13 @@ void _terminalInitCommands() {
});
#endif
terminalRegisterCommand(F("HOST"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("HOST"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 2) {
terminalError(ctx, F("<hostname>"));
return;
}
dns::start(String(ctx.argv[1]), [&](const char* name, const ip_addr_t* addr, void*) {
dns::start(std::move(ctx.argv[1]), [&](const char* name, const ip_addr_t* addr, void*) {
if (!addr) {
ctx.output.printf_P(PSTR("%s not found\n"), name);
return;
@ -537,7 +537,7 @@ void _terminalInitCommands() {
}
});
terminalRegisterCommand(F("NETSTAT"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("NETSTAT"), [](::terminal::CommandContext&& ctx) {
auto print = [](Print& out, tcp_pcb* list) {
for (tcp_pcb* pcb = list; pcb != nullptr; pcb = pcb->next) {
out.printf_P(PSTR("state %s local %s:%hu remote %s:%hu\n"),
@ -787,11 +787,11 @@ void terminalError(Print& print, const String& error) {
print.printf_P(PSTR("-ERROR: %s\n"), error.c_str());
}
void terminalOK(const terminal::CommandContext& ctx) {
void terminalOK(const ::terminal::CommandContext& ctx) {
terminalOK(ctx.output);
}
void terminalError(const terminal::CommandContext& ctx, const String& error) {
void terminalError(const ::terminal::CommandContext& ctx, const String& error) {
terminalError(ctx.output, error);
}


+ 1
- 1
code/espurna/terminal_commands.h View File

@ -38,7 +38,7 @@ public:
NoInput // We got nothing in the buffer and stream read() returns -1
};
using CommandFunc = void(*)(const CommandContext&);
using CommandFunc = void(*)(CommandContext&&);
using ProcessFunc = bool(*)(Result);
using Names = std::vector<const __FlashStringHelper*>;


+ 13
- 12
code/espurna/terminal_parsing.cpp View File

@ -92,8 +92,8 @@ static char hex_digit_to_int(char c) {
CommandLine parse_commandline(const char *line) {
const char *p = line;
Argv argv;
argv.reserve(4);
CommandLine out;
out.argv.reserve(4);
String current;
@ -136,11 +136,11 @@ CommandLine parse_commandline(const char *line) {
} else if (*p == '"') {
/* closing quote must be followed by a space or
* nothing at all. */
if (*(p+1) && !isspace(*(p+1))) goto err;
if (*(p+1) && !isspace(*(p+1))) goto on_error;
done=1;
} else if (!*p) {
/* unterminated quotes */
goto err;
goto on_error;
} else {
char buf[2] {*p, '\0'};
current += buf;
@ -152,11 +152,11 @@ CommandLine parse_commandline(const char *line) {
} else if (*p == '\'') {
/* closing quote must be followed by a space or
* nothing at all. */
if (*(p+1) && !isspace(*(p+1))) goto err;
if (*(p+1) && !isspace(*(p+1))) goto on_error;
done=1;
} else if (!*p) {
/* unterminated quotes */
goto err;
goto on_error;
} else {
char buf[2] {*p, '\0'};
current += buf;
@ -186,17 +186,18 @@ CommandLine parse_commandline(const char *line) {
if (*p) p++;
}
/* add the token to the vector */
argv.emplace_back(std::move(current));
out.argv.emplace_back(std::move(current));
} else {
/* Even on empty input string return something not NULL. */
goto out;
goto on_out;
}
}
err:
argv.clear();
out:
return CommandLine{std::move(argv)};
on_error:
out.argv.clear();
on_out:
return out;
}
// Fowler–Noll–Vo hash function to hash command strings that treats input as lowercase


+ 2
- 2
code/espurna/tuya.cpp View File

@ -612,7 +612,7 @@ error:
#if TERMINAL_SUPPORT
terminalRegisterCommand(F("TUYA.SHOW"), [](const terminal::CommandContext& ctx) {
terminalRegisterCommand(F("TUYA.SHOW"), [](::terminal::CommandContext&& ctx) {
ctx.output.printf_P(PSTR("Product: %s\n"), product.length() ? product.c_str() : "(unknown)");
ctx.output.print(F("\nConfig:\n"));
@ -634,7 +634,7 @@ error:
}
});
terminalRegisterCommand(F("TUYA.SAVE"), [](const terminal::CommandContext&) {
terminalRegisterCommand(F("TUYA.SAVE"), [](::terminal::CommandContext&&) {
for (auto& kv : config) {
setSetting(kv.key, kv.value);
}


+ 7
- 7
code/espurna/wifi.cpp View File

@ -1966,7 +1966,7 @@ namespace terminal {
void init() {
terminalRegisterCommand(F("WIFI.STATIONS"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("WIFI.STATIONS"), [](::terminal::CommandContext&& ctx) {
size_t stations { 0ul };
for (auto* it = wifi_softap_get_station_info(); it; it = STAILQ_NEXT(it, next), ++stations) {
ctx.output.printf_P(PSTR("%s %s\n"),
@ -1984,7 +1984,7 @@ void init() {
terminalOK(ctx);
});
terminalRegisterCommand(F("NETWORK"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("NETWORK"), [](::terminal::CommandContext&& ctx) {
for (auto& addr : addrList) {
ctx.output.printf_P(PSTR("%s%d %4s %6s "),
addr.ifname().c_str(),
@ -2021,7 +2021,7 @@ void init() {
}
});
terminalRegisterCommand(F("WIFI"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("WIFI"), [](::terminal::CommandContext&& ctx) {
const auto mode = wifi::opmode();
ctx.output.printf_P(PSTR("OPMODE: %s\n"), wifi::debug::opmode(mode).c_str());
@ -2054,23 +2054,23 @@ void init() {
terminalOK(ctx);
});
terminalRegisterCommand(F("WIFI.RESET"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("WIFI.RESET"), [](::terminal::CommandContext&& ctx) {
wifiDisconnect();
wifi::settings::configure();
terminalOK(ctx);
});
terminalRegisterCommand(F("WIFI.STA"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("WIFI.STA"), [](::terminal::CommandContext&& ctx) {
wifi::sta::toggle();
terminalOK(ctx);
});
terminalRegisterCommand(F("WIFI.AP"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("WIFI.AP"), [](::terminal::CommandContext&& ctx) {
wifi::ap::toggle();
terminalOK(ctx);
});
terminalRegisterCommand(F("WIFI.SCAN"), [](const ::terminal::CommandContext& ctx) {
terminalRegisterCommand(F("WIFI.SCAN"), [](::terminal::CommandContext&& ctx) {
wifi::sta::scan::wait(
[&](bss_info* info) {
ctx.output.printf_P(PSTR("BSSID: %s AUTH: %11s RSSI: %3hhd CH: %2hhu SSID: %s\n"),


+ 11
- 11
code/test/unit/terminal/terminal.cpp View File

@ -41,7 +41,7 @@ void test_hex_codes() {
static bool abc_done = false;
terminal::Terminal::addCommand(F("abc"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("abc"), [](::terminal::CommandContext&& ctx) {
TEST_ASSERT_EQUAL(2, ctx.argv.size());
TEST_ASSERT_EQUAL_STRING("abc", ctx.argv[0].c_str());
TEST_ASSERT_EQUAL_STRING("abc", ctx.argv[1].c_str());
@ -68,17 +68,17 @@ void test_multiple_commands() {
// set up counter to be chained between commands
static int command_calls = 0;
terminal::Terminal::addCommand(F("test1"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test1"), [](::terminal::CommandContext&& ctx) {
TEST_ASSERT_EQUAL_MESSAGE(1, ctx.argv.size(), "Command without args should have argc == 1");
TEST_ASSERT_EQUAL(0, command_calls);
command_calls = 1;
});
terminal::Terminal::addCommand(F("test2"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test2"), [](::terminal::CommandContext&& ctx) {
TEST_ASSERT_EQUAL_MESSAGE(1, ctx.argv.size(), "Command without args should have argc == 1");
TEST_ASSERT_EQUAL(1, command_calls);
command_calls = 2;
});
terminal::Terminal::addCommand(F("test3"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test3"), [](::terminal::CommandContext&& ctx) {
TEST_ASSERT_EQUAL_MESSAGE(1, ctx.argv.size(), "Command without args should have argc == 1");
TEST_ASSERT_EQUAL(2, command_calls);
command_calls = 3;
@ -113,7 +113,7 @@ void test_command() {
static int counter = 0;
terminal::Terminal::addCommand(F("test.command"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test.command"), [](::terminal::CommandContext&& ctx) {
TEST_ASSERT_EQUAL_MESSAGE(1, ctx.argv.size(), "Command without args should have argc == 1");
++counter;
});
@ -150,12 +150,12 @@ void test_command_args() {
static bool waiting = false;
terminal::Terminal::addCommand(F("test.command.arg1"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test.command.arg1"), [](::terminal::CommandContext&& ctx) {
TEST_ASSERT_EQUAL(2, ctx.argv.size());
waiting = false;
});
terminal::Terminal::addCommand(F("test.command.arg1_empty"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test.command.arg1_empty"), [](::terminal::CommandContext&& ctx) {
TEST_ASSERT_EQUAL(2, ctx.argv.size());
TEST_ASSERT(!ctx.argv[1].length());
waiting = false;
@ -192,7 +192,7 @@ void test_buffer() {
void test_quotes() {
terminal::Terminal::addCommand(F("test.quotes"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test.quotes"), [](::terminal::CommandContext&& ctx) {
for (auto& arg : ctx.argv) {
TEST_MESSAGE(arg.c_str());
}
@ -216,10 +216,10 @@ void test_quotes() {
void test_case_insensitive() {
terminal::Terminal::addCommand(F("test.lowercase1"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test.lowercase1"), [](::terminal::CommandContext&& ctx) {
TEST_FAIL_MESSAGE("`test.lowercase1` was registered first, but there's another function by the same name. This should not be called");
});
terminal::Terminal::addCommand(F("TEST.LOWERCASE1"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("TEST.LOWERCASE1"), [](::terminal::CommandContext&& ctx) {
__asm__ volatile ("nop");
});
@ -235,7 +235,7 @@ void test_case_insensitive() {
void test_output() {
terminal::Terminal::addCommand(F("test.output"), [](const terminal::CommandContext& ctx) {
terminal::Terminal::addCommand(F("test.output"), [](::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 2) return;
ctx.output.print(ctx.argv[1]);
});


Loading…
Cancel
Save