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