ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
serial_proxy.cpp
Go to the documentation of this file.
1#include "serial_proxy.h"
2
3#ifdef USE_SERIAL_PROXY
4
5#include "esphome/core/log.h"
6
7#include <cinttypes>
8#include "esphome/core/util.h"
9
10#ifdef USE_API
13#endif
14
16
17static const char *const TAG = "serial_proxy";
18
20 // Set up modem control pins if configured
21 if (this->rts_pin_ != nullptr) {
22 this->rts_pin_->setup();
23 this->rts_pin_->digital_write(this->rts_state_);
24 }
25 if (this->dtr_pin_ != nullptr) {
26 this->dtr_pin_->setup();
27 this->dtr_pin_->digital_write(this->dtr_state_);
28 }
29#ifdef USE_API
30 // instance_index_ is fixed at registration time; pre-set it so loop() only needs to update data
32#endif
33 // No subscriber at startup; disable loop until a client subscribes
34 this->disable_loop();
35}
36
38#ifdef USE_API
39 // Safety check — loop should only run when subscribed, but guard against races
40 if (this->api_connection_ == nullptr) [[unlikely]] {
41 this->disable_loop();
42 return;
43 }
44
45 // Detect subscriber disconnect
46 if (this->api_connection_->is_marked_for_removal() || !this->api_connection_->is_connection_setup() ||
48 ESP_LOGW(TAG, "Subscriber disconnected");
49 this->api_connection_ = nullptr;
50 this->disable_loop();
51 return;
52 }
53
54 // Read available data from UART and forward to subscribed client
55 size_t available = this->available();
56 if (available == 0)
57 return;
58
59 this->read_and_send_(available);
60#endif
61}
62
63#ifdef USE_API
64void __attribute__((noinline)) SerialProxy::read_and_send_(size_t available) {
65 // Read in chunks up to SERIAL_PROXY_MAX_READ_SIZE
66 uint8_t buffer[SERIAL_PROXY_MAX_READ_SIZE];
67 size_t to_read = std::min(available, sizeof(buffer));
68
69 if (!this->read_array(buffer, to_read))
70 return;
71
72 this->outgoing_msg_.set_data(buffer, to_read);
74}
75#endif
76
78 ESP_LOGCONFIG(TAG,
79 "Serial Proxy [%" PRIu32 "]:\n"
80 " Name: %s\n"
81 " Port Type: %s\n"
82 " RTS Pin: %s\n"
83 " DTR Pin: %s",
84 this->instance_index_, this->name_ != nullptr ? this->name_ : "",
87 : "TTL",
88 this->rts_pin_ != nullptr ? "configured" : "not configured",
89 this->dtr_pin_ != nullptr ? "configured" : "not configured");
90}
91
92void SerialProxy::configure(uint32_t baudrate, bool flow_control, uint8_t parity, uint8_t stop_bits,
93 uint8_t data_size) {
94 ESP_LOGD(TAG,
95 "Configuring serial proxy [%" PRIu32 "]: baud=%" PRIu32 ", flow_ctrl=%s, parity=%" PRIu8 ", stop=%" PRIu8
96 ", data=%" PRIu8,
97 this->instance_index_, baudrate, YESNO(flow_control), parity, stop_bits, data_size);
98
99 auto *uart_comp = this->parent_;
100 if (uart_comp == nullptr) {
101 ESP_LOGE(TAG, "UART component not available");
102 return;
103 }
104
105 // Validate all parameters before applying any (values come from a remote client)
106 if (baudrate == 0) {
107 ESP_LOGW(TAG, "Invalid baud rate: 0");
108 return;
109 }
110 if (stop_bits < 1 || stop_bits > 2) {
111 ESP_LOGW(TAG, "Invalid stop bits: %u (must be 1 or 2)", stop_bits);
112 return;
113 }
114 if (data_size < 5 || data_size > 8) {
115 ESP_LOGW(TAG, "Invalid data bits: %u (must be 5-8)", data_size);
116 return;
117 }
118 if (parity > 2) {
119 ESP_LOGW(TAG, "Invalid parity: %u (must be 0-2)", parity);
120 return;
121 }
122
123 // Apply validated parameters
124 uart_comp->set_baud_rate(baudrate);
125 uart_comp->set_stop_bits(stop_bits);
126 uart_comp->set_data_bits(data_size);
127
128 // Map parity value to UARTParityOptions
129 static const uart::UARTParityOptions PARITY_MAP[] = {
133 };
134 uart_comp->set_parity(PARITY_MAP[parity]);
135
136 // load_settings() is available on ESP8266 and ESP32 platforms
137#if defined(USE_ESP8266) || defined(USE_ESP32)
138 uart_comp->load_settings(true);
139#endif
140
141 if (flow_control) {
142 ESP_LOGW(TAG, "Hardware flow control requested but is not yet supported");
143 }
144}
145
146void SerialProxy::write_from_client(const uint8_t *data, size_t len) {
147 if (data == nullptr || len == 0)
148 return;
149 this->write_array(data, len);
150}
151
153 const bool rts = (line_states & SERIAL_PROXY_LINE_STATE_FLAG_RTS) != 0;
154 const bool dtr = (line_states & SERIAL_PROXY_LINE_STATE_FLAG_DTR) != 0;
155 ESP_LOGV(TAG, "Setting modem pins [%" PRIu32 "]: RTS=%s, DTR=%s", this->instance_index_, ONOFF(rts), ONOFF(dtr));
156
157 if (this->rts_pin_ != nullptr) {
158 this->rts_state_ = rts;
159 this->rts_pin_->digital_write(rts);
160 }
161 if (this->dtr_pin_ != nullptr) {
162 this->dtr_state_ = dtr;
163 this->dtr_pin_->digital_write(dtr);
164 }
165}
166
168 return (this->rts_state_ ? static_cast<uint32_t>(SERIAL_PROXY_LINE_STATE_FLAG_RTS) : 0u) |
169 (this->dtr_state_ ? static_cast<uint32_t>(SERIAL_PROXY_LINE_STATE_FLAG_DTR) : 0u);
170}
171
173 ESP_LOGV(TAG, "Flushing serial proxy [%" PRIu32 "]", this->instance_index_);
174 return this->flush();
175}
176
177#ifdef USE_API
179 switch (type) {
181 if (this->api_connection_ != nullptr) {
182 ESP_LOGE(TAG, "Only one API subscription is allowed at a time");
183 return;
184 }
185 this->api_connection_ = api_connection;
186 this->enable_loop();
187 ESP_LOGV(TAG, "API connection subscribed to serial proxy [%" PRIu32 "]", this->instance_index_);
188 break;
190 if (this->api_connection_ != api_connection) {
191 ESP_LOGV(TAG, "API connection is not subscribed to serial proxy [%" PRIu32 "]", this->instance_index_);
192 return;
193 }
194 this->api_connection_ = nullptr;
195 this->disable_loop();
196 ESP_LOGV(TAG, "API connection unsubscribed from serial proxy [%" PRIu32 "]", this->instance_index_);
197 break;
198 default:
199 ESP_LOGW(TAG, "Unknown serial proxy request type: %" PRIu32, static_cast<uint32_t>(type));
200 break;
201 }
202}
203#endif
204
205} // namespace esphome::serial_proxy
206
207#endif // USE_SERIAL_PROXY
void enable_loop()
Enable this component's loop.
Definition component.h:245
void disable_loop()
Disable this component's loop.
virtual void setup()=0
virtual void digital_write(bool value)=0
void send_serial_proxy_data(const SerialProxyDataReceived &msg)
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:3137
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.
bool rts_state_
Current modem pin states.
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.
uart::UARTFlushResult flush_port()
Flush the serial port (block until all TX data is sent)
void set_modem_pins(uint32_t line_states)
Set modem pin states from a bitmask of SerialProxyLineStateFlag values.
void write_from_client(const uint8_t *data, size_t len)
Write data received from an API client to the serial device.
api::APIConnection * api_connection_
Subscribed API client (only one allowed at a time)
void set_baud_rate(uint32_t baud_rate)
UARTFlushResult flush()
Definition uart.h:48
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
UARTComponent * parent_
Definition uart.h:73
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
struct @65::@66 __attribute__
uint16_t type
@ SERIAL_PROXY_PORT_TYPE_RS232
Definition api_pb2.h:16
@ SERIAL_PROXY_PORT_TYPE_RS485
Definition api_pb2.h:17
@ SERIAL_PROXY_REQUEST_TYPE_UNSUBSCRIBE
Definition api_pb2.h:333
@ SERIAL_PROXY_REQUEST_TYPE_SUBSCRIBE
Definition api_pb2.h:332
@ 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.
UARTFlushResult
Result of a flush() call.
bool api_is_connected()
Return whether the node has at least one client connected to the native API.
Definition util.cpp:17
std::string size_t len
Definition helpers.h:1045
static void uint32_t