| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2022 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuinfouniqueid.h" |
| 5 | |
| 6 | #include "common/fileutils.h" |
| 7 | #include "core/info/infoproviderregistry.h" |
| 8 | #include <algorithm> |
| 9 | #include <cctype> |
| 10 | #include <utility> |
| 11 | |
| 12 | namespace AMD { |
| 13 | class GPUInfoUniqueIDDataSource |
| 14 | : public IDataSource<std::string, std::filesystem::path const> |
| 15 | { |
| 16 | public: |
| 17 | std::string source() const override |
| 18 | { |
| 19 | return "unique_id"; |
| 20 | } |
| 21 | |
| 22 | bool read(std::string &data, std::filesystem::path const &path) override |
| 23 | { |
| 24 | auto const filePath = path / source(); |
| 25 | if (Utils::File::isFilePathValid(filePath)) { |
| 26 | auto const lines = Utils::File::readFileLines(filePath); |
| 27 | if (!lines.empty()) { |
| 28 | data = lines.front(); |
| 29 | return true; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return false; |
| 34 | } |
| 35 | }; |
| 36 | } // namespace AMD |
| 37 | |
| 38 | AMD::GPUInfoUniqueID::GPUInfoUniqueID( |
| 39 | std::unique_ptr<IDataSource<std::string, std::filesystem::path const>> |
| 40 | &&dataSource) noexcept |
| 41 | : dataSource_(std::move(dataSource)) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | std::vector<std::pair<std::string, std::string>> |
| 46 | AMD::GPUInfoUniqueID::provideInfo(Vendor vendor, int, IGPUInfo::Path const &path, |
| 47 | IHWIDTranslator const &) const |
| 48 | { |
| 49 | std::vector<std::pair<std::string, std::string>> info; |
| 50 | |
| 51 | if (vendor == Vendor::AMD) { |
| 52 | std::string data; |
| 53 | if (dataSource_->read(data, path.sys)) { |
| 54 | std::transform(data.cbegin(), data.cend(), data.begin(), ::toupper); |
| 55 | info.emplace_back(IGPUInfo::Keys::uniqueID, std::move(data)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return info; |
| 60 | } |
| 61 | |
| 62 | std::vector<std::string> |
| 63 | AMD::GPUInfoUniqueID::provideCapabilities(Vendor, int, |
| 64 | IGPUInfo::Path const &) const |
| 65 | { |
| 66 | return {}; |
| 67 | } |
| 68 | |
| 69 | bool AMD::GPUInfoUniqueID::registered_ = |
| 70 | InfoProviderRegistry::add(std::make_unique<AMD::GPUInfoUniqueID>( |
| 71 | std::make_unique<GPUInfoUniqueIDDataSource>())); |