James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [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 | 1fb9f3f | 2020-08-28 08:15:13 -0400 | [diff] [blame] | 16 | /// \file EntityManager.cpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 18 | #include "EntityManager.hpp" |
| 19 | |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 20 | #include "Overlay.hpp" |
| 21 | #include "Utils.hpp" |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 22 | #include "VariantVisitors.hpp" |
| 23 | |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 24 | #include <boost/algorithm/string/case_conv.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 25 | #include <boost/algorithm/string/classification.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 26 | #include <boost/algorithm/string/predicate.hpp> |
| 27 | #include <boost/algorithm/string/replace.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 28 | #include <boost/algorithm/string/split.hpp> |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 29 | #include <boost/asio/io_context.hpp> |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 30 | #include <boost/asio/steady_timer.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 31 | #include <boost/container/flat_map.hpp> |
| 32 | #include <boost/container/flat_set.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 33 | #include <boost/range/iterator_range.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 34 | #include <nlohmann/json.hpp> |
| 35 | #include <sdbusplus/asio/connection.hpp> |
| 36 | #include <sdbusplus/asio/object_server.hpp> |
| 37 | |
Igor Kononenko | 9fd87e5 | 2020-10-06 01:18:17 +0300 | [diff] [blame] | 38 | #include <charconv> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 39 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 40 | #include <fstream> |
| 41 | #include <iostream> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 42 | #include <regex> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 43 | #include <variant> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 44 | constexpr const char* configurationDirectory = PACKAGE_DIR "configurations"; |
| 45 | constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas"; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 46 | constexpr const char* tempConfigDir = "/tmp/configuration/"; |
| 47 | constexpr const char* lastConfiguration = "/tmp/configuration/last.json"; |
| 48 | constexpr const char* currentConfiguration = "/var/configuration/system.json"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 49 | constexpr const char* globalSchema = "global.json"; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 50 | constexpr const int32_t maxMapperDepth = 0; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 51 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 52 | constexpr const bool debug = false; |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 53 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 54 | struct CmpStr |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 55 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 56 | bool operator()(const char* a, const char* b) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 57 | { |
| 58 | return std::strcmp(a, b) < 0; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | // underscore T for collison with dbus c api |
| 63 | enum class probe_type_codes |
| 64 | { |
| 65 | FALSE_T, |
| 66 | TRUE_T, |
| 67 | AND, |
| 68 | OR, |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 69 | FOUND, |
| 70 | MATCH_ONE |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 71 | }; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 72 | const static boost::container::flat_map<const char*, probe_type_codes, CmpStr> |
| 73 | probeTypes{{{"FALSE", probe_type_codes::FALSE_T}, |
| 74 | {"TRUE", probe_type_codes::TRUE_T}, |
| 75 | {"AND", probe_type_codes::AND}, |
| 76 | {"OR", probe_type_codes::OR}, |
| 77 | {"FOUND", probe_type_codes::FOUND}, |
| 78 | {"MATCH_ONE", probe_type_codes::MATCH_ONE}}}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 79 | |
Adrian Ambrożewicz | c789fca | 2020-05-14 15:50:05 +0200 | [diff] [blame] | 80 | static constexpr std::array<const char*, 6> settableInterfaces = { |
| 81 | "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"}; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 82 | using JsonVariantType = |
James Feist | 338b8a7 | 2019-03-01 10:16:45 -0800 | [diff] [blame] | 83 | std::variant<std::vector<std::string>, std::vector<double>, std::string, |
| 84 | int64_t, uint64_t, double, int32_t, uint32_t, int16_t, |
| 85 | uint16_t, uint8_t, bool>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 86 | using GetSubTreeType = std::vector< |
| 87 | std::pair<std::string, |
| 88 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 89 | |
| 90 | using ManagedObjectType = boost::container::flat_map< |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 91 | sdbusplus::message::object_path, |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 92 | boost::container::flat_map< |
| 93 | std::string, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 94 | boost::container::flat_map<std::string, BasicVariantType>>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 95 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 96 | // store reference to all interfaces so we can destroy them later |
| 97 | boost::container::flat_map< |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 98 | std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>> |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 99 | inventory; |
| 100 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 101 | // todo: pass this through nicer |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 102 | std::shared_ptr<sdbusplus::asio::connection> systemBus; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 103 | static nlohmann::json lastJson; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 104 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 105 | boost::asio::io_context io; |
| 106 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 107 | const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]"); |
| 108 | const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 109 | |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 110 | void registerCallback(nlohmann::json& systemConfiguration, |
| 111 | sdbusplus::asio::object_server& objServer, |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 112 | const std::string& path); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 113 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 114 | static std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 115 | createInterface(sdbusplus::asio::object_server& objServer, |
| 116 | const std::string& path, const std::string& interface, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 117 | const std::string& parent, bool checkNull = false) |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 118 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 119 | // on first add we have no reason to check for null before add, as there |
| 120 | // won't be any. For dynamically added interfaces, we check for null so that |
| 121 | // a constant delete/add will not create a memory leak |
| 122 | |
| 123 | auto ptr = objServer.add_interface(path, interface); |
| 124 | auto& dataVector = inventory[parent]; |
| 125 | if (checkNull) |
| 126 | { |
| 127 | auto it = std::find_if(dataVector.begin(), dataVector.end(), |
| 128 | [](const auto& p) { return p.expired(); }); |
| 129 | if (it != dataVector.end()) |
| 130 | { |
| 131 | *it = ptr; |
| 132 | return ptr; |
| 133 | } |
| 134 | } |
| 135 | dataVector.emplace_back(ptr); |
| 136 | return ptr; |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 137 | } |
| 138 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 139 | void getInterfaces( |
| 140 | const std::tuple<std::string, std::string, std::string>& call, |
| 141 | const std::vector<std::shared_ptr<PerformProbe>>& probeVector, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 142 | const std::shared_ptr<PerformScan>& scan, size_t retries = 5) |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 143 | { |
| 144 | if (!retries) |
| 145 | { |
| 146 | std::cerr << "retries exhausted on " << std::get<0>(call) << " " |
| 147 | << std::get<1>(call) << " " << std::get<2>(call) << "\n"; |
| 148 | return; |
| 149 | } |
| 150 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 151 | systemBus->async_method_call( |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 152 | [call, scan, probeVector, retries]( |
| 153 | boost::system::error_code& errc, |
| 154 | const boost::container::flat_map<std::string, BasicVariantType>& |
| 155 | resp) { |
| 156 | if (errc) |
| 157 | { |
| 158 | std::cerr << "error calling getall on " << std::get<0>(call) |
| 159 | << " " << std::get<1>(call) << " " |
| 160 | << std::get<2>(call) << "\n"; |
| 161 | |
| 162 | std::shared_ptr<boost::asio::steady_timer> timer = |
| 163 | std::make_shared<boost::asio::steady_timer>(io); |
| 164 | timer->expires_after(std::chrono::seconds(2)); |
| 165 | |
| 166 | timer->async_wait([timer, call, scan, probeVector, |
| 167 | retries](const boost::system::error_code&) { |
| 168 | getInterfaces(call, probeVector, scan, retries - 1); |
| 169 | }); |
| 170 | return; |
| 171 | } |
| 172 | |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 173 | scan->dbusProbeObjects[std::get<1>(call)][std::get<2>(call)] = resp; |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 174 | }, |
| 175 | std::get<0>(call), std::get<1>(call), "org.freedesktop.DBus.Properties", |
| 176 | "GetAll", std::get<2>(call)); |
| 177 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 178 | if constexpr (debug) |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 179 | { |
| 180 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 181 | } |
| 182 | } |
| 183 | |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 184 | // Populates scan->dbusProbeObjects with all interfaces and properties |
| 185 | // for the paths that own the interfaces passed in. |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 186 | void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector, |
| 187 | boost::container::flat_set<std::string>&& interfaces, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 188 | const std::shared_ptr<PerformScan>& scan, |
| 189 | size_t retries = 5) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 190 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 191 | // Filter out interfaces already obtained. |
| 192 | for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects) |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 193 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 194 | for (const auto& [interface, _] : probeInterfaces) |
| 195 | { |
| 196 | interfaces.erase(interface); |
| 197 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 198 | } |
| 199 | if (interfaces.empty()) |
| 200 | { |
| 201 | return; |
| 202 | } |
| 203 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 204 | // find all connections in the mapper that expose a specific type |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 205 | systemBus->async_method_call( |
Matt Spinler | 4f32889 | 2021-02-24 13:19:14 -0600 | [diff] [blame] | 206 | [interfaces, probeVector{std::move(probeVector)}, scan, |
| 207 | retries](boost::system::error_code& ec, |
| 208 | const GetSubTreeType& interfaceSubtree) mutable { |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 209 | boost::container::flat_set< |
| 210 | std::tuple<std::string, std::string, std::string>> |
| 211 | interfaceConnections; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 212 | if (ec) |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 213 | { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 214 | if (ec.value() == ENOENT) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 215 | { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 216 | return; // wasn't found by mapper |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 217 | } |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 218 | std::cerr << "Error communicating to mapper.\n"; |
| 219 | |
James Feist | 32d1f0a | 2020-09-10 14:49:25 -0700 | [diff] [blame] | 220 | if (!retries) |
| 221 | { |
| 222 | // if we can't communicate to the mapper something is very |
| 223 | // wrong |
| 224 | std::exit(EXIT_FAILURE); |
| 225 | } |
| 226 | std::shared_ptr<boost::asio::steady_timer> timer = |
| 227 | std::make_shared<boost::asio::steady_timer>(io); |
| 228 | timer->expires_after(std::chrono::seconds(10)); |
| 229 | |
| 230 | timer->async_wait( |
| 231 | [timer, interfaces{std::move(interfaces)}, scan, |
| 232 | probeVector{std::move(probeVector)}, |
| 233 | retries](const boost::system::error_code&) mutable { |
| 234 | findDbusObjects(std::move(probeVector), |
| 235 | std::move(interfaces), scan, |
| 236 | retries - 1); |
| 237 | }); |
| 238 | return; |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 239 | } |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 240 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 241 | for (const auto& [path, object] : interfaceSubtree) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 242 | { |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 243 | for (const auto& [busname, ifaces] : object) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 244 | { |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 245 | for (const std::string& iface : ifaces) |
| 246 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 247 | // The 3 default org.freedeskstop interfaces (Peer, |
| 248 | // Introspectable, and Properties) are returned by |
| 249 | // the mapper but don't have properties, so don't bother |
| 250 | // with the GetAll call to save some cycles. |
| 251 | if (!boost::algorithm::starts_with(iface, |
| 252 | "org.freedesktop")) |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 253 | { |
| 254 | interfaceConnections.emplace(busname, path, iface); |
| 255 | } |
| 256 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 257 | } |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 258 | |
| 259 | // Get a PropertiesChanged callback for all |
| 260 | // interfaces on this path. |
| 261 | registerCallback(scan->_systemConfiguration, scan->objServer, |
| 262 | path); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 263 | } |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 264 | |
James Feist | 63845bf | 2019-01-24 12:19:51 -0800 | [diff] [blame] | 265 | if (interfaceConnections.empty()) |
| 266 | { |
James Feist | 63845bf | 2019-01-24 12:19:51 -0800 | [diff] [blame] | 267 | return; |
| 268 | } |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 269 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 270 | for (const auto& call : interfaceConnections) |
| 271 | { |
| 272 | getInterfaces(call, probeVector, scan); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 273 | } |
| 274 | }, |
| 275 | "xyz.openbmc_project.ObjectMapper", |
| 276 | "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 277 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth, |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 278 | interfaces); |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 279 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 280 | if constexpr (debug) |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 281 | { |
| 282 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 283 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 284 | } |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 285 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 286 | // probes dbus interface dictionary for a key with a value that matches a regex |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 287 | // When an interface passes a probe, also save its D-Bus path with it. |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 288 | bool probeDbus(const std::string& interface, |
| 289 | const std::map<std::string, nlohmann::json>& matches, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 290 | FoundDeviceT& devices, const std::shared_ptr<PerformScan>& scan, |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 291 | bool& foundProbe) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 292 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 293 | bool foundMatch = false; |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 294 | foundProbe = false; |
| 295 | |
| 296 | for (const auto& [path, interfaces] : scan->dbusProbeObjects) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 297 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 298 | auto it = interfaces.find(interface); |
| 299 | if (it == interfaces.end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 300 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 301 | continue; |
| 302 | } |
| 303 | |
| 304 | foundProbe = true; |
| 305 | |
| 306 | bool deviceMatches = true; |
| 307 | const boost::container::flat_map<std::string, BasicVariantType>& |
| 308 | properties = it->second; |
| 309 | |
| 310 | for (const auto& [matchProp, matchJSON] : matches) |
| 311 | { |
| 312 | auto deviceValue = properties.find(matchProp); |
| 313 | if (deviceValue != properties.end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 314 | { |
Jonathan Doman | 395c6d4 | 2021-05-04 19:25:10 -0700 | [diff] [blame] | 315 | deviceMatches = |
| 316 | deviceMatches && matchProbe(matchJSON, deviceValue->second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 317 | } |
| 318 | else |
| 319 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 320 | // Move on to the next DBus path |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 321 | deviceMatches = false; |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | if (deviceMatches) |
| 326 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 327 | if constexpr (debug) |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 328 | { |
| 329 | std::cerr << "probeDBus: Found probe match on " << path << " " |
| 330 | << interface << "\n"; |
| 331 | } |
| 332 | devices.emplace_back(properties, path); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 333 | foundMatch = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | return foundMatch; |
| 337 | } |
| 338 | |
| 339 | // default probe entry point, iterates a list looking for specific types to |
| 340 | // call specific probe functions |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 341 | bool probe(const std::vector<std::string>& probeCommand, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 342 | const std::shared_ptr<PerformScan>& scan, FoundDeviceT& foundDevs) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 343 | { |
| 344 | const static std::regex command(R"(\((.*)\))"); |
| 345 | std::smatch match; |
| 346 | bool ret = false; |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 347 | bool matchOne = false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 348 | bool cur = true; |
| 349 | probe_type_codes lastCommand = probe_type_codes::FALSE_T; |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 350 | bool first = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 351 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 352 | for (auto& probe : probeCommand) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 353 | { |
| 354 | bool foundProbe = false; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 355 | boost::container::flat_map<const char*, probe_type_codes, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 356 | CmpStr>::const_iterator probeType; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 357 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 358 | for (probeType = probeTypes.begin(); probeType != probeTypes.end(); |
Patrick Venture | 4b7a745 | 2019-10-28 08:41:02 -0700 | [diff] [blame] | 359 | ++probeType) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 360 | { |
| 361 | if (probe.find(probeType->first) != std::string::npos) |
| 362 | { |
| 363 | foundProbe = true; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | if (foundProbe) |
| 368 | { |
| 369 | switch (probeType->second) |
| 370 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 371 | case probe_type_codes::FALSE_T: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 372 | { |
James Feist | e31e00a | 2019-07-24 10:45:43 -0700 | [diff] [blame] | 373 | cur = false; |
| 374 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 375 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 376 | case probe_type_codes::TRUE_T: |
| 377 | { |
James Feist | e31e00a | 2019-07-24 10:45:43 -0700 | [diff] [blame] | 378 | cur = true; |
| 379 | break; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 380 | } |
| 381 | case probe_type_codes::MATCH_ONE: |
| 382 | { |
| 383 | // set current value to last, this probe type shouldn't |
| 384 | // affect the outcome |
| 385 | cur = ret; |
| 386 | matchOne = true; |
| 387 | break; |
| 388 | } |
| 389 | /*case probe_type_codes::AND: |
| 390 | break; |
| 391 | case probe_type_codes::OR: |
| 392 | break; |
| 393 | // these are no-ops until the last command switch |
| 394 | */ |
| 395 | case probe_type_codes::FOUND: |
| 396 | { |
| 397 | if (!std::regex_search(probe, match, command)) |
| 398 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 399 | std::cerr << "found probe syntax error " << probe |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 400 | << "\n"; |
| 401 | return false; |
| 402 | } |
| 403 | std::string commandStr = *(match.begin() + 1); |
| 404 | boost::replace_all(commandStr, "'", ""); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 405 | cur = (std::find(scan->passedProbes.begin(), |
| 406 | scan->passedProbes.end(), |
| 407 | commandStr) != scan->passedProbes.end()); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 408 | break; |
| 409 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 410 | default: |
| 411 | { |
| 412 | break; |
| 413 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | // look on dbus for object |
| 417 | else |
| 418 | { |
| 419 | if (!std::regex_search(probe, match, command)) |
| 420 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 421 | std::cerr << "dbus probe syntax error " << probe << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 422 | return false; |
| 423 | } |
| 424 | std::string commandStr = *(match.begin() + 1); |
| 425 | // convert single ticks and single slashes into legal json |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 426 | boost::replace_all(commandStr, "'", "\""); |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 427 | boost::replace_all(commandStr, R"(\)", R"(\\)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 428 | auto json = nlohmann::json::parse(commandStr, nullptr, false); |
| 429 | if (json.is_discarded()) |
| 430 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 431 | std::cerr << "dbus command syntax error " << commandStr << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 432 | return false; |
| 433 | } |
| 434 | // we can match any (string, variant) property. (string, string) |
| 435 | // does a regex |
| 436 | std::map<std::string, nlohmann::json> dbusProbeMap = |
| 437 | json.get<std::map<std::string, nlohmann::json>>(); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 438 | auto findStart = probe.find('('); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 439 | if (findStart == std::string::npos) |
| 440 | { |
| 441 | return false; |
| 442 | } |
| 443 | std::string probeInterface = probe.substr(0, findStart); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 444 | cur = probeDbus(probeInterface, dbusProbeMap, foundDevs, scan, |
| 445 | foundProbe); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // some functions like AND and OR only take affect after the |
| 449 | // fact |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 450 | if (lastCommand == probe_type_codes::AND) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 451 | { |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 452 | ret = cur && ret; |
| 453 | } |
| 454 | else if (lastCommand == probe_type_codes::OR) |
| 455 | { |
| 456 | ret = cur || ret; |
| 457 | } |
| 458 | |
| 459 | if (first) |
| 460 | { |
| 461 | ret = cur; |
| 462 | first = false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 463 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 464 | lastCommand = probeType != probeTypes.end() ? probeType->second |
| 465 | : probe_type_codes::FALSE_T; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // probe passed, but empty device |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 469 | if (ret && foundDevs.size() == 0) |
| 470 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 471 | foundDevs.emplace_back( |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 472 | boost::container::flat_map<std::string, BasicVariantType>{}, |
| 473 | std::string{}); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 474 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 475 | if (matchOne && ret) |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 476 | { |
James Feist | 71f295f | 2019-06-20 13:35:12 -0700 | [diff] [blame] | 477 | // match the last one |
| 478 | auto last = foundDevs.back(); |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 479 | foundDevs.clear(); |
James Feist | 71f295f | 2019-06-20 13:35:12 -0700 | [diff] [blame] | 480 | |
| 481 | foundDevs.emplace_back(std::move(last)); |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 482 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 483 | return ret; |
| 484 | } |
| 485 | |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 486 | PerformProbe::PerformProbe( |
| 487 | const std::vector<std::string>& probeCommand, |
| 488 | std::shared_ptr<PerformScan>& scanPtr, |
| 489 | std::function<void(FoundDeviceT&, const DBusProbeObjectT&)>&& callback) : |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 490 | _probeCommand(probeCommand), |
| 491 | scan(scanPtr), _callback(std::move(callback)) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 492 | {} |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 493 | PerformProbe::~PerformProbe() |
| 494 | { |
| 495 | FoundDeviceT foundDevs; |
| 496 | if (probe(_probeCommand, scan, foundDevs)) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 497 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 498 | _callback(foundDevs, scan->dbusProbeObjects); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 499 | } |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 500 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 501 | |
| 502 | // writes output files to persist data |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 503 | bool writeJsonFiles(const nlohmann::json& systemConfiguration) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 504 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 505 | std::filesystem::create_directory(configurationOutDir); |
| 506 | std::ofstream output(currentConfiguration); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 507 | if (!output.good()) |
| 508 | { |
| 509 | return false; |
| 510 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 511 | output << systemConfiguration.dump(4); |
| 512 | output.close(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 513 | return true; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 514 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 515 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 516 | template <typename JsonType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 517 | bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value, |
| 518 | nlohmann::json& systemConfiguration) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 519 | { |
| 520 | try |
| 521 | { |
| 522 | nlohmann::json::json_pointer ptr(ptrStr); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 523 | nlohmann::json& ref = systemConfiguration[ptr]; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 524 | ref = value; |
| 525 | return true; |
| 526 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 527 | catch (const std::out_of_range&) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 528 | { |
| 529 | return false; |
| 530 | } |
| 531 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 532 | |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 533 | // template function to add array as dbus property |
| 534 | template <typename PropertyType> |
| 535 | void addArrayToDbus(const std::string& name, const nlohmann::json& array, |
| 536 | sdbusplus::asio::dbus_interface* iface, |
| 537 | sdbusplus::asio::PropertyPermission permission, |
| 538 | nlohmann::json& systemConfiguration, |
| 539 | const std::string& jsonPointerString) |
| 540 | { |
| 541 | std::vector<PropertyType> values; |
| 542 | for (const auto& property : array) |
| 543 | { |
| 544 | auto ptr = property.get_ptr<const PropertyType*>(); |
| 545 | if (ptr != nullptr) |
| 546 | { |
| 547 | values.emplace_back(*ptr); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 552 | { |
| 553 | iface->register_property(name, values); |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | iface->register_property( |
| 558 | name, values, |
| 559 | [&systemConfiguration, |
| 560 | jsonPointerString{std::string(jsonPointerString)}]( |
| 561 | const std::vector<PropertyType>& newVal, |
| 562 | std::vector<PropertyType>& val) { |
| 563 | val = newVal; |
| 564 | if (!setJsonFromPointer(jsonPointerString, val, |
| 565 | systemConfiguration)) |
| 566 | { |
| 567 | std::cerr << "error setting json field\n"; |
| 568 | return -1; |
| 569 | } |
| 570 | if (!writeJsonFiles(systemConfiguration)) |
| 571 | { |
| 572 | std::cerr << "error setting json file\n"; |
| 573 | return -1; |
| 574 | } |
| 575 | return 1; |
| 576 | }); |
| 577 | } |
| 578 | } |
| 579 | |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 580 | template <typename PropertyType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 581 | void addProperty(const std::string& propertyName, const PropertyType& value, |
| 582 | sdbusplus::asio::dbus_interface* iface, |
| 583 | nlohmann::json& systemConfiguration, |
| 584 | const std::string& jsonPointerString, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 585 | sdbusplus::asio::PropertyPermission permission) |
| 586 | { |
| 587 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 588 | { |
| 589 | iface->register_property(propertyName, value); |
| 590 | return; |
| 591 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 592 | iface->register_property( |
| 593 | propertyName, value, |
| 594 | [&systemConfiguration, |
| 595 | jsonPointerString{std::string(jsonPointerString)}]( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 596 | const PropertyType& newVal, PropertyType& val) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 597 | val = newVal; |
| 598 | if (!setJsonFromPointer(jsonPointerString, val, |
| 599 | systemConfiguration)) |
| 600 | { |
| 601 | std::cerr << "error setting json field\n"; |
| 602 | return -1; |
| 603 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 604 | if (!writeJsonFiles(systemConfiguration)) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 605 | { |
| 606 | std::cerr << "error setting json file\n"; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 607 | return -1; |
| 608 | } |
| 609 | return 1; |
| 610 | }); |
| 611 | } |
| 612 | |
| 613 | void createDeleteObjectMethod( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 614 | const std::string& jsonPointerPath, |
| 615 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 616 | sdbusplus::asio::object_server& objServer, |
| 617 | nlohmann::json& systemConfiguration) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 618 | { |
| 619 | std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface; |
| 620 | iface->register_method( |
| 621 | "Delete", [&objServer, &systemConfiguration, interface, |
| 622 | jsonPointerPath{std::string(jsonPointerPath)}]() { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 623 | std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface = |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 624 | interface.lock(); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 625 | if (!dbusInterface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 626 | { |
| 627 | // this technically can't happen as the pointer is pointing to |
| 628 | // us |
| 629 | throw DBusInternalError(); |
| 630 | } |
| 631 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 632 | systemConfiguration[ptr] = nullptr; |
| 633 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 634 | // todo(james): dig through sdbusplus to find out why we can't |
| 635 | // delete it in a method call |
| 636 | io.post([&objServer, dbusInterface]() mutable { |
| 637 | objServer.remove_interface(dbusInterface); |
| 638 | }); |
| 639 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 640 | if (!writeJsonFiles(systemConfiguration)) |
| 641 | { |
| 642 | std::cerr << "error setting json file\n"; |
| 643 | throw DBusInternalError(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 644 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 645 | }); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 646 | } |
| 647 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 648 | // adds simple json types to interface's properties |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 649 | void populateInterfaceFromJson( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 650 | nlohmann::json& systemConfiguration, const std::string& jsonPointerPath, |
| 651 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 652 | nlohmann::json& dict, sdbusplus::asio::object_server& objServer, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 653 | sdbusplus::asio::PropertyPermission permission = |
| 654 | sdbusplus::asio::PropertyPermission::readOnly) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 655 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 656 | for (auto& dictPair : dict.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 657 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 658 | auto type = dictPair.value().type(); |
| 659 | bool array = false; |
| 660 | if (dictPair.value().type() == nlohmann::json::value_t::array) |
| 661 | { |
| 662 | array = true; |
| 663 | if (!dictPair.value().size()) |
| 664 | { |
| 665 | continue; |
| 666 | } |
| 667 | type = dictPair.value()[0].type(); |
| 668 | bool isLegal = true; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 669 | for (const auto& arrayItem : dictPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 670 | { |
| 671 | if (arrayItem.type() != type) |
| 672 | { |
| 673 | isLegal = false; |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | if (!isLegal) |
| 678 | { |
| 679 | std::cerr << "dbus format error" << dictPair.value() << "\n"; |
| 680 | continue; |
| 681 | } |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 682 | } |
| 683 | if (type == nlohmann::json::value_t::object) |
| 684 | { |
| 685 | continue; // handled elsewhere |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 686 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 687 | std::string key = jsonPointerPath + "/" + dictPair.key(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 688 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 689 | { |
| 690 | // all setable numbers are doubles as it is difficult to always |
| 691 | // create a configuration file with all whole numbers as decimals |
| 692 | // i.e. 1.0 |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 693 | if (array) |
| 694 | { |
| 695 | if (dictPair.value()[0].is_number()) |
| 696 | { |
| 697 | type = nlohmann::json::value_t::number_float; |
| 698 | } |
| 699 | } |
| 700 | else if (dictPair.value().is_number()) |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 701 | { |
| 702 | type = nlohmann::json::value_t::number_float; |
| 703 | } |
| 704 | } |
| 705 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 706 | switch (type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 707 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 708 | case (nlohmann::json::value_t::boolean): |
| 709 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 710 | if (array) |
| 711 | { |
| 712 | // todo: array of bool isn't detected correctly by |
| 713 | // sdbusplus, change it to numbers |
| 714 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 715 | iface.get(), permission, |
| 716 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 717 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 718 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 719 | else |
| 720 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 721 | addProperty(dictPair.key(), dictPair.value().get<bool>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 722 | iface.get(), systemConfiguration, key, |
| 723 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 724 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 725 | break; |
| 726 | } |
| 727 | case (nlohmann::json::value_t::number_integer): |
| 728 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 729 | if (array) |
| 730 | { |
| 731 | addArrayToDbus<int64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 732 | iface.get(), permission, |
| 733 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 734 | } |
| 735 | else |
| 736 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 737 | addProperty(dictPair.key(), dictPair.value().get<int64_t>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 738 | iface.get(), systemConfiguration, key, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 739 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 740 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | case (nlohmann::json::value_t::number_unsigned): |
| 744 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 745 | if (array) |
| 746 | { |
| 747 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 748 | iface.get(), permission, |
| 749 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 750 | } |
| 751 | else |
| 752 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 753 | addProperty(dictPair.key(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 754 | dictPair.value().get<uint64_t>(), iface.get(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 755 | systemConfiguration, key, |
| 756 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 757 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 758 | break; |
| 759 | } |
| 760 | case (nlohmann::json::value_t::number_float): |
| 761 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 762 | if (array) |
| 763 | { |
| 764 | addArrayToDbus<double>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 765 | iface.get(), permission, |
| 766 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 767 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 768 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 769 | else |
| 770 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 771 | addProperty(dictPair.key(), dictPair.value().get<double>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 772 | iface.get(), systemConfiguration, key, |
| 773 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 774 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 775 | break; |
| 776 | } |
| 777 | case (nlohmann::json::value_t::string): |
| 778 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 779 | if (array) |
| 780 | { |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 781 | addArrayToDbus<std::string>( |
| 782 | dictPair.key(), dictPair.value(), iface.get(), |
| 783 | permission, systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 784 | } |
| 785 | else |
| 786 | { |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 787 | addProperty( |
| 788 | dictPair.key(), dictPair.value().get<std::string>(), |
| 789 | iface.get(), systemConfiguration, key, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 790 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 791 | break; |
| 792 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 793 | default: |
| 794 | { |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 795 | std::cerr << "Unexpected json type in system configuration " |
| 796 | << dictPair.key() << ": " |
| 797 | << dictPair.value().type_name() << "\n"; |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 798 | break; |
| 799 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 800 | } |
| 801 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 802 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 803 | { |
| 804 | createDeleteObjectMethod(jsonPointerPath, iface, objServer, |
| 805 | systemConfiguration); |
| 806 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 807 | iface->initialize(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 808 | } |
| 809 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 810 | sdbusplus::asio::PropertyPermission getPermission(const std::string& interface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 811 | { |
| 812 | return std::find(settableInterfaces.begin(), settableInterfaces.end(), |
| 813 | interface) != settableInterfaces.end() |
| 814 | ? sdbusplus::asio::PropertyPermission::readWrite |
| 815 | : sdbusplus::asio::PropertyPermission::readOnly; |
| 816 | } |
| 817 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 818 | void createAddObjectMethod(const std::string& jsonPointerPath, |
| 819 | const std::string& path, |
| 820 | nlohmann::json& systemConfiguration, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 821 | sdbusplus::asio::object_server& objServer, |
| 822 | const std::string& board) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 823 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 824 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface( |
| 825 | objServer, path, "xyz.openbmc_project.AddObject", board); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 826 | |
| 827 | iface->register_method( |
| 828 | "AddObject", |
| 829 | [&systemConfiguration, &objServer, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 830 | jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)}, |
| 831 | board](const boost::container::flat_map<std::string, JsonVariantType>& |
| 832 | data) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 833 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 834 | nlohmann::json& base = systemConfiguration[ptr]; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 835 | auto findExposes = base.find("Exposes"); |
| 836 | |
| 837 | if (findExposes == base.end()) |
| 838 | { |
| 839 | throw std::invalid_argument("Entity must have children."); |
| 840 | } |
| 841 | |
| 842 | // this will throw invalid-argument to sdbusplus if invalid json |
| 843 | nlohmann::json newData{}; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 844 | for (const auto& item : data) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 845 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 846 | nlohmann::json& newJson = newData[item.first]; |
| 847 | std::visit([&newJson](auto&& val) { newJson = std::move(val); }, |
| 848 | item.second); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | auto findName = newData.find("Name"); |
| 852 | auto findType = newData.find("Type"); |
| 853 | if (findName == newData.end() || findType == newData.end()) |
| 854 | { |
| 855 | throw std::invalid_argument("AddObject missing Name or Type"); |
| 856 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 857 | const std::string* type = findType->get_ptr<const std::string*>(); |
| 858 | const std::string* name = findName->get_ptr<const std::string*>(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 859 | if (type == nullptr || name == nullptr) |
| 860 | { |
| 861 | throw std::invalid_argument("Type and Name must be a string."); |
| 862 | } |
| 863 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 864 | bool foundNull = false; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 865 | size_t lastIndex = 0; |
| 866 | // we add in the "exposes" |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 867 | for (const auto& expose : *findExposes) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 868 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 869 | if (expose.is_null()) |
| 870 | { |
| 871 | foundNull = true; |
| 872 | continue; |
| 873 | } |
| 874 | |
| 875 | if (expose["Name"] == *name && expose["Type"] == *type) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 876 | { |
| 877 | throw std::invalid_argument( |
| 878 | "Field already in JSON, not adding"); |
| 879 | } |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 880 | |
| 881 | if (foundNull) |
| 882 | { |
| 883 | continue; |
| 884 | } |
| 885 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 886 | lastIndex++; |
| 887 | } |
| 888 | |
| 889 | std::ifstream schemaFile(std::string(schemaDirectory) + "/" + |
| 890 | *type + ".json"); |
| 891 | // todo(james) we might want to also make a list of 'can add' |
| 892 | // interfaces but for now I think the assumption if there is a |
| 893 | // schema avaliable that it is allowed to update is fine |
| 894 | if (!schemaFile.good()) |
| 895 | { |
| 896 | throw std::invalid_argument( |
| 897 | "No schema avaliable, cannot validate."); |
| 898 | } |
| 899 | nlohmann::json schema = |
| 900 | nlohmann::json::parse(schemaFile, nullptr, false); |
| 901 | if (schema.is_discarded()) |
| 902 | { |
| 903 | std::cerr << "Schema not legal" << *type << ".json\n"; |
| 904 | throw DBusInternalError(); |
| 905 | } |
| 906 | if (!validateJson(schema, newData)) |
| 907 | { |
| 908 | throw std::invalid_argument("Data does not match schema"); |
| 909 | } |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 910 | if (foundNull) |
| 911 | { |
| 912 | findExposes->at(lastIndex) = newData; |
| 913 | } |
| 914 | else |
| 915 | { |
| 916 | findExposes->push_back(newData); |
| 917 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 918 | if (!writeJsonFiles(systemConfiguration)) |
| 919 | { |
| 920 | std::cerr << "Error writing json files\n"; |
| 921 | throw DBusInternalError(); |
| 922 | } |
| 923 | std::string dbusName = *name; |
| 924 | |
| 925 | std::regex_replace(dbusName.begin(), dbusName.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 926 | dbusName.end(), illegalDbusMemberRegex, "_"); |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 927 | |
| 928 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 929 | createInterface(objServer, path + "/" + dbusName, |
| 930 | "xyz.openbmc_project.Configuration." + *type, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 931 | board, true); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 932 | // permission is read-write, as since we just created it, must be |
| 933 | // runtime modifiable |
| 934 | populateInterfaceFromJson( |
| 935 | systemConfiguration, |
James Feist | 16a02f2 | 2019-05-13 15:21:37 -0700 | [diff] [blame] | 936 | jsonPointerPath + "/Exposes/" + std::to_string(lastIndex), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 937 | interface, newData, objServer, |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 938 | sdbusplus::asio::PropertyPermission::readWrite); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 939 | }); |
| 940 | iface->initialize(); |
| 941 | } |
| 942 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 943 | void postToDbus(const nlohmann::json& newConfiguration, |
| 944 | nlohmann::json& systemConfiguration, |
| 945 | sdbusplus::asio::object_server& objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 946 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 947 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 948 | // iterate through boards |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 949 | for (auto& boardPair : newConfiguration.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 950 | { |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 951 | std::string boardKey = boardPair.value()["Name"]; |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 952 | std::string boardKeyOrig = boardPair.value()["Name"]; |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 953 | std::string jsonPointerPath = "/" + boardPair.key(); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 954 | // loop through newConfiguration, but use values from system |
| 955 | // configuration to be able to modify via dbus later |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 956 | auto boardValues = systemConfiguration[boardPair.key()]; |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 957 | auto findBoardType = boardValues.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 958 | std::string boardType; |
| 959 | if (findBoardType != boardValues.end() && |
| 960 | findBoardType->type() == nlohmann::json::value_t::string) |
| 961 | { |
| 962 | boardType = findBoardType->get<std::string>(); |
| 963 | std::regex_replace(boardType.begin(), boardType.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 964 | boardType.end(), illegalDbusMemberRegex, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 965 | } |
| 966 | else |
| 967 | { |
| 968 | std::cerr << "Unable to find type for " << boardKey |
| 969 | << " reverting to Chassis.\n"; |
| 970 | boardType = "Chassis"; |
| 971 | } |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 972 | std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 973 | |
| 974 | std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 975 | illegalDbusMemberRegex, "_"); |
| 976 | std::string boardName = "/xyz/openbmc_project/inventory/system/"; |
| 977 | boardName += boardtypeLower; |
| 978 | boardName += "/"; |
| 979 | boardName += boardKey; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 980 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 981 | std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface = |
| 982 | createInterface(objServer, boardName, |
| 983 | "xyz.openbmc_project.Inventory.Item", boardKey); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 984 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 985 | std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface = |
| 986 | createInterface(objServer, boardName, |
| 987 | "xyz.openbmc_project.Inventory.Item." + boardType, |
| 988 | boardKeyOrig); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 989 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 990 | createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 991 | objServer, boardKeyOrig); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 992 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 993 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 994 | boardIface, boardValues, objServer); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 995 | jsonPointerPath += "/"; |
| 996 | // iterate through board properties |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 997 | for (auto& boardField : boardValues.items()) |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 998 | { |
| 999 | if (boardField.value().type() == nlohmann::json::value_t::object) |
| 1000 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1001 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1002 | createInterface(objServer, boardName, boardField.key(), |
| 1003 | boardKeyOrig); |
| 1004 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1005 | populateInterfaceFromJson(systemConfiguration, |
| 1006 | jsonPointerPath + boardField.key(), |
| 1007 | iface, boardField.value(), objServer); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1010 | |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 1011 | auto exposes = boardValues.find("Exposes"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1012 | if (exposes == boardValues.end()) |
| 1013 | { |
| 1014 | continue; |
| 1015 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1016 | // iterate through exposes |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 1017 | jsonPointerPath += "Exposes/"; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1018 | |
| 1019 | // store the board level pointer so we can modify it on the way down |
| 1020 | std::string jsonPointerPathBoard = jsonPointerPath; |
| 1021 | size_t exposesIndex = -1; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1022 | for (auto& item : *exposes) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1023 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1024 | exposesIndex++; |
| 1025 | jsonPointerPath = jsonPointerPathBoard; |
| 1026 | jsonPointerPath += std::to_string(exposesIndex); |
| 1027 | |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1028 | auto findName = item.find("Name"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1029 | if (findName == item.end()) |
| 1030 | { |
| 1031 | std::cerr << "cannot find name in field " << item << "\n"; |
| 1032 | continue; |
| 1033 | } |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 1034 | auto findStatus = item.find("Status"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1035 | // if status is not found it is assumed to be status = 'okay' |
| 1036 | if (findStatus != item.end()) |
| 1037 | { |
| 1038 | if (*findStatus == "disabled") |
| 1039 | { |
| 1040 | continue; |
| 1041 | } |
| 1042 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1043 | auto findType = item.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1044 | std::string itemType; |
| 1045 | if (findType != item.end()) |
| 1046 | { |
| 1047 | itemType = findType->get<std::string>(); |
| 1048 | std::regex_replace(itemType.begin(), itemType.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1049 | itemType.end(), illegalDbusPathRegex, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | itemType = "unknown"; |
| 1054 | } |
| 1055 | std::string itemName = findName->get<std::string>(); |
| 1056 | std::regex_replace(itemName.begin(), itemName.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1057 | itemName.end(), illegalDbusMemberRegex, "_"); |
| 1058 | std::string ifacePath = boardName; |
| 1059 | ifacePath += "/"; |
| 1060 | ifacePath += itemName; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1061 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1062 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface = |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1063 | createInterface(objServer, ifacePath, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1064 | "xyz.openbmc_project.Configuration." + itemType, |
| 1065 | boardKeyOrig); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1066 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1067 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1068 | itemIface, item, objServer, |
| 1069 | getPermission(itemType)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1070 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1071 | for (auto& objectPair : item.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1072 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1073 | jsonPointerPath = jsonPointerPathBoard + |
| 1074 | std::to_string(exposesIndex) + "/" + |
| 1075 | objectPair.key(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1076 | if (objectPair.value().type() == |
| 1077 | nlohmann::json::value_t::object) |
| 1078 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1079 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 1080 | objectIface = createInterface( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1081 | objServer, ifacePath, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1082 | "xyz.openbmc_project.Configuration." + itemType + |
| 1083 | "." + objectPair.key(), |
| 1084 | boardKeyOrig); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1085 | |
Adrian Ambrożewicz | c789fca | 2020-05-14 15:50:05 +0200 | [diff] [blame] | 1086 | populateInterfaceFromJson(systemConfiguration, |
| 1087 | jsonPointerPath, objectIface, |
| 1088 | objectPair.value(), objServer, |
| 1089 | getPermission(objectPair.key())); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1090 | } |
| 1091 | else if (objectPair.value().type() == |
| 1092 | nlohmann::json::value_t::array) |
| 1093 | { |
| 1094 | size_t index = 0; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1095 | if (!objectPair.value().size()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1096 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1097 | continue; |
| 1098 | } |
| 1099 | bool isLegal = true; |
| 1100 | auto type = objectPair.value()[0].type(); |
| 1101 | if (type != nlohmann::json::value_t::object) |
| 1102 | { |
| 1103 | continue; |
| 1104 | } |
| 1105 | |
| 1106 | // verify legal json |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1107 | for (const auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1108 | { |
| 1109 | if (arrayItem.type() != type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1110 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1111 | isLegal = false; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1112 | break; |
| 1113 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1114 | } |
| 1115 | if (!isLegal) |
| 1116 | { |
| 1117 | std::cerr << "dbus format error" << objectPair.value() |
| 1118 | << "\n"; |
| 1119 | break; |
| 1120 | } |
| 1121 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1122 | for (auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1123 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1124 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1125 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 1126 | objectIface = createInterface( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1127 | objServer, ifacePath, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1128 | "xyz.openbmc_project.Configuration." + |
| 1129 | itemType + "." + objectPair.key() + |
| 1130 | std::to_string(index), |
| 1131 | boardKeyOrig); |
| 1132 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1133 | populateInterfaceFromJson( |
| 1134 | systemConfiguration, |
| 1135 | jsonPointerPath + "/" + std::to_string(index), |
| 1136 | objectIface, arrayItem, objServer, |
| 1137 | getPermission(objectPair.key())); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1138 | index++; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1146 | // reads json files out of the filesystem |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1147 | bool findJsonFiles(std::list<nlohmann::json>& configurations) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1148 | { |
| 1149 | // find configuration files |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1150 | std::vector<std::filesystem::path> jsonPaths; |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 1151 | if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)", |
| 1152 | jsonPaths)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1153 | { |
| 1154 | std::cerr << "Unable to find any configuration files in " |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1155 | << configurationDirectory << "\n"; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1156 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1157 | } |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1158 | |
| 1159 | std::ifstream schemaStream(std::string(schemaDirectory) + "/" + |
| 1160 | globalSchema); |
| 1161 | if (!schemaStream.good()) |
| 1162 | { |
| 1163 | std::cerr |
| 1164 | << "Cannot open schema file, cannot validate JSON, exiting\n\n"; |
| 1165 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1166 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1167 | } |
| 1168 | nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false); |
| 1169 | if (schema.is_discarded()) |
| 1170 | { |
| 1171 | std::cerr |
| 1172 | << "Illegal schema file detected, cannot validate JSON, exiting\n"; |
| 1173 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1174 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1177 | for (auto& jsonPath : jsonPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1178 | { |
| 1179 | std::ifstream jsonStream(jsonPath.c_str()); |
| 1180 | if (!jsonStream.good()) |
| 1181 | { |
| 1182 | std::cerr << "unable to open " << jsonPath.string() << "\n"; |
| 1183 | continue; |
| 1184 | } |
| 1185 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1186 | if (data.is_discarded()) |
| 1187 | { |
| 1188 | std::cerr << "syntax error in " << jsonPath.string() << "\n"; |
| 1189 | continue; |
| 1190 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 1191 | /* |
| 1192 | * todo(james): reenable this once less things are in flight |
| 1193 | * |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1194 | if (!validateJson(schema, data)) |
| 1195 | { |
| 1196 | std::cerr << "Error validating " << jsonPath.string() << "\n"; |
| 1197 | continue; |
| 1198 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 1199 | */ |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1200 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1201 | if (data.type() == nlohmann::json::value_t::array) |
| 1202 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1203 | for (auto& d : data) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1204 | { |
| 1205 | configurations.emplace_back(d); |
| 1206 | } |
| 1207 | } |
| 1208 | else |
| 1209 | { |
| 1210 | configurations.emplace_back(data); |
| 1211 | } |
| 1212 | } |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1213 | return true; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1214 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1215 | |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1216 | std::string getRecordName( |
| 1217 | const boost::container::flat_map<std::string, BasicVariantType>& probe, |
| 1218 | const std::string& probeName) |
| 1219 | { |
| 1220 | if (probe.empty()) |
| 1221 | { |
| 1222 | return probeName; |
| 1223 | } |
| 1224 | |
| 1225 | // use an array so alphabetical order from the |
| 1226 | // flat_map is maintained |
| 1227 | auto device = nlohmann::json::array(); |
| 1228 | for (auto& devPair : probe) |
| 1229 | { |
| 1230 | device.push_back(devPair.first); |
| 1231 | std::visit([&device](auto&& v) { device.push_back(v); }, |
| 1232 | devPair.second); |
| 1233 | } |
| 1234 | size_t hash = std::hash<std::string>{}(probeName + device.dump()); |
| 1235 | // hashes are hard to distinguish, use the |
| 1236 | // non-hashed version if we want debug |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1237 | if constexpr (debug) |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1238 | { |
| 1239 | return probeName + device.dump(); |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | return std::to_string(hash); |
| 1244 | } |
| 1245 | } |
| 1246 | |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1247 | PerformScan::PerformScan(nlohmann::json& systemConfiguration, |
| 1248 | nlohmann::json& missingConfigurations, |
| 1249 | std::list<nlohmann::json>& configurations, |
| 1250 | sdbusplus::asio::object_server& objServerIn, |
| 1251 | std::function<void()>&& callback) : |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1252 | _systemConfiguration(systemConfiguration), |
| 1253 | _missingConfigurations(missingConfigurations), |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1254 | _configurations(configurations), objServer(objServerIn), |
| 1255 | _callback(std::move(callback)) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 1256 | {} |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1257 | void PerformScan::run() |
| 1258 | { |
| 1259 | boost::container::flat_set<std::string> dbusProbeInterfaces; |
| 1260 | std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1261 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1262 | for (auto it = _configurations.begin(); it != _configurations.end();) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1263 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1264 | auto findProbe = it->find("Probe"); |
| 1265 | auto findName = it->find("Name"); |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1266 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1267 | nlohmann::json probeCommand; |
| 1268 | // check for poorly formatted fields, probe must be an array |
| 1269 | if (findProbe == it->end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1270 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1271 | std::cerr << "configuration file missing probe:\n " << *it << "\n"; |
| 1272 | it = _configurations.erase(it); |
| 1273 | continue; |
| 1274 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1275 | if ((*findProbe).type() != nlohmann::json::value_t::array) |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1276 | { |
| 1277 | probeCommand = nlohmann::json::array(); |
| 1278 | probeCommand.push_back(*findProbe); |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | probeCommand = *findProbe; |
| 1283 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1284 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1285 | if (findName == it->end()) |
| 1286 | { |
| 1287 | std::cerr << "configuration file missing name:\n " << *it << "\n"; |
| 1288 | it = _configurations.erase(it); |
| 1289 | continue; |
| 1290 | } |
| 1291 | std::string probeName = *findName; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1292 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1293 | if (std::find(passedProbes.begin(), passedProbes.end(), probeName) != |
| 1294 | passedProbes.end()) |
| 1295 | { |
| 1296 | it = _configurations.erase(it); |
| 1297 | continue; |
| 1298 | } |
| 1299 | nlohmann::json* recordPtr = &(*it); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1300 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1301 | // store reference to this to children to makes sure we don't get |
| 1302 | // destroyed too early |
| 1303 | auto thisRef = shared_from_this(); |
| 1304 | auto probePointer = std::make_shared<PerformProbe>( |
| 1305 | probeCommand, thisRef, |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1306 | [&, recordPtr, probeName](FoundDeviceT& foundDevices, |
| 1307 | const DBusProbeObjectT& allInterfaces) { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1308 | _passed = true; |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1309 | std::set<nlohmann::json> usedNames; |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1310 | passedProbes.push_back(probeName); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1311 | std::list<size_t> indexes(foundDevices.size()); |
| 1312 | std::iota(indexes.begin(), indexes.end(), 1); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1313 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1314 | size_t indexIdx = probeName.find('$'); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1315 | bool hasTemplateName = (indexIdx != std::string::npos); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1316 | |
| 1317 | // copy over persisted configurations and make sure we remove |
| 1318 | // indexes that are already used |
| 1319 | for (auto itr = foundDevices.begin(); |
| 1320 | itr != foundDevices.end();) |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1321 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1322 | std::string recordName = |
| 1323 | getRecordName(std::get<0>(*itr), probeName); |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1324 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1325 | auto fromLastJson = lastJson.find(recordName); |
| 1326 | if (fromLastJson != lastJson.end()) |
| 1327 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1328 | auto findExposes = fromLastJson->find("Exposes"); |
| 1329 | // delete nulls from any updates |
| 1330 | if (findExposes != fromLastJson->end()) |
| 1331 | { |
| 1332 | auto copy = nlohmann::json::array(); |
| 1333 | for (auto& expose : *findExposes) |
| 1334 | { |
| 1335 | if (expose.is_null()) |
| 1336 | { |
| 1337 | continue; |
| 1338 | } |
| 1339 | copy.emplace_back(expose); |
| 1340 | } |
| 1341 | *findExposes = copy; |
| 1342 | } |
| 1343 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1344 | // keep user changes |
| 1345 | _systemConfiguration[recordName] = *fromLastJson; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1346 | _missingConfigurations.erase(recordName); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1347 | itr = foundDevices.erase(itr); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1348 | if (hasTemplateName) |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1349 | { |
| 1350 | auto nameIt = fromLastJson->find("Name"); |
| 1351 | if (nameIt == fromLastJson->end()) |
| 1352 | { |
| 1353 | std::cerr << "Last JSON Illegal\n"; |
| 1354 | continue; |
| 1355 | } |
Igor Kononenko | 9fd87e5 | 2020-10-06 01:18:17 +0300 | [diff] [blame] | 1356 | int index = 0; |
| 1357 | auto str = |
| 1358 | nameIt->get<std::string>().substr(indexIdx); |
| 1359 | auto [p, ec] = std::from_chars( |
| 1360 | str.data(), str.data() + str.size(), index); |
| 1361 | if (ec != std::errc()) |
| 1362 | { |
| 1363 | continue; // non-numeric replacement |
| 1364 | } |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1365 | usedNames.insert(nameIt.value()); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1366 | auto usedIt = std::find(indexes.begin(), |
| 1367 | indexes.end(), index); |
| 1368 | |
| 1369 | if (usedIt == indexes.end()) |
| 1370 | { |
| 1371 | continue; // less items now |
| 1372 | } |
| 1373 | indexes.erase(usedIt); |
| 1374 | } |
| 1375 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1376 | continue; |
| 1377 | } |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1378 | itr++; |
| 1379 | } |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1380 | |
| 1381 | std::optional<std::string> replaceStr; |
| 1382 | |
Matt Spinler | 4bab932 | 2021-04-08 14:38:43 -0500 | [diff] [blame] | 1383 | DBusProbeObjectT::mapped_type emptyInterfaces; |
| 1384 | boost::container::flat_map<std::string, BasicVariantType> |
| 1385 | emptyProps; |
| 1386 | emptyInterfaces.emplace(std::string{}, emptyProps); |
| 1387 | |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1388 | for (auto& foundDeviceAndPath : foundDevices) |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1389 | { |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1390 | const boost::container::flat_map< |
| 1391 | std::string, BasicVariantType>& foundDevice = |
| 1392 | std::get<0>(foundDeviceAndPath); |
| 1393 | const std::string& path = std::get<1>(foundDeviceAndPath); |
| 1394 | |
| 1395 | // Need all interfaces on this path so that template |
| 1396 | // substitutions can be done with any of the contained |
Matt Spinler | 4bab932 | 2021-04-08 14:38:43 -0500 | [diff] [blame] | 1397 | // properties. If the probe that passed didn't use an |
| 1398 | // interface, such as if it was just TRUE, then |
| 1399 | // templateCharReplace will just get passed in an empty |
| 1400 | // map. |
| 1401 | const DBusProbeObjectT::mapped_type* allInterfacesOnPath = |
| 1402 | &emptyInterfaces; |
| 1403 | |
| 1404 | auto ifacesIt = allInterfaces.find(path); |
| 1405 | if (ifacesIt != allInterfaces.end()) |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1406 | { |
Matt Spinler | 4bab932 | 2021-04-08 14:38:43 -0500 | [diff] [blame] | 1407 | allInterfacesOnPath = &ifacesIt->second; |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1408 | } |
| 1409 | |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1410 | nlohmann::json record = *recordPtr; |
| 1411 | std::string recordName = |
| 1412 | getRecordName(foundDevice, probeName); |
| 1413 | size_t foundDeviceIdx = indexes.front(); |
| 1414 | indexes.pop_front(); |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1415 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1416 | // check name first so we have no duplicate names |
| 1417 | auto getName = record.find("Name"); |
| 1418 | if (getName == record.end()) |
| 1419 | { |
| 1420 | std::cerr << "Record Missing Name! " << record.dump(); |
| 1421 | continue; // this should be impossible at this level |
| 1422 | } |
| 1423 | |
| 1424 | nlohmann::json copyForName = {{"Name", getName.value()}}; |
| 1425 | nlohmann::json::iterator copyIt = copyForName.begin(); |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1426 | std::optional<std::string> replaceVal = |
Matt Spinler | 4bab932 | 2021-04-08 14:38:43 -0500 | [diff] [blame] | 1427 | templateCharReplace(copyIt, *allInterfacesOnPath, |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1428 | foundDeviceIdx, replaceStr); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1429 | |
| 1430 | if (!replaceStr && replaceVal) |
| 1431 | { |
| 1432 | if (usedNames.find(copyIt.value()) != usedNames.end()) |
| 1433 | { |
| 1434 | replaceStr = replaceVal; |
| 1435 | copyForName = {{"Name", getName.value()}}; |
| 1436 | copyIt = copyForName.begin(); |
Matt Spinler | 4bab932 | 2021-04-08 14:38:43 -0500 | [diff] [blame] | 1437 | templateCharReplace(copyIt, *allInterfacesOnPath, |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1438 | foundDeviceIdx, replaceStr); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | if (replaceStr) |
| 1443 | { |
| 1444 | std::cerr << "Duplicates found, replacing " |
| 1445 | << *replaceStr |
| 1446 | << " with found device index.\n Consider " |
| 1447 | "fixing template to not have duplicates\n"; |
| 1448 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1449 | |
| 1450 | for (auto keyPair = record.begin(); keyPair != record.end(); |
| 1451 | keyPair++) |
| 1452 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1453 | if (keyPair.key() == "Name") |
| 1454 | { |
| 1455 | keyPair.value() = copyIt.value(); |
| 1456 | usedNames.insert(copyIt.value()); |
| 1457 | |
| 1458 | continue; // already covered above |
| 1459 | } |
Matt Spinler | 4bab932 | 2021-04-08 14:38:43 -0500 | [diff] [blame] | 1460 | templateCharReplace(keyPair, *allInterfacesOnPath, |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1461 | foundDeviceIdx, replaceStr); |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1464 | // insert into configuration temporarily to be able to |
| 1465 | // reference ourselves |
| 1466 | |
| 1467 | _systemConfiguration[recordName] = record; |
| 1468 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1469 | auto findExpose = record.find("Exposes"); |
| 1470 | if (findExpose == record.end()) |
| 1471 | { |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1472 | _systemConfiguration[recordName] = record; |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1473 | continue; |
| 1474 | } |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1475 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1476 | for (auto& expose : *findExpose) |
| 1477 | { |
| 1478 | for (auto keyPair = expose.begin(); |
| 1479 | keyPair != expose.end(); keyPair++) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1480 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1481 | |
Matt Spinler | 4bab932 | 2021-04-08 14:38:43 -0500 | [diff] [blame] | 1482 | templateCharReplace(keyPair, *allInterfacesOnPath, |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1483 | foundDeviceIdx, replaceStr); |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1484 | |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1485 | bool isBind = |
| 1486 | boost::starts_with(keyPair.key(), "Bind"); |
| 1487 | bool isDisable = keyPair.key() == "DisableNode"; |
| 1488 | |
| 1489 | // special cases |
| 1490 | if (!(isBind || isDisable)) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1491 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1492 | continue; |
| 1493 | } |
| 1494 | |
| 1495 | if (keyPair.value().type() != |
| 1496 | nlohmann::json::value_t::string && |
| 1497 | keyPair.value().type() != |
| 1498 | nlohmann::json::value_t::array) |
| 1499 | { |
| 1500 | std::cerr << "Value is invalid type " |
| 1501 | << keyPair.key() << "\n"; |
| 1502 | continue; |
| 1503 | } |
| 1504 | |
| 1505 | std::vector<std::string> matches; |
| 1506 | if (keyPair.value().type() == |
| 1507 | nlohmann::json::value_t::string) |
| 1508 | { |
| 1509 | matches.emplace_back(keyPair.value()); |
| 1510 | } |
| 1511 | else |
| 1512 | { |
| 1513 | for (const auto& value : keyPair.value()) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1514 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1515 | if (value.type() != |
| 1516 | nlohmann::json::value_t::string) |
| 1517 | { |
| 1518 | std::cerr << "Value is invalid type " |
| 1519 | << value << "\n"; |
| 1520 | break; |
| 1521 | } |
| 1522 | matches.emplace_back(value); |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1523 | } |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1524 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1525 | |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1526 | std::set<std::string> foundMatches; |
| 1527 | for (auto& configurationPair : |
| 1528 | _systemConfiguration.items()) |
| 1529 | { |
| 1530 | if (isDisable) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1531 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1532 | // don't disable ourselves |
| 1533 | if (configurationPair.key() == recordName) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1534 | { |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1535 | continue; |
| 1536 | } |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1537 | } |
| 1538 | auto configListFind = |
| 1539 | configurationPair.value().find("Exposes"); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1540 | |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1541 | if (configListFind == |
| 1542 | configurationPair.value().end() || |
| 1543 | configListFind->type() != |
| 1544 | nlohmann::json::value_t::array) |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1545 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1546 | continue; |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1547 | } |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1548 | for (auto& exposedObject : *configListFind) |
| 1549 | { |
| 1550 | auto matchIt = std::find_if( |
| 1551 | matches.begin(), matches.end(), |
| 1552 | [name = (exposedObject)["Name"] |
| 1553 | .get<std::string>()]( |
| 1554 | const std::string& s) { |
| 1555 | return s == name; |
| 1556 | }); |
| 1557 | if (matchIt == matches.end()) |
| 1558 | { |
| 1559 | continue; |
| 1560 | } |
| 1561 | foundMatches.insert(*matchIt); |
| 1562 | |
| 1563 | if (isBind) |
| 1564 | { |
| 1565 | std::string bind = keyPair.key().substr( |
| 1566 | sizeof("Bind") - 1); |
| 1567 | |
| 1568 | exposedObject["Status"] = "okay"; |
| 1569 | expose[bind] = exposedObject; |
| 1570 | } |
| 1571 | else if (isDisable) |
| 1572 | { |
| 1573 | exposedObject["Status"] = "disabled"; |
| 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | if (foundMatches.size() != matches.size()) |
| 1578 | { |
| 1579 | std::cerr << "configuration file " |
| 1580 | "dependency error, " |
| 1581 | "could not find " |
| 1582 | << keyPair.key() << " " |
| 1583 | << keyPair.value() << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1587 | // overwrite ourselves with cleaned up version |
| 1588 | _systemConfiguration[recordName] = record; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1589 | _missingConfigurations.erase(recordName); |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1590 | } |
| 1591 | }); |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1592 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1593 | // parse out dbus probes by discarding other probe types, store in a |
| 1594 | // map |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1595 | for (const nlohmann::json& probeJson : probeCommand) |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1596 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1597 | const std::string* probe = probeJson.get_ptr<const std::string*>(); |
| 1598 | if (probe == nullptr) |
| 1599 | { |
| 1600 | std::cerr << "Probe statement wasn't a string, can't parse"; |
| 1601 | continue; |
| 1602 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1603 | bool found = false; |
| 1604 | boost::container::flat_map<const char*, probe_type_codes, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1605 | CmpStr>::const_iterator probeType; |
| 1606 | for (probeType = probeTypes.begin(); probeType != probeTypes.end(); |
| 1607 | ++probeType) |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1608 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1609 | if (probe->find(probeType->first) != std::string::npos) |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1610 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1611 | found = true; |
| 1612 | break; |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1613 | } |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1614 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1615 | if (found) |
| 1616 | { |
| 1617 | continue; |
| 1618 | } |
| 1619 | // syntax requires probe before first open brace |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1620 | auto findStart = probe->find('('); |
| 1621 | std::string interface = probe->substr(0, findStart); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1622 | dbusProbeInterfaces.emplace(interface); |
| 1623 | dbusProbePointers.emplace_back(probePointer); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1624 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1625 | it++; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1626 | } |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1627 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1628 | // probe vector stores a shared_ptr to each PerformProbe that cares |
| 1629 | // about a dbus interface |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1630 | findDbusObjects(std::move(dbusProbePointers), |
| 1631 | std::move(dbusProbeInterfaces), shared_from_this()); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1632 | if constexpr (debug) |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1633 | { |
| 1634 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 1635 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | PerformScan::~PerformScan() |
| 1639 | { |
| 1640 | if (_passed) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1641 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1642 | auto nextScan = std::make_shared<PerformScan>( |
| 1643 | _systemConfiguration, _missingConfigurations, _configurations, |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1644 | objServer, std::move(_callback)); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1645 | nextScan->passedProbes = std::move(passedProbes); |
| 1646 | nextScan->dbusProbeObjects = std::move(dbusProbeObjects); |
| 1647 | nextScan->run(); |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1648 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1649 | if constexpr (debug) |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1650 | { |
| 1651 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 1652 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1653 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1654 | else |
| 1655 | { |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1656 | _callback(); |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1657 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1658 | if constexpr (debug) |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1659 | { |
| 1660 | std::cerr << __func__ << " " << __LINE__ << "\n"; |
| 1661 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1662 | } |
| 1663 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 1664 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1665 | void startRemovedTimer(boost::asio::steady_timer& timer, |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1666 | nlohmann::json& systemConfiguration) |
| 1667 | { |
| 1668 | static bool scannedPowerOff = false; |
| 1669 | static bool scannedPowerOn = false; |
| 1670 | |
James Feist | fb00f39 | 2019-06-25 14:16:48 -0700 | [diff] [blame] | 1671 | if (systemConfiguration.empty() || lastJson.empty()) |
| 1672 | { |
| 1673 | return; // not ready yet |
| 1674 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1675 | if (scannedPowerOn) |
| 1676 | { |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | if (!isPowerOn() && scannedPowerOff) |
| 1681 | { |
| 1682 | return; |
| 1683 | } |
| 1684 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1685 | timer.expires_after(std::chrono::seconds(10)); |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1686 | timer.async_wait( |
| 1687 | [&systemConfiguration](const boost::system::error_code& ec) { |
| 1688 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1689 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1690 | // we were cancelled |
| 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | bool powerOff = !isPowerOn(); |
| 1695 | for (const auto& item : lastJson.items()) |
| 1696 | { |
| 1697 | if (systemConfiguration.find(item.key()) == |
| 1698 | systemConfiguration.end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1699 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1700 | bool isDetectedPowerOn = false; |
| 1701 | auto powerState = item.value().find("PowerState"); |
| 1702 | if (powerState != item.value().end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1703 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1704 | auto ptr = powerState->get_ptr<const std::string*>(); |
| 1705 | if (ptr) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1706 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1707 | if (*ptr == "On" || *ptr == "BiosPost") |
| 1708 | { |
| 1709 | isDetectedPowerOn = true; |
| 1710 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1711 | } |
| 1712 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1713 | if (powerOff && isDetectedPowerOn) |
| 1714 | { |
| 1715 | // power not on yet, don't know if it's there or not |
| 1716 | continue; |
| 1717 | } |
| 1718 | if (!powerOff && scannedPowerOff && isDetectedPowerOn) |
| 1719 | { |
| 1720 | // already logged it when power was off |
| 1721 | continue; |
| 1722 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1723 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1724 | logDeviceRemoved(item.value()); |
| 1725 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1726 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1727 | scannedPowerOff = true; |
| 1728 | if (!powerOff) |
| 1729 | { |
| 1730 | scannedPowerOn = true; |
| 1731 | } |
| 1732 | }); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1735 | // main properties changed entry |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1736 | void propertiesChangedCallback(nlohmann::json& systemConfiguration, |
| 1737 | sdbusplus::asio::object_server& objServer) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1738 | { |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1739 | static bool inProgress = false; |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1740 | static boost::asio::steady_timer timer(io); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1741 | static size_t instance = 0; |
| 1742 | instance++; |
| 1743 | size_t count = instance; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1744 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1745 | timer.expires_after(std::chrono::seconds(5)); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1746 | |
| 1747 | // setup an async wait as we normally get flooded with new requests |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1748 | timer.async_wait([&systemConfiguration, &objServer, |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1749 | count](const boost::system::error_code& ec) { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1750 | if (ec == boost::asio::error::operation_aborted) |
| 1751 | { |
| 1752 | // we were cancelled |
| 1753 | return; |
| 1754 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1755 | if (ec) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1756 | { |
| 1757 | std::cerr << "async wait error " << ec << "\n"; |
| 1758 | return; |
| 1759 | } |
| 1760 | |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1761 | if (inProgress) |
| 1762 | { |
| 1763 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1764 | return; |
| 1765 | } |
| 1766 | inProgress = true; |
| 1767 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1768 | nlohmann::json oldConfiguration = systemConfiguration; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1769 | auto missingConfigurations = std::make_shared<nlohmann::json>(); |
| 1770 | *missingConfigurations = systemConfiguration; |
| 1771 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1772 | std::list<nlohmann::json> configurations; |
| 1773 | if (!findJsonFiles(configurations)) |
| 1774 | { |
| 1775 | std::cerr << "cannot find json files\n"; |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1776 | inProgress = false; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1777 | return; |
| 1778 | } |
| 1779 | |
| 1780 | auto perfScan = std::make_shared<PerformScan>( |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1781 | systemConfiguration, *missingConfigurations, configurations, |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1782 | objServer, |
| 1783 | [&systemConfiguration, &objServer, count, oldConfiguration, |
| 1784 | missingConfigurations]() { |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1785 | // this is something that since ac has been applied to the bmc |
| 1786 | // we saw, and we no longer see it |
| 1787 | bool powerOff = !isPowerOn(); |
| 1788 | for (const auto& item : missingConfigurations->items()) |
| 1789 | { |
| 1790 | bool isDetectedPowerOn = false; |
| 1791 | auto powerState = item.value().find("PowerState"); |
| 1792 | if (powerState != item.value().end()) |
| 1793 | { |
| 1794 | auto ptr = powerState->get_ptr<const std::string*>(); |
| 1795 | if (ptr) |
| 1796 | { |
| 1797 | if (*ptr == "On" || *ptr == "BiosPost") |
| 1798 | { |
| 1799 | isDetectedPowerOn = true; |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | if (powerOff && isDetectedPowerOn) |
| 1804 | { |
| 1805 | // power not on yet, don't know if it's there or not |
| 1806 | continue; |
| 1807 | } |
| 1808 | std::string name = item.value()["Name"].get<std::string>(); |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1809 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1810 | ifaces = inventory[name]; |
| 1811 | for (auto& iface : ifaces) |
| 1812 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1813 | auto sharedPtr = iface.lock(); |
| 1814 | if (!sharedPtr) |
| 1815 | { |
| 1816 | continue; // was already deleted elsewhere |
| 1817 | } |
| 1818 | objServer.remove_interface(sharedPtr); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1819 | } |
| 1820 | ifaces.clear(); |
| 1821 | systemConfiguration.erase(item.key()); |
| 1822 | logDeviceRemoved(item.value()); |
| 1823 | } |
| 1824 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1825 | nlohmann::json newConfiguration = systemConfiguration; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1826 | for (auto it = newConfiguration.begin(); |
| 1827 | it != newConfiguration.end();) |
| 1828 | { |
| 1829 | auto findKey = oldConfiguration.find(it.key()); |
| 1830 | if (findKey != oldConfiguration.end()) |
| 1831 | { |
| 1832 | it = newConfiguration.erase(it); |
| 1833 | } |
| 1834 | else |
| 1835 | { |
| 1836 | it++; |
| 1837 | } |
| 1838 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1839 | for (const auto& item : newConfiguration.items()) |
| 1840 | { |
| 1841 | logDeviceAdded(item.value()); |
| 1842 | } |
| 1843 | |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1844 | inProgress = false; |
| 1845 | |
Jonathan Doman | 6d64982 | 2021-05-05 16:53:04 -0700 | [diff] [blame] | 1846 | io.post([count, newConfiguration, &systemConfiguration, |
| 1847 | &objServer]() { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1848 | loadOverlays(newConfiguration); |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 1849 | |
Jonathan Doman | 6d64982 | 2021-05-05 16:53:04 -0700 | [diff] [blame] | 1850 | io.post([&systemConfiguration]() { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1851 | if (!writeJsonFiles(systemConfiguration)) |
| 1852 | { |
| 1853 | std::cerr << "Error writing json files\n"; |
| 1854 | } |
| 1855 | }); |
Jonathan Doman | 6d64982 | 2021-05-05 16:53:04 -0700 | [diff] [blame] | 1856 | io.post([count, newConfiguration, &systemConfiguration, |
| 1857 | &objServer]() { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1858 | postToDbus(newConfiguration, systemConfiguration, |
| 1859 | objServer); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1860 | if (count != instance) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1861 | { |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1862 | return; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1863 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1864 | startRemovedTimer(timer, systemConfiguration); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1865 | }); |
| 1866 | }); |
| 1867 | }); |
| 1868 | perfScan->run(); |
| 1869 | }); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1872 | void registerCallback(nlohmann::json& systemConfiguration, |
| 1873 | sdbusplus::asio::object_server& objServer, |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1874 | const std::string& path) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1875 | { |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1876 | static boost::container::flat_map<std::string, sdbusplus::bus::match::match> |
| 1877 | dbusMatches; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1878 | |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1879 | auto find = dbusMatches.find(path); |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1880 | if (find != dbusMatches.end()) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1881 | { |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1882 | return; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1883 | } |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1884 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
| 1885 | |
| 1886 | [&](sdbusplus::message::message&) { |
| 1887 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1888 | }; |
| 1889 | |
| 1890 | sdbusplus::bus::match::match match( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1891 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1892 | "type='signal',member='PropertiesChanged',path='" + path + "'", |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1893 | eventHandler); |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 1894 | dbusMatches.emplace(path, std::move(match)); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1897 | int main() |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1898 | { |
| 1899 | // setup connection to dbus |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1900 | systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 1901 | systemBus->request_name("xyz.openbmc_project.EntityManager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1902 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1903 | sdbusplus::asio::object_server objServer(systemBus); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1904 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1905 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface = |
| 1906 | objServer.add_interface("/xyz/openbmc_project/EntityManager", |
| 1907 | "xyz.openbmc_project.EntityManager"); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1908 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1909 | // to keep reference to the match / filter objects so they don't get |
| 1910 | // destroyed |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1911 | |
| 1912 | nlohmann::json systemConfiguration = nlohmann::json::object(); |
| 1913 | |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1914 | // We need a poke from DBus for static providers that create all their |
| 1915 | // objects prior to claiming a well-known name, and thus don't emit any |
| 1916 | // org.freedesktop.DBus.Properties signals. Similarly if a process exits |
| 1917 | // for any reason, expected or otherwise, we'll need a poke to remove |
| 1918 | // entities from DBus. |
| 1919 | sdbusplus::bus::match::match nameOwnerChangedMatch( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1920 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1921 | sdbusplus::bus::match::rules::nameOwnerChanged(), |
| 1922 | [&](sdbusplus::message::message&) { |
| 1923 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1924 | }); |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1925 | // We also need a poke from DBus when new interfaces are created or |
| 1926 | // destroyed. |
| 1927 | sdbusplus::bus::match::match interfacesAddedMatch( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1928 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1929 | sdbusplus::bus::match::rules::interfacesAdded(), |
| 1930 | [&](sdbusplus::message::message&) { |
| 1931 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1932 | }); |
| 1933 | sdbusplus::bus::match::match interfacesRemovedMatch( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1934 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1935 | sdbusplus::bus::match::rules::interfacesRemoved(), |
| 1936 | [&](sdbusplus::message::message&) { |
| 1937 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1938 | }); |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1939 | |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1940 | io.post( |
| 1941 | [&]() { propertiesChangedCallback(systemConfiguration, objServer); }); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1942 | |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1943 | entityIface->register_method("ReScan", [&]() { |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1944 | propertiesChangedCallback(systemConfiguration, objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1945 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1946 | entityIface->initialize(); |
| 1947 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1948 | if (fwVersionIsSame()) |
| 1949 | { |
| 1950 | if (std::filesystem::is_regular_file(currentConfiguration)) |
| 1951 | { |
| 1952 | // this file could just be deleted, but it's nice for debug |
| 1953 | std::filesystem::create_directory(tempConfigDir); |
| 1954 | std::filesystem::remove(lastConfiguration); |
| 1955 | std::filesystem::copy(currentConfiguration, lastConfiguration); |
| 1956 | std::filesystem::remove(currentConfiguration); |
| 1957 | |
| 1958 | std::ifstream jsonStream(lastConfiguration); |
| 1959 | if (jsonStream.good()) |
| 1960 | { |
| 1961 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1962 | if (data.is_discarded()) |
| 1963 | { |
| 1964 | std::cerr << "syntax error in " << lastConfiguration |
| 1965 | << "\n"; |
| 1966 | } |
| 1967 | else |
| 1968 | { |
| 1969 | lastJson = std::move(data); |
| 1970 | } |
| 1971 | } |
| 1972 | else |
| 1973 | { |
| 1974 | std::cerr << "unable to open " << lastConfiguration << "\n"; |
| 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | else |
| 1979 | { |
| 1980 | // not an error, just logging at this level to make it in the journal |
| 1981 | std::cerr << "Clearing previous configuration\n"; |
| 1982 | std::filesystem::remove(currentConfiguration); |
| 1983 | } |
| 1984 | |
| 1985 | // some boards only show up after power is on, we want to not say they are |
| 1986 | // removed until the same state happens |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1987 | setupPowerMatch(systemBus); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1988 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1989 | io.run(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1990 | |
| 1991 | return 0; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1992 | } |