2015-11-08 16:26:55 +01:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
// Local Hyperion includes
|
|
|
|
#include "LedHIDDevice.h"
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
LedHIDDevice::LedHIDDevice(const unsigned short VendorId, const unsigned short ProductId, int delayAfterConnect_ms, const bool useFeature)
|
|
|
|
: _VendorId(VendorId)
|
|
|
|
, _ProductId(ProductId)
|
|
|
|
, _useFeature(useFeature)
|
|
|
|
, _deviceHandle(nullptr)
|
|
|
|
, _delayAfterConnect_ms(delayAfterConnect_ms)
|
|
|
|
, _blockedForDelay(false)
|
2015-11-08 16:26:55 +01:00
|
|
|
{
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
|
|
|
LedHIDDevice::~LedHIDDevice()
|
|
|
|
{
|
|
|
|
if (_deviceHandle != nullptr)
|
|
|
|
{
|
|
|
|
hid_close(_deviceHandle);
|
|
|
|
_deviceHandle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hid_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedHIDDevice::open()
|
|
|
|
{
|
|
|
|
// Initialize the usb context
|
|
|
|
int error = hid_init();
|
|
|
|
if (error != 0)
|
|
|
|
{
|
2016-06-25 22:08:17 +02:00
|
|
|
Error(_log, "Error while initializing the hidapi context");
|
2015-11-08 16:26:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-25 22:08:17 +02:00
|
|
|
Debug(_log,"Hidapi initialized");
|
2015-11-08 16:26:55 +01:00
|
|
|
|
|
|
|
// Open the device
|
2016-06-25 22:08:17 +02:00
|
|
|
Info(_log, "Opening device: VID %04hx PID %04hx\n", _VendorId, _ProductId);
|
2015-11-08 16:26:55 +01:00
|
|
|
_deviceHandle = hid_open(_VendorId, _ProductId, nullptr);
|
|
|
|
|
|
|
|
if (_deviceHandle == nullptr)
|
|
|
|
{
|
|
|
|
// Failed to open the device
|
2016-06-25 22:08:17 +02:00
|
|
|
Error(_log,"Failed to open HID device. Maybe your PID/VID setting is wrong? Make sure to add a udev rule/use sudo.");
|
2015-11-08 16:26:55 +01:00
|
|
|
|
|
|
|
// http://www.signal11.us/oss/hidapi/
|
|
|
|
/*
|
|
|
|
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);
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-25 22:08:17 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Info(_log,"Opened HID device successful");
|
2015-11-08 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait after device got opened if enabled
|
|
|
|
if (_delayAfterConnect_ms > 0)
|
|
|
|
{
|
|
|
|
_blockedForDelay = true;
|
|
|
|
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
|
2016-06-25 22:08:17 +02:00
|
|
|
Debug(_log, "Device blocked for %d ms", _delayAfterConnect_ms);
|
2015-11-08 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LedHIDDevice::writeBytes(const unsigned size, const uint8_t * data)
|
|
|
|
{
|
|
|
|
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;
|
2016-06-25 22:08:17 +02:00
|
|
|
QTimer::singleShot(delay_ms, this, SLOT(unblockAfterDelay()));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle first error
|
|
|
|
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
|
|
|
|
if(_useFeature){
|
|
|
|
ret = hid_send_feature_report(_deviceHandle, ledData, size + 1);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
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.");
|
2015-11-08 16:26:55 +01:00
|
|
|
|
|
|
|
hid_close(_deviceHandle);
|
|
|
|
_deviceHandle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedHIDDevice::unblockAfterDelay()
|
|
|
|
{
|
2016-06-25 22:08:17 +02:00
|
|
|
Debug(_log,"Device unblocked");
|
2015-11-08 16:26:55 +01:00
|
|
|
_blockedForDelay = false;
|
|
|
|
}
|