libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
bos.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "desc_dev.hpp"
7#include "libxr_def.hpp"
8#include "libxr_type.hpp"
9#include "usb/core/core.hpp"
10
11namespace LibXR::USB
12{
18enum class DevCapabilityType : uint8_t
19{
20 USB20_EXTENSION = 0x02,
21};
22
23static constexpr uint8_t DESCRIPTOR_TYPE_BOS =
24 static_cast<uint8_t>(DescriptorType::BOS);
25
26static constexpr uint8_t DESCRIPTOR_TYPE_DEVICE_CAPABILITY = static_cast<uint8_t>(
27 DescriptorType::DEVICE_CAPABILITY);
29
30static constexpr uint8_t DEV_CAPABILITY_TYPE_USB20EXT = static_cast<uint8_t>(
31 DevCapabilityType::USB20_EXTENSION);
33
34static constexpr size_t BOS_HEADER_SIZE =
35 5;
36static constexpr size_t USB2_EXT_CAP_SIZE =
37 7;
38
44{
45 bool handled = false;
46 ConstRawData in_data{nullptr, 0};
47 bool write_zlp = false;
49 true;
50};
51
56{
57 public:
58 virtual ~BosCapability() = default;
59
67
76 virtual ErrorCode OnVendorRequest(bool /*in_isr*/, const SetupPacket* /*setup*/,
77 BosVendorResult& /*result*/)
78 {
79 return ErrorCode::NOT_SUPPORT;
80 }
81};
82
87{
88 public:
89 virtual ~BosCapabilityProvider() = default;
90
95 virtual size_t GetBosCapabilityCount() { return 0; }
96
102 virtual BosCapability* GetBosCapability(size_t index)
103 {
104 UNUSED(index);
105 return nullptr;
106 }
107};
108
116{
117 public:
124 BosManager(size_t buffer_size, size_t cap_num)
125 : cap_capacity_(cap_num),
126 caps_(new BosCapability*[cap_num]),
127 bos_buffer_({new uint8_t[buffer_size], buffer_size})
128 {
129 }
130
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 }
146
147 BosManager(const BosManager&) = delete;
148 BosManager& operator=(const BosManager&) = delete;
149
154 {
155 count_ = 0;
156 bos_desc_size_ = 0;
157 }
158
166 {
167 ASSERT(bos_buffer_.addr_);
168 ASSERT(cap != nullptr);
169 ASSERT(count_ < cap_capacity_);
170
171 caps_[count_++] = cap;
172 return true;
173 }
174
182 {
183 ASSERT(bos_buffer_.addr_);
184
185 bool has_usb2_ext = false;
186 for (size_t i = 0; i < count_; ++i)
187 {
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 {
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 {
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 }
249
258 ErrorCode ProcessVendorRequest(bool in_isr, const SetupPacket* setup,
259 BosVendorResult& result)
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 }
289
290 private:
291 size_t cap_capacity_ = 0;
292 BosCapability** caps_ = nullptr;
293 size_t count_ = 0;
294
295 size_t bos_desc_size_ = 0;
296 RawData bos_buffer_ = {nullptr, 0};
297};
298
299} // namespace LibXR::USB
常量原始数据封装类。 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
Definition libxr_mem.cpp:3
原始数据封装类。 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.
Definition bos.hpp:56
virtual ErrorCode OnVendorRequest(bool, const SetupPacket *, BosVendorResult &)
处理该能力相关 Vendor 请求 / Handle vendor request for this capability
Definition bos.hpp:76
virtual ConstRawData GetCapabilityDescriptor() const =0
返回能力块(不含 BOS 头) Return capability block (without BOS header).
BOS 能力提供者接口 / BOS capability provider interface.
Definition bos.hpp:87
virtual size_t GetBosCapabilityCount()
获取 BOS 能力数量 / Get BOS capability count
Definition bos.hpp:95
virtual BosCapability * GetBosCapability(size_t index)
获取指定索引的 BOS 能力 / Get BOS capability at index
Definition bos.hpp:102
BOS 管理器:能力收集、BOS 描述符拼装、Vendor 请求链式分发 BOS manager: capability collection, descriptor building,...
Definition bos.hpp:116
size_t cap_capacity_
能力指针容量 / Capability pointer capacity
Definition bos.hpp:291
size_t count_
已注册能力数量 / Registered capability count
Definition bos.hpp:293
~BosManager()
析构函数 / Destructor
Definition bos.hpp:134
bool AddCapability(BosCapability *cap)
添加能力 / Add capability
Definition bos.hpp:165
ConstRawData GetBosDescriptor()
构建 BOS 描述符(BOS 头 + 能力块) Build BOS descriptor (header + blocks).
Definition bos.hpp:181
void ClearCapabilities()
清空已注册能力 / Clear registered capabilities
Definition bos.hpp:153
BosManager(size_t buffer_size, size_t cap_num)
构造函数 / Constructor
Definition bos.hpp:124
ErrorCode ProcessVendorRequest(bool in_isr, const SetupPacket *setup, BosVendorResult &result)
Vendor 请求分发 / Vendor request dispatch.
Definition bos.hpp:258
BosCapability ** caps_
能力指针表 / Capability pointer table
Definition bos.hpp:292
RawData bos_buffer_
BOS 缓冲区 / BOS buffer.
Definition bos.hpp:296
size_t bos_desc_size_
最近一次构建大小 / Last built size
Definition bos.hpp:295
Vendor 请求处理结果(EP0 控制传输) Vendor request handling result for EP0 control transfers.
Definition bos.hpp:44
bool handled
已处理该请求 / Request consumed by capability
Definition bos.hpp:45
ConstRawData in_data
IN 数据阶段返回数据 / IN data stage payload.
Definition bos.hpp:46
bool write_zlp
状态阶段发送 ZLP / Send ZLP at status stage
Definition bos.hpp:47
bool early_read_zlp
提前准备 OUT 以适配主机短读 / Arm OUT early for short-read tolerance
Definition bos.hpp:48
USB 标准请求 SETUP 包(固定8字节) Standard USB setup packet (8 bytes)
Definition core.hpp:57