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