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 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string/classification.hpp> |
| 21 | #include <boost/algorithm/string/find.hpp> |
| 22 | #include <boost/algorithm/string/predicate.hpp> |
| 23 | #include <boost/algorithm/string/replace.hpp> |
| 24 | #include <boost/algorithm/string/split.hpp> |
| 25 | #include <boost/container/flat_map.hpp> |
| 26 | #include <boost/lexical_cast.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 27 | #include <sdbusplus/bus/match.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 28 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 29 | #include <filesystem> |
Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 30 | #include <iostream> |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 31 | #include <map> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 32 | #include <regex> |
| 33 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 34 | namespace fs = std::filesystem; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 35 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 36 | bool findFiles(const fs::path& dirPath, const std::string& matchString, |
| 37 | std::vector<fs::path>& foundPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 38 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 39 | if (!fs::exists(dirPath)) |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 40 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 41 | return false; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 42 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 43 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 44 | std::regex search(matchString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 45 | std::smatch match; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 46 | for (const auto& p : fs::directory_iterator(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 47 | { |
| 48 | std::string path = p.path().string(); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 49 | if (std::regex_search(path, match, search)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 50 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 51 | foundPaths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 57 | bool findFiles(const std::vector<fs::path>&& dirPaths, |
| 58 | const std::string& matchString, |
| 59 | std::vector<fs::path>& foundPaths) |
| 60 | { |
| 61 | std::map<fs::path, fs::path> paths; |
| 62 | std::regex search(matchString); |
| 63 | std::smatch match; |
| 64 | for (const auto& dirPath : dirPaths) |
| 65 | { |
| 66 | if (!fs::exists(dirPath)) |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 67 | { |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 68 | continue; |
Bruce Mitchell | a6d4733 | 2021-12-13 10:18:03 -0600 | [diff] [blame] | 69 | } |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 70 | |
| 71 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 72 | { |
| 73 | std::string path = p.path().string(); |
| 74 | if (std::regex_search(path, match, search)) |
| 75 | { |
| 76 | paths[p.path().filename()] = p.path(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | for (const auto& [key, value] : paths) |
| 82 | { |
| 83 | foundPaths.emplace_back(value); |
| 84 | } |
| 85 | |
| 86 | return !foundPaths.empty(); |
| 87 | } |
| 88 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 89 | bool getI2cDevicePaths(const fs::path& dirPath, |
| 90 | boost::container::flat_map<size_t, fs::path>& busPaths) |
| 91 | { |
| 92 | if (!fs::exists(dirPath)) |
| 93 | { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | // Regex for matching the path |
| 98 | std::regex searchPath(std::string(R"(i2c-\d+$)")); |
| 99 | // Regex for matching the bus numbers |
| 100 | std::regex searchBus(std::string(R"(\w[^-]*$)")); |
| 101 | std::smatch matchPath; |
| 102 | std::smatch matchBus; |
| 103 | for (const auto& p : fs::directory_iterator(dirPath)) |
| 104 | { |
| 105 | std::string path = p.path().string(); |
| 106 | if (std::regex_search(path, matchPath, searchPath)) |
| 107 | { |
| 108 | if (std::regex_search(path, matchBus, searchBus)) |
| 109 | { |
| 110 | size_t bus = stoul(*matchBus.begin()); |
| 111 | busPaths.insert(std::pair<size_t, fs::path>(bus, p.path())); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 119 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 120 | /// |
| 121 | /// Default match JSON/DBus match implementation |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 122 | /// \tparam T The concrete DBus value type from DBusValueVariant |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 123 | template <typename T> |
| 124 | struct MatchProbe |
| 125 | { |
| 126 | /// \param probe the probe statement to match against |
| 127 | /// \param value the property value being matched to a probe |
| 128 | /// \return true if the dbusValue matched the probe otherwise false |
| 129 | static bool match(const nlohmann::json& probe, const T& value) |
| 130 | { |
| 131 | return probe == value; |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | /// \brief JSON/DBus matching Callable for std::variant (visitor) |
| 136 | /// |
| 137 | /// std::string specialization of MatchProbe enabling regex matching |
| 138 | template <> |
| 139 | struct MatchProbe<std::string> |
| 140 | { |
| 141 | /// \param probe the probe statement to match against |
| 142 | /// \param value the string value being matched to a probe |
| 143 | /// \return true if the dbusValue matched the probe otherwise false |
| 144 | static bool match(const nlohmann::json& probe, const std::string& value) |
| 145 | { |
| 146 | if (probe.is_string()) |
| 147 | { |
| 148 | try |
| 149 | { |
| 150 | std::regex search(probe); |
| 151 | std::smatch regMatch; |
| 152 | return std::regex_search(value, regMatch, search); |
| 153 | } |
| 154 | catch (const std::regex_error&) |
| 155 | { |
| 156 | std::cerr << "Syntax error in regular expression: " << probe |
| 157 | << " will never match"; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Skip calling nlohmann here, since it will never match a non-string |
| 162 | // to a std::string |
| 163 | return false; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | /// \brief Forwarding JSON/DBus matching Callable for std::variant (visitor) |
| 168 | /// |
| 169 | /// Forward calls to the correct template instantiation of MatchProbe |
| 170 | struct MatchProbeForwarder |
| 171 | { |
| 172 | explicit MatchProbeForwarder(const nlohmann::json& probe) : probeRef(probe) |
| 173 | {} |
| 174 | const nlohmann::json& probeRef; |
| 175 | |
| 176 | template <typename T> |
| 177 | bool operator()(const T& dbusValue) const |
| 178 | { |
| 179 | return MatchProbe<T>::match(probeRef, dbusValue); |
| 180 | } |
| 181 | }; |
| 182 | |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 183 | bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue) |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 184 | { |
Brad Bishop | 5d52541 | 2020-08-26 08:50:50 -0400 | [diff] [blame] | 185 | return std::visit(MatchProbeForwarder(probe), dbusValue); |
| 186 | } |