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

telnet + wrench #6717

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions esphome/components/telnet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID

CODEOWNERS = ["@apbodrov"]

telnet_ns = cg.esphome_ns.namespace("telnet")
TelnetComponent = telnet_ns.class_("TelnetComponent", cg.Component)


CONFIG_SCHEMA = cv.All(
cv.Schema({cv.GenerateID(): cv.declare_id(TelnetComponent)}),
)


async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add_library("lennarthennigs/ESP Telnet", "2.2.1")
await cg.register_component(var, config)
76 changes: 76 additions & 0 deletions esphome/components/telnet/telnet_component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "telnet_component.h"
#include "wrench.h"

#include "ESPTelnet.h"

namespace esphome {
namespace telnet {

ESPTelnet telnet;
static const char *const TAG = "telnet";
uint16_t port = 23;
WRState *w;
unsigned char *outBytes; // compiled code is alloc'ed
int outLen;

void print(WRContext *c, const WRValue *argv, const int argn, WRValue &retVal, void *usr) {
char buf[1024];
for (int i = 0; i < argn; ++i) {
printf("%s", argv[i].asString(buf, 1024));
}
}

// (optional) callback functions for telnet events
void onTelnetConnect(String ip) {
ESP_LOGI(TAG, "%s connected", ip);
telnet.println("\nWelcome " + telnet.getIP());
telnet.println("(Use ^] + q to disconnect.)");
}

void onTelnetDisconnect(String ip) { ESP_LOGI(TAG, "%s disconnected", ip); }

void onTelnetReconnect(String ip) { ESP_LOGI(TAG, "%s reconnected", ip); }

void onTelnetConnectionAttempt(String ip) { ESP_LOGI(TAG, "%s tried to connected", ip); }

void onTelnetInput(String str) {
// checks for a certain command
if (str == "ping") {
telnet.println("> pong");
ESP_LOGD(TAG, "- Telnet: pong");
// disconnect the client
} else if (str == "bye") {
ESP_LOGI(TAG, "client disconnected");
telnet.println("> disconnecting you...");
telnet.disconnectClient();
} else {
int err = wr_compile(str.c_str(), strlen(str.c_str()), &outBytes, &outLen); // compile it
if (err == 0) {
wr_run(w, outBytes, outLen); // load and run the code!
delete[] outBytes; // clean up
}
}
}

void TelnetComponent::loop() { telnet.loop(); }

void TelnetComponent::setup() {
// passing on functions for various telnet events
telnet.onConnect(onTelnetConnect);
telnet.onConnectionAttempt(onTelnetConnectionAttempt);
telnet.onReconnect(onTelnetReconnect);
telnet.onDisconnect(onTelnetDisconnect);
telnet.onInputReceived(onTelnetInput);

Serial.print("- Telnet: ");
if (telnet.begin(port)) {
ESP_LOGI(TAG, "Telnet running");
} else {
ESP_LOGE(TAG, "Telnet error");
}
w = wr_newState(); // create the state
wr_registerFunction(w, "print", print); // bind a function
}

} // namespace telnet
} // namespace esphome
15 changes: 15 additions & 0 deletions esphome/components/telnet/telnet_component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "esphome/core/component.h"

namespace esphome {
namespace telnet {

class TelnetComponent : public Component {
public:
void loop() override;
void setup() override;
};

} // namespace telnet
} // namespace esphome