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