构建 BOS 描述符(BOS 头 + 能力块) Build BOS descriptor (header + blocks).
182 {
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
218 ASSERT(total <= 0xFFFF);
219
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 {
234
236 offset += blk.size_;
237 }
238
239 if (NEED_AUTO_USB2_EXT)
240 {
243 offset += sizeof(USB2_EXT_CAP);
244 }
245
247 return {buffer, offset};
248 }
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
size_t size_
数据大小(字节)。 The size of the data (in bytes).
virtual ConstRawData GetCapabilityDescriptor() const =0
返回能力块(不含 BOS 头) Return capability block (without BOS header).