libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
read_port.hpp
1#pragma once
2
3#include <atomic>
4#include <cstddef>
5#include <cstdint>
6
7#include "operation.hpp"
8#include "queue.hpp"
9
10namespace LibXR
11{
12
18{
19 public:
20 // Exposed low-level state and helpers for the read-path core. Some members stay public
21 // because low-level libxr tests and driver glue inspect them directly.
22 // 读路径核心的低层状态与辅助类型。部分成员保持 public,
23 // 是因为 libxr 的底层测试与驱动胶水层会直接检查它们。
24
25 // Read BLOCK states:
26 // PENDING = waiting for queue-fed completion after read_fun_ was notified
27 // CLEARING = ClearQueuedData() owns software dequeue progress
28 // BLOCK_CLAIMED = wakeup now belongs to the waiter
29 // BLOCK_DETACHED = timeout detached the waiter
30 // The same semaphore may be reused only after the previous BLOCK call
31 // returns and the port goes back to IDLE.
32 // 读 BLOCK 状态:
33 // PENDING = 已通知 read_fun_,等待队列侧完成
34 // CLEARING = ClearQueuedData() 占有软件出队进度
35 // BLOCK_CLAIMED = 唤醒已经归当前 waiter 所有
36 // BLOCK_DETACHED = timeout 已把 waiter 分离
37 // 同一个信号量只能在上一次 BLOCK 调用返回、端口回到 IDLE 后复用。
38 enum class BusyState : uint32_t
39 {
40 IDLE = 0,
41 PENDING = 1,
43 CLEARING = 2,
45 BLOCK_CLAIMED = 3,
47 BLOCK_DETACHED = 4,
49 EVENT = UINT32_MAX
51 };
52
54 nullptr;
57 std::atomic<BusyState> busy_{
60
70 ReadPort(size_t buffer_size = 128);
71
83 virtual ~ReadPort() = default;
84
95 size_t EmptySize();
96
107 size_t Size();
108
111 bool Readable();
112
127
139 void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock& info);
140
151 void MarkAsRunning(ReadInfoBlock& info);
152
177 ErrorCode operator()(RawData data, ReadOperation& op, bool in_isr = false);
178
186 virtual void OnRxDequeue(bool) {}
187
209 [[nodiscard]] ErrorCode ClearQueuedData(bool in_isr = false);
210
218 void ProcessPendingReads(bool in_isr);
219
235 void FailAndClearAll(ErrorCode reason, bool in_isr);
236};
237
238} // namespace LibXR
可写原始数据视图 / Mutable raw data view
ReadPort class for handling read operations.
Definition read_port.hpp:18
bool Readable()
Checks if read operations are supported.
Definition read_port.cpp:26
ReadFun read_fun_
Driver/backend read notification entry. 底层驱动或后端读取通知入口。
Definition read_port.hpp:53
virtual ~ReadPort()=default
虚析构函数,保证通过基类指针析构派生读端口的安全性。 Virtual destructor for safe destruction of derived read ports through a ba...
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info)
完成已由队列路径认领的读取操作。 Completes a read operation already claimed by the queue path.
Definition read_port.cpp:34
ReadPort(size_t buffer_size=128)
Constructs a ReadPort with queue sizes.
Definition read_port.cpp:7
ErrorCode ClearQueuedData(bool in_isr=false)
清空当前已排队的 RX 字节。
ErrorCode block_result_
Final status for the current BLOCK read.
Definition read_port.hpp:59
@ IDLE
No active waiter and no pending completion. 无等待者、无挂起完成。
std::atomic< BusyState > busy_
Shared read-progress handoff state. 共享的读进度交接状态。
Definition read_port.hpp:57
size_t EmptySize()
获取队列的剩余可用空间。 Gets the remaining available space in the queue.
Definition read_port.cpp:14
ErrorCode operator()(RawData data, ReadOperation &op, bool in_isr=false)
读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.
Definition read_port.cpp:54
SPSCQueue< uint8_t > * queue_data_
RX payload queue. 接收数据字节队列。
Definition read_port.hpp:55
void ProcessPendingReads(bool in_isr)
Processes pending reads.
ReadPort & operator=(ReadFun fun)
赋值运算符重载,用于设置读取函数。 Overloaded assignment operator to set the read function.
Definition read_port.cpp:28
size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
Definition read_port.cpp:20
void FailAndClearAll(ErrorCode reason, bool in_isr)
失败完成并清空当前所有挂起读操作。
virtual void OnRxDequeue(bool)
RX 数据从软件队列成功出队后的通知。 Notification after bytes are popped from RX data queue.
ReadInfoBlock info_
In-flight read request metadata. 当前在途读取请求的元数据。
Definition read_port.hpp:56
void MarkAsRunning(ReadInfoBlock &info)
标记读取操作为运行中。 Marks the read operation as running.
Definition read_port.cpp:52
单生产者单消费者无锁队列。
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read notifications.
Read information block structure.