blob: 796f6e74fa86cc4c4be999b7e4d122dce0aa2b4f [file] [log] [blame]
Alexander Hansen4e1142d2025-07-25 17:07:27 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
James Feist3cb5fec2018-01-23 14:41:51 -08003
Brad Bishope45d8c72022-05-25 15:12:53 -04004#include "entity_manager.hpp"
James Feist1df06a42019-04-11 14:23:04 -07005
Christopher Meisfc9e7fd2025-04-03 13:13:35 +02006#include "../utils.hpp"
7#include "../variant_visitors.hpp"
Christopher Meisbdaa6b22025-04-02 10:49:02 +02008#include "configuration.hpp"
Christopher Meis12bea9b2025-04-03 10:14:42 +02009#include "dbus_interface.hpp"
Alexander Hansenf57a2592025-06-27 15:07:07 +020010#include "log_device_inventory.hpp"
Brad Bishope45d8c72022-05-25 15:12:53 -040011#include "overlay.hpp"
Christopher Meis26fbbd52025-03-26 14:55:06 +010012#include "perform_scan.hpp"
Benjamin Fairca2eb042022-09-13 06:40:42 +000013#include "topology.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +020014#include "utils.hpp"
James Feist481c5d52019-08-13 14:40:40 -070015
James Feist02d2b932020-02-06 16:28:48 -080016#include <boost/asio/io_context.hpp>
Ed Tanous49a888c2023-03-06 13:44:51 -080017#include <boost/asio/post.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070018#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080019#include <boost/container/flat_map.hpp>
20#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070021#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070022#include <nlohmann/json.hpp>
Alexander Hansen8feb0452025-09-15 14:29:20 +020023#include <phosphor-logging/lg2.hpp>
James Feist8c505da2020-05-28 10:06:33 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
Alexander Hansenc58af0b2025-09-12 15:47:19 +020026#include <xyz/openbmc_project/Association/Definitions/common.hpp>
27#include <xyz/openbmc_project/Inventory/Item/Bmc/common.hpp>
28#include <xyz/openbmc_project/Inventory/Item/System/common.hpp>
29#include <xyz/openbmc_project/Inventory/Item/common.hpp>
James Feist8c505da2020-05-28 10:06:33 -070030
James Feist637b3ef2019-04-15 16:35:30 -070031#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080032#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103033#include <functional>
Andrew Jeffery666583b2021-12-01 15:50:12 +103034#include <map>
James Feista465ccc2019-02-08 12:51:01 -080035#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070036constexpr const char* tempConfigDir = "/tmp/configuration/";
37constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
James Feistf1b14142019-04-10 15:22:09 -070038
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020039static constexpr std::array<const char*, 6> settableInterfaces = {
40 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist3cb5fec2018-01-23 14:41:51 -080041
Ed Tanous07d467b2021-02-23 14:48:37 -080042const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
43const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080044
James Feista465ccc2019-02-08 12:51:01 -080045sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -070046{
47 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
48 interface) != settableInterfaces.end()
49 ? sdbusplus::asio::PropertyPermission::readWrite
50 : sdbusplus::asio::PropertyPermission::readOnly;
51}
52
Christopher Meiscf6a75b2025-06-03 07:53:50 +020053EntityManager::EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020054 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
55 boost::asio::io_context& io) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020056 systemBus(systemBus),
57 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
58 lastJson(nlohmann::json::object()),
Alexander Hansenb1340da2025-06-27 14:29:13 +020059 systemConfiguration(nlohmann::json::object()), io(io),
Alexander Hansen89737252025-08-04 15:15:13 +020060 dbus_interface(io, objServer), powerStatus(*systemBus),
61 propertiesChangedTimer(io)
Christopher Meiscf6a75b2025-06-03 07:53:50 +020062{
63 // All other objects that EntityManager currently support are under the
64 // inventory subtree.
65 // See the discussion at
66 // https://discord.com/channels/775381525260664832/1018929092009144380
67 objServer.add_manager("/xyz/openbmc_project/inventory");
James Feist75fdeeb2018-02-20 14:26:16 -080068
Christopher Meiscf6a75b2025-06-03 07:53:50 +020069 entityIface = objServer.add_interface("/xyz/openbmc_project/EntityManager",
70 "xyz.openbmc_project.EntityManager");
71 entityIface->register_method("ReScan", [this]() {
72 propertiesChangedCallback();
73 });
74 dbus_interface::tryIfaceInitialize(entityIface);
Christopher Meisf7252572025-06-11 13:22:05 +020075
76 initFilters(configuration.probeInterfaces);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020077}
78
79void EntityManager::postToDbus(const nlohmann::json& newConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -080080{
Matt Spinler6eb60972023-08-14 16:36:20 -050081 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +000082
James Feist97a63f12018-05-17 13:50:57 -070083 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -050084 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -080085 {
Alexander Hansen4fcd9462025-07-30 17:44:44 +020086 postBoardToDBus(boardId, boardConfig, newBoards);
James Feist1b2e2242018-01-30 13:45:19 -080087 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000088
Matt Spinler6eb60972023-08-14 16:36:20 -050089 for (const auto& [assocPath, assocPropValue] :
Alexander Hansen32e74182025-08-20 16:16:00 +020090 topology.getAssocs(std::views::keys(newBoards)))
Benjamin Fairca2eb042022-09-13 06:40:42 +000091 {
Matt Spinler6eb60972023-08-14 16:36:20 -050092 auto findBoard = newBoards.find(assocPath);
93 if (findBoard == newBoards.end())
94 {
95 continue;
96 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000097
Alexander Hansen57604ed2025-06-27 13:22:28 +020098 auto ifacePtr = dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +020099 assocPath,
100 sdbusplus::common::xyz::openbmc_project::association::Definitions::
101 interface,
Matt Spinler6eb60972023-08-14 16:36:20 -0500102 findBoard->second);
103
104 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200105 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000106 }
James Feist1b2e2242018-01-30 13:45:19 -0800107}
108
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200109void EntityManager::postBoardToDBus(
110 const std::string& boardId, const nlohmann::json& boardConfig,
111 std::map<std::string, std::string>& newBoards)
112{
113 std::string boardName = boardConfig["Name"];
114 std::string boardNameOrig = boardConfig["Name"];
115 std::string jsonPointerPath = "/" + boardId;
116 // loop through newConfiguration, but use values from system
117 // configuration to be able to modify via dbus later
118 auto boardValues = systemConfiguration[boardId];
119 auto findBoardType = boardValues.find("Type");
120 std::string boardType;
121 if (findBoardType != boardValues.end() &&
122 findBoardType->type() == nlohmann::json::value_t::string)
123 {
124 boardType = findBoardType->get<std::string>();
125 std::regex_replace(boardType.begin(), boardType.begin(),
126 boardType.end(), illegalDbusMemberRegex, "_");
127 }
128 else
129 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200130 lg2::error("Unable to find type for {BOARD} reverting to Chassis.",
131 "BOARD", boardName);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200132 boardType = "Chassis";
133 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200134
Christopher Meis811160e2025-08-08 08:48:37 +0200135 const std::string boardPath =
136 em_utils::buildInventorySystemPath(boardName, boardType);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200137
138 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200139 dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200140 boardPath,
141 sdbusplus::common::xyz::openbmc_project::inventory::Item::interface,
142 boardName);
143
144 const std::string invItemIntf = std::format(
145 "{}.{}",
146 sdbusplus::common::xyz::openbmc_project::inventory::Item::interface,
147 boardType);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200148
149 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200150 dbus_interface.createInterface(boardPath, invItemIntf, boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200151
Alexander Hansen89737252025-08-04 15:15:13 +0200152 dbus_interface.createAddObjectMethod(jsonPointerPath, boardPath,
153 systemConfiguration, boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200154
Alexander Hansen89737252025-08-04 15:15:13 +0200155 dbus_interface.populateInterfaceFromJson(
156 systemConfiguration, jsonPointerPath, boardIface, boardValues);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200157 jsonPointerPath += "/";
158 // iterate through board properties
159 for (const auto& [propName, propValue] : boardValues.items())
160 {
161 if (propValue.type() == nlohmann::json::value_t::object)
162 {
163 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen89737252025-08-04 15:15:13 +0200164 dbus_interface.createInterface(boardPath, propName,
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200165 boardNameOrig);
166
Alexander Hansen89737252025-08-04 15:15:13 +0200167 dbus_interface.populateInterfaceFromJson(
168 systemConfiguration, jsonPointerPath + propName, iface,
169 propValue);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200170 }
171 }
172
Alexander Hansen4fa40122025-07-30 18:26:35 +0200173 nlohmann::json::iterator exposes = boardValues.find("Exposes");
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200174 if (exposes == boardValues.end())
175 {
176 return;
177 }
178 // iterate through exposes
179 jsonPointerPath += "Exposes/";
180
181 // store the board level pointer so we can modify it on the way down
182 std::string jsonPointerPathBoard = jsonPointerPath;
183 size_t exposesIndex = -1;
Alexander Hansen4fa40122025-07-30 18:26:35 +0200184 for (nlohmann::json& item : *exposes)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200185 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200186 postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
187 jsonPointerPath, jsonPointerPathBoard,
188 boardPath, boardType);
189 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200190
Alexander Hansen4fa40122025-07-30 18:26:35 +0200191 newBoards.emplace(boardPath, boardNameOrig);
192}
193
194void EntityManager::postExposesRecordsToDBus(
195 nlohmann::json& item, size_t& exposesIndex,
196 const std::string& boardNameOrig, std::string jsonPointerPath,
197 const std::string& jsonPointerPathBoard, const std::string& boardPath,
198 const std::string& boardType)
199{
200 exposesIndex++;
201 jsonPointerPath = jsonPointerPathBoard;
202 jsonPointerPath += std::to_string(exposesIndex);
203
204 auto findName = item.find("Name");
205 if (findName == item.end())
206 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200207 lg2::error("cannot find name in field {ITEM}", "ITEM", item);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200208 return;
209 }
210 auto findStatus = item.find("Status");
211 // if status is not found it is assumed to be status = 'okay'
212 if (findStatus != item.end())
213 {
214 if (*findStatus == "disabled")
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200215 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200216 return;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200217 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200218 }
219 auto findType = item.find("Type");
220 std::string itemType;
221 if (findType != item.end())
222 {
223 itemType = findType->get<std::string>();
224 std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
225 illegalDbusPathRegex, "_");
226 }
227 else
228 {
229 itemType = "unknown";
230 }
231 std::string itemName = findName->get<std::string>();
232 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
233 illegalDbusMemberRegex, "_");
234 std::string ifacePath = boardPath;
235 ifacePath += "/";
236 ifacePath += itemName;
237
238 if (itemType == "BMC")
239 {
240 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
241 dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200242 ifacePath,
243 sdbusplus::common::xyz::openbmc_project::inventory::item::Bmc::
244 interface,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200245 boardNameOrig);
Alexander Hansen89737252025-08-04 15:15:13 +0200246 dbus_interface.populateInterfaceFromJson(
247 systemConfiguration, jsonPointerPath, bmcIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200248 getPermission(itemType));
249 }
250 else if (itemType == "System")
251 {
252 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
253 dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200254 ifacePath,
255 sdbusplus::common::xyz::openbmc_project::inventory::item::
256 System::interface,
Alexander Hansen89737252025-08-04 15:15:13 +0200257 boardNameOrig);
258 dbus_interface.populateInterfaceFromJson(
259 systemConfiguration, jsonPointerPath, systemIface, item,
260 getPermission(itemType));
Alexander Hansen4fa40122025-07-30 18:26:35 +0200261 }
262
263 for (const auto& [name, config] : item.items())
264 {
265 jsonPointerPath = jsonPointerPathBoard;
266 jsonPointerPath.append(std::to_string(exposesIndex))
267 .append("/")
268 .append(name);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200269
270 if (!postConfigurationRecord(name, config, boardNameOrig, itemType,
271 jsonPointerPath, ifacePath))
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200272 {
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200273 break;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200274 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200275 }
276
Alexander Hansen4fa40122025-07-30 18:26:35 +0200277 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
278 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200279 ifacePath, "xyz.openbmc_project.Configuration." + itemType,
280 boardNameOrig);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200281
Alexander Hansen89737252025-08-04 15:15:13 +0200282 dbus_interface.populateInterfaceFromJson(
283 systemConfiguration, jsonPointerPath, itemIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200284 getPermission(itemType));
285
286 topology.addBoard(boardPath, boardType, boardNameOrig, item);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200287}
288
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200289bool EntityManager::postConfigurationRecord(
290 const std::string& name, nlohmann::json& config,
291 const std::string& boardNameOrig, const std::string& itemType,
292 const std::string& jsonPointerPath, const std::string& ifacePath)
293{
294 if (config.type() == nlohmann::json::value_t::object)
295 {
296 std::string ifaceName = "xyz.openbmc_project.Configuration.";
297 ifaceName.append(itemType).append(".").append(name);
298
299 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200300 dbus_interface.createInterface(ifacePath, ifaceName, boardNameOrig);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200301
Alexander Hansen89737252025-08-04 15:15:13 +0200302 dbus_interface.populateInterfaceFromJson(
303 systemConfiguration, jsonPointerPath, objectIface, config,
304 getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200305 }
306 else if (config.type() == nlohmann::json::value_t::array)
307 {
308 size_t index = 0;
309 if (config.empty())
310 {
311 return true;
312 }
313 bool isLegal = true;
314 auto type = config[0].type();
315 if (type != nlohmann::json::value_t::object)
316 {
317 return true;
318 }
319
320 // verify legal json
321 for (const auto& arrayItem : config)
322 {
323 if (arrayItem.type() != type)
324 {
325 isLegal = false;
326 break;
327 }
328 }
329 if (!isLegal)
330 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200331 lg2::error("dbus format error {JSON}", "JSON", config);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200332 return false;
333 }
334
335 for (auto& arrayItem : config)
336 {
337 std::string ifaceName = "xyz.openbmc_project.Configuration.";
338 ifaceName.append(itemType).append(".").append(name);
339 ifaceName.append(std::to_string(index));
340
341 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200342 dbus_interface.createInterface(ifacePath, ifaceName,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200343 boardNameOrig);
344
Alexander Hansen89737252025-08-04 15:15:13 +0200345 dbus_interface.populateInterfaceFromJson(
346 systemConfiguration,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200347 jsonPointerPath + "/" + std::to_string(index), objectIface,
Alexander Hansen89737252025-08-04 15:15:13 +0200348 arrayItem, getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200349 index++;
350 }
351 }
352
353 return true;
354}
355
Andrew Jeffery55192932022-03-24 12:29:27 +1030356static bool deviceRequiresPowerOn(const nlohmann::json& entity)
357{
358 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030359 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030360 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030361 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030362 }
363
Ed Tanous3013fb42022-07-09 08:27:06 -0700364 const auto* ptr = powerState->get_ptr<const std::string*>();
365 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030366 {
367 return false;
368 }
369
370 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030371}
372
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030373static void pruneDevice(const nlohmann::json& systemConfiguration,
374 const bool powerOff, const bool scannedPowerOff,
375 const std::string& name, const nlohmann::json& device)
376{
377 if (systemConfiguration.contains(name))
378 {
379 return;
380 }
381
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030382 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030383 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030384 return;
385 }
386
387 logDeviceRemoved(device);
388}
389
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200390void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
391 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700392{
James Feistfb00f392019-06-25 14:16:48 -0700393 if (systemConfiguration.empty() || lastJson.empty())
394 {
395 return; // not ready yet
396 }
James Feist1df06a42019-04-11 14:23:04 -0700397 if (scannedPowerOn)
398 {
399 return;
400 }
401
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200402 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700403 {
404 return;
405 }
406
James Feistb1728ca2020-04-30 15:40:55 -0700407 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030408 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200409 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400410 if (ec == boost::asio::error::operation_aborted)
411 {
412 return;
413 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030414
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200415 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400416 for (const auto& [name, device] : lastJson.items())
417 {
418 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
419 name, device);
420 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030421
Patrick Williamsb7077432024-08-16 15:22:21 -0400422 scannedPowerOff = true;
423 if (!powerOff)
424 {
425 scannedPowerOn = true;
426 }
427 });
James Feist1df06a42019-04-11 14:23:04 -0700428}
429
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200430void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
431 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030432{
433 if (powerOff && deviceRequiresPowerOn(device))
434 {
435 // power not on yet, don't know if it's there or not
436 return;
437 }
438
Alexander Hansen57604ed2025-06-27 13:22:28 +0200439 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030440 for (auto& iface : ifaces)
441 {
442 auto sharedPtr = iface.lock();
443 if (!!sharedPtr)
444 {
445 objServer.remove_interface(sharedPtr);
446 }
447 }
448
449 ifaces.clear();
450 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500451 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030452 logDeviceRemoved(device);
453}
454
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200455void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030456 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200457 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030458 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
459 //
460 // Discord discussion:
461 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
462 //
463 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200464 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030465{
Alexander Hansena555acf2025-06-27 11:59:10 +0200466 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030467
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200468 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200469 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030470 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200471 lg2::error("Error writing json files");
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030472 }
473 });
474
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200475 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
476 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030477 if (count == instance)
478 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200479 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030480 }
481 });
482}
483
James Feist8f2710a2018-05-09 17:18:55 -0700484// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200485void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700486{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200487 propertiesChangedInstance++;
488 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700489
Alexander Hansenb1340da2025-06-27 14:29:13 +0200490 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700491
492 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200493 propertiesChangedTimer.async_wait(
494 [this, count](const boost::system::error_code& ec) {
495 if (ec == boost::asio::error::operation_aborted)
496 {
497 // we were cancelled
498 return;
499 }
500 if (ec)
501 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200502 lg2::error("async wait error {ERR}", "ERR", ec.message());
Alexander Hansenb1340da2025-06-27 14:29:13 +0200503 return;
504 }
James Feist8f2710a2018-05-09 17:18:55 -0700505
Alexander Hansenb1340da2025-06-27 14:29:13 +0200506 if (propertiesChangedInProgress)
507 {
508 propertiesChangedCallback();
509 return;
510 }
511 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700512
Alexander Hansenb1340da2025-06-27 14:29:13 +0200513 nlohmann::json oldConfiguration = systemConfiguration;
514 auto missingConfigurations = std::make_shared<nlohmann::json>();
515 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700516
Alexander Hansenb1340da2025-06-27 14:29:13 +0200517 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200518 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200519 [this, count, oldConfiguration, missingConfigurations]() {
520 // this is something that since ac has been applied to the
521 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200522 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200523 for (const auto& [name, device] :
524 missingConfigurations->items())
525 {
526 pruneConfiguration(powerOff, name, device);
527 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200528 nlohmann::json newConfiguration = systemConfiguration;
529
Christopher Meisf7252572025-06-11 13:22:05 +0200530 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200531
532 for (const auto& [_, device] : newConfiguration.items())
533 {
534 logDeviceAdded(device);
535 }
536
537 propertiesChangedInProgress = false;
538
539 boost::asio::post(io, [this, newConfiguration, count] {
540 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200541 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200542 std::ref(propertiesChangedTimer), newConfiguration);
543 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200544 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200545 perfScan->run();
546 });
James Feist75fdeeb2018-02-20 14:26:16 -0800547}
548
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600549// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400550static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200551 sdbusplus::message_t& msg,
552 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600553{
554 sdbusplus::message::object_path path;
555 DBusObject interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600556 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800557 return std::ranges::any_of(interfaces | std::views::keys,
558 [&probeInterfaces](const auto& ifaceName) {
559 return probeInterfaces.contains(ifaceName);
560 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600561}
562
563// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400564static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200565 sdbusplus::message_t& msg,
566 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600567{
568 sdbusplus::message::object_path path;
Eldin Lee802cbf22025-08-22 14:44:04 +0800569 std::vector<std::string> interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600570 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800571 return std::ranges::any_of(interfaces,
572 [&probeInterfaces](const auto& ifaceName) {
573 return probeInterfaces.contains(ifaceName);
574 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600575}
576
Alexander Hansenad28e402025-06-25 12:38:20 +0200577void EntityManager::handleCurrentConfigurationJson()
578{
Alexander Hansene6651852025-01-21 16:22:05 +0100579 if (EM_CACHE_CONFIGURATION && em_utils::fwVersionIsSame())
Alexander Hansenad28e402025-06-25 12:38:20 +0200580 {
Christopher Meisf7252572025-06-11 13:22:05 +0200581 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200582 {
583 // this file could just be deleted, but it's nice for debug
584 std::filesystem::create_directory(tempConfigDir);
585 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200586 std::filesystem::copy(currentConfiguration, lastConfiguration);
587 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200588
589 std::ifstream jsonStream(lastConfiguration);
590 if (jsonStream.good())
591 {
592 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
593 if (data.is_discarded())
594 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200595 lg2::error("syntax error in {PATH}", "PATH",
596 lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200597 }
598 else
599 {
600 lastJson = std::move(data);
601 }
602 }
603 else
604 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200605 lg2::error("unable to open {PATH}", "PATH", lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200606 }
607 }
608 }
609 else
610 {
611 // not an error, just logging at this level to make it in the journal
Marc Olberdingb2f82822025-09-25 13:56:41 -0700612 std::error_code ec;
Alexander Hansen8feb0452025-09-15 14:29:20 +0200613 lg2::error("Clearing previous configuration");
Marc Olberdingb2f82822025-09-25 13:56:41 -0700614 std::filesystem::remove(currentConfiguration, ec);
Alexander Hansenad28e402025-06-25 12:38:20 +0200615 }
616}
617
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200618void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800619{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200620 if (dbusMatches.contains(path))
621 {
622 return;
623 }
Nan Zhoua3315672022-09-20 19:48:14 +0000624
Alexander Hansen60803182025-06-27 14:41:28 +0200625 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
626
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200627 std::function<void(sdbusplus::message_t & message)> eventHandler =
628 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700629
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200630 sdbusplus::bus::match_t match(
631 static_cast<sdbusplus::bus_t&>(*systemBus),
632 "type='signal',member='PropertiesChanged',path='" + path + "'",
633 eventHandler);
634 dbusMatches.emplace(path, std::move(match));
635}
James Feistfd1264a2018-05-03 12:10:00 -0700636
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200637// We need a poke from DBus for static providers that create all their
638// objects prior to claiming a well-known name, and thus don't emit any
639// org.freedesktop.DBus.Properties signals. Similarly if a process exits
640// for any reason, expected or otherwise, we'll need a poke to remove
641// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200642void EntityManager::initFilters(
643 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200644{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200645 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500646 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500647 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200648 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400649 auto [name, oldOwner,
650 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500651
Patrick Williamsb7077432024-08-16 15:22:21 -0400652 if (name.starts_with(':'))
653 {
654 // We should do nothing with unique-name connections.
655 return;
656 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500657
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200658 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400659 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200660
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500661 // We also need a poke from DBus when new interfaces are created or
662 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200663 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500664 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500665 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200666 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400667 if (iaContainsProbeInterface(msg, probeInterfaces))
668 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200669 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400670 }
671 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200672
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200673 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500674 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500675 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200676 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400677 if (irContainsProbeInterface(msg, probeInterfaces))
678 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200679 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400680 }
681 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200682}