Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'banner' option #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ stream_server:
port: 1234
```

Welcome/MOTD Message
--------------------

The optional 'banner' property allows to specify a string text to be always sent to client upon connecting.
This is very useful for things like always forcing telnet client into character mode.
Example:

```yaml
stream_server:
banner: "\xFF\xFB\x01\xFF\xFB\x03\xFF\xFC\x34\r\nWelcome to my console"
```

The example above sends a special sequence to telnet that disables local echo and enables character mode.

Sensors
-------
The server provides a binary sensor that signals whether there currently is a client connected:
Expand Down
7 changes: 6 additions & 1 deletion components/stream_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# ESPHome doesn't know the Stream abstraction yet, so hardcode to use a UART for now.

AUTO_LOAD = ["socket"]
CONF_BANNER = "banner"

DEPENDENCIES = ["uart", "network"]

Expand All @@ -30,6 +31,9 @@ def validate_buffer_size(buffer_size):
cv.Optional(CONF_BUFFER_SIZE, default=128): cv.All(
cv.positive_int, validate_buffer_size
),
cv.Optional(CONF_BANNER): cv.All(
cv.string
)
}
)
.extend(cv.COMPONENT_SCHEMA)
Expand All @@ -41,6 +45,7 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_port(config[CONF_PORT]))
cg.add(var.set_buffer_size(config[CONF_BUFFER_SIZE]))

if CONF_BANNER in config:
cg.add(var.set_banner(config[CONF_BANNER]))
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
5 changes: 5 additions & 0 deletions components/stream_server/stream_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ void StreamServerComponent::accept() {
if (!socket)
return;

if (banner_ && strlen(banner_)) {
socket->write(banner_, strlen(banner_));
}

socket->setblocking(false);

std::string identifier = socket->getpeername();
this->clients_.emplace_back(std::move(socket), identifier, this->buf_head_);
ESP_LOGD(TAG, "New client connected from %s", identifier.c_str());
Expand Down
3 changes: 3 additions & 0 deletions components/stream_server/stream_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class StreamServerComponent : public esphome::Component {
void set_connection_count_sensor(esphome::sensor::Sensor *connection_count) { this->connection_count_sensor_ = connection_count; }
#endif

void set_banner(const char *banner) { this->banner_ = banner; };

void setup() override;
void loop() override;
void dump_config() override;
Expand Down Expand Up @@ -63,6 +65,7 @@ class StreamServerComponent : public esphome::Component {
esphome::uart::UARTComponent *stream_{nullptr};
uint16_t port_;
size_t buf_size_;
const char *banner_ = NULL;

#ifdef USE_BINARY_SENSOR
esphome::binary_sensor::BinarySensor *connected_sensor_;
Expand Down