15static const char *
const TAG =
"serial_proxy";
46 ESP_LOGW(TAG,
"Subscriber disconnected");
65 size_t to_read = std::min(
available,
sizeof(buffer));
77 "Serial Proxy [%u]:\n"
86 this->
rts_pin_ !=
nullptr ?
"configured" :
"not configured",
87 this->
dtr_pin_ !=
nullptr ?
"configured" :
"not configured");
92 ESP_LOGD(TAG,
"Configuring serial proxy [%u]: baud=%u, flow_ctrl=%s, parity=%u, stop=%u, data=%u",
93 this->
instance_index_, baudrate, YESNO(flow_control), parity, stop_bits, data_size);
95 auto *uart_comp = this->
parent_;
96 if (uart_comp ==
nullptr) {
97 ESP_LOGE(TAG,
"UART component not available");
103 ESP_LOGW(TAG,
"Invalid baud rate: 0");
106 if (stop_bits < 1 || stop_bits > 2) {
107 ESP_LOGW(TAG,
"Invalid stop bits: %u (must be 1 or 2)", stop_bits);
110 if (data_size < 5 || data_size > 8) {
111 ESP_LOGW(TAG,
"Invalid data bits: %u (must be 5-8)", data_size);
115 ESP_LOGW(TAG,
"Invalid parity: %u (must be 0-2)", parity);
121 uart_comp->set_stop_bits(stop_bits);
122 uart_comp->set_data_bits(data_size);
130 uart_comp->set_parity(PARITY_MAP[parity]);
133#if defined(USE_ESP8266) || defined(USE_ESP32)
134 uart_comp->load_settings(
true);
138 ESP_LOGW(TAG,
"Hardware flow control requested but is not yet supported");
143 if (data ==
nullptr ||
len == 0)
151 ESP_LOGV(TAG,
"Setting modem pins [%u]: RTS=%s, DTR=%s", this->
instance_index_, ONOFF(rts), ONOFF(dtr));
170 return this->
flush();
178 ESP_LOGE(TAG,
"Only one API subscription is allowed at a time");
183 ESP_LOGV(TAG,
"API connection subscribed to serial proxy [%u]", this->
instance_index_);
187 ESP_LOGV(TAG,
"API connection is not subscribed to serial proxy [%u]", this->
instance_index_);
192 ESP_LOGV(TAG,
"API connection unsubscribed from serial proxy [%u]", this->
instance_index_);
195 ESP_LOGW(TAG,
"Unknown serial proxy request type: %u",
static_cast<uint32_t>(
type));
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
virtual void digital_write(bool value)=0
void send_serial_proxy_data(const SerialProxyDataReceived &msg)
bool is_marked_for_removal() const
void set_data(const uint8_t *data, size_t len)
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.
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.
void dump_config() override
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)
void set_baud_rate(uint32_t baud_rate)
optional< std::array< uint8_t, N > > read_array()
void write_array(const uint8_t *data, size_t len)
struct @65::@66 __attribute__
@ SERIAL_PROXY_PORT_TYPE_RS232
@ SERIAL_PROXY_PORT_TYPE_RS485
@ SERIAL_PROXY_REQUEST_TYPE_UNSUBSCRIBE
@ SERIAL_PROXY_REQUEST_TYPE_SUBSCRIBE
@ 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.
@ UART_CONFIG_PARITY_EVEN
@ UART_CONFIG_PARITY_NONE
FlushResult
Result of a flush() call.
bool api_is_connected()
Return whether the node has at least one client connected to the native API.