libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::LinuxUART Class Reference

Linux UART 串口驱动实现 / Linux UART driver implementation. More...

#include <linux_uart.hpp>

Inheritance diagram for LibXR::LinuxUART:
[legend]
Collaboration diagram for LibXR::LinuxUART:
[legend]

Public Member Functions

 LinuxUART (const char *dev_path, unsigned int baudrate=115200, Parity parity=Parity::NO_PARITY, uint8_t data_bits=8, uint8_t stop_bits=1, uint32_t tx_queue_size=5, size_t buffer_size=512, size_t thread_stack_size=65536)
 通过设备路径构造 UART / Construct UART by device path
 
 LinuxUART (const std::string &vid, const std::string &pid, unsigned int baudrate=115200, Parity parity=Parity::NO_PARITY, uint8_t data_bits=8, uint8_t stop_bits=1, uint32_t tx_queue_size=5, size_t buffer_size=512, size_t thread_stack_size=65536)
 通过 USB VID/PID 构造 UART / Construct UART by USB VID/PID
 
 LinuxUART (const std::string &vid, const std::string &pid, const std::string &control_interface_name, unsigned int baudrate=115200, Parity parity=Parity::NO_PARITY, uint8_t data_bits=8, uint8_t stop_bits=1, uint32_t tx_queue_size=5, size_t buffer_size=512, size_t thread_stack_size=65536)
 通过 USB VID/PID/control interface name 构造 UART Construct UART by USB VID/PID/control interface name
 
 LinuxUART (const std::string &vid, const std::string &pid, const std::string &control_interface_name, const std::string &serial, unsigned int baudrate=115200, Parity parity=Parity::NO_PARITY, uint8_t data_bits=8, uint8_t stop_bits=1, uint32_t tx_queue_size=5, size_t buffer_size=512, size_t thread_stack_size=65536)
 通过 USB VID/PID/control interface name/serial 构造 UART Construct UART by USB VID/PID/control interface name/serial
 
std::string GetByPathForTTY (const std::string &tty_name)
 
void SetLowLatency (int fd)
 
ErrorCode SetConfig (UART::Configuration config) override
 设置 UART 配置 / Sets the UART configuration
 
- Public Member Functions inherited from LibXR::UART
template<typename ReadPortType = ReadPort, typename WritePortType = WritePort>
 UART (ReadPortType *read_port, WritePortType *write_port)
 UART 构造函数 / UART constructor.
 
template<typename OperationType , typename = std::enable_if_t<std::is_base_of_v< WriteOperation, std::decay_t<OperationType>>>>
ErrorCode Write (ConstRawData data, OperationType &&op, bool in_isr=false)
 
template<typename OperationType , typename = std::enable_if_t<std::is_base_of_v< ReadOperation, std::decay_t<OperationType>>>>
ErrorCode Read (RawData data, OperationType &&op, bool in_isr=false)
 

Static Public Member Functions

static bool FindUSBTTYByVidPid (const std::string &target_vid, const std::string &target_pid, std::string &tty_path)
 
static bool FindUSBTTYByVidPid (const std::string &target_vid, const std::string &target_pid, const std::string &target_control_interface_name, std::string &tty_path)
 
static bool FindUSBTTYByVidPid (const std::string &target_vid, const std::string &target_pid, const std::string &target_control_interface_name, const std::string &target_serial, std::string &tty_path)
 
static ErrorCode ReadFun (ReadPort &, bool)
 
static ErrorCode WriteFun (WritePort &port, bool)
 

Private Member Functions

void RxLoop ()
 
void TxLoop ()
 

Private Attributes

int fd_ = -1
 
bool connected_ = true
 
Configuration config_
 
std::string device_path_
 
Thread rx_thread_
 
Thread tx_thread_
 
uint8_t * rx_buff_ = nullptr
 
uint8_t * tx_buff_ = nullptr
 
size_t buff_size_ = 0
 
Semaphore write_sem_
 
Mutex read_mutex_
 
ReadPort _read_port
 
WritePort _write_port
 

Additional Inherited Members

- Public Types inherited from LibXR::UART
enum class  Parity : uint8_t { NO_PARITY = 0 , EVEN = 1 , ODD = 2 }
 奇偶校验模式 / Parity mode More...
 
- Data Fields inherited from LibXR::UART
ReadPortread_port_
 读取端口 / Read port
 
WritePortwrite_port_
 写入端口 / Write port
 

Detailed Description

Linux UART 串口驱动实现 / Linux UART driver implementation.

支持按设备路径或 USB VID/PID(/control interface name[/serial]) 自动发现并创建 UART 通道。 Supports UART creation by device path or USB VID/PID(/control interface name[/serial]) auto discovery.

Definition at line 36 of file linux_uart.hpp.

Constructor & Destructor Documentation

◆ LinuxUART() [1/4]

LibXR::LinuxUART::LinuxUART ( const char * dev_path,
unsigned int baudrate = 115200,
Parity parity = Parity::NO_PARITY,
uint8_t data_bits = 8,
uint8_t stop_bits = 1,
uint32_t tx_queue_size = 5,
size_t buffer_size = 512,
size_t thread_stack_size = 65536 )
inline

通过设备路径构造 UART / Construct UART by device path

Definition at line 42 of file linux_uart.hpp.

46 : UART(&_read_port, &_write_port),
47 rx_buff_(new uint8_t[buffer_size]),
48 tx_buff_(new uint8_t[buffer_size]),
49 buff_size_(buffer_size),
50 _read_port(buffer_size),
51 _write_port(tx_queue_size, buffer_size)
52 {
53 ASSERT(buff_size_ > 0);
54
55 while (!std::filesystem::exists(dev_path))
56 {
57 XR_LOG_WARN("Cannot find UART device: %s, retrying...", dev_path);
58 Thread::Sleep(100);
59 }
60
61 device_path_ = GetByPathForTTY(dev_path);
62
63 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
64 if (fd_ < 0)
65 {
66 XR_LOG_ERROR("Cannot open UART device: %s", device_path_.c_str());
67 ASSERT(false);
68 }
69 else
70 {
71 XR_LOG_PASS("Open UART device: %s", device_path_.c_str());
72 }
73
74 config_ = {};
75 config_.baudrate = baudrate;
76 config_.parity = parity;
77 config_.data_bits = data_bits;
78 config_.stop_bits = stop_bits;
79
80 SetConfig(config_);
81
82 _read_port = ReadFun;
83 _write_port = WriteFun;
84
85 rx_thread_.Create<LinuxUART*>(
86 this, [](LinuxUART* self) { self->RxLoop(); }, "rx_uart", thread_stack_size,
88
89 tx_thread_.Create<LinuxUART*>(
90 this, [](LinuxUART* self) { self->TxLoop(); }, "tx_uart", thread_stack_size,
92 }
ErrorCode SetConfig(UART::Configuration config) override
设置 UART 配置 / Sets the UART configuration
LinuxUART(const char *dev_path, unsigned int baudrate=115200, Parity parity=Parity::NO_PARITY, uint8_t data_bits=8, uint8_t stop_bits=1, uint32_t tx_queue_size=5, size_t buffer_size=512, size_t thread_stack_size=65536)
通过设备路径构造 UART / Construct UART by device path
@ REALTIME
实时优先级 Realtime priority
void Create(ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
创建新线程 Creates a new thread
Definition thread.hpp:68
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:15
UART(ReadPortType *read_port, WritePortType *write_port)
UART 构造函数 / UART constructor.
Definition uart.hpp:66
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read notifications.
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.
uint8_t stop_bits
停止位长度 / Number of stop bits
Definition uart.hpp:50
Parity parity
校验模式 / Parity mode
Definition uart.hpp:47
uint8_t data_bits
数据位长度 / Number of data bits
Definition uart.hpp:48
uint32_t baudrate
波特率 / Baud rate
Definition uart.hpp:45

◆ LinuxUART() [2/4]

LibXR::LinuxUART::LinuxUART ( const std::string & vid,
const std::string & pid,
unsigned int baudrate = 115200,
Parity parity = Parity::NO_PARITY,
uint8_t data_bits = 8,
uint8_t stop_bits = 1,
uint32_t tx_queue_size = 5,
size_t buffer_size = 512,
size_t thread_stack_size = 65536 )
inline

通过 USB VID/PID 构造 UART / Construct UART by USB VID/PID

Definition at line 97 of file linux_uart.hpp.

101 : LinuxUART(vid, pid, "", "", baudrate, parity, data_bits, stop_bits,
102 tx_queue_size, buffer_size, thread_stack_size)
103 {
104 }

◆ LinuxUART() [3/4]

LibXR::LinuxUART::LinuxUART ( const std::string & vid,
const std::string & pid,
const std::string & control_interface_name,
unsigned int baudrate = 115200,
Parity parity = Parity::NO_PARITY,
uint8_t data_bits = 8,
uint8_t stop_bits = 1,
uint32_t tx_queue_size = 5,
size_t buffer_size = 512,
size_t thread_stack_size = 65536 )
inline

通过 USB VID/PID/control interface name 构造 UART Construct UART by USB VID/PID/control interface name

Note
CDC ACM 有 control/data 两个 interface;这里匹配 Linux ttyACM 父级 usb_interface 的 control interface 名称,不匹配 data interface 名称。 CDC ACM has control/data interfaces; this selector matches the Linux ttyACM parent usb_interface control interface name, not the data interface name.

Definition at line 116 of file linux_uart.hpp.

121 : LinuxUART(vid, pid, control_interface_name, "", baudrate, parity, data_bits,
122 stop_bits, tx_queue_size, buffer_size, thread_stack_size)
123 {
124 }

◆ LinuxUART() [4/4]

LibXR::LinuxUART::LinuxUART ( const std::string & vid,
const std::string & pid,
const std::string & control_interface_name,
const std::string & serial,
unsigned int baudrate = 115200,
Parity parity = Parity::NO_PARITY,
uint8_t data_bits = 8,
uint8_t stop_bits = 1,
uint32_t tx_queue_size = 5,
size_t buffer_size = 512,
size_t thread_stack_size = 65536 )
inline

通过 USB VID/PID/control interface name/serial 构造 UART Construct UART by USB VID/PID/control interface name/serial

Note
CDC ACM 有 control/data 两个 interface;这里匹配 Linux ttyACM 父级 usb_interface 的 control interface 名称,不匹配 data interface 名称。 CDC ACM has control/data interfaces; this selector matches the Linux ttyACM parent usb_interface control interface name, not the data interface name.

Definition at line 136 of file linux_uart.hpp.

141 : UART(&_read_port, &_write_port),
142 rx_buff_(new uint8_t[buffer_size]),
143 tx_buff_(new uint8_t[buffer_size]),
144 buff_size_(buffer_size),
145 _read_port(buffer_size),
146 _write_port(tx_queue_size, buffer_size)
147 {
148 while (!FindUSBTTYByVidPid(vid, pid, control_interface_name, serial, device_path_))
149 {
150 XR_LOG_WARN(
151 "Cannot find USB TTY device with VID=%s PID=%s SERIAL=%s CONTROL_INTERFACE=%s, retrying...",
152 vid.c_str(), pid.c_str(), serial.empty() ? "*" : serial.c_str(),
153 control_interface_name.empty() ? "*" : control_interface_name.c_str());
154 Thread::Sleep(100);
155 }
156
157 XR_LOG_PASS("Found USB TTY: %s", device_path_.c_str());
158
159 if (std::filesystem::exists(device_path_) == false)
160 {
161 XR_LOG_ERROR("Cannot find UART device: %s", device_path_.c_str());
162 ASSERT(false);
163 return;
164 }
165
166 device_path_ = GetByPathForTTY(device_path_);
167
168 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
169 if (fd_ < 0)
170 {
171 XR_LOG_ERROR("Cannot open UART device: %s", device_path_.c_str());
172 ASSERT(false);
173 }
174 else
175 {
176 XR_LOG_PASS("Open UART device: %s", device_path_.c_str());
177 }
178
179 config_ = {};
180 config_.baudrate = baudrate;
181 config_.parity = parity;
182 config_.data_bits = data_bits;
183 config_.stop_bits = stop_bits;
184
185 SetConfig(config_);
186
187 _read_port = ReadFun;
188 _write_port = WriteFun;
189
190 rx_thread_.Create<LinuxUART*>(
191 this, [](LinuxUART* self) { self->RxLoop(); }, "rx_uart", thread_stack_size,
193
194 tx_thread_.Create<LinuxUART*>(
195 this, [](LinuxUART* self) { self->TxLoop(); }, "tx_uart", thread_stack_size,
197 }

Member Function Documentation

◆ FindUSBTTYByVidPid() [1/3]

static bool LibXR::LinuxUART::FindUSBTTYByVidPid ( const std::string & target_vid,
const std::string & target_pid,
const std::string & target_control_interface_name,
const std::string & target_serial,
std::string & tty_path )
inlinestatic

Definition at line 239 of file linux_uart.hpp.

243 {
244 struct udev* udev = udev_new();
245 if (!udev)
246 {
247 XR_LOG_ERROR("Cannot create udev context");
248 return false;
249 }
250
251 struct udev_enumerate* enumerate = udev_enumerate_new(udev);
252 udev_enumerate_add_match_subsystem(enumerate, "tty");
253 udev_enumerate_scan_devices(enumerate);
254
255 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
256 struct udev_list_entry* entry = nullptr;
257 std::vector<std::string> matches;
258
259 udev_list_entry_foreach(entry, devices)
260 {
261 const char* path = udev_list_entry_get_name(entry);
262 struct udev_device* tty_dev = udev_device_new_from_syspath(udev, path);
263 if (!tty_dev)
264 {
265 continue;
266 }
267
268 struct udev_device* usb_dev =
269 udev_device_get_parent_with_subsystem_devtype(tty_dev, "usb", "usb_device");
270 struct udev_device* usb_interface = udev_device_get_parent_with_subsystem_devtype(
271 tty_dev, "usb", "usb_interface");
272
273 if (usb_dev)
274 {
275 const char* vid = udev_device_get_sysattr_value(usb_dev, "idVendor");
276 const char* pid = udev_device_get_sysattr_value(usb_dev, "idProduct");
277 const char* serial = udev_device_get_sysattr_value(usb_dev, "serial");
278 const char* control_interface_name = nullptr;
279 if (usb_interface)
280 {
281 // For Linux cdc_acm tty nodes this parent is the CDC control interface
282 // (for example if00 / if02), not the CDC data interface.
283 control_interface_name =
284 udev_device_get_sysattr_value(usb_interface, "interface");
285 }
286
287 if (vid && pid && target_vid == vid && target_pid == pid &&
288 (target_serial.empty() || (serial && target_serial == serial)) &&
289 (target_control_interface_name.empty() ||
290 (control_interface_name &&
291 target_control_interface_name == control_interface_name)))
292 {
293 const char* devnode = udev_device_get_devnode(tty_dev);
294 if (devnode)
295 {
296 matches.emplace_back(devnode);
297 }
298 }
299 }
300
301 udev_device_unref(tty_dev);
302 }
303
304 udev_enumerate_unref(enumerate);
305 udev_unref(udev);
306 if (matches.empty())
307 {
308 return false;
309 }
310
311 std::sort(matches.begin(), matches.end());
312 tty_path = matches.front();
313
314 if (matches.size() > 1)
315 {
316 XR_LOG_WARN(
317 "Multiple USB TTY devices found with VID=%s PID=%s SERIAL=%s CONTROL_INTERFACE=%s, using %s. Specify serial or control interface name to disambiguate.",
318 target_vid.c_str(), target_pid.c_str(),
319 target_serial.empty() ? "*" : target_serial.c_str(),
320 target_control_interface_name.empty() ? "*"
321 : target_control_interface_name.c_str(),
322 tty_path.c_str());
323 }
324
325 return true;
326 }

◆ FindUSBTTYByVidPid() [2/3]

static bool LibXR::LinuxUART::FindUSBTTYByVidPid ( const std::string & target_vid,
const std::string & target_pid,
const std::string & target_control_interface_name,
std::string & tty_path )
inlinestatic

Definition at line 230 of file linux_uart.hpp.

234 {
235 return FindUSBTTYByVidPid(target_vid, target_pid, target_control_interface_name, "",
236 tty_path);
237 }

◆ FindUSBTTYByVidPid() [3/3]

static bool LibXR::LinuxUART::FindUSBTTYByVidPid ( const std::string & target_vid,
const std::string & target_pid,
std::string & tty_path )
inlinestatic

Definition at line 223 of file linux_uart.hpp.

226 {
227 return FindUSBTTYByVidPid(target_vid, target_pid, "", "", tty_path);
228 }

◆ GetByPathForTTY()

std::string LibXR::LinuxUART::GetByPathForTTY ( const std::string & tty_name)
inline

Definition at line 199 of file linux_uart.hpp.

200 {
201 const std::string BASE = "/dev/serial/by-path";
202 if (strncmp(tty_name.c_str(), BASE.c_str(), BASE.length()) == 0 ||
203 !std::filesystem::exists(BASE))
204 {
205 return tty_name;
206 }
207 for (const auto& entry : std::filesystem::directory_iterator(BASE))
208 {
209 std::error_code ec;
210 const auto full = std::filesystem::canonical(entry.path(), ec);
211 if (ec)
212 {
213 continue;
214 }
215 if (full == tty_name)
216 {
217 return entry.path().string(); // 返回符号链接路径
218 }
219 }
220 return tty_name; // 未命中 by-path 时保留原始 tty 路径
221 }

◆ ReadFun()

static ErrorCode LibXR::LinuxUART::ReadFun ( ReadPort & ,
bool  )
inlinestatic

Definition at line 447 of file linux_uart.hpp.

447{ return ErrorCode::PENDING; }
@ PENDING
等待中 | Pending

◆ RxLoop()

void LibXR::LinuxUART::RxLoop ( )
inlineprivate

Definition at line 457 of file linux_uart.hpp.

458 {
459 while (true)
460 {
461 if (!connected_)
462 {
463 close(fd_);
464 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
465
466 if (fd_ < 0)
467 {
468 XR_LOG_WARN("Cannot open UART device: %s", device_path_.c_str());
469 Thread::Sleep(1000);
470 }
471 else
472 {
473 SetConfig(config_);
474 XR_LOG_PASS("Reopen UART device: %s", device_path_.c_str());
475 connected_ = true;
476 }
477 }
478 auto n = read(fd_, rx_buff_, buff_size_);
479 if (n > 0)
480 {
481 read_port_->queue_data_->PushBatch(rx_buff_, n);
483 }
484 else
485 {
486 XR_LOG_WARN("Cannot read UART device: %s", device_path_.c_str());
487 connected_ = false;
488 }
489 }
490 }
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
LockFreeQueue< uint8_t > * queue_data_
RX payload queue. 接收数据字节队列。
Definition read_port.hpp:54
void ProcessPendingReads(bool in_isr)
Processes pending reads.
ReadPort * read_port_
读取端口 / Read port
Definition uart.hpp:53

◆ SetConfig()

ErrorCode LibXR::LinuxUART::SetConfig ( UART::Configuration config)
inlineoverridevirtual

设置 UART 配置 / Sets the UART configuration

Parameters
configUART 配置信息 / UART configuration settings
Returns
返回操作状态,成功时返回 ErrorCode::OK,否则返回相应错误码 / Returns the operation status, ErrorCode::OK if successful, otherwise an error code

该方法为纯虚函数,子类必须实现具体的 UART 配置逻辑。 This is a pure virtual function. Subclasses must implement the specific UART configuration logic.

Implements LibXR::UART.

Definition at line 336 of file linux_uart.hpp.

337 {
338 if (&config != &config_)
339 {
340 config_ = config;
341 }
342
343 struct termios2 tio{};
344 if (ioctl(fd_, TCGETS2, &tio) != 0)
345 {
346 return ErrorCode::INIT_ERR;
347 }
348
349 // 设置自定义波特率
350 tio.c_cflag &= ~CBAUD;
351 tio.c_cflag |= BOTHER;
352 tio.c_ispeed = config.baudrate;
353 tio.c_ospeed = config.baudrate;
354
355 // 输入模式:关闭软件流控、特殊字符处理
356 tio.c_iflag &= ~(IXON | IXOFF | IXANY | ISTRIP | IGNCR | INLCR | ICRNL
357#ifdef IUCLC
358 | IUCLC
359#endif
360 );
361
362 // 输出模式:关闭所有加工
363 tio.c_oflag &= ~(OPOST
364#ifdef ONLCR
365 | ONLCR
366#endif
367#ifdef OCRNL
368 | OCRNL
369#endif
370#ifdef ONOCR
371 | ONOCR
372#endif
373#ifdef ONLRET
374 | ONLRET
375#endif
376 );
377
378 // 本地模式:禁用行缓冲、回显、信号中断
379 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
380
381 // 控制模式:设置数据位、校验、停止位、流控
382 tio.c_cflag &= ~CSIZE;
383 switch (config.data_bits)
384 {
385 case 5:
386 tio.c_cflag |= CS5;
387 break;
388 case 6:
389 tio.c_cflag |= CS6;
390 break;
391 case 7:
392 tio.c_cflag |= CS7;
393 break;
394 case 8:
395 tio.c_cflag |= CS8;
396 break;
397 default:
398 return ErrorCode::ARG_ERR;
399 }
400
401 // 停止位
402 tio.c_cflag &= ~CSTOPB;
403 if (config.stop_bits == 2)
404 {
405 tio.c_cflag |= CSTOPB;
406 }
407
408 // 奇偶校验
409 switch (config.parity)
410 {
412 tio.c_cflag &= ~PARENB;
413 break;
415 tio.c_cflag |= PARENB;
416 tio.c_cflag &= ~PARODD;
417 break;
419 tio.c_cflag |= PARENB;
420 tio.c_cflag |= PARODD;
421 break;
422 }
423
424 // 禁用硬件流控
425 tio.c_cflag &= ~CRTSCTS;
426
427 // 启用本地模式、读功能
428 tio.c_cflag |= (CLOCAL | CREAD);
429
430 // 控制字符配置:阻塞直到读到 1 字节
431 // for (int i = 0; i < NCCS; ++i) tio.c_cc[i] = 0;
432 tio.c_cc[VTIME] = 0;
433 tio.c_cc[VMIN] = 1;
434
435 if (ioctl(fd_, TCSETS2, &tio) != 0)
436 {
437 return ErrorCode::INIT_ERR;
438 }
439
440 SetLowLatency(fd_);
441
442 tcflush(fd_, TCIOFLUSH);
443
444 return ErrorCode::OK;
445 }
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
@ INIT_ERR
初始化错误 | Initialization error
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error

◆ SetLowLatency()

void LibXR::LinuxUART::SetLowLatency ( int fd)
inline

Definition at line 328 of file linux_uart.hpp.

329 {
330 struct serial_struct serinfo;
331 ioctl(fd, TIOCGSERIAL, &serinfo);
332 serinfo.flags |= ASYNC_LOW_LATENCY;
333 ioctl(fd, TIOCSSERIAL, &serinfo);
334 }

◆ TxLoop()

void LibXR::LinuxUART::TxLoop ( )
inlineprivate

Definition at line 492 of file linux_uart.hpp.

493 {
494 WriteInfoBlock info;
495 while (true)
496 {
497 if (!connected_)
498 {
499 Thread::Sleep(1);
500 continue;
501 }
502
503 if (write_sem_.Wait() != ErrorCode::OK)
504 {
505 continue;
506 }
507
508 if (write_port_->queue_info_->Pop(info) == ErrorCode::OK)
509 {
510 if (write_port_->queue_data_->PopBatch(tx_buff_, info.data.size_) ==
512 {
513 auto written = write(fd_, tx_buff_, info.data.size_);
514 if (written < 0)
515 {
516 XR_LOG_WARN("Cannot write UART device: %s", device_path_.c_str());
517 connected_ = false;
518 }
519 write_port_->Finish(false,
520 (written == static_cast<int>(info.data.size_))
523 info);
524 }
525 else
526 {
527 info.op.UpdateStatus(false, ErrorCode::FAILED);
528 }
529 }
530 }
531 }
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:53
WritePort * write_port_
写入端口 / Write port
Definition uart.hpp:54
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info)
更新写入操作的状态。 Updates the status of the write operation.
LockFreeQueue< uint8_t > * queue_data_
Payload queue for pending write bytes. 挂起写入字节的数据队列。
LockFreeQueue< WriteInfoBlock > * queue_info_
Metadata queue for pending write batches. 挂起写批次的元数据队列。
@ FAILED
操作失败 | Operation failed

◆ WriteFun()

static ErrorCode LibXR::LinuxUART::WriteFun ( WritePort & port,
bool  )
inlinestatic

Definition at line 449 of file linux_uart.hpp.

450 {
451 auto* uart = LibXR::ContainerOf(&port, &LinuxUART::_write_port);
452 uart->write_sem_.Post();
453 return ErrorCode::PENDING;
454 }
OwnerType * ContainerOf(MemberType *ptr, MemberType OwnerType::*member) noexcept
通过成员指针恢复其所属对象指针

Field Documentation

◆ _read_port

ReadPort LibXR::LinuxUART::_read_port
private

Definition at line 545 of file linux_uart.hpp.

◆ _write_port

WritePort LibXR::LinuxUART::_write_port
private

Definition at line 546 of file linux_uart.hpp.

◆ buff_size_

size_t LibXR::LinuxUART::buff_size_ = 0
private

Definition at line 541 of file linux_uart.hpp.

◆ config_

Configuration LibXR::LinuxUART::config_
private

Definition at line 535 of file linux_uart.hpp.

◆ connected_

bool LibXR::LinuxUART::connected_ = true
private

Definition at line 534 of file linux_uart.hpp.

◆ device_path_

std::string LibXR::LinuxUART::device_path_
private

Definition at line 536 of file linux_uart.hpp.

◆ fd_

int LibXR::LinuxUART::fd_ = -1
private

Definition at line 533 of file linux_uart.hpp.

◆ read_mutex_

Mutex LibXR::LinuxUART::read_mutex_
private

Definition at line 543 of file linux_uart.hpp.

◆ rx_buff_

uint8_t* LibXR::LinuxUART::rx_buff_ = nullptr
private

Definition at line 539 of file linux_uart.hpp.

◆ rx_thread_

Thread LibXR::LinuxUART::rx_thread_
private

Definition at line 537 of file linux_uart.hpp.

◆ tx_buff_

uint8_t* LibXR::LinuxUART::tx_buff_ = nullptr
private

Definition at line 540 of file linux_uart.hpp.

◆ tx_thread_

Thread LibXR::LinuxUART::tx_thread_
private

Definition at line 538 of file linux_uart.hpp.

◆ write_sem_

Semaphore LibXR::LinuxUART::write_sem_
private

Definition at line 542 of file linux_uart.hpp.


The documentation for this class was generated from the following file: