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