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 while (true)
34 {
35 fd_set rfds;
36 FD_ZERO(&rfds);
37 FD_SET(STDIN_FILENO, &rfds);
38
39 int ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
40
41 if (ret > 0 && FD_ISSET(STDIN_FILENO, &rfds))
42 {
43 int ready = 0;
44 if (ioctl(STDIN_FILENO, FIONREAD, &ready) != -1 && ready > 0)
45 {
46 auto size = fread(read_buff, sizeof(char), ready, stdin);
47 if (size < 1)
48 {
49 continue;
50 }
51 read_port->queue_data_->PushBatch(read_buff, size);
52 read_port->ProcessPendingReads(false);
53 }
54 }
55 }
56}
57
58void StdoThread(LibXR::WritePort *write_port)
59{
61 static uint8_t write_buff[static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE)];
62
63 while (true)
64 {
65 if (stdo_sem.Wait() == ErrorCode::OK)
66 {
67 auto ans = write_port->queue_info_->Pop(info);
68 if (ans != ErrorCode::OK)
69 {
70 continue;
71 }
72
73 ans = write_port->queue_data_->PopBatch(write_buff, info.data.size_);
74 if (ans != ErrorCode::OK)
75 {
76 continue;
77 }
78
79 auto write_size = fwrite(write_buff, sizeof(char), info.data.size_, stdout);
80 fflush(stdout);
81 write_port->Finish(
82 false, write_size == info.data.size_ ? ErrorCode::OK : ErrorCode::FAILED, info,
83 write_size);
84 }
85 }
86}
87
88void LibXR::PlatformInit(uint32_t timer_pri, uint32_t timer_stack_depth)
89{
90 LibXR::Timer::priority_ = static_cast<LibXR::Thread::Priority>(timer_pri);
91 LibXR::Timer::stack_depth_ = timer_stack_depth;
92 auto write_fun = [](WritePort &port)
93 {
94 UNUSED(port);
95 stdo_sem.Post();
96 return ErrorCode::FAILED;
97 };
98
100 new LibXR::WritePort(32, static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
101
102 *LibXR::STDIO::write_ = write_fun;
103
104 auto read_fun = [](ReadPort &port)
105 {
106 UNUSED(port);
107 return ErrorCode::FAILED;
108 };
109
111 new LibXR::ReadPort(static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
112
113 *LibXR::STDIO::read_ = read_fun;
114
115 gettimeofday(&libxr_linux_start_time, nullptr);
116 UNUSED(clock_gettime(CLOCK_REALTIME, &libxr_linux_start_time_spec));
117
118 struct termios tty;
119 tcgetattr(STDIN_FILENO, &tty); // 获取当前终端属性
120 tty.c_lflag &= ~(ICANON | ECHO); // 禁用规范模式和回显
121 tcsetattr(STDIN_FILENO, TCSANOW, &tty); // 立即生效
122
123 LibXR::Thread stdi_thread, stdo_thread;
124 stdi_thread.Create<LibXR::ReadPort *>(LibXR::STDIO::read_, StdiThread, "STDIO.read_",
126
127 stdo_thread.Create<LibXR::WritePort *>(LibXR::STDIO::write_, StdoThread, "STDIO.write_",
129}
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:599
static WritePort * write_
Write port instance. 写入端口。
Definition libxr_rw.hpp:600
信号量类,实现线程同步机制 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
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:21
@ 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
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:166
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:164
WritePort class for handling write operations.
Definition libxr_rw.hpp:403
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