Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +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 | */ |
| 16 | /// \file PerformScan.cpp |
| 17 | #include "EntityManager.hpp" |
| 18 | |
| 19 | #include <boost/algorithm/string/predicate.hpp> |
| 20 | #include <boost/asio/steady_timer.hpp> |
| 21 | #include <boost/container/flat_map.hpp> |
| 22 | #include <boost/container/flat_set.hpp> |
| 23 | |
| 24 | #include <charconv> |
| 25 | |
| 26 | /* Hacks from splitting EntityManager.cpp */ |
| 27 | extern std::shared_ptr<sdbusplus::asio::connection> systemBus; |
| 28 | extern nlohmann::json lastJson; |
| 29 | extern void |
| 30 | propertiesChangedCallback(nlohmann::json& systemConfiguration, |
| 31 | sdbusplus::asio::object_server& objServer); |
| 32 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 33 | using GetSubTreeType = std::vector< |
| 34 | std::pair<std::string, |
| 35 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 36 | |
| 37 | constexpr const int32_t maxMapperDepth = 0; |
| 38 | |
| 39 | constexpr const bool debug = false; |
| 40 | |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 41 | struct DBusInterfaceInstance |
| 42 | { |
| 43 | std::string busName; |
| 44 | std::string path; |
| 45 | std::string interface; |
| 46 | }; |
| 47 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 48 | void getInterfaces( |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 49 | const DBusInterfaceInstance& instance, |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 50 | const std::vector<std::shared_ptr<PerformProbe>>& probeVector, |
| 51 | const std::shared_ptr<PerformScan>& scan, size_t retries = 5) |
| 52 | { |
| 53 | if (!retries) |
| 54 | { |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 55 | std::cerr << "retries exhausted on " << instance.busName << " " |
| 56 | << instance.path << " " << instance.interface << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | systemBus->async_method_call( |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 61 | [instance, scan, probeVector, retries](boost::system::error_code& errc, |
| 62 | const DBusInterface& resp) { |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 63 | if (errc) |
| 64 | { |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 65 | std::cerr << "error calling getall on " << instance.busName |
| 66 | << " " << instance.path << " " |
| 67 | << instance.interface << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 68 | |
Andrew Jeffery | 91c5eaa | 2022-04-05 16:45:52 +0930 | [diff] [blame] | 69 | auto timer = std::make_shared<boost::asio::steady_timer>(io); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 70 | timer->expires_after(std::chrono::seconds(2)); |
| 71 | |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 72 | timer->async_wait([timer, instance, scan, probeVector, |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 73 | retries](const boost::system::error_code&) { |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 74 | getInterfaces(instance, probeVector, scan, retries - 1); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 75 | }); |
| 76 | return; |
| 77 | } |
| 78 | |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 79 | scan->dbusProbeObjects[instance.path][instance.interface] = resp; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 80 | }, |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 81 | instance.busName, instance.path, "org.freedesktop.DBus.Properties", |
| 82 | "GetAll", instance.interface); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 83 | |
| 84 | if constexpr (debug) |
| 85 | { |
| 86 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 87 | } |
| 88 | } |
| 89 | |
Andrew Jeffery | 4dcc55d | 2022-04-05 16:49:42 +0930 | [diff] [blame] | 90 | static void registerCallback(nlohmann::json& systemConfiguration, |
| 91 | sdbusplus::asio::object_server& objServer, |
| 92 | const std::string& path) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 93 | { |
| 94 | static boost::container::flat_map<std::string, sdbusplus::bus::match::match> |
| 95 | dbusMatches; |
| 96 | |
| 97 | auto find = dbusMatches.find(path); |
| 98 | if (find != dbusMatches.end()) |
| 99 | { |
| 100 | return; |
| 101 | } |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 102 | |
Andrew Jeffery | 26db575 | 2022-04-05 16:53:36 +0930 | [diff] [blame] | 103 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 104 | [&](sdbusplus::message::message&) { |
| 105 | propertiesChangedCallback(systemConfiguration, objServer); |
| 106 | }; |
| 107 | |
| 108 | sdbusplus::bus::match::match match( |
| 109 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 110 | "type='signal',member='PropertiesChanged',path='" + path + "'", |
| 111 | eventHandler); |
| 112 | dbusMatches.emplace(path, std::move(match)); |
| 113 | } |
| 114 | |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 115 | static void |
| 116 | processDbusObjects(std::vector<std::shared_ptr<PerformProbe>>& probeVector, |
| 117 | const std::shared_ptr<PerformScan>& scan, |
| 118 | const GetSubTreeType& interfaceSubtree) |
| 119 | { |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 120 | for (const auto& [path, object] : interfaceSubtree) |
| 121 | { |
Andrew Jeffery | 4732183 | 2022-04-05 16:30:29 +0930 | [diff] [blame] | 122 | // Get a PropertiesChanged callback for all interfaces on this path. |
| 123 | registerCallback(scan->_systemConfiguration, scan->objServer, path); |
| 124 | |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 125 | for (const auto& [busname, ifaces] : object) |
| 126 | { |
| 127 | for (const std::string& iface : ifaces) |
| 128 | { |
| 129 | // The 3 default org.freedeskstop interfaces (Peer, |
| 130 | // Introspectable, and Properties) are returned by |
| 131 | // the mapper but don't have properties, so don't bother |
| 132 | // with the GetAll call to save some cycles. |
| 133 | if (!boost::algorithm::starts_with(iface, "org.freedesktop")) |
| 134 | { |
Andrew Jeffery | 4732183 | 2022-04-05 16:30:29 +0930 | [diff] [blame] | 135 | getInterfaces({busname, path, iface}, probeVector, scan); |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 142 | // Populates scan->dbusProbeObjects with all interfaces and properties |
| 143 | // for the paths that own the interfaces passed in. |
| 144 | void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector, |
| 145 | boost::container::flat_set<std::string>&& interfaces, |
| 146 | const std::shared_ptr<PerformScan>& scan, |
| 147 | size_t retries = 5) |
| 148 | { |
| 149 | // Filter out interfaces already obtained. |
| 150 | for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects) |
| 151 | { |
| 152 | for (const auto& [interface, _] : probeInterfaces) |
| 153 | { |
| 154 | interfaces.erase(interface); |
| 155 | } |
| 156 | } |
| 157 | if (interfaces.empty()) |
| 158 | { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | // find all connections in the mapper that expose a specific type |
| 163 | systemBus->async_method_call( |
| 164 | [interfaces, probeVector{std::move(probeVector)}, scan, |
| 165 | retries](boost::system::error_code& ec, |
| 166 | const GetSubTreeType& interfaceSubtree) mutable { |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 167 | if (ec) |
| 168 | { |
| 169 | if (ec.value() == ENOENT) |
| 170 | { |
| 171 | return; // wasn't found by mapper |
| 172 | } |
| 173 | std::cerr << "Error communicating to mapper.\n"; |
| 174 | |
| 175 | if (!retries) |
| 176 | { |
| 177 | // if we can't communicate to the mapper something is very |
| 178 | // wrong |
| 179 | std::exit(EXIT_FAILURE); |
| 180 | } |
Andrew Jeffery | 91c5eaa | 2022-04-05 16:45:52 +0930 | [diff] [blame] | 181 | |
| 182 | auto timer = std::make_shared<boost::asio::steady_timer>(io); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 183 | timer->expires_after(std::chrono::seconds(10)); |
| 184 | |
| 185 | timer->async_wait( |
| 186 | [timer, interfaces{std::move(interfaces)}, scan, |
| 187 | probeVector{std::move(probeVector)}, |
| 188 | retries](const boost::system::error_code&) mutable { |
| 189 | findDbusObjects(std::move(probeVector), |
| 190 | std::move(interfaces), scan, |
| 191 | retries - 1); |
| 192 | }); |
| 193 | return; |
| 194 | } |
| 195 | |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 196 | processDbusObjects(probeVector, scan, interfaceSubtree); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 197 | }, |
| 198 | "xyz.openbmc_project.ObjectMapper", |
| 199 | "/xyz/openbmc_project/object_mapper", |
| 200 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth, |
| 201 | interfaces); |
| 202 | |
| 203 | if constexpr (debug) |
| 204 | { |
| 205 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 206 | } |
| 207 | } |
| 208 | |
Andrew Jeffery | 09a09a6 | 2022-04-12 22:49:57 +0930 | [diff] [blame^] | 209 | static std::string getRecordName(const DBusInterface& probe, |
| 210 | const std::string& probeName) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 211 | { |
| 212 | if (probe.empty()) |
| 213 | { |
| 214 | return probeName; |
| 215 | } |
| 216 | |
Andrew Jeffery | 928c5b2 | 2022-04-05 16:57:51 +0930 | [diff] [blame] | 217 | // use an array so alphabetical order from the flat_map is maintained |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 218 | auto device = nlohmann::json::array(); |
| 219 | for (auto& devPair : probe) |
| 220 | { |
| 221 | device.push_back(devPair.first); |
| 222 | std::visit([&device](auto&& v) { device.push_back(v); }, |
| 223 | devPair.second); |
| 224 | } |
Andrew Jeffery | 8650187 | 2022-04-05 16:58:22 +0930 | [diff] [blame] | 225 | |
Andrew Jeffery | 928c5b2 | 2022-04-05 16:57:51 +0930 | [diff] [blame] | 226 | // hashes are hard to distinguish, use the non-hashed version if we want |
| 227 | // debug |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 228 | if constexpr (debug) |
| 229 | { |
| 230 | return probeName + device.dump(); |
| 231 | } |
Andrew Jeffery | 1ae4ed6 | 2022-04-05 16:55:43 +0930 | [diff] [blame] | 232 | |
Andrew Jeffery | 8650187 | 2022-04-05 16:58:22 +0930 | [diff] [blame] | 233 | return std::to_string(std::hash<std::string>{}(probeName + device.dump())); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | PerformScan::PerformScan(nlohmann::json& systemConfiguration, |
| 237 | nlohmann::json& missingConfigurations, |
| 238 | std::list<nlohmann::json>& configurations, |
| 239 | sdbusplus::asio::object_server& objServerIn, |
| 240 | std::function<void()>&& callback) : |
| 241 | _systemConfiguration(systemConfiguration), |
| 242 | _missingConfigurations(missingConfigurations), |
| 243 | _configurations(configurations), objServer(objServerIn), |
| 244 | _callback(std::move(callback)) |
| 245 | {} |
| 246 | void PerformScan::run() |
| 247 | { |
| 248 | boost::container::flat_set<std::string> dbusProbeInterfaces; |
| 249 | std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers; |
| 250 | |
| 251 | for (auto it = _configurations.begin(); it != _configurations.end();) |
| 252 | { |
| 253 | auto findProbe = it->find("Probe"); |
| 254 | auto findName = it->find("Name"); |
| 255 | |
| 256 | nlohmann::json probeCommand; |
| 257 | // check for poorly formatted fields, probe must be an array |
| 258 | if (findProbe == it->end()) |
| 259 | { |
| 260 | std::cerr << "configuration file missing probe:\n " << *it << "\n"; |
| 261 | it = _configurations.erase(it); |
| 262 | continue; |
| 263 | } |
| 264 | if ((*findProbe).type() != nlohmann::json::value_t::array) |
| 265 | { |
| 266 | probeCommand = nlohmann::json::array(); |
| 267 | probeCommand.push_back(*findProbe); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | probeCommand = *findProbe; |
| 272 | } |
| 273 | |
| 274 | if (findName == it->end()) |
| 275 | { |
| 276 | std::cerr << "configuration file missing name:\n " << *it << "\n"; |
| 277 | it = _configurations.erase(it); |
| 278 | continue; |
| 279 | } |
| 280 | std::string probeName = *findName; |
| 281 | |
| 282 | if (std::find(passedProbes.begin(), passedProbes.end(), probeName) != |
| 283 | passedProbes.end()) |
| 284 | { |
| 285 | it = _configurations.erase(it); |
| 286 | continue; |
| 287 | } |
| 288 | nlohmann::json* recordPtr = &(*it); |
| 289 | |
| 290 | // store reference to this to children to makes sure we don't get |
| 291 | // destroyed too early |
| 292 | auto thisRef = shared_from_this(); |
| 293 | auto probePointer = std::make_shared<PerformProbe>( |
| 294 | probeCommand, thisRef, |
Andrew Jeffery | ac20bd9 | 2022-04-05 11:11:40 +0930 | [diff] [blame] | 295 | [&, recordPtr, |
Andrew Jeffery | f5772d2 | 2022-04-12 22:05:30 +0930 | [diff] [blame] | 296 | probeName](FoundDevices& foundDevices, |
Andrew Jeffery | ef5c843 | 2022-04-12 22:34:37 +0930 | [diff] [blame] | 297 | const MapperGetSubTreeResponse& dbusSubtree) { |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 298 | _passed = true; |
| 299 | std::set<nlohmann::json> usedNames; |
| 300 | passedProbes.push_back(probeName); |
| 301 | std::list<size_t> indexes(foundDevices.size()); |
| 302 | std::iota(indexes.begin(), indexes.end(), 1); |
| 303 | |
| 304 | size_t indexIdx = probeName.find('$'); |
| 305 | bool hasTemplateName = (indexIdx != std::string::npos); |
| 306 | |
| 307 | // copy over persisted configurations and make sure we remove |
| 308 | // indexes that are already used |
| 309 | for (auto itr = foundDevices.begin(); |
| 310 | itr != foundDevices.end();) |
| 311 | { |
| 312 | std::string recordName = |
Andrew Jeffery | f5772d2 | 2022-04-12 22:05:30 +0930 | [diff] [blame] | 313 | getRecordName(itr->interface, probeName); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 314 | |
| 315 | auto fromLastJson = lastJson.find(recordName); |
| 316 | if (fromLastJson != lastJson.end()) |
| 317 | { |
| 318 | auto findExposes = fromLastJson->find("Exposes"); |
| 319 | // delete nulls from any updates |
| 320 | if (findExposes != fromLastJson->end()) |
| 321 | { |
| 322 | auto copy = nlohmann::json::array(); |
| 323 | for (auto& expose : *findExposes) |
| 324 | { |
| 325 | if (expose.is_null()) |
| 326 | { |
| 327 | continue; |
| 328 | } |
| 329 | copy.emplace_back(expose); |
| 330 | } |
| 331 | *findExposes = copy; |
| 332 | } |
| 333 | |
| 334 | // keep user changes |
| 335 | _systemConfiguration[recordName] = *fromLastJson; |
| 336 | _missingConfigurations.erase(recordName); |
| 337 | itr = foundDevices.erase(itr); |
| 338 | if (hasTemplateName) |
| 339 | { |
| 340 | auto nameIt = fromLastJson->find("Name"); |
| 341 | if (nameIt == fromLastJson->end()) |
| 342 | { |
| 343 | std::cerr << "Last JSON Illegal\n"; |
| 344 | continue; |
| 345 | } |
| 346 | int index = 0; |
| 347 | auto str = |
| 348 | nameIt->get<std::string>().substr(indexIdx); |
| 349 | auto [p, ec] = std::from_chars( |
| 350 | str.data(), str.data() + str.size(), index); |
| 351 | if (ec != std::errc()) |
| 352 | { |
| 353 | continue; // non-numeric replacement |
| 354 | } |
| 355 | usedNames.insert(nameIt.value()); |
| 356 | auto usedIt = std::find(indexes.begin(), |
| 357 | indexes.end(), index); |
| 358 | |
| 359 | if (usedIt == indexes.end()) |
| 360 | { |
| 361 | continue; // less items now |
| 362 | } |
| 363 | indexes.erase(usedIt); |
| 364 | } |
| 365 | |
| 366 | continue; |
| 367 | } |
| 368 | itr++; |
| 369 | } |
| 370 | |
| 371 | std::optional<std::string> replaceStr; |
| 372 | |
Andrew Jeffery | 0a8de74 | 2022-04-12 22:32:01 +0930 | [diff] [blame] | 373 | DBusObject emptyObject; |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 374 | DBusInterface emptyInterface; |
Andrew Jeffery | 0a8de74 | 2022-04-12 22:32:01 +0930 | [diff] [blame] | 375 | emptyObject.emplace(std::string{}, emptyInterface); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 376 | |
Andrew Jeffery | 920a281 | 2022-04-12 22:17:32 +0930 | [diff] [blame] | 377 | for (const auto& [foundDevice, path] : foundDevices) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 378 | { |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 379 | // Need all interfaces on this path so that template |
| 380 | // substitutions can be done with any of the contained |
| 381 | // properties. If the probe that passed didn't use an |
| 382 | // interface, such as if it was just TRUE, then |
| 383 | // templateCharReplace will just get passed in an empty |
| 384 | // map. |
Andrew Jeffery | ef5c843 | 2022-04-12 22:34:37 +0930 | [diff] [blame] | 385 | auto ifacesIt = dbusSubtree.find(path); |
Andrew Jeffery | d45b1e7 | 2022-04-12 22:47:14 +0930 | [diff] [blame] | 386 | const DBusObject& dbusObject = |
| 387 | (ifacesIt == dbusSubtree.end()) ? emptyObject |
| 388 | : ifacesIt->second; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 389 | |
| 390 | nlohmann::json record = *recordPtr; |
| 391 | std::string recordName = |
| 392 | getRecordName(foundDevice, probeName); |
| 393 | size_t foundDeviceIdx = indexes.front(); |
| 394 | indexes.pop_front(); |
| 395 | |
| 396 | // check name first so we have no duplicate names |
| 397 | auto getName = record.find("Name"); |
| 398 | if (getName == record.end()) |
| 399 | { |
| 400 | std::cerr << "Record Missing Name! " << record.dump(); |
| 401 | continue; // this should be impossible at this level |
| 402 | } |
| 403 | |
| 404 | nlohmann::json copyForName = {{"Name", getName.value()}}; |
| 405 | nlohmann::json::iterator copyIt = copyForName.begin(); |
Andrew Jeffery | c6558f6 | 2022-04-05 15:39:31 +0930 | [diff] [blame] | 406 | std::optional<std::string> replaceVal = templateCharReplace( |
Andrew Jeffery | d45b1e7 | 2022-04-12 22:47:14 +0930 | [diff] [blame] | 407 | copyIt, dbusObject, foundDeviceIdx, replaceStr); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 408 | |
| 409 | if (!replaceStr && replaceVal) |
| 410 | { |
| 411 | if (usedNames.find(copyIt.value()) != usedNames.end()) |
| 412 | { |
| 413 | replaceStr = replaceVal; |
| 414 | copyForName = {{"Name", getName.value()}}; |
| 415 | copyIt = copyForName.begin(); |
Andrew Jeffery | d45b1e7 | 2022-04-12 22:47:14 +0930 | [diff] [blame] | 416 | templateCharReplace(copyIt, dbusObject, |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 417 | foundDeviceIdx, replaceStr); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (replaceStr) |
| 422 | { |
| 423 | std::cerr << "Duplicates found, replacing " |
| 424 | << *replaceStr |
| 425 | << " with found device index.\n Consider " |
| 426 | "fixing template to not have duplicates\n"; |
| 427 | } |
| 428 | |
| 429 | for (auto keyPair = record.begin(); keyPair != record.end(); |
| 430 | keyPair++) |
| 431 | { |
| 432 | if (keyPair.key() == "Name") |
| 433 | { |
| 434 | keyPair.value() = copyIt.value(); |
| 435 | usedNames.insert(copyIt.value()); |
| 436 | |
| 437 | continue; // already covered above |
| 438 | } |
Andrew Jeffery | d45b1e7 | 2022-04-12 22:47:14 +0930 | [diff] [blame] | 439 | templateCharReplace(keyPair, dbusObject, foundDeviceIdx, |
| 440 | replaceStr); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // insert into configuration temporarily to be able to |
| 444 | // reference ourselves |
| 445 | |
| 446 | _systemConfiguration[recordName] = record; |
| 447 | |
| 448 | auto findExpose = record.find("Exposes"); |
| 449 | if (findExpose == record.end()) |
| 450 | { |
| 451 | _systemConfiguration[recordName] = record; |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | for (auto& expose : *findExpose) |
| 456 | { |
| 457 | for (auto keyPair = expose.begin(); |
| 458 | keyPair != expose.end(); keyPair++) |
| 459 | { |
| 460 | |
Andrew Jeffery | d45b1e7 | 2022-04-12 22:47:14 +0930 | [diff] [blame] | 461 | templateCharReplace(keyPair, dbusObject, |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 462 | foundDeviceIdx, replaceStr); |
| 463 | |
| 464 | bool isBind = |
| 465 | boost::starts_with(keyPair.key(), "Bind"); |
| 466 | bool isDisable = keyPair.key() == "DisableNode"; |
| 467 | |
| 468 | // special cases |
| 469 | if (!(isBind || isDisable)) |
| 470 | { |
| 471 | continue; |
| 472 | } |
| 473 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 474 | std::vector<std::string> matches; |
| 475 | if (keyPair.value().type() == |
| 476 | nlohmann::json::value_t::string) |
| 477 | { |
| 478 | matches.emplace_back(keyPair.value()); |
| 479 | } |
Andrew Jeffery | 3c5f12b | 2022-04-07 09:49:15 +0930 | [diff] [blame] | 480 | else if (keyPair.value().type() == |
| 481 | nlohmann::json::value_t::array) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 482 | { |
| 483 | for (const auto& value : keyPair.value()) |
| 484 | { |
| 485 | if (value.type() != |
| 486 | nlohmann::json::value_t::string) |
| 487 | { |
| 488 | std::cerr << "Value is invalid type " |
| 489 | << value << "\n"; |
| 490 | break; |
| 491 | } |
| 492 | matches.emplace_back(value); |
| 493 | } |
| 494 | } |
Andrew Jeffery | 3c5f12b | 2022-04-07 09:49:15 +0930 | [diff] [blame] | 495 | else |
| 496 | { |
| 497 | std::cerr << "Value is invalid type " |
| 498 | << keyPair.key() << "\n"; |
| 499 | continue; |
| 500 | } |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 501 | |
| 502 | std::set<std::string> foundMatches; |
Andrew Jeffery | f518471 | 2022-03-25 13:38:07 +1030 | [diff] [blame] | 503 | for (auto& [configId, config] : |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 504 | _systemConfiguration.items()) |
| 505 | { |
| 506 | if (isDisable) |
| 507 | { |
| 508 | // don't disable ourselves |
Andrew Jeffery | f518471 | 2022-03-25 13:38:07 +1030 | [diff] [blame] | 509 | if (configId == recordName) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 510 | { |
| 511 | continue; |
| 512 | } |
| 513 | } |
Andrew Jeffery | f518471 | 2022-03-25 13:38:07 +1030 | [diff] [blame] | 514 | auto configListFind = config.find("Exposes"); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 515 | |
Andrew Jeffery | f518471 | 2022-03-25 13:38:07 +1030 | [diff] [blame] | 516 | if (configListFind == config.end() || |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 517 | configListFind->type() != |
| 518 | nlohmann::json::value_t::array) |
| 519 | { |
| 520 | continue; |
| 521 | } |
| 522 | for (auto& exposedObject : *configListFind) |
| 523 | { |
| 524 | auto matchIt = std::find_if( |
| 525 | matches.begin(), matches.end(), |
| 526 | [name = (exposedObject)["Name"] |
| 527 | .get<std::string>()]( |
| 528 | const std::string& s) { |
| 529 | return s == name; |
| 530 | }); |
| 531 | if (matchIt == matches.end()) |
| 532 | { |
| 533 | continue; |
| 534 | } |
| 535 | foundMatches.insert(*matchIt); |
| 536 | |
| 537 | if (isBind) |
| 538 | { |
| 539 | std::string bind = keyPair.key().substr( |
| 540 | sizeof("Bind") - 1); |
| 541 | |
| 542 | exposedObject["Status"] = "okay"; |
| 543 | expose[bind] = exposedObject; |
| 544 | } |
| 545 | else if (isDisable) |
| 546 | { |
| 547 | exposedObject["Status"] = "disabled"; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | if (foundMatches.size() != matches.size()) |
| 552 | { |
| 553 | std::cerr << "configuration file " |
| 554 | "dependency error, " |
| 555 | "could not find " |
| 556 | << keyPair.key() << " " |
| 557 | << keyPair.value() << "\n"; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | // overwrite ourselves with cleaned up version |
| 562 | _systemConfiguration[recordName] = record; |
| 563 | _missingConfigurations.erase(recordName); |
| 564 | } |
| 565 | }); |
| 566 | |
| 567 | // parse out dbus probes by discarding other probe types, store in a |
| 568 | // map |
| 569 | for (const nlohmann::json& probeJson : probeCommand) |
| 570 | { |
| 571 | const std::string* probe = probeJson.get_ptr<const std::string*>(); |
| 572 | if (probe == nullptr) |
| 573 | { |
| 574 | std::cerr << "Probe statement wasn't a string, can't parse"; |
| 575 | continue; |
| 576 | } |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 577 | if (findProbeType(probe->c_str())) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 578 | { |
| 579 | continue; |
| 580 | } |
| 581 | // syntax requires probe before first open brace |
| 582 | auto findStart = probe->find('('); |
| 583 | std::string interface = probe->substr(0, findStart); |
| 584 | dbusProbeInterfaces.emplace(interface); |
| 585 | dbusProbePointers.emplace_back(probePointer); |
| 586 | } |
| 587 | it++; |
| 588 | } |
| 589 | |
| 590 | // probe vector stores a shared_ptr to each PerformProbe that cares |
| 591 | // about a dbus interface |
| 592 | findDbusObjects(std::move(dbusProbePointers), |
| 593 | std::move(dbusProbeInterfaces), shared_from_this()); |
| 594 | if constexpr (debug) |
| 595 | { |
| 596 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | PerformScan::~PerformScan() |
| 601 | { |
| 602 | if (_passed) |
| 603 | { |
| 604 | auto nextScan = std::make_shared<PerformScan>( |
| 605 | _systemConfiguration, _missingConfigurations, _configurations, |
| 606 | objServer, std::move(_callback)); |
| 607 | nextScan->passedProbes = std::move(passedProbes); |
| 608 | nextScan->dbusProbeObjects = std::move(dbusProbeObjects); |
| 609 | nextScan->run(); |
| 610 | |
| 611 | if constexpr (debug) |
| 612 | { |
| 613 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 614 | } |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | _callback(); |
| 619 | |
| 620 | if constexpr (debug) |
| 621 | { |
| 622 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 623 | } |
| 624 | } |
| 625 | } |