Add ability to blacklist devices

This commit is contained in:
pixl 2020-07-04 00:10:44 -04:00
parent 2654f319c6
commit ef84577b9c
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
5 changed files with 60 additions and 2 deletions

View File

@ -31,6 +31,36 @@ Configuration::Configuration(const char *config_file)
throw e; throw e;
} }
const Setting &root = cfg.getRoot(); const Setting &root = cfg.getRoot();
try
{
auto& _blacklist = root.lookup("blacklist");
if(_blacklist.isArray() || _blacklist.isList())
{
int len = _blacklist.getLength();
for(int i = 0; i < len; i++)
{
if(!_blacklist[i].isNumber()) {
log_printf(WARN, "Line %d: blacklist must only contain "
"PIDs", _blacklist[i].getSourceLine());
if(_blacklist.isArray())
break;
if(_blacklist.isList())
continue;
}
blacklist.push_back((int)_blacklist[i]);
}
}
else
{
log_printf(WARN, "Line %d: blacklist must be an array or list, "
"ignnoring.", _blacklist.getSourceLine());
}
}
catch(const SettingNotFoundException &e)
{
}
Setting* _devices; Setting* _devices;
try { _devices = &root["devices"]; } try { _devices = &root["devices"]; }

View File

@ -32,6 +32,7 @@ namespace logid
Configuration(const char* config_file); Configuration(const char* config_file);
Configuration() {} Configuration() {}
std::map<std::string, DeviceConfig*> devices; std::map<std::string, DeviceConfig*> devices;
std::vector<uint16_t> blacklist;
private: private:
libconfig::Config cfg; libconfig::Config cfg;
}; };

View File

@ -45,6 +45,14 @@ bool Device::init()
catch(HIDPP20::Error &e) { return false; } catch(HIDPP20::Error &e) { return false; }
name = hidpp_dev->name(); name = hidpp_dev->name();
if(std::find(global_config->blacklist.begin(), global_config->blacklist.end(),
hidpp_dev->productID()) != global_config->blacklist.end())
{
log_printf(INFO, "Ignored blacklisted device %s", name.c_str());
throw BlacklistedDevice();
}
features = getFeatures(); features = getFeatures();
// Set config, if none is found for this device then use default // Set config, if none is found for this device then use default
if(global_config->devices.find(name) == global_config->devices.end()) if(global_config->devices.find(name) == global_config->devices.end())
@ -237,7 +245,11 @@ void Device::waitForReceiver()
usleep(200000); usleep(200000);
try
{
if(this->init()) break; if(this->init()) break;
}
catch(BlacklistedDevice& e) { return; }
log_printf(ERROR, "Failed to initialize device %d on %s, waiting for receiver"); log_printf(ERROR, "Failed to initialize device %d on %s, waiting for receiver");
delete(listener); delete(listener);

View File

@ -18,6 +18,16 @@ namespace logid
class EventListener; class EventListener;
class DeviceConfig; class DeviceConfig;
class BlacklistedDevice : public std::exception
{
public:
BlacklistedDevice() = default;
virtual const char* what()
{
return "Blacklisted device";
}
};
class Device class Device
{ {
public: public:

View File

@ -40,10 +40,15 @@ DeviceFinder::~DeviceFinder()
this->devices_mutex.unlock(); this->devices_mutex.unlock();
} }
///TODO: Unused return variable?
Device* DeviceFinder::insertNewDevice(const std::string &path, HIDPP::DeviceIndex index) Device* DeviceFinder::insertNewDevice(const std::string &path, HIDPP::DeviceIndex index)
{ {
auto device = new Device(path, index); auto device = new Device(path, index);
try
{
device->init(); device->init();
}
catch(BlacklistedDevice& e) { return nullptr; }
this->devices_mutex.lock(); this->devices_mutex.lock();
log_printf(INFO, "%s detected: device %d on %s", device->name.c_str(), index, path.c_str()); log_printf(INFO, "%s detected: device %d on %s", device->name.c_str(), index, path.c_str());