Newer
Older
Filipe Marques
committed
* This file is part of the mhwd - Manjaro Hardware Detection project
*
* mhwd - Manjaro Hardware Detection
* Roland Singer <roland@manjaro.org>
* Łukasz Matysiak <december0123@gmail.com>
* Copyright (C) 2007 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include "Data.hpp"
}
Data::~Data()
{
}
void Data::updateInstalledConfigData()
{
for (auto&& PCIDevice = PCIDevices.begin();
PCIDevice != PCIDevices.end(); ++PCIDevice)
for (auto&& USBDevice = USBDevices.begin();
USBDevice != USBDevices.end(); ++USBDevice)
}
installedPCIConfigs.clear();
installedUSBConfigs.clear();
// Refill data
fillInstalledConfigs("PCI");
fillInstalledConfigs("USB");
setMatchingConfigs(PCIDevices, installedPCIConfigs, true);
setMatchingConfigs(USBDevices, installedUSBConfigs, true);
void Data::fillInstalledConfigs(std::string type)
{
configs = &installedUSBConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_USB_DATABASE_DIR, MHWD_CONFIG_NAME);
}
else
{
configs = &installedPCIConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_PCI_DATABASE_DIR, MHWD_CONFIG_NAME);
}
for (auto&& configPath = configPaths.begin();
configPath != configPaths.end(); ++configPath)
{
Config *config = new Config((*configPath), type);
if (config->readConfigFile((*configPath)))
{
invalidConfigs.push_back(std::shared_ptr<Config>{config});
void Data::getAllDevicesOfConfig(std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices)
{
devices = USBDevices;
}
else
{
devices = PCIDevices;
}
getAllDevicesOfConfig(devices, config, foundDevices);
void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& devices,
std::shared_ptr<Config> config,
std::vector<std::shared_ptr<Device>>& foundDevices)
for (auto&& hwdID = config->hwdIDs_.begin();
hwdID != config->hwdIDs_.end(); ++hwdID)
{
bool foundDevice = false;
// Check all devices
for (auto&& i_device = devices.begin(); i_device != devices.end();
bool found = std::find_if(hwdID->classIDs.begin(), hwdID->classIDs.end(), [i_device](const std::string& classID){
return (("*" == classID) || (classID == (*i_device)->classID_));
}) != hwdID->classIDs.end();
if (!found)
{
continue;
}
else
{
// Check blacklisted class ids
found = std::find_if(hwdID->blacklistedClassIDs.begin(), hwdID->blacklistedClassIDs.end(), [i_device](const std::string& blacklistedClassID){
return (blacklistedClassID == (*i_device)->classID_);
}) != hwdID->blacklistedClassIDs.end();
if (found)
{
continue;
}
else
{
// Check vendor ids
found = std::find_if(hwdID->vendorIDs.begin(), hwdID->vendorIDs.end(), [i_device](const std::string& vendorID){
return (("*" == vendorID) || (vendorID == (*i_device)->vendorID_));
}) != hwdID->vendorIDs.end();
if (!found)
{
continue;
}
else
{
// Check blacklisted vendor ids
found = std::find_if(hwdID->blacklistedVendorIDs.begin(), hwdID->blacklistedVendorIDs.end(), [i_device](const std::string& blacklistedVendorID){
return (blacklistedVendorID == (*i_device)->vendorID_);
}) != hwdID->blacklistedVendorIDs.end();
if (found)
{
continue;
}
else
{
// Check device ids
found = std::find_if(hwdID->deviceIDs.begin(), hwdID->deviceIDs.end(), [i_device](const std::string& deviceID){
return (("*" == deviceID) || (deviceID == (*i_device)->deviceID_));
}) != hwdID->deviceIDs.end();
if (!found)
{
continue;
}
else
{
// Check blacklisted device ids
found = std::find_if(hwdID->blacklistedDeviceIDs.begin(), hwdID->blacklistedDeviceIDs.end(), [i_device](const std::string& blacklistedDeviceID){
return (blacklistedDeviceID == (*i_device)->deviceID_);
}) != hwdID->blacklistedDeviceIDs.end();
if (found)
{
continue;
}
else
{
foundDevice = true;
}
}
}
}
}
}
}
if (!foundDevice)
{
std::vector<std::shared_ptr<Config>> Data::getAllDependenciesToInstall(
std::shared_ptr<Config> config)
std::vector<std::shared_ptr<Config>> depends;
std::vector<std::shared_ptr<Config>> installedConfigs;
{
installedConfigs = installedUSBConfigs;
}
else
{
installedConfigs = installedPCIConfigs;
}
// Get all depends
getAllDependenciesToInstall(config, installedConfigs, &depends);
void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config,
std::vector<std::shared_ptr<Config>>& installedConfigs,
std::vector<std::shared_ptr<Config>> *dependencies)
for (auto&& configDependency = config->dependencies_.begin();
configDependency != config->dependencies_.end(); ++configDependency)
auto found = std::find_if(installedConfigs.begin(), installedConfigs.end(),
[configDependency](const std::shared_ptr<Config>& rhs) -> bool {
return (rhs->name_ == *configDependency);
found = std::find_if(dependencies->begin(), dependencies->end(),
[configDependency](const std::shared_ptr<Config>& rhs) -> bool {
return (rhs->name_ == *configDependency);
// Add to vector and check for further subdepends...
std::shared_ptr<Config> dependconfig {
getDatabaseConfig((*configDependency), config->type_)};
if (nullptr == dependconfig)
getAllDependenciesToInstall(dependconfig, installedConfigs, dependencies);
}
std::shared_ptr<Config> Data::getDatabaseConfig(const std::string configName,
{
allConfigs = allUSBConfigs;
}
else
{
allConfigs = allPCIConfigs;
}
for (auto&& config = allConfigs.begin(); config != allConfigs.end();
{
if (configName == (*config)->name_)
{
return (*config);
}
}
return nullptr;
std::vector<std::shared_ptr<Config>> Data::getAllLocalConflicts(std::shared_ptr<Config> config)
std::vector<std::shared_ptr<Config>> conflicts;
std::vector<std::shared_ptr<Config>> dependencies = getAllDependenciesToInstall(config);
std::vector<std::shared_ptr<Config>> installedConfigs;
{
installedConfigs = installedUSBConfigs;
}
else
{
installedConfigs = installedPCIConfigs;
}
for (auto&& dependency = dependencies.begin();
dependency != dependencies.end(); ++dependency)
for (auto&& dependencyConflict = (*dependency)->conflicts_.begin();
dependencyConflict != (*dependency)->conflicts_.end(); ++dependencyConflict)
for (auto&& installedConfig = installedConfigs.begin();
installedConfig != installedConfigs.end(); ++installedConfig)
{
if ((*dependencyConflict) != (*installedConfig)->name_)
{
continue;
}
else
{
// Check if already in vector
bool found = false;
for (auto&& conflict = conflicts.begin();
conflict != conflicts.end(); ++conflict)
{
if ((*conflict)->name_ == (*dependencyConflict))
{
found = true;
break;
}
}
if (found)
{
continue;
}
else
{
break;
}
}
}
}
}
return conflicts;
std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_ptr<Config> config)
std::vector<std::shared_ptr<Config>> requirements;
std::vector<std::shared_ptr<Config>> installedConfigs;
{
installedConfigs = installedUSBConfigs;
}
else
{
installedConfigs = installedPCIConfigs;
}
// Check if this config is required by another installed config
for (auto&& installedConfig = installedConfigs.begin();
installedConfig != installedConfigs.end(); ++installedConfig)
for (auto&& dependency = (*installedConfig)->dependencies_.begin();
dependency != (*installedConfig)->dependencies_.end(); dependency++)
{
if ((*dependency) != config->name_)
{
continue;
}
else
{
// Check if already in vector
bool found = false;
for (auto&& requirement = requirements.begin();
requirement != requirements.end(); ++requirement)
{
if ((*requirement)->name_ == (*installedConfig)->name_)
{
found = true;
break;
}
}
if (!found)
{
break;
}
}
}
}
return requirements;
void Data::fillDevices(std::string type)
{
hw = hw_usb;
devices = &USBDevices;
}
else
{
hw = hw_pci;
devices = &PCIDevices;
}
// Get the hardware devices
std::unique_ptr<hd_data_t> hd_data{new hd_data_t()};
hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr);
device->classID_ = from_Hex(hdIter->base_class.id, 2) + from_Hex(hdIter->sub_class.id, 2).toLower();
device->vendorID_ = from_Hex(hdIter->vendor.id, 4).toLower();
device->deviceID_ = from_Hex(hdIter->device.id, 4).toLower();
device->className_ = from_CharArray(hdIter->base_class.name);
device->vendorName_ = from_CharArray(hdIter->vendor.name);
device->deviceName_ = from_CharArray(hdIter->device.name);
device->sysfsBusID_ = from_CharArray(hdIter->sysfs_bus_id);
device->sysfsID_ = from_CharArray(hdIter->sysfs_id);
void Data::fillAllConfigs(std::string type)
{
configs = &allUSBConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_USB_CONFIG_DIR, MHWD_CONFIG_NAME);
}
else
{
configs = &allPCIConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_PCI_CONFIG_DIR, MHWD_CONFIG_NAME);
}
for (auto&& configPath = configPaths.begin();
configPath != configPaths.end(); ++configPath)
{
std::unique_ptr<Config> config{new Config((*configPath), type)};
if (config->readConfigFile((*configPath)))
{
bool Data::fillConfig(std::shared_ptr<Config> config, std::string configPath, std::string type)
config->type_ = type;
config->priority_ = 0;
config->freedriver_ = true;
config->basePath_ = configPath.substr(0, configPath.find_last_of('/'));
config->configPath_ = configPath;
// Add new HardwareIDs group to vector if vector is empty
if (config->hwdIDs_.empty())
{
return config->readConfigFile(config->configPath_);
std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& directoryPath,
while (nullptr != (dir = readdir(d)))
std::string filename = dir->d_name;
std::string filepath = directoryPath + "/" + filename;
if (("." == filename) || (".." == filename) || ("" == filename))
{
continue;
}
else
{
struct stat filestatus;
lstat(filepath.c_str(), &filestatus);
if (S_ISREG(filestatus.st_mode) &&
(onlyFilename.empty() || (onlyFilename == filename)))
{
list.push_back(filepath);
}
else if (S_ISDIR(filestatus.st_mode))
{
std::vector<std::string> templist = getRecursiveDirectoryFileList(filepath,
onlyFilename);
for (auto&& iterator = templist.begin();
iterator != templist.end(); iterator++)
{
list.push_back((*iterator));
}
}
}
}
closedir(d);
}
}
Vita::string Data::getRightConfigPath(Vita::string str, Vita::string baseConfigPath)
{
if ((str.size() <= 0) || (str.substr(0, 1) == "/"))
{
return str;
}
return baseConfigPath + "/" + str;
}
std::vector<std::string> Data::splitValue(Vita::string str, Vita::string onlyEnding)
{
std::vector<Vita::string> work = str.toLower().explode(" ");
std::vector<std::string> final;
for (auto&& iterator = work.begin(); iterator != work.end();
if (("" != *iterator) && onlyEnding.empty())
else if (("" != *iterator) && (Vita::string(*iterator).explode(".").back() == onlyEnding)
&& ((*iterator).size() > 5))
{
final.push_back(Vita::string(*iterator).substr(0, (*iterator).size() - 5));
}
}
return final;
for (auto&& PCIDevice = PCIDevices.begin();
for (auto&& USBDevice = USBDevices.begin();
}
allPCIConfigs.clear();
allUSBConfigs.clear();
// Refill data
fillAllConfigs("PCI");
fillAllConfigs("USB");
setMatchingConfigs(PCIDevices, allPCIConfigs, false);
setMatchingConfigs(USBDevices, allUSBConfigs, false);
// Update also installed config data
updateInstalledConfigData();
void Data::setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& devices,
std::vector<std::shared_ptr<Config>>& configs, bool setAsInstalled)
for (auto&& config = configs.begin(); config != configs.end();
++config)
{
setMatchingConfig((*config), devices, setAsInstalled);
}
void Data::setMatchingConfig(std::shared_ptr<Config> config,
const std::vector<std::shared_ptr<Device>>& devices, bool setAsInstalled)
getAllDevicesOfConfig(devices, config, foundDevices);
for (auto&& foundDevice = foundDevices.begin();
foundDevice != foundDevices.end(); ++foundDevice)
{
if (setAsInstalled)
{
addConfigSorted((*foundDevice)->installedConfigs_, config);
addConfigSorted((*foundDevice)->availableConfigs_, config);
void Data::addConfigSorted(std::vector<std::shared_ptr<Config>>& configs,
std::shared_ptr<Config> config)
for (auto&& iterator = configs.begin();
{
if (config->name_ == (*iterator)->name_)
{
return;
}
}
for (auto&& iterator = configs.begin(); iterator != configs.end();
iterator++)
{
if (config->priority_ > (*iterator)->priority_)
{
configs.insert(iterator, std::shared_ptr<Config>(config));
Vita::string Data::from_Hex(std::uint16_t hexnum, int fill)
std::stringstream stream;
stream << std::hex << std::setfill('0') << std::setw(fill) << hexnum;
return stream.str();
std::string Data::from_CharArray(char* c)