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 <bits/types/FILE.h>
4#include <sys/ioctl.h>
5#include <sys/select.h>
6#include <sys/time.h>
7#include <sys/types.h>
8#include <termios.h>
9#include <unistd.h>
10
11#include <cstddef>
12
13#include "libxr_def.hpp"
14#include "libxr_rw.hpp"
15#include "libxr_type.hpp"
16#include "linux_timebase.hpp"
17#include "logger.hpp"
18#include "thread.hpp"
19#include "timer.hpp"
20
21struct timeval libxr_linux_start_time;
22
23struct timespec libxr_linux_start_time_spec; // NOLINT
24
25static LibXR::LinuxTimebase libxr_linux_timebase;
26
27static LibXR::Semaphore stdo_sem;
28
29void StdiThread(LibXR::ReadPort *read_port)
30{
31 static uint8_t read_buff[static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE)];
32
33 if (!isatty(STDIN_FILENO))
34 {
35 XR_LOG_WARN("STDIO.read_: stdin is not a TTY, parking thread forever");
36 while (true)
37 {
38 LibXR::Thread::Sleep(UINT32_MAX);
39 }
40 }
41
42 while (true)
43 {
44 fd_set rfds;
45 FD_ZERO(&rfds);
46 FD_SET(STDIN_FILENO, &rfds);
47
48 int ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
49
50 if (ret > 0 && FD_ISSET(STDIN_FILENO, &rfds))
51 {
52 int ready = 0;
53 if (ioctl(STDIN_FILENO, FIONREAD, &ready) != -1 && ready > 0)
54 {
55 auto size = fread(read_buff, sizeof(char), ready, stdin);
56 if (size < 1)
57 {
58 continue;
59 }
60 read_port->queue_data_->PushBatch(read_buff, size);
61 read_port->ProcessPendingReads(false);
62 }
63 }
64 }
65}
66
67void StdoThread(LibXR::WritePort *write_port)
68{
70 static uint8_t write_buff[static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE)];
71
72 while (true)
73 {
74 if (stdo_sem.Wait() == ErrorCode::OK)
75 {
76 auto ans = write_port->queue_info_->Pop(info);
77 if (ans != ErrorCode::OK)
78 {
79 continue;
80 }
81
82 ans = write_port->queue_data_->PopBatch(write_buff, info.data.size_);
83 if (ans != ErrorCode::OK)
84 {
85 continue;
86 }
87
88 auto write_size = fwrite(write_buff, sizeof(char), info.data.size_, stdout);
89 fflush(stdout);
90 write_port->Finish(
91 false, write_size == info.data.size_ ? ErrorCode::OK : ErrorCode::FAILED, info,
92 write_size);
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)
102 {
103 UNUSED(port);
104 stdo_sem.Post();
105 return ErrorCode::FAILED;
106 };
107
109 new LibXR::WritePort(32, static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
110
111 *LibXR::STDIO::write_ = write_fun;
112
113 auto read_fun = [](ReadPort &port)
114 {
115 UNUSED(port);
116 return ErrorCode::FAILED;
117 };
118
120 new LibXR::ReadPort(static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
121
122 *LibXR::STDIO::read_ = read_fun;
123
124 gettimeofday(&libxr_linux_start_time, nullptr);
125 UNUSED(clock_gettime(CLOCK_REALTIME, &libxr_linux_start_time_spec));
126
127 struct termios tty;
128 tcgetattr(STDIN_FILENO, &tty); // 获取当前终端属性
129 tty.c_lflag &= ~(ICANON | ECHO); // 禁用规范模式和回显
130 tcsetattr(STDIN_FILENO, TCSANOW, &tty); // 立即生效
131
132 LibXR::Thread stdi_thread, stdo_thread;
133 stdi_thread.Create<LibXR::ReadPort *>(LibXR::STDIO::read_, StdiThread, "STDIO.read_",
135
136 stdo_thread.Create<LibXR::WritePort *>(LibXR::STDIO::write_, StdoThread, "STDIO.write_",
138}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
LinuxTimebase 类,用于获取 Linux 系统的时间基准。Provides a timebase for Linux systems.
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 libxr_rw.hpp:269
virtual void ProcessPendingReads(bool in_isr)
Processes pending reads.
Definition libxr_rw.cpp:126
static ReadPort * read_
Read port instance. 读取端口。
Definition libxr_rw.hpp:605
static WritePort * write_
Write port instance. 写入端口。
Definition libxr_rw.hpp:606
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:23
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:16
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:23
@ 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:66
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:16
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.
Definition libxr_rw.hpp:406
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
Definition libxr_rw.cpp:207
void PlatformInit(uint32_t timer_pri=2, uint32_t timer_stack_depth=65536)
平台初始化函数 Platform initialization function
ConstRawData data
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:260