Files
espurna/code/espurna/libs/PrintString.h
Max Prokhorov b8fc8cd1fd Terminal: change command-line parser (#2247)
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.
2020-05-25 23:41:37 +03:00

60 lines
1.2 KiB
C++

/*
Arduino Print buffer. Size is fixed, unlike StreamString.
Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
*/
#pragma once
#include <Arduino.h>
#include <Print.h>
#include <core_version.h>
struct PrintString final : public Print, public String {
PrintString(size_t reserved) :
_reserved(reserved)
{
reserve(reserved);
}
size_t write(const uint8_t* data, size_t size) override {
if (!size || !data) return 0;
// we *will* receive C-strings as input
size_t want = length() + size;
if (data[size - 1] == '\0') {
size -= 1;
want -= 1;
}
if (want > _reserved) return 0;
// XXX: 2.3.0 uses str... methods that expect '0' at the end of the 'data'
// see WString{.cpp,.h} for the implementation
#if defined(ARDUINO_ESP8266_RELEASE_2_3_0)
std::copy(data, data + size, buffer + len);
len = want;
buffer[len] = '\0';
#else
concat(reinterpret_cast<const char*>(data), size);
#endif
return size;
}
size_t write(uint8_t ch) override {
if (length() + 1 > _reserved) return 0;
return concat(static_cast<char>(ch));
}
private:
const size_t _reserved;
};