libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::USB::EndpointPool Class Reference

USB端点池类 / USB endpoint pool class. More...

#include <ep_pool.hpp>

Inheritance diagram for LibXR::USB::EndpointPool:
[legend]
Collaboration diagram for LibXR::USB::EndpointPool:
[legend]

Public Member Functions

 EndpointPool (size_t endpoint_num)
 构造函数 / Constructor
 
ErrorCode Get (Endpoint *&ep_info, Endpoint::Direction direction, Endpoint::EPNumber ep_num=Endpoint::EPNumber::EP_AUTO)
 分配端点 / Allocate endpoint
 
ErrorCode Release (Endpoint *ep_info)
 回收端点 / Release endpoint
 
ErrorCode FindEndpoint (uint8_t ep_addr, Endpoint *&ans)
 查找端点/ Lookup endpoint
 
EndpointGetEndpoint0Out ()
 获取端点0的OUT对象 / Get Endpoint 0's OUT object
 
EndpointGetEndpoint0In ()
 获取端点0的IN对象 / Get Endpoint 0's IN object
 
void SetEndpoint0 (Endpoint *ep0_in, Endpoint *ep0_out)
 设置端点0的IN/OUT对象 / Set Endpoint 0 IN/OUT objects
 

Private Attributes

Endpointep0_in_ = nullptr
 端点0 IN对象 / Endpoint 0 IN pointer
 
Endpointep0_out_ = nullptr
 端点0 OUT对象 / Endpoint 0 OUT pointer
 

Additional Inherited Members

- Protected Types inherited from LibXR::LockFreePool< Endpoint * >
enum  SlotState
 槽状态 / Slot state More...
 
- Protected Member Functions inherited from LibXR::LockFreePool< Endpoint * >
Slot & operator[] (uint32_t index)
 
 LockFreePool (uint32_t slot_count)
 构造对象池 / Constructor for the pool
 
 ~LockFreePool ()
 析构,释放槽池内存 / Destructor, releasing pool memory
 
ErrorCode Put (const Endpoint *&data)
 向池中放入一个元素 / Put an element into the pool
 
ErrorCode Put (const Endpoint *&data, uint32_t &start_index)
 向池中放入一个元素,返回起始槽索引 / Put an element into the pool and return the starting slot index
 
ErrorCode PutToSlot (const Endpoint *&data, uint32_t index)
 向指定槽放入一个元素 / Put an element into a specific slot
 
ErrorCode Get (Endpoint *&data)
 从池中取出一个元素 / Retrieve an element from the pool
 
ErrorCode Get (Endpoint *&data, uint32_t &start_index)
 从指定槽位开始,取出一个元素 / Retrieve an element from the pool
 
ErrorCode GetFromSlot (Endpoint *&data, uint32_t index)
 从指定槽位开始,取出一个元素 / Retrieve an element from the pool
 
ErrorCode RecycleSlot (uint32_t index)
 回收指定槽位 / Recycle a slot
 
size_t Size () const
 查询池中可取元素数量 / Query the number of available elements in the pool
 
size_t EmptySize ()
 查询当前池可用槽数量 / Query the number of writable slots in the pool
 
uint32_t SlotCount () const
 获取槽总数 / Get the total number of slots in the pool
 

Detailed Description

USB端点池类 / USB endpoint pool class.

继承自 LockFreePool<Endpoint*>,用于高效管理 USB 端点对象指针。 所有 Endpoint* 在池初始化时填充,池只负责分配、查找与回收,不负责对象生命周期。

Inherited from LockFreePool<Endpoint*>, this pool manages fixed Endpoint pointers. All Endpoint objects must be valid during the pool's lifetime.

Definition at line 22 of file ep_pool.hpp.

Constructor & Destructor Documentation

◆ EndpointPool()

EndpointPool::EndpointPool ( size_t endpoint_num)

构造函数 / Constructor

Parameters
endpoint_num端点总数(包含端点0,必须 >=2)/ Total number of endpoints (including EP0, must >=2)

Definition at line 5 of file ep_pool.cpp.

6 : LockFreePool<Endpoint*>(endpoint_num - 2)
7{
8 ASSERT(endpoint_num >= 2);
9}

Member Function Documentation

◆ FindEndpoint()

ErrorCode EndpointPool::FindEndpoint ( uint8_t ep_addr,
Endpoint *& ans )

查找端点/ Lookup endpoint

Parameters
ep_addr端点地址,IN端点高位需加0x80 / Endpoint address (0x80 for IN endpoints)
[out]ans查找到的端点对象指针 / Found endpoint pointer
Return values
ErrorCode操作结果(OK/NOT_FOUND)/ Operation result

Definition at line 61 of file ep_pool.cpp.

62{
64
65 if (ep_addr & 0x80)
66 {
67 ep_addr &= 0x7F; // IN端点地址
68 direction = Endpoint::Direction::IN;
69 }
70
71 for (uint32_t i = 0; i < SlotCount(); ++i)
72 {
73 auto& slot_container = (*this)[i];
74 auto state = slot_container.slot.state.load(std::memory_order_acquire);
75 if (state == SlotState::READY &&
76 Endpoint::EPNumberToAddr(slot_container.slot.data->GetNumber(),
77 slot_container.slot.data->GetDirection()) == ep_addr &&
78 slot_container.slot.data->GetDirection() == direction)
79 {
80 ans = slot_container.slot.data;
81 return ErrorCode::OK;
82 }
83 if (state == SlotState::FREE)
84 {
85 break; // 没有找到,直接退出
86 }
87 }
88
89 return ErrorCode::NOT_FOUND;
90}
Direction
端点方向 Endpoint direction
Definition ep.hpp:29
@ IN
输入方向 / IN direction
@ OUT
输出方向 / OUT direction
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Definition ep.hpp:104

◆ Get()

ErrorCode EndpointPool::Get ( Endpoint *& ep_info,
Endpoint::Direction direction,
Endpoint::EPNumber ep_num = Endpoint::EPNumber::EP_AUTO )

分配端点 / Allocate endpoint

Parameters
[out]ep_info分配得到的端点对象指针 / Allocated endpoint pointer
direction端点方向(IN/OUT/BOTH)/ Endpoint direction
ep_num指定端点号/ Endpoint number 0)
Return values
ErrorCode操作结果(OK/NOT_FOUND)/ Operation result

Definition at line 11 of file ep_pool.cpp.

13{
14 for (uint32_t i = 0; i < SlotCount(); ++i)
15 {
16 auto& slot_container = (*this)[i];
17 auto state = slot_container.slot.state.load(std::memory_order_acquire);
18 if (state == SlotState::READY)
19 {
20 if (ep_num != Endpoint::EPNumber::EP_AUTO &&
21 ep_num != slot_container.slot.data->GetNumber())
22 {
23 continue; // 如果指定了端点号,则跳过不匹配的
24 }
25
26 if (slot_container.slot.data->AvailableDirection() == direction ||
27 slot_container.slot.data->AvailableDirection() == Endpoint::Direction::BOTH)
28 {
30 return ErrorCode::OK;
31 }
32 }
33 }
34 return ErrorCode::NOT_FOUND;
35}
ErrorCode Get(Data &data)
从池中取出一个元素 / Retrieve an element from the pool
@ EP_AUTO
自动分配端点号 / Auto allocate
@ BOTH
双向(可配置成IN/OUT) / Both (can be configured as IN/OUT)

◆ GetEndpoint0In()

Endpoint * EndpointPool::GetEndpoint0In ( )

获取端点0的IN对象 / Get Endpoint 0's IN object

Returns
Endpoint* 端点0 IN对象指针 / Endpoint 0 IN pointer

Definition at line 94 of file ep_pool.cpp.

94{ return ep0_in_; }
Endpoint * ep0_in_
端点0 IN对象 / Endpoint 0 IN pointer
Definition ep_pool.hpp:81

◆ GetEndpoint0Out()

Endpoint * EndpointPool::GetEndpoint0Out ( )

获取端点0的OUT对象 / Get Endpoint 0's OUT object

Returns
Endpoint* 端点0 OUT对象指针 / Endpoint 0 OUT pointer

Definition at line 92 of file ep_pool.cpp.

92{ return ep0_out_; }
Endpoint * ep0_out_
端点0 OUT对象 / Endpoint 0 OUT pointer
Definition ep_pool.hpp:82

◆ Release()

ErrorCode EndpointPool::Release ( Endpoint * ep_info)

回收端点 / Release endpoint

Parameters
ep_info待回收的端点对象指针 / Endpoint pointer to release
Return values
ErrorCode操作结果(OK/NOT_FOUND)/ Operation result

Definition at line 37 of file ep_pool.cpp.

38{
39 for (uint32_t i = 0; i < SlotCount(); ++i)
40 {
41 auto& slot = (*this)[i];
42 auto state = slot.slot.state.load(std::memory_order_acquire);
43 if (state == SlotState::RECYCLE)
44 {
45 if (slot.slot.data == ep_info)
46 {
47 slot.slot.state.store(SlotState::READY, std::memory_order_release);
48 return ErrorCode::OK;
49 }
50 }
51
52 if (state == SlotState::FREE)
53 {
54 break;
55 }
56 }
57
58 return ErrorCode::NOT_FOUND;
59}

◆ SetEndpoint0()

void EndpointPool::SetEndpoint0 ( Endpoint * ep0_in,
Endpoint * ep0_out )

设置端点0的IN/OUT对象 / Set Endpoint 0 IN/OUT objects

Parameters
ep0_in端点0 IN对象指针 / Endpoint 0 IN pointer
ep0_out端点0 OUT对象指针 / Endpoint 0 OUT pointer

Definition at line 96 of file ep_pool.cpp.

97{
98 ep0_in_ = ep0_in;
99 ep0_out_ = ep0_out;
100}

Field Documentation

◆ ep0_in_

Endpoint* LibXR::USB::EndpointPool::ep0_in_ = nullptr
private

端点0 IN对象 / Endpoint 0 IN pointer

Definition at line 81 of file ep_pool.hpp.

◆ ep0_out_

Endpoint* LibXR::USB::EndpointPool::ep0_out_ = nullptr
private

端点0 OUT对象 / Endpoint 0 OUT pointer

Definition at line 82 of file ep_pool.hpp.


The documentation for this class was generated from the following files: