Modified hid device internal methods
This commit is contained in:
@@ -2,26 +2,25 @@
|
|||||||
#include "HidDevice.h"
|
#include "HidDevice.h"
|
||||||
#include "RageLog.h"
|
#include "RageLog.h"
|
||||||
|
|
||||||
HidDevice::HidDevice(int vid, const std::vector<int> pids, int interfaceNum, bool autoReconnection, bool nonBlockingWrite) :
|
HidDevice::HidDevice(int vid, const std::vector<int> pids, int interfaceNum, bool autoReconnection, bool nonBlockingRead) :
|
||||||
vid{ vid },
|
vid{ vid },
|
||||||
pids{ pids },
|
pids{ pids },
|
||||||
interfaceNum{ interfaceNum },
|
interfaceNum{ interfaceNum },
|
||||||
autoReconnection{ autoReconnection },
|
autoReconnection{ autoReconnection },
|
||||||
nonBlockingWrite{ nonBlockingWrite }
|
nonBlockingRead{ nonBlockingRead }
|
||||||
{
|
{
|
||||||
bool result = TryConnect();
|
bool result = TryConnect();
|
||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
{
|
||||||
LOG->Warn("HidDevice %04x:%s: %d not found", vid, GetPidsString(pids).c_str(), interfaceNum);
|
LOG->Warn("HidDevice with vendor_id %04x and pids %s - %d not found", vid, GetPidsString(pids).c_str(), interfaceNum);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
foundOnce = true;
|
foundOnce = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
HidDevice::HidDevice(int vid, int pid, int interfaceNum, bool autoReconnection, bool nonBlockingWrite) :
|
HidDevice::HidDevice(int vid, int pid, int interfaceNum, bool autoReconnection, bool nonBlockingRead) :
|
||||||
HidDevice(vid, make_pids(pid, 1), interfaceNum, autoReconnection, nonBlockingWrite)
|
HidDevice(vid, make_pids(pid, 1), interfaceNum, autoReconnection, nonBlockingRead)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,29 +34,34 @@ void HidDevice::Close()
|
|||||||
{
|
{
|
||||||
hid_close(handle);
|
hid_close(handle);
|
||||||
handle = nullptr;
|
handle = nullptr;
|
||||||
|
|
||||||
|
foundDeviceInfo.vid = 0;
|
||||||
|
foundDeviceInfo.pid = 0;
|
||||||
|
foundDeviceInfo.interfaceNum = 0;
|
||||||
|
foundDeviceInfo.path = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HidDevice::Open(const char* path)
|
bool HidDevice::Open(const char* path)
|
||||||
{
|
{
|
||||||
handle = hid_open_path(path);
|
handle = hid_open_path(path);
|
||||||
|
|
||||||
if(nonBlockingWrite)
|
if(nonBlockingRead)
|
||||||
hid_set_nonblocking(handle, 1);
|
hid_set_nonblocking(handle, 1);
|
||||||
|
|
||||||
if(handle)
|
if(handle)
|
||||||
LOG->Info("HidDevice %04x:%s: %d opened by path %s", vid, GetPidsString(pids).c_str(), interfaceNum, path);
|
LOG->Info("HidDevice %04x:%04x: %d opened by path %s", foundDeviceInfo.vid, foundDeviceInfo.pid, foundDeviceInfo.interfaceNum, foundDeviceInfo.path);
|
||||||
|
|
||||||
return handle != nullptr;
|
return handle != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HidDevice::TryConnect()
|
bool HidDevice::TryConnect()
|
||||||
{
|
{
|
||||||
char* path = GetPath(vid, pids, interfaceNum);
|
GetDeviceInfo(vid, pids, interfaceNum, &foundDeviceInfo);
|
||||||
|
|
||||||
if (path == nullptr)
|
if (foundDeviceInfo.path == nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return Open(path);
|
return Open(foundDeviceInfo.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HidDevice::CheckConnection()
|
bool HidDevice::CheckConnection()
|
||||||
@@ -103,8 +107,9 @@ const RString HidDevice::GetPidsString(const std::vector<int> pids)
|
|||||||
return pidsString;
|
return pidsString;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* HidDevice::GetPath(int vid, const std::vector<int> pids, int interfaceNumber)
|
void HidDevice::GetDeviceInfo(int vid, const std::vector<int> pids, int interfaceNumber, HidDeviceInfo* device_info)
|
||||||
{
|
{
|
||||||
|
bool found{ false };
|
||||||
struct hid_device_info* devs, * cur_dev;
|
struct hid_device_info* devs, * cur_dev;
|
||||||
size_t size = pids.size();
|
size_t size = pids.size();
|
||||||
|
|
||||||
@@ -123,12 +128,16 @@ char* HidDevice::GetPath(int vid, const std::vector<int> pids, int interfaceNumb
|
|||||||
{
|
{
|
||||||
if (interfaceNumber == -1)
|
if (interfaceNumber == -1)
|
||||||
{
|
{
|
||||||
return cur_dev->path;
|
found = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (cur_dev->interface_number == interfaceNumber)
|
if (cur_dev->interface_number == interfaceNumber)
|
||||||
return cur_dev->path;
|
{
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,36 +146,44 @@ char* HidDevice::GetPath(int vid, const std::vector<int> pids, int interfaceNumb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
if (found && cur_dev != nullptr)
|
||||||
|
{
|
||||||
|
device_info->vid = cur_dev->vendor_id;
|
||||||
|
device_info->pid = cur_dev->product_id;
|
||||||
|
device_info->interfaceNum = cur_dev->interface_number;
|
||||||
|
device_info->path = cur_dev->path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int HidDevice::Read(unsigned char* data, size_t length)
|
int HidDevice::Read(unsigned char* data, size_t length)
|
||||||
{
|
{
|
||||||
if (!CheckConnection())
|
if (!CheckConnection())
|
||||||
return NOT_CONNECTED;
|
return NotConnected;
|
||||||
|
|
||||||
int result = hid_read(handle, data, length);
|
int result = hid_read(handle, data, length);
|
||||||
|
|
||||||
if (result == FAIL)
|
if (result == -1)
|
||||||
{
|
{
|
||||||
LOG->Warn("HidDevice %04x:%s: %d read failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), interfaceNum, GetError());
|
LOG->Warn("HidDevice %04x:%04x: %d read failed. Fail reason %ls", foundDeviceInfo.vid, foundDeviceInfo.pid, foundDeviceInfo.interfaceNum, GetError());
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int HidDevice::Write(const unsigned char* data, size_t length)
|
|
||||||
{
|
|
||||||
if (!CheckConnection())
|
|
||||||
return NOT_CONNECTED;
|
|
||||||
|
|
||||||
int result = hid_write(handle, data, length);
|
|
||||||
|
|
||||||
if (result != length)
|
|
||||||
{
|
|
||||||
LOG->Warn("HidDevice %04x:%s: %d write failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), interfaceNum, GetError());
|
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HidResults HidDevice::Write(const unsigned char* data, size_t length)
|
||||||
|
{
|
||||||
|
if (!CheckConnection())
|
||||||
|
return NotConnected;
|
||||||
|
|
||||||
|
int result = hid_write(handle, data, length);
|
||||||
|
|
||||||
|
if (result != length)
|
||||||
|
{
|
||||||
|
LOG->Warn("HidDevice %04x:%04x: %d write failed. Fail reason %ls", foundDeviceInfo.vid, foundDeviceInfo.pid, foundDeviceInfo.interfaceNum, GetError());
|
||||||
|
Close();
|
||||||
|
return OperationFailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Success;
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,8 +4,11 @@
|
|||||||
#include "hidapi.h"
|
#include "hidapi.h"
|
||||||
#include "vector"
|
#include "vector"
|
||||||
|
|
||||||
#define FAIL -1
|
enum HidResults {
|
||||||
#define NOT_CONNECTED -2
|
OperationFailed = -1,
|
||||||
|
NotConnected = -2,
|
||||||
|
Success = 0,
|
||||||
|
};
|
||||||
|
|
||||||
static std::vector<int> make_pids(int base_pid, int size)
|
static std::vector<int> make_pids(int base_pid, int size)
|
||||||
{
|
{
|
||||||
@@ -17,18 +20,29 @@ static std::vector<int> make_pids(int base_pid, int size)
|
|||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct HidDeviceInfo {
|
||||||
|
char* path{nullptr};
|
||||||
|
int pid;
|
||||||
|
int vid;
|
||||||
|
int interfaceNum;
|
||||||
|
};
|
||||||
|
|
||||||
class HidDevice
|
class HidDevice
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
HidDeviceInfo foundDeviceInfo{};
|
||||||
hid_device* handle{nullptr};
|
hid_device* handle{nullptr};
|
||||||
|
|
||||||
static const RString GetPidsString(const std::vector<int> pids);
|
static const RString GetPidsString(const std::vector<int> pids);
|
||||||
|
|
||||||
|
//Info necessary to search the connected device once a connection attempt is made
|
||||||
int vid;
|
int vid;
|
||||||
const std::vector<int> pids;
|
const std::vector<int> pids;
|
||||||
int interfaceNum = -1;
|
int interfaceNum = -1;
|
||||||
|
|
||||||
|
//Behaviour configuration
|
||||||
bool autoReconnection = true;
|
bool autoReconnection = true;
|
||||||
bool nonBlockingWrite = false;
|
bool nonBlockingRead = false;
|
||||||
|
|
||||||
bool foundOnce = false;
|
bool foundOnce = false;
|
||||||
void Close();
|
void Close();
|
||||||
@@ -37,10 +51,10 @@ private:
|
|||||||
bool CheckConnection();
|
bool CheckConnection();
|
||||||
const wchar_t* GetError();
|
const wchar_t* GetError();
|
||||||
public:
|
public:
|
||||||
static char* GetPath(int vid, const std::vector<int> pids, int interfaceNum = -1);
|
static void GetDeviceInfo(int vid, const std::vector<int> pids, int interfaceNumber, HidDeviceInfo* device_info);
|
||||||
|
|
||||||
HidDevice(int vid, const std::vector<int> pids, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false);
|
HidDevice(int vid, const std::vector<int> pids, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingRead = false);
|
||||||
HidDevice(int vid, int pid, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false);
|
HidDevice(int vid, int pid, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingRead = false);
|
||||||
|
|
||||||
virtual ~HidDevice();
|
virtual ~HidDevice();
|
||||||
|
|
||||||
@@ -48,7 +62,7 @@ public:
|
|||||||
bool FoundOnce();
|
bool FoundOnce();
|
||||||
|
|
||||||
int Read(unsigned char* data, size_t length);
|
int Read(unsigned char* data, size_t length);
|
||||||
int Write(const unsigned char* data, size_t length);
|
HidResults Write(const unsigned char* data, size_t length);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user