ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
uart_component_host.cpp
Go to the documentation of this file.
1#ifdef USE_HOST
6#include "esphome/core/log.h"
7
8#if !(defined(__linux__) || defined(__APPLE__))
9#error This HostUartComponent implementation is not supported on this host OS
10#endif
11
12#include <cerrno>
13#include <cstdio>
14#include <cstring>
15#include <unistd.h>
16#include <fcntl.h>
17#include <termios.h>
18#include <sys/ioctl.h>
19
20#ifdef USE_LOGGER
22#endif
23
24namespace {
25
26speed_t get_baud(int baud) {
27#ifdef __APPLE__
28 return baud;
29#else
30 switch (baud) {
31 case 50:
32 return B50;
33 case 75:
34 return B75;
35 case 110:
36 return B110;
37 case 134:
38 return B134;
39 case 150:
40 return B150;
41 case 200:
42 return B200;
43 case 300:
44 return B300;
45 case 600:
46 return B600;
47 case 1200:
48 return B1200;
49 case 1800:
50 return B1800;
51 case 2400:
52 return B2400;
53 case 4800:
54 return B4800;
55 case 9600:
56 return B9600;
57 case 19200:
58 return B19200;
59 case 38400:
60 return B38400;
61 case 57600:
62 return B57600;
63 case 115200:
64 return B115200;
65 case 230400:
66 return B230400;
67 case 460800:
68 return B460800;
69 case 500000:
70 return B500000;
71 case 576000:
72 return B576000;
73 case 921600:
74 return B921600;
75 case 1000000:
76 return B1000000;
77 case 1152000:
78 return B1152000;
79 case 1500000:
80 return B1500000;
81 case 2000000:
82 return B2000000;
83 case 2500000:
84 return B2500000;
85 case 3000000:
86 return B3000000;
87 case 3500000:
88 return B3500000;
89 case 4000000:
90 return B4000000;
91 default:
92 return B0;
93 }
94#endif
95}
96
97} // namespace
98
99namespace esphome::uart {
100
101ESPHOME_LOG_TAG(TAG, "uart");
102
104 if (this->file_descriptor_ != -1) {
105 close(this->file_descriptor_);
106 this->file_descriptor_ = -1;
107 }
108}
109
111 ESP_LOGCONFIG(TAG, "Opening UART port");
112 speed_t baud = get_baud(this->baud_rate_);
113 if (baud == B0) {
114 ESP_LOGE(TAG, "Unsupported baud rate: %d", this->baud_rate_);
115 this->mark_failed();
116 return;
117 }
118 this->file_descriptor_ = ::open(this->port_name_.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
119 if (this->file_descriptor_ == -1) {
120 this->update_error_(strerror(errno));
121 this->mark_failed();
122 return;
123 }
124 fcntl(this->file_descriptor_, F_SETFL, 0);
125 struct termios options;
126 tcgetattr(this->file_descriptor_, &options);
127 cfmakeraw(&options);
128 options.c_cflag &= ~CRTSCTS;
129 options.c_cflag |= CREAD | CLOCAL;
130 options.c_iflag &= ~(IXOFF | IXANY);
131 // Set data bits
132 options.c_cflag &= ~CSIZE; // Mask the character size bits
133 switch (this->data_bits_) {
134 case 5:
135 options.c_cflag |= CS5;
136 break;
137 case 6:
138 options.c_cflag |= CS6;
139 break;
140 case 7:
141 options.c_cflag |= CS7;
142 break;
143 case 8:
144 default:
145 options.c_cflag |= CS8;
146 break;
147 }
148 // Set parity
149 switch (this->parity_) {
151 options.c_cflag &= ~PARENB;
152 break;
154 options.c_cflag |= PARENB;
155 options.c_cflag &= ~PARODD;
156 break;
158 options.c_cflag |= PARENB;
159 options.c_cflag |= PARODD;
160 break;
161 default:
162 break;
163 };
164 // Set stop bits
165 assign_bits(options.c_cflag, CSTOPB, this->stop_bits_ == 2);
166 cfsetispeed(&options, baud);
167 cfsetospeed(&options, baud);
168 tcsetattr(this->file_descriptor_, TCSANOW, &options);
169}
170
172 ESP_LOGCONFIG(TAG, "UART:");
173 ESP_LOGCONFIG(TAG, " Port: %s", this->port_name_.c_str());
174 if (this->file_descriptor_ == -1) {
175 ESP_LOGCONFIG(TAG, " Port status: Not opened");
176 if (!this->first_error_.empty()) {
177 ESP_LOGCONFIG(TAG, " Error: %s", this->first_error_.c_str());
178 }
179 return;
180 }
181 ESP_LOGCONFIG(TAG,
182 " Port status: opened\n"
183 " Baud Rate: %d\n"
184 " Data Bits: %d\n"
185 " Parity: %s\n"
186 " Stop Bits: %d",
187 this->baud_rate_, this->data_bits_,
188 this->parity_ == UART_CONFIG_PARITY_NONE ? "None"
189 : this->parity_ == UART_CONFIG_PARITY_EVEN ? "Even"
190 : "Odd",
191 this->stop_bits_);
192 this->check_logger_conflict();
193}
194
195void HostUartComponent::write_array(const uint8_t *data, size_t len) {
196 if (this->file_descriptor_ == -1) {
197 return;
198 }
199 size_t written = ::write(this->file_descriptor_, data, len);
200 if (written != len) {
201 this->update_error_(strerror(errno));
202 return;
203 }
204#ifdef USE_UART_DEBUGGER
205 for (size_t i = 0; i < len; i++) {
206 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
207 }
208#endif
209}
210
211bool HostUartComponent::peek_byte(uint8_t *data) {
212 if (this->file_descriptor_ == -1) {
213 return false;
214 }
215 if (!this->has_peek_) {
216 if (!this->check_read_timeout_()) {
217 return false;
218 }
219 if (::read(this->file_descriptor_, &this->peek_byte_, 1) != 1) {
220 this->update_error_(strerror(errno));
221 return false;
222 }
223 this->has_peek_ = true;
224 }
225 *data = this->peek_byte_;
226 return true;
227}
228
229bool HostUartComponent::read_array(uint8_t *data, size_t len) {
230 if ((this->file_descriptor_ == -1) || (len == 0)) {
231 return false;
232 }
233 if (!this->check_read_timeout_(len))
234 return false;
235 size_t length_to_read = len;
236 if (this->has_peek_) {
237 length_to_read--;
238 *data = this->peek_byte_;
239 this->has_peek_ = false;
240 }
241 if (length_to_read > 0) {
242 int sz = ::read(this->file_descriptor_, data + (len - length_to_read), length_to_read);
243 if (sz == -1) {
244 this->update_error_(strerror(errno));
245 return false;
246 }
247 }
248#ifdef USE_UART_DEBUGGER
249 for (size_t i = 0; i < len; i++) {
250 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
251 }
252#endif
253 return true;
254}
255
257 if (this->file_descriptor_ == -1) {
258 return 0;
259 }
260 int available;
261 int res = ioctl(this->file_descriptor_, FIONREAD, &available);
262 if (res == -1) {
263 this->update_error_(strerror(errno));
264 return 0;
265 }
266 size_t result = available;
267 if (this->has_peek_)
268 result++;
269 return result;
270};
271
273 if (this->file_descriptor_ == -1) {
275 }
276 ESP_LOGV(TAG, " Flushing");
277 if (tcdrain(this->file_descriptor_) == -1) {
278 this->update_error_(strerror(errno));
280 }
282}
283
284bool HostUartComponent::set_modem_control(bool dtr, bool rts) {
285 if (this->file_descriptor_ == -1) {
286 return false;
287 }
288 // One TIOCMSET so the device never sees a state in between the old and the new one
289 int bits;
290 if (ioctl(this->file_descriptor_, TIOCMGET, &bits) == -1) {
291 this->update_error_(strerror(errno));
292 return false;
293 }
294 bits = (bits & ~(TIOCM_DTR | TIOCM_RTS)) | (dtr ? TIOCM_DTR : 0) | (rts ? TIOCM_RTS : 0);
295 if (ioctl(this->file_descriptor_, TIOCMSET, &bits) == -1) {
296 this->update_error_(strerror(errno));
297 return false;
298 }
299 return true;
300}
301
302bool HostUartComponent::get_dtr() const { return (this->get_modem_bits_() & TIOCM_DTR) != 0; }
303
304bool HostUartComponent::get_rts() const { return (this->get_modem_bits_() & TIOCM_RTS) != 0; }
305
307 int bits = 0;
308 if (this->file_descriptor_ != -1) {
309 ioctl(this->file_descriptor_, TIOCMGET, &bits);
310 }
311 return bits;
312}
313
314void HostUartComponent::update_error_(const std::string &error) {
315 if (this->first_error_.empty()) {
316 this->first_error_ = error;
317 }
318 ESP_LOGE(TAG, "Port error: %s", error.c_str());
319}
320
321} // namespace esphome::uart
322#endif // USE_HOST
void mark_failed()
Mark this component as failed.
bool read_array(uint8_t *data, size_t len) override
bool set_modem_control(bool dtr, bool rts)
Drive DTR/RTS. Returns false when the port is closed or the ioctl fails.
void write_array(const uint8_t *data, size_t len) override
void update_error_(const std::string &error)
bool peek_byte(uint8_t *data) override
bool check_read_timeout_(size_t len=1)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
uint8_t options
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
@ UART_FLUSH_RESULT_FAILED
Confirmed: driver or hardware error.
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150
const void size_t len
Definition hal.h:68
int written
Definition helpers.h:1340