2015-11-08 16:26:55 +01:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
// Local Hyperion includes
|
2016-08-28 07:12:48 +02:00
|
|
|
#include "ProviderHID.h"
|
2015-11-08 16:26:55 +01:00
|
|
|
|
2020-08-08 00:21:19 +02:00
|
|
|
ProviderHID::ProviderHID(const QJsonObject &deviceConfig)
|
|
|
|
: LedDevice(deviceConfig)
|
|
|
|
, _VendorId(0)
|
2020-02-10 15:21:58 +01:00
|
|
|
, _ProductId(0)
|
|
|
|
, _useFeature(false)
|
|
|
|
, _deviceHandle(nullptr)
|
|
|
|
, _delayAfterConnect_ms (0)
|
|
|
|
, _blockedForDelay(false)
|
2015-11-08 16:26:55 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:12:48 +02:00
|
|
|
ProviderHID::~ProviderHID()
|
2015-11-08 16:26:55 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
if (_deviceHandle != nullptr)
|
|
|
|
{
|
|
|
|
hid_close(_deviceHandle);
|
|
|
|
}
|
|
|
|
hid_exit();
|
2015-11-08 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
bool ProviderHID::init(const QJsonObject &deviceConfig)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
bool isInitOK = false;
|
2016-12-02 12:07:24 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Initialise sub-class
|
|
|
|
if ( LedDevice::init(deviceConfig) )
|
|
|
|
{
|
|
|
|
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0);
|
|
|
|
auto VendorIdString = deviceConfig["VID"].toString("0x2341").toStdString();
|
|
|
|
auto ProductIdString = deviceConfig["PID"].toString("0x8036").toStdString();
|
2016-08-23 20:07:12 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Convert HEX values to integer
|
|
|
|
_VendorId = std::stoul(VendorIdString, nullptr, 16);
|
|
|
|
_ProductId = std::stoul(ProductIdString, nullptr, 16);
|
2016-08-23 20:07:12 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Initialize the USB context
|
|
|
|
if ( hid_init() != 0)
|
|
|
|
{
|
|
|
|
this->setInError("Error initializing the HIDAPI context");
|
|
|
|
isInitOK = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug(_log,"HIDAPI initialized");
|
|
|
|
isInitOK = true;
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
return isInitOK;
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-28 07:12:48 +02:00
|
|
|
int ProviderHID::open()
|
2015-11-08 16:26:55 +01:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
int retval = -1;
|
2020-07-12 20:27:56 +02:00
|
|
|
_isDeviceReady = false;
|
2015-11-08 16:26:55 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Open the device
|
|
|
|
Info(_log, "Opening device: VID %04hx PID %04hx\n", _VendorId, _ProductId);
|
|
|
|
_deviceHandle = hid_open(_VendorId, _ProductId, nullptr);
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
if (_deviceHandle == nullptr)
|
|
|
|
{
|
|
|
|
// Failed to open the device
|
|
|
|
this->setInError( "Failed to open HID device. Maybe your PID/VID setting is wrong? Make sure to add a udev rule/use sudo." );
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// http://www.signal11.us/oss/hidapi/
|
|
|
|
/*
|
2020-02-10 15:21:58 +01:00
|
|
|
std::cout << "Showing a list of all available HID devices:" << std::endl;
|
|
|
|
auto devs = hid_enumerate(0x00, 0x00);
|
|
|
|
auto cur_dev = devs;
|
|
|
|
while (cur_dev) {
|
|
|
|
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
|
|
|
|
cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
|
|
|
|
printf("\n");
|
|
|
|
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
|
|
|
|
printf(" Product: %ls\n", cur_dev->product_string);
|
|
|
|
printf("\n");
|
|
|
|
cur_dev = cur_dev->next;
|
|
|
|
}
|
|
|
|
hid_free_enumeration(devs);
|
|
|
|
*/
|
2015-11-08 16:26:55 +01:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Info(_log,"Opened HID device successful");
|
|
|
|
// Everything is OK -> enable device
|
|
|
|
_isDeviceReady = true;
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait after device got opened if enabled
|
|
|
|
if (_delayAfterConnect_ms > 0)
|
|
|
|
{
|
|
|
|
_blockedForDelay = true;
|
|
|
|
QTimer::singleShot(_delayAfterConnect_ms, this, &ProviderHID::unblockAfterDelay );
|
|
|
|
Debug(_log, "Device blocked for %d ms", _delayAfterConnect_ms);
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
int ProviderHID::close()
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
int retval = 0;
|
|
|
|
_isDeviceReady = false;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// LedDevice specific closing activities
|
2020-02-10 15:21:58 +01:00
|
|
|
if (_deviceHandle != nullptr)
|
2015-11-08 16:26:55 +01:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
hid_close(_deviceHandle);
|
|
|
|
_deviceHandle = nullptr;
|
2015-11-08 16:26:55 +01:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return retval;
|
2015-11-08 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 13:09:15 +02:00
|
|
|
int ProviderHID::writeBytes(unsigned size, const uint8_t * data)
|
2015-11-08 16:26:55 +01:00
|
|
|
{
|
|
|
|
if (_blockedForDelay) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_deviceHandle == nullptr)
|
|
|
|
{
|
|
|
|
// try to reopen
|
|
|
|
auto status = open();
|
|
|
|
if(status < 0){
|
|
|
|
// Try again in 3 seconds
|
2016-06-25 22:08:17 +02:00
|
|
|
int delay_ms = 3000;
|
2015-11-08 16:26:55 +01:00
|
|
|
_blockedForDelay = true;
|
2020-07-12 20:27:56 +02:00
|
|
|
QTimer::singleShot(delay_ms, this, &ProviderHID::unblockAfterDelay );
|
2016-06-25 22:08:17 +02:00
|
|
|
Debug(_log,"Device blocked for %d ms", delay_ms);
|
2015-11-08 16:26:55 +01:00
|
|
|
}
|
|
|
|
// Return here, to not write led data if the device should be blocked after connect
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend report ID to the buffer
|
|
|
|
uint8_t ledData[size + 1];
|
|
|
|
ledData[0] = 0; // Report ID
|
|
|
|
memcpy(ledData + 1, data, size_t(size));
|
|
|
|
|
|
|
|
// Send data via feature or out report
|
|
|
|
int ret;
|
|
|
|
if(_useFeature){
|
|
|
|
ret = hid_send_feature_report(_deviceHandle, ledData, size + 1);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
ret = hid_write(_deviceHandle, ledData, size + 1);
|
|
|
|
}
|
2020-08-08 23:12:43 +02:00
|
|
|
|
2015-11-08 16:26:55 +01:00
|
|
|
// Handle first error
|
2020-08-08 23:12:43 +02:00
|
|
|
if(ret < 0)
|
|
|
|
{
|
2016-06-25 22:08:17 +02:00
|
|
|
Error(_log,"Failed to write to HID device.");
|
2015-11-08 16:26:55 +01:00
|
|
|
|
|
|
|
// Try again
|
2020-08-08 23:12:43 +02:00
|
|
|
if(_useFeature)
|
|
|
|
{
|
2015-11-08 16:26:55 +01:00
|
|
|
ret = hid_send_feature_report(_deviceHandle, ledData, size + 1);
|
|
|
|
}
|
2020-08-08 23:12:43 +02:00
|
|
|
else
|
|
|
|
{
|
2015-11-08 16:26:55 +01:00
|
|
|
ret = hid_write(_deviceHandle, ledData, size + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writing failed again, device might have disconnected
|
|
|
|
if(ret < 0){
|
2016-06-25 22:08:17 +02:00
|
|
|
Error(_log,"Failed to write to HID device.");
|
2020-08-08 23:12:43 +02:00
|
|
|
|
2015-11-08 16:26:55 +01:00
|
|
|
hid_close(_deviceHandle);
|
|
|
|
_deviceHandle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:12:48 +02:00
|
|
|
void ProviderHID::unblockAfterDelay()
|
2015-11-08 16:26:55 +01:00
|
|
|
{
|
2016-06-25 22:08:17 +02:00
|
|
|
Debug(_log,"Device unblocked");
|
2015-11-08 16:26:55 +01:00
|
|
|
_blockedForDelay = false;
|
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
QJsonObject ProviderHID::discover(const QJsonObject& /*params*/)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
QJsonObject devicesDiscovered;
|
|
|
|
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
|
|
|
|
|
|
|
QJsonArray deviceList;
|
|
|
|
|
|
|
|
// Discover HID Devices
|
|
|
|
auto devs = hid_enumerate(0x00, 0x00);
|
|
|
|
|
|
|
|
if ( devs != nullptr )
|
|
|
|
{
|
|
|
|
auto cur_dev = devs;
|
|
|
|
while (cur_dev)
|
|
|
|
{
|
|
|
|
QJsonObject deviceInfo;
|
|
|
|
deviceInfo.insert("manufacturer",QString::fromWCharArray(cur_dev->manufacturer_string));
|
|
|
|
deviceInfo.insert("path",cur_dev->path);
|
|
|
|
deviceInfo.insert("productIdentifier", QString("0x%1").arg(static_cast<ushort>(cur_dev->product_id),0,16));
|
|
|
|
deviceInfo.insert("release_number",QString("0x%1").arg(static_cast<ushort>(cur_dev->release_number),0,16));
|
|
|
|
deviceInfo.insert("serialNumber",QString::fromWCharArray(cur_dev->serial_number));
|
|
|
|
deviceInfo.insert("usage_page", QString("0x%1").arg(static_cast<ushort>(cur_dev->usage_page),0,16));
|
|
|
|
deviceInfo.insert("vendorIdentifier", QString("0x%1").arg(static_cast<ushort>(cur_dev->vendor_id),0,16));
|
|
|
|
deviceInfo.insert("interface_number",cur_dev->interface_number);
|
|
|
|
deviceList.append(deviceInfo);
|
|
|
|
|
|
|
|
cur_dev = cur_dev->next;
|
|
|
|
}
|
|
|
|
hid_free_enumeration(devs);
|
|
|
|
}
|
|
|
|
|
|
|
|
devicesDiscovered.insert("devices", deviceList);
|
|
|
|
return devicesDiscovered;
|
|
|
|
}
|