87 lines
2.8 KiB
C
87 lines
2.8 KiB
C
#pragma once
|
||
|
||
#include <QtCore/QByteArray>
|
||
#include <QtCore/QString>
|
||
#include <QtCore/QStringList>
|
||
#include <QtCore/QVector>
|
||
|
||
/*
|
||
* MID 层公共定义:设备配置、匹配条件、HID 报文常量等都集中于此。
|
||
* 高密注释放在每个结构/函数附近,方便教学时直接引用。
|
||
*/
|
||
|
||
/* HID 报文 ID:用于区分 NKRO、Consumer、Vendor */
|
||
enum Mid_Enum_ReportId : quint8
|
||
{
|
||
Mid_Enum_ReportId_None = 0x00,
|
||
Mid_Enum_ReportId_Nkro = 0x01,
|
||
Mid_Enum_ReportId_Consumer = 0x03,
|
||
Mid_Enum_ReportId_Vendor = 0x04
|
||
};
|
||
|
||
/* 默认 VID/PID:教学用固件的 USB 身份 */
|
||
const quint16 MID_CONST_VENDOR_ID_DEFAULT = 0x1209;
|
||
const quint16 MID_CONST_PRODUCT_ID_DEFAULT = 0x0001;
|
||
|
||
/* 上位机配置:允许 UI 快速切换 VID/PID */
|
||
struct Mid_Struct_DeviceConfig
|
||
{
|
||
quint16 VendorId = MID_CONST_VENDOR_ID_DEFAULT;
|
||
quint16 ProductId = MID_CONST_PRODUCT_ID_DEFAULT;
|
||
};
|
||
|
||
/* 匹配条件:Win32 SetupAPI + HidP_Caps 的输入结构 */
|
||
struct Mid_Struct_DeviceMatch
|
||
{
|
||
quint16 VendorId = 0;
|
||
quint16 ProductId = 0;
|
||
quint16 UsagePage = 0;
|
||
quint16 Usage = 0;
|
||
QString Name;
|
||
};
|
||
|
||
/* 统一的“原始包”封装:记录来源端口与字节内容 */
|
||
struct Mid_Struct_RawPacket
|
||
{
|
||
bool IsValid = false;
|
||
QByteArray ByteArray;
|
||
QString PortName;
|
||
};
|
||
|
||
/* Vendor 状态:逻辑层解析后的结果(Modifier + Usage 列表) */
|
||
struct Mid_Struct_VendorState
|
||
{
|
||
bool IsValid = false;
|
||
quint8 Modifier = 0;
|
||
QByteArray UsageBitmap;
|
||
QVector<quint16> UsageList;
|
||
QStringList UsageTextList;
|
||
};
|
||
|
||
const quint16 MID_CONST_USAGE_PAGE_NKRO = 0x0001;
|
||
const quint16 MID_CONST_USAGE_NKRO = 0x0006;
|
||
const quint16 MID_CONST_USAGE_PAGE_CONSUMER = 0x000C;
|
||
const quint16 MID_CONST_USAGE_CONSUMER = 0x0001;
|
||
const quint16 MID_CONST_USAGE_PAGE_VENDOR = 0xFF00;
|
||
const quint16 MID_CONST_USAGE_VENDOR = 0x0002;
|
||
const int MID_CONST_KEYBOARD_USAGE_MAX = 0x00E7;
|
||
const int MID_CONST_USAGE_BITMAP_SIZE = 29;
|
||
const int MID_CONST_PACKET_SIZE_NKRO = 31;
|
||
const int MID_CONST_PACKET_SIZE_VENDOR = 31;
|
||
const int MID_CONST_PACKET_SIZE_CONSUMER = 3;
|
||
|
||
/* 设备匹配函数:针对 NKRO / Consumer / Vendor */
|
||
Mid_Struct_DeviceMatch Mid_Func_GetNkroMatch(const Mid_Struct_DeviceConfig& DeviceConfig);
|
||
Mid_Struct_DeviceMatch Mid_Func_GetConsumerMatch(const Mid_Struct_DeviceConfig& DeviceConfig);
|
||
Mid_Struct_DeviceMatch Mid_Func_GetVendorMatch(const Mid_Struct_DeviceConfig& DeviceConfig);
|
||
/* HID 接口枚举 & 辅助文本转换 */
|
||
bool Mid_Func_FindHidInterface(
|
||
const Mid_Struct_DeviceMatch& Match,
|
||
QString* p_DevicePath,
|
||
quint16* p_InputLength,
|
||
quint16* p_OutputLength);
|
||
QString Mid_Func_GetHexText(const QByteArray& ByteArray);
|
||
QString Mid_Func_GetModifierText(quint8 Modifier);
|
||
QString Mid_Func_GetKeyboardUsageText(quint16 Usage);
|
||
QString Mid_Func_GetConsumerUsageText(quint16 Usage);
|