James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
Brad Bishop | 1fb9f3f | 2020-08-28 08:15:13 -0400 | [diff] [blame] | 16 | /// \file Utils.cpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 18 | #include "Utils.hpp" |
| 19 | |
| 20 | #include "VariantVisitors.hpp" |
| 21 | |
| 22 | #include <boost/algorithm/string/classification.hpp> |
| 23 | #include <boost/algorithm/string/find.hpp> |
| 24 | #include <boost/algorithm/string/predicate.hpp> |
| 25 | #include <boost/algorithm/string/replace.hpp> |
| 26 | #include <boost/algorithm/string/split.hpp> |
| 27 | #include <boost/container/flat_map.hpp> |
| 28 | #include <boost/lexical_cast.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 29 | #include <sdbusplus/bus/match.hpp> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 30 | #include <valijson/adapters/nlohmann_json_adapter.hpp> |
| 31 | #include <valijson/schema.hpp> |
| 32 | #include <valijson/schema_parser.hpp> |
| 33 | #include <valijson/validator.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 34 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 35 | #include <filesystem> |
| 36 | #include <fstream> |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 37 | #include <map> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 38 | #include <regex> |
| 39 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 40 | constexpr const char* templateChar = "$"; |
| 41 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 42 | namespace fs = std::filesystem; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 43 | static bool powerStatusOn = false; |
| 44 | static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 45 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 46 | bool findFiles(const fs::path& dirPath, const std::string& matchString, |
| 47 | std::vector<fs::path>& foundPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 48 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 49 | if (!fs::exists(dirPath)) |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 50 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 51 | return false; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 52 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 53 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 54 | std::regex search(matchString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 55 | std::smatch match; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 56 | for (const auto& p : fs::directory_iterator(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 57 | { |
| 58 | std::string path = p.path().string(); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 59 | if (std::regex_search(path, match, search)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 60 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 61 | foundPaths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 67 | bool findFiles(const std::vector<fs::path>&& dirPaths, |
| 68 | const std::string& matchString, |
| 69 | std::vector<fs::path>& foundPaths) |
| 70 | { |
| 71 | std::map<fs::path, fs::path> paths; |
| 72 | std::regex search(matchString); |
| 73 | std::smatch match; |
| 74 | for (const auto& dirPath : dirPaths) |
| 75 | { |
| 76 | if (!fs::exists(dirPath)) |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 77 | { |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 78 | continue; |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 79 | } |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 80 | |
| 81 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 82 | { |
| 83 | std::string path = p.path().string(); |
| 84 | if (std::regex_search(path, match, search)) |
| 85 | { |
| 86 | paths[p.path().filename()] = p.path(); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | for (const auto& [key, value] : paths) |
| 92 | { |
| 93 | foundPaths.emplace_back(value); |
| 94 | } |
| 95 | |
| 96 | return !foundPaths.empty(); |
| 97 | } |
| 98 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 99 | bool getI2cDevicePaths(const fs::path& dirPath, |
| 100 | boost::container::flat_map<size_t, fs::path>& busPaths) |
| 101 | { |
| 102 | if (!fs::exists(dirPath)) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // Regex for matching the path |
| 108 | std::regex searchPath(std::string(R"(i2c-\d+$)")); |
| 109 | // Regex for matching the bus numbers |
| 110 | std::regex searchBus(std::string(R"(\w[^-]*$)")); |
| 111 | std::smatch matchPath; |
| 112 | std::smatch matchBus; |
| 113 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 114 | { |
| 115 | std::string path = p.path().string(); |
| 116 | if (std::regex_search(path, matchPath, searchPath)) |
| 117 | { |
| 118 | if (std::regex_search(path, matchBus, searchBus)) |
| 119 | { |
| 120 | size_t bus = stoul(*matchBus.begin()); |
| 121 | busPaths.insert(std::pair<size_t, fs::path>(bus, p.path())); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 129 | bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input) |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 130 | { |
| 131 | valijson::Schema schema; |
| 132 | valijson::SchemaParser parser; |
| 133 | valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile); |
| 134 | parser.populateSchema(schemaAdapter, schema); |
| 135 | valijson::Validator validator; |
| 136 | valijson::adapters::NlohmannJsonAdapter targetAdapter(input); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 137 | if (!validator.validate(schema, targetAdapter, nullptr)) |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 138 | { |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 142 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 143 | |
| 144 | bool isPowerOn(void) |
| 145 | { |
| 146 | if (!powerMatch) |
| 147 | { |
| 148 | throw std::runtime_error("Power Match Not Created"); |
| 149 | } |
| 150 | return powerStatusOn; |
| 151 | } |
| 152 | |
| 153 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 154 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 155 | powerMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 156 | static_cast<sdbusplus::bus::bus&>(*conn), |
James Feist | 61f5ac4 | 2020-03-11 14:37:25 -0700 | [diff] [blame] | 157 | "type='signal',interface='" + std::string(properties::interface) + |
| 158 | "',path='" + std::string(power::path) + "',arg0='" + |
| 159 | std::string(power::interface) + "'", |
| 160 | [](sdbusplus::message::message& message) { |
| 161 | std::string objectName; |
| 162 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 163 | values; |
| 164 | message.read(objectName, values); |
| 165 | auto findState = values.find(power::property); |
| 166 | if (findState != values.end()) |
| 167 | { |
| 168 | powerStatusOn = boost::ends_with( |
| 169 | std::get<std::string>(findState->second), "Running"); |
| 170 | } |
| 171 | }); |
| 172 | |
| 173 | conn->async_method_call( |
| 174 | [](boost::system::error_code ec, |
| 175 | const std::variant<std::string>& state) { |
| 176 | if (ec) |
| 177 | { |
| 178 | return; |
| 179 | } |
| 180 | powerStatusOn = |
| 181 | boost::ends_with(std::get<std::string>(state), "Running"); |
| 182 | }, |
| 183 | power::busname, power::path, properties::interface, properties::get, |
| 184 | power::interface, power::property); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 187 | // Replaces the template character like the other version of this function, |
| 188 | // but checks all properties on all interfaces provided to do the substitution |
| 189 | // with. |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 190 | std::optional<std::string> |
| 191 | templateCharReplace(nlohmann::json::iterator& keyPair, |
| 192 | const DBusObject& object, const size_t index, |
| 193 | const std::optional<std::string>& replaceStr) |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 194 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 195 | for (const auto& [_, interface] : object) |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 196 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 197 | auto ret = templateCharReplace(keyPair, interface, index, replaceStr); |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 198 | if (ret) |
| 199 | { |
| 200 | return ret; |
| 201 | } |
| 202 | } |
| 203 | return std::nullopt; |
| 204 | } |
| 205 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 206 | // finds the template character (currently set to $) and replaces the value with |
| 207 | // the field found in a dbus object i.e. $ADDRESS would get populated with the |
| 208 | // ADDRESS field from a object on dbus |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 209 | std::optional<std::string> |
| 210 | templateCharReplace(nlohmann::json::iterator& keyPair, |
| 211 | const DBusInterface& interface, const size_t index, |
| 212 | const std::optional<std::string>& replaceStr) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 213 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 214 | std::optional<std::string> ret = std::nullopt; |
| 215 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 216 | if (keyPair.value().type() == nlohmann::json::value_t::object || |
| 217 | keyPair.value().type() == nlohmann::json::value_t::array) |
| 218 | { |
| 219 | for (auto nextLayer = keyPair.value().begin(); |
| 220 | nextLayer != keyPair.value().end(); nextLayer++) |
| 221 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 222 | templateCharReplace(nextLayer, interface, index, replaceStr); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 223 | } |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 224 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | std::string* strPtr = keyPair.value().get_ptr<std::string*>(); |
| 228 | if (strPtr == nullptr) |
| 229 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 230 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | boost::replace_all(*strPtr, std::string(templateChar) + "index", |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 234 | std::to_string(index)); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 235 | if (replaceStr) |
| 236 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 237 | boost::replace_all(*strPtr, *replaceStr, std::to_string(index)); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 238 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 239 | |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 240 | for (auto& propertyPair : interface) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 241 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 242 | std::string templateName = templateChar + propertyPair.first; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 243 | boost::iterator_range<std::string::const_iterator> find = |
| 244 | boost::ifind_first(*strPtr, templateName); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 245 | if (!find) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 246 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 247 | continue; |
| 248 | } |
James Feist | 8c20feb | 2019-08-14 15:10:11 -0700 | [diff] [blame] | 249 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 250 | constexpr const std::array<char, 5> mathChars = {'+', '-', '%', '*', |
| 251 | '/'}; |
| 252 | size_t start = find.begin() - strPtr->begin(); |
| 253 | size_t nextItemIdx = start + templateName.size() + 1; |
| 254 | |
| 255 | // check for additional operations |
| 256 | if (!start && find.end() == strPtr->end()) |
| 257 | { |
| 258 | std::visit([&](auto&& val) { keyPair.value() = val; }, |
| 259 | propertyPair.second); |
| 260 | return ret; |
| 261 | } |
| 262 | if (nextItemIdx > strPtr->size() || |
| 263 | std::find(mathChars.begin(), mathChars.end(), |
| 264 | strPtr->at(nextItemIdx)) == mathChars.end()) |
| 265 | { |
| 266 | std::string val = |
| 267 | std::visit(VariantToStringVisitor(), propertyPair.second); |
| 268 | boost::ireplace_all(*strPtr, templateChar + propertyPair.first, |
| 269 | val); |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | // save the prefix |
| 274 | std::string prefix = strPtr->substr(0, start); |
| 275 | |
| 276 | // operate on the rest |
| 277 | std::string end = strPtr->substr(nextItemIdx); |
| 278 | |
| 279 | std::vector<std::string> split; |
| 280 | boost::split(split, end, boost::is_any_of(" ")); |
| 281 | |
| 282 | // need at least 1 operation and number |
| 283 | if (split.size() < 2) |
| 284 | { |
| 285 | std::cerr << "Syntax error on template replacement of " << *strPtr |
| 286 | << "\n"; |
| 287 | for (const std::string& data : split) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 288 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 289 | std::cerr << data << " "; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 290 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 291 | std::cerr << "\n"; |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | // we assume that the replacement is a number, because we can |
| 296 | // only do math on numbers.. we might concatenate strings in the |
| 297 | // future, but thats later |
| 298 | int number = std::visit(VariantToIntVisitor(), propertyPair.second); |
| 299 | |
| 300 | bool isOperator = true; |
| 301 | TemplateOperation next = TemplateOperation::addition; |
| 302 | |
| 303 | auto it = split.begin(); |
| 304 | |
| 305 | for (; it != split.end(); it++) |
| 306 | { |
| 307 | if (isOperator) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 308 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 309 | if (*it == "+") |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 310 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 311 | next = TemplateOperation::addition; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 312 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 313 | else if (*it == "-") |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 314 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 315 | next = TemplateOperation::subtraction; |
| 316 | } |
| 317 | else if (*it == "*") |
| 318 | { |
| 319 | next = TemplateOperation::multiplication; |
| 320 | } |
| 321 | else if (*it == R"(%)") |
| 322 | { |
| 323 | next = TemplateOperation::modulo; |
| 324 | } |
| 325 | else if (*it == R"(/)") |
| 326 | { |
| 327 | next = TemplateOperation::division; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 328 | } |
| 329 | else |
| 330 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 334 | else |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 335 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 336 | int constant = 0; |
| 337 | try |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 338 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 339 | constant = std::stoi(*it); |
| 340 | } |
| 341 | catch (const std::invalid_argument&) |
| 342 | { |
| 343 | std::cerr << "Parameter not supported for templates " << *it |
| 344 | << "\n"; |
| 345 | continue; |
| 346 | } |
| 347 | switch (next) |
| 348 | { |
| 349 | case TemplateOperation::addition: |
| 350 | { |
| 351 | number += constant; |
| 352 | break; |
| 353 | } |
| 354 | case TemplateOperation::subtraction: |
| 355 | { |
| 356 | number -= constant; |
| 357 | break; |
| 358 | } |
| 359 | case TemplateOperation::multiplication: |
| 360 | { |
| 361 | number *= constant; |
| 362 | break; |
| 363 | } |
| 364 | case TemplateOperation::division: |
| 365 | { |
| 366 | number /= constant; |
| 367 | break; |
| 368 | } |
| 369 | case TemplateOperation::modulo: |
| 370 | { |
| 371 | number = number % constant; |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | default: |
| 376 | break; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 379 | isOperator = !isOperator; |
| 380 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 381 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 382 | std::string result = prefix + std::to_string(number); |
| 383 | |
| 384 | std::string replaced(find.begin(), find.end()); |
| 385 | for (auto it2 = split.begin(); it2 != split.end(); it2++) |
| 386 | { |
| 387 | replaced += " "; |
| 388 | replaced += *it2; |
| 389 | if (it2 == it) |
Zhikui Ren | a0d1b3f | 2021-10-05 16:21:56 -0700 | [diff] [blame] | 390 | { |
| 391 | break; |
| 392 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 393 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame^] | 394 | ret = replaced; |
| 395 | |
| 396 | if (it != split.end()) |
| 397 | { |
| 398 | for (; it != split.end(); it++) |
| 399 | { |
| 400 | result += " " + *it; |
| 401 | } |
| 402 | } |
| 403 | keyPair.value() = result; |
| 404 | |
| 405 | // We probably just invalidated the pointer abovei, |
| 406 | // reset and continue to handle multiple templates |
| 407 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 408 | if (strPtr == nullptr) |
| 409 | { |
| 410 | break; |
| 411 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 415 | if (strPtr == nullptr) |
| 416 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 417 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // convert hex numbers to ints |
| 421 | if (boost::starts_with(*strPtr, "0x")) |
| 422 | { |
| 423 | try |
| 424 | { |
| 425 | size_t pos = 0; |
| 426 | int64_t temp = std::stoul(*strPtr, &pos, 0); |
| 427 | if (pos == strPtr->size()) |
| 428 | { |
| 429 | keyPair.value() = static_cast<uint64_t>(temp); |
| 430 | } |
| 431 | } |
Patrick Williams | 83b1e9b | 2021-10-06 12:47:24 -0500 | [diff] [blame] | 432 | catch (const std::invalid_argument&) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 433 | {} |
Patrick Williams | 83b1e9b | 2021-10-06 12:47:24 -0500 | [diff] [blame] | 434 | catch (const std::out_of_range&) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 435 | {} |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 436 | } |
| 437 | // non-hex numbers |
| 438 | else |
| 439 | { |
| 440 | try |
| 441 | { |
| 442 | uint64_t temp = boost::lexical_cast<uint64_t>(*strPtr); |
| 443 | keyPair.value() = temp; |
| 444 | } |
Patrick Williams | 83b1e9b | 2021-10-06 12:47:24 -0500 | [diff] [blame] | 445 | catch (const boost::bad_lexical_cast&) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 446 | {} |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 447 | } |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 448 | return ret; |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 449 | } |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 450 | |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 451 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 452 | /// |
| 453 | /// Default match JSON/DBus match implementation |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 454 | /// \tparam T The concrete DBus value type from DBusValueVariant |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 455 | template <typename T> |
| 456 | struct MatchProbe |
| 457 | { |
| 458 | /// \param probe the probe statement to match against |
| 459 | /// \param value the property value being matched to a probe |
| 460 | /// \return true if the dbusValue matched the probe otherwise false |
| 461 | static bool match(const nlohmann::json& probe, const T& value) |
| 462 | { |
| 463 | return probe == value; |
| 464 | } |
| 465 | }; |
| 466 | |
| 467 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 468 | /// |
| 469 | /// std::string specialization of MatchProbe enabling regex matching |
| 470 | template <> |
| 471 | struct MatchProbe<std::string> |
| 472 | { |
| 473 | /// \param probe the probe statement to match against |
| 474 | /// \param value the string value being matched to a probe |
| 475 | /// \return true if the dbusValue matched the probe otherwise false |
| 476 | static bool match(const nlohmann::json& probe, const std::string& value) |
| 477 | { |
| 478 | if (probe.is_string()) |
| 479 | { |
| 480 | try |
| 481 | { |
| 482 | std::regex search(probe); |
| 483 | std::smatch regMatch; |
| 484 | return std::regex_search(value, regMatch, search); |
| 485 | } |
| 486 | catch (const std::regex_error&) |
| 487 | { |
| 488 | std::cerr << "Syntax error in regular expression: " << probe |
| 489 | << " will never match"; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Skip calling nlohmann here, since it will never match a non-string |
| 494 | // to a std::string |
| 495 | return false; |
| 496 | } |
| 497 | }; |
| 498 | |
| 499 | /// \brief Forwarding JSON/DBus matching Callable for std::variant (visitor) |
| 500 | /// |
| 501 | /// Forward calls to the correct template instantiation of MatchProbe |
| 502 | struct MatchProbeForwarder |
| 503 | { |
| 504 | explicit MatchProbeForwarder(const nlohmann::json& probe) : probeRef(probe) |
| 505 | {} |
| 506 | const nlohmann::json& probeRef; |
| 507 | |
| 508 | template <typename T> |
| 509 | bool operator()(const T& dbusValue) const |
| 510 | { |
| 511 | return MatchProbe<T>::match(probeRef, dbusValue); |
| 512 | } |
| 513 | }; |
| 514 | |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 515 | bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue) |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 516 | { |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 517 | return std::visit(MatchProbeForwarder(probe), dbusValue); |
| 518 | } |