libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
device_class.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "libxr_type.hpp"
7#include "usb/core/desc_cfg.hpp"
8
9namespace LibXR::USB
10{
11class DeviceComposition;
12class DeviceCore;
13
21{
22 public:
26 DeviceClass() = default;
27
35 virtual const char* GetInterfaceString(size_t local_interface_index) const
36 {
37 UNUSED(local_interface_index);
38 return nullptr;
39 }
40
45 size_t GetBosCapabilityCount() override { return 0u; }
46
51 BosCapability* GetBosCapability(size_t index) override
52 {
53 UNUSED(index);
54 return nullptr;
55 }
56
57 protected:
65 [[nodiscard]] uint8_t GetInterfaceStringIndex(size_t local_interface_index) const;
66
73 {
74 RawData read_data{nullptr, 0};
78 bool read_zlp = false;
80 bool write_zlp = false;
81
82 RawData& OutData() { return read_data; }
83 const RawData& OutData() const { return read_data; }
84
85 ConstRawData& InData() { return write_data; }
86 const ConstRawData& InData() const { return write_data; }
87
88 bool& ExpectStatusOutZLP() { return read_zlp; }
89 bool ExpectStatusOutZLP() const { return read_zlp; }
90
91 bool& SendStatusInZLP() { return write_zlp; }
92 bool SendStatusInZLP() const { return write_zlp; }
93 };
94
107 virtual ErrorCode OnGetDescriptor(bool in_isr, uint8_t bRequest, uint16_t wValue,
108 uint16_t wLength, ConstRawData& out_data)
109 {
110 UNUSED(in_isr);
111 UNUSED(bRequest);
112 UNUSED(wValue);
113 UNUSED(wLength);
114 UNUSED(out_data);
116 }
117
130 virtual ErrorCode OnClassRequest(bool in_isr, uint8_t bRequest, uint16_t wValue,
131 uint16_t wLength, uint16_t wIndex,
132 ControlTransferResult& result)
133 {
134 UNUSED(in_isr);
135 UNUSED(bRequest);
136 UNUSED(wValue);
137 UNUSED(wLength);
138 UNUSED(wIndex);
139 UNUSED(result);
141 }
142
155 virtual ErrorCode OnClassData(bool in_isr, uint8_t bRequest, LibXR::ConstRawData& data)
156 {
157 UNUSED(in_isr);
158 UNUSED(bRequest);
159 UNUSED(data);
161 }
162
170 virtual void OnClassInDataStatusComplete(bool in_isr, uint8_t bRequest)
171 {
172 UNUSED(in_isr);
173 UNUSED(bRequest);
174 }
175
187 virtual ErrorCode OnVendorRequest(bool in_isr, uint8_t bRequest, uint16_t wValue,
188 uint16_t wLength, uint16_t wIndex,
189 ControlTransferResult& result)
190 {
191 UNUSED(in_isr);
192 UNUSED(bRequest);
193 UNUSED(wValue);
194 UNUSED(wLength);
195 UNUSED(wIndex);
196 UNUSED(result);
198 }
199
200 private:
201 // These helpers are driven by DeviceComposition during initialization-time string
202 // registration and are not part of the public class contract.
203 // 这些辅助函数只在初始化期由 DeviceComposition 调用,不属于对外类接口。
204 void SetInterfaceStringBaseIndex(uint8_t string_index);
205
206 friend class DeviceComposition;
207 friend class DeviceCore;
208
210 0u;
211};
212
213} // namespace LibXR::USB
常量原始数据封装类。 A class for encapsulating constant raw data.
原始数据封装类。 A class for encapsulating raw data.
BOS 能力接口 / BOS capability interface.
Definition bos.hpp:57
USB 配置项基类(功能块)/ USB configuration item base (functional block)
Definition desc_cfg.hpp:36
USB 设备类接口基类 / USB device class interface base.
virtual ErrorCode OnClassRequest(bool in_isr, uint8_t bRequest, uint16_t wValue, uint16_t wLength, uint16_t wIndex, ControlTransferResult &result)
处理 Class-specific 请求(Setup stage)/ Handle class-specific request (Setup stage)
virtual void OnClassInDataStatusComplete(bool in_isr, uint8_t bRequest)
类请求的 IN 数据阶段在 STATUS OUT 完成后回调 Called after the STATUS OUT completes for a Class IN data request.
virtual ErrorCode OnClassData(bool in_isr, uint8_t bRequest, LibXR::ConstRawData &data)
处理 Class request 数据阶段 / Handle class request data stage
virtual ErrorCode OnVendorRequest(bool in_isr, uint8_t bRequest, uint16_t wValue, uint16_t wLength, uint16_t wIndex, ControlTransferResult &result)
处理 Vendor request(Setup stage)/ Handle vendor request (Setup stage)
uint8_t interface_string_base_index_
首个接口字符串索引 / First interface string index
BosCapability * GetBosCapability(size_t index) override
返回指定 BOS capability Return the BOS capability at the given index.
uint8_t GetInterfaceStringIndex(size_t local_interface_index) const
返回已分配的接口字符串索引 Return the assigned USB string index for a local interface.
size_t GetBosCapabilityCount() override
返回本类提供的 BOS capability 数量 Return the number of BOS capabilities exposed by this class.
DeviceClass()=default
默认构造 / Default constructor
virtual ErrorCode OnGetDescriptor(bool in_isr, uint8_t bRequest, uint16_t wValue, uint16_t wLength, ConstRawData &out_data)
处理标准请求 GET_DESCRIPTOR(类特定描述符) Handle standard GET_DESCRIPTOR request (class-specific descriptors).
virtual const char * GetInterfaceString(size_t local_interface_index) const
返回本类暴露的第 N 个接口字符串 Return the string for the Nth local interface exposed by this class.
USB 组合管理器:class 列表、字符串、BOS、配置切换 USB composition manager: class list, strings, BOS,...
USB 设备协议栈核心:EP0 控制传输、描述符、配置、标准/类/厂商请求 USB device core: EP0 control transfer, descriptors,...
Definition dev_core.hpp:22
ErrorCode
定义错误码枚举
@ NOT_SUPPORT
不支持 | Not supported
控制请求(Class/Vendor)处理结果 / Control request (Class/Vendor) handling result
bool write_zlp
发送 STATUS IN(发送 ZLP)/ Send STATUS IN (send ZLP)