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
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 {
31 }
32 }
33 }
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);
49 }
50 }
51
52 if (state == SlotState::FREE)
53 {
54 break;
55 }
56 }
57
59}
60
62{
63 const Endpoint::Direction direction =
65
66 if ((ep_addr & 0x7F) == 0)
67 {
68 ans = (direction == Endpoint::Direction::IN) ? ep0_in_ : ep0_out_;
69 return (ans != nullptr) ? LibXR::ErrorCode::OK : LibXR::ErrorCode::NOT_FOUND;
70 }
71
72 for (uint32_t i = 0; i < SlotCount(); ++i)
73 {
74 auto& slot_container = (*this)[i];
75 auto state = slot_container.slot.state.load(std::memory_order_acquire);
76 if (state == SlotState::RECYCLE &&
77 slot_container.slot.data->GetAddress() == ep_addr &&
78 slot_container.slot.data->GetDirection() == direction)
79 {
80 ans = slot_container.slot.data;
82 }
83 if (state == SlotState::FREE)
84 {
85 break; // 没有找到,直接退出
86 }
87 }
88
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:24
EPNumber
端点号 Endpoint number
Definition ep.hpp:42
@ EP_AUTO
自动分配端点号 / Auto allocate
Direction
端点方向 Endpoint direction
Definition ep.hpp:31
@ BOTH
双向(可配置为 IN/OUT) / Both (configurable as IN/OUT)
@ IN
输入方向 / IN direction
@ OUT
输出方向 / OUT direction
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
ErrorCode Get(Endpoint *&ep_info, Endpoint::Direction direction, Endpoint::EPNumber ep_num=Endpoint::EPNumber::EP_AUTO)
分配端点 / Allocate endpoint
Definition ep_pool.cpp:11
ErrorCode Release(Endpoint *ep_info)
回收端点 / Release endpoint
Definition ep_pool.cpp:37
Endpoint * ep0_in_
端点0 IN对象 / Endpoint 0 IN pointer
Definition ep_pool.hpp:81
ErrorCode FindEndpoint(uint8_t ep_addr, Endpoint *&ans)
查找端点/ Lookup endpoint
Definition ep_pool.cpp:61
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
定义错误码枚举
@ NOT_FOUND
未找到 | Not found
@ OK
操作成功 | Operation successful