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