构建 BOS 描述符(BOS 头 + 能力块) Build BOS descriptor (header + blocks).
159 {
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
195 ASSERT(total <= 0xFFFF);
196
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 {
211
213 offset += blk.size_;
214 }
215
216 if (NEED_AUTO_USB2_EXT)
217 {
220 offset += sizeof(USB2_EXT_CAP);
221 }
222
223 return {buffer, offset};
224 }
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).