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_mem.hpp"
9#include "libxr_type.hpp"
10#include "usb/core/core.hpp"
11
12namespace LibXR::USB
13{
19enum class DevCapabilityType : uint8_t
20{
21 USB20_EXTENSION = 0x02,
22};
23
24static constexpr uint8_t DESCRIPTOR_TYPE_BOS =
25 static_cast<uint8_t>(DescriptorType::BOS);
26
27static constexpr uint8_t DESCRIPTOR_TYPE_DEVICE_CAPABILITY = static_cast<uint8_t>(
28 DescriptorType::DEVICE_CAPABILITY);
30
31static constexpr uint8_t DEV_CAPABILITY_TYPE_USB20EXT = static_cast<uint8_t>(
32 DevCapabilityType::USB20_EXTENSION);
34
35static constexpr size_t BOS_HEADER_SIZE =
36 5;
37static constexpr size_t USB2_EXT_CAP_SIZE =
38 7;
39
45{
46 bool handled = false;
47 ConstRawData in_data{nullptr, 0};
48 bool write_zlp = false;
50 true;
51};
52
57{
58 public:
66
75 virtual ErrorCode OnVendorRequest(bool /*in_isr*/, const SetupPacket* /*setup*/,
76 BosVendorResult& /*result*/)
77 {
79 }
80};
81
86{
87 public:
92 virtual size_t GetBosCapabilityCount() { return 0; }
93
99 virtual BosCapability* GetBosCapability(size_t index)
100 {
101 UNUSED(index);
102 return nullptr;
103 }
104};
105
113{
114 public:
121 BosManager(size_t buffer_size, size_t cap_num)
122 : cap_capacity_(cap_num),
123 caps_(new BosCapability*[cap_num]),
124 bos_buffer_({new uint8_t[buffer_size], buffer_size})
125 {
126 }
127
128 BosManager(const BosManager&) = delete;
129 BosManager& operator=(const BosManager&) = delete;
130
134 void ClearCapabilities() { count_ = 0; }
135
143 {
144 ASSERT(bos_buffer_.addr_);
145 ASSERT(cap != nullptr);
146 ASSERT(count_ < cap_capacity_);
147
148 caps_[count_++] = cap;
149 return true;
150 }
151
159 {
160 ASSERT(bos_buffer_.addr_);
161
162 bool has_usb2_ext = false;
163 for (size_t i = 0; i < count_; ++i)
164 {
166 ASSERT(blk.addr_ != nullptr);
167 ASSERT(blk.size_ >= 3);
168
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)
172 {
173 has_usb2_ext = true;
174 }
175 }
176
177 static constexpr uint8_t USB2_EXT_CAP[USB2_EXT_CAP_SIZE] = {7, 0x10, 0x02, 0x00,
178 0x00, 0x00, 0x00};
179 const bool NEED_AUTO_USB2_EXT = !has_usb2_ext;
180
181 size_t total = BOS_HEADER_SIZE;
182 for (size_t i = 0; i < count_; ++i)
183 {
185 ASSERT(blk.addr_ != nullptr);
186 ASSERT(blk.size_ >= 1);
187 total += blk.size_;
188 }
189 if (NEED_AUTO_USB2_EXT)
190 {
191 total += sizeof(USB2_EXT_CAP);
192 }
193
194 ASSERT(total <= bos_buffer_.size_);
195 ASSERT(total <= 0xFFFF);
196
197 auto buffer = reinterpret_cast<uint8_t*>(bos_buffer_.addr_);
198
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));
204
205 size_t offset = BOS_HEADER_SIZE;
206
207 for (size_t i = 0; i < count_; ++i)
208 {
210 ASSERT(offset + blk.size_ <= bos_buffer_.size_);
211
212 LibXR::Memory::FastCopy(&buffer[offset], blk.addr_, blk.size_);
213 offset += blk.size_;
214 }
215
216 if (NEED_AUTO_USB2_EXT)
217 {
218 ASSERT(offset + sizeof(USB2_EXT_CAP) <= bos_buffer_.size_);
219 LibXR::Memory::FastCopy(&buffer[offset], USB2_EXT_CAP, sizeof(USB2_EXT_CAP));
220 offset += sizeof(USB2_EXT_CAP);
221 }
222
223 return {buffer, offset};
224 }
225
234 ErrorCode ProcessVendorRequest(bool in_isr, const SetupPacket* setup,
235 BosVendorResult& result)
236 {
237 if (setup == nullptr)
238 {
239 return ErrorCode::ARG_ERR;
240 }
241
242 for (size_t i = 0; i < count_; ++i)
243 {
244 BosVendorResult tmp{};
245 auto ec = caps_[i]->OnVendorRequest(in_isr, setup, tmp);
246
247 if (ec == ErrorCode::NOT_SUPPORT)
248 {
249 continue;
250 }
251 if (ec != ErrorCode::OK)
252 {
253 return ec;
254 }
255 if (tmp.handled)
256 {
257 result = tmp;
258 return ErrorCode::OK;
259 }
260 return ErrorCode::FAILED;
261 }
262
264 }
265
266 private:
267 size_t cap_capacity_ = 0;
268 BosCapability** caps_ = nullptr;
269 size_t count_ = 0;
270
271 RawData bos_buffer_ = {nullptr, 0};
272};
273
274} // 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:5
原始数据封装类。 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:57
virtual ErrorCode OnVendorRequest(bool, const SetupPacket *, BosVendorResult &)
处理该能力相关 Vendor 请求 / Handle vendor request for this capability
Definition bos.hpp:75
virtual ConstRawData GetCapabilityDescriptor() const =0
返回能力块(不含 BOS 头) Return capability block (without BOS header).
BOS 能力提供者接口 / BOS capability provider interface.
Definition bos.hpp:86
virtual size_t GetBosCapabilityCount()
获取 BOS 能力数量 / Get BOS capability count
Definition bos.hpp:92
virtual BosCapability * GetBosCapability(size_t index)
获取指定索引的 BOS 能力 / Get BOS capability at index
Definition bos.hpp:99
BOS 管理器:能力收集、BOS 描述符拼装、Vendor 请求链式分发 BOS manager: capability collection, descriptor building,...
Definition bos.hpp:113
size_t cap_capacity_
能力指针容量 / Capability pointer capacity
Definition bos.hpp:267
size_t count_
已注册能力数量 / Registered capability count
Definition bos.hpp:269
bool AddCapability(BosCapability *cap)
添加能力 / Add capability
Definition bos.hpp:142
ConstRawData GetBosDescriptor()
构建 BOS 描述符(BOS 头 + 能力块) Build BOS descriptor (header + blocks).
Definition bos.hpp:158
void ClearCapabilities()
清空已注册能力 / Clear registered capabilities
Definition bos.hpp:134
BosManager(size_t buffer_size, size_t cap_num)
构造函数 / Constructor
Definition bos.hpp:121
ErrorCode ProcessVendorRequest(bool in_isr, const SetupPacket *setup, BosVendorResult &result)
Vendor 请求分发 / Vendor request dispatch.
Definition bos.hpp:234
BosCapability ** caps_
能力指针表 / Capability pointer table
Definition bos.hpp:268
RawData bos_buffer_
BOS 缓冲区 / BOS buffer.
Definition bos.hpp:271
ErrorCode
定义错误码枚举
@ NOT_SUPPORT
不支持 | Not supported
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
Vendor 请求处理结果(EP0 控制传输) Vendor request handling result for EP0 control transfers.
Definition bos.hpp:45
bool handled
已处理该请求 / Request consumed by capability
Definition bos.hpp:46
ConstRawData in_data
IN 数据阶段返回数据 / IN data stage payload.
Definition bos.hpp:47
bool write_zlp
状态阶段发送 ZLP / Send ZLP at status stage
Definition bos.hpp:48
bool early_read_zlp
提前准备 OUT 以适配主机短读 / Arm OUT early for short-read tolerance
Definition bos.hpp:49
USB 标准请求 SETUP 包(固定8字节) Standard USB setup packet (8 bytes)
Definition core.hpp:57