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 | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file utils.cpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 18 | #include "utils.hpp" |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 19 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 20 | #include "expression.hpp" |
| 21 | #include "variant_visitors.hpp" |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 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 | |
Jae Hyun Yoo | 1c9d378 | 2022-03-30 15:10:14 -0700 | [diff] [blame] | 36 | #include <charconv> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 37 | #include <filesystem> |
| 38 | #include <fstream> |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 39 | #include <map> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 40 | #include <regex> |
| 41 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 42 | constexpr const char* templateChar = "$"; |
| 43 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 44 | namespace fs = std::filesystem; |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 45 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 46 | static bool powerStatusOn = false; |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 47 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 48 | static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 49 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 50 | bool findFiles(const fs::path& dirPath, const std::string& matchString, |
| 51 | std::vector<fs::path>& foundPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 52 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 53 | if (!fs::exists(dirPath)) |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 54 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 55 | return false; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 56 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 57 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 58 | std::regex search(matchString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 59 | std::smatch match; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 60 | for (const auto& p : fs::directory_iterator(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 61 | { |
| 62 | std::string path = p.path().string(); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 63 | if (std::regex_search(path, match, search)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 64 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 65 | foundPaths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 71 | bool findFiles(const std::vector<fs::path>&& dirPaths, |
| 72 | const std::string& matchString, |
| 73 | std::vector<fs::path>& foundPaths) |
| 74 | { |
| 75 | std::map<fs::path, fs::path> paths; |
| 76 | std::regex search(matchString); |
| 77 | std::smatch match; |
| 78 | for (const auto& dirPath : dirPaths) |
| 79 | { |
| 80 | if (!fs::exists(dirPath)) |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 81 | { |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 82 | continue; |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 83 | } |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 84 | |
| 85 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 86 | { |
| 87 | std::string path = p.path().string(); |
| 88 | if (std::regex_search(path, match, search)) |
| 89 | { |
| 90 | paths[p.path().filename()] = p.path(); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | for (const auto& [key, value] : paths) |
| 96 | { |
| 97 | foundPaths.emplace_back(value); |
| 98 | } |
| 99 | |
| 100 | return !foundPaths.empty(); |
| 101 | } |
| 102 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 103 | bool getI2cDevicePaths(const fs::path& dirPath, |
| 104 | boost::container::flat_map<size_t, fs::path>& busPaths) |
| 105 | { |
| 106 | if (!fs::exists(dirPath)) |
| 107 | { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // Regex for matching the path |
| 112 | std::regex searchPath(std::string(R"(i2c-\d+$)")); |
| 113 | // Regex for matching the bus numbers |
| 114 | std::regex searchBus(std::string(R"(\w[^-]*$)")); |
| 115 | std::smatch matchPath; |
| 116 | std::smatch matchBus; |
| 117 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 118 | { |
| 119 | std::string path = p.path().string(); |
| 120 | if (std::regex_search(path, matchPath, searchPath)) |
| 121 | { |
| 122 | if (std::regex_search(path, matchBus, searchBus)) |
| 123 | { |
| 124 | size_t bus = stoul(*matchBus.begin()); |
| 125 | busPaths.insert(std::pair<size_t, fs::path>(bus, p.path())); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 133 | bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input) |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 134 | { |
| 135 | valijson::Schema schema; |
| 136 | valijson::SchemaParser parser; |
| 137 | valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile); |
| 138 | parser.populateSchema(schemaAdapter, schema); |
| 139 | valijson::Validator validator; |
| 140 | valijson::adapters::NlohmannJsonAdapter targetAdapter(input); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 141 | return validator.validate(schema, targetAdapter, nullptr); |
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 | |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 144 | bool isPowerOn() |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 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 | { |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 155 | powerMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 156 | static_cast<sdbusplus::bus_t&>(*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) + "'", |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 160 | [](sdbusplus::message_t& message) { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 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 | }); |
James Feist | 61f5ac4 | 2020-03-11 14:37:25 -0700 | [diff] [blame] | 172 | |
| 173 | conn->async_method_call( |
| 174 | [](boost::system::error_code ec, |
| 175 | const std::variant<std::string>& state) { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 176 | if (ec) |
| 177 | { |
| 178 | return; |
| 179 | } |
| 180 | powerStatusOn = |
| 181 | boost::ends_with(std::get<std::string>(state), "Running"); |
| 182 | }, |
James Feist | 61f5ac4 | 2020-03-11 14:37:25 -0700 | [diff] [blame] | 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. |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 190 | std::optional<std::string> templateCharReplace( |
| 191 | nlohmann::json::iterator& keyPair, const DBusObject& object, |
| 192 | const size_t index, const std::optional<std::string>& replaceStr) |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 193 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 194 | for (const auto& [_, interface] : object) |
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 | auto ret = templateCharReplace(keyPair, interface, index, replaceStr); |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 197 | if (ret) |
| 198 | { |
| 199 | return ret; |
| 200 | } |
| 201 | } |
| 202 | return std::nullopt; |
| 203 | } |
| 204 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 205 | // finds the template character (currently set to $) and replaces the value with |
| 206 | // the field found in a dbus object i.e. $ADDRESS would get populated with the |
| 207 | // ADDRESS field from a object on dbus |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 208 | std::optional<std::string> templateCharReplace( |
| 209 | nlohmann::json::iterator& keyPair, const DBusInterface& interface, |
| 210 | const size_t index, const std::optional<std::string>& replaceStr) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 211 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 212 | std::optional<std::string> ret = std::nullopt; |
| 213 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 214 | if (keyPair.value().type() == nlohmann::json::value_t::object || |
| 215 | keyPair.value().type() == nlohmann::json::value_t::array) |
| 216 | { |
| 217 | for (auto nextLayer = keyPair.value().begin(); |
| 218 | nextLayer != keyPair.value().end(); nextLayer++) |
| 219 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 220 | templateCharReplace(nextLayer, interface, index, replaceStr); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 221 | } |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 222 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | std::string* strPtr = keyPair.value().get_ptr<std::string*>(); |
| 226 | if (strPtr == nullptr) |
| 227 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 228 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | boost::replace_all(*strPtr, std::string(templateChar) + "index", |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 232 | std::to_string(index)); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 233 | if (replaceStr) |
| 234 | { |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 235 | boost::replace_all(*strPtr, *replaceStr, std::to_string(index)); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 236 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 237 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 238 | for (const auto& [propName, propValue] : interface) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 239 | { |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 240 | std::string templateName = templateChar + propName; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 241 | boost::iterator_range<std::string::const_iterator> find = |
| 242 | boost::ifind_first(*strPtr, templateName); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 243 | if (!find) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 244 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 245 | continue; |
| 246 | } |
James Feist | 8c20feb | 2019-08-14 15:10:11 -0700 | [diff] [blame] | 247 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 248 | size_t start = find.begin() - strPtr->begin(); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 249 | |
| 250 | // check for additional operations |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 251 | if ((start == 0U) && find.end() == strPtr->end()) |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 252 | { |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 253 | std::visit([&](auto&& val) { keyPair.value() = val; }, propValue); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 254 | return ret; |
| 255 | } |
Andrew Jeffery | 7d05afc | 2022-04-07 15:12:10 +0930 | [diff] [blame] | 256 | |
| 257 | constexpr const std::array<char, 5> mathChars = {'+', '-', '%', '*', |
| 258 | '/'}; |
| 259 | size_t nextItemIdx = start + templateName.size() + 1; |
| 260 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 261 | if (nextItemIdx > strPtr->size() || |
| 262 | std::find(mathChars.begin(), mathChars.end(), |
| 263 | strPtr->at(nextItemIdx)) == mathChars.end()) |
| 264 | { |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 265 | std::string val = std::visit(VariantToStringVisitor(), propValue); |
Andrew Jeffery | 8d8b62f | 2022-04-07 12:32:13 +0930 | [diff] [blame] | 266 | boost::ireplace_all(*strPtr, templateName, val); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 267 | continue; |
| 268 | } |
| 269 | |
| 270 | // save the prefix |
| 271 | std::string prefix = strPtr->substr(0, start); |
| 272 | |
| 273 | // operate on the rest |
| 274 | std::string end = strPtr->substr(nextItemIdx); |
| 275 | |
| 276 | std::vector<std::string> split; |
| 277 | boost::split(split, end, boost::is_any_of(" ")); |
| 278 | |
| 279 | // need at least 1 operation and number |
| 280 | if (split.size() < 2) |
| 281 | { |
| 282 | std::cerr << "Syntax error on template replacement of " << *strPtr |
| 283 | << "\n"; |
| 284 | for (const std::string& data : split) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 285 | { |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 286 | std::cerr << data << " "; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 287 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 288 | std::cerr << "\n"; |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | // we assume that the replacement is a number, because we can |
| 293 | // only do math on numbers.. we might concatenate strings in the |
| 294 | // future, but thats later |
Andrew Jeffery | d9e3e19 | 2022-04-07 12:30:45 +0930 | [diff] [blame] | 295 | int number = std::visit(VariantToIntVisitor(), propValue); |
Andrew Jeffery | 9555961 | 2022-04-07 17:22:47 +0930 | [diff] [blame] | 296 | auto exprBegin = split.begin(); |
| 297 | auto exprEnd = split.end(); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 298 | |
Andrew Jeffery | 9555961 | 2022-04-07 17:22:47 +0930 | [diff] [blame] | 299 | number = expression::evaluate(number, exprBegin, exprEnd); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 300 | |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 301 | std::string replaced(find.begin(), find.end()); |
Andrew Jeffery | 9555961 | 2022-04-07 17:22:47 +0930 | [diff] [blame] | 302 | while (exprBegin != exprEnd) |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 303 | { |
Andrew Jeffery | efcbf00 | 2022-04-07 17:29:22 +0930 | [diff] [blame] | 304 | replaced.append(" ").append(*exprBegin++); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 305 | } |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 306 | ret = replaced; |
| 307 | |
Andrew Jeffery | f1102ff | 2022-04-07 17:30:46 +0930 | [diff] [blame] | 308 | std::string result = prefix + std::to_string(number); |
Andrew Jeffery | deb97f1 | 2022-04-07 17:27:46 +0930 | [diff] [blame] | 309 | while (exprEnd != split.end()) |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 310 | { |
Andrew Jeffery | efcbf00 | 2022-04-07 17:29:22 +0930 | [diff] [blame] | 311 | result.append(" ").append(*exprEnd++); |
Andrew Jeffery | e0ed587 | 2022-04-07 12:16:33 +0930 | [diff] [blame] | 312 | } |
| 313 | keyPair.value() = result; |
| 314 | |
| 315 | // We probably just invalidated the pointer abovei, |
| 316 | // reset and continue to handle multiple templates |
| 317 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 318 | if (strPtr == nullptr) |
| 319 | { |
| 320 | break; |
| 321 | } |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | strPtr = keyPair.value().get_ptr<std::string*>(); |
| 325 | if (strPtr == nullptr) |
| 326 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 327 | return ret; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Jae Hyun Yoo | 1c9d378 | 2022-03-30 15:10:14 -0700 | [diff] [blame] | 330 | std::string_view strView = *strPtr; |
| 331 | int base = 10; |
| 332 | if (boost::starts_with(strView, "0x")) |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 333 | { |
Jae Hyun Yoo | 1c9d378 | 2022-03-30 15:10:14 -0700 | [diff] [blame] | 334 | strView.remove_prefix(2); |
| 335 | base = 16; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 336 | } |
Jae Hyun Yoo | 1c9d378 | 2022-03-30 15:10:14 -0700 | [diff] [blame] | 337 | |
| 338 | uint64_t temp = 0; |
| 339 | const char* strDataEndPtr = strView.data() + strView.size(); |
| 340 | const std::from_chars_result res = |
| 341 | std::from_chars(strView.data(), strDataEndPtr, temp, base); |
| 342 | if (res.ec == std::errc{} && res.ptr == strDataEndPtr) |
| 343 | { |
| 344 | keyPair.value() = temp; |
| 345 | } |
Andrew Jeffery | 159588b | 2022-04-07 17:04:13 +0930 | [diff] [blame] | 346 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 347 | return ret; |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 348 | } |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 349 | |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 350 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 351 | /// |
| 352 | /// Default match JSON/DBus match implementation |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 353 | /// \tparam T The concrete DBus value type from DBusValueVariant |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 354 | template <typename T> |
| 355 | struct MatchProbe |
| 356 | { |
| 357 | /// \param probe the probe statement to match against |
| 358 | /// \param value the property value being matched to a probe |
| 359 | /// \return true if the dbusValue matched the probe otherwise false |
| 360 | static bool match(const nlohmann::json& probe, const T& value) |
| 361 | { |
| 362 | return probe == value; |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 367 | /// |
| 368 | /// std::string specialization of MatchProbe enabling regex matching |
| 369 | template <> |
| 370 | struct MatchProbe<std::string> |
| 371 | { |
| 372 | /// \param probe the probe statement to match against |
| 373 | /// \param value the string value being matched to a probe |
| 374 | /// \return true if the dbusValue matched the probe otherwise false |
| 375 | static bool match(const nlohmann::json& probe, const std::string& value) |
| 376 | { |
| 377 | if (probe.is_string()) |
| 378 | { |
| 379 | try |
| 380 | { |
| 381 | std::regex search(probe); |
| 382 | std::smatch regMatch; |
| 383 | return std::regex_search(value, regMatch, search); |
| 384 | } |
| 385 | catch (const std::regex_error&) |
| 386 | { |
| 387 | std::cerr << "Syntax error in regular expression: " << probe |
| 388 | << " will never match"; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Skip calling nlohmann here, since it will never match a non-string |
| 393 | // to a std::string |
| 394 | return false; |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | /// \brief Forwarding JSON/DBus matching Callable for std::variant (visitor) |
| 399 | /// |
| 400 | /// Forward calls to the correct template instantiation of MatchProbe |
| 401 | struct MatchProbeForwarder |
| 402 | { |
| 403 | explicit MatchProbeForwarder(const nlohmann::json& probe) : probeRef(probe) |
| 404 | {} |
| 405 | const nlohmann::json& probeRef; |
| 406 | |
| 407 | template <typename T> |
| 408 | bool operator()(const T& dbusValue) const |
| 409 | { |
| 410 | return MatchProbe<T>::match(probeRef, dbusValue); |
| 411 | } |
| 412 | }; |
| 413 | |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 414 | bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue) |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 415 | { |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 416 | return std::visit(MatchProbeForwarder(probe), dbusValue); |
| 417 | } |