add uart_mitm component

This commit is contained in:
Samuel Sieb
2023-01-17 14:04:51 -08:00
parent e71fec0a45
commit 7dbe88a03c
4 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
# component that acts as a mitm for two uart ports
Two configured uarts are required.
Example:
```yaml
uart_mitm:
uart1: uart1
uart2: uart2
```

View File

@@ -0,0 +1,29 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import CONF_ID
DEPENDENCIES = ['uart']
serial_ns = cg.esphome_ns.namespace('serial')
UARTMITM = serial_ns.class_('UARTMITM', cg.Component)
CONF_UART1 = "uart1"
CONF_UART2 = "uart2"
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(UARTMITM),
cv.Required(CONF_UART1): cv.use_id(uart.UARTComponent),
cv.Required(CONF_UART2): cv.use_id(uart.UARTComponent),
})
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
uart1 = await cg.get_variable(config[CONF_UART1])
cg.add(var.set_uart1(uart1))
uart2 = await cg.get_variable(config[CONF_UART2])
cg.add(var.set_uart2(uart2))

View File

@@ -0,0 +1,24 @@
#include "uart_mitm.h"
#include "esphome/core/log.h"
namespace esphome {
namespace serial {
static const char *const TAG = "uart_mitm";
void UARTMITM::loop() {
uint8_t c;
while (this->uart1_->available()) {
this->uart1_->read_byte(&c);
this->uart2_->write_byte(c);
}
while (this->uart2_->available()) {
this->uart2_->read_byte(&c);
this->uart1_->write_byte(c);
}
}
void UARTMITM::dump_config() { ESP_LOGCONFIG(TAG, "UART MITM"); }
} // namespace serial
} // namespace esphome

View File

@@ -0,0 +1,23 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace serial {
class UARTMITM : public Component {
public:
float get_setup_priority() const override { return setup_priority::LATE; }
void loop() override;
void dump_config() override;
void set_uart1(uart::UARTComponent *uart) { this->uart1_ = uart; }
void set_uart2(uart::UARTComponent *uart) { this->uart2_ = uart; }
protected:
uart::UARTComponent *uart1_;
uart::UARTComponent *uart2_;
};
} // namespace serial
} // namespace esphome