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 | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file entity_manager.cpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 18 | #include "entity_manager.hpp" |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 19 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 20 | #include "overlay.hpp" |
Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 21 | #include "topology.hpp" |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 22 | #include "utils.hpp" |
| 23 | #include "variant_visitors.hpp" |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 24 | |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 25 | #include <boost/algorithm/string/case_conv.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 26 | #include <boost/algorithm/string/classification.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 27 | #include <boost/algorithm/string/predicate.hpp> |
| 28 | #include <boost/algorithm/string/replace.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 29 | #include <boost/algorithm/string/split.hpp> |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 30 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 49a888c | 2023-03-06 13:44:51 -0800 | [diff] [blame] | 31 | #include <boost/asio/post.hpp> |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 32 | #include <boost/asio/steady_timer.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 33 | #include <boost/container/flat_map.hpp> |
| 34 | #include <boost/container/flat_set.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 35 | #include <boost/range/iterator_range.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 36 | #include <nlohmann/json.hpp> |
| 37 | #include <sdbusplus/asio/connection.hpp> |
| 38 | #include <sdbusplus/asio/object_server.hpp> |
| 39 | |
Igor Kononenko | 9fd87e5 | 2020-10-06 01:18:17 +0300 | [diff] [blame] | 40 | #include <charconv> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 41 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 42 | #include <fstream> |
Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 43 | #include <functional> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 44 | #include <iostream> |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 45 | #include <map> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 46 | #include <regex> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 47 | #include <variant> |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 48 | constexpr const char* hostConfigurationDirectory = SYSCONF_DIR "configurations"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 49 | constexpr const char* configurationDirectory = PACKAGE_DIR "configurations"; |
| 50 | constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas"; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 51 | constexpr const char* tempConfigDir = "/tmp/configuration/"; |
| 52 | constexpr const char* lastConfiguration = "/tmp/configuration/last.json"; |
| 53 | constexpr const char* currentConfiguration = "/var/configuration/system.json"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 54 | constexpr const char* globalSchema = "global.json"; |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 55 | |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 56 | const boost::container::flat_map<const char*, probe_type_codes, CmpStr> |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 57 | probeTypes{{{"FALSE", probe_type_codes::FALSE_T}, |
| 58 | {"TRUE", probe_type_codes::TRUE_T}, |
| 59 | {"AND", probe_type_codes::AND}, |
| 60 | {"OR", probe_type_codes::OR}, |
| 61 | {"FOUND", probe_type_codes::FOUND}, |
| 62 | {"MATCH_ONE", probe_type_codes::MATCH_ONE}}}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 63 | |
Adrian Ambrożewicz | c789fca | 2020-05-14 15:50:05 +0200 | [diff] [blame] | 64 | static constexpr std::array<const char*, 6> settableInterfaces = { |
| 65 | "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"}; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 66 | using JsonVariantType = |
James Feist | 338b8a7 | 2019-03-01 10:16:45 -0800 | [diff] [blame] | 67 | std::variant<std::vector<std::string>, std::vector<double>, std::string, |
| 68 | int64_t, uint64_t, double, int32_t, uint32_t, int16_t, |
| 69 | uint16_t, uint8_t, bool>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 70 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 71 | // store reference to all interfaces so we can destroy them later |
| 72 | boost::container::flat_map< |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 73 | std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>> |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 74 | inventory; |
| 75 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 76 | // todo: pass this through nicer |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 77 | std::shared_ptr<sdbusplus::asio::connection> systemBus; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 78 | nlohmann::json lastJson; |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 79 | Topology topology; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 80 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 81 | boost::asio::io_context io; |
| 82 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 83 | const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]"); |
| 84 | const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 85 | |
John Edward Broadbent | d97c631 | 2023-10-26 20:32:07 +0000 | [diff] [blame^] | 86 | void tryIfaceInitialize(std::shared_ptr<sdbusplus::asio::dbus_interface>& iface) |
| 87 | { |
| 88 | try |
| 89 | { |
| 90 | iface->initialize(); |
| 91 | } |
| 92 | catch (std::exception& e) |
| 93 | { |
| 94 | std::cerr << "Unable to initialize dbus interface : " << e.what() |
| 95 | << "\n" |
| 96 | << "object Path : " << iface->get_object_path() << "\n" |
| 97 | << "interface name : " << iface->get_interface_name() << "\n"; |
| 98 | } |
| 99 | } |
| 100 | |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 101 | FoundProbeTypeT findProbeType(const std::string& probe) |
| 102 | { |
| 103 | boost::container::flat_map<const char*, probe_type_codes, |
| 104 | CmpStr>::const_iterator probeType; |
| 105 | for (probeType = probeTypes.begin(); probeType != probeTypes.end(); |
| 106 | ++probeType) |
| 107 | { |
| 108 | if (probe.find(probeType->first) != std::string::npos) |
| 109 | { |
| 110 | return probeType; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return std::nullopt; |
| 115 | } |
| 116 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 117 | static std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 118 | createInterface(sdbusplus::asio::object_server& objServer, |
| 119 | const std::string& path, const std::string& interface, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 120 | const std::string& parent, bool checkNull = false) |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 121 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 122 | // on first add we have no reason to check for null before add, as there |
| 123 | // won't be any. For dynamically added interfaces, we check for null so that |
| 124 | // a constant delete/add will not create a memory leak |
| 125 | |
| 126 | auto ptr = objServer.add_interface(path, interface); |
| 127 | auto& dataVector = inventory[parent]; |
| 128 | if (checkNull) |
| 129 | { |
| 130 | auto it = std::find_if(dataVector.begin(), dataVector.end(), |
| 131 | [](const auto& p) { return p.expired(); }); |
| 132 | if (it != dataVector.end()) |
| 133 | { |
| 134 | *it = ptr; |
| 135 | return ptr; |
| 136 | } |
| 137 | } |
| 138 | dataVector.emplace_back(ptr); |
| 139 | return ptr; |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 140 | } |
| 141 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 142 | // writes output files to persist data |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 143 | bool writeJsonFiles(const nlohmann::json& systemConfiguration) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 144 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 145 | std::filesystem::create_directory(configurationOutDir); |
| 146 | std::ofstream output(currentConfiguration); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 147 | if (!output.good()) |
| 148 | { |
| 149 | return false; |
| 150 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 151 | output << systemConfiguration.dump(4); |
| 152 | output.close(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 153 | return true; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 154 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 155 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 156 | template <typename JsonType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 157 | bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value, |
| 158 | nlohmann::json& systemConfiguration) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 159 | { |
| 160 | try |
| 161 | { |
| 162 | nlohmann::json::json_pointer ptr(ptrStr); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 163 | nlohmann::json& ref = systemConfiguration[ptr]; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 164 | ref = value; |
| 165 | return true; |
| 166 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 167 | catch (const std::out_of_range&) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 168 | { |
| 169 | return false; |
| 170 | } |
| 171 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 172 | |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 173 | // template function to add array as dbus property |
| 174 | template <typename PropertyType> |
| 175 | void addArrayToDbus(const std::string& name, const nlohmann::json& array, |
| 176 | sdbusplus::asio::dbus_interface* iface, |
| 177 | sdbusplus::asio::PropertyPermission permission, |
| 178 | nlohmann::json& systemConfiguration, |
| 179 | const std::string& jsonPointerString) |
| 180 | { |
| 181 | std::vector<PropertyType> values; |
| 182 | for (const auto& property : array) |
| 183 | { |
| 184 | auto ptr = property.get_ptr<const PropertyType*>(); |
| 185 | if (ptr != nullptr) |
| 186 | { |
| 187 | values.emplace_back(*ptr); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 192 | { |
| 193 | iface->register_property(name, values); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | iface->register_property( |
| 198 | name, values, |
| 199 | [&systemConfiguration, |
| 200 | jsonPointerString{std::string(jsonPointerString)}]( |
| 201 | const std::vector<PropertyType>& newVal, |
| 202 | std::vector<PropertyType>& val) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 203 | val = newVal; |
| 204 | if (!setJsonFromPointer(jsonPointerString, val, |
| 205 | systemConfiguration)) |
| 206 | { |
| 207 | std::cerr << "error setting json field\n"; |
| 208 | return -1; |
| 209 | } |
| 210 | if (!writeJsonFiles(systemConfiguration)) |
| 211 | { |
| 212 | std::cerr << "error setting json file\n"; |
| 213 | return -1; |
| 214 | } |
| 215 | return 1; |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 216 | }); |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 220 | template <typename PropertyType> |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 221 | void addProperty(const std::string& name, const PropertyType& value, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 222 | sdbusplus::asio::dbus_interface* iface, |
| 223 | nlohmann::json& systemConfiguration, |
| 224 | const std::string& jsonPointerString, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 225 | sdbusplus::asio::PropertyPermission permission) |
| 226 | { |
| 227 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 228 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 229 | iface->register_property(name, value); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 230 | return; |
| 231 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 232 | iface->register_property( |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 233 | name, value, |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 234 | [&systemConfiguration, |
| 235 | jsonPointerString{std::string(jsonPointerString)}]( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 236 | const PropertyType& newVal, PropertyType& val) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 237 | val = newVal; |
| 238 | if (!setJsonFromPointer(jsonPointerString, val, systemConfiguration)) |
| 239 | { |
| 240 | std::cerr << "error setting json field\n"; |
| 241 | return -1; |
| 242 | } |
| 243 | if (!writeJsonFiles(systemConfiguration)) |
| 244 | { |
| 245 | std::cerr << "error setting json file\n"; |
| 246 | return -1; |
| 247 | } |
| 248 | return 1; |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 249 | }); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void createDeleteObjectMethod( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 253 | const std::string& jsonPointerPath, |
| 254 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 255 | sdbusplus::asio::object_server& objServer, |
| 256 | nlohmann::json& systemConfiguration) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 257 | { |
| 258 | std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface; |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 259 | iface->register_method("Delete", |
| 260 | [&objServer, &systemConfiguration, interface, |
| 261 | jsonPointerPath{std::string(jsonPointerPath)}]() { |
| 262 | std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface = |
| 263 | interface.lock(); |
| 264 | if (!dbusInterface) |
| 265 | { |
| 266 | // this technically can't happen as the pointer is pointing to |
| 267 | // us |
| 268 | throw DBusInternalError(); |
| 269 | } |
| 270 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
| 271 | systemConfiguration[ptr] = nullptr; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 272 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 273 | // todo(james): dig through sdbusplus to find out why we can't |
| 274 | // delete it in a method call |
| 275 | boost::asio::post(io, [&objServer, dbusInterface]() mutable { |
| 276 | objServer.remove_interface(dbusInterface); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 277 | }); |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 278 | |
| 279 | if (!writeJsonFiles(systemConfiguration)) |
| 280 | { |
| 281 | std::cerr << "error setting json file\n"; |
| 282 | throw DBusInternalError(); |
| 283 | } |
| 284 | }); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 285 | } |
| 286 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 287 | // adds simple json types to interface's properties |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 288 | void populateInterfaceFromJson( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 289 | nlohmann::json& systemConfiguration, const std::string& jsonPointerPath, |
| 290 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 291 | nlohmann::json& dict, sdbusplus::asio::object_server& objServer, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 292 | sdbusplus::asio::PropertyPermission permission = |
| 293 | sdbusplus::asio::PropertyPermission::readOnly) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 294 | { |
Patrick Williams | 2594d36 | 2022-09-28 06:46:24 -0500 | [diff] [blame] | 295 | for (const auto& [key, value] : dict.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 296 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 297 | auto type = value.type(); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 298 | bool array = false; |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 299 | if (value.type() == nlohmann::json::value_t::array) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 300 | { |
| 301 | array = true; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 302 | if (value.empty()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 303 | { |
| 304 | continue; |
| 305 | } |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 306 | type = value[0].type(); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 307 | bool isLegal = true; |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 308 | for (const auto& arrayItem : value) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 309 | { |
| 310 | if (arrayItem.type() != type) |
| 311 | { |
| 312 | isLegal = false; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | if (!isLegal) |
| 317 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 318 | std::cerr << "dbus format error" << value << "\n"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 319 | continue; |
| 320 | } |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 321 | } |
| 322 | if (type == nlohmann::json::value_t::object) |
| 323 | { |
| 324 | continue; // handled elsewhere |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 325 | } |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 326 | |
| 327 | std::string path = jsonPointerPath; |
| 328 | path.append("/").append(key); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 329 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 330 | { |
| 331 | // all setable numbers are doubles as it is difficult to always |
| 332 | // create a configuration file with all whole numbers as decimals |
| 333 | // i.e. 1.0 |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 334 | if (array) |
| 335 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 336 | if (value[0].is_number()) |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 337 | { |
| 338 | type = nlohmann::json::value_t::number_float; |
| 339 | } |
| 340 | } |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 341 | else if (value.is_number()) |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 342 | { |
| 343 | type = nlohmann::json::value_t::number_float; |
| 344 | } |
| 345 | } |
| 346 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 347 | switch (type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 348 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 349 | case (nlohmann::json::value_t::boolean): |
| 350 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 351 | if (array) |
| 352 | { |
| 353 | // todo: array of bool isn't detected correctly by |
| 354 | // sdbusplus, change it to numbers |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 355 | addArrayToDbus<uint64_t>(key, value, iface.get(), |
| 356 | permission, systemConfiguration, |
| 357 | path); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 358 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 359 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 360 | else |
| 361 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 362 | addProperty(key, value.get<bool>(), iface.get(), |
| 363 | systemConfiguration, path, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 364 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | case (nlohmann::json::value_t::number_integer): |
| 368 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 369 | if (array) |
| 370 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 371 | addArrayToDbus<int64_t>(key, value, iface.get(), permission, |
Andrew Jeffery | 029ee28 | 2022-03-25 13:11:36 +1030 | [diff] [blame] | 372 | systemConfiguration, path); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 373 | } |
| 374 | else |
| 375 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 376 | addProperty(key, value.get<int64_t>(), iface.get(), |
| 377 | systemConfiguration, path, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 378 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 379 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | case (nlohmann::json::value_t::number_unsigned): |
| 383 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 384 | if (array) |
| 385 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 386 | addArrayToDbus<uint64_t>(key, value, iface.get(), |
| 387 | permission, systemConfiguration, |
| 388 | path); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 389 | } |
| 390 | else |
| 391 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 392 | addProperty(key, value.get<uint64_t>(), iface.get(), |
Andrew Jeffery | 029ee28 | 2022-03-25 13:11:36 +1030 | [diff] [blame] | 393 | systemConfiguration, path, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 394 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 395 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | case (nlohmann::json::value_t::number_float): |
| 399 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 400 | if (array) |
| 401 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 402 | addArrayToDbus<double>(key, value, iface.get(), permission, |
Andrew Jeffery | 029ee28 | 2022-03-25 13:11:36 +1030 | [diff] [blame] | 403 | systemConfiguration, path); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 404 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 405 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 406 | else |
| 407 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 408 | addProperty(key, value.get<double>(), iface.get(), |
| 409 | systemConfiguration, path, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 410 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | case (nlohmann::json::value_t::string): |
| 414 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 415 | if (array) |
| 416 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 417 | addArrayToDbus<std::string>(key, value, iface.get(), |
| 418 | permission, systemConfiguration, |
| 419 | path); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 420 | } |
| 421 | else |
| 422 | { |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 423 | addProperty(key, value.get<std::string>(), iface.get(), |
| 424 | systemConfiguration, path, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 425 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 426 | break; |
| 427 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 428 | default: |
| 429 | { |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 430 | std::cerr << "Unexpected json type in system configuration " |
Andrew Jeffery | 65ea450 | 2022-03-25 13:15:34 +1030 | [diff] [blame] | 431 | << key << ": " << value.type_name() << "\n"; |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 432 | break; |
| 433 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 436 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 437 | { |
| 438 | createDeleteObjectMethod(jsonPointerPath, iface, objServer, |
| 439 | systemConfiguration); |
| 440 | } |
John Edward Broadbent | d97c631 | 2023-10-26 20:32:07 +0000 | [diff] [blame^] | 441 | tryIfaceInitialize(iface); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 442 | } |
| 443 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 444 | sdbusplus::asio::PropertyPermission getPermission(const std::string& interface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 445 | { |
| 446 | return std::find(settableInterfaces.begin(), settableInterfaces.end(), |
| 447 | interface) != settableInterfaces.end() |
| 448 | ? sdbusplus::asio::PropertyPermission::readWrite |
| 449 | : sdbusplus::asio::PropertyPermission::readOnly; |
| 450 | } |
| 451 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 452 | void createAddObjectMethod(const std::string& jsonPointerPath, |
| 453 | const std::string& path, |
| 454 | nlohmann::json& systemConfiguration, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 455 | sdbusplus::asio::object_server& objServer, |
| 456 | const std::string& board) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 457 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 458 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface( |
| 459 | objServer, path, "xyz.openbmc_project.AddObject", board); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 460 | |
| 461 | iface->register_method( |
| 462 | "AddObject", |
| 463 | [&systemConfiguration, &objServer, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 464 | jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)}, |
| 465 | board](const boost::container::flat_map<std::string, JsonVariantType>& |
| 466 | data) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 467 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
| 468 | nlohmann::json& base = systemConfiguration[ptr]; |
| 469 | auto findExposes = base.find("Exposes"); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 470 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 471 | if (findExposes == base.end()) |
| 472 | { |
| 473 | throw std::invalid_argument("Entity must have children."); |
| 474 | } |
| 475 | |
| 476 | // this will throw invalid-argument to sdbusplus if invalid json |
| 477 | nlohmann::json newData{}; |
| 478 | for (const auto& item : data) |
| 479 | { |
| 480 | nlohmann::json& newJson = newData[item.first]; |
| 481 | std::visit( |
| 482 | [&newJson](auto&& val) { |
| 483 | newJson = std::forward<decltype(val)>(val); |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 484 | }, |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 485 | item.second); |
| 486 | } |
| 487 | |
| 488 | auto findName = newData.find("Name"); |
| 489 | auto findType = newData.find("Type"); |
| 490 | if (findName == newData.end() || findType == newData.end()) |
| 491 | { |
| 492 | throw std::invalid_argument("AddObject missing Name or Type"); |
| 493 | } |
| 494 | const std::string* type = findType->get_ptr<const std::string*>(); |
| 495 | const std::string* name = findName->get_ptr<const std::string*>(); |
| 496 | if (type == nullptr || name == nullptr) |
| 497 | { |
| 498 | throw std::invalid_argument("Type and Name must be a string."); |
| 499 | } |
| 500 | |
| 501 | bool foundNull = false; |
| 502 | size_t lastIndex = 0; |
| 503 | // we add in the "exposes" |
| 504 | for (const auto& expose : *findExposes) |
| 505 | { |
| 506 | if (expose.is_null()) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 507 | { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 508 | foundNull = true; |
| 509 | continue; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 512 | if (expose["Name"] == *name && expose["Type"] == *type) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 513 | { |
| 514 | throw std::invalid_argument( |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 515 | "Field already in JSON, not adding"); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 516 | } |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 517 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 518 | if (foundNull) |
| 519 | { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 520 | continue; |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 521 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 522 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 523 | lastIndex++; |
| 524 | } |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 525 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 526 | std::ifstream schemaFile(std::string(schemaDirectory) + "/" + |
| 527 | boost::to_lower_copy(*type) + ".json"); |
| 528 | // todo(james) we might want to also make a list of 'can add' |
| 529 | // interfaces but for now I think the assumption if there is a |
| 530 | // schema avaliable that it is allowed to update is fine |
| 531 | if (!schemaFile.good()) |
| 532 | { |
| 533 | throw std::invalid_argument( |
| 534 | "No schema avaliable, cannot validate."); |
| 535 | } |
| 536 | nlohmann::json schema = nlohmann::json::parse(schemaFile, nullptr, |
| 537 | false); |
| 538 | if (schema.is_discarded()) |
| 539 | { |
| 540 | std::cerr << "Schema not legal" << *type << ".json\n"; |
| 541 | throw DBusInternalError(); |
| 542 | } |
| 543 | if (!validateJson(schema, newData)) |
| 544 | { |
| 545 | throw std::invalid_argument("Data does not match schema"); |
| 546 | } |
| 547 | if (foundNull) |
| 548 | { |
| 549 | findExposes->at(lastIndex) = newData; |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | findExposes->push_back(newData); |
| 554 | } |
| 555 | if (!writeJsonFiles(systemConfiguration)) |
| 556 | { |
| 557 | std::cerr << "Error writing json files\n"; |
| 558 | throw DBusInternalError(); |
| 559 | } |
| 560 | std::string dbusName = *name; |
| 561 | |
| 562 | std::regex_replace(dbusName.begin(), dbusName.begin(), dbusName.end(), |
| 563 | illegalDbusMemberRegex, "_"); |
| 564 | |
| 565 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 566 | createInterface(objServer, path + "/" + dbusName, |
| 567 | "xyz.openbmc_project.Configuration." + *type, board, |
| 568 | true); |
| 569 | // permission is read-write, as since we just created it, must be |
| 570 | // runtime modifiable |
| 571 | populateInterfaceFromJson( |
| 572 | systemConfiguration, |
| 573 | jsonPointerPath + "/Exposes/" + std::to_string(lastIndex), |
| 574 | interface, newData, objServer, |
| 575 | sdbusplus::asio::PropertyPermission::readWrite); |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 576 | }); |
John Edward Broadbent | d97c631 | 2023-10-26 20:32:07 +0000 | [diff] [blame^] | 577 | tryIfaceInitialize(iface); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 578 | } |
| 579 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 580 | void postToDbus(const nlohmann::json& newConfiguration, |
| 581 | nlohmann::json& systemConfiguration, |
| 582 | sdbusplus::asio::object_server& objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 583 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 584 | { |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 585 | std::map<std::string, std::string> newBoards; // path -> name |
Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 586 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 587 | // iterate through boards |
Patrick Williams | 2594d36 | 2022-09-28 06:46:24 -0500 | [diff] [blame] | 588 | for (const auto& [boardId, boardConfig] : newConfiguration.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 589 | { |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 590 | std::string boardName = boardConfig["Name"]; |
| 591 | std::string boardNameOrig = boardConfig["Name"]; |
Andrew Jeffery | 13132df | 2022-03-25 13:29:41 +1030 | [diff] [blame] | 592 | std::string jsonPointerPath = "/" + boardId; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 593 | // loop through newConfiguration, but use values from system |
| 594 | // configuration to be able to modify via dbus later |
Andrew Jeffery | 13132df | 2022-03-25 13:29:41 +1030 | [diff] [blame] | 595 | auto boardValues = systemConfiguration[boardId]; |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 596 | auto findBoardType = boardValues.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 597 | std::string boardType; |
| 598 | if (findBoardType != boardValues.end() && |
| 599 | findBoardType->type() == nlohmann::json::value_t::string) |
| 600 | { |
| 601 | boardType = findBoardType->get<std::string>(); |
| 602 | std::regex_replace(boardType.begin(), boardType.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 603 | boardType.end(), illegalDbusMemberRegex, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 604 | } |
| 605 | else |
| 606 | { |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 607 | std::cerr << "Unable to find type for " << boardName |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 608 | << " reverting to Chassis.\n"; |
| 609 | boardType = "Chassis"; |
| 610 | } |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 611 | std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 612 | |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 613 | std::regex_replace(boardName.begin(), boardName.begin(), |
| 614 | boardName.end(), illegalDbusMemberRegex, "_"); |
| 615 | std::string boardPath = "/xyz/openbmc_project/inventory/system/"; |
| 616 | boardPath += boardtypeLower; |
| 617 | boardPath += "/"; |
| 618 | boardPath += boardName; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 619 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 620 | std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface = |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 621 | createInterface(objServer, boardPath, |
| 622 | "xyz.openbmc_project.Inventory.Item", boardName); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 623 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 624 | std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface = |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 625 | createInterface(objServer, boardPath, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 626 | "xyz.openbmc_project.Inventory.Item." + boardType, |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 627 | boardNameOrig); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 628 | |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 629 | createAddObjectMethod(jsonPointerPath, boardPath, systemConfiguration, |
| 630 | objServer, boardNameOrig); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 631 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 632 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 633 | boardIface, boardValues, objServer); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 634 | jsonPointerPath += "/"; |
| 635 | // iterate through board properties |
Patrick Williams | 2594d36 | 2022-09-28 06:46:24 -0500 | [diff] [blame] | 636 | for (const auto& [propName, propValue] : boardValues.items()) |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 637 | { |
Andrew Jeffery | a96950d | 2022-03-25 13:32:46 +1030 | [diff] [blame] | 638 | if (propValue.type() == nlohmann::json::value_t::object) |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 639 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 640 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 641 | createInterface(objServer, boardPath, propName, |
| 642 | boardNameOrig); |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 643 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 644 | populateInterfaceFromJson(systemConfiguration, |
Andrew Jeffery | a96950d | 2022-03-25 13:32:46 +1030 | [diff] [blame] | 645 | jsonPointerPath + propName, iface, |
| 646 | propValue, objServer); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 649 | |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 650 | auto exposes = boardValues.find("Exposes"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 651 | if (exposes == boardValues.end()) |
| 652 | { |
| 653 | continue; |
| 654 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 655 | // iterate through exposes |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 656 | jsonPointerPath += "Exposes/"; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 657 | |
| 658 | // store the board level pointer so we can modify it on the way down |
| 659 | std::string jsonPointerPathBoard = jsonPointerPath; |
| 660 | size_t exposesIndex = -1; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 661 | for (auto& item : *exposes) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 662 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 663 | exposesIndex++; |
| 664 | jsonPointerPath = jsonPointerPathBoard; |
| 665 | jsonPointerPath += std::to_string(exposesIndex); |
| 666 | |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 667 | auto findName = item.find("Name"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 668 | if (findName == item.end()) |
| 669 | { |
| 670 | std::cerr << "cannot find name in field " << item << "\n"; |
| 671 | continue; |
| 672 | } |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 673 | auto findStatus = item.find("Status"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 674 | // if status is not found it is assumed to be status = 'okay' |
| 675 | if (findStatus != item.end()) |
| 676 | { |
| 677 | if (*findStatus == "disabled") |
| 678 | { |
| 679 | continue; |
| 680 | } |
| 681 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 682 | auto findType = item.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 683 | std::string itemType; |
| 684 | if (findType != item.end()) |
| 685 | { |
| 686 | itemType = findType->get<std::string>(); |
| 687 | std::regex_replace(itemType.begin(), itemType.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 688 | itemType.end(), illegalDbusPathRegex, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 689 | } |
| 690 | else |
| 691 | { |
| 692 | itemType = "unknown"; |
| 693 | } |
| 694 | std::string itemName = findName->get<std::string>(); |
| 695 | std::regex_replace(itemName.begin(), itemName.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 696 | itemName.end(), illegalDbusMemberRegex, "_"); |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 697 | std::string ifacePath = boardPath; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 698 | ifacePath += "/"; |
| 699 | ifacePath += itemName; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 700 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 701 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface = |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 702 | createInterface(objServer, ifacePath, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 703 | "xyz.openbmc_project.Configuration." + itemType, |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 704 | boardNameOrig); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 705 | |
Sui Chen | 74ebe59 | 2022-09-13 10:22:03 -0700 | [diff] [blame] | 706 | if (itemType == "BMC") |
| 707 | { |
| 708 | std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface = |
| 709 | createInterface(objServer, ifacePath, |
| 710 | "xyz.openbmc_project.Inventory.Item.Bmc", |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 711 | boardNameOrig); |
Sui Chen | 74ebe59 | 2022-09-13 10:22:03 -0700 | [diff] [blame] | 712 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
| 713 | bmcIface, item, objServer, |
| 714 | getPermission(itemType)); |
| 715 | } |
Edward Lee | eb587b4 | 2023-03-08 18:59:04 +0000 | [diff] [blame] | 716 | else if (itemType == "System") |
| 717 | { |
| 718 | std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface = |
| 719 | createInterface(objServer, ifacePath, |
| 720 | "xyz.openbmc_project.Inventory.Item.System", |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 721 | boardNameOrig); |
Edward Lee | eb587b4 | 2023-03-08 18:59:04 +0000 | [diff] [blame] | 722 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
| 723 | systemIface, item, objServer, |
| 724 | getPermission(itemType)); |
| 725 | } |
Sui Chen | 74ebe59 | 2022-09-13 10:22:03 -0700 | [diff] [blame] | 726 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 727 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 728 | itemIface, item, objServer, |
| 729 | getPermission(itemType)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 730 | |
Patrick Williams | 2594d36 | 2022-09-28 06:46:24 -0500 | [diff] [blame] | 731 | for (const auto& [name, config] : item.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 732 | { |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 733 | jsonPointerPath = jsonPointerPathBoard; |
| 734 | jsonPointerPath.append(std::to_string(exposesIndex)) |
| 735 | .append("/") |
| 736 | .append(name); |
| 737 | if (config.type() == nlohmann::json::value_t::object) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 738 | { |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 739 | std::string ifaceName = |
| 740 | "xyz.openbmc_project.Configuration."; |
| 741 | ifaceName.append(itemType).append(".").append(name); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 742 | |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 743 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 744 | objectIface = createInterface(objServer, ifacePath, |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 745 | ifaceName, boardNameOrig); |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 746 | |
| 747 | populateInterfaceFromJson( |
| 748 | systemConfiguration, jsonPointerPath, objectIface, |
| 749 | config, objServer, getPermission(name)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 750 | } |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 751 | else if (config.type() == nlohmann::json::value_t::array) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 752 | { |
| 753 | size_t index = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 754 | if (config.empty()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 755 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 756 | continue; |
| 757 | } |
| 758 | bool isLegal = true; |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 759 | auto type = config[0].type(); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 760 | if (type != nlohmann::json::value_t::object) |
| 761 | { |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | // verify legal json |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 766 | for (const auto& arrayItem : config) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 767 | { |
| 768 | if (arrayItem.type() != type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 769 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 770 | isLegal = false; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 771 | break; |
| 772 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 773 | } |
| 774 | if (!isLegal) |
| 775 | { |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 776 | std::cerr << "dbus format error" << config << "\n"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 777 | break; |
| 778 | } |
| 779 | |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 780 | for (auto& arrayItem : config) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 781 | { |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 782 | std::string ifaceName = |
| 783 | "xyz.openbmc_project.Configuration."; |
| 784 | ifaceName.append(itemType).append(".").append(name); |
| 785 | ifaceName.append(std::to_string(index)); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 786 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 787 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 788 | objectIface = createInterface( |
Matt Spinler | 3d1909a | 2023-08-10 16:39:44 -0500 | [diff] [blame] | 789 | objServer, ifacePath, ifaceName, boardNameOrig); |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 790 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 791 | populateInterfaceFromJson( |
| 792 | systemConfiguration, |
| 793 | jsonPointerPath + "/" + std::to_string(index), |
| 794 | objectIface, arrayItem, objServer, |
Andrew Jeffery | 5a6379c | 2022-03-25 13:25:31 +1030 | [diff] [blame] | 795 | getPermission(name)); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 796 | index++; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | } |
Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 800 | |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 801 | topology.addBoard(boardPath, boardType, boardNameOrig, item); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 802 | } |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 803 | |
| 804 | newBoards.emplace(boardPath, boardNameOrig); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 805 | } |
Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 806 | |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 807 | for (const auto& [assocPath, assocPropValue] : |
| 808 | topology.getAssocs(newBoards)) |
Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 809 | { |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 810 | auto findBoard = newBoards.find(assocPath); |
| 811 | if (findBoard == newBoards.end()) |
| 812 | { |
| 813 | continue; |
| 814 | } |
Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 815 | |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 816 | auto ifacePtr = createInterface( |
| 817 | objServer, assocPath, "xyz.openbmc_project.Association.Definitions", |
| 818 | findBoard->second); |
| 819 | |
| 820 | ifacePtr->register_property("Associations", assocPropValue); |
John Edward Broadbent | d97c631 | 2023-10-26 20:32:07 +0000 | [diff] [blame^] | 821 | tryIfaceInitialize(ifacePtr); |
Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 822 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 823 | } |
| 824 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 825 | // reads json files out of the filesystem |
Andrew Jeffery | f331179 | 2022-03-29 22:09:00 +1030 | [diff] [blame] | 826 | bool loadConfigurations(std::list<nlohmann::json>& configurations) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 827 | { |
| 828 | // find configuration files |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 829 | std::vector<std::filesystem::path> jsonPaths; |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 830 | if (!findFiles( |
| 831 | std::vector<std::filesystem::path>{configurationDirectory, |
| 832 | hostConfigurationDirectory}, |
| 833 | R"(.*\.json)", jsonPaths)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 834 | { |
| 835 | std::cerr << "Unable to find any configuration files in " |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 836 | << configurationDirectory << "\n"; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 837 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 838 | } |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 839 | |
| 840 | std::ifstream schemaStream(std::string(schemaDirectory) + "/" + |
| 841 | globalSchema); |
| 842 | if (!schemaStream.good()) |
| 843 | { |
| 844 | std::cerr |
| 845 | << "Cannot open schema file, cannot validate JSON, exiting\n\n"; |
| 846 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 847 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 848 | } |
| 849 | nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false); |
| 850 | if (schema.is_discarded()) |
| 851 | { |
| 852 | std::cerr |
| 853 | << "Illegal schema file detected, cannot validate JSON, exiting\n"; |
| 854 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 855 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 856 | } |
| 857 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 858 | for (auto& jsonPath : jsonPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 859 | { |
| 860 | std::ifstream jsonStream(jsonPath.c_str()); |
| 861 | if (!jsonStream.good()) |
| 862 | { |
| 863 | std::cerr << "unable to open " << jsonPath.string() << "\n"; |
| 864 | continue; |
| 865 | } |
| 866 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 867 | if (data.is_discarded()) |
| 868 | { |
| 869 | std::cerr << "syntax error in " << jsonPath.string() << "\n"; |
| 870 | continue; |
| 871 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 872 | /* |
| 873 | * todo(james): reenable this once less things are in flight |
| 874 | * |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 875 | if (!validateJson(schema, data)) |
| 876 | { |
| 877 | std::cerr << "Error validating " << jsonPath.string() << "\n"; |
| 878 | continue; |
| 879 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 880 | */ |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 881 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 882 | if (data.type() == nlohmann::json::value_t::array) |
| 883 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 884 | for (auto& d : data) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 885 | { |
| 886 | configurations.emplace_back(d); |
| 887 | } |
| 888 | } |
| 889 | else |
| 890 | { |
| 891 | configurations.emplace_back(data); |
| 892 | } |
| 893 | } |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 894 | return true; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 895 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 896 | |
Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 897 | static bool deviceRequiresPowerOn(const nlohmann::json& entity) |
| 898 | { |
| 899 | auto powerState = entity.find("PowerState"); |
Andrew Jeffery | b620944 | 2022-03-24 12:36:20 +1030 | [diff] [blame] | 900 | if (powerState == entity.end()) |
Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 901 | { |
Andrew Jeffery | b620944 | 2022-03-24 12:36:20 +1030 | [diff] [blame] | 902 | return false; |
Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 903 | } |
| 904 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 905 | const auto* ptr = powerState->get_ptr<const std::string*>(); |
| 906 | if (ptr == nullptr) |
Andrew Jeffery | b620944 | 2022-03-24 12:36:20 +1030 | [diff] [blame] | 907 | { |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | return *ptr == "On" || *ptr == "BiosPost"; |
Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 912 | } |
| 913 | |
Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 914 | static void pruneDevice(const nlohmann::json& systemConfiguration, |
| 915 | const bool powerOff, const bool scannedPowerOff, |
| 916 | const std::string& name, const nlohmann::json& device) |
| 917 | { |
| 918 | if (systemConfiguration.contains(name)) |
| 919 | { |
| 920 | return; |
| 921 | } |
| 922 | |
Andrew Jeffery | 4db38bc | 2022-03-24 13:42:41 +1030 | [diff] [blame] | 923 | if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff)) |
Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 924 | { |
Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 925 | return; |
| 926 | } |
| 927 | |
| 928 | logDeviceRemoved(device); |
| 929 | } |
| 930 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 931 | void startRemovedTimer(boost::asio::steady_timer& timer, |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 932 | nlohmann::json& systemConfiguration) |
| 933 | { |
| 934 | static bool scannedPowerOff = false; |
| 935 | static bool scannedPowerOn = false; |
| 936 | |
James Feist | fb00f39 | 2019-06-25 14:16:48 -0700 | [diff] [blame] | 937 | if (systemConfiguration.empty() || lastJson.empty()) |
| 938 | { |
| 939 | return; // not ready yet |
| 940 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 941 | if (scannedPowerOn) |
| 942 | { |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | if (!isPowerOn() && scannedPowerOff) |
| 947 | { |
| 948 | return; |
| 949 | } |
| 950 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 951 | timer.expires_after(std::chrono::seconds(10)); |
Andrew Jeffery | 27a1cfb | 2022-03-24 12:31:53 +1030 | [diff] [blame] | 952 | timer.async_wait( |
| 953 | [&systemConfiguration](const boost::system::error_code& ec) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 954 | if (ec == boost::asio::error::operation_aborted) |
| 955 | { |
| 956 | return; |
| 957 | } |
Andrew Jeffery | 27a1cfb | 2022-03-24 12:31:53 +1030 | [diff] [blame] | 958 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 959 | bool powerOff = !isPowerOn(); |
| 960 | for (const auto& [name, device] : lastJson.items()) |
| 961 | { |
| 962 | pruneDevice(systemConfiguration, powerOff, scannedPowerOff, name, |
| 963 | device); |
| 964 | } |
Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 965 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 966 | scannedPowerOff = true; |
| 967 | if (!powerOff) |
| 968 | { |
| 969 | scannedPowerOn = true; |
| 970 | } |
| 971 | }); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Andrew Jeffery | 2f750d2 | 2022-03-24 14:32:57 +1030 | [diff] [blame] | 974 | static std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& |
| 975 | getDeviceInterfaces(const nlohmann::json& device) |
| 976 | { |
| 977 | return inventory[device["Name"].get<std::string>()]; |
| 978 | } |
| 979 | |
Andrew Jeffery | 0d0c146 | 2022-03-24 15:26:39 +1030 | [diff] [blame] | 980 | static void pruneConfiguration(nlohmann::json& systemConfiguration, |
| 981 | sdbusplus::asio::object_server& objServer, |
| 982 | bool powerOff, const std::string& name, |
| 983 | const nlohmann::json& device) |
| 984 | { |
| 985 | if (powerOff && deviceRequiresPowerOn(device)) |
| 986 | { |
| 987 | // power not on yet, don't know if it's there or not |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | auto& ifaces = getDeviceInterfaces(device); |
| 992 | for (auto& iface : ifaces) |
| 993 | { |
| 994 | auto sharedPtr = iface.lock(); |
| 995 | if (!!sharedPtr) |
| 996 | { |
| 997 | objServer.remove_interface(sharedPtr); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | ifaces.clear(); |
| 1002 | systemConfiguration.erase(name); |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 1003 | topology.remove(device["Name"].get<std::string>()); |
Andrew Jeffery | 0d0c146 | 2022-03-24 15:26:39 +1030 | [diff] [blame] | 1004 | logDeviceRemoved(device); |
| 1005 | } |
| 1006 | |
Andrew Jeffery | 7c4cbbe | 2022-03-24 15:27:10 +1030 | [diff] [blame] | 1007 | static void deriveNewConfiguration(const nlohmann::json& oldConfiguration, |
| 1008 | nlohmann::json& newConfiguration) |
| 1009 | { |
| 1010 | for (auto it = newConfiguration.begin(); it != newConfiguration.end();) |
| 1011 | { |
| 1012 | auto findKey = oldConfiguration.find(it.key()); |
| 1013 | if (findKey != oldConfiguration.end()) |
| 1014 | { |
| 1015 | it = newConfiguration.erase(it); |
| 1016 | } |
| 1017 | else |
| 1018 | { |
| 1019 | it++; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 1024 | static void publishNewConfiguration( |
| 1025 | const size_t& instance, const size_t count, |
| 1026 | boost::asio::steady_timer& timer, nlohmann::json& systemConfiguration, |
| 1027 | // Gerrit discussion: |
| 1028 | // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6 |
| 1029 | // |
| 1030 | // Discord discussion: |
| 1031 | // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854 |
| 1032 | // |
| 1033 | // NOLINTNEXTLINE(performance-unnecessary-value-param) |
| 1034 | const nlohmann::json newConfiguration, |
| 1035 | sdbusplus::asio::object_server& objServer) |
| 1036 | { |
| 1037 | loadOverlays(newConfiguration); |
| 1038 | |
Ed Tanous | 49a888c | 2023-03-06 13:44:51 -0800 | [diff] [blame] | 1039 | boost::asio::post(io, [systemConfiguration]() { |
Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 1040 | if (!writeJsonFiles(systemConfiguration)) |
| 1041 | { |
| 1042 | std::cerr << "Error writing json files\n"; |
| 1043 | } |
| 1044 | }); |
| 1045 | |
Ed Tanous | 49a888c | 2023-03-06 13:44:51 -0800 | [diff] [blame] | 1046 | boost::asio::post(io, [&instance, count, &timer, newConfiguration, |
| 1047 | &systemConfiguration, &objServer]() { |
Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 1048 | postToDbus(newConfiguration, systemConfiguration, objServer); |
| 1049 | if (count == instance) |
| 1050 | { |
| 1051 | startRemovedTimer(timer, systemConfiguration); |
| 1052 | } |
| 1053 | }); |
| 1054 | } |
| 1055 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1056 | // main properties changed entry |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1057 | void propertiesChangedCallback(nlohmann::json& systemConfiguration, |
| 1058 | sdbusplus::asio::object_server& objServer) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1059 | { |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1060 | static bool inProgress = false; |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1061 | static boost::asio::steady_timer timer(io); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1062 | static size_t instance = 0; |
| 1063 | instance++; |
| 1064 | size_t count = instance; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1065 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 1066 | timer.expires_after(std::chrono::seconds(5)); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1067 | |
| 1068 | // setup an async wait as we normally get flooded with new requests |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1069 | timer.async_wait([&systemConfiguration, &objServer, |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1070 | count](const boost::system::error_code& ec) { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1071 | if (ec == boost::asio::error::operation_aborted) |
| 1072 | { |
| 1073 | // we were cancelled |
| 1074 | return; |
| 1075 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1076 | if (ec) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1077 | { |
| 1078 | std::cerr << "async wait error " << ec << "\n"; |
| 1079 | return; |
| 1080 | } |
| 1081 | |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1082 | if (inProgress) |
| 1083 | { |
| 1084 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1085 | return; |
| 1086 | } |
| 1087 | inProgress = true; |
| 1088 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1089 | nlohmann::json oldConfiguration = systemConfiguration; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1090 | auto missingConfigurations = std::make_shared<nlohmann::json>(); |
| 1091 | *missingConfigurations = systemConfiguration; |
| 1092 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1093 | std::list<nlohmann::json> configurations; |
Andrew Jeffery | f331179 | 2022-03-29 22:09:00 +1030 | [diff] [blame] | 1094 | if (!loadConfigurations(configurations)) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1095 | { |
Andrew Jeffery | f331179 | 2022-03-29 22:09:00 +1030 | [diff] [blame] | 1096 | std::cerr << "Could not load configurations\n"; |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1097 | inProgress = false; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1098 | return; |
| 1099 | } |
| 1100 | |
| 1101 | auto perfScan = std::make_shared<PerformScan>( |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1102 | systemConfiguration, *missingConfigurations, configurations, |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1103 | objServer, |
| 1104 | [&systemConfiguration, &objServer, count, oldConfiguration, |
| 1105 | missingConfigurations]() { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1106 | // this is something that since ac has been applied to the bmc |
| 1107 | // we saw, and we no longer see it |
| 1108 | bool powerOff = !isPowerOn(); |
| 1109 | for (const auto& [name, device] : missingConfigurations->items()) |
| 1110 | { |
| 1111 | pruneConfiguration(systemConfiguration, objServer, powerOff, |
| 1112 | name, device); |
| 1113 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1114 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1115 | nlohmann::json newConfiguration = systemConfiguration; |
Andrew Jeffery | 7c4cbbe | 2022-03-24 15:27:10 +1030 | [diff] [blame] | 1116 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1117 | deriveNewConfiguration(oldConfiguration, newConfiguration); |
Andrew Jeffery | 7c4cbbe | 2022-03-24 15:27:10 +1030 | [diff] [blame] | 1118 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1119 | for (const auto& [_, device] : newConfiguration.items()) |
| 1120 | { |
| 1121 | logDeviceAdded(device); |
| 1122 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1123 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1124 | inProgress = false; |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1125 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1126 | boost::asio::post( |
| 1127 | io, std::bind_front(publishNewConfiguration, std::ref(instance), |
| 1128 | count, std::ref(timer), |
| 1129 | std::ref(systemConfiguration), |
| 1130 | newConfiguration, std::ref(objServer))); |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 1131 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1132 | perfScan->run(); |
| 1133 | }); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 1136 | // Extract the D-Bus interfaces to probe from the JSON config files. |
| 1137 | static std::set<std::string> getProbeInterfaces() |
| 1138 | { |
| 1139 | std::set<std::string> interfaces; |
| 1140 | std::list<nlohmann::json> configurations; |
| 1141 | if (!loadConfigurations(configurations)) |
| 1142 | { |
| 1143 | return interfaces; |
| 1144 | } |
| 1145 | |
| 1146 | for (auto it = configurations.begin(); it != configurations.end();) |
| 1147 | { |
| 1148 | auto findProbe = it->find("Probe"); |
| 1149 | if (findProbe == it->end()) |
| 1150 | { |
| 1151 | std::cerr << "configuration file missing probe:\n " << *it << "\n"; |
| 1152 | it++; |
| 1153 | continue; |
| 1154 | } |
| 1155 | |
| 1156 | nlohmann::json probeCommand; |
| 1157 | if ((*findProbe).type() != nlohmann::json::value_t::array) |
| 1158 | { |
| 1159 | probeCommand = nlohmann::json::array(); |
| 1160 | probeCommand.push_back(*findProbe); |
| 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | probeCommand = *findProbe; |
| 1165 | } |
| 1166 | |
| 1167 | for (const nlohmann::json& probeJson : probeCommand) |
| 1168 | { |
| 1169 | const std::string* probe = probeJson.get_ptr<const std::string*>(); |
| 1170 | if (probe == nullptr) |
| 1171 | { |
| 1172 | std::cerr << "Probe statement wasn't a string, can't parse"; |
| 1173 | continue; |
| 1174 | } |
| 1175 | // Skip it if the probe cmd doesn't contain an interface. |
| 1176 | if (findProbeType(*probe)) |
| 1177 | { |
| 1178 | continue; |
| 1179 | } |
| 1180 | |
| 1181 | // syntax requires probe before first open brace |
| 1182 | auto findStart = probe->find('('); |
| 1183 | if (findStart != std::string::npos) |
| 1184 | { |
| 1185 | std::string interface = probe->substr(0, findStart); |
| 1186 | interfaces.emplace(interface); |
| 1187 | } |
| 1188 | } |
| 1189 | it++; |
| 1190 | } |
| 1191 | |
| 1192 | return interfaces; |
| 1193 | } |
| 1194 | |
| 1195 | // Check if InterfacesAdded payload contains an iface that needs probing. |
| 1196 | static bool |
| 1197 | iaContainsProbeInterface(sdbusplus::message_t& msg, |
| 1198 | const std::set<std::string>& probeInterfaces) |
| 1199 | { |
| 1200 | sdbusplus::message::object_path path; |
| 1201 | DBusObject interfaces; |
| 1202 | std::set<std::string> interfaceSet; |
| 1203 | std::set<std::string> intersect; |
| 1204 | |
| 1205 | msg.read(path, interfaces); |
| 1206 | |
| 1207 | std::for_each(interfaces.begin(), interfaces.end(), |
| 1208 | [&interfaceSet](const auto& iface) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1209 | interfaceSet.insert(iface.first); |
| 1210 | }); |
Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 1211 | |
| 1212 | std::set_intersection(interfaceSet.begin(), interfaceSet.end(), |
| 1213 | probeInterfaces.begin(), probeInterfaces.end(), |
| 1214 | std::inserter(intersect, intersect.end())); |
| 1215 | return !intersect.empty(); |
| 1216 | } |
| 1217 | |
| 1218 | // Check if InterfacesRemoved payload contains an iface that needs probing. |
| 1219 | static bool |
| 1220 | irContainsProbeInterface(sdbusplus::message_t& msg, |
| 1221 | const std::set<std::string>& probeInterfaces) |
| 1222 | { |
| 1223 | sdbusplus::message::object_path path; |
| 1224 | std::set<std::string> interfaces; |
| 1225 | std::set<std::string> intersect; |
| 1226 | |
| 1227 | msg.read(path, interfaces); |
| 1228 | |
| 1229 | std::set_intersection(interfaces.begin(), interfaces.end(), |
| 1230 | probeInterfaces.begin(), probeInterfaces.end(), |
| 1231 | std::inserter(intersect, intersect.end())); |
| 1232 | return !intersect.empty(); |
| 1233 | } |
| 1234 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1235 | int main() |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1236 | { |
| 1237 | // setup connection to dbus |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1238 | systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 1239 | systemBus->request_name("xyz.openbmc_project.EntityManager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1240 | |
Nan Zhou | a331567 | 2022-09-20 19:48:14 +0000 | [diff] [blame] | 1241 | // The EntityManager object itself doesn't expose any properties. |
| 1242 | // No need to set up ObjectManager for the |EntityManager| object. |
| 1243 | sdbusplus::asio::object_server objServer(systemBus, /*skipManager=*/true); |
| 1244 | |
| 1245 | // All other objects that EntityManager currently support are under the |
| 1246 | // inventory subtree. |
| 1247 | // See the discussion at |
| 1248 | // https://discord.com/channels/775381525260664832/1018929092009144380 |
| 1249 | objServer.add_manager("/xyz/openbmc_project/inventory"); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1250 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1251 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface = |
| 1252 | objServer.add_interface("/xyz/openbmc_project/EntityManager", |
| 1253 | "xyz.openbmc_project.EntityManager"); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1254 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1255 | // to keep reference to the match / filter objects so they don't get |
| 1256 | // destroyed |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1257 | |
| 1258 | nlohmann::json systemConfiguration = nlohmann::json::object(); |
| 1259 | |
Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 1260 | std::set<std::string> probeInterfaces = getProbeInterfaces(); |
| 1261 | |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1262 | // We need a poke from DBus for static providers that create all their |
| 1263 | // objects prior to claiming a well-known name, and thus don't emit any |
| 1264 | // org.freedesktop.DBus.Properties signals. Similarly if a process exits |
| 1265 | // for any reason, expected or otherwise, we'll need a poke to remove |
| 1266 | // entities from DBus. |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 1267 | sdbusplus::bus::match_t nameOwnerChangedMatch( |
| 1268 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1269 | sdbusplus::bus::match::rules::nameOwnerChanged(), |
Patrick Williams | 7b8786f | 2022-10-10 10:23:37 -0500 | [diff] [blame] | 1270 | [&](sdbusplus::message_t& m) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1271 | auto [name, oldOwner, |
| 1272 | newOwner] = m.unpack<std::string, std::string, std::string>(); |
Patrick Williams | 7b8786f | 2022-10-10 10:23:37 -0500 | [diff] [blame] | 1273 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1274 | if (name.starts_with(':')) |
| 1275 | { |
| 1276 | // We should do nothing with unique-name connections. |
| 1277 | return; |
| 1278 | } |
Patrick Williams | 7b8786f | 2022-10-10 10:23:37 -0500 | [diff] [blame] | 1279 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1280 | propertiesChangedCallback(systemConfiguration, objServer); |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 1281 | }); |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1282 | // We also need a poke from DBus when new interfaces are created or |
| 1283 | // destroyed. |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 1284 | sdbusplus::bus::match_t interfacesAddedMatch( |
| 1285 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1286 | sdbusplus::bus::match::rules::interfacesAdded(), |
Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 1287 | [&](sdbusplus::message_t& msg) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1288 | if (iaContainsProbeInterface(msg, probeInterfaces)) |
| 1289 | { |
| 1290 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1291 | } |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 1292 | }); |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 1293 | sdbusplus::bus::match_t interfacesRemovedMatch( |
| 1294 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1295 | sdbusplus::bus::match::rules::interfacesRemoved(), |
Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 1296 | [&](sdbusplus::message_t& msg) { |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1297 | if (irContainsProbeInterface(msg, probeInterfaces)) |
| 1298 | { |
| 1299 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1300 | } |
Patrick Williams | b9dd7f8 | 2023-10-20 11:20:11 -0500 | [diff] [blame] | 1301 | }); |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1302 | |
Ed Tanous | 49a888c | 2023-03-06 13:44:51 -0800 | [diff] [blame] | 1303 | boost::asio::post(io, [&]() { |
| 1304 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1305 | }); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1306 | |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1307 | entityIface->register_method("ReScan", [&]() { |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1308 | propertiesChangedCallback(systemConfiguration, objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1309 | }); |
John Edward Broadbent | d97c631 | 2023-10-26 20:32:07 +0000 | [diff] [blame^] | 1310 | tryIfaceInitialize(entityIface); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1311 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1312 | if (fwVersionIsSame()) |
| 1313 | { |
| 1314 | if (std::filesystem::is_regular_file(currentConfiguration)) |
| 1315 | { |
| 1316 | // this file could just be deleted, but it's nice for debug |
| 1317 | std::filesystem::create_directory(tempConfigDir); |
| 1318 | std::filesystem::remove(lastConfiguration); |
| 1319 | std::filesystem::copy(currentConfiguration, lastConfiguration); |
| 1320 | std::filesystem::remove(currentConfiguration); |
| 1321 | |
| 1322 | std::ifstream jsonStream(lastConfiguration); |
| 1323 | if (jsonStream.good()) |
| 1324 | { |
| 1325 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1326 | if (data.is_discarded()) |
| 1327 | { |
| 1328 | std::cerr << "syntax error in " << lastConfiguration |
| 1329 | << "\n"; |
| 1330 | } |
| 1331 | else |
| 1332 | { |
| 1333 | lastJson = std::move(data); |
| 1334 | } |
| 1335 | } |
| 1336 | else |
| 1337 | { |
| 1338 | std::cerr << "unable to open " << lastConfiguration << "\n"; |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | else |
| 1343 | { |
| 1344 | // not an error, just logging at this level to make it in the journal |
| 1345 | std::cerr << "Clearing previous configuration\n"; |
| 1346 | std::filesystem::remove(currentConfiguration); |
| 1347 | } |
| 1348 | |
| 1349 | // some boards only show up after power is on, we want to not say they are |
| 1350 | // removed until the same state happens |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1351 | setupPowerMatch(systemBus); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1352 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1353 | io.run(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1354 | |
| 1355 | return 0; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1356 | } |