auto formatting

This commit is contained in:
Proddy
2022-09-28 20:27:48 +02:00
parent 777c9db0f6
commit 87d0db0b5c
32 changed files with 893 additions and 916 deletions

View File

@@ -49,9 +49,9 @@ void APSettingsService::startAP() {
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_AP, WIFI_BW_HT20);
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str(), _state.channel, _state.ssidHidden, _state.maxClients);
#ifdef ARDUINO_LOLIN_C3_MINI
WiFi.setTxPower(WIFI_POWER_8_5dBm); //https://www.wemos.cc/en/latest/c3/c3_mini.html#about-wifi
#endif
#ifdef ARDUINO_LOLIN_C3_MINI
WiFi.setTxPower(WIFI_POWER_8_5dBm); //https://www.wemos.cc/en/latest/c3/c3_mini.html#about-wifi
#endif
if (!_dnsServer) {
IPAddress apIp = WiFi.softAPIP();
emsesp::EMSESP::logger().info(F("Starting Access Point with captive portal on %s"), apIp.toString().c_str());

View File

@@ -56,7 +56,7 @@ class NTPSettingsService : public StatefulService<NTPSettings> {
public:
NTPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
void begin();
void begin();
static void ntp_received(struct timeval * tv);
private:
@@ -68,7 +68,6 @@ class NTPSettingsService : public StatefulService<NTPSettings> {
void WiFiEvent(WiFiEvent_t event);
void configureNTP();
void configureTime(AsyncWebServerRequest * request, JsonVariant & json);
};
#endif

View File

@@ -75,9 +75,9 @@ void NetworkSettingsService::manageSTA() {
});
WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network
#ifdef ARDUINO_LOLIN_C3_MINI
WiFi.setTxPower(WIFI_POWER_8_5dBm); //https://www.wemos.cc/en/latest/c3/c3_mini.html#about-wifi
#endif
#ifdef ARDUINO_LOLIN_C3_MINI
WiFi.setTxPower(WIFI_POWER_8_5dBm); //https://www.wemos.cc/en/latest/c3/c3_mini.html#about-wifi
#endif
}
}

View File

@@ -262,32 +262,30 @@ void Shell::loop_normal() {
} else if (esc_ == 1) { // pos1
cursor_ = line_buffer_.length();
} else if (esc_ == 11) { // F1
// TODO use flash here?
set_command_str(("help"));
set_command_str("help");
} else if (esc_ == 12) { // F2
set_command_str(("show"));
set_command_str("show");
} else if (esc_ == 13) { // F3
set_command_str(("log notice"));
set_command_str("log notice");
} else if (esc_ == 14) { // F4
set_command_str(("log info"));
set_command_str("log info");
} else if (esc_ == 15) { // F5
set_command_str(("log debug"));
set_command_str("log debug");
} else if (esc_ == 17) { // F6
set_command_str(("watch off"));
set_command_str("watch off");
} else if (esc_ == 18) { // F7
set_command_str(("watch on"));
set_command_str("watch on");
} else if (esc_ == 19) { // F8
set_command_str(("watch raw"));
set_command_str("watch raw");
} else if (esc_ == 20) { // F9
set_command_str(("call system info"));
set_command_str("call system info");
} else if (esc_ == 21) { // F10
set_command_str(("call system settings"));
set_command_str("call system settings");
} else if (esc_ == 23) { // F11
line_buffer_ = (("call send \"0B \""));
line_buffer_ = ("call send \"0B \"");
cursor_ = 1;
} else if (esc_ == 24) { // F12
set_command_str(("log debug; watch raw"));
set_command_str("log debug; watch raw");
}
esc_ = 0;
} else if (c >= '0' && (c <= '9')) { // numbers

View File

@@ -685,8 +685,8 @@ class Shell : public std::enable_shared_from_this<Shell>, public uuid::log::Hand
PasswordData(const char * password_prompt, password_function && password_function);
~PasswordData() override = default;
const char * password_prompt_; /*!< Prompt requesting password input. @since 0.1.0 */
password_function password_function_; /*!< Function to execute after password entry. @since 0.1.0 */
const char * password_prompt_; /*!< Prompt requesting password input. @since 0.1.0 */
password_function password_function_; /*!< Function to execute after password entry. @since 0.1.0 */
};
/**
@@ -761,7 +761,7 @@ class Shell : public std::enable_shared_from_this<Shell>, public uuid::log::Hand
const std::shared_ptr<const uuid::log::Message> content_; /*!< Log message content. @since 0.1.0 */
};
Shell(const Shell &) = delete;
Shell(const Shell &) = delete;
Shell & operator=(const Shell &) = delete;
/**
@@ -906,7 +906,7 @@ class Shell : public std::enable_shared_from_this<Shell>, public uuid::log::Hand
size_t maximum_log_messages_ = MAX_LOG_MESSAGES; /*!< Maximum command line length in bytes. @since 0.6.0 */
std::string line_buffer_; /*!< Command line buffer. Limited to maximum_command_line_length() bytes. @since 0.1.0 */
std::string line_old_[MAX_LINES]; /*!< old Command line buffer.*/
uint8_t line_no_ = 0;
uint8_t line_no_ = 0;
size_t maximum_command_line_length_ = MAX_COMMAND_LINE_LENGTH; /*!< Maximum command line length in bytes. @since 0.6.0 */
unsigned char previous_ = 0; /*!< Previous character that was entered on the command line. Used to detect CRLF line endings. @since 0.1.0 */
uint8_t cursor_ = 0; /*!< cursor position from end of line */
@@ -955,9 +955,9 @@ class CommandLine {
~CommandLine() = default;
#ifdef UNIT_TEST
CommandLine(CommandLine &&) = default;
CommandLine & operator=(CommandLine &&) = default;
CommandLine(const CommandLine &) __attribute__((deprecated)) = default;
CommandLine(CommandLine &&) = default;
CommandLine & operator=(CommandLine &&) = default;
CommandLine(const CommandLine &) __attribute__((deprecated)) = default;
CommandLine & operator=(const CommandLine &) __attribute__((deprecated)) = default;
#endif
@@ -1279,8 +1279,8 @@ class Commands {
*/
void add_command(unsigned int context,
unsigned int flags,
const string_vector & name,
const string_vector & arguments,
const string_vector & name,
const string_vector & arguments,
command_function function,
argument_completion_function arg_function);
@@ -1344,11 +1344,7 @@ class Commands {
* completions for this command.
* @since 0.1.0
*/
Command(unsigned int flags,
const string_vector name,
const string_vector arguments,
command_function function,
argument_completion_function arg_function);
Command(unsigned int flags, const string_vector name, const string_vector arguments, command_function function, argument_completion_function arg_function);
~Command();
/**
@@ -1372,13 +1368,13 @@ class Commands {
}
unsigned int flags_; /*!< Shell flags that must be set for this command to be available. @since 0.1.0 */
const string_vector name_; /*!< Name of the command as a std::vector of flash strings. @since 0.1.0 */
const string_vector arguments_; /*!< Help text for arguments that the command accepts as a std::vector of flash strings. @since 0.1.0 */
const string_vector name_; /*!< Name of the command as a std::vector of flash strings. @since 0.1.0 */
const string_vector arguments_; /*!< Help text for arguments that the command accepts as a std::vector of flash strings. @since 0.1.0 */
command_function function_; /*!< Function to be used when the command is executed. @since 0.1.0 */
argument_completion_function arg_function_; /*!< Function to be used to perform argument completions for this command. @since 0.1.0 */
private:
Command(const Command &) = delete;
Command(const Command &) = delete;
Command & operator=(const Command &) = delete;
};
@@ -1487,7 +1483,7 @@ class StreamConsole : virtual public Shell {
explicit StreamConsole(Stream & stream);
private:
StreamConsole(const StreamConsole &) = delete;
StreamConsole(const StreamConsole &) = delete;
StreamConsole & operator=(const StreamConsole &) = delete;
/**

View File

@@ -626,8 +626,8 @@ class Logger {
static std::map<Handler *, Level> handlers_; /*!< Registered log handlers. @since 1.0.0 */
static Level level_; /*!< Minimum global log level across all handlers. @since 1.0.0 */
const char * name_; /*!< Logger name (flash string). @since 1.0.0 */
const Facility facility_; /*!< Default logging facility for messages. @since 1.0.0 */
const char * name_; /*!< Logger name (flash string). @since 1.0.0 */
const Facility facility_; /*!< Default logging facility for messages. @since 1.0.0 */
};
} // namespace log

View File

@@ -28,329 +28,322 @@ namespace uuid {
namespace telnet {
TelnetStream::TelnetStream(WiFiClient &client)
: client_(client) {
output_buffer_.reserve(BUFFER_SIZE);
TelnetStream::TelnetStream(WiFiClient & client)
: client_(client) {
output_buffer_.reserve(BUFFER_SIZE);
}
void TelnetStream::start() {
raw_write({
IAC, WILL, OPT_ECHO,
IAC, WILL, OPT_BINARY,
IAC, WILL, OPT_SGA,
IAC, DONT, OPT_ECHO,
IAC, DO, OPT_BINARY,
IAC, DO, OPT_SGA
});
raw_write({IAC, WILL, OPT_ECHO, IAC, WILL, OPT_BINARY, IAC, WILL, OPT_SGA, IAC, DONT, OPT_ECHO, IAC, DO, OPT_BINARY, IAC, DO, OPT_SGA});
}
int TelnetStream::available() {
if (peek() == -1) {
return 0;
} else {
return 1;
}
if (peek() == -1) {
return 0;
} else {
return 1;
}
}
int TelnetStream::read() {
if (peek_ != -1) {
int data = peek_;
peek_ = -1;
return data;
}
if (peek_ != -1) {
int data = peek_;
peek_ = -1;
return data;
}
buffer_flush();
buffer_flush();
restart:
int data = raw_read();
int data = raw_read();
if (data == -1) {
return -1;
}
if (data == -1) {
return -1;
}
unsigned char c = data;
unsigned char c = data;
if (sub_negotiation_) {
if (previous_raw_in_ == IAC) {
switch (c) {
case SE:
sub_negotiation_ = false;
previous_raw_in_ = 0;
goto restart;
if (sub_negotiation_) {
if (previous_raw_in_ == IAC) {
switch (c) {
case SE:
sub_negotiation_ = false;
previous_raw_in_ = 0;
goto restart;
case IAC:
previous_raw_in_ = 0;
goto restart;
}
} else {
switch (c) {
case IAC:
previous_raw_in_ = c;
goto restart;
case IAC:
previous_raw_in_ = 0;
goto restart;
}
} else {
switch (c) {
case IAC:
previous_raw_in_ = c;
goto restart;
default:
previous_raw_in_ = 0;
goto restart;
}
}
} else {
if (previous_raw_in_ == IAC) {
switch (c) {
case IP:
// Interrupt (^C)
previous_raw_in_ = 0;
c = '\x03';
break;
default:
previous_raw_in_ = 0;
goto restart;
}
}
} else {
if (previous_raw_in_ == IAC) {
switch (c) {
case IP:
// Interrupt (^C)
previous_raw_in_ = 0;
c = '\x03';
break;
case EC:
// Backspace (^H)
previous_raw_in_ = 0;
c = '\x08';
break;
case EC:
// Backspace (^H)
previous_raw_in_ = 0;
c = '\x08';
break;
case EL:
// Delete line (^U)
previous_raw_in_ = 0;
c = '\x15';
break;
case EL:
// Delete line (^U)
previous_raw_in_ = 0;
c = '\x15';
break;
case IAC:
previous_raw_in_ = 0;
break;
case IAC:
previous_raw_in_ = 0;
break;
case SB:
case WILL:
case WONT:
case DO:
case DONT:
previous_raw_in_ = c;
goto restart;
case SB:
case WILL:
case WONT:
case DO:
case DONT:
previous_raw_in_ = c;
goto restart;
case SE:
case DM:
case BRK:
case AO:
case AYT:
case GA:
case NOP:
default:
previous_raw_in_ = 0;
goto restart;
}
} else if (previous_raw_in_ == SB) {
sub_negotiation_ = true;
previous_raw_in_ = 0;
goto restart;
} else if (previous_raw_in_ == WILL || previous_raw_in_ == WONT) {
switch (c) {
case OPT_ECHO:
// Don't do these
raw_write({IAC, DONT, c});
break;
case SE:
case DM:
case BRK:
case AO:
case AYT:
case GA:
case NOP:
default:
previous_raw_in_ = 0;
goto restart;
}
} else if (previous_raw_in_ == SB) {
sub_negotiation_ = true;
previous_raw_in_ = 0;
goto restart;
} else if (previous_raw_in_ == WILL || previous_raw_in_ == WONT) {
switch (c) {
case OPT_ECHO:
// Don't do these
raw_write({IAC, DONT, c});
break;
case OPT_BINARY:
case OPT_SGA:
// Do these
raw_write({IAC, DO, c});
break;
case OPT_BINARY:
case OPT_SGA:
// Do these
raw_write({IAC, DO, c});
break;
default:
// Don't do anything else
raw_write({IAC, DONT, c});
break;
}
default:
// Don't do anything else
raw_write({IAC, DONT, c});
break;
}
previous_raw_in_ = 0;
goto restart;
} else if (previous_raw_in_ == DO) {
switch (c) {
case OPT_ECHO:
case OPT_BINARY:
case OPT_SGA:
// These are always enabled
break;
previous_raw_in_ = 0;
goto restart;
} else if (previous_raw_in_ == DO) {
switch (c) {
case OPT_ECHO:
case OPT_BINARY:
case OPT_SGA:
// These are always enabled
break;
default:
// Refuse to do anything else
raw_write({IAC, WONT, c});
break;
}
default:
// Refuse to do anything else
raw_write({IAC, WONT, c});
break;
}
previous_raw_in_ = 0;
goto restart;
} else if (previous_raw_in_ == DONT) {
switch (c) {
case OPT_ECHO:
case OPT_BINARY:
case OPT_SGA:
// Insist that we do these
raw_write({IAC, WILL, c});
break;
previous_raw_in_ = 0;
goto restart;
} else if (previous_raw_in_ == DONT) {
switch (c) {
case OPT_ECHO:
case OPT_BINARY:
case OPT_SGA:
// Insist that we do these
raw_write({IAC, WILL, c});
break;
default:
// Everything else is always disabled
break;
}
default:
// Everything else is always disabled
break;
}
previous_raw_in_ = 0;
goto restart;
} else {
switch (c) {
case IAC:
previous_raw_in_ = c;
goto restart;
previous_raw_in_ = 0;
goto restart;
} else {
switch (c) {
case IAC:
previous_raw_in_ = c;
goto restart;
default:
previous_raw_in_ = 0;
break;
}
}
}
default:
previous_raw_in_ = 0;
break;
}
}
}
if (previous_in_ == CR) {
if (c == NUL) {
previous_in_ = 0;
goto restart;
}
}
if (previous_in_ == CR) {
if (c == NUL) {
previous_in_ = 0;
goto restart;
}
}
previous_in_ = c;
return c;
previous_in_ = c;
return c;
}
int TelnetStream::peek() {
buffer_flush();
buffer_flush();
// It's too complicated to implement this by calling peek()
// on the original stream, especially if the original stream
// doesn't actually support peeking.
if (peek_ == -1) {
peek_ = read();
}
// It's too complicated to implement this by calling peek()
// on the original stream, especially if the original stream
// doesn't actually support peeking.
if (peek_ == -1) {
peek_ = read();
}
return peek_;
return peek_;
}
size_t TelnetStream::write(uint8_t data) {
if (previous_out_ == CR && data != LF) {
previous_out_ = data;
if (previous_out_ == CR && data != LF) {
previous_out_ = data;
if (raw_write({NUL, data}) != 2) {
return 0;
}
} else {
previous_out_ = data;
}
if (raw_write({NUL, data}) != 2) {
return 0;
}
} else {
previous_out_ = data;
}
if (data == IAC) {
if (raw_write({IAC, IAC}) != 2) {
return 0;
}
} else {
if (raw_write(data) != 1) {
return 0;
}
}
if (data == IAC) {
if (raw_write({IAC, IAC}) != 2) {
return 0;
}
} else {
if (raw_write(data) != 1) {
return 0;
}
}
return 1;
return 1;
}
size_t TelnetStream::write(const uint8_t *buffer, size_t size) {
std::vector<unsigned char> data;
data.reserve(size);
size_t TelnetStream::write(const uint8_t * buffer, size_t size) {
std::vector<unsigned char> data;
data.reserve(size);
while (size-- > 0) {
unsigned char c = *buffer++;
while (size-- > 0) {
unsigned char c = *buffer++;
if (previous_out_ == CR && c != LF) {
data.push_back((unsigned char)NUL);
}
if (previous_out_ == CR && c != LF) {
data.push_back((unsigned char)NUL);
}
if (c == IAC) {
data.push_back((unsigned char)IAC);
}
if (c == IAC) {
data.push_back((unsigned char)IAC);
}
previous_out_ = c;
data.push_back(c);
}
previous_out_ = c;
data.push_back(c);
}
size_t len = raw_write(data);
if (len < size) {
len = 0;
}
return len;
size_t len = raw_write(data);
if (len < size) {
len = 0;
}
return len;
}
void TelnetStream::flush() {
// This is a pure virtual function in Arduino's Stream class, which
// makes no sense because that class is for input and this is an
// output function. Later versions move it to Print as an empty
// virtual function so this is here for backward compatibility.
// This is a pure virtual function in Arduino's Stream class, which
// makes no sense because that class is for input and this is an
// output function. Later versions move it to Print as an empty
// virtual function so this is here for backward compatibility.
}
int TelnetStream::raw_available() {
return client_.available();
return client_.available();
}
int TelnetStream::raw_read() {
return client_.read();
return client_.read();
}
void TelnetStream::buffer_flush() {
if (!output_buffer_.empty()) {
size_t len = client_.write(reinterpret_cast<const unsigned char*>(output_buffer_.data()), output_buffer_.size());
if (len != output_buffer_.size()) {
client_.stop();
}
output_buffer_.clear();
output_buffer_.shrink_to_fit();
}
if (!output_buffer_.empty()) {
size_t len = client_.write(reinterpret_cast<const unsigned char *>(output_buffer_.data()), output_buffer_.size());
if (len != output_buffer_.size()) {
client_.stop();
}
output_buffer_.clear();
output_buffer_.shrink_to_fit();
}
}
size_t TelnetStream::raw_write(unsigned char data) {
output_buffer_.push_back(data);
output_buffer_.push_back(data);
if (output_buffer_.size() >= BUFFER_SIZE) {
buffer_flush();
}
if (output_buffer_.size() >= BUFFER_SIZE) {
buffer_flush();
}
return 1;
return 1;
}
size_t TelnetStream::raw_write(const std::vector<unsigned char> &data) {
return raw_write(reinterpret_cast<const unsigned char*>(data.data()), data.size());
size_t TelnetStream::raw_write(const std::vector<unsigned char> & data) {
return raw_write(reinterpret_cast<const unsigned char *>(data.data()), data.size());
}
size_t TelnetStream::raw_write(const uint8_t *buffer, size_t size) {
size_t offset = 0;
size_t remaining = size;
size_t TelnetStream::raw_write(const uint8_t * buffer, size_t size) {
size_t offset = 0;
size_t remaining = size;
if (!output_buffer_.empty()) {
// Fill the rest of the buffer
size_t block = std::min(remaining, BUFFER_SIZE - output_buffer_.size());
if (!output_buffer_.empty()) {
// Fill the rest of the buffer
size_t block = std::min(remaining, BUFFER_SIZE - output_buffer_.size());
output_buffer_.insert(output_buffer_.end(), buffer, buffer + block);
offset += block;
remaining -= block;
output_buffer_.insert(output_buffer_.end(), buffer, buffer + block);
offset += block;
remaining -= block;
if (output_buffer_.size() >= BUFFER_SIZE) {
buffer_flush();
}
}
if (output_buffer_.size() >= BUFFER_SIZE) {
buffer_flush();
}
}
if (remaining >= BUFFER_SIZE) {
// Output directly if it won't fit in the buffer
size_t len = client_.write(buffer + offset, remaining);
if (len != remaining) {
client_.stop();
return offset + len;
}
} else if (remaining > 0) {
// Put the rest in the buffer
output_buffer_.insert(output_buffer_.end(), buffer + offset, buffer + offset + remaining);
}
if (remaining >= BUFFER_SIZE) {
// Output directly if it won't fit in the buffer
size_t len = client_.write(buffer + offset, remaining);
if (len != remaining) {
client_.stop();
return offset + len;
}
} else if (remaining > 0) {
// Put the rest in the buffer
output_buffer_.insert(output_buffer_.end(), buffer + offset, buffer + offset + remaining);
}
return size;
return size;
}
} // namespace telnet

View File

@@ -21,9 +21,9 @@
#include <Arduino.h>
#ifdef ARDUINO_ARCH_ESP8266
# include <ESP8266WiFi.h>
#include <ESP8266WiFi.h>
#else
# include <WiFi.h>
#include <WiFi.h>
#endif
#include <WiFiUdp.h>
@@ -51,47 +51,47 @@ namespace telnet {
*
* @since 0.1.0
*/
class TelnetStream: public ::Stream {
public:
/**
class TelnetStream : public ::Stream {
public:
/**
* Create a new telnet stream wrapper.
*
* @param[in] client Client connection.
* @since 0.1.0
*/
explicit TelnetStream(WiFiClient &client);
virtual ~TelnetStream() = default;
explicit TelnetStream(WiFiClient & client);
virtual ~TelnetStream() = default;
/**
/**
* Perform initial negotiation.
*
* @since 0.1.0
*/
void start();
void start();
/**
/**
* Check for available input.
*
* @return The number of bytes available to read.
* @since 0.1.0
*/
int available() override;
/**
int available() override;
/**
* Read one byte from the available input.
*
* @return An unsigned char if input is available, otherwise -1.
* @since 0.1.0
*/
int read() override;
/**
int read() override;
/**
* Read one byte from the available input without advancing to the
* next one.
*
* @return An unsigned char if input is available, otherwise -1.
* @since 0.1.0
*/
int peek() override;
/**
int peek() override;
/**
* Write one byte to the output stream.
*
* Disconnect the client if the socket buffer is full.
@@ -100,8 +100,8 @@ public:
* @return The number of bytes that were output.
* @since 0.1.0
*/
size_t write(uint8_t data) override;
/**
size_t write(uint8_t data) override;
/**
* Write an array of bytes to the output stream.
*
* Disconnect the client if the socket buffer is full.
@@ -111,8 +111,8 @@ public:
* @return The number of bytes that were output.
* @since 0.1.0
*/
size_t write(const uint8_t *buffer, size_t size) override;
/**
size_t write(const uint8_t * buffer, size_t size) override;
/**
* Does nothing.
*
* This is a pure virtual function in Arduino's Stream class, which
@@ -122,66 +122,69 @@ public:
*
* @since 0.1.0
*/
void flush() override;
void flush() override;
private:
static constexpr const unsigned char NUL = 0; /*!< No operation. @since 0.1.0 */
static constexpr const unsigned char BEL = 7; /*!< Produces an audible or visible signal. @since 0.1.0 */
static constexpr const unsigned char BS = 8; /*!< Moves the print head one character position towards the left margin. @since 0.1.0 */
static constexpr const unsigned char HT = 9; /*!< Moves the printer to the next horizontal tab stop. @since 0.1.0 */
static constexpr const unsigned char LF = 10; /*!< Line Feed. @since 0.1.0 */
static constexpr const unsigned char VT = 11; /*!< Moves the printer to the next vertical tab stop. @since 0.1.0 */
static constexpr const unsigned char FF = 12; /*!< Moves the printer to the top of the next page, keeping the same horizontal position. @since 0.1.0 */
static constexpr const unsigned char CR = 13; /*!< Carriage Return. @since 0.1.0 */
static constexpr const unsigned char SE = 240; /*!< End of sub-negotiation parameters. @since 0.1.0 */
static constexpr const unsigned char NOP = 241; /*!< No operation. @since 0.1.0 */
static constexpr const unsigned char DM = 242; /*!< The data stream portion of a Synch. @since 0.1.0 */
static constexpr const unsigned char BRK = 243; /*!< NVT character BRK. @since 0.1.0 */
static constexpr const unsigned char IP = 244; /*!< Interrupt Process function. @since 0.1.0 */
static constexpr const unsigned char AO = 245; /*!< Abort Output function. @since 0.1.0 */
static constexpr const unsigned char AYT = 246; /*!< Are You There function. @since 0.1.0 */
static constexpr const unsigned char EC = 247; /*!< Erase Character function. @since 0.1.0 */
static constexpr const unsigned char EL = 248; /*!< Erase Line function. @since 0.1.0 */
static constexpr const unsigned char GA = 249; /*!< Go Ahead signal. @since 0.1.0 */
static constexpr const unsigned char SB = 250; /*!< Sub-negotiation of the indicated option. @since 0.1.0 */
static constexpr const unsigned char WILL = 251; /*!< Indicates the desire to begin performing, or confirmation that you are now performing, the indicated option. @since 0.1.0 */
static constexpr const unsigned char WONT = 252; /*!< Indicates the refusal to perform, or continue performing, the indicated option. @since 0.1.0 */
static constexpr const unsigned char DO = 253; /*!< Indicates the request that the other party perform, or confirmation that you are expecting the other party to perform, the indicated option. @since 0.1.0 */
static constexpr const unsigned char DONT = 254; /*!< Indicates the demand that the other party stop performing, or confirmation that you are no longer expecting the other party to perform, the indicated option. @since 0.1.0 */
static constexpr const unsigned char IAC = 255; /*!< Interpret As Command escape character. @since 0.1.0 */
private:
static constexpr const unsigned char NUL = 0; /*!< No operation. @since 0.1.0 */
static constexpr const unsigned char BEL = 7; /*!< Produces an audible or visible signal. @since 0.1.0 */
static constexpr const unsigned char BS = 8; /*!< Moves the print head one character position towards the left margin. @since 0.1.0 */
static constexpr const unsigned char HT = 9; /*!< Moves the printer to the next horizontal tab stop. @since 0.1.0 */
static constexpr const unsigned char LF = 10; /*!< Line Feed. @since 0.1.0 */
static constexpr const unsigned char VT = 11; /*!< Moves the printer to the next vertical tab stop. @since 0.1.0 */
static constexpr const unsigned char FF = 12; /*!< Moves the printer to the top of the next page, keeping the same horizontal position. @since 0.1.0 */
static constexpr const unsigned char CR = 13; /*!< Carriage Return. @since 0.1.0 */
static constexpr const unsigned char SE = 240; /*!< End of sub-negotiation parameters. @since 0.1.0 */
static constexpr const unsigned char NOP = 241; /*!< No operation. @since 0.1.0 */
static constexpr const unsigned char DM = 242; /*!< The data stream portion of a Synch. @since 0.1.0 */
static constexpr const unsigned char BRK = 243; /*!< NVT character BRK. @since 0.1.0 */
static constexpr const unsigned char IP = 244; /*!< Interrupt Process function. @since 0.1.0 */
static constexpr const unsigned char AO = 245; /*!< Abort Output function. @since 0.1.0 */
static constexpr const unsigned char AYT = 246; /*!< Are You There function. @since 0.1.0 */
static constexpr const unsigned char EC = 247; /*!< Erase Character function. @since 0.1.0 */
static constexpr const unsigned char EL = 248; /*!< Erase Line function. @since 0.1.0 */
static constexpr const unsigned char GA = 249; /*!< Go Ahead signal. @since 0.1.0 */
static constexpr const unsigned char SB = 250; /*!< Sub-negotiation of the indicated option. @since 0.1.0 */
static constexpr const unsigned char WILL =
251; /*!< Indicates the desire to begin performing, or confirmation that you are now performing, the indicated option. @since 0.1.0 */
static constexpr const unsigned char WONT = 252; /*!< Indicates the refusal to perform, or continue performing, the indicated option. @since 0.1.0 */
static constexpr const unsigned char DO =
253; /*!< Indicates the request that the other party perform, or confirmation that you are expecting the other party to perform, the indicated option. @since 0.1.0 */
static constexpr const unsigned char DONT =
254; /*!< Indicates the demand that the other party stop performing, or confirmation that you are no longer expecting the other party to perform, the indicated option. @since 0.1.0 */
static constexpr const unsigned char IAC = 255; /*!< Interpret As Command escape character. @since 0.1.0 */
static constexpr const unsigned char OPT_BINARY = 0; /*!< Binary (8-bit) transmission mode. (RFC 856). @since 0.1.0 */
static constexpr const unsigned char OPT_ECHO = 1; /*!< Remote Echo (RFC 857). @since 0.1.0 */
static constexpr const unsigned char OPT_SGA = 3; /*!< Suppress Go Ahead (RFC 858). @since 0.1.0 */
static constexpr const unsigned char OPT_BINARY = 0; /*!< Binary (8-bit) transmission mode. (RFC 856). @since 0.1.0 */
static constexpr const unsigned char OPT_ECHO = 1; /*!< Remote Echo (RFC 857). @since 0.1.0 */
static constexpr const unsigned char OPT_SGA = 3; /*!< Suppress Go Ahead (RFC 858). @since 0.1.0 */
static constexpr const size_t BUFFER_SIZE = 536; /*!< Output buffer size. @since 0.1.0 */
static constexpr const size_t BUFFER_SIZE = 536; /*!< Output buffer size. @since 0.1.0 */
TelnetStream(const TelnetStream&) = delete;
TelnetStream& operator=(const TelnetStream&) = delete;
TelnetStream(const TelnetStream &) = delete;
TelnetStream & operator=(const TelnetStream &) = delete;
/**
/**
* Directly check for available input.
*
* @return The number of bytes available to read.
* @since 0.1.0
*/
int raw_available();
/**
int raw_available();
/**
* Read one byte directly from the available input.
*
* @return An unsigned char if input is available, otherwise -1.
* @since 0.1.0
*/
int raw_read();
/**
int raw_read();
/**
* Flush output stream buffer.
*
* Disconnect the client if the socket buffer is full.
*
* @since 0.1.0
*/
void buffer_flush();
/**
void buffer_flush();
/**
* Write one byte directly to the output stream.
*
* Disconnect the client if the socket buffer is full.
@@ -190,8 +193,8 @@ private:
* @return The number of bytes that were output.
* @since 0.1.0
*/
size_t raw_write(unsigned char data);
/**
size_t raw_write(unsigned char data);
/**
* Write a vector of bytes directly to the output stream.
*
* Disconnect the client if the socket buffer is full.
@@ -200,8 +203,8 @@ private:
* @return The number of bytes that were output.
* @since 0.1.0
*/
size_t raw_write(const std::vector<unsigned char> &data);
/**
size_t raw_write(const std::vector<unsigned char> & data);
/**
* Write an array of bytes directly to the output stream.
*
* Disconnect the client if the socket buffer is full.
@@ -211,15 +214,15 @@ private:
* @return The number of bytes that were output.
* @since 0.1.0
*/
size_t raw_write(const uint8_t *buffer, size_t size);
size_t raw_write(const uint8_t * buffer, size_t size);
WiFiClient &client_; /*!< Client connection. @since 0.1.0 */
unsigned char previous_raw_in_ = 0; /*!< Previous raw character that was received. Used to detect commands. @since 0.1.0 */
bool sub_negotiation_ = false; /*!< Sub-negotiation mode. @since 0.1.0 */
unsigned char previous_in_ = 0; /*!< Previous character that was received. Used to detect CR NUL. @since 0.1.0 */
unsigned char previous_out_ = 0; /*!< Previous character that was sent. Used to insert NUL after CR without LF. @since 0.1.0 */
int peek_ = -1; /*!< Previously read data cached by peek(). @since 0.1.0 */
std::vector<char> output_buffer_; /*!< Buffer data to be output until a read function is called. @since 0.1.0 */
WiFiClient & client_; /*!< Client connection. @since 0.1.0 */
unsigned char previous_raw_in_ = 0; /*!< Previous raw character that was received. Used to detect commands. @since 0.1.0 */
bool sub_negotiation_ = false; /*!< Sub-negotiation mode. @since 0.1.0 */
unsigned char previous_in_ = 0; /*!< Previous character that was received. Used to detect CR NUL. @since 0.1.0 */
unsigned char previous_out_ = 0; /*!< Previous character that was sent. Used to insert NUL after CR without LF. @since 0.1.0 */
int peek_ = -1; /*!< Previously read data cached by peek(). @since 0.1.0 */
std::vector<char> output_buffer_; /*!< Buffer data to be output until a read function is called. @since 0.1.0 */
};
/**
@@ -228,13 +231,13 @@ private:
* @since 0.1.0
*/
class TelnetService {
public:
static constexpr size_t MAX_CONNECTIONS = 3; /*!< Maximum number of concurrent open connections. @since 0.1.0 */
static constexpr uint16_t DEFAULT_PORT = 23; /*!< Default TCP port to listen on. @since 0.1.0 */
static constexpr unsigned long DEFAULT_IDLE_TIMEOUT = 600; /*!< Default initial idle timeout (in seconds). @since 0.1.0 */
static constexpr unsigned long DEFAULT_WRITE_TIMEOUT = 0; /*!< Default write timeout (in milliseconds). @ since 0.1.0 */
public:
static constexpr size_t MAX_CONNECTIONS = 3; /*!< Maximum number of concurrent open connections. @since 0.1.0 */
static constexpr uint16_t DEFAULT_PORT = 23; /*!< Default TCP port to listen on. @since 0.1.0 */
static constexpr unsigned long DEFAULT_IDLE_TIMEOUT = 600; /*!< Default initial idle timeout (in seconds). @since 0.1.0 */
static constexpr unsigned long DEFAULT_WRITE_TIMEOUT = 0; /*!< Default write timeout (in milliseconds). @ since 0.1.0 */
/**
/**
* Function to handle the creation of a shell.
*
* @param[in] stream Stream for the telnet connection.
@@ -242,9 +245,9 @@ public:
* @param[in] port Remote port.
* @since 0.1.0
*/
using shell_factory_function = std::function<std::shared_ptr<uuid::console::Shell>(Stream &stream, const IPAddress &addr, uint16_t port)>;
using shell_factory_function = std::function<std::shared_ptr<uuid::console::Shell>(Stream & stream, const IPAddress & addr, uint16_t port)>;
/**
/**
* Create a new telnet service listening on the default port.
*
* @param[in] commands Commands available for execution in shells.
@@ -252,9 +255,9 @@ public:
* @param[in] flags Initial flags for shells.
* @since 0.1.0
*/
TelnetService(std::shared_ptr<uuid::console::Commands> commands, unsigned int context = 0, unsigned int flags = 0);
TelnetService(std::shared_ptr<uuid::console::Commands> commands, unsigned int context = 0, unsigned int flags = 0);
/**
/**
* Create a new telnet service listening on a specific port.
*
* @param[in] port TCP listening port.
@@ -263,74 +266,74 @@ public:
* @param[in] flags Initial flags for shells.
* @since 0.1.0
*/
TelnetService(uint16_t port, std::shared_ptr<uuid::console::Commands> commands, unsigned int context = 0, unsigned int flags = 0);
TelnetService(uint16_t port, std::shared_ptr<uuid::console::Commands> commands, unsigned int context = 0, unsigned int flags = 0);
/**
/**
* Create a new telnet service listening on the default port.
*
* @param[in] shell_factory Function to create a shell for new connections.
* @since 0.1.0
*/
explicit TelnetService(shell_factory_function shell_factory);
explicit TelnetService(shell_factory_function shell_factory);
/**
/**
* Create a new telnet service listening on a specific port.
*
* @param[in] port TCP listening port.
* @param[in] shell_factory Function to create a shell for new connections.
* @since 0.1.0
*/
TelnetService(uint16_t port, shell_factory_function shell_factory);
TelnetService(uint16_t port, shell_factory_function shell_factory);
~TelnetService() = default;
~TelnetService() = default;
/**
/**
* Start listening for connections on the configured port.
*
* @since 0.1.0
*/
void start();
/**
void start();
/**
* Close all connections.
*
* The listening status is not affected.
*
* @since 0.1.0
*/
void close_all();
/**
void close_all();
/**
* Stop listening for connections.
*
* Existing connections are not affected.
*
* @since 0.1.0
*/
void stop();
void stop();
/**
/**
* Get the maximum number of concurrent open connections.
*
* @return The maximum number of concurrent open connections.
* @since 0.1.0
*/
size_t maximum_connections() const;
/**
size_t maximum_connections() const;
/**
* Set the maximum number of concurrent open connections.
*
* Defaults to TelnetService::MAX_CONNECTIONS.
*
* @since 0.1.0
*/
void maximum_connections(size_t count);
void maximum_connections(size_t count);
/**
/**
* Get the initial idle timeout for new connections.
*
* @return The initial idle timeout in seconds (or 0 for disabled).
* @since 0.1.0
*/
unsigned long initial_idle_timeout() const;
/**
unsigned long initial_idle_timeout() const;
/**
* Set the initial idle timeout for new connections.
*
* Defaults to TelnetService::DEFAULT_IDLE_TIMEOUT.
@@ -338,17 +341,17 @@ public:
* @param[in] timeout Idle timeout in seconds (or 0 to disable).
* @since 0.1.0
*/
void initial_idle_timeout(unsigned long timeout);
void initial_idle_timeout(unsigned long timeout);
/**
/**
* Get the default socket write timeout for new connections.
*
* @return The default socket write timeout in seconds (or 0 for
* platform default).
* @since 0.1.0
*/
unsigned long default_write_timeout() const;
/**
unsigned long default_write_timeout() const;
/**
* Set the default socket write timeout for new connections.
*
* Defaults to TelnetService::DEFAULT_WRITE_TIMEOUT (platform
@@ -358,26 +361,26 @@ public:
* platform default).
* @since 0.1.0
*/
void default_write_timeout(unsigned long timeout);
void default_write_timeout(unsigned long timeout);
/**
/**
* Accept new connections.
*
* @since 0.1.0
*/
void loop();
void loop();
private:
/**
private:
/**
* Telnet connection.
*
* Holds the client and stream instance for the lifetime of the shell.
*
* @since 0.1.0
*/
class Connection {
public:
/**
class Connection {
public:
/**
* Create a telnet connection shell.
*
* @param[in] shell_factory Function to create a shell for new connections.
@@ -386,52 +389,52 @@ private:
* @param[in] write_timeout Idle timeout in milliseconds.
* @since 0.1.0
*/
Connection(shell_factory_function &shell_factory, WiFiClient &&client, unsigned long idle_timeout, unsigned long write_timeout);
~Connection() = default;
Connection(shell_factory_function & shell_factory, WiFiClient && client, unsigned long idle_timeout, unsigned long write_timeout);
~Connection() = default;
/**
/**
* Check if the shell is still active.
*
* @return Active status of the shell.
* @since 0.1.0
*/
bool active();
/**
bool active();
/**
* Stop the shell if the client is not connected.
*
* @return Active status of the shell.
* @since 0.1.0
*/
bool loop();
/**
bool loop();
/**
* Stop the shell.
*
* @since 0.1.0
*/
void stop();
void stop();
private:
Connection(const Connection&) = delete;
Connection& operator=(const Connection&) = delete;
private:
Connection(const Connection &) = delete;
Connection & operator=(const Connection &) = delete;
WiFiClient client_; /*!< Client connection. @since 0.1.0 */
TelnetStream stream_; /*!< Telnet stream for the connection. @since 0.1.0 */
std::shared_ptr<uuid::console::Shell> shell_; /*!< Shell for connection. @since 0.1.0 */
IPAddress addr_; /*!< Remote address of connection. @since 0.1.0 */
uint16_t port_; /*!< Remote port of connection. @since 0.1.0 */
};
WiFiClient client_; /*!< Client connection. @since 0.1.0 */
TelnetStream stream_; /*!< Telnet stream for the connection. @since 0.1.0 */
std::shared_ptr<uuid::console::Shell> shell_; /*!< Shell for connection. @since 0.1.0 */
IPAddress addr_; /*!< Remote address of connection. @since 0.1.0 */
uint16_t port_; /*!< Remote port of connection. @since 0.1.0 */
};
TelnetService(const TelnetService&) = delete;
TelnetService& operator=(const TelnetService&) = delete;
TelnetService(const TelnetService &) = delete;
TelnetService & operator=(const TelnetService &) = delete;
static uuid::log::Logger logger_; /*!< uuid::log::Logger instance for telnet services. @since 0.1.0 */
static uuid::log::Logger logger_; /*!< uuid::log::Logger instance for telnet services. @since 0.1.0 */
WiFiServer server_; /*!< TCP server. @since 0.1.0 */
size_t maximum_connections_ = MAX_CONNECTIONS; /*!< Maximum number of concurrent open connections. @since 0.1.0 */
std::list<Connection> connections_; /*!< Open connections. @since 0.1.0 */
shell_factory_function shell_factory_; /*!< Function to create a shell. @since 0.1.0 */
unsigned long initial_idle_timeout_ = DEFAULT_IDLE_TIMEOUT; /*!< Initial idle timeout (in seconds). @since 0.1.0 */
unsigned long write_timeout_ = DEFAULT_WRITE_TIMEOUT; /*!< Write timeout (in milliseconds). @since 0.1.0 */
WiFiServer server_; /*!< TCP server. @since 0.1.0 */
size_t maximum_connections_ = MAX_CONNECTIONS; /*!< Maximum number of concurrent open connections. @since 0.1.0 */
std::list<Connection> connections_; /*!< Open connections. @since 0.1.0 */
shell_factory_function shell_factory_; /*!< Function to create a shell. @since 0.1.0 */
unsigned long initial_idle_timeout_ = DEFAULT_IDLE_TIMEOUT; /*!< Initial idle timeout (in seconds). @since 0.1.0 */
unsigned long write_timeout_ = DEFAULT_WRITE_TIMEOUT; /*!< Write timeout (in milliseconds). @since 0.1.0 */
};
} // namespace telnet