libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ep_pool.cpp
1#include "ep_pool.hpp"
2
3using namespace LibXR::USB;
4
5EndpointPool::EndpointPool(size_t endpoint_num)
6 : LockFreePool<Endpoint*>(endpoint_num - 2)
7{
8 ASSERT(endpoint_num >= 2);
9}
10
11ErrorCode EndpointPool::Get(Endpoint*& ep_info, Endpoint::Direction direction,
12 Endpoint::EPNumber ep_num)
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}
36
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}
60
61ErrorCode EndpointPool::FindEndpoint(uint8_t ep_addr, Endpoint*& ans)
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}
91
93
95
97{
98 ep0_in_ = ep0_in;
99 ep0_out_ = ep0_out;
100}
无锁无序槽池 / Lock-free, unordered slot pool
ErrorCode Get(Data &data)
从池中取出一个元素 / Retrieve an element from the pool
USB端点基类 / USB Endpoint base class.
Definition ep.hpp:22
EPNumber
端点号 / Endpoint number
Definition ep.hpp:39
@ EP_AUTO
自动分配端点号 / Auto allocate
Direction
端点方向 Endpoint direction
Definition ep.hpp:29
@ BOTH
双向(可配置成IN/OUT) / Both (can be configured as IN/OUT)
@ 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
ErrorCode FindEndpoint(uint8_t ep_addr, Endpoint *&ans)
查找端点/ Lookup endpoint
Definition ep_pool.cpp:61
EndpointPool(size_t endpoint_num)
构造函数 / Constructor
Definition ep_pool.cpp:5
Endpoint * GetEndpoint0In()
获取端点0的IN对象 / Get Endpoint 0's IN object
Definition ep_pool.cpp:94
void SetEndpoint0(Endpoint *ep0_in, Endpoint *ep0_out)
设置端点0的IN/OUT对象 / Set Endpoint 0 IN/OUT objects
Definition ep_pool.cpp:96
Endpoint * ep0_in_
端点0 IN对象 / Endpoint 0 IN pointer
Definition ep_pool.hpp:81
ErrorCode Get(Endpoint *&ep_info, Endpoint::Direction direction, Endpoint::EPNumber ep_num=Endpoint::EPNumber::EP_AUTO)
分配端点 / Allocate endpoint
Definition ep_pool.cpp:11
Endpoint * ep0_out_
端点0 OUT对象 / Endpoint 0 OUT pointer
Definition ep_pool.hpp:82
Endpoint * GetEndpoint0Out()
获取端点0的OUT对象 / Get Endpoint 0's OUT object
Definition ep_pool.cpp:92
ErrorCode Release(Endpoint *ep_info)
回收端点 / Release endpoint
Definition ep_pool.cpp:37