7#include "libxr_mem.hpp"
8#include "libxr_type.hpp"
9#include "usb/core/bos.hpp"
10#include "usb/core/core.hpp"
12namespace LibXR::USB::WebUsb
17static constexpr uint16_t WEBUSB_REQUEST_GET_URL = 0x0002u;
18static constexpr uint8_t WEBUSB_VENDOR_CODE_DEFAULT = 0x01u;
19static constexpr uint8_t WEBUSB_URL_DESCRIPTOR_TYPE = 0x03u;
20static constexpr uint8_t WEBUSB_URL_SCHEME_HTTP = 0x00u;
21static constexpr uint8_t WEBUSB_URL_SCHEME_HTTPS = 0x01u;
22static constexpr uint8_t WEBUSB_LANDING_PAGE_INDEX = 0x01u;
25static constexpr uint8_t WEBUSB_PLATFORM_CAPABILITY_UUID[16] = {
26 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47,
27 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65,
37 uint8_t bLength = 0x18u;
38 uint8_t bDescriptorType = 0x10u;
39 uint8_t bDevCapabilityType = 0x05u;
40 uint8_t bReserved = 0x00u;
41 uint8_t PlatformCapabilityUUID[16] = {};
42 uint16_t bcdVersion = 0x0100u;
43 uint8_t bVendorCode = WEBUSB_VENDOR_CODE_DEFAULT;
44 uint8_t iLandingPage = 0u;
49 "WebUSB platform capability size mismatch.");
67 uint8_t vendor_code = WEBUSB_VENDOR_CODE_DEFAULT)
68 : vendor_code_(vendor_code)
72 WEBUSB_PLATFORM_CAPABILITY_UUID,
73 sizeof(WEBUSB_PLATFORM_CAPABILITY_UUID));
74 platform_cap_.bVendorCode = vendor_code_;
92 vendor_code_ = vendor_code;
93 platform_cap_.bVendorCode = vendor_code_;
108 if (landing_page_url ==
nullptr || landing_page_url[0] ==
'\0')
111 platform_cap_.iLandingPage = 0u;
115 uint8_t scheme = WEBUSB_URL_SCHEME_HTTPS;
116 const char* url_body =
nullptr;
121 platform_cap_.iLandingPage = 0u;
125 const size_t URL_BODY_LEN = std::strlen(url_body);
126 if (URL_BODY_LEN == 0u || URL_BODY_LEN > 252u)
133 platform_cap_.iLandingPage = 0u;
144 platform_cap_.iLandingPage = WEBUSB_LANDING_PAGE_INDEX;
157 return {
reinterpret_cast<const uint8_t*
>(&platform_cap_),
sizeof(platform_cap_)};
171 if (setup ==
nullptr)
178 if ((BM & 0x60u) != 0x40u)
182 if ((BM & 0x1Fu) != 0x00u)
186 if ((BM & 0x80u) == 0u)
190 if (setup->
bRequest != vendor_code_ || setup->
wIndex != WEBUSB_REQUEST_GET_URL)
194 if ((setup->
wValue & 0xFFu) != WEBUSB_LANDING_PAGE_INDEX || setup->
wLength == 0u)
214 static constexpr const char kHttpsPrefix[] =
"https://";
215 static constexpr const char kHttpPrefix[] =
"http://";
217 if (std::strncmp(input, kHttpsPrefix,
sizeof(kHttpsPrefix) - 1u) == 0)
219 scheme = WEBUSB_URL_SCHEME_HTTPS;
220 body = input +
sizeof(kHttpsPrefix) - 1u;
223 if (std::strncmp(input, kHttpPrefix,
sizeof(kHttpPrefix) - 1u) == 0)
225 scheme = WEBUSB_URL_SCHEME_HTTP;
226 body = input +
sizeof(kHttpPrefix) - 1u;
233 uint8_t vendor_code_ = WEBUSB_VENDOR_CODE_DEFAULT;
234 bool enabled_ =
false;
常量原始数据封装类。 A class for encapsulating constant raw data.
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
原始数据封装类。 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.
WebUSB BOS 能力包装器 / WebUSB BOS capability wrapper.
ErrorCode OnVendorRequest(bool, const SetupPacket *setup, LibXR::USB::BosVendorResult &result) override
处理 WebUSB GET_URL vendor request Handle the WebUSB GET_URL vendor request.
RawData url_descriptor_
已编码 URL 描述符 / Encoded URL descriptor
bool Enabled() const
当前是否启用 WebUSB / Whether WebUSB is currently enabled
static bool ParseLandingPageUrl(const char *input, uint8_t &scheme, const char *&body)
解析 URL scheme,并返回去掉前缀后的正文 Parse the URL scheme and return the prefix-stripped body.
void SetVendorCode(uint8_t vendor_code)
更新 GET_URL 使用的 vendor code Update the vendor code used by GET_URL.
WebUsbBosCapability(const char *landing_page_url=nullptr, uint8_t vendor_code=WEBUSB_VENDOR_CODE_DEFAULT)
构造能力对象,并可选缓存 landing page URL Construct the capability and optionally cache the landing-page URL.
bool SetLandingPageUrl(const char *landing_page_url)
解析并缓存 landing page URL Parse and cache the landing-page URL.
ConstRawData GetCapabilityDescriptor() const override
获取 BOS capability 描述符 / Get the BOS capability descriptor
uint8_t url_descriptor_storage_[255]
URL 描述符静态缓冲 / Static URL descriptor buffer.
@ NOT_SUPPORT
不支持 | Not supported
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
Vendor 请求处理结果(EP0 控制传输) Vendor request handling result for EP0 control transfers.
bool handled
已处理该请求 / Request consumed by capability
ConstRawData in_data
IN 数据阶段返回数据 / IN data stage payload.
bool write_zlp
状态阶段发送 ZLP / Send ZLP at status stage
bool early_read_zlp
提前准备 OUT 以适配主机短读 / Arm OUT early for short-read tolerance
USB 标准请求 SETUP 包(固定8字节) Standard USB setup packet (8 bytes)
uint16_t wIndex
对象索引,如接口号或端点号 / Index (e.g., interface or endpoint)
uint16_t wLength
数据阶段长度 / Number of bytes in data stage
uint16_t wValue
参数字段,含请求相关值 / Value field (e.g., descriptor type/index)
uint8_t bRequest
请求码 / Request code (e.g., GET_DESCRIPTOR)