| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "hwidtranslator.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cctype> |
| 8 | #include <exception> |
| 9 | #include <spdlog/spdlog.h> |
| 10 | #include <sstream> |
| 11 | #include <utility> |
| 12 | |
| 13 | HWIDTranslator::HWIDTranslator( |
| 14 | std::vector<Vendor> vendors, |
| 15 | std::unique_ptr<IDataSource<std::vector<char>>> &&dataSource) noexcept |
| 16 | { |
| 17 | std::vector<char> data; |
| 18 | try { |
| 19 | if (dataSource->read(data)) |
| 20 | parseHWIDSFileData(data, std::move(vendors)); |
| 21 | } |
| 22 | catch (std::exception const &e) { |
| 23 | SPDLOG_DEBUG(e.what()); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | std::string HWIDTranslator::vendor(std::string const &vendorID) const |
| 28 | { |
| 29 | if (!vendors_.empty()) { |
| 30 | std::string key(vendorID); |
| 31 | std::transform(key.cbegin(), key.cend(), key.begin(), ::tolower); |
| 32 | |
| 33 | auto const vendorIt = vendors_.find(key); |
| 34 | if (vendorIt != vendors_.cend()) |
| 35 | return vendorIt->second; |
| 36 | } |
| 37 | |
| 38 | return std::string{}; |
| 39 | } |
| 40 | |
| 41 | std::string HWIDTranslator::device(std::string const &vendorID, |
| 42 | std::string const &deviceID) const |
| 43 | { |
| 44 | if (!devices_.empty()) { |
| 45 | std::string key; |
| 46 | key.reserve(vendorID.length() + deviceID.length()); |
| 47 | |
| 48 | key.append(vendorID).append(deviceID); |
| 49 | std::transform(key.cbegin(), key.cend(), key.begin(), ::tolower); |
| 50 | |
| 51 | auto const deviceIt = devices_.find(key); |
| 52 | if (deviceIt != devices_.cend()) |
| 53 | return deviceIt->second; |
| 54 | } |
| 55 | |
| 56 | return std::string{}; |
| 57 | } |
| 58 | |
| 59 | std::string HWIDTranslator::subdevice(std::string const &vendorID, |
| 60 | std::string const &deviceID, |
| 61 | std::string const &subvendorID, |
| 62 | std::string const &subdeviceID) const |
| 63 | { |
| 64 | if (!subdevices_.empty()) { |
| 65 | std::string key; |
| 66 | key.reserve(vendorID.length() + deviceID.length() + subvendorID.length() + |
| 67 | subdeviceID.length()); |
| 68 | key.append(vendorID).append(deviceID).append(subvendorID).append(subdeviceID); |
| 69 | std::transform(key.cbegin(), key.cend(), key.begin(), ::tolower); |
| 70 | |
| 71 | auto const subdeviceIt = subdevices_.find(key); |
| 72 | if (subdeviceIt != subdevices_.cend()) |
| 73 | return subdeviceIt->second; |
| 74 | } |
| 75 | |
| 76 | return std::string{}; |
| 77 | } |
| 78 | |
| 79 | std::string HWIDTranslator::extractName(std::string const &line, |
| 80 | size_t start) const |
| 81 | { |
| 82 | auto nameStartPos = line.find_first_not_of(' ', start); |
| 83 | if (nameStartPos != std::string::npos) { |
| 84 | auto bracketPos = line.find(" [", nameStartPos); |
| 85 | auto parenPos = line.find(" (", nameStartPos); |
| 86 | auto nameEndPos = std::min({bracketPos, parenPos, std::string::npos}); |
| 87 | return line.substr(nameStartPos, nameEndPos - nameStartPos); |
| 88 | } |
| 89 | |
| 90 | return std::string{}; |
| 91 | } |
| 92 | |
| 93 | void HWIDTranslator::parseHWIDSFileData(std::vector<char> &data, |
| 94 | std::vector<Vendor> &&vendors) |
| 95 | { |
| 96 | std::istringstream dataStream; |
| 97 | dataStream.rdbuf()->pubsetbuf(data.data(), static_cast<long>(data.size())); |
| 98 | |
| 99 | std::string line; |
| 100 | line.reserve(200); |
| 101 | std::string vendorID; |
| 102 | vendorID.reserve(4); |
| 103 | std::string deviceID; |
| 104 | deviceID.reserve(4); |
| 105 | std::string subvendorID; |
| 106 | subvendorID.reserve(4); |
| 107 | std::string subdeviceID; |
| 108 | subdeviceID.reserve(4); |
| 109 | |
| 110 | std::string devicesKey; |
| 111 | devicesKey.reserve(8); |
| 112 | std::string subdevicesKey; |
| 113 | subdevicesKey.reserve(16); |
| 114 | |
| 115 | std::vector<char> skipLineChars{'#', '\0', 'C'}; |
| 116 | bool skipVendor = true; |
| 117 | size_t parsedVendorCount = 0; |
| 118 | while (parsedVendorCount < vendors.size() && std::getline(dataStream, line)) { |
| 119 | auto const firstChar = line[0]; |
| 120 | |
| 121 | // if the line starts with one of skipLineChars, ignore the line |
| 122 | if (std::find(skipLineChars.cbegin(), skipLineChars.cend(), firstChar) != |
| 123 | skipLineChars.cend()) { |
| 124 | if (firstChar == 'C') |
| 125 | skipVendor = true; |
| 126 | } |
| 127 | // vendor |
| 128 | else if (firstChar != '\t') { |
| 129 | if (!skipVendor) // vendor parsed! |
| 130 | ++parsedVendorCount; |
| 131 | |
| 132 | auto spacePos = line.find(' '); |
| 133 | vendorID = line.substr(0, spacePos); |
| 134 | int vendorIDNum = -1; |
| 135 | |
| 136 | try { |
| 137 | vendorIDNum = std::stoi(vendorID, nullptr, 16); |
| 138 | |
| 139 | // only parse vendors from vendorIDs |
| 140 | if (std::find(vendors.cbegin(), vendors.cend(), |
| 141 | static_cast<Vendor>(vendorIDNum)) == vendors.cend()) { |
| 142 | skipVendor = true; |
| 143 | } |
| 144 | else { |
| 145 | vendors_.insert({vendorID, extractName(line, spacePos)}); |
| 146 | skipVendor = false; |
| 147 | } |
| 148 | } |
| 149 | catch (std::exception const &e) { |
| 150 | SPDLOG_DEBUG("Cannot convert vendor id {} from hwdata file.\nError: {}", |
| 151 | vendorID, e.what()); |
| 152 | skipVendor = true; |
| 153 | } |
| 154 | } |
| 155 | // device or subvendor subdevice |
| 156 | else { |
| 157 | if (!skipVendor) { |
| 158 | auto secondChar = line[1]; |
| 159 | |
| 160 | // device |
| 161 | if (secondChar != '\t') { |
| 162 | auto spacePos = line.find(' ', 1); |
| 163 | deviceID = line.substr(1, spacePos - 1); |
| 164 | |
| 165 | devicesKey.clear(); |
| 166 | devicesKey.append(vendorID).append(deviceID); |
| 167 | |
| 168 | devices_.insert({devicesKey, extractName(line, spacePos)}); |
| 169 | } |
| 170 | // subvendor subdevice |
| 171 | else if (line.length() > 2) { |
| 172 | auto subvendorSpacePos = line.find(' ', 2); |
| 173 | subvendorID = line.substr(2, subvendorSpacePos - 2); |
| 174 | |
| 175 | auto subdeviceStartPos = line.find_first_not_of(' ', subvendorSpacePos); |
| 176 | auto subdeviceEndPos = line.find(' ', subdeviceStartPos); |
| 177 | subdeviceID = line.substr(subdeviceStartPos, |
| 178 | subdeviceEndPos - subdeviceStartPos); |
| 179 | |
| 180 | subdevicesKey.clear(); |
| 181 | subdevicesKey.append(vendorID) |
| 182 | .append(deviceID) |
| 183 | .append(subvendorID) |
| 184 | .append(subdeviceID); |
| 185 | |
| 186 | subdevices_.insert({subdevicesKey, extractName(line, subdeviceEndPos)}); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |