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