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