7#include "libxr_def.hpp"
8#include "libxr_mem.hpp"
9#include "libxr_type.hpp"
10#include "usb/core/core.hpp"
19enum class DevCapabilityType : uint8_t
21 USB20_EXTENSION = 0x02,
24static constexpr uint8_t DESCRIPTOR_TYPE_BOS =
25 static_cast<uint8_t
>(DescriptorType::BOS);
27static constexpr uint8_t DESCRIPTOR_TYPE_DEVICE_CAPABILITY =
static_cast<uint8_t
>(
28 DescriptorType::DEVICE_CAPABILITY);
31static constexpr uint8_t DEV_CAPABILITY_TYPE_USB20EXT =
static_cast<uint8_t
>(
32 DevCapabilityType::USB20_EXTENSION);
35static constexpr size_t BOS_HEADER_SIZE =
37static constexpr size_t USB2_EXT_CAP_SIZE =
124 bos_buffer_({
new uint8_t[buffer_size], buffer_size})
145 ASSERT(cap !=
nullptr);
162 bool has_usb2_ext =
false;
163 for (
size_t i = 0; i <
count_; ++i)
166 ASSERT(blk.
addr_ !=
nullptr);
167 ASSERT(blk.
size_ >= 3);
169 const uint8_t* p =
reinterpret_cast<const uint8_t*
>(blk.
addr_);
170 if (p[1] == DESCRIPTOR_TYPE_DEVICE_CAPABILITY &&
171 p[2] == DEV_CAPABILITY_TYPE_USB20EXT)
177 static constexpr uint8_t USB2_EXT_CAP[USB2_EXT_CAP_SIZE] = {7, 0x10, 0x02, 0x00,
179 const bool NEED_AUTO_USB2_EXT = !has_usb2_ext;
181 size_t total = BOS_HEADER_SIZE;
182 for (
size_t i = 0; i <
count_; ++i)
185 ASSERT(blk.
addr_ !=
nullptr);
186 ASSERT(blk.
size_ >= 1);
189 if (NEED_AUTO_USB2_EXT)
191 total +=
sizeof(USB2_EXT_CAP);
195 ASSERT(total <= 0xFFFF);
199 buffer[0] =
static_cast<uint8_t
>(BOS_HEADER_SIZE);
200 buffer[1] = DESCRIPTOR_TYPE_BOS;
201 buffer[2] =
static_cast<uint8_t
>(total & 0xFF);
202 buffer[3] =
static_cast<uint8_t
>((total >> 8) & 0xFF);
203 buffer[4] =
static_cast<uint8_t
>(
count_ + (NEED_AUTO_USB2_EXT ? 1 : 0));
205 size_t offset = BOS_HEADER_SIZE;
207 for (
size_t i = 0; i <
count_; ++i)
216 if (NEED_AUTO_USB2_EXT)
220 offset +=
sizeof(USB2_EXT_CAP);
223 return {buffer, offset};
237 if (setup ==
nullptr)
242 for (
size_t i = 0; i <
count_; ++i)
常量原始数据封装类。 A class for encapsulating constant raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
BOS 能力接口 / BOS capability interface.
virtual ErrorCode OnVendorRequest(bool, const SetupPacket *, BosVendorResult &)
处理该能力相关 Vendor 请求 / Handle vendor request for this capability
virtual ConstRawData GetCapabilityDescriptor() const =0
返回能力块(不含 BOS 头) Return capability block (without BOS header).
BOS 能力提供者接口 / BOS capability provider interface.
virtual size_t GetBosCapabilityCount()
获取 BOS 能力数量 / Get BOS capability count
virtual BosCapability * GetBosCapability(size_t index)
获取指定索引的 BOS 能力 / Get BOS capability at index
BOS 管理器:能力收集、BOS 描述符拼装、Vendor 请求链式分发 BOS manager: capability collection, descriptor building,...
size_t cap_capacity_
能力指针容量 / Capability pointer capacity
size_t count_
已注册能力数量 / Registered capability count
bool AddCapability(BosCapability *cap)
添加能力 / Add capability
ConstRawData GetBosDescriptor()
构建 BOS 描述符(BOS 头 + 能力块) Build BOS descriptor (header + blocks).
void ClearCapabilities()
清空已注册能力 / Clear registered capabilities
BosManager(size_t buffer_size, size_t cap_num)
构造函数 / Constructor
ErrorCode ProcessVendorRequest(bool in_isr, const SetupPacket *setup, BosVendorResult &result)
Vendor 请求分发 / Vendor request dispatch.
BosCapability ** caps_
能力指针表 / Capability pointer table
RawData bos_buffer_
BOS 缓冲区 / BOS buffer.
@ NOT_SUPPORT
不支持 | Not supported
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
Vendor 请求处理结果(EP0 控制传输) Vendor request handling result for EP0 control transfers.
bool handled
已处理该请求 / Request consumed by capability
ConstRawData in_data
IN 数据阶段返回数据 / IN data stage payload.
bool write_zlp
状态阶段发送 ZLP / Send ZLP at status stage
bool early_read_zlp
提前准备 OUT 以适配主机短读 / Arm OUT early for short-read tolerance
USB 标准请求 SETUP 包(固定8字节) Standard USB setup packet (8 bytes)