Files
0417_QT_code/DRI/Dri_Vendor.cpp

249 lines
7.2 KiB
C++
Raw Permalink Normal View History

2026-04-17 16:25:19 +08:00
#include "DRI/Dri_Vendor.h"
2026-03-26 10:45:29 +08:00
2026-04-17 16:25:19 +08:00
#include <QtCore/QStringList>
2026-03-26 10:45:29 +08:00
namespace
{
2026-04-17 16:25:19 +08:00
void Dri_Vendor_CloseHandle(HANDLE* p_Handle)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
if ((*p_Handle != nullptr) && (*p_Handle != INVALID_HANDLE_VALUE))
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
CloseHandle(*p_Handle);
2026-03-26 10:45:29 +08:00
}
2026-04-17 16:25:19 +08:00
*p_Handle = INVALID_HANDLE_VALUE;
2026-03-26 10:45:29 +08:00
}
2026-04-17 16:25:19 +08:00
bool Dri_Vendor_TryWrite(
2026-03-26 10:45:29 +08:00
HANDLE HandleWrite,
quint16 OutputLength,
const QByteArray& Packet,
2026-04-17 16:25:19 +08:00
const QString& SuccessText,
const QString& ErrorPrefix,
QString* p_TextStatus,
QStringList* p_ErrorList)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
QString TextError;
if (Dri_Hid_WritePacket(HandleWrite, OutputLength, Packet, &TextError))
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
if (p_TextStatus != nullptr)
{
*p_TextStatus = SuccessText;
}
2026-03-26 10:45:29 +08:00
return true;
}
2026-04-17 16:25:19 +08:00
if (!TextError.isEmpty())
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
p_ErrorList->append(ErrorPrefix.arg(TextError));
2026-03-26 10:45:29 +08:00
}
return false;
}
} // namespace
2026-04-17 16:25:19 +08:00
void Dri_Vendor_Close(Dri_Vendor_Struct_Port* p_Port)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
Dri_Hid_CloseReadPort(&p_Port->ReadPort);
Dri_Vendor_CloseHandle(&p_Port->HandleWriteVendor);
Dri_Vendor_CloseHandle(&p_Port->HandleWriteCommand);
Dri_Vendor_CloseHandle(&p_Port->HandleWriteNkro);
p_Port->VendorWriteOutputLength = 0;
p_Port->CommandWriteOutputLength = 0;
p_Port->NkroWriteOutputLength = 0;
p_Port->IsBluetoothTransport = false;
2026-03-26 10:45:29 +08:00
}
2026-04-17 16:25:19 +08:00
bool Dri_Vendor_Init(
2026-03-26 10:45:29 +08:00
Dri_Vendor_Struct_Port* p_Port,
const Mid_Struct_DeviceConfig& DeviceConfig,
QString* p_TextStatus)
{
2026-04-17 16:25:19 +08:00
Dri_Vendor_Close(p_Port);
Mid_Struct_DeviceMatch VendorMatch;
VendorMatch.VendorId = DeviceConfig.VendorId;
VendorMatch.ProductId = DeviceConfig.ProductId;
VendorMatch.UsagePage = MID_CONST_USAGE_PAGE_VENDOR;
VendorMatch.Usage = MID_CONST_USAGE_VENDOR;
2026-03-26 10:45:29 +08:00
QString VendorPath;
2026-04-17 16:25:19 +08:00
QString VendorInstanceId;
2026-03-26 10:45:29 +08:00
quint16 InputLength = 0;
2026-04-17 16:25:19 +08:00
quint16 VendorOutputLength = 0;
if (!Mid_FindHidInterface(
VendorMatch,
2026-03-26 10:45:29 +08:00
&VendorPath,
&InputLength,
2026-04-17 16:25:19 +08:00
&VendorOutputLength,
&VendorInstanceId))
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
if (p_TextStatus != nullptr)
{
*p_TextStatus = QStringLiteral("Vendor interface was not found: FF00 / 0002.");
}
2026-03-26 10:45:29 +08:00
return false;
}
2026-04-17 16:25:19 +08:00
p_Port->IsBluetoothTransport = Mid_IsBluetoothHidInstanceId(VendorInstanceId);
const QString VendorPortName = p_Port->IsBluetoothTransport
? QStringLiteral("Bluetooth/HID Vendor")
: QStringLiteral("Vendor");
if (!Dri_Hid_InitReadPort(
&p_Port->ReadPort,
VendorPath,
InputLength,
Mid_Enum_RawPacketSource_UsbVendorHid,
VendorPortName,
p_TextStatus))
2026-03-26 10:45:29 +08:00
{
return false;
}
2026-04-17 16:25:19 +08:00
p_Port->HandleWriteVendor = Dri_Hid_OpenWriteHandle(VendorPath, nullptr);
p_Port->VendorWriteOutputLength = VendorOutputLength;
QString CommandPath;
quint16 CommandOutputLength = 0;
Mid_Struct_DeviceMatch CommandMatch;
CommandMatch.VendorId = DeviceConfig.VendorId;
CommandMatch.ProductId = DeviceConfig.ProductId;
CommandMatch.UsagePage = MID_CONST_USAGE_PAGE_VENDOR_COMMAND;
CommandMatch.Usage = MID_CONST_USAGE_VENDOR_COMMAND;
if (Mid_FindHidInterface(
CommandMatch,
&CommandPath,
nullptr,
&CommandOutputLength))
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
QString TextError;
p_Port->HandleWriteCommand = Dri_Hid_OpenWriteHandle(CommandPath, &TextError);
p_Port->CommandWriteOutputLength = CommandOutputLength;
if ((p_Port->HandleWriteCommand == INVALID_HANDLE_VALUE) && (p_TextStatus != nullptr))
{
*p_TextStatus = QStringLiteral("Vendor command write handle failed: %1").arg(TextError);
}
}
else if (p_TextStatus != nullptr)
{
*p_TextStatus = QStringLiteral("Vendor command interface was not found: FF01 / 0005.");
2026-03-26 10:45:29 +08:00
}
QString NkroPath;
quint16 NkroOutputLength = 0;
2026-04-17 16:25:19 +08:00
Mid_Struct_DeviceMatch NkroMatch;
NkroMatch.VendorId = DeviceConfig.VendorId;
NkroMatch.ProductId = DeviceConfig.ProductId;
NkroMatch.UsagePage = MID_CONST_USAGE_PAGE_NKRO;
NkroMatch.Usage = MID_CONST_USAGE_NKRO;
if (Mid_FindHidInterface(
NkroMatch,
2026-03-26 10:45:29 +08:00
&NkroPath,
nullptr,
&NkroOutputLength))
{
QString TextError;
2026-04-17 16:25:19 +08:00
p_Port->HandleWriteNkro = Dri_Hid_OpenWriteHandle(NkroPath, &TextError);
2026-03-26 10:45:29 +08:00
p_Port->NkroWriteOutputLength = NkroOutputLength;
2026-04-17 16:25:19 +08:00
if ((p_Port->HandleWriteNkro == INVALID_HANDLE_VALUE) && (p_TextStatus != nullptr))
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
*p_TextStatus = QStringLiteral("NKRO write handle failed: %1").arg(TextError);
2026-03-26 10:45:29 +08:00
}
}
2026-04-17 16:25:19 +08:00
else if (p_TextStatus != nullptr)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
*p_TextStatus = QStringLiteral("NKRO write interface was not found.");
2026-03-26 10:45:29 +08:00
}
return true;
}
2026-04-17 16:25:19 +08:00
bool Dri_Vendor_Read(
2026-03-26 10:45:29 +08:00
Dri_Vendor_Struct_Port* p_Port,
Mid_Struct_RawPacket* p_Packet,
QString* p_TextStatus)
{
2026-04-17 16:25:19 +08:00
return Dri_Hid_Read(&p_Port->ReadPort, p_Packet, p_TextStatus);
}
2026-03-26 10:45:29 +08:00
2026-04-17 16:25:19 +08:00
bool Dri_Vendor_Write(
Dri_Vendor_Struct_Port* p_Port,
const QByteArray& ByteArray,
QString* p_TextStatus)
{
const QString RouteLabel = p_Port->IsBluetoothTransport
? QStringLiteral("Bluetooth Vendor")
: QStringLiteral("Vendor");
2026-03-26 10:45:29 +08:00
2026-04-17 16:25:19 +08:00
if (!p_Port->ReadPort.IsOpened)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
if (p_TextStatus != nullptr)
{
*p_TextStatus = QStringLiteral("%1 read path is not open, packet was not sent.").arg(RouteLabel);
}
2026-03-26 10:45:29 +08:00
return false;
}
2026-04-17 16:25:19 +08:00
if (ByteArray.isEmpty())
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
if (p_TextStatus != nullptr)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
*p_TextStatus = QStringLiteral("%1 packet is empty.").arg(RouteLabel);
2026-03-26 10:45:29 +08:00
}
return false;
}
2026-04-17 16:25:19 +08:00
QStringList ErrorList;
const quint8 ReportId = static_cast<quint8>(ByteArray.at(0));
2026-03-26 10:45:29 +08:00
2026-04-17 16:25:19 +08:00
if (ReportId == Mid_Enum_ReportId_VendorCommand)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
if (Dri_Vendor_TryWrite(
p_Port->HandleWriteCommand,
p_Port->CommandWriteOutputLength,
ByteArray,
QStringLiteral("Packet sent to %1 command interface.").arg(RouteLabel),
RouteLabel + QStringLiteral(" command write failed: %1"),
p_TextStatus,
&ErrorList))
{
return true;
}
2026-03-26 10:45:29 +08:00
}
2026-04-17 16:25:19 +08:00
else
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
if (Dri_Vendor_TryWrite(
p_Port->HandleWriteNkro,
p_Port->NkroWriteOutputLength,
ByteArray,
QStringLiteral("Packet sent to NKRO write interface."),
QStringLiteral("NKRO write failed: %1"),
p_TextStatus,
&ErrorList))
2026-03-26 10:45:29 +08:00
{
return true;
}
2026-04-17 16:25:19 +08:00
if (Dri_Vendor_TryWrite(
p_Port->HandleWriteVendor,
p_Port->VendorWriteOutputLength,
ByteArray,
QStringLiteral("Packet sent to %1 write interface.").arg(RouteLabel),
RouteLabel + QStringLiteral(" write failed: %1"),
p_TextStatus,
&ErrorList))
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
return true;
2026-03-26 10:45:29 +08:00
}
}
2026-04-17 16:25:19 +08:00
if (p_TextStatus != nullptr)
2026-03-26 10:45:29 +08:00
{
2026-04-17 16:25:19 +08:00
*p_TextStatus = ErrorList.isEmpty()
? QStringLiteral("No writable output handle is available, packet send was skipped.")
: ErrorList.join(QStringLiteral("\n"));
2026-03-26 10:45:29 +08:00
}
return false;
}
2026-04-17 16:25:19 +08:00