blob: a3b4a726fa5f25f394be7722482f3516726cdcc5 [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,
Alexander Hansen8290ca42025-08-04 15:27:22 +020055 boost::asio::io_context& io,
56 const std::vector<std::filesystem::path>& configurationDirectories) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020057 systemBus(systemBus),
58 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
Alexander Hansen8290ca42025-08-04 15:27:22 +020059 configuration(configurationDirectories), lastJson(nlohmann::json::object()),
Alexander Hansenb1340da2025-06-27 14:29:13 +020060 systemConfiguration(nlohmann::json::object()), io(io),
Alexander Hansen89737252025-08-04 15:15:13 +020061 dbus_interface(io, objServer), powerStatus(*systemBus),
62 propertiesChangedTimer(io)
Christopher Meiscf6a75b2025-06-03 07:53:50 +020063{
64 // All other objects that EntityManager currently support are under the
65 // inventory subtree.
66 // See the discussion at
67 // https://discord.com/channels/775381525260664832/1018929092009144380
68 objServer.add_manager("/xyz/openbmc_project/inventory");
James Feist75fdeeb2018-02-20 14:26:16 -080069
Christopher Meiscf6a75b2025-06-03 07:53:50 +020070 entityIface = objServer.add_interface("/xyz/openbmc_project/EntityManager",
71 "xyz.openbmc_project.EntityManager");
72 entityIface->register_method("ReScan", [this]() {
73 propertiesChangedCallback();
74 });
75 dbus_interface::tryIfaceInitialize(entityIface);
Christopher Meisf7252572025-06-11 13:22:05 +020076
77 initFilters(configuration.probeInterfaces);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020078}
79
80void EntityManager::postToDbus(const nlohmann::json& newConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -080081{
Matt Spinler6eb60972023-08-14 16:36:20 -050082 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +000083
James Feist97a63f12018-05-17 13:50:57 -070084 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -050085 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -080086 {
Alexander Hansen4fcd9462025-07-30 17:44:44 +020087 postBoardToDBus(boardId, boardConfig, newBoards);
James Feist1b2e2242018-01-30 13:45:19 -080088 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000089
Matt Spinler6eb60972023-08-14 16:36:20 -050090 for (const auto& [assocPath, assocPropValue] :
Alexander Hansen32e74182025-08-20 16:16:00 +020091 topology.getAssocs(std::views::keys(newBoards)))
Benjamin Fairca2eb042022-09-13 06:40:42 +000092 {
Matt Spinler6eb60972023-08-14 16:36:20 -050093 auto findBoard = newBoards.find(assocPath);
94 if (findBoard == newBoards.end())
95 {
96 continue;
97 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000098
Alexander Hansen57604ed2025-06-27 13:22:28 +020099 auto ifacePtr = dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200100 assocPath,
101 sdbusplus::common::xyz::openbmc_project::association::Definitions::
102 interface,
Matt Spinler6eb60972023-08-14 16:36:20 -0500103 findBoard->second);
104
105 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200106 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000107 }
James Feist1b2e2242018-01-30 13:45:19 -0800108}
109
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200110void EntityManager::postBoardToDBus(
111 const std::string& boardId, const nlohmann::json& boardConfig,
112 std::map<std::string, std::string>& newBoards)
113{
114 std::string boardName = boardConfig["Name"];
115 std::string boardNameOrig = boardConfig["Name"];
116 std::string jsonPointerPath = "/" + boardId;
117 // loop through newConfiguration, but use values from system
118 // configuration to be able to modify via dbus later
119 auto boardValues = systemConfiguration[boardId];
120 auto findBoardType = boardValues.find("Type");
121 std::string boardType;
122 if (findBoardType != boardValues.end() &&
123 findBoardType->type() == nlohmann::json::value_t::string)
124 {
125 boardType = findBoardType->get<std::string>();
126 std::regex_replace(boardType.begin(), boardType.begin(),
127 boardType.end(), illegalDbusMemberRegex, "_");
128 }
129 else
130 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200131 lg2::error("Unable to find type for {BOARD} reverting to Chassis.",
132 "BOARD", boardName);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200133 boardType = "Chassis";
134 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200135
Christopher Meis811160e2025-08-08 08:48:37 +0200136 const std::string boardPath =
137 em_utils::buildInventorySystemPath(boardName, boardType);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200138
139 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200140 dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200141 boardPath,
142 sdbusplus::common::xyz::openbmc_project::inventory::Item::interface,
143 boardName);
144
145 const std::string invItemIntf = std::format(
146 "{}.{}",
147 sdbusplus::common::xyz::openbmc_project::inventory::Item::interface,
148 boardType);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200149
150 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200151 dbus_interface.createInterface(boardPath, invItemIntf, boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200152
Alexander Hansen89737252025-08-04 15:15:13 +0200153 dbus_interface.createAddObjectMethod(jsonPointerPath, boardPath,
154 systemConfiguration, boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200155
Alexander Hansen89737252025-08-04 15:15:13 +0200156 dbus_interface.populateInterfaceFromJson(
157 systemConfiguration, jsonPointerPath, boardIface, boardValues);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200158 jsonPointerPath += "/";
159 // iterate through board properties
160 for (const auto& [propName, propValue] : boardValues.items())
161 {
162 if (propValue.type() == nlohmann::json::value_t::object)
163 {
164 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen89737252025-08-04 15:15:13 +0200165 dbus_interface.createInterface(boardPath, propName,
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200166 boardNameOrig);
167
Alexander Hansen89737252025-08-04 15:15:13 +0200168 dbus_interface.populateInterfaceFromJson(
169 systemConfiguration, jsonPointerPath + propName, iface,
170 propValue);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200171 }
172 }
173
Alexander Hansen4fa40122025-07-30 18:26:35 +0200174 nlohmann::json::iterator exposes = boardValues.find("Exposes");
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200175 if (exposes == boardValues.end())
176 {
177 return;
178 }
179 // iterate through exposes
180 jsonPointerPath += "Exposes/";
181
182 // store the board level pointer so we can modify it on the way down
183 std::string jsonPointerPathBoard = jsonPointerPath;
184 size_t exposesIndex = -1;
Alexander Hansen4fa40122025-07-30 18:26:35 +0200185 for (nlohmann::json& item : *exposes)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200186 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200187 postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
188 jsonPointerPath, jsonPointerPathBoard,
189 boardPath, boardType);
190 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200191
Alexander Hansen4fa40122025-07-30 18:26:35 +0200192 newBoards.emplace(boardPath, boardNameOrig);
193}
194
195void EntityManager::postExposesRecordsToDBus(
196 nlohmann::json& item, size_t& exposesIndex,
197 const std::string& boardNameOrig, std::string jsonPointerPath,
198 const std::string& jsonPointerPathBoard, const std::string& boardPath,
199 const std::string& boardType)
200{
201 exposesIndex++;
202 jsonPointerPath = jsonPointerPathBoard;
203 jsonPointerPath += std::to_string(exposesIndex);
204
205 auto findName = item.find("Name");
206 if (findName == item.end())
207 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200208 lg2::error("cannot find name in field {ITEM}", "ITEM", item);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200209 return;
210 }
211 auto findStatus = item.find("Status");
212 // if status is not found it is assumed to be status = 'okay'
213 if (findStatus != item.end())
214 {
215 if (*findStatus == "disabled")
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200216 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200217 return;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200218 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200219 }
220 auto findType = item.find("Type");
221 std::string itemType;
222 if (findType != item.end())
223 {
224 itemType = findType->get<std::string>();
225 std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
226 illegalDbusPathRegex, "_");
227 }
228 else
229 {
230 itemType = "unknown";
231 }
232 std::string itemName = findName->get<std::string>();
233 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
234 illegalDbusMemberRegex, "_");
235 std::string ifacePath = boardPath;
236 ifacePath += "/";
237 ifacePath += itemName;
238
239 if (itemType == "BMC")
240 {
241 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
242 dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200243 ifacePath,
244 sdbusplus::common::xyz::openbmc_project::inventory::item::Bmc::
245 interface,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200246 boardNameOrig);
Alexander Hansen89737252025-08-04 15:15:13 +0200247 dbus_interface.populateInterfaceFromJson(
248 systemConfiguration, jsonPointerPath, bmcIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200249 getPermission(itemType));
250 }
251 else if (itemType == "System")
252 {
253 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
254 dbus_interface.createInterface(
Alexander Hansenc58af0b2025-09-12 15:47:19 +0200255 ifacePath,
256 sdbusplus::common::xyz::openbmc_project::inventory::item::
257 System::interface,
Alexander Hansen89737252025-08-04 15:15:13 +0200258 boardNameOrig);
259 dbus_interface.populateInterfaceFromJson(
260 systemConfiguration, jsonPointerPath, systemIface, item,
261 getPermission(itemType));
Alexander Hansen4fa40122025-07-30 18:26:35 +0200262 }
263
264 for (const auto& [name, config] : item.items())
265 {
266 jsonPointerPath = jsonPointerPathBoard;
267 jsonPointerPath.append(std::to_string(exposesIndex))
268 .append("/")
269 .append(name);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200270
271 if (!postConfigurationRecord(name, config, boardNameOrig, itemType,
272 jsonPointerPath, ifacePath))
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200273 {
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200274 break;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200275 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200276 }
277
Alexander Hansen4fa40122025-07-30 18:26:35 +0200278 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
279 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200280 ifacePath, "xyz.openbmc_project.Configuration." + itemType,
281 boardNameOrig);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200282
Alexander Hansen89737252025-08-04 15:15:13 +0200283 dbus_interface.populateInterfaceFromJson(
284 systemConfiguration, jsonPointerPath, itemIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200285 getPermission(itemType));
286
287 topology.addBoard(boardPath, boardType, boardNameOrig, item);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200288}
289
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200290bool EntityManager::postConfigurationRecord(
291 const std::string& name, nlohmann::json& config,
292 const std::string& boardNameOrig, const std::string& itemType,
293 const std::string& jsonPointerPath, const std::string& ifacePath)
294{
295 if (config.type() == nlohmann::json::value_t::object)
296 {
297 std::string ifaceName = "xyz.openbmc_project.Configuration.";
298 ifaceName.append(itemType).append(".").append(name);
299
300 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200301 dbus_interface.createInterface(ifacePath, ifaceName, boardNameOrig);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200302
Alexander Hansen89737252025-08-04 15:15:13 +0200303 dbus_interface.populateInterfaceFromJson(
304 systemConfiguration, jsonPointerPath, objectIface, config,
305 getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200306 }
307 else if (config.type() == nlohmann::json::value_t::array)
308 {
309 size_t index = 0;
310 if (config.empty())
311 {
312 return true;
313 }
314 bool isLegal = true;
315 auto type = config[0].type();
316 if (type != nlohmann::json::value_t::object)
317 {
318 return true;
319 }
320
321 // verify legal json
322 for (const auto& arrayItem : config)
323 {
324 if (arrayItem.type() != type)
325 {
326 isLegal = false;
327 break;
328 }
329 }
330 if (!isLegal)
331 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200332 lg2::error("dbus format error {JSON}", "JSON", config);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200333 return false;
334 }
335
336 for (auto& arrayItem : config)
337 {
338 std::string ifaceName = "xyz.openbmc_project.Configuration.";
339 ifaceName.append(itemType).append(".").append(name);
340 ifaceName.append(std::to_string(index));
341
342 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200343 dbus_interface.createInterface(ifacePath, ifaceName,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200344 boardNameOrig);
345
Alexander Hansen89737252025-08-04 15:15:13 +0200346 dbus_interface.populateInterfaceFromJson(
347 systemConfiguration,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200348 jsonPointerPath + "/" + std::to_string(index), objectIface,
Alexander Hansen89737252025-08-04 15:15:13 +0200349 arrayItem, getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200350 index++;
351 }
352 }
353
354 return true;
355}
356
Andrew Jeffery55192932022-03-24 12:29:27 +1030357static bool deviceRequiresPowerOn(const nlohmann::json& entity)
358{
359 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030360 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030361 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030362 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030363 }
364
Ed Tanous3013fb42022-07-09 08:27:06 -0700365 const auto* ptr = powerState->get_ptr<const std::string*>();
366 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030367 {
368 return false;
369 }
370
371 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030372}
373
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030374static void pruneDevice(const nlohmann::json& systemConfiguration,
375 const bool powerOff, const bool scannedPowerOff,
376 const std::string& name, const nlohmann::json& device)
377{
378 if (systemConfiguration.contains(name))
379 {
380 return;
381 }
382
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030383 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030384 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030385 return;
386 }
387
388 logDeviceRemoved(device);
389}
390
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200391void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
392 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700393{
James Feistfb00f392019-06-25 14:16:48 -0700394 if (systemConfiguration.empty() || lastJson.empty())
395 {
396 return; // not ready yet
397 }
James Feist1df06a42019-04-11 14:23:04 -0700398 if (scannedPowerOn)
399 {
400 return;
401 }
402
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200403 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700404 {
405 return;
406 }
407
James Feistb1728ca2020-04-30 15:40:55 -0700408 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030409 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200410 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400411 if (ec == boost::asio::error::operation_aborted)
412 {
413 return;
414 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030415
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200416 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400417 for (const auto& [name, device] : lastJson.items())
418 {
419 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
420 name, device);
421 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030422
Patrick Williamsb7077432024-08-16 15:22:21 -0400423 scannedPowerOff = true;
424 if (!powerOff)
425 {
426 scannedPowerOn = true;
427 }
428 });
James Feist1df06a42019-04-11 14:23:04 -0700429}
430
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200431void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
432 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030433{
434 if (powerOff && deviceRequiresPowerOn(device))
435 {
436 // power not on yet, don't know if it's there or not
437 return;
438 }
439
Alexander Hansen57604ed2025-06-27 13:22:28 +0200440 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030441 for (auto& iface : ifaces)
442 {
443 auto sharedPtr = iface.lock();
444 if (!!sharedPtr)
445 {
446 objServer.remove_interface(sharedPtr);
447 }
448 }
449
450 ifaces.clear();
451 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500452 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030453 logDeviceRemoved(device);
454}
455
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200456void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030457 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200458 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030459 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
460 //
461 // Discord discussion:
462 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
463 //
464 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200465 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030466{
Alexander Hansena555acf2025-06-27 11:59:10 +0200467 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030468
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200469 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200470 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030471 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200472 lg2::error("Error writing json files");
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030473 }
474 });
475
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200476 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
477 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030478 if (count == instance)
479 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200480 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030481 }
482 });
483}
484
James Feist8f2710a2018-05-09 17:18:55 -0700485// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200486void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700487{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200488 propertiesChangedInstance++;
489 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700490
Alexander Hansenb1340da2025-06-27 14:29:13 +0200491 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700492
493 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200494 propertiesChangedTimer.async_wait(
495 [this, count](const boost::system::error_code& ec) {
496 if (ec == boost::asio::error::operation_aborted)
497 {
498 // we were cancelled
499 return;
500 }
501 if (ec)
502 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200503 lg2::error("async wait error {ERR}", "ERR", ec.message());
Alexander Hansenb1340da2025-06-27 14:29:13 +0200504 return;
505 }
James Feist8f2710a2018-05-09 17:18:55 -0700506
Alexander Hansenb1340da2025-06-27 14:29:13 +0200507 if (propertiesChangedInProgress)
508 {
509 propertiesChangedCallback();
510 return;
511 }
512 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700513
Alexander Hansenb1340da2025-06-27 14:29:13 +0200514 nlohmann::json oldConfiguration = systemConfiguration;
515 auto missingConfigurations = std::make_shared<nlohmann::json>();
516 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700517
Alexander Hansenb1340da2025-06-27 14:29:13 +0200518 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200519 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200520 [this, count, oldConfiguration, missingConfigurations]() {
521 // this is something that since ac has been applied to the
522 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200523 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200524 for (const auto& [name, device] :
525 missingConfigurations->items())
526 {
527 pruneConfiguration(powerOff, name, device);
528 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200529 nlohmann::json newConfiguration = systemConfiguration;
530
Christopher Meisf7252572025-06-11 13:22:05 +0200531 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200532
533 for (const auto& [_, device] : newConfiguration.items())
534 {
535 logDeviceAdded(device);
536 }
537
538 propertiesChangedInProgress = false;
539
540 boost::asio::post(io, [this, newConfiguration, count] {
541 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200542 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200543 std::ref(propertiesChangedTimer), newConfiguration);
544 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200545 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200546 perfScan->run();
547 });
James Feist75fdeeb2018-02-20 14:26:16 -0800548}
549
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600550// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400551static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200552 sdbusplus::message_t& msg,
553 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600554{
555 sdbusplus::message::object_path path;
556 DBusObject interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600557 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800558 return std::ranges::any_of(interfaces | std::views::keys,
559 [&probeInterfaces](const auto& ifaceName) {
560 return probeInterfaces.contains(ifaceName);
561 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600562}
563
564// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400565static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200566 sdbusplus::message_t& msg,
567 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600568{
569 sdbusplus::message::object_path path;
Eldin Lee802cbf22025-08-22 14:44:04 +0800570 std::vector<std::string> interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600571 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800572 return std::ranges::any_of(interfaces,
573 [&probeInterfaces](const auto& ifaceName) {
574 return probeInterfaces.contains(ifaceName);
575 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600576}
577
Alexander Hansenad28e402025-06-25 12:38:20 +0200578void EntityManager::handleCurrentConfigurationJson()
579{
Alexander Hansene6651852025-01-21 16:22:05 +0100580 if (EM_CACHE_CONFIGURATION && em_utils::fwVersionIsSame())
Alexander Hansenad28e402025-06-25 12:38:20 +0200581 {
Christopher Meisf7252572025-06-11 13:22:05 +0200582 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200583 {
584 // this file could just be deleted, but it's nice for debug
585 std::filesystem::create_directory(tempConfigDir);
586 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200587 std::filesystem::copy(currentConfiguration, lastConfiguration);
588 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200589
590 std::ifstream jsonStream(lastConfiguration);
591 if (jsonStream.good())
592 {
593 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
594 if (data.is_discarded())
595 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200596 lg2::error("syntax error in {PATH}", "PATH",
597 lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200598 }
599 else
600 {
601 lastJson = std::move(data);
602 }
603 }
604 else
605 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200606 lg2::error("unable to open {PATH}", "PATH", lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200607 }
608 }
609 }
610 else
611 {
612 // not an error, just logging at this level to make it in the journal
Marc Olberdingb2f82822025-09-25 13:56:41 -0700613 std::error_code ec;
Alexander Hansen8feb0452025-09-15 14:29:20 +0200614 lg2::error("Clearing previous configuration");
Marc Olberdingb2f82822025-09-25 13:56:41 -0700615 std::filesystem::remove(currentConfiguration, ec);
Alexander Hansenad28e402025-06-25 12:38:20 +0200616 }
617}
618
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200619void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800620{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200621 if (dbusMatches.contains(path))
622 {
623 return;
624 }
Nan Zhoua3315672022-09-20 19:48:14 +0000625
Alexander Hansen60803182025-06-27 14:41:28 +0200626 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
627
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200628 std::function<void(sdbusplus::message_t & message)> eventHandler =
629 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700630
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200631 sdbusplus::bus::match_t match(
632 static_cast<sdbusplus::bus_t&>(*systemBus),
633 "type='signal',member='PropertiesChanged',path='" + path + "'",
634 eventHandler);
635 dbusMatches.emplace(path, std::move(match));
636}
James Feistfd1264a2018-05-03 12:10:00 -0700637
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200638// We need a poke from DBus for static providers that create all their
639// objects prior to claiming a well-known name, and thus don't emit any
640// org.freedesktop.DBus.Properties signals. Similarly if a process exits
641// for any reason, expected or otherwise, we'll need a poke to remove
642// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200643void EntityManager::initFilters(
644 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200645{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200646 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500647 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500648 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200649 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400650 auto [name, oldOwner,
651 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500652
Patrick Williamsb7077432024-08-16 15:22:21 -0400653 if (name.starts_with(':'))
654 {
655 // We should do nothing with unique-name connections.
656 return;
657 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500658
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200659 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400660 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200661
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500662 // We also need a poke from DBus when new interfaces are created or
663 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200664 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500665 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500666 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200667 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400668 if (iaContainsProbeInterface(msg, probeInterfaces))
669 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200670 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400671 }
672 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200673
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200674 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500675 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500676 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200677 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400678 if (irContainsProbeInterface(msg, probeInterfaces))
679 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200680 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400681 }
682 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200683}