Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Alexander Hansen | 5df916f | 2025-09-26 10:31:36 -0400 | [diff] [blame] | 3 | #include "../utils.hpp" |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 4 | #include "../variant_visitors.hpp" |
| 5 | #include "expression.hpp" |
Alexander Hansen | bd85baa | 2025-08-06 10:46:57 +0200 | [diff] [blame] | 6 | #include "phosphor-logging/lg2.hpp" |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 7 | |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 9 | #include <sdbusplus/bus/match.hpp> |
| 10 | |
| 11 | #include <fstream> |
Christopher Meis | 811160e | 2025-08-08 08:48:37 +0200 | [diff] [blame] | 12 | #include <regex> |
| 13 | |
| 14 | const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]"); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 15 | |
Alexander Hansen | 0ab32b3 | 2025-06-27 14:50:33 +0200 | [diff] [blame] | 16 | namespace em_utils |
| 17 | { |
| 18 | |
| 19 | constexpr const char* templateChar = "$"; |
| 20 | |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 21 | bool fwVersionIsSame() |
| 22 | { |
| 23 | std::ifstream version(versionFile); |
| 24 | if (!version.good()) |
| 25 | { |
Alexander Hansen | bd85baa | 2025-08-06 10:46:57 +0200 | [diff] [blame] | 26 | lg2::error("Can't read {PATH}", "PATH", versionFile); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 27 | return false; |
| 28 | } |
| 29 | |
| 30 | std::string versionData; |
| 31 | std::string line; |
| 32 | while (std::getline(version, line)) |
| 33 | { |
| 34 | versionData += line; |
| 35 | } |
| 36 | |
| 37 | std::string expectedHash = |
| 38 | std::to_string(std::hash<std::string>{}(versionData)); |
| 39 | |
Alexander Hansen | bd85baa | 2025-08-06 10:46:57 +0200 | [diff] [blame] | 40 | std::error_code ec; |
| 41 | std::filesystem::create_directory(configurationOutDir, ec); |
| 42 | |
| 43 | if (ec) |
| 44 | { |
| 45 | lg2::error("could not create directory {DIR}", "DIR", |
| 46 | configurationOutDir); |
| 47 | return false; |
| 48 | } |
| 49 | |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 50 | std::ifstream hashFile(versionHashFile); |
| 51 | if (hashFile.good()) |
| 52 | { |
| 53 | std::string hashString; |
| 54 | hashFile >> hashString; |
| 55 | |
| 56 | if (expectedHash == hashString) |
| 57 | { |
| 58 | return true; |
| 59 | } |
| 60 | hashFile.close(); |
| 61 | } |
| 62 | |
| 63 | std::ofstream output(versionHashFile); |
| 64 | output << expectedHash; |
| 65 | return false; |
| 66 | } |
| 67 | |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 68 | void handleLeftOverTemplateVars(nlohmann::json::iterator& keyPair) |
| 69 | { |
| 70 | if (keyPair.value().type() == nlohmann::json::value_t::object || |
| 71 | keyPair.value().type() == nlohmann::json::value_t::array) |
| 72 | { |
| 73 | for (auto nextLayer = keyPair.value().begin(); |
| 74 | nextLayer != keyPair.value().end(); nextLayer++) |
| 75 | { |
| 76 | handleLeftOverTemplateVars(nextLayer); |
| 77 | } |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | std::string* strPtr = keyPair.value().get_ptr<std::string*>(); |
| 82 | if (strPtr == nullptr) |
| 83 | { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Walking through the string to find $<templateVar> |
| 88 | while (true) |
| 89 | { |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 90 | std::ranges::subrange<std::string::const_iterator> findStart = |
| 91 | iFindFirst(*strPtr, std::string_view(templateChar)); |
| 92 | |
| 93 | if (!findStart) |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 94 | { |
| 95 | break; |
| 96 | } |
| 97 | |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 98 | std::ranges::subrange<std::string::iterator> searchRange( |
| 99 | strPtr->begin() + (findStart.end() - strPtr->begin()), |
| 100 | strPtr->end()); |
| 101 | std::ranges::subrange<std::string::const_iterator> findSpace = |
| 102 | iFindFirst(searchRange, " "); |
| 103 | |
| 104 | std::string::const_iterator templateVarEnd; |
| 105 | |
| 106 | if (!findSpace) |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 107 | { |
| 108 | // No space means the template var spans to the end of |
| 109 | // of the keyPair value |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 110 | templateVarEnd = strPtr->end(); |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
| 114 | // A space marks the end of a template var |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 115 | templateVarEnd = findSpace.begin(); |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | lg2::error( |
| 119 | "There's still template variable {VAR} un-replaced. Removing it from the string.\n", |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 120 | "VAR", std::string(findStart.begin(), templateVarEnd)); |
| 121 | strPtr->erase(findStart.begin(), templateVarEnd); |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 125 | // Replaces the template character like the other version of this function, |
| 126 | // but checks all properties on all interfaces provided to do the substitution |
| 127 | // with. |
| 128 | std::optional<std::string> templateCharReplace( |
| 129 | nlohmann::json::iterator& keyPair, const DBusObject& object, |
| 130 | const size_t index, const std::optional<std::string>& replaceStr) |
| 131 | { |
| 132 | for (const auto& [_, interface] : object) |
| 133 | { |
| 134 | auto ret = templateCharReplace(keyPair, interface, index, replaceStr); |
| 135 | if (ret) |
| 136 | { |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 137 | handleLeftOverTemplateVars(keyPair); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | } |
Chau Ly | 7962944 | 2025-08-20 10:27:20 +0000 | [diff] [blame] | 141 | handleLeftOverTemplateVars(keyPair); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 142 | return std::nullopt; |
| 143 | } |
| 144 | |
| 145 | // finds the template character (currently set to $) and replaces the value with |
| 146 | // the field found in a dbus object i.e. $ADDRESS would get populated with the |
| 147 | // ADDRESS field from a object on dbus |
| 148 | std::optional<std::string> templateCharReplace( |
| 149 | nlohmann::json::iterator& keyPair, const DBusInterface& interface, |
| 150 | const size_t index, const std::optional<std::string>& replaceStr) |
| 151 | { |
| 152 | std::optional<std::string> ret = std::nullopt; |
| 153 | |
| 154 | if (keyPair.value().type() == nlohmann::json::value_t::object || |
| 155 | keyPair.value().type() == nlohmann::json::value_t::array) |
| 156 | { |
| 157 | for (auto nextLayer = keyPair.value().begin(); |
| 158 | nextLayer != keyPair.value().end(); nextLayer++) |
| 159 | { |
| 160 | templateCharReplace(nextLayer, interface, index, replaceStr); |
| 161 | } |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | std::string* strPtr = keyPair.value().get_ptr<std::string*>(); |
| 166 | if (strPtr == nullptr) |
| 167 | { |
| 168 | return ret; |
| 169 | } |
| 170 | |
George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame] | 171 | replaceAll(*strPtr, std::string(templateChar) + "index", |
| 172 | std::to_string(index)); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 173 | if (replaceStr) |
| 174 | { |
George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame] | 175 | replaceAll(*strPtr, *replaceStr, std::to_string(index)); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | for (const auto& [propName, propValue] : interface) |
| 179 | { |
| 180 | std::string templateName = templateChar + propName; |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 181 | std::ranges::subrange<std::string::const_iterator> find = |
| 182 | iFindFirst(*strPtr, templateName); |
| 183 | if (!find) |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 184 | { |
| 185 | continue; |
| 186 | } |
| 187 | |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 188 | size_t start = find.begin() - strPtr->begin(); |
| 189 | |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 190 | // check for additional operations |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 191 | if ((start == 0U) && find.end() == strPtr->end()) |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 192 | { |
| 193 | std::visit([&](auto&& val) { keyPair.value() = val; }, propValue); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | constexpr const std::array<char, 5> mathChars = {'+', '-', '%', '*', |
| 198 | '/'}; |
| 199 | size_t nextItemIdx = start + templateName.size() + 1; |
| 200 | |
| 201 | if (nextItemIdx > strPtr->size() || |
| 202 | std::find(mathChars.begin(), mathChars.end(), |
| 203 | strPtr->at(nextItemIdx)) == mathChars.end()) |
| 204 | { |
| 205 | std::string val = std::visit(VariantToStringVisitor(), propValue); |
George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame] | 206 | iReplaceAll(*strPtr, templateName, val); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 207 | continue; |
| 208 | } |
| 209 | |
| 210 | // save the prefix |
| 211 | std::string prefix = strPtr->substr(0, start); |
| 212 | |
| 213 | // operate on the rest |
| 214 | std::string end = strPtr->substr(nextItemIdx); |
| 215 | |
George Liu | ecf1a31 | 2025-08-25 10:43:12 +0800 | [diff] [blame] | 216 | std::vector<std::string> splitResult = split(end, ' '); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 217 | |
| 218 | // need at least 1 operation and number |
George Liu | ecf1a31 | 2025-08-25 10:43:12 +0800 | [diff] [blame] | 219 | if (splitResult.size() < 2) |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 220 | { |
Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 221 | lg2::error("Syntax error on template replacement of {STR}", "STR", |
| 222 | *strPtr); |
George Liu | ecf1a31 | 2025-08-25 10:43:12 +0800 | [diff] [blame] | 223 | for (const std::string& data : splitResult) |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 224 | { |
Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 225 | lg2::error("{SPLIT} ", "SPLIT", data); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 226 | } |
Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 227 | lg2::error(""); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 228 | continue; |
| 229 | } |
| 230 | |
| 231 | // we assume that the replacement is a number, because we can |
| 232 | // only do math on numbers.. we might concatenate strings in the |
| 233 | // future, but thats later |
| 234 | int number = std::visit(VariantToIntVisitor(), propValue); |
George Liu | ecf1a31 | 2025-08-25 10:43:12 +0800 | [diff] [blame] | 235 | auto exprBegin = splitResult.begin(); |
| 236 | auto exprEnd = splitResult.end(); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 237 | |
| 238 | number = expression::evaluate(number, exprBegin, exprEnd); |
| 239 | |
Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 240 | std::string replaced(find.begin(), find.end()); |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 241 | while (exprBegin != exprEnd) |
| 242 | { |
| 243 | replaced.append(" ").append(*exprBegin++); |
| 244 | } |
| 245 | ret = replaced; |
| 246 | |
| 247 | std::string result = prefix + std::to_string(number); |
George Liu | ecf1a31 | 2025-08-25 10:43:12 +0800 | [diff] [blame] | 248 | while (exprEnd != splitResult.end()) |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 249 | { |
| 250 | result.append(" ").append(*exprEnd++); |
| 251 | } |
| 252 | keyPair.value() = result; |
| 253 | |
| 254 | // We probably just invalidated the pointer abovei, |
| 255 | // reset and continue to handle multiple templates |
| 256 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 257 | if (strPtr == nullptr) |
| 258 | { |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 264 | if (strPtr == nullptr) |
| 265 | { |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | std::string_view strView = *strPtr; |
| 270 | int base = 10; |
George Liu | 164af2f | 2025-08-21 17:16:31 +0800 | [diff] [blame] | 271 | if (strView.starts_with("0x")) |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 272 | { |
| 273 | strView.remove_prefix(2); |
| 274 | base = 16; |
| 275 | } |
| 276 | |
| 277 | uint64_t temp = 0; |
Alexander Hansen | 5df916f | 2025-09-26 10:31:36 -0400 | [diff] [blame] | 278 | bool fullMatch = false; |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 279 | const std::from_chars_result res = |
Alexander Hansen | 5df916f | 2025-09-26 10:31:36 -0400 | [diff] [blame] | 280 | fromCharsWrapper(strView, temp, fullMatch, base); |
| 281 | if (res.ec == std::errc{} && fullMatch) |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 282 | { |
| 283 | keyPair.value() = temp; |
| 284 | } |
| 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | |
Christopher Meis | 811160e | 2025-08-08 08:48:37 +0200 | [diff] [blame] | 289 | std::string buildInventorySystemPath(std::string& boardName, |
| 290 | const std::string& boardType) |
| 291 | { |
| 292 | std::string path = "/xyz/openbmc_project/inventory/system/"; |
George Liu | fd977ec | 2025-08-25 11:36:44 +0800 | [diff] [blame] | 293 | std::string boardTypeLower = toLowerCopy(boardType); |
Christopher Meis | 811160e | 2025-08-08 08:48:37 +0200 | [diff] [blame] | 294 | std::regex_replace(boardName.begin(), boardName.begin(), boardName.end(), |
| 295 | illegalDbusMemberRegex, "_"); |
| 296 | |
| 297 | return std::format("{}{}/{}", path, boardTypeLower, boardName); |
| 298 | } |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 299 | } // namespace em_utils |