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/select.h>
5#include <sys/time.h>
6#include <sys/types.h>
7#include <termios.h>
8#include <unistd.h>
9
10#include <cstddef>
11
12#include "libxr_def.hpp"
13#include "libxr_rw.hpp"
14#include "libxr_type.hpp"
15#include "linux_timebase.hpp"
16
17struct timeval libxr_linux_start_time;
18
19struct timespec libxr_linux_start_time_spec; // NOLINT
20
21static LibXR::LinuxTimebase libxr_linux_timebase;
22
24{
25 auto write_fun = [](WritePort &port)
26 {
27 static uint8_t write_buff[1024];
29 while (true)
30 {
31 if (port.queue_info_->Pop(info) != ErrorCode::OK)
32 {
33 return ErrorCode::OK;
34 }
35
36 port.queue_data_->PopBatch(write_buff, info.size);
37 auto ans = fwrite(write_buff, 1, info.size, stdout);
38 UNUSED(ans);
39 port.queue_info_->Pop(info);
40
41 port.UpdateStatus(false, ErrorCode::OK, info.op, info.size);
42 }
43
44 return ErrorCode::OK;
45 };
46
48 new LibXR::WritePort(32, static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
49
50 *LibXR::STDIO::write_ = write_fun;
51
52 auto read_fun = [](ReadPort &port)
53 {
54 ReadInfoBlock info;
55 port.queue_block_->Pop(info);
56
57 auto need_read = info.data_.size_;
58
59 port.read_size_ = fread(info.data_.addr_, sizeof(char), need_read, stdin);
60 port.UpdateStatus(false, ErrorCode::OK, info, need_read);
61 return ErrorCode::OK;
62 };
63
65 new LibXR::ReadPort(32, static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
66
67 *LibXR::STDIO::read_ = read_fun;
68
69 gettimeofday(&libxr_linux_start_time, nullptr);
70 UNUSED(clock_gettime(CLOCK_REALTIME, &libxr_linux_start_time_spec));
71
72 struct termios tty;
73 tcgetattr(STDIN_FILENO, &tty); // 获取当前终端属性
74 tty.c_lflag &= ~(ICANON | ECHO); // 禁用规范模式和回显
75 tcsetattr(STDIN_FILENO, TCSANOW, &tty); // 立即生效
76}
LinuxTimebase 类,用于获取 Linux 系统的时间基准。Provides a timebase for Linux systems.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
Read information block structure.
Definition libxr_rw.hpp:286
RawData data_
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:288
ReadPort class for handling read operations.
Definition libxr_rw.hpp:309
static ReadPort * read_
Read port instance. 读取端口。
Definition libxr_rw.hpp:693
static WritePort * write_
Write port instance. 写入端口。
Definition libxr_rw.hpp:694
WritePort class for handling write operations.
Definition libxr_rw.hpp:503
void PlatformInit()
平台初始化函数 Platform initialization function