Use receiver events to listen for devices

This commit is contained in:
PixlOne
2019-10-02 18:41:10 -04:00
parent 00df833c54
commit ca04204a8c
5 changed files with 212 additions and 75 deletions
+48 -28
View File
@@ -15,12 +15,13 @@
#include "util.h"
#include "Device.h"
#define MAX_CONNECTION_TRIES 10
#define TIME_BETWEEN_CONNECTION_TRIES 1s
#define NON_WIRELESS_DEV(index) (index) == HIDPP::DefaultDevice ? "default" : "corderd"
void stopAndDeleteConnectedDevice (ConnectedDevice &connected_device)
{
log_printf(INFO, "%s (Device %d on %s) disconnected", connected_device.device->name.c_str(), connected_device.device->index, connected_device.device->path.c_str());
if(!connected_device.device->waiting_for_receiver)
log_printf(INFO, "%s (Device %d on %s) disconnected", connected_device.device->name.c_str(),
connected_device.device->index, connected_device.device->path.c_str());
connected_device.device->stop();
connected_device.associatedThread.join();
delete(connected_device.device);
@@ -37,9 +38,10 @@ DeviceFinder::~DeviceFinder()
this->devices_mutex.unlock();
}
void DeviceFinder::insertNewDevice(const std::string &path, HIDPP::DeviceIndex index)
Device* DeviceFinder::insertNewDevice(const std::string &path, HIDPP::DeviceIndex index)
{
Device *device = new Device(path, index);
auto device = new Device(path, index);
device->init();
this->devices_mutex.lock();
log_printf(INFO, "%s detected: device %d on %s", device->name.c_str(), index, path.c_str());
@@ -51,6 +53,25 @@ void DeviceFinder::insertNewDevice(const std::string &path, HIDPP::DeviceIndex i
})
});
this->devices_mutex.unlock();
return device;
}
Device* DeviceFinder::insertNewReceiverDevice(const std::string &path, HIDPP::DeviceIndex index)
{
auto *device = new Device(path, index);
this->devices_mutex.lock();
auto path_bucket = this->devices.emplace(path, std::map<HIDPP::DeviceIndex, ConnectedDevice>()).first;
path_bucket->second.emplace(index, ConnectedDevice{
device,
std::thread([device]() {
device->wait_for_receiver();
})
});
this->devices_mutex.unlock();
return device;
}
void DeviceFinder::stopAndDeleteAllDevicesIn (const std::string &path)
@@ -65,8 +86,6 @@ void DeviceFinder::stopAndDeleteAllDevicesIn (const std::string &path)
this->devices.erase(path_bucket);
}
this->devices_mutex.unlock();
log_printf(WARN, "Attempted to disconnect not previously connected devices on %s", path.c_str());
}
void DeviceFinder::stopAndDeleteDevice (const std::string &path, HIDPP::DeviceIndex index)
@@ -86,7 +105,6 @@ void DeviceFinder::stopAndDeleteDevice (const std::string &path, HIDPP::DeviceIn
log_printf(WARN, "Attempted to disconnect not previously connected device %d on %s", index, path.c_str());
}
void DeviceFinder::addDevice(const char *path)
{
@@ -96,22 +114,12 @@ void DeviceFinder::addDevice(const char *path)
// Asynchronously scan device
std::thread{[=]()
{
//Check if device is an HID++ device and handle it accordingly
try
{
HIDPP::SimpleDispatcher dispatcher(string_path.c_str());
bool has_receiver_index = false;
for(HIDPP::DeviceIndex index: {
HIDPP::DefaultDevice, HIDPP::CordedDevice,
HIDPP::WirelessDevice1, HIDPP::WirelessDevice2,
HIDPP::WirelessDevice3, HIDPP::WirelessDevice4,
HIDPP::WirelessDevice5, HIDPP::WirelessDevice6})
for(HIDPP::DeviceIndex index: { HIDPP::DefaultDevice, HIDPP::CordedDevice })
{
if(!has_receiver_index && index == HIDPP::WirelessDevice1)
break;
bool device_not_connected = true;
bool device_unknown = false;
int remaining_tries = MAX_CONNECTION_TRIES;
@@ -123,7 +131,17 @@ void DeviceFinder::addDevice(const char *path)
uint major, minor;
std::tie(major, minor) = version;
if(index == HIDPP::DefaultDevice && version == std::make_tuple(1, 0))
has_receiver_index = true;
{
HIDPP10::Device receiver(&dispatcher, index);
HIDPP10::IReceiver irecv(&receiver);
log_printf(INFO, "Found %s on %s", receiver.name().c_str(), string_path.c_str());
for(HIDPP::DeviceIndex recv_index : { HIDPP::WirelessDevice1, HIDPP::WirelessDevice2,
HIDPP::WirelessDevice3, HIDPP::WirelessDevice4,
HIDPP::WirelessDevice5, HIDPP::WirelessDevice6 })
this->insertNewReceiverDevice(string_path, recv_index);
irecv.getPairedDevices();
return;
}
if(major > 1) // HID++ 2.0 devices only
{
this->insertNewDevice(string_path, index);
@@ -136,14 +154,14 @@ void DeviceFinder::addDevice(const char *path)
{
if(remaining_tries == 1)
{
log_printf(WARN, "While querying %s (possibly asleep), wireless device %d: %s", string_path.c_str(), index, e.what());
log_printf(WARN, "While querying %s (possibly asleep), %s device: %s", string_path.c_str(), NON_WIRELESS_DEV(index), e.what());
remaining_tries += MAX_CONNECTION_TRIES; // asleep devices may raise a resource error, so do not count this try
}
}
}
else if(e.errorCode() != HIDPP10::Error::UnknownDevice)
{
if(remaining_tries == 1)
log_printf(ERROR, "While querying %s, wireless device %d: %s", string_path.c_str(), index, e.what());
log_printf(ERROR, "While querying %s, %s device: %s", string_path.c_str(), NON_WIRELESS_DEV(index), e.what());
}
else device_unknown = true;
}
@@ -152,30 +170,32 @@ void DeviceFinder::addDevice(const char *path)
if(e.errorCode() != HIDPP20::Error::UnknownDevice)
{
if(remaining_tries == 1)
log_printf(ERROR, "Error while querying %s, device %d: %s", string_path.c_str(), index, e.what());
log_printf(ERROR, "Error while querying %s, device %d: %s", string_path.c_str(), NON_WIRELESS_DEV(index), e.what());
}
else device_unknown = true;
}
catch(HIDPP::Dispatcher::TimeoutError &e)
{
if(remaining_tries == 1)
log_printf(ERROR, "Device %s (index %d) timed out.", string_path.c_str(), index);
{
log_printf(ERROR, "Time out on %s device: %s (possibly asleep)", NON_WIRELESS_DEV(index), string_path.c_str());
remaining_tries += MAX_CONNECTION_TRIES; // asleep devices may raise a timeout error, so do not count this try
}
}
catch(std::runtime_error &e)
{
if(remaining_tries == 1)
log_printf(ERROR, "Runtime error on device %d on %s: %s", index, string_path.c_str(), e.what());
log_printf(ERROR, "Runtime error on %s device on %s: %s", NON_WIRELESS_DEV(index), string_path.c_str(), e.what());
}
remaining_tries--;
std::this_thread::sleep_for(TIME_BETWEEN_CONNECTION_TRIES);
} while (device_not_connected && !device_unknown && remaining_tries > 0);
}
}
}
catch(HIDPP::Dispatcher::NoHIDPPReportException &e) { }
catch(std::system_error &e) { log_printf(WARN, "Failed to open %s: %s", string_path.c_str(), e.what()); }
}}.detach();
}