ESPHome 2026.3.0
Loading...
Searching...
No Matches
serial_proxy.h
Go to the documentation of this file.
1#pragma once
2
3// WARNING: This component is EXPERIMENTAL. The API may change at any time
4// without following the normal breaking changes policy. Use at your own risk.
5// Once the API is considered stable, this warning will be removed.
6
8
9#ifdef USE_SERIAL_PROXY
10
12#include "esphome/core/hal.h"
14
15// Include api_pb2.h only when the API is enabled. The full include is needed
16// to hold SerialProxyDataReceived by value as a pre-allocated member.
17// Guarding prevents pulling conflicting Zephyr logging macro names into
18// translation units that include this header without USE_API defined.
19#ifdef USE_API
21#endif
22
23// Forward-declare types needed outside the USE_API guard.
24namespace esphome::api {
25class APIConnection;
26namespace enums {
29} // namespace enums
30} // namespace esphome::api
31
32namespace esphome::serial_proxy {
33
40
42inline constexpr size_t SERIAL_PROXY_MAX_READ_SIZE = 256;
43
44class SerialProxy : public uart::UARTDevice, public Component {
45 public:
46 void setup() override;
47 void loop() override;
48 void dump_config() override;
49 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
50
53
55 void set_instance_index(uint32_t index) { this->instance_index_ = index; }
56
58 void set_name(const char *name) { this->name_ = name; }
59
61 const char *get_name() const { return this->name_; }
62
64 void set_port_type(api::enums::SerialProxyPortType port_type) { this->port_type_ = port_type; }
65
68
75 void configure(uint32_t baudrate, bool flow_control, uint8_t parity, uint8_t stop_bits, uint8_t data_size);
76
79
82
86 void write_from_client(const uint8_t *data, size_t len);
87
89 void set_modem_pins(uint32_t line_states);
90
93
96
98 void set_rts_pin(GPIOPin *pin) { this->rts_pin_ = pin; }
99
101 void set_dtr_pin(GPIOPin *pin) { this->dtr_pin_ = pin; }
102
103 protected:
104#ifdef USE_API
107#endif
108
111
114
115#ifdef USE_API
118#endif
119
121 const char *name_{nullptr};
122
125
127 GPIOPin *rts_pin_{nullptr};
128 GPIOPin *dtr_pin_{nullptr};
129
131 bool rts_state_{false};
132 bool dtr_state_{false};
133};
134
135} // namespace esphome::serial_proxy
136
137#endif // USE_SERIAL_PROXY
float get_setup_priority() const override
void set_dtr_pin(GPIOPin *pin)
Set the DTR GPIO pin (from YAML configuration)
void read_and_send_(size_t available)
Read from UART and send to API client (slow path with 256-byte stack buffer)
uint32_t get_modem_pins() const
Get current modem pin states as a bitmask of SerialProxyLineStateFlag values.
uint32_t instance_index_
Instance index for identifying this proxy in API messages.
void set_port_type(api::enums::SerialProxyPortType port_type)
Set the port type (from YAML configuration)
void set_instance_index(uint32_t index)
Set the instance index (called by Application::register_serial_proxy)
uint32_t get_instance_index() const
Get the instance index (position in Application's serial_proxies_ vector)
bool rts_state_
Current modem pin states.
void set_name(const char *name)
Set the human-readable port name (from YAML configuration)
void configure(uint32_t baudrate, bool flow_control, uint8_t parity, uint8_t stop_bits, uint8_t data_size)
Configure UART parameters and apply them.
void serial_proxy_request(api::APIConnection *api_connection, api::enums::SerialProxyRequestType type)
Handle a subscribe/unsubscribe request from an API client.
api::SerialProxyDataReceived outgoing_msg_
Pre-allocated outgoing message; instance field is set once in setup()
const char * name_
Human-readable port name (points to a string literal in flash)
api::enums::SerialProxyPortType port_type_
Port type.
GPIOPin * rts_pin_
Optional GPIO pins for modem control.
void set_rts_pin(GPIOPin *pin)
Set the RTS GPIO pin (from YAML configuration)
void set_modem_pins(uint32_t line_states)
Set modem pin states from a bitmask of SerialProxyLineStateFlag values.
api::APIConnection * get_api_connection()
Get the currently subscribed API connection (nullptr if none)
void write_from_client(const uint8_t *data, size_t len)
Write data received from an API client to the serial device.
api::enums::SerialProxyPortType get_port_type() const
Get the port type.
uart::FlushResult flush_port()
Flush the serial port (block until all TX data is sent)
api::APIConnection * api_connection_
Subscribed API client (only one allowed at a time)
const char * get_name() const
Get the human-readable port name.
uint16_t type
SerialProxyLineStateFlag
Bit flags for the line_states field exchanged with API clients.
@ SERIAL_PROXY_LINE_STATE_FLAG_RTS
RTS (Request To Send)
@ SERIAL_PROXY_LINE_STATE_FLAG_DTR
DTR (Data Terminal Ready)
constexpr size_t SERIAL_PROXY_MAX_READ_SIZE
Maximum bytes to read from UART in a single loop iteration.
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:43
FlushResult
Result of a flush() call.
std::string size_t len
Definition helpers.h:892
static void uint32_t