Use events instead of continous pinging

This commit is contained in:
PixlOne
2019-07-28 02:08:18 -04:00
parent 684f502b2e
commit b95cc60fd1
13 changed files with 366 additions and 291 deletions
+71 -68
View File
@@ -2,10 +2,11 @@
#include <hidpp/SimpleDispatcher.h>
#include <hidpp/Device.h>
#include <hidpp10/Error.h>
#include <hidpp10/IReceiver.h>
#include <hidpp20/Error.h>
#include <cstring>
#include <unistd.h>
#include <future>
#include <thread>
#include <fstream>
#include <sstream>
@@ -13,16 +14,10 @@
#include "util.h"
#include "Device.h"
void find_device()
{
auto df = new DeviceFinder();
df->run();
}
constexpr uint16_t DeviceFinder::UnifyingReceivers[];
void DeviceFinder::addDevice(const char *path)
{
const int max_tries = 5;
const int try_delay = 50000;
std::string string_path(path);
// Asynchronously scan device
std::thread{[=]()
@@ -50,6 +45,10 @@ void DeviceFinder::addDevice(const char *path)
if(hidraw_info.find("HID_NAME") != hidraw_info.end())
if (hidraw_info.find("HID_NAME")->second.find("Logitech") == std::string::npos) return;
std::vector<Device*> _devs;
const int max_tries = 5;
const int try_delay = 50000;
//Check if device is an HID++ device and handle it accordingly
try
{
@@ -61,80 +60,84 @@ void DeviceFinder::addDevice(const char *path)
HIDPP::WirelessDevice3, HIDPP::WirelessDevice4,
HIDPP::WirelessDevice5, HIDPP::WirelessDevice6})
{
/// TODO: CONTINUOUSLY SCAN ALL DEVICES ON RECEIVER
//Skip wireless devices if default device (receiver) has failed
if(!has_receiver_index && index == HIDPP::WirelessDevice1)
break;
for(int i = 0; i < max_tries; i++)
{
try
{
HIDPP::Device d(&dispatcher, index);
auto version = d.protocolVersion();
if(index == HIDPP::DefaultDevice && version == std::make_tuple(1, 0))
has_receiver_index = true;
uint major, minor;
std::tie(major, minor) = d.protocolVersion();
if(major > 1) // HID++ 2.0 devices only
{
auto dev = new Device(string_path, index);
handlers.insert({
dev, std::async(std::launch::async, &Device::start, *dev)
});
log_printf(INFO, "%s detected: device %d on %s", d.name().c_str(), index, string_path.c_str());
}
break;
}
catch(HIDPP10::Error &e)
{
if(e.errorCode() != HIDPP10::Error::UnknownDevice)
{
if(i == max_tries - 1)
log_printf(WARN, "Error while querying %s, wireless device %d: %s", string_path.c_str(), index, e.what());
else usleep(try_delay);
}
}
catch(HIDPP20::Error &e)
{
if(e.errorCode() != HIDPP20::Error::UnknownDevice)
{
if(i == max_tries - 1)
log_printf(WARN, "Error while querying %s, device %d: %s", string_path.c_str(), index, e.what());
else usleep(try_delay);
}
}
catch(HIDPP::Dispatcher::TimeoutError &e)
{
if(i == max_tries - 1)
log_printf(WARN, "Device %s (index %d) timed out.", string_path.c_str(), index);
else usleep(try_delay);
}
catch(std::runtime_error &e)
{
if(i == max_tries - 1)
log_printf(WARN, "Runtime error on device %d on %s: %s", index, string_path.c_str(), e.what());
else usleep(try_delay);
}
}
if(!has_receiver_index && index == HIDPP::WirelessDevice1)
break;
for(int i = 0; i < max_tries; i++)
{
try
{
HIDPP::Device d(&dispatcher, index);
auto version = d.protocolVersion();
uint major, minor;
std::tie(major, minor) = version;
if(index == HIDPP::DefaultDevice && version == std::make_tuple(1, 0))
has_receiver_index = true;
if(major > 1) // HID++ 2.0 devices only
{
auto dev = new Device(string_path, index);
_devs.push_back(dev);
log_printf(INFO, "%s detected: device %d on %s", d.name().c_str(), index, string_path.c_str());
}
break;
}
catch(HIDPP10::Error &e)
{
if(e.errorCode() != HIDPP10::Error::UnknownDevice)
{
if(i == max_tries - 1)
log_printf(ERROR, "Error while querying %s, wireless device %d: %s", string_path.c_str(), index, e.what());
else usleep(try_delay);
}
else break;
}
catch(HIDPP20::Error &e)
{
if(e.errorCode() != HIDPP20::Error::UnknownDevice)
{
if(i == max_tries - 1)
log_printf(ERROR, "Error while querying %s, device %d: %s", string_path.c_str(), index, e.what());
else usleep(try_delay);
}
else break;
}
catch(HIDPP::Dispatcher::TimeoutError &e)
{
if(i == max_tries - 1)
log_printf(ERROR, "Device %s (index %d) timed out.", string_path.c_str(), index);
else usleep(try_delay);
}
catch(std::runtime_error &e)
{
if(i == max_tries - 1)
log_printf(ERROR, "Runtime error on device %d on %s: %s", index, string_path.c_str(), e.what());
else usleep(try_delay);
}
}
}
}
catch(HIDPP::Dispatcher::NoHIDPPReportException &e) {}
catch(std::system_error &e) { log_printf(WARN, "Failed to open %s: %s", string_path.c_str(), e.what()); }
for(auto dev : _devs)
{
devices.insert({dev, std::thread{[dev]() { dev->start(); }}});
devices[dev].detach();
}
}}.detach();
}
void DeviceFinder::removeDevice(const char* path)
{
// Iterate through Devices, stop all in path
auto it = handlers.begin();
while (it != handlers.end())
auto it = devices.begin();
while (it != devices.end())
{
if(it->first->path == path)
{
log_printf(INFO, "%s on %s disconnected.", it->first->name.c_str(), path);
it->first->stop();
it->second.wait();
//handlers.erase(it);
it->second.join();
devices.erase(it);
it++;
}
else