Data.cpp 18.1 KB
Newer Older
Philip Müller's avatar
Philip Müller committed
1
/*
2
 *  This file is part of the mhwd - Manjaro Hardware Detection project
3
 *
dec's avatar
dec committed
4 5 6
 *  mhwd - Manjaro Hardware Detection
 *  Roland Singer <roland@manjaro.org>
 *  Łukasz Matysiak <december0123@gmail.com>
Philip Müller's avatar
Philip Müller committed
7
 *  Filipe Marques <eagle.software3@gmail.com>
Philip Müller's avatar
Philip Müller committed
8
 *
dec's avatar
dec committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  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/>.
Philip Müller's avatar
Philip Müller committed
23 24
 */

Łukasz Matysiak's avatar
Łukasz Matysiak committed
25 26
#include "Data.hpp"

Philip Müller's avatar
Philip Müller committed
27
#include <dirent.h>
december0123's avatar
december0123 committed
28

december0123's avatar
december0123 committed
29
#include <algorithm>
Philip Müller's avatar
Philip Müller committed
30 31 32
#include <fstream>
#include <iomanip>
#include <sstream>
december0123's avatar
december0123 committed
33 34 35
#include <string>
#include <vector>

Philip Müller's avatar
Philip Müller committed
36 37
Data::Data()
{
38 39
    fillDevices(hw_pci, PCIDevices);
    fillDevices(hw_usb, USBDevices);
Philip Müller's avatar
Philip Müller committed
40

december0123's avatar
december0123 committed
41
    updateConfigData();
Philip Müller's avatar
Philip Müller committed
42 43 44 45
}

void Data::updateInstalledConfigData()
{
december0123's avatar
december0123 committed
46
    // Clear config vectors in each device element
47 48

    for (auto& PCIDevice : PCIDevices)
december0123's avatar
december0123 committed
49
    {
50
        PCIDevice->installedConfigs_.clear();
december0123's avatar
december0123 committed
51 52
    }

53
    for (auto& USBDevice : USBDevices)
december0123's avatar
december0123 committed
54
    {
55
        USBDevice->installedConfigs_.clear();
december0123's avatar
december0123 committed
56 57 58 59 60 61 62 63 64
    }

    installedPCIConfigs.clear();
    installedUSBConfigs.clear();

    // Refill data
    fillInstalledConfigs("PCI");
    fillInstalledConfigs("USB");

december0123's avatar
december0123 committed
65 66
    setMatchingConfigs(PCIDevices, installedPCIConfigs, true);
    setMatchingConfigs(USBDevices, installedUSBConfigs, true);
Philip Müller's avatar
Philip Müller committed
67 68
}

69
void Data::fillInstalledConfigs(std::string type)
Philip Müller's avatar
Philip Müller committed
70
{
december0123's avatar
december0123 committed
71
    std::vector<std::string> configPaths;
december0123's avatar
december0123 committed
72
    std::vector<std::shared_ptr<Config>>* configs;
december0123's avatar
december0123 committed
73

74
    if ("USB" == type)
december0123's avatar
december0123 committed
75 76 77 78 79 80 81 82 83 84
    {
        configs = &installedUSBConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_USB_DATABASE_DIR, MHWD_CONFIG_NAME);
    }
    else
    {
        configs = &installedPCIConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_PCI_DATABASE_DIR, MHWD_CONFIG_NAME);
    }

85
    for (const auto& configPath : configPaths)
december0123's avatar
december0123 committed
86
    {
87
        Config *config = new Config(configPath, type);
december0123's avatar
december0123 committed
88

89
        if (config->readConfigFile(configPath))
december0123's avatar
december0123 committed
90
        {
december0123's avatar
december0123 committed
91
            configs->push_back(std::shared_ptr<Config>{config});
december0123's avatar
december0123 committed
92 93 94
        }
        else
        {
december0123's avatar
december0123 committed
95
            invalidConfigs.push_back(std::shared_ptr<Config>{config});
december0123's avatar
december0123 committed
96 97
        }
    }
Philip Müller's avatar
Philip Müller committed
98 99
}

december0123's avatar
december0123 committed
100
void Data::getAllDevicesOfConfig(std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices)
Philip Müller's avatar
Philip Müller committed
101
{
december0123's avatar
december0123 committed
102
    std::vector<std::shared_ptr<Device>> devices;
december0123's avatar
december0123 committed
103

104
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
105 106 107 108 109 110 111 112
    {
        devices = USBDevices;
    }
    else
    {
        devices = PCIDevices;
    }

december0123's avatar
december0123 committed
113
    getAllDevicesOfConfig(devices, config, foundDevices);
Philip Müller's avatar
Philip Müller committed
114 115
}

december0123's avatar
december0123 committed
116 117 118
void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& devices,
        std::shared_ptr<Config> config,
        std::vector<std::shared_ptr<Device>>& foundDevices)
Philip Müller's avatar
Philip Müller committed
119
{
december0123's avatar
december0123 committed
120
    foundDevices.clear();
december0123's avatar
december0123 committed
121

122
    for (auto&& hwdID = config->hwdIDs_.begin();
december0123's avatar
december0123 committed
123 124 125 126
            hwdID != config->hwdIDs_.end(); ++hwdID)
    {
        bool foundDevice = false;
        // Check all devices
127
        for (auto&& i_device = devices.begin(); i_device != devices.end();
december0123's avatar
december0123 committed
128 129 130
                ++i_device)
        {
            // Check class ids
131
            bool found = std::find_if(hwdID->classIDs.begin(), hwdID->classIDs.end(), [i_device](const std::string& classID){
132 133
                                return (("*" == classID) || (classID == (*i_device)->classID_));
                            }) != hwdID->classIDs.end();
december0123's avatar
december0123 committed
134

135
            if (found)
december0123's avatar
december0123 committed
136 137
            {
                // Check blacklisted class ids
138
                found = std::find_if(hwdID->blacklistedClassIDs.begin(), hwdID->blacklistedClassIDs.end(), [i_device](const std::string& blacklistedClassID){
139 140
                                return (blacklistedClassID == (*i_device)->classID_);
                            }) != hwdID->blacklistedClassIDs.end();
december0123's avatar
december0123 committed
141

142
                if (!found)
december0123's avatar
december0123 committed
143 144
                {
                    // Check vendor ids
145 146 147
                    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();
december0123's avatar
december0123 committed
148

149
                    if (found)
december0123's avatar
december0123 committed
150 151
                    {
                        // Check blacklisted vendor ids
152
                        found = std::find_if(hwdID->blacklistedVendorIDs.begin(), hwdID->blacklistedVendorIDs.end(), [i_device](const std::string& blacklistedVendorID){
153 154
                                        return (blacklistedVendorID == (*i_device)->vendorID_);
                                    }) != hwdID->blacklistedVendorIDs.end();
december0123's avatar
december0123 committed
155

156
                        if (!found)
december0123's avatar
december0123 committed
157 158
                        {
                            // Check device ids
159 160 161
                            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();
december0123's avatar
december0123 committed
162

163
                            if (found)
december0123's avatar
december0123 committed
164 165
                            {
                                // Check blacklisted device ids
166
                                found = std::find_if(hwdID->blacklistedDeviceIDs.begin(), hwdID->blacklistedDeviceIDs.end(), [i_device](const std::string& blacklistedDeviceID){
167 168
                                                return (blacklistedDeviceID == (*i_device)->deviceID_);
                                            }) != hwdID->blacklistedDeviceIDs.end();
169
                                if (!found)
december0123's avatar
december0123 committed
170 171
                                {
                                    foundDevice = true;
december0123's avatar
december0123 committed
172
                                    foundDevices.push_back(*i_device);
december0123's avatar
december0123 committed
173 174 175 176 177 178 179 180 181 182
                                }
                            }
                        }
                    }
                }
            }
        }

        if (!foundDevice)
        {
december0123's avatar
december0123 committed
183
            foundDevices.clear();
december0123's avatar
december0123 committed
184 185 186
            return;
        }
    }
Philip Müller's avatar
Philip Müller committed
187 188
}

december0123's avatar
december0123 committed
189 190
std::vector<std::shared_ptr<Config>> Data::getAllDependenciesToInstall(
        std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
191
{
december0123's avatar
december0123 committed
192 193
    std::vector<std::shared_ptr<Config>> depends;
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
194

195
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
196 197 198 199 200 201 202 203
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

december0123's avatar
december0123 committed
204
    getAllDependenciesToInstall(config, installedConfigs, &depends);
december0123's avatar
december0123 committed
205 206

    return depends;
Philip Müller's avatar
Philip Müller committed
207 208
}

december0123's avatar
december0123 committed
209 210 211
void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config,
        std::vector<std::shared_ptr<Config>>& installedConfigs,
        std::vector<std::shared_ptr<Config>> *dependencies)
Philip Müller's avatar
Philip Müller committed
212
{
Łukasz Matysiak's avatar
Łukasz Matysiak committed
213
    for (const auto& configDependency : config->dependencies_)
december0123's avatar
december0123 committed
214
    {
december0123's avatar
december0123 committed
215
        auto found = std::find_if(installedConfigs.begin(), installedConfigs.end(),
Łukasz Matysiak's avatar
Łukasz Matysiak committed
216
                [configDependency](const std::shared_ptr<Config>& config) -> bool {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
217
                    return (config->name_ == configDependency);
218
                }) != installedConfigs.end();
december0123's avatar
december0123 committed
219

220
        if (!found)
december0123's avatar
december0123 committed
221
        {
december0123's avatar
december0123 committed
222
            found = std::find_if(dependencies->begin(), dependencies->end(),
Łukasz Matysiak's avatar
Łukasz Matysiak committed
223
                    [configDependency](const std::shared_ptr<Config>& config) -> bool {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
224
                        return (config->name_ == configDependency);
225
                    }) != dependencies->end();
december0123's avatar
december0123 committed
226

227
            if (!found)
december0123's avatar
december0123 committed
228
            {
december0123's avatar
december0123 committed
229
                // Add to vector and check for further subdepends...
230
                std::shared_ptr<Config> dependconfig {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
231
                    getDatabaseConfig(configDependency, config->type_)};
232
                if (nullptr != dependconfig)
december0123's avatar
december0123 committed
233
                {
dec's avatar
dec committed
234
                    dependencies->emplace_back(dependconfig);
december0123's avatar
december0123 committed
235 236
                    getAllDependenciesToInstall(dependconfig, installedConfigs, dependencies);
                }
december0123's avatar
december0123 committed
237 238 239
            }
        }
    }
Philip Müller's avatar
Philip Müller committed
240 241
}

december0123's avatar
december0123 committed
242
std::shared_ptr<Config> Data::getDatabaseConfig(const std::string configName,
december0123's avatar
december0123 committed
243
        const std::string configType)
Philip Müller's avatar
Philip Müller committed
244
{
december0123's avatar
december0123 committed
245
    std::vector<std::shared_ptr<Config>> allConfigs;
december0123's avatar
december0123 committed
246

247
    if ("USB" == configType)
december0123's avatar
december0123 committed
248 249 250 251 252 253 254 255
    {
        allConfigs = allUSBConfigs;
    }
    else
    {
        allConfigs = allPCIConfigs;
    }

256
    for (auto& config : allConfigs)
december0123's avatar
december0123 committed
257
    {
258 259

        if (configName == config->name_)
december0123's avatar
december0123 committed
260
        {
261 262

            return config;
december0123's avatar
december0123 committed
263 264 265 266
        }
    }

    return nullptr;
Philip Müller's avatar
Philip Müller committed
267 268
}

december0123's avatar
december0123 committed
269
std::vector<std::shared_ptr<Config>> Data::getAllLocalConflicts(std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
270
{
december0123's avatar
december0123 committed
271 272 273
    std::vector<std::shared_ptr<Config>> conflicts;
    std::vector<std::shared_ptr<Config>> dependencies = getAllDependenciesToInstall(config);
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
274

275
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
276 277 278 279 280 281 282 283
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

dec's avatar
dec committed
284
    dependencies.emplace_back(config);
december0123's avatar
december0123 committed
285

Łukasz Matysiak's avatar
Łukasz Matysiak committed
286
    for (const auto& dependency : dependencies)
december0123's avatar
december0123 committed
287
    {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
288
        for (const auto& dependencyConflict : dependency->conflicts_)
december0123's avatar
december0123 committed
289
        {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
290
            for (auto& installedConfig : installedConfigs)
december0123's avatar
december0123 committed
291
            {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
292
                if (dependencyConflict == installedConfig->name_)
december0123's avatar
december0123 committed
293
                {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
294 295 296 297 298
                    bool found = std::find_if(conflicts.begin(), conflicts.end(),
                            [&dependencyConflict](const std::shared_ptr<Config>& conflict)
                            {
                                return conflict->name_ == dependencyConflict;
                            }) != conflicts.end();
december0123's avatar
december0123 committed
299

300
                    if (!found)
december0123's avatar
december0123 committed
301
                    {
Łukasz Matysiak's avatar
Łukasz Matysiak committed
302
                        conflicts.emplace_back(installedConfig);
december0123's avatar
december0123 committed
303 304 305 306 307 308 309 310
                        break;
                    }
                }
            }
        }
    }

    return conflicts;
Philip Müller's avatar
Philip Müller committed
311 312
}

december0123's avatar
december0123 committed
313
std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
314
{
december0123's avatar
december0123 committed
315 316
    std::vector<std::shared_ptr<Config>> requirements;
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
317

318
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
319 320 321 322 323 324 325 326 327
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

    // Check if this config is required by another installed config
328
    for (auto&& installedConfig = installedConfigs.begin();
329
            installedConfig != installedConfigs.end(); ++installedConfig)
december0123's avatar
december0123 committed
330
    {
331
        for (auto&& dependency = (*installedConfig)->dependencies_.begin();
december0123's avatar
december0123 committed
332 333
                dependency != (*installedConfig)->dependencies_.end(); dependency++)
        {
334
            if ((*dependency) == config->name_)
december0123's avatar
december0123 committed
335 336
            {
                // Check if already in vector
337 338 339 340 341
                bool found = std::find_if(requirements.begin(), requirements.end(),
                        [&installedConfig](const std::shared_ptr<Config>& req)
                        {
                            return req->name_ == (*installedConfig)->name_;
                        }) != requirements.end();
december0123's avatar
december0123 committed
342 343 344

                if (!found)
                {
dec's avatar
dec committed
345
                    requirements.emplace_back(*installedConfig);
december0123's avatar
december0123 committed
346 347 348 349 350 351 352
                    break;
                }
            }
        }
    }

    return requirements;
Philip Müller's avatar
Philip Müller committed
353 354
}

355
void Data::fillDevices(hw_item hw, std::vector<std::shared_ptr<Device>>& devices)
Philip Müller's avatar
Philip Müller committed
356
{
december0123's avatar
december0123 committed
357
    // Get the hardware devices
dec's avatar
dec committed
358 359
    std::unique_ptr<hd_data_t> hd_data{new hd_data_t()};
    hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr);
december0123's avatar
december0123 committed
360

dec's avatar
dec committed
361
    std::unique_ptr<Device> device;
dec's avatar
dec committed
362
    for (hd_t *hdIter = hd; hdIter; hdIter = hdIter->next)
december0123's avatar
december0123 committed
363
    {
dec's avatar
dec committed
364
        device.reset(new Device());
365
        device->type_ = (hw == hw_usb ? "USB" : "PCI");
dec's avatar
dec committed
366 367 368 369 370 371 372 373
        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);
374
        devices.emplace_back(device.release());
december0123's avatar
december0123 committed
375 376
    }

dec's avatar
dec committed
377
    hd_free_hd_list(hd);
dec's avatar
dec committed
378
    hd_free_hd_data(hd_data.get());
Philip Müller's avatar
Philip Müller committed
379 380
}

381
void Data::fillAllConfigs(std::string type)
Philip Müller's avatar
Philip Müller committed
382
{
december0123's avatar
december0123 committed
383
    std::vector<std::string> configPaths;
december0123's avatar
december0123 committed
384
    std::vector<std::shared_ptr<Config>>* configs;
december0123's avatar
december0123 committed
385

386
    if ("USB" == type)
december0123's avatar
december0123 committed
387 388 389 390 391 392 393 394 395 396
    {
        configs = &allUSBConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_USB_CONFIG_DIR, MHWD_CONFIG_NAME);
    }
    else
    {
        configs = &allPCIConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_PCI_CONFIG_DIR, MHWD_CONFIG_NAME);
    }

397
    for (auto&& configPath = configPaths.begin();
december0123's avatar
december0123 committed
398 399
            configPath != configPaths.end(); ++configPath)
    {
dec's avatar
dec committed
400
        std::unique_ptr<Config> config{new Config((*configPath), type)};
december0123's avatar
december0123 committed
401 402 403

        if (config->readConfigFile((*configPath)))
        {
dec's avatar
dec committed
404
            configs->emplace_back(config.release());
december0123's avatar
december0123 committed
405 406 407
        }
        else
        {
dec's avatar
dec committed
408
            invalidConfigs.emplace_back(config.release());
december0123's avatar
december0123 committed
409 410
        }
    }
Philip Müller's avatar
Philip Müller committed
411 412
}

413
std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& directoryPath,
december0123's avatar
december0123 committed
414
        std::string onlyFilename)
Philip Müller's avatar
Philip Müller committed
415
{
december0123's avatar
december0123 committed
416
    std::vector<std::string> list;
december0123's avatar
december0123 committed
417
    struct dirent *dir = nullptr;
dec's avatar
dec committed
418
    DIR* d = opendir(directoryPath.c_str());
december0123's avatar
december0123 committed
419 420
    if (d)
    {
421
        while (nullptr != (dir = readdir(d)))
december0123's avatar
december0123 committed
422
        {
423 424
            const std::string filename {dir->d_name};
            if (("." != filename) && (".." != filename) && ("" != filename))
december0123's avatar
december0123 committed
425
            {
426
                std::string filepath {directoryPath + "/" + filename};
december0123's avatar
december0123 committed
427 428 429
                struct stat filestatus;
                lstat(filepath.c_str(), &filestatus);

december0123's avatar
december0123 committed
430 431
                if (S_ISREG(filestatus.st_mode) &&
                        (onlyFilename.empty() || (onlyFilename == filename)))
december0123's avatar
december0123 committed
432 433 434 435 436 437 438 439
                {
                    list.push_back(filepath);
                }
                else if (S_ISDIR(filestatus.st_mode))
                {
                    std::vector<std::string> templist = getRecursiveDirectoryFileList(filepath,
                            onlyFilename);

440
                    for (auto&& iterator = templist.begin();
december0123's avatar
december0123 committed
441 442 443 444 445 446 447 448 449 450
                            iterator != templist.end(); iterator++)
                    {
                        list.push_back((*iterator));
                    }
                }
            }
        }

        closedir(d);
    }
december0123's avatar
december0123 committed
451
    delete dir;
december0123's avatar
december0123 committed
452
    return list;
Philip Müller's avatar
Philip Müller committed
453 454 455 456
}

Vita::string Data::getRightConfigPath(Vita::string str, Vita::string baseConfigPath)
{
december0123's avatar
december0123 committed
457
    str = str.trim();
458
    if ((str.size() <= 0) || (str.substr(0, 1) == "/"))
december0123's avatar
december0123 committed
459 460 461 462
    {
        return str;
    }
    return baseConfigPath + "/" + str;
Philip Müller's avatar
Philip Müller committed
463 464 465 466
}

std::vector<std::string> Data::splitValue(Vita::string str, Vita::string onlyEnding)
{
467
    std::vector<Vita::string> work {str.toLower().explode(" ")};
december0123's avatar
december0123 committed
468 469
    std::vector<std::string> final;

470
    for (auto&& iterator = work.begin(); iterator != work.end();
december0123's avatar
december0123 committed
471 472
            iterator++)
    {
473
        if (("" != *iterator) && onlyEnding.empty())
december0123's avatar
december0123 committed
474 475 476
        {
            final.push_back(*iterator);
        }
477 478
        else if (("" != *iterator) && (Vita::string(*iterator).explode(".").back() == onlyEnding)
                && ((*iterator).size() > 5))
december0123's avatar
december0123 committed
479 480 481 482 483 484
        {
            final.push_back(Vita::string(*iterator).substr(0, (*iterator).size() - 5));
        }
    }

    return final;
Philip Müller's avatar
Philip Müller committed
485 486 487 488
}

void Data::updateConfigData()
{
december0123's avatar
december0123 committed
489
    // Clear config vectors in each device element
490
    for (auto&& PCIDevice = PCIDevices.begin();
december0123's avatar
december0123 committed
491 492
            PCIDevice != PCIDevices.end(); PCIDevice++)
    {
december0123's avatar
december0123 committed
493
        (*PCIDevice)->availableConfigs_.clear();
december0123's avatar
december0123 committed
494 495
    }

496
    for (auto&& USBDevice = USBDevices.begin();
december0123's avatar
december0123 committed
497 498
            USBDevice != USBDevices.end(); USBDevice++)
    {
december0123's avatar
december0123 committed
499
        (*USBDevice)->availableConfigs_.clear();
december0123's avatar
december0123 committed
500 501 502 503 504 505 506 507
    }
    allPCIConfigs.clear();
    allUSBConfigs.clear();

    // Refill data
    fillAllConfigs("PCI");
    fillAllConfigs("USB");

december0123's avatar
december0123 committed
508 509
    setMatchingConfigs(PCIDevices, allPCIConfigs, false);
    setMatchingConfigs(USBDevices, allUSBConfigs, false);
december0123's avatar
december0123 committed
510 511 512

    // Update also installed config data
    updateInstalledConfigData();
Philip Müller's avatar
Philip Müller committed
513 514
}

december0123's avatar
december0123 committed
515
void Data::setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& devices,
516
        std::vector<std::shared_ptr<Config>>& configs, bool setAsInstalled)
Philip Müller's avatar
Philip Müller committed
517
{
518
    for (auto&& config = configs.begin(); config != configs.end();
december0123's avatar
december0123 committed
519 520 521 522
            ++config)
    {
        setMatchingConfig((*config), devices, setAsInstalled);
    }
Philip Müller's avatar
Philip Müller committed
523 524
}

525
void Data::setMatchingConfig(std::shared_ptr<Config> config,
526
        const std::vector<std::shared_ptr<Device>>& devices, bool setAsInstalled)
Philip Müller's avatar
Philip Müller committed
527
{
december0123's avatar
december0123 committed
528
    std::vector<std::shared_ptr<Device>> foundDevices;
december0123's avatar
december0123 committed
529

december0123's avatar
december0123 committed
530
    getAllDevicesOfConfig(devices, config, foundDevices);
december0123's avatar
december0123 committed
531 532

    // Set config to all matching devices
533
    for (auto&& foundDevice = foundDevices.begin();
december0123's avatar
december0123 committed
534 535 536 537
            foundDevice != foundDevices.end(); ++foundDevice)
    {
        if (setAsInstalled)
        {
december0123's avatar
december0123 committed
538
            addConfigSorted((*foundDevice)->installedConfigs_, config);
december0123's avatar
december0123 committed
539 540 541
        }
        else
        {
december0123's avatar
december0123 committed
542
            addConfigSorted((*foundDevice)->availableConfigs_, config);
december0123's avatar
december0123 committed
543 544
        }
    }
Philip Müller's avatar
Philip Müller committed
545 546
}

547
void Data::addConfigSorted(std::vector<std::shared_ptr<Config>>& configs,
548
        std::shared_ptr<Config> newConfig)
Philip Müller's avatar
Philip Müller committed
549
{
550 551 552 553 554
    bool found = std::find_if(configs.begin(), configs.end(),
            [&newConfig](const std::shared_ptr<Config>& config)
            {
                return newConfig->name_ == config->name_;
            }) != configs.end();
555

556
    if (!found)
december0123's avatar
december0123 committed
557
    {
558
        for (auto&& config = configs.begin(); config != configs.end(); ++config)
december0123's avatar
december0123 committed
559
        {
560 561 562 563 564
            if (newConfig->priority_ > (*config)->priority_)
            {
                configs.insert(config, std::shared_ptr<Config>(newConfig));
                return;
            }
december0123's avatar
december0123 committed
565
        }
566
        configs.emplace_back(newConfig);
december0123's avatar
december0123 committed
567
    }
Philip Müller's avatar
Philip Müller committed
568 569
}

december0123's avatar
december0123 committed
570
Vita::string Data::from_Hex(std::uint16_t hexnum, int fill)
Philip Müller's avatar
Philip Müller committed
571
{
december0123's avatar
december0123 committed
572 573 574
    std::stringstream stream;
    stream << std::hex << std::setfill('0') << std::setw(fill) << hexnum;
    return stream.str();
Philip Müller's avatar
Philip Müller committed
575 576
}

577
std::string Data::from_CharArray(char* c)
Philip Müller's avatar
Philip Müller committed
578
{
579
    if (nullptr == c)
december0123's avatar
december0123 committed
580 581 582
    {
        return "";
    }
Philip Müller's avatar
Philip Müller committed
583

584
    return std::string(c);
Philip Müller's avatar
Philip Müller committed
585
}