Switched device removal and insertion responsibility to DeviceFinder

master
rockerbacon 5 years ago
parent 13bce5cc76
commit ba0bf93b80
  1. 16
      src/logid/Device.cpp
  2. 99
      src/logid/DeviceFinder.cpp
  3. 18
      src/logid/DeviceFinder.h

@ -248,19 +248,7 @@ void ReceiverHandler::handleEvent(const HIDPP::Report &event)
{
case HIDPP10::IReceiver::DeviceUnpaired:
{
// Find device, stop it, and delete it
auto it = finder->devices.begin();
while (it != finder->devices.end())
{
if(it->first->path == dev->path && it->first->index == event.deviceIndex())
{
log_printf(INFO, "%s (Device %d on %s) unpaired.", it->first->name.c_str(), event.deviceIndex(), dev->path.c_str());
it->first->stop();
it->second.join();
finder->devices.erase(it);
}
else it++;
}
finder->stopAndDeleteDevice(dev->path, event.deviceIndex());
break;
}
case HIDPP10::IReceiver::DevicePaired:
@ -384,4 +372,4 @@ std::map<uint16_t, uint8_t> Device::get_features()
_features.insert( {i, ifs.getFeatureID(i) } );
return _features;
}
}

@ -14,6 +14,76 @@
#include "util.h"
#include "Device.h"
void stopAndDeletePairedDevice (PairedDevice &pairedDevice)
{
log_printf(INFO, "%s (Device %d on %s) disconnected", pairedDevice.device->name.c_str(), pairedDevice.device->index, pairedDevice.device->path.c_str());
pairedDevice.device->stop();
pairedDevice.associatedThread.join();
delete(pairedDevice.device);
}
DeviceFinder::~DeviceFinder()
{
this->devicesMutex.lock();
for (auto it = this->devices.begin(); it != this->devices.end(); it++) {
for (auto jt = it->second.begin(); jt != it->second.end(); jt++) {
stopAndDeletePairedDevice(jt->second);
}
}
this->devicesMutex.unlock();
}
void DeviceFinder::insertNewDevice(const std::string &path, HIDPP::DeviceIndex index)
{
Device *device = new Device(path, index);
this->devicesMutex.lock();
log_printf(INFO, "%s detected: device %d on %s", device->name.c_str(), index, path.c_str());
auto pathBucket = this->devices.emplace(path, std::map<HIDPP::DeviceIndex, PairedDevice>()).first;
pathBucket->second.emplace(index, PairedDevice{
device,
std::thread([device]() {
device->start();
})
});
this->devicesMutex.unlock();
}
void DeviceFinder::stopAndDeleteAllDevicesIn (const std::string &path)
{
this->devicesMutex.lock();
auto pathBucket = this->devices.find(path);
if (pathBucket != this->devices.end())
{
for (auto& indexBucket : pathBucket->second) {
stopAndDeletePairedDevice(indexBucket.second);
}
this->devices.erase(pathBucket);
}
this->devicesMutex.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)
{
this->devicesMutex.lock();
auto pathBucket = this->devices.find(path);
if (pathBucket != this->devices.end())
{
auto indexBucket = pathBucket->second.find(index);
if (indexBucket != pathBucket->second.end())
{
stopAndDeletePairedDevice(indexBucket->second);
pathBucket->second.erase(indexBucket);
}
}
this->devicesMutex.unlock();
log_printf(WARN, "Attempted to disconnect not previously connected device %d on %s", index, path.c_str());
}
void DeviceFinder::addDevice(const char *path)
{
std::string string_path(path);
@ -34,6 +104,7 @@ void DeviceFinder::addDevice(const char *path)
HIDPP::WirelessDevice3, HIDPP::WirelessDevice4,
HIDPP::WirelessDevice5, HIDPP::WirelessDevice6})
{
if(!has_receiver_index && index == HIDPP::WirelessDevice1)
break;
for(int i = 0; i < max_tries; i++)
@ -48,18 +119,7 @@ void DeviceFinder::addDevice(const char *path)
has_receiver_index = true;
if(major > 1) // HID++ 2.0 devices only
{
auto dev = new Device(string_path, index);
this->devicesMutex.lock();
this->devices.insert({
dev,
std::thread{[dev]() {
dev->start();
}}
});
this->devicesMutex.unlock();
log_printf(INFO, "%s detected: device %d on %s", d.name().c_str(), index, string_path.c_str());
this->insertNewDevice(string_path, index);
}
break;
}
@ -106,18 +166,5 @@ void DeviceFinder::addDevice(const char *path)
void DeviceFinder::removeDevice(const char* path)
{
devicesMutex.lock();
// Iterate through Devices, stop all in path
for (auto it = devices.begin(); it != devices.end(); it++)
{
if(it->first->path == path)
{
log_printf(INFO, "%s on %s disconnected.", it->first->name.c_str(), path);
it->first->stop();
it->second.join();
delete(it->first);
devices.erase(it);
}
}
devicesMutex.unlock();
this->stopAndDeleteAllDevicesIn(std::string(path));
}

@ -5,22 +5,34 @@
#include <hidpp10/Device.h>
#include <hidpp10/IReceiver.h>
#include <hidpp20/IReprogControls.h>
#include <map>
#include <unordered_map>
#include <list>
#include <thread>
#include "Device.h"
#include <mutex>
#include "Device.h"
class Device;
struct PairedDevice {
Device *device;
std::thread associatedThread;
};
class DeviceFinder : public HID::DeviceMonitor
{
public:
std::map<Device*, std::thread> devices;
~DeviceFinder();
void insertNewDevice (const std::string &path, HIDPP::DeviceIndex index);
void stopAndDeleteAllDevicesIn (const std::string &path);
void stopAndDeleteDevice (const std::string &path, HIDPP::DeviceIndex index);
protected:
void addDevice(const char* path);
void removeDevice(const char* path);
private:
std::mutex devicesMutex;
std::map<std::string, std::map<HIDPP::DeviceIndex, PairedDevice>> devices;
};
extern DeviceFinder* finder;

Loading…
Cancel
Save