3#include "dfu/dfu_def.hpp"
14 using JumpCallback = void (*)(
void*);
15 static constexpr const char* DEFAULT_INTERFACE_STRING =
"XRUSB DFU RT";
16 static constexpr uint8_t kAttrWillDetach = 0x08u;
23 JumpCallback jump_to_bootloader,
void* jump_ctx =
nullptr,
24 uint16_t detach_timeout_ms = 50u,
25 const char* interface_string = DEFAULT_INTERFACE_STRING,
26 const char* webusb_landing_page_url =
nullptr,
27 uint8_t webusb_vendor_code = LibXR::USB::WebUsb::WEBUSB_VENDOR_CODE_DEFAULT)
68 uint8_t bDescriptorType = 0x21;
69 uint8_t bmAttributes = 0u;
70 uint16_t wDetachTimeOut = 0;
71 uint16_t wTransferSize = 0;
72 uint16_t bcdDFUVersion = 0x0110u;
80 uint8_t bStatus =
static_cast<uint8_t
>(DFUStatusCode::OK);
81 uint8_t bwPollTimeout[3] = {0, 0, 0};
82 uint8_t bState =
static_cast<uint8_t
>(DFUState::APP_IDLE);
85 void SetPollTimeout(uint32_t timeout_ms)
87 bwPollTimeout[0] =
static_cast<uint8_t
>(timeout_ms & 0xFFu);
88 bwPollTimeout[1] =
static_cast<uint8_t
>((timeout_ms >> 8) & 0xFFu);
89 bwPollTimeout[2] =
static_cast<uint8_t
>((timeout_ms >> 16) & 0xFFu);
100 9,
static_cast<uint8_t
>(DescriptorType::INTERFACE), 0, 0, 0, 0xFEu, 0x01u, 0x01u,
111 interface_num_ = start_itf_num;
112 current_alt_setting_ = 0u;
115 state_ = DFUState::APP_IDLE;
132 state_ = DFUState::APP_IDLE;
142 if (itf != interface_num_)
150 current_alt_setting_ = alt;
156 if (itf != interface_num_)
160 alt = current_alt_setting_;
167 const uint8_t desc_type =
static_cast<uint8_t
>((wValue >> 8) & 0xFFu);
168 if (desc_type !=
desc_block_.func_desc.bDescriptorType)
173 out_data = {
reinterpret_cast<const uint8_t*
>(&
desc_block_.func_desc),
185 if ((wIndex & 0xFFu) != interface_num_)
190 switch (
static_cast<DFURequest
>(bRequest))
192 case DFURequest::DETACH:
201 if (wLength != 0u ||
state_ != DFUState::APP_IDLE)
209 state_ = DFUState::APP_DETACH;
211 result.SendStatusInZLP() =
true;
214 case DFURequest::GETSTATUS:
222 const uint32_t now_ms =
224 const uint32_t remain_ms =
238 case DFURequest::GETSTATE:
常量原始数据封装类。 A class for encapsulating constant raw data.
原始数据封装类。 A class for encapsulating raw data.
static MillisecondTimestamp GetMilliseconds()
获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
void SetData(RawData data)
设置内部数据缓存 / Set internal data cache
uint8_t GetInterfaceStringIndex(size_t local_interface_index) const
返回已分配的接口字符串索引 Return the assigned USB string index for a local interface.
DFU 单接口类公共基类 / Common base for single-interface DFU classes.
Runtime DFU 类:只负责 DETACH 后跳转 bootloader Runtime DFU class: only handles DETACH and later jumps to boo...
ErrorCode SetAltSetting(uint8_t itf, uint8_t alt) override
可选:设置接口备用设置 / Optional: set interface alternate setting
void BindEndpoints(EndpointPool &, uint8_t start_itf_num, bool) override
绑定端点资源 / Bind endpoint resources
uint16_t default_detach_timeout_ms_
默认 detach 超时 / Default detach timeout
DfuRuntimeClass(JumpCallback jump_to_bootloader, void *jump_ctx=nullptr, uint16_t detach_timeout_ms=50u, const char *interface_string=DEFAULT_INTERFACE_STRING, const char *webusb_landing_page_url=nullptr, uint8_t webusb_vendor_code=LibXR::USB::WebUsb::WEBUSB_VENDOR_CODE_DEFAULT)
构造 Runtime DFU 类 Construct the runtime DFU class.
uint32_t detach_deadline_ms_
detach 截止时刻 / Detach deadline tick
size_t GetMaxConfigSize() override
最大配置描述符占用 / Maximum bytes required in configuration descriptor
uint8_t state_response_
GETSTATE 缓冲字节 / GETSTATE byte buffer.
bool detach_pending_
是否等待 detach 超时 / Waiting for detach timeout
void UnbindEndpoints(EndpointPool &, bool) override
解绑端点资源 / Unbind endpoint resources
size_t GetInterfaceCount() override
接口数量 / Number of interfaces contributed
DFUState state_
Runtime DFU 状态 / Runtime DFU state.
JumpCallback jump_to_bootloader_
跳 boot 回调 / Boot jump callback
ErrorCode OnClassRequest(bool, uint8_t bRequest, uint16_t wValue, uint16_t wLength, uint16_t wIndex, ControlTransferResult &result) override
处理 Class-specific 请求(Setup stage)/ Handle class-specific request (Setup stage)
void * jump_ctx_
跳转上下文 / Jump callback context
ErrorCode OnGetDescriptor(bool, uint8_t, uint16_t wValue, uint16_t, ConstRawData &out_data) override
处理标准请求 GET_DESCRIPTOR(类特定描述符) Handle standard GET_DESCRIPTOR request (class-specific descriptors).
uint16_t detach_timeout_ms_
当前 detach 超时 / Active detach timeout
ErrorCode GetAltSetting(uint8_t itf, uint8_t &alt) override
可选:获取接口备用设置 / Optional: get interface alternate setting
DescriptorBlock desc_block_
描述符缓存 / Descriptor cache
bool HasIAD() override
是否包含 IAD / Whether an IAD is used
StatusResponse status_response_
GETSTATUS 缓冲区 / GETSTATUS buffer.
USB端点池类 / USB endpoint pool class.
@ INIT_ERR
初始化错误 | Initialization error
@ NOT_FOUND
未找到 | Not found
@ NOT_SUPPORT
不支持 | Not supported
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
接口描述符(9 字节)/ Interface descriptor (9 bytes)
uint8_t iInterface
接口字符串索引 / Interface string index
uint8_t bInterfaceNumber
接口号 / Interface number
控制请求(Class/Vendor)处理结果 / Control request (Class/Vendor) handling result
Runtime DFU 的接口描述符块 Runtime DFU descriptor block.
DFU Functional Descriptor(Runtime 变体) DFU Functional Descriptor for the runtime variant.
GETSTATUS 返回包 / GETSTATUS response payload.