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

BOS 管理器:能力收集、BOS 描述符拼装、Vendor 请求链式分发 BOS manager: capability collection, descriptor building, vendor dispatch. More...

#include <bos.hpp>

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

Public Member Functions

 BosManager (size_t buffer_size, size_t cap_num)
 构造函数 / Constructor
 
 ~BosManager ()
 析构函数 / Destructor
 
 BosManager (const BosManager &)=delete
 
BosManageroperator= (const BosManager &)=delete
 
void ClearCapabilities ()
 清空已注册能力 / Clear registered capabilities
 
bool AddCapability (BosCapability *cap)
 添加能力 / Add capability
 
ConstRawData GetBosDescriptor ()
 构建 BOS 描述符(BOS 头 + 能力块) Build BOS descriptor (header + blocks).
 
ErrorCode ProcessVendorRequest (bool in_isr, const SetupPacket *setup, BosVendorResult &result)
 Vendor 请求分发 / Vendor request dispatch.
 

Private Attributes

size_t cap_capacity_ = 0
 能力指针容量 / Capability pointer capacity
 
BosCapability ** caps_ = nullptr
 能力指针表 / Capability pointer table
 
size_t count_ = 0
 已注册能力数量 / Registered capability count
 
size_t bos_desc_size_ = 0
 最近一次构建大小 / Last built size
 
RawData bos_buffer_ = {nullptr, 0}
 BOS 缓冲区 / BOS buffer.
 

Detailed Description

BOS 管理器:能力收集、BOS 描述符拼装、Vendor 请求链式分发 BOS manager: capability collection, descriptor building, vendor dispatch.

Note
仅拥有内部数组/缓冲区,不拥有 BosCapability 对象。

Definition at line 115 of file bos.hpp.

Constructor & Destructor Documentation

◆ BosManager()

LibXR::USB::BosManager::BosManager ( size_t buffer_size,
size_t cap_num )
inline

构造函数 / Constructor

Parameters
buffer_sizeBOS 描述符缓冲区大小 / BOS buffer size
cap_num能力指针容量 / Capability pointer capacity

Definition at line 124 of file bos.hpp.

125 : cap_capacity_(cap_num),
126 caps_(new BosCapability*[cap_num]),
127 bos_buffer_({new uint8_t[buffer_size], buffer_size})
128 {
129 }
size_t cap_capacity_
能力指针容量 / Capability pointer capacity
Definition bos.hpp:291
BosCapability ** caps_
能力指针表 / Capability pointer table
Definition bos.hpp:292
RawData bos_buffer_
BOS 缓冲区 / BOS buffer.
Definition bos.hpp:296

◆ ~BosManager()

LibXR::USB::BosManager::~BosManager ( )
inline

析构函数 / Destructor

Definition at line 134 of file bos.hpp.

135 {
136 delete[] caps_;
137 caps_ = nullptr;
138
139 delete[] reinterpret_cast<uint8_t*>(bos_buffer_.addr_);
140 bos_buffer_ = {nullptr, 0};
141
142 cap_capacity_ = 0;
143 count_ = 0;
144 bos_desc_size_ = 0;
145 }
void * addr_
数据存储地址。 The storage address of the data.
size_t count_
已注册能力数量 / Registered capability count
Definition bos.hpp:293
size_t bos_desc_size_
最近一次构建大小 / Last built size
Definition bos.hpp:295

Member Function Documentation

◆ AddCapability()

bool LibXR::USB::BosManager::AddCapability ( BosCapability * cap)
inline

添加能力 / Add capability

Parameters
cap能力指针 / Capability pointer
Returns
true:成功;false:失败 / true: success; false: failure

Definition at line 165 of file bos.hpp.

166 {
167 ASSERT(bos_buffer_.addr_);
168 ASSERT(cap != nullptr);
169 ASSERT(count_ < cap_capacity_);
170
171 caps_[count_++] = cap;
172 return true;
173 }

◆ ClearCapabilities()

void LibXR::USB::BosManager::ClearCapabilities ( )
inline

清空已注册能力 / Clear registered capabilities

Definition at line 153 of file bos.hpp.

154 {
155 count_ = 0;
156 bos_desc_size_ = 0;
157 }

◆ GetBosDescriptor()

ConstRawData LibXR::USB::BosManager::GetBosDescriptor ( )
inline

构建 BOS 描述符(BOS 头 + 能力块) Build BOS descriptor (header + blocks).

Returns
ConstRawData BOS 描述符数据 / BOS descriptor bytes

Definition at line 181 of file bos.hpp.

182 {
183 ASSERT(bos_buffer_.addr_);
184
185 bool has_usb2_ext = false;
186 for (size_t i = 0; i < count_; ++i)
187 {
188 ConstRawData blk = caps_[i]->GetCapabilityDescriptor();
189 ASSERT(blk.addr_ != nullptr);
190 ASSERT(blk.size_ >= 3);
191
192 const uint8_t* p = reinterpret_cast<const uint8_t*>(blk.addr_);
193 if (p[1] == DESCRIPTOR_TYPE_DEVICE_CAPABILITY &&
194 p[2] == DEV_CAPABILITY_TYPE_USB20EXT)
195 {
196 has_usb2_ext = true;
197 }
198 }
199
200 static constexpr uint8_t USB2_EXT_CAP[USB2_EXT_CAP_SIZE] = {7, 0x10, 0x02, 0x00,
201 0x00, 0x00, 0x00};
202 const bool NEED_AUTO_USB2_EXT = !has_usb2_ext;
203
204 size_t total = BOS_HEADER_SIZE;
205 for (size_t i = 0; i < count_; ++i)
206 {
207 ConstRawData blk = caps_[i]->GetCapabilityDescriptor();
208 ASSERT(blk.addr_ != nullptr);
209 ASSERT(blk.size_ >= 1);
210 total += blk.size_;
211 }
212 if (NEED_AUTO_USB2_EXT)
213 {
214 total += sizeof(USB2_EXT_CAP);
215 }
216
217 ASSERT(total <= bos_buffer_.size_);
218 ASSERT(total <= 0xFFFF);
219
220 auto buffer = reinterpret_cast<uint8_t*>(bos_buffer_.addr_);
221
222 buffer[0] = static_cast<uint8_t>(BOS_HEADER_SIZE);
223 buffer[1] = DESCRIPTOR_TYPE_BOS;
224 buffer[2] = static_cast<uint8_t>(total & 0xFF);
225 buffer[3] = static_cast<uint8_t>((total >> 8) & 0xFF);
226 buffer[4] = static_cast<uint8_t>(count_ + (NEED_AUTO_USB2_EXT ? 1 : 0));
227
228 size_t offset = BOS_HEADER_SIZE;
229
230 for (size_t i = 0; i < count_; ++i)
231 {
232 ConstRawData blk = caps_[i]->GetCapabilityDescriptor();
233 ASSERT(offset + blk.size_ <= bos_buffer_.size_);
234
235 LibXR::Memory::FastCopy(&buffer[offset], blk.addr_, blk.size_);
236 offset += blk.size_;
237 }
238
239 if (NEED_AUTO_USB2_EXT)
240 {
241 ASSERT(offset + sizeof(USB2_EXT_CAP) <= bos_buffer_.size_);
242 LibXR::Memory::FastCopy(&buffer[offset], USB2_EXT_CAP, sizeof(USB2_EXT_CAP));
243 offset += sizeof(USB2_EXT_CAP);
244 }
245
246 bos_desc_size_ = offset;
247 return {buffer, offset};
248 }
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:3
size_t size_
数据大小(字节)。 The size of the data (in bytes).
virtual ConstRawData GetCapabilityDescriptor() const =0
返回能力块(不含 BOS 头) Return capability block (without BOS header).

◆ ProcessVendorRequest()

ErrorCode LibXR::USB::BosManager::ProcessVendorRequest ( bool in_isr,
const SetupPacket * setup,
BosVendorResult & result )
inline

Vendor 请求分发 / Vendor request dispatch.

Parameters
in_isrISR 上下文标志 / ISR context flag
setupSetup 包 / Setup packet
result输出结果 / Output result
Returns
OK:已处理;NOT_SUPPORT:无匹配;其他:匹配但失败

Definition at line 258 of file bos.hpp.

260 {
261 if (setup == nullptr)
262 {
263 return ErrorCode::ARG_ERR;
264 }
265
266 for (size_t i = 0; i < count_; ++i)
267 {
268 BosVendorResult tmp{};
269 auto ec = caps_[i]->OnVendorRequest(in_isr, setup, tmp);
270
271 if (ec == ErrorCode::NOT_SUPPORT)
272 {
273 continue;
274 }
275 if (ec != ErrorCode::OK)
276 {
277 return ec;
278 }
279 if (tmp.handled)
280 {
281 result = tmp;
282 return ErrorCode::OK;
283 }
284 return ErrorCode::FAILED;
285 }
286
287 return ErrorCode::NOT_SUPPORT;
288 }
virtual ErrorCode OnVendorRequest(bool, const SetupPacket *, BosVendorResult &)
处理该能力相关 Vendor 请求 / Handle vendor request for this capability
Definition bos.hpp:76

Field Documentation

◆ bos_buffer_

RawData LibXR::USB::BosManager::bos_buffer_ = {nullptr, 0}
private

BOS 缓冲区 / BOS buffer.

Definition at line 296 of file bos.hpp.

296{nullptr, 0};

◆ bos_desc_size_

size_t LibXR::USB::BosManager::bos_desc_size_ = 0
private

最近一次构建大小 / Last built size

Definition at line 295 of file bos.hpp.

◆ cap_capacity_

size_t LibXR::USB::BosManager::cap_capacity_ = 0
private

能力指针容量 / Capability pointer capacity

Definition at line 291 of file bos.hpp.

◆ caps_

BosCapability** LibXR::USB::BosManager::caps_ = nullptr
private

能力指针表 / Capability pointer table

Definition at line 292 of file bos.hpp.

◆ count_

size_t LibXR::USB::BosManager::count_ = 0
private

已注册能力数量 / Registered capability count

Definition at line 293 of file bos.hpp.


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