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 | |
Andrew Jeffery | a05437e | 2022-04-07 16:17:21 +0930 | [diff] [blame] | 20 | #include "Expression.hpp" |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 21 | #include "VariantVisitors.hpp" |
| 22 | |
| 23 | #include <boost/algorithm/string/classification.hpp> |
| 24 | #include <boost/algorithm/string/find.hpp> |
| 25 | #include <boost/algorithm/string/predicate.hpp> |
| 26 | #include <boost/algorithm/string/replace.hpp> |
| 27 | #include <boost/algorithm/string/split.hpp> |
| 28 | #include <boost/container/flat_map.hpp> |
| 29 | #include <boost/lexical_cast.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 30 | #include <sdbusplus/bus/match.hpp> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 31 | #include <valijson/adapters/nlohmann_json_adapter.hpp> |
| 32 | #include <valijson/schema.hpp> |
| 33 | #include <valijson/schema_parser.hpp> |
| 34 | #include <valijson/validator.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 35 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 36 | #include <filesystem> |
| 37 | #include <fstream> |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 38 | #include <map> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 39 | #include <regex> |
| 40 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 41 | constexpr const char* templateChar = "$"; |
| 42 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 43 | namespace fs = std::filesystem; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 44 | static bool powerStatusOn = false; |
| 45 | static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 46 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 47 | bool findFiles(const fs::path& dirPath, const std::string& matchString, |
| 48 | std::vector<fs::path>& foundPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 49 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 50 | if (!fs::exists(dirPath)) |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 51 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 52 | return false; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 53 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 54 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 55 | std::regex search(matchString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 56 | std::smatch match; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 57 | for (const auto& p : fs::directory_iterator(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 58 | { |
| 59 | std::string path = p.path().string(); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 60 | if (std::regex_search(path, match, search)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 61 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 62 | foundPaths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 68 | bool findFiles(const std::vector<fs::path>&& dirPaths, |
| 69 | const std::string& matchString, |
| 70 | std::vector<fs::path>& foundPaths) |
| 71 | { |
| 72 | std::map<fs::path, fs::path> paths; |
| 73 | std::regex search(matchString); |
| 74 | std::smatch match; |
| 75 | for (const auto& dirPath : dirPaths) |
| 76 | { |
| 77 | if (!fs::exists(dirPath)) |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 78 | { |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 79 | continue; |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 80 | } |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 81 | |
| 82 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 83 | { |
| 84 | std::string path = p.path().string(); |
| 85 | if (std::regex_search(path, match, search)) |
| 86 | { |
| 87 | paths[p.path().filename()] = p.path(); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | for (const auto& [key, value] : paths) |
| 93 | { |
| 94 | foundPaths.emplace_back(value); |
| 95 | } |
| 96 | |
| 97 | return !foundPaths.empty(); |
| 98 | } |
| 99 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 100 | bool getI2cDevicePaths(const fs::path& dirPath, |
| 101 | boost::container::flat_map<size_t, fs::path>& busPaths) |
| 102 | { |
| 103 | if (!fs::exists(dirPath)) |
| 104 | { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // Regex for matching the path |
| 109 | std::regex searchPath(std::string(R"(i2c-\d+$)")); |
| 110 | // Regex for matching the bus numbers |
| 111 | std::regex searchBus(std::string(R"(\w[^-]*$)")); |
| 112 | std::smatch matchPath; |
| 113 | std::smatch matchBus; |
| 114 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 115 | { |
| 116 | std::string path = p.path().string(); |
| 117 | if (std::regex_search(path, matchPath, searchPath)) |
| 118 | { |
| 119 | if (std::regex_search(path, matchBus, searchBus)) |
| 120 | { |
| 121 | size_t bus = stoul(*matchBus.begin()); |
| 122 | busPaths.insert(std::pair<size_t, fs::path>(bus, p.path())); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 130 | bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input) |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 131 | { |
| 132 | valijson::Schema schema; |
| 133 | valijson::SchemaParser parser; |
| 134 | valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile); |
| 135 | parser.populateSchema(schemaAdapter, schema); |
| 136 | valijson::Validator validator; |
| 137 | valijson::adapters::NlohmannJsonAdapter targetAdapter(input); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 138 | if (!validator.validate(schema, targetAdapter, nullptr)) |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 139 | { |
| 140 | return false; |
| 141 | } |
| 142 | return true; |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 143 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 144 | |
| 145 | bool isPowerOn(void) |
| 146 | { |
| 147 | if (!powerMatch) |
| 148 | { |
| 149 | throw std::runtime_error("Power Match Not Created"); |
| 150 | } |
| 151 | return powerStatusOn; |
| 152 | } |
| 153 | |
| 154 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 155 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 156 | powerMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 157 | static_cast<sdbusplus::bus::bus&>(*conn), |
James Feist | 61f5ac4 | 2020-03-11 14:37:25 -0700 | [diff] [blame] | 158 | "type='signal',interface='" + std::string(properties::interface) + |
| 159 | "',path='" + std::string(power::path) + "',arg0='" + |
| 160 | std::string(power::interface) + "'", |
| 161 | [](sdbusplus::message::message& message) { |
| 162 | std::string objectName; |
| 163 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 164 | values; |
| 165 | message.read(objectName, values); |
| 166 | auto findState = values.find(power::property); |
| 167 | if (findState != values.end()) |
| 168 | { |
| 169 | powerStatusOn = boost::ends_with( |
| 170 | std::get<std::string>(findState->second), "Running"); |
| 171 | } |
| 172 | }); |
| 173 | |
| 174 | conn->async_method_call( |
| 175 | [](boost::system::error_code ec, |
| 176 | const std::variant<std::string>& state) { |
| 177 | if (ec) |
| 178 | { |
| 179 | return; |
| 180 | } |
| 181 | powerStatusOn = |
| 182 | boost::ends_with(std::get<std::string>(state), "Running"); |
| 183 | }, |
| 184 | power::busname, power::path, properties::interface, properties::get, |
| 185 | power::interface, power::property); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 188 | // Replaces the template character like the other version of this function, |
| 189 | // but checks all properties on all interfaces provided to do the substitution |
| 190 | // with. |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 191 | std::optional<std::string> |
| 192 | templateCharReplace(nlohmann::json::iterator& keyPair, |
| 193 | const DBusObject& object, const size_t index, |
| 194 | const std::optional<std::string>& replaceStr) |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 195 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 196 | for (const auto& [_, interface] : object) |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 197 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 198 | auto ret = templateCharReplace(keyPair, interface, index, replaceStr); |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 199 | if (ret) |
| 200 | { |
| 201 | return ret; |
| 202 | } |
| 203 | } |
| 204 | return std::nullopt; |
| 205 | } |
| 206 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 207 | // finds the template character (currently set to $) and replaces the value with |
| 208 | // the field found in a dbus object i.e. $ADDRESS would get populated with the |
| 209 | // ADDRESS field from a object on dbus |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 210 | std::optional<std::string> |
| 211 | templateCharReplace(nlohmann::json::iterator& keyPair, |
| 212 | const DBusInterface& interface, const size_t index, |
| 213 | const std::optional<std::string>& replaceStr) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 214 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 215 | std::optional<std::string> ret = std::nullopt; |
| 216 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 217 | if (keyPair.value().type() == nlohmann::json::value_t::object || |
| 218 | keyPair.value().type() == nlohmann::json::value_t::array) |
| 219 | { |
| 220 | for (auto nextLayer = keyPair.value().begin(); |
| 221 | nextLayer != keyPair.value().end(); nextLayer++) |
| 222 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 223 | templateCharReplace(nextLayer, interface, index, replaceStr); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 224 | } |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 225 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | std::string* strPtr = keyPair.value().get_ptr<std::string*>(); |
| 229 | if (strPtr == nullptr) |
| 230 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 231 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | boost::replace_all(*strPtr, std::string(templateChar) + "index", |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 235 | std::to_string(index)); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 236 | if (replaceStr) |
| 237 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 238 | boost::replace_all(*strPtr, *replaceStr, std::to_string(index)); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 239 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 240 | |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 241 | for (auto& [propName, propValue] : interface) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 242 | { |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 243 | std::string templateName = templateChar + propName; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 244 | boost::iterator_range<std::string::const_iterator> find = |
| 245 | boost::ifind_first(*strPtr, templateName); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 246 | if (!find) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 247 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 248 | continue; |
| 249 | } |
James Feist | 8c20feb | 2019-08-14 15:10:11 -0700 | [diff] [blame] | 250 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 251 | size_t start = find.begin() - strPtr->begin(); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 252 | |
| 253 | // check for additional operations |
| 254 | if (!start && find.end() == strPtr->end()) |
| 255 | { |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 256 | std::visit([&](auto&& val) { keyPair.value() = val; }, propValue); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 257 | return ret; |
| 258 | } |
Andrew Jeffery | 7d05afc | 2022-04-07 15:12:10 +0930 | [diff] [blame] | 259 | |
| 260 | constexpr const std::array<char, 5> mathChars = {'+', '-', '%', '*', |
| 261 | '/'}; |
| 262 | size_t nextItemIdx = start + templateName.size() + 1; |
| 263 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 264 | if (nextItemIdx > strPtr->size() || |
| 265 | std::find(mathChars.begin(), mathChars.end(), |
| 266 | strPtr->at(nextItemIdx)) == mathChars.end()) |
| 267 | { |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 268 | std::string val = std::visit(VariantToStringVisitor(), propValue); |
Andrew Jeffery | 8d8b62f | 2022-04-07 12:32:13 +0930 | [diff] [blame] | 269 | boost::ireplace_all(*strPtr, templateName, val); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 5ace4e4 | 2022-04-07 16:58:30 +0930 | [diff] [blame] | 295 | auto it = split.begin(); |
| 296 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 297 | // we assume that the replacement is a number, because we can |
| 298 | // only do math on numbers.. we might concatenate strings in the |
| 299 | // future, but thats later |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 300 | int number = std::visit(VariantToIntVisitor(), propValue); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 301 | |
Andrew Jeffery | 5ace4e4 | 2022-04-07 16:58:30 +0930 | [diff] [blame] | 302 | number = expression::evaluate(number, it, split.end()); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 303 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 304 | std::string result = prefix + std::to_string(number); |
| 305 | |
| 306 | std::string replaced(find.begin(), find.end()); |
| 307 | for (auto it2 = split.begin(); it2 != split.end(); it2++) |
| 308 | { |
| 309 | replaced += " "; |
| 310 | replaced += *it2; |
| 311 | if (it2 == it) |
Zhikui Ren | a0d1b3f | 2021-10-05 16:21:56 -0700 | [diff] [blame] | 312 | { |
| 313 | break; |
| 314 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 315 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 316 | ret = replaced; |
| 317 | |
| 318 | if (it != split.end()) |
| 319 | { |
| 320 | for (; it != split.end(); it++) |
| 321 | { |
| 322 | result += " " + *it; |
| 323 | } |
| 324 | } |
| 325 | keyPair.value() = result; |
| 326 | |
| 327 | // We probably just invalidated the pointer abovei, |
| 328 | // reset and continue to handle multiple templates |
| 329 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 330 | if (strPtr == nullptr) |
| 331 | { |
| 332 | break; |
| 333 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 337 | if (strPtr == nullptr) |
| 338 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 339 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Andrew Jeffery | 159588b | 2022-04-07 17:04:13 +0930 | [diff] [blame^] | 342 | try |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 343 | { |
Andrew Jeffery | 159588b | 2022-04-07 17:04:13 +0930 | [diff] [blame^] | 344 | size_t pos = 0; |
| 345 | int64_t temp = std::stoul(*strPtr, &pos, 0); |
| 346 | if (pos == strPtr->size()) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 347 | { |
Andrew Jeffery | 159588b | 2022-04-07 17:04:13 +0930 | [diff] [blame^] | 348 | keyPair.value() = static_cast<uint64_t>(temp); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 349 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 350 | } |
Andrew Jeffery | 159588b | 2022-04-07 17:04:13 +0930 | [diff] [blame^] | 351 | catch (const std::invalid_argument&) |
| 352 | {} |
| 353 | catch (const std::out_of_range&) |
| 354 | {} |
| 355 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 356 | return ret; |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 357 | } |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 358 | |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 359 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 360 | /// |
| 361 | /// Default match JSON/DBus match implementation |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 362 | /// \tparam T The concrete DBus value type from DBusValueVariant |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 363 | template <typename T> |
| 364 | struct MatchProbe |
| 365 | { |
| 366 | /// \param probe the probe statement to match against |
| 367 | /// \param value the property value being matched to a probe |
| 368 | /// \return true if the dbusValue matched the probe otherwise false |
| 369 | static bool match(const nlohmann::json& probe, const T& value) |
| 370 | { |
| 371 | return probe == value; |
| 372 | } |
| 373 | }; |
| 374 | |
| 375 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 376 | /// |
| 377 | /// std::string specialization of MatchProbe enabling regex matching |
| 378 | template <> |
| 379 | struct MatchProbe<std::string> |
| 380 | { |
| 381 | /// \param probe the probe statement to match against |
| 382 | /// \param value the string value being matched to a probe |
| 383 | /// \return true if the dbusValue matched the probe otherwise false |
| 384 | static bool match(const nlohmann::json& probe, const std::string& value) |
| 385 | { |
| 386 | if (probe.is_string()) |
| 387 | { |
| 388 | try |
| 389 | { |
| 390 | std::regex search(probe); |
| 391 | std::smatch regMatch; |
| 392 | return std::regex_search(value, regMatch, search); |
| 393 | } |
| 394 | catch (const std::regex_error&) |
| 395 | { |
| 396 | std::cerr << "Syntax error in regular expression: " << probe |
| 397 | << " will never match"; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Skip calling nlohmann here, since it will never match a non-string |
| 402 | // to a std::string |
| 403 | return false; |
| 404 | } |
| 405 | }; |
| 406 | |
| 407 | /// \brief Forwarding JSON/DBus matching Callable for std::variant (visitor) |
| 408 | /// |
| 409 | /// Forward calls to the correct template instantiation of MatchProbe |
| 410 | struct MatchProbeForwarder |
| 411 | { |
| 412 | explicit MatchProbeForwarder(const nlohmann::json& probe) : probeRef(probe) |
| 413 | {} |
| 414 | const nlohmann::json& probeRef; |
| 415 | |
| 416 | template <typename T> |
| 417 | bool operator()(const T& dbusValue) const |
| 418 | { |
| 419 | return MatchProbe<T>::match(probeRef, dbusValue); |
| 420 | } |
| 421 | }; |
| 422 | |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 423 | bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue) |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 424 | { |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 425 | return std::visit(MatchProbeForwarder(probe), dbusValue); |
| 426 | } |