Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 perform_probe.cpp |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 17 | #include "perform_probe.hpp" |
| 18 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 19 | #include "entity_manager.hpp" |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 20 | #include "perform_scan.hpp" |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 21 | |
| 22 | #include <boost/algorithm/string/replace.hpp> |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 23 | #include <phosphor-logging/lg2.hpp> |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 24 | |
| 25 | #include <regex> |
Andrew Jeffery | ea4ff02 | 2022-04-21 12:31:40 +0930 | [diff] [blame] | 26 | #include <utility> |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 27 | |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 28 | // probes dbus interface dictionary for a key with a value that matches a regex |
| 29 | // When an interface passes a probe, also save its D-Bus path with it. |
Andrew Jeffery | f5772d2 | 2022-04-12 22:05:30 +0930 | [diff] [blame] | 30 | bool probeDbus(const std::string& interfaceName, |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 31 | const std::map<std::string, nlohmann::json>& matches, |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 32 | scan::FoundDevices& devices, |
| 33 | const std::shared_ptr<scan::PerformScan>& scan, bool& foundProbe) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 34 | { |
| 35 | bool foundMatch = false; |
| 36 | foundProbe = false; |
| 37 | |
| 38 | for (const auto& [path, interfaces] : scan->dbusProbeObjects) |
| 39 | { |
Andrew Jeffery | f5772d2 | 2022-04-12 22:05:30 +0930 | [diff] [blame] | 40 | auto it = interfaces.find(interfaceName); |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 41 | if (it == interfaces.end()) |
| 42 | { |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | foundProbe = true; |
| 47 | |
| 48 | bool deviceMatches = true; |
Andrew Jeffery | f5772d2 | 2022-04-12 22:05:30 +0930 | [diff] [blame] | 49 | const DBusInterface& interface = it->second; |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 50 | |
| 51 | for (const auto& [matchProp, matchJSON] : matches) |
| 52 | { |
Andrew Jeffery | f5772d2 | 2022-04-12 22:05:30 +0930 | [diff] [blame] | 53 | auto deviceValue = interface.find(matchProp); |
| 54 | if (deviceValue != interface.end()) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 55 | { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 56 | deviceMatches = deviceMatches && |
| 57 | matchProbe(matchJSON, deviceValue->second); |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 58 | } |
| 59 | else |
| 60 | { |
| 61 | // Move on to the next DBus path |
| 62 | deviceMatches = false; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | if (deviceMatches) |
| 67 | { |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 68 | lg2::debug("Found probe match on {PATH} {IFACE}", "PATH", path, |
| 69 | "IFACE", interfaceName); |
Patrick Williams | d2b7861 | 2023-10-20 17:24:54 -0500 | [diff] [blame] | 70 | devices.emplace_back(interface, path); |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 71 | foundMatch = true; |
| 72 | } |
| 73 | } |
| 74 | return foundMatch; |
| 75 | } |
| 76 | |
| 77 | // default probe entry point, iterates a list looking for specific types to |
| 78 | // call specific probe functions |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 79 | bool doProbe(const std::vector<std::string>& probeCommand, |
| 80 | const std::shared_ptr<scan::PerformScan>& scan, |
| 81 | scan::FoundDevices& foundDevs) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 82 | { |
| 83 | const static std::regex command(R"(\((.*)\))"); |
| 84 | std::smatch match; |
| 85 | bool ret = false; |
| 86 | bool matchOne = false; |
| 87 | bool cur = true; |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 88 | probe::probe_type_codes lastCommand = probe::probe_type_codes::FALSE_T; |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 89 | bool first = true; |
| 90 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 91 | for (const auto& probe : probeCommand) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 92 | { |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 93 | probe::FoundProbeTypeT probeType = probe::findProbeType(probe); |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 94 | if (probeType) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 95 | { |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 96 | switch ((*probeType)->second) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 97 | { |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 98 | case probe::probe_type_codes::FALSE_T: |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 99 | { |
| 100 | cur = false; |
| 101 | break; |
| 102 | } |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 103 | case probe::probe_type_codes::TRUE_T: |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 104 | { |
| 105 | cur = true; |
| 106 | break; |
| 107 | } |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 108 | case probe::probe_type_codes::MATCH_ONE: |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 109 | { |
| 110 | // set current value to last, this probe type shouldn't |
| 111 | // affect the outcome |
| 112 | cur = ret; |
| 113 | matchOne = true; |
| 114 | break; |
| 115 | } |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 116 | /*case probe::probe_type_codes::AND: |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 117 | break; |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 118 | case probe::probe_type_codes::OR: |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 119 | break; |
| 120 | // these are no-ops until the last command switch |
| 121 | */ |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 122 | case probe::probe_type_codes::FOUND: |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 123 | { |
| 124 | if (!std::regex_search(probe, match, command)) |
| 125 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 126 | std::cerr |
| 127 | << "found probe syntax error " << probe << "\n"; |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | std::string commandStr = *(match.begin() + 1); |
| 131 | boost::replace_all(commandStr, "'", ""); |
| 132 | cur = (std::find(scan->passedProbes.begin(), |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 133 | scan->passedProbes.end(), commandStr) != |
| 134 | scan->passedProbes.end()); |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | default: |
| 138 | { |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | // look on dbus for object |
| 144 | else |
| 145 | { |
| 146 | if (!std::regex_search(probe, match, command)) |
| 147 | { |
| 148 | std::cerr << "dbus probe syntax error " << probe << "\n"; |
| 149 | return false; |
| 150 | } |
| 151 | std::string commandStr = *(match.begin() + 1); |
| 152 | // convert single ticks and single slashes into legal json |
| 153 | boost::replace_all(commandStr, "'", "\""); |
| 154 | boost::replace_all(commandStr, R"(\)", R"(\\)"); |
Potin Lai | 0f3a4d9 | 2023-12-05 00:13:55 +0800 | [diff] [blame] | 155 | auto json = nlohmann::json::parse(commandStr, nullptr, false, true); |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 156 | if (json.is_discarded()) |
| 157 | { |
| 158 | std::cerr << "dbus command syntax error " << commandStr << "\n"; |
| 159 | return false; |
| 160 | } |
| 161 | // we can match any (string, variant) property. (string, string) |
| 162 | // does a regex |
| 163 | std::map<std::string, nlohmann::json> dbusProbeMap = |
| 164 | json.get<std::map<std::string, nlohmann::json>>(); |
| 165 | auto findStart = probe.find('('); |
| 166 | if (findStart == std::string::npos) |
| 167 | { |
| 168 | return false; |
| 169 | } |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 170 | bool foundProbe = !!probeType; |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 171 | std::string probeInterface = probe.substr(0, findStart); |
| 172 | cur = probeDbus(probeInterface, dbusProbeMap, foundDevs, scan, |
| 173 | foundProbe); |
| 174 | } |
| 175 | |
| 176 | // some functions like AND and OR only take affect after the |
| 177 | // fact |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 178 | if (lastCommand == probe::probe_type_codes::AND) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 179 | { |
| 180 | ret = cur && ret; |
| 181 | } |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 182 | else if (lastCommand == probe::probe_type_codes::OR) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 183 | { |
| 184 | ret = cur || ret; |
| 185 | } |
| 186 | |
| 187 | if (first) |
| 188 | { |
| 189 | ret = cur; |
| 190 | first = false; |
| 191 | } |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 192 | lastCommand = probeType ? (*probeType)->second |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 193 | : probe::probe_type_codes::FALSE_T; |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // probe passed, but empty device |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 197 | if (ret && foundDevs.empty()) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 198 | { |
Patrick Williams | d2b7861 | 2023-10-20 17:24:54 -0500 | [diff] [blame] | 199 | foundDevs.emplace_back( |
| 200 | boost::container::flat_map<std::string, DBusValueVariant>{}, |
| 201 | std::string{}); |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 202 | } |
| 203 | if (matchOne && ret) |
| 204 | { |
| 205 | // match the last one |
| 206 | auto last = foundDevs.back(); |
| 207 | foundDevs.clear(); |
| 208 | |
| 209 | foundDevs.emplace_back(std::move(last)); |
| 210 | } |
| 211 | return ret; |
| 212 | } |
| 213 | |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 214 | namespace probe |
| 215 | { |
| 216 | |
Andrew Jeffery | ea4ff02 | 2022-04-21 12:31:40 +0930 | [diff] [blame] | 217 | PerformProbe::PerformProbe(nlohmann::json& recordRef, |
| 218 | const std::vector<std::string>& probeCommand, |
| 219 | std::string probeName, |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 220 | std::shared_ptr<scan::PerformScan>& scanPtr) : |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 221 | recordRef(recordRef), _probeCommand(probeCommand), |
| 222 | probeName(std::move(probeName)), scan(scanPtr) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 223 | {} |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 224 | |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 225 | PerformProbe::~PerformProbe() |
| 226 | { |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 227 | scan::FoundDevices foundDevs; |
| 228 | if (doProbe(_probeCommand, scan, foundDevs)) |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 229 | { |
Andrew Jeffery | af9b46b | 2022-04-21 12:34:43 +0930 | [diff] [blame] | 230 | scan->updateSystemConfiguration(recordRef, probeName, foundDevs); |
Andrew Jeffery | f07c5ed | 2021-12-01 14:22:04 +1030 | [diff] [blame] | 231 | } |
| 232 | } |
Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame^] | 233 | |
| 234 | FoundProbeTypeT findProbeType(const std::string& probe) |
| 235 | { |
| 236 | const boost::container::flat_map<const char*, probe_type_codes, CmpStr> |
| 237 | probeTypes{{{"FALSE", probe_type_codes::FALSE_T}, |
| 238 | {"TRUE", probe_type_codes::TRUE_T}, |
| 239 | {"AND", probe_type_codes::AND}, |
| 240 | {"OR", probe_type_codes::OR}, |
| 241 | {"FOUND", probe_type_codes::FOUND}, |
| 242 | {"MATCH_ONE", probe_type_codes::MATCH_ONE}}}; |
| 243 | |
| 244 | boost::container::flat_map<const char*, probe_type_codes, |
| 245 | CmpStr>::const_iterator probeType; |
| 246 | for (probeType = probeTypes.begin(); probeType != probeTypes.end(); |
| 247 | ++probeType) |
| 248 | { |
| 249 | if (probe.find(probeType->first) != std::string::npos) |
| 250 | { |
| 251 | return probeType; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return std::nullopt; |
| 256 | } |
| 257 | |
| 258 | } // namespace probe |