Added error print and other small refactoring. Now it evaluates hid path before trying connection
This commit is contained in:
@@ -2,28 +2,29 @@
|
|||||||
#include "HidDevice.h"
|
#include "HidDevice.h"
|
||||||
#include "RageLog.h"
|
#include "RageLog.h"
|
||||||
|
|
||||||
HidDevice::HidDevice(int vid, int pid, int interfaceNum) : path{ GetPath(vid, pid, interfaceNum) }
|
HidDevice::HidDevice(int vid, int pid, int interfaceNum, bool autoReconnection) :
|
||||||
|
vid{ vid },
|
||||||
|
pid{ pid },
|
||||||
|
interfaceNum{ interfaceNum },
|
||||||
|
autoReconnection{autoReconnection}
|
||||||
{
|
{
|
||||||
bool result = TryConnect();
|
bool result = TryConnect();
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
|
||||||
LOG->Warn("HID device with VID/PID %x/%x not found.", vid, pid);
|
LOG->Warn("HID device with VID/PID %x/%x not found.", vid, pid);
|
||||||
hid_exit();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
path = path;
|
foundOnce = true;
|
||||||
hid_set_nonblocking(handle, 1);
|
hid_set_nonblocking(handle, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HidDevice::~HidDevice()
|
HidDevice::~HidDevice()
|
||||||
{
|
{
|
||||||
if (handle != nullptr)
|
Close();
|
||||||
hid_close(handle);
|
|
||||||
|
|
||||||
hid_exit();
|
hid_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +34,7 @@ void HidDevice::Close()
|
|||||||
handle = nullptr;
|
handle = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HidDevice::Open()
|
bool HidDevice::Open(const char* path)
|
||||||
{
|
{
|
||||||
handle = hid_open_path(path);
|
handle = hid_open_path(path);
|
||||||
|
|
||||||
@@ -42,16 +43,31 @@ bool HidDevice::Open()
|
|||||||
|
|
||||||
bool HidDevice::TryConnect()
|
bool HidDevice::TryConnect()
|
||||||
{
|
{
|
||||||
|
char* path = GetPath(vid, pid, interfaceNum);
|
||||||
|
|
||||||
if (path == nullptr)
|
if (path == nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return Open();
|
return Open(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HidDevice::CheckConnection()
|
||||||
|
{
|
||||||
|
if (IsConnected())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!autoReconnection || !foundOnce)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return TryConnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
const wchar_t* HidDevice::GetError()
|
||||||
|
{
|
||||||
|
return hid_read_error(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HidDevice::IsConnected() {
|
bool HidDevice::IsConnected() {
|
||||||
if (handle == nullptr)
|
|
||||||
return TryConnect();
|
|
||||||
|
|
||||||
return handle != nullptr;
|
return handle != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,19 +106,27 @@ char* HidDevice::GetPath(int vid, int pid, int interfaceNumber)
|
|||||||
|
|
||||||
void HidDevice::Read(unsigned char* data, size_t length)
|
void HidDevice::Read(unsigned char* data, size_t length)
|
||||||
{
|
{
|
||||||
if (!IsConnected())
|
if (!CheckConnection())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
hid_read(handle, data, length);
|
int result = hid_read(handle, data, length);
|
||||||
|
|
||||||
|
if (result == -1)
|
||||||
|
{
|
||||||
|
LOG->Warn("HID device with VID/PID %x/%x read failed. Fail reason %s", vid, pid, GetError());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HidDevice::Write(const unsigned char* data, size_t length)
|
void HidDevice::Write(const unsigned char* data, size_t length)
|
||||||
{
|
{
|
||||||
if (!IsConnected())
|
if (!CheckConnection())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int result = hid_write(handle, data, length);
|
int result = hid_write(handle, data, length);
|
||||||
|
|
||||||
if (result != length)
|
if (result != length)
|
||||||
|
{
|
||||||
|
LOG->Warn("HID device with VID/PID %x/%x write failed. Fail reason %s", vid, pid, GetError());
|
||||||
Close();
|
Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,23 @@ class HidDevice
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
hid_device* handle{nullptr};
|
hid_device* handle{nullptr};
|
||||||
char* path = nullptr;
|
|
||||||
|
bool autoReconnection = true;
|
||||||
|
|
||||||
|
int vid;
|
||||||
|
int pid;
|
||||||
|
int interfaceNum = -1;
|
||||||
|
|
||||||
|
bool foundOnce = false;
|
||||||
void Close();
|
void Close();
|
||||||
bool Open();
|
bool Open(const char* path);
|
||||||
bool TryConnect();
|
bool TryConnect();
|
||||||
|
bool CheckConnection();
|
||||||
|
const wchar_t* GetError();
|
||||||
public:
|
public:
|
||||||
static char* GetPath(int vid, int pid, int interfaceNum = -1);
|
static char* GetPath(int vid, int pid, int interfaceNum = -1);
|
||||||
|
|
||||||
HidDevice(int vid, int pid, int interfaceNum = -1);
|
HidDevice(int vid, int pid, int interfaceNum = -1, bool autoReconnection = true);
|
||||||
|
|
||||||
virtual ~HidDevice();
|
virtual ~HidDevice();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user