libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_system.cpp
1#include "libxr_system.hpp"
2
3#include <sys/ioctl.h>
4#include <sys/select.h>
5#include <sys/types.h>
6#include <termios.h>
7#include <unistd.h>
8
9#include <cstddef>
10
11#include "libxr_def.hpp"
12#include "libxr_rw.hpp"
13#include "libxr_type.hpp"
14#include "linux_timebase.hpp"
15#include "logger.hpp"
16#include "thread.hpp"
17#include "timer.hpp"
18
19struct timespec libxr_linux_start_time_spec; // NOLINT
20
21static LibXR::LinuxTimebase libxr_linux_timebase;
22
23static LibXR::Semaphore stdo_sem;
24static constexpr size_t host_stdio_queue_bytes = 4096;
25
26void StdiThread(LibXR::ReadPort* read_port)
27{
28 static uint8_t read_buff[host_stdio_queue_bytes];
29
30 if (!isatty(STDIN_FILENO))
31 {
32 XR_LOG_WARN("STDIO.read_: stdin is not a TTY, parking thread forever");
33 while (true)
34 {
35 LibXR::Thread::Sleep(UINT32_MAX);
36 }
37 }
38
39 while (true)
40 {
41 fd_set rfds;
42 FD_ZERO(&rfds);
43 FD_SET(STDIN_FILENO, &rfds);
44
45 int ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
46
47 if (ret > 0 && FD_ISSET(STDIN_FILENO, &rfds))
48 {
49 int ready = 0;
50 if (ioctl(STDIN_FILENO, FIONREAD, &ready) != -1 && ready > 0)
51 {
52 auto size = fread(read_buff, sizeof(char), ready, stdin);
53 if (size < 1)
54 {
55 continue;
56 }
57 read_port->queue_data_->PushBatch(read_buff, size);
58 read_port->ProcessPendingReads(false);
59 }
60 }
61 }
62}
63
64void StdoThread(LibXR::WritePort* write_port)
65{
67 static uint8_t write_buff[host_stdio_queue_bytes];
68
69 while (true)
70 {
71 if (stdo_sem.Wait() == LibXR::ErrorCode::OK)
72 {
73 auto ans = write_port->queue_info_->Pop(info);
74 if (ans != LibXR::ErrorCode::OK)
75 {
76 continue;
77 }
78
79 ans = write_port->queue_data_->PopBatch(write_buff, info.data.size_);
80 if (ans != LibXR::ErrorCode::OK)
81 {
82 continue;
83 }
84
85 auto write_size = fwrite(write_buff, sizeof(char), info.data.size_, stdout);
86 auto fflush_ans = fflush(stdout);
87
88 UNUSED(write_size);
89 UNUSED(fflush_ans);
90
91 write_port->Finish(
92 false, write_size == info.data.size_ ? LibXR::ErrorCode::OK : LibXR::ErrorCode::FAILED, info);
93 }
94 }
95}
96
97void LibXR::PlatformInit(uint32_t timer_pri, uint32_t timer_stack_depth)
98{
99 LibXR::Timer::priority_ = static_cast<LibXR::Thread::Priority>(timer_pri);
100 LibXR::Timer::stack_depth_ = timer_stack_depth;
101 auto write_fun = [](WritePort& port, bool)
102 {
103 UNUSED(port);
104 stdo_sem.Post();
106 };
107
108 LibXR::STDIO::write_ = new LibXR::WritePort(32, host_stdio_queue_bytes);
109
110 *LibXR::STDIO::write_ = write_fun;
111
112 auto read_fun = [](ReadPort& port, bool)
113 {
114 UNUSED(port);
116 };
117
118 LibXR::STDIO::read_ = new LibXR::ReadPort(host_stdio_queue_bytes);
119
120 *LibXR::STDIO::read_ = read_fun;
121
122 UNUSED(clock_gettime(CLOCK_MONOTONIC, &libxr_linux_start_time_spec));
123
124 struct termios tty;
125 tcgetattr(STDIN_FILENO, &tty); // 获取当前终端属性
126 tty.c_lflag &= ~(ICANON | ECHO); // 禁用规范模式和回显
127 tcsetattr(STDIN_FILENO, TCSANOW, &tty); // 立即生效
128
129 LibXR::Thread stdi_thread, stdo_thread;
130 stdi_thread.Create<LibXR::ReadPort*>(LibXR::STDIO::read_, StdiThread, "STDIO.read_",
132
133 stdo_thread.Create<LibXR::WritePort*>(LibXR::STDIO::write_, StdoThread, "STDIO.write_",
135}
size_t size_
数据字节数 / Data size in bytes
Linux 时间基准实现 / Linux timebase implementation.
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
ReadPort class for handling read operations.
Definition read_port.hpp:18
LockFreeQueue< uint8_t > * queue_data_
RX payload queue. 接收数据字节队列。
Definition read_port.hpp:54
void ProcessPendingReads(bool in_isr)
Processes pending reads.
static ReadPort * read_
Read port instance. 读取端口。
Definition stdio.hpp:25
static WritePort * write_
Write port instance. 写入端口。
Definition stdio.hpp:26
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:44
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:53
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:18
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:25
@ MEDIUM
中等优先级 Medium 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
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:169
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:167
WritePort class for handling write operations.
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
@ PENDING
等待中 | Pending
@ OK
操作成功 | Operation successful
void PlatformInit(uint32_t timer_pri=2, uint32_t timer_stack_depth=65536)
平台初始化函数 Platform initialization function
ConstRawData data
Data buffer. 数据缓冲区。