Added list of pids instead of single pid. Read returns back -1 when in error

This commit is contained in:
DANDO\Aless
2025-03-22 21:48:23 +01:00
committed by teejusb
parent 0b13eb17f2
commit a6c2a5bc2a
10 changed files with 80 additions and 33 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
REGISTER_LIGHTS_DRIVER_CLASS(HidBlueDot);
LightsDriver_HidBlueDot::LightsDriver_HidBlueDot() :
dev{VID, PID},
dev{ VID, pids },
m_iCabData{ 0 },
m_iPadData{ 0 }
{
@@ -7,6 +7,8 @@
#define VID 0x04BD
#define PID 0xBD
static constexpr int pids[] = { PID };
class LightsDriver_HidBlueDot : public LightsDriver
{
private:
+1 -1
View File
@@ -9,7 +9,7 @@ REGISTER_LIGHTS_DRIVER_CLASS(PacDrive);
static Preference<RString> g_sPacDriveLightOrdering("PacDriveLightOrdering", "openitg");
int iPacDriveLightOrder = 0;
LightsDriver_PacDrive::LightsDriver_PacDrive() : dev{PACDRIVE_VID, PACDRIVE_PID, PACDRIVE_INTERFACE}
LightsDriver_PacDrive::LightsDriver_PacDrive() : dev{PACDRIVE_VID, GetPids(), PACDRIVE_INTERFACE}
{
prev_led_state.raw = 0;
memset(state.raw_state, 0x00, sizeof(state.raw_state));
+10
View File
@@ -28,6 +28,16 @@
#define PACDRIVE_PID 0x1500
#define PACDRIVE_PID_MAX 8
static constexpr int* GetPids()
{
int vec[PACDRIVE_PID]{0};
for (int i = 0; i < PACDRIVE_PID_MAX; i++)
vec[i] = PACDRIVE_PID + i;
return vec;
}
#define PACDRIVE_INTERFACE 0
// the first byte of the buffer is a static report id.
+2 -1
View File
@@ -6,8 +6,9 @@
REGISTER_LIGHTS_DRIVER_CLASS(snek);
LightsDriver_snek::LightsDriver_snek() :
dev{ SNEK_VID , SNEK_PID , SNEK_LIGHTING_INTERFACENUM },
dev{ SNEK_VID , pids, SNEK_LIGHTING_INTERFACENUM },
stateChanged{ false },
outputBuffer{0}
{
+3
View File
@@ -95,6 +95,9 @@ enum SnekLightIndex
#define SNEK_INDEX_DANCE_P2_LEFT SNEK_LIGHTINDEX_CN11_FL3
#define SNEK_INDEX_DANCE_P2_RIGHT SNEK_LIGHTINDEX_CN11_FL4
static constexpr int pids[] = { SNEK_PID };
class LightsDriver_snek : public LightsDriver
{
private:
+1 -1
View File
@@ -7,7 +7,7 @@
REGISTER_LIGHTS_DRIVER_CLASS(stac);
LightsDriver_stac::LightsDriver_stac() :
devs{ {STAC_VID , STAC_PID_P1, STAC_LIGHTING_INTERFACE}, {STAC_VID , STAC_PID_P2, STAC_LIGHTING_INTERFACE } },
devs{ {STAC_VID , pids1, STAC_LIGHTING_INTERFACE}, {STAC_VID , pids2, STAC_LIGHTING_INTERFACE } },
stateChanged{ false },
outputBuffer{ {0} }
{
+4
View File
@@ -47,6 +47,10 @@ enum StacLightIndex
STAC_LIGHTINDEX_MAX
};
static constexpr int pids1[] = { STAC_PID_P1 };
static constexpr int pids2[] = { STAC_PID_P2 };
class LightsDriver_stac : public LightsDriver
{
private:
+48 -23
View File
@@ -2,18 +2,18 @@
#include "HidDevice.h"
#include "RageLog.h"
HidDevice::HidDevice(int vid, int pid, int interfaceNum, bool autoReconnection, bool nonBlockingWrite) :
HidDevice::HidDevice(int vid, const int pids[], int interfaceNum, bool autoReconnection, bool nonBlockingWrite) :
vid{ vid },
pid{ pid },
pids{ pids },
interfaceNum{ interfaceNum },
autoReconnection{autoReconnection},
nonBlockingWrite{nonBlockingWrite}
autoReconnection{ autoReconnection },
nonBlockingWrite{ nonBlockingWrite }
{
bool result = TryConnect();
if (!result) {
LOG->Warn("HID device with VID/PID %04x/%04x not found.", vid, pid);
if (!result)
{
LOG->Warn("HID device with VID/PID %04x/%s not found.", vid, GetPidsString(pids).c_str());
return;
}
else
@@ -40,14 +40,14 @@ bool HidDevice::Open(const char* path)
hid_set_nonblocking(handle, 1);
if(handle)
LOG->Info("HidDevice opened %04x:%04x:%d by path %s", vid, pid, interfaceNum, path);
LOG->Info("HidDevice opened %04x:%04x:%d by path %s", vid, GetPidsString(pids).c_str(), interfaceNum, path);
return handle != nullptr;
}
bool HidDevice::TryConnect()
{
char* path = GetPath(vid, pid, interfaceNum);
char* path = GetPath(vid, pids, interfaceNum);
if (path == nullptr)
return false;
@@ -80,11 +80,30 @@ bool HidDevice::FoundOnce()
return foundOnce;
}
char* HidDevice::GetPath(int vid, int pid, int interfaceNumber)
const RString HidDevice::GetPidsString(const int pids[])
{
RString pidsString;
char pid[5] = { 0 };
size_t size = sizeof(pids) / sizeof(pids[0]);
for (size_t i = 0; i < size; ++i)
{
sprintf(pid, "%04X", pids[i]);
pidsString += pid;
if (i != size - 1)
pidsString += ",";
}
return pidsString;
}
char* HidDevice::GetPath(int vid, const int pids[], int interfaceNumber)
{
struct hid_device_info* devs, * cur_dev;
size_t size = sizeof(pids) / sizeof(pids[0]);
devs = hid_enumerate(vid, pid);
devs = hid_enumerate(vid, 0);
cur_dev = devs;
if (devs && cur_dev)
@@ -92,17 +111,20 @@ char* HidDevice::GetPath(int vid, int pid, int interfaceNumber)
// Look for the desired devices by iterating connected ones
while (cur_dev)
{
if (cur_dev->vendor_id == vid &&
cur_dev->product_id == pid)
for (int i = 0; i < size; i++)
{
if (interfaceNumber == -1)
if (cur_dev->vendor_id == vid &&
cur_dev->product_id == pids[i])
{
return cur_dev->path;
}
else
{
if(cur_dev->interface_number == interfaceNumber)
if (interfaceNumber == -1)
{
return cur_dev->path;
}
else
{
if (cur_dev->interface_number == interfaceNumber)
return cur_dev->path;
}
}
}
@@ -113,16 +135,19 @@ char* HidDevice::GetPath(int vid, int pid, int interfaceNumber)
return nullptr;
}
void HidDevice::Read(unsigned char* data, size_t length)
int HidDevice::Read(unsigned char* data, size_t length)
{
if (!CheckConnection())
return;
return -1;
int result = hid_read(handle, data, length);
if (result == -1)
{
LOG->Warn("HID device with VID/PID %04x/%04x read failed. Fail reason %ls", vid, pid, GetError());
LOG->Warn("HID device with VID/PID %04x/%s read failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), GetError());
}
return result;
}
void HidDevice::Write(const unsigned char* data, size_t length)
@@ -134,7 +159,7 @@ void HidDevice::Write(const unsigned char* data, size_t length)
if (result != length)
{
LOG->Warn("HID device with VID/PID %04x/%04x write failed. Fail reason %ls", vid, pid, GetError());
LOG->Warn("HID device with VID/PID %04x/%s write failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), GetError());
Close();
}
}
+8 -6
View File
@@ -8,11 +8,13 @@ class HidDevice
private:
hid_device* handle{nullptr};
bool autoReconnection = true;
bool nonBlockingWrite;
static const RString GetPidsString(const int pids[]);
int vid;
int pid;
const int* pids;
int interfaceNum = -1;
bool autoReconnection = true;
bool nonBlockingWrite = false;
bool foundOnce = false;
void Close();
@@ -21,16 +23,16 @@ private:
bool CheckConnection();
const wchar_t* GetError();
public:
static char* GetPath(int vid, int pid, int interfaceNum = -1);
static char* GetPath(int vid, const int pids[], int interfaceNum = -1);
HidDevice(int vid, int pid, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false);
HidDevice(int vid, const int pids[], int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false);
virtual ~HidDevice();
bool IsConnected();
bool FoundOnce();
void Read(unsigned char* data, size_t length);
int Read(unsigned char* data, size_t length);
void Write(const unsigned char* data, size_t length);
};