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