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

#include <dirent.h>
december0123's avatar
december0123 committed
23

december0123's avatar
december0123 committed
24
#include <algorithm>
Philip Müller's avatar
Philip Müller committed
25
26
27
#include <fstream>
#include <iomanip>
#include <sstream>
december0123's avatar
december0123 committed
28
29
30
31
32
#include <string>
#include <vector>

#include "Data.hpp"

Philip Müller's avatar
Philip Müller committed
33
34
Data::Data()
{
december0123's avatar
december0123 committed
35
36
    fillDevices("PCI");
    fillDevices("USB");
Philip Müller's avatar
Philip Müller committed
37

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

Data::~Data()
{
}

void Data::updateInstalledConfigData()
{
december0123's avatar
december0123 committed
47
    // Clear config vectors in each device element
48
    for (auto&& PCIDevice = PCIDevices.begin();
49
            PCIDevice != PCIDevices.end(); ++PCIDevice)
december0123's avatar
december0123 committed
50
    {
december0123's avatar
december0123 committed
51
        (*PCIDevice)->installedConfigs_.clear();
december0123's avatar
december0123 committed
52
53
    }

54
    for (auto&& USBDevice = USBDevices.begin();
55
            USBDevice != USBDevices.end(); ++USBDevice)
december0123's avatar
december0123 committed
56
    {
december0123's avatar
december0123 committed
57
        (*USBDevice)->installedConfigs_.clear();
december0123's avatar
december0123 committed
58
59
60
61
62
63
64
65
66
    }

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

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

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

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

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

87
    for (auto&& configPath = configPaths.begin();
december0123's avatar
december0123 committed
88
89
90
91
92
93
            configPath != configPaths.end(); ++configPath)
    {
        Config *config = new Config((*configPath), type);

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

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

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

december0123's avatar
december0123 committed
116
    getAllDevicesOfConfig(devices, config, foundDevices);
Philip Müller's avatar
Philip Müller committed
117
118
}

december0123's avatar
december0123 committed
119
120
121
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
122
{
december0123's avatar
december0123 committed
123
    foundDevices.clear();
december0123's avatar
december0123 committed
124

125
    for (auto&& hwdID = config->hwdIDs_.begin();
december0123's avatar
december0123 committed
126
127
128
129
            hwdID != config->hwdIDs_.end(); ++hwdID)
    {
        bool foundDevice = false;
        // Check all devices
130
        for (auto&& i_device = devices.begin(); i_device != devices.end();
december0123's avatar
december0123 committed
131
132
133
134
                ++i_device)
        {
            bool found = false;
            // Check class ids
135
            for (auto&& classID = hwdID->classIDs.begin();
december0123's avatar
december0123 committed
136
137
                    classID != hwdID->classIDs.end(); ++classID)
            {
december0123's avatar
december0123 committed
138
                if (*classID == "*" || *classID == (*i_device)->classID_)
december0123's avatar
december0123 committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                continue;
            }
            else
            {
                // Check blacklisted class ids
                found = false;
december0123's avatar
december0123 committed
153

154
                for (auto&& blacklistedClassID =
december0123's avatar
december0123 committed
155
                        (*hwdID).blacklistedClassIDs.begin();
december0123's avatar
december0123 committed
156
157
                        blacklistedClassID != (*hwdID).blacklistedClassIDs.end();
                        ++blacklistedClassID)
december0123's avatar
december0123 committed
158
                {
december0123's avatar
december0123 committed
159
                    if (*blacklistedClassID == (*i_device)->classID_)
december0123's avatar
december0123 committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
                    {
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    continue;
                }
                else
                {
                    // Check vendor ids
                    found = false;

175
                    for (auto&& vendorID = hwdID->vendorIDs.begin();
december0123's avatar
december0123 committed
176
177
                            vendorID != hwdID->vendorIDs.end(); ++vendorID)
                    {
178
                        if (("*" == *vendorID) || (*vendorID == (*i_device)->vendorID_))
december0123's avatar
december0123 committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        continue;
                    }
                    else
                    {
                        // Check blacklisted vendor ids
                        found = false;

194
                        for (auto&& blacklistedVendorID =
december0123's avatar
december0123 committed
195
                                hwdID->blacklistedVendorIDs.begin();
december0123's avatar
december0123 committed
196
197
                                blacklistedVendorID != hwdID->blacklistedVendorIDs.end();
                                ++blacklistedVendorID)
december0123's avatar
december0123 committed
198
                        {
december0123's avatar
december0123 committed
199
                            if (*blacklistedVendorID == (*i_device)->vendorID_)
december0123's avatar
december0123 committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
                            {
                                found = true;
                                break;
                            }
                        }

                        if (found)
                        {
                            continue;
                        }
                        else
                        {
                            // Check device ids
                            found = false;

215
                            for (auto&& deviceID = hwdID->deviceIDs.begin();
december0123's avatar
december0123 committed
216
217
                                    deviceID != hwdID->deviceIDs.end(); ++deviceID)
                            {
218
                                if (("*" == *deviceID) || (*deviceID == (*i_device)->deviceID_))
december0123's avatar
december0123 committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (!found)
                            {
                                continue;
                            }
                            else
                            {
                                // Check blacklisted device ids
                                found = false;

234
                                for (auto&& blacklistedDeviceID =
december0123's avatar
december0123 committed
235
                                        hwdID->blacklistedDeviceIDs.begin();
december0123's avatar
december0123 committed
236
237
                                        blacklistedDeviceID != hwdID->blacklistedDeviceIDs.end();
                                        ++blacklistedDeviceID)
december0123's avatar
december0123 committed
238
                                {
december0123's avatar
december0123 committed
239
                                    if (*blacklistedDeviceID == (*i_device)->deviceID_)
december0123's avatar
december0123 committed
240
241
242
243
244
245
246
247
248
249
250
251
252
                                    {
                                        found = true;
                                        break;
                                    }
                                }

                                if (found)
                                {
                                    continue;
                                }
                                else
                                {
                                    foundDevice = true;
december0123's avatar
december0123 committed
253
                                    foundDevices.push_back(*i_device);
december0123's avatar
december0123 committed
254
255
256
257
258
259
260
261
262
263
                                }
                            }
                        }
                    }
                }
            }
        }

        if (!foundDevice)
        {
december0123's avatar
december0123 committed
264
            foundDevices.clear();
december0123's avatar
december0123 committed
265
266
267
            return;
        }
    }
Philip Müller's avatar
Philip Müller committed
268
269
}

december0123's avatar
december0123 committed
270
271
std::vector<std::shared_ptr<Config>> Data::getAllDependenciesToInstall(
        std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
272
{
december0123's avatar
december0123 committed
273
274
    std::vector<std::shared_ptr<Config>> depends;
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
275
276

    // Get the right configs
277
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
278
279
280
281
282
283
284
285
286
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

    // Get all depends
december0123's avatar
december0123 committed
287
    getAllDependenciesToInstall(config, installedConfigs, &depends);
december0123's avatar
december0123 committed
288
289

    return depends;
Philip Müller's avatar
Philip Müller committed
290
291
}

december0123's avatar
december0123 committed
292
293
294
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
295
{
296
    for (auto&& configDependency = config->dependencies_.begin();
297
            configDependency != config->dependencies_.end(); ++configDependency)
december0123's avatar
december0123 committed
298
    {
december0123's avatar
december0123 committed
299
        auto found = std::find_if(installedConfigs.begin(), installedConfigs.end(),
dec's avatar
dec committed
300
301
                [configDependency](const std::shared_ptr<Config>& rhs) -> bool {
                    return (rhs->name_ == *configDependency);
december0123's avatar
december0123 committed
302
                });
december0123's avatar
december0123 committed
303

december0123's avatar
december0123 committed
304
        if (found != installedConfigs.end())
december0123's avatar
december0123 committed
305
306
307
308
309
        {
            continue;
        }
        else
        {
december0123's avatar
december0123 committed
310
            found = std::find_if(dependencies->begin(), dependencies->end(),
dec's avatar
dec committed
311
312
                    [configDependency](const std::shared_ptr<Config>& rhs) -> bool {
                        return (rhs->name_ == *configDependency);
december0123's avatar
december0123 committed
313
314
315
                    });

            if (found != dependencies->end())
december0123's avatar
december0123 committed
316
317
318
319
320
            {
                continue;
            }
            else
            {
december0123's avatar
december0123 committed
321
                // Add to vector and check for further subdepends...
322
323
324
                std::shared_ptr<Config> dependconfig {
                	getDatabaseConfig((*configDependency), config->type_)};
                if (nullptr == dependconfig)
december0123's avatar
december0123 committed
325
326
327
328
329
                {
                    continue;
                }
                else
                {
dec's avatar
dec committed
330
                    dependencies->emplace_back(dependconfig);
december0123's avatar
december0123 committed
331
332
                    getAllDependenciesToInstall(dependconfig, installedConfigs, dependencies);
                }
december0123's avatar
december0123 committed
333
334
335
            }
        }
    }
Philip Müller's avatar
Philip Müller committed
336
337
}

december0123's avatar
december0123 committed
338
std::shared_ptr<Config> Data::getDatabaseConfig(const std::string configName,
december0123's avatar
december0123 committed
339
        const std::string configType)
Philip Müller's avatar
Philip Müller committed
340
{
december0123's avatar
december0123 committed
341
    std::vector<std::shared_ptr<Config>> allConfigs;
december0123's avatar
december0123 committed
342
343

    // Get the right configs
344
    if ("USB" == configType)
december0123's avatar
december0123 committed
345
346
347
348
349
350
351
352
    {
        allConfigs = allUSBConfigs;
    }
    else
    {
        allConfigs = allPCIConfigs;
    }

353
    for (auto&& config = allConfigs.begin(); config != allConfigs.end();
354
            ++config)
december0123's avatar
december0123 committed
355
356
357
358
359
360
361
362
    {
        if (configName == (*config)->name_)
        {
            return (*config);
        }
    }

    return nullptr;
Philip Müller's avatar
Philip Müller committed
363
364
}

december0123's avatar
december0123 committed
365
std::vector<std::shared_ptr<Config>> Data::getAllLocalConflicts(std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
366
{
december0123's avatar
december0123 committed
367
368
369
    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
370
371

    // Get the right configs
372
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
373
374
375
376
377
378
379
380
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

dec's avatar
dec committed
381
    dependencies.emplace_back(config);
december0123's avatar
december0123 committed
382

383
    for (auto&& dependency = dependencies.begin();
384
            dependency != dependencies.end(); ++dependency)
december0123's avatar
december0123 committed
385
    {
386
387
        for (auto&& dependencyConflict = (*dependency)->conflicts_.begin();
                dependencyConflict != (*dependency)->conflicts_.end(); ++dependencyConflict)
december0123's avatar
december0123 committed
388
        {
389
            for (auto&& installedConfig = installedConfigs.begin();
390
                    installedConfig != installedConfigs.end(); ++installedConfig)
december0123's avatar
december0123 committed
391
392
393
394
395
396
397
398
399
            {
                if ((*dependencyConflict) != (*installedConfig)->name_)
                {
                    continue;
                }
                else
                {
                    // Check if already in vector
                    bool found = false;
400
                    for (auto&& conflict = conflicts.begin();
401
                            conflict != conflicts.end(); ++conflict)
december0123's avatar
december0123 committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
                    {
                        if ((*conflict)->name_ == (*dependencyConflict))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        continue;
                    }
                    else
                    {
dec's avatar
dec committed
416
                        conflicts.emplace_back(*installedConfig);
december0123's avatar
december0123 committed
417
418
419
420
421
422
423
424
                        break;
                    }
                }
            }
        }
    }

    return conflicts;
Philip Müller's avatar
Philip Müller committed
425
426
}

december0123's avatar
december0123 committed
427
std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
428
{
december0123's avatar
december0123 committed
429
430
    std::vector<std::shared_ptr<Config>> requirements;
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
431
432

    // Get the right configs
433
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
434
435
436
437
438
439
440
441
442
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

    // Check if this config is required by another installed config
443
    for (auto&& installedConfig = installedConfigs.begin();
444
            installedConfig != installedConfigs.end(); ++installedConfig)
december0123's avatar
december0123 committed
445
    {
446
        for (auto&& dependency = (*installedConfig)->dependencies_.begin();
december0123's avatar
december0123 committed
447
448
449
450
451
452
453
454
455
456
                dependency != (*installedConfig)->dependencies_.end(); dependency++)
        {
            if ((*dependency) != config->name_)
            {
                continue;
            }
            else
            {
                // Check if already in vector
                bool found = false;
457
                for (auto&& requirement = requirements.begin();
458
                        requirement != requirements.end(); ++requirement)
december0123's avatar
december0123 committed
459
460
461
462
463
464
465
466
467
468
                {
                    if ((*requirement)->name_ == (*installedConfig)->name_)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
dec's avatar
dec committed
469
                    requirements.emplace_back(*installedConfig);
december0123's avatar
december0123 committed
470
471
472
473
474
475
476
                    break;
                }
            }
        }
    }

    return requirements;
Philip Müller's avatar
Philip Müller committed
477
478
}

479
void Data::fillDevices(std::string type)
Philip Müller's avatar
Philip Müller committed
480
{
december0123's avatar
december0123 committed
481
    hw_item hw;
december0123's avatar
december0123 committed
482
    std::vector<std::shared_ptr<Device>>* devices;
december0123's avatar
december0123 committed
483

484
    if ("USB" == type)
december0123's avatar
december0123 committed
485
486
487
488
489
490
491
492
493
494
495
    {
        hw = hw_usb;
        devices = &USBDevices;
    }
    else
    {
        hw = hw_pci;
        devices = &PCIDevices;
    }

    // Get the hardware devices
dec's avatar
dec committed
496
497
    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
498
499
    hd_t *beginningOfhd = hd;

dec's avatar
dec committed
500
    std::unique_ptr<Device> device;
december0123's avatar
december0123 committed
501
502
    for (; hd; hd = hd->next)
    {
dec's avatar
dec committed
503
        device.reset(new Device());
december0123's avatar
december0123 committed
504
505
506
507
508
509
510
511
512
        device->type_ = type;
        device->classID_ = from_Hex(hd->base_class.id, 2) + from_Hex(hd->sub_class.id, 2).toLower();
        device->vendorID_ = from_Hex(hd->vendor.id, 4).toLower();
        device->deviceID_ = from_Hex(hd->device.id, 4).toLower();
        device->className_ = from_CharArray(hd->base_class.name);
        device->vendorName_ = from_CharArray(hd->vendor.name);
        device->deviceName_ = from_CharArray(hd->device.name);
        device->sysfsBusID_ = from_CharArray(hd->sysfs_bus_id);
        device->sysfsID_ = from_CharArray(hd->sysfs_id);
dec's avatar
dec committed
513
        devices->emplace_back(device.release());
december0123's avatar
december0123 committed
514
515
516
    }

    hd_free_hd_list(beginningOfhd);
dec's avatar
dec committed
517
    hd_free_hd_data(hd_data.get());
Philip Müller's avatar
Philip Müller committed
518
519
}

520
void Data::fillAllConfigs(std::string type)
Philip Müller's avatar
Philip Müller committed
521
{
december0123's avatar
december0123 committed
522
    std::vector<std::string> configPaths;
december0123's avatar
december0123 committed
523
    std::vector<std::shared_ptr<Config>>* configs;
december0123's avatar
december0123 committed
524

525
    if ("USB" == type)
december0123's avatar
december0123 committed
526
527
528
529
530
531
532
533
534
535
    {
        configs = &allUSBConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_USB_CONFIG_DIR, MHWD_CONFIG_NAME);
    }
    else
    {
        configs = &allPCIConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_PCI_CONFIG_DIR, MHWD_CONFIG_NAME);
    }

536
    for (auto&& configPath = configPaths.begin();
december0123's avatar
december0123 committed
537
538
            configPath != configPaths.end(); ++configPath)
    {
dec's avatar
dec committed
539
        std::unique_ptr<Config> config{new Config((*configPath), type)};
december0123's avatar
december0123 committed
540
541
542

        if (config->readConfigFile((*configPath)))
        {
dec's avatar
dec committed
543
            configs->emplace_back(config.release());
december0123's avatar
december0123 committed
544
545
546
        }
        else
        {
dec's avatar
dec committed
547
            invalidConfigs.emplace_back(config.release());
december0123's avatar
december0123 committed
548
549
        }
    }
Philip Müller's avatar
Philip Müller committed
550
551
}

december0123's avatar
december0123 committed
552
bool Data::fillConfig(std::shared_ptr<Config> config, std::string configPath, std::string type)
Philip Müller's avatar
Philip Müller committed
553
{
december0123's avatar
december0123 committed
554
555
556
557
558
559
560
561
562
    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())
    {
dec's avatar
dec committed
563
        config->hwdIDs_.emplace_back();
december0123's avatar
december0123 committed
564
565
    }

566
    return config->readConfigFile(config->configPath_);
Philip Müller's avatar
Philip Müller committed
567
568
}

569
std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& directoryPath,
december0123's avatar
december0123 committed
570
        std::string onlyFilename)
Philip Müller's avatar
Philip Müller committed
571
{
december0123's avatar
december0123 committed
572
    std::vector<std::string> list;
december0123's avatar
december0123 committed
573
    struct dirent *dir = nullptr;
dec's avatar
dec committed
574
    DIR* d = opendir(directoryPath.c_str());
december0123's avatar
december0123 committed
575
576
    if (d)
    {
577
        while (nullptr != (dir = readdir(d)))
december0123's avatar
december0123 committed
578
        {
579
            std::string filename = dir->d_name;
december0123's avatar
december0123 committed
580
581
            std::string filepath = directoryPath + "/" + filename;

582
            if (("." == filename) || (".." == filename) || ("" == filename))
december0123's avatar
december0123 committed
583
584
585
586
587
588
589
590
            {
                continue;
            }
            else
            {
                struct stat filestatus;
                lstat(filepath.c_str(), &filestatus);

december0123's avatar
december0123 committed
591
592
                if (S_ISREG(filestatus.st_mode) &&
                        (onlyFilename.empty() || (onlyFilename == filename)))
december0123's avatar
december0123 committed
593
594
595
596
597
598
599
600
                {
                    list.push_back(filepath);
                }
                else if (S_ISDIR(filestatus.st_mode))
                {
                    std::vector<std::string> templist = getRecursiveDirectoryFileList(filepath,
                            onlyFilename);

601
                    for (auto&& iterator = templist.begin();
december0123's avatar
december0123 committed
602
603
604
605
606
607
608
609
610
611
                            iterator != templist.end(); iterator++)
                    {
                        list.push_back((*iterator));
                    }
                }
            }
        }

        closedir(d);
    }
december0123's avatar
december0123 committed
612
    delete dir;
december0123's avatar
december0123 committed
613
    return list;
Philip Müller's avatar
Philip Müller committed
614
615
616
617
}

Vita::string Data::getRightConfigPath(Vita::string str, Vita::string baseConfigPath)
{
december0123's avatar
december0123 committed
618
    str = str.trim();
619
    if ((str.size() <= 0) || (str.substr(0, 1) == "/"))
december0123's avatar
december0123 committed
620
621
622
623
    {
        return str;
    }
    return baseConfigPath + "/" + str;
Philip Müller's avatar
Philip Müller committed
624
625
626
627
}

std::vector<std::string> Data::splitValue(Vita::string str, Vita::string onlyEnding)
{
december0123's avatar
december0123 committed
628
629
630
    std::vector<Vita::string> work = str.toLower().explode(" ");
    std::vector<std::string> final;

631
    for (auto&& iterator = work.begin(); iterator != work.end();
december0123's avatar
december0123 committed
632
633
            iterator++)
    {
634
        if (("" != *iterator) && onlyEnding.empty())
december0123's avatar
december0123 committed
635
636
637
        {
            final.push_back(*iterator);
        }
638
639
        else if (("" != *iterator) && (Vita::string(*iterator).explode(".").back() == onlyEnding)
                && ((*iterator).size() > 5))
december0123's avatar
december0123 committed
640
641
642
643
644
645
        {
            final.push_back(Vita::string(*iterator).substr(0, (*iterator).size() - 5));
        }
    }

    return final;
Philip Müller's avatar
Philip Müller committed
646
647
648
649
}

void Data::updateConfigData()
{
december0123's avatar
december0123 committed
650
    // Clear config vectors in each device element
651
    for (auto&& PCIDevice = PCIDevices.begin();
december0123's avatar
december0123 committed
652
653
            PCIDevice != PCIDevices.end(); PCIDevice++)
    {
december0123's avatar
december0123 committed
654
        (*PCIDevice)->availableConfigs_.clear();
december0123's avatar
december0123 committed
655
656
    }

657
    for (auto&& USBDevice = USBDevices.begin();
december0123's avatar
december0123 committed
658
659
            USBDevice != USBDevices.end(); USBDevice++)
    {
december0123's avatar
december0123 committed
660
        (*USBDevice)->availableConfigs_.clear();
december0123's avatar
december0123 committed
661
662
663
664
665
666
667
668
    }
    allPCIConfigs.clear();
    allUSBConfigs.clear();

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

december0123's avatar
december0123 committed
669
670
    setMatchingConfigs(PCIDevices, allPCIConfigs, false);
    setMatchingConfigs(USBDevices, allUSBConfigs, false);
december0123's avatar
december0123 committed
671
672
673

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

december0123's avatar
december0123 committed
676
void Data::setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& devices,
677
        std::vector<std::shared_ptr<Config>>& configs, bool setAsInstalled)
Philip Müller's avatar
Philip Müller committed
678
{
679
    for (auto&& config = configs.begin(); config != configs.end();
december0123's avatar
december0123 committed
680
681
682
683
            ++config)
    {
        setMatchingConfig((*config), devices, setAsInstalled);
    }
Philip Müller's avatar
Philip Müller committed
684
685
}

686
687
void Data::setMatchingConfig(std::shared_ptr<Config> config,
		const std::vector<std::shared_ptr<Device>>& devices, bool setAsInstalled)
Philip Müller's avatar
Philip Müller committed
688
{
december0123's avatar
december0123 committed
689
    std::vector<std::shared_ptr<Device>> foundDevices;
december0123's avatar
december0123 committed
690

december0123's avatar
december0123 committed
691
    getAllDevicesOfConfig(devices, config, foundDevices);
december0123's avatar
december0123 committed
692
693

    // Set config to all matching devices
694
    for (auto&& foundDevice = foundDevices.begin();
december0123's avatar
december0123 committed
695
696
697
698
            foundDevice != foundDevices.end(); ++foundDevice)
    {
        if (setAsInstalled)
        {
december0123's avatar
december0123 committed
699
            addConfigSorted((*foundDevice)->installedConfigs_, config);
december0123's avatar
december0123 committed
700
701
702
        }
        else
        {
december0123's avatar
december0123 committed
703
            addConfigSorted((*foundDevice)->availableConfigs_, config);
december0123's avatar
december0123 committed
704
705
        }
    }
Philip Müller's avatar
Philip Müller committed
706
707
}

708
709
void Data::addConfigSorted(std::vector<std::shared_ptr<Config>>& configs,
		std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
710
{
711
    for (auto&& iterator = configs.begin();
december0123's avatar
december0123 committed
712
            iterator != configs.end(); iterator++)
december0123's avatar
december0123 committed
713
714
715
716
717
718
719
    {
        if (config->name_ == (*iterator)->name_)
        {
            return;
        }
    }

720
    for (auto&& iterator = configs.begin(); iterator != configs.end();
december0123's avatar
december0123 committed
721
722
723
724
            iterator++)
    {
        if (config->priority_ > (*iterator)->priority_)
        {
december0123's avatar
december0123 committed
725
            configs.insert(iterator, std::shared_ptr<Config>(config));
december0123's avatar
december0123 committed
726
727
728
729
            return;
        }
    }

dec's avatar
dec committed
730
    configs.emplace_back(config);
Philip Müller's avatar
Philip Müller committed
731
732
}

december0123's avatar
december0123 committed
733
Vita::string Data::from_Hex(std::uint16_t hexnum, int fill)
Philip Müller's avatar
Philip Müller committed
734
{
december0123's avatar
december0123 committed
735
736
737
    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
738
739
740
741
}

Vita::string Data::from_CharArray(char* c)
{
742
    if (nullptr == c)
december0123's avatar
december0123 committed
743
744
745
    {
        return "";
    }
Philip Müller's avatar
Philip Müller committed
746

december0123's avatar
december0123 committed
747
    return Vita::string(c);
Philip Müller's avatar
Philip Müller committed
748
}