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
20struct timeval libxr_linux_start_time;
21
22struct timespec libxr_linux_start_time_spec; // NOLINT
23
24static LibXR::LinuxTimebase libxr_linux_timebase;
25
26static LibXR::Semaphore stdo_sem;
27
28void StdiThread(LibXR::ReadPort *read_port)
29{
30 static uint8_t read_buff[static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE)];
31
32 while (true)
33 {
34 fd_set rfds;
35 FD_ZERO(&rfds);
36 FD_SET(STDIN_FILENO, &rfds);
37
38 int ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
39
40 if (ret > 0 && FD_ISSET(STDIN_FILENO, &rfds))
41 {
42 int ready = 0;
43 if (ioctl(STDIN_FILENO, FIONREAD, &ready) != -1 && ready > 0)
44 {
45 auto size = fread(read_buff, sizeof(char), ready, stdin);
46 if (size < 1)
47 {
48 continue;
49 }
50 read_port->queue_data_->PushBatch(read_buff, size);
51 read_port->ProcessPendingReads(false);
52 }
53 }
54 }
55}
56
57void StdoThread(LibXR::WritePort *write_port)
58{
60 static uint8_t write_buff[static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE)];
61
62 while (true)
63 {
64 if (stdo_sem.Wait() == ErrorCode::OK)
65 {
66 auto ans = write_port->queue_info_->Pop(info);
67 if (ans != ErrorCode::OK)
68 {
69 continue;
70 }
71
72 ans = write_port->queue_data_->PopBatch(write_buff, info.data.size_);
73 if (ans != ErrorCode::OK)
74 {
75 continue;
76 }
77
78 auto write_size = fwrite(write_buff, sizeof(char), info.data.size_, stdout);
79 fflush(stdout);
80 write_port->Finish(
81 false, write_size == info.data.size_ ? ErrorCode::OK : ErrorCode::FAILED, info,
82 write_size);
83 }
84 }
85}
86
88{
89 auto write_fun = [](WritePort &port)
90 {
91 UNUSED(port);
92 stdo_sem.Post();
93 return ErrorCode::FAILED;
94 };
95
97 new LibXR::WritePort(32, static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
98
99 *LibXR::STDIO::write_ = write_fun;
100
101 auto read_fun = [](ReadPort &port)
102 {
103 UNUSED(port);
104 return ErrorCode::FAILED;
105 };
106
108 new LibXR::ReadPort(static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
109
110 *LibXR::STDIO::read_ = read_fun;
111
112 gettimeofday(&libxr_linux_start_time, nullptr);
113 UNUSED(clock_gettime(CLOCK_REALTIME, &libxr_linux_start_time_spec));
114
115 struct termios tty;
116 tcgetattr(STDIN_FILENO, &tty); // 获取当前终端属性
117 tty.c_lflag &= ~(ICANON | ECHO); // 禁用规范模式和回显
118 tcsetattr(STDIN_FILENO, TCSANOW, &tty); // 立即生效
119
120 LibXR::Thread stdi_thread, stdo_thread;
121 stdi_thread.Create<LibXR::ReadPort *>(LibXR::STDIO::read_, StdiThread, "STDIO.read_",
123
124 stdo_thread.Create<LibXR::WritePort *>(LibXR::STDIO::write_, StdoThread, "STDIO.write_",
126}
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:268
virtual void ProcessPendingReads(bool in_isr)
Processes pending reads.
Definition libxr_rw.cpp:127
static ReadPort * read_
Read port instance. 读取端口。
Definition libxr_rw.hpp:598
static WritePort * write_
Write port instance. 写入端口。
Definition libxr_rw.hpp:599
信号量类,实现线程同步机制 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:14
@ 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:64
WritePort class for handling write operations.
Definition libxr_rw.hpp:402
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
Definition libxr_rw.cpp:208
void PlatformInit()
平台初始化函数 Platform initialization function