blob: f9560f369e83f7a94a97622bdd4fea1b2b958e78 [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>
26
James Feist637b3ef2019-04-15 16:35:30 -070027#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080028#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103029#include <functional>
Andrew Jeffery666583b2021-12-01 15:50:12 +103030#include <map>
James Feista465ccc2019-02-08 12:51:01 -080031#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070032constexpr const char* tempConfigDir = "/tmp/configuration/";
33constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
James Feistf1b14142019-04-10 15:22:09 -070034
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020035static constexpr std::array<const char*, 6> settableInterfaces = {
36 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist3cb5fec2018-01-23 14:41:51 -080037
Ed Tanous07d467b2021-02-23 14:48:37 -080038const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
39const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080040
James Feista465ccc2019-02-08 12:51:01 -080041sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -070042{
43 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
44 interface) != settableInterfaces.end()
45 ? sdbusplus::asio::PropertyPermission::readWrite
46 : sdbusplus::asio::PropertyPermission::readOnly;
47}
48
Christopher Meiscf6a75b2025-06-03 07:53:50 +020049EntityManager::EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020050 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
51 boost::asio::io_context& io) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020052 systemBus(systemBus),
53 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
54 lastJson(nlohmann::json::object()),
Alexander Hansenb1340da2025-06-27 14:29:13 +020055 systemConfiguration(nlohmann::json::object()), io(io),
Alexander Hansen89737252025-08-04 15:15:13 +020056 dbus_interface(io, objServer), powerStatus(*systemBus),
57 propertiesChangedTimer(io)
Christopher Meiscf6a75b2025-06-03 07:53:50 +020058{
59 // All other objects that EntityManager currently support are under the
60 // inventory subtree.
61 // See the discussion at
62 // https://discord.com/channels/775381525260664832/1018929092009144380
63 objServer.add_manager("/xyz/openbmc_project/inventory");
James Feist75fdeeb2018-02-20 14:26:16 -080064
Christopher Meiscf6a75b2025-06-03 07:53:50 +020065 entityIface = objServer.add_interface("/xyz/openbmc_project/EntityManager",
66 "xyz.openbmc_project.EntityManager");
67 entityIface->register_method("ReScan", [this]() {
68 propertiesChangedCallback();
69 });
70 dbus_interface::tryIfaceInitialize(entityIface);
Christopher Meisf7252572025-06-11 13:22:05 +020071
72 initFilters(configuration.probeInterfaces);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020073}
74
75void EntityManager::postToDbus(const nlohmann::json& newConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -080076{
Matt Spinler6eb60972023-08-14 16:36:20 -050077 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +000078
James Feist97a63f12018-05-17 13:50:57 -070079 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -050080 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -080081 {
Alexander Hansen4fcd9462025-07-30 17:44:44 +020082 postBoardToDBus(boardId, boardConfig, newBoards);
James Feist1b2e2242018-01-30 13:45:19 -080083 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000084
Matt Spinler6eb60972023-08-14 16:36:20 -050085 for (const auto& [assocPath, assocPropValue] :
Alexander Hansen32e74182025-08-20 16:16:00 +020086 topology.getAssocs(std::views::keys(newBoards)))
Benjamin Fairca2eb042022-09-13 06:40:42 +000087 {
Matt Spinler6eb60972023-08-14 16:36:20 -050088 auto findBoard = newBoards.find(assocPath);
89 if (findBoard == newBoards.end())
90 {
91 continue;
92 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000093
Alexander Hansen57604ed2025-06-27 13:22:28 +020094 auto ifacePtr = dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +020095 assocPath, "xyz.openbmc_project.Association.Definitions",
Matt Spinler6eb60972023-08-14 16:36:20 -050096 findBoard->second);
97
98 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +020099 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000100 }
James Feist1b2e2242018-01-30 13:45:19 -0800101}
102
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200103void EntityManager::postBoardToDBus(
104 const std::string& boardId, const nlohmann::json& boardConfig,
105 std::map<std::string, std::string>& newBoards)
106{
107 std::string boardName = boardConfig["Name"];
108 std::string boardNameOrig = boardConfig["Name"];
109 std::string jsonPointerPath = "/" + boardId;
110 // loop through newConfiguration, but use values from system
111 // configuration to be able to modify via dbus later
112 auto boardValues = systemConfiguration[boardId];
113 auto findBoardType = boardValues.find("Type");
114 std::string boardType;
115 if (findBoardType != boardValues.end() &&
116 findBoardType->type() == nlohmann::json::value_t::string)
117 {
118 boardType = findBoardType->get<std::string>();
119 std::regex_replace(boardType.begin(), boardType.begin(),
120 boardType.end(), illegalDbusMemberRegex, "_");
121 }
122 else
123 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200124 lg2::error("Unable to find type for {BOARD} reverting to Chassis.",
125 "BOARD", boardName);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200126 boardType = "Chassis";
127 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200128
Christopher Meis811160e2025-08-08 08:48:37 +0200129 const std::string boardPath =
130 em_utils::buildInventorySystemPath(boardName, boardType);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200131
132 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200133 dbus_interface.createInterface(
134 boardPath, "xyz.openbmc_project.Inventory.Item", boardName);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200135
136 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
137 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200138 boardPath, "xyz.openbmc_project.Inventory.Item." + boardType,
139 boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200140
Alexander Hansen89737252025-08-04 15:15:13 +0200141 dbus_interface.createAddObjectMethod(jsonPointerPath, boardPath,
142 systemConfiguration, boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200143
Alexander Hansen89737252025-08-04 15:15:13 +0200144 dbus_interface.populateInterfaceFromJson(
145 systemConfiguration, jsonPointerPath, boardIface, boardValues);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200146 jsonPointerPath += "/";
147 // iterate through board properties
148 for (const auto& [propName, propValue] : boardValues.items())
149 {
150 if (propValue.type() == nlohmann::json::value_t::object)
151 {
152 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen89737252025-08-04 15:15:13 +0200153 dbus_interface.createInterface(boardPath, propName,
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200154 boardNameOrig);
155
Alexander Hansen89737252025-08-04 15:15:13 +0200156 dbus_interface.populateInterfaceFromJson(
157 systemConfiguration, jsonPointerPath + propName, iface,
158 propValue);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200159 }
160 }
161
Alexander Hansen4fa40122025-07-30 18:26:35 +0200162 nlohmann::json::iterator exposes = boardValues.find("Exposes");
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200163 if (exposes == boardValues.end())
164 {
165 return;
166 }
167 // iterate through exposes
168 jsonPointerPath += "Exposes/";
169
170 // store the board level pointer so we can modify it on the way down
171 std::string jsonPointerPathBoard = jsonPointerPath;
172 size_t exposesIndex = -1;
Alexander Hansen4fa40122025-07-30 18:26:35 +0200173 for (nlohmann::json& item : *exposes)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200174 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200175 postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
176 jsonPointerPath, jsonPointerPathBoard,
177 boardPath, boardType);
178 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200179
Alexander Hansen4fa40122025-07-30 18:26:35 +0200180 newBoards.emplace(boardPath, boardNameOrig);
181}
182
183void EntityManager::postExposesRecordsToDBus(
184 nlohmann::json& item, size_t& exposesIndex,
185 const std::string& boardNameOrig, std::string jsonPointerPath,
186 const std::string& jsonPointerPathBoard, const std::string& boardPath,
187 const std::string& boardType)
188{
189 exposesIndex++;
190 jsonPointerPath = jsonPointerPathBoard;
191 jsonPointerPath += std::to_string(exposesIndex);
192
193 auto findName = item.find("Name");
194 if (findName == item.end())
195 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200196 lg2::error("cannot find name in field {ITEM}", "ITEM", item);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200197 return;
198 }
199 auto findStatus = item.find("Status");
200 // if status is not found it is assumed to be status = 'okay'
201 if (findStatus != item.end())
202 {
203 if (*findStatus == "disabled")
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200204 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200205 return;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200206 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200207 }
208 auto findType = item.find("Type");
209 std::string itemType;
210 if (findType != item.end())
211 {
212 itemType = findType->get<std::string>();
213 std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
214 illegalDbusPathRegex, "_");
215 }
216 else
217 {
218 itemType = "unknown";
219 }
220 std::string itemName = findName->get<std::string>();
221 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
222 illegalDbusMemberRegex, "_");
223 std::string ifacePath = boardPath;
224 ifacePath += "/";
225 ifacePath += itemName;
226
227 if (itemType == "BMC")
228 {
229 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
230 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200231 ifacePath, "xyz.openbmc_project.Inventory.Item.Bmc",
Alexander Hansen4fa40122025-07-30 18:26:35 +0200232 boardNameOrig);
Alexander Hansen89737252025-08-04 15:15:13 +0200233 dbus_interface.populateInterfaceFromJson(
234 systemConfiguration, jsonPointerPath, bmcIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200235 getPermission(itemType));
236 }
237 else if (itemType == "System")
238 {
239 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
240 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200241 ifacePath, "xyz.openbmc_project.Inventory.Item.System",
242 boardNameOrig);
243 dbus_interface.populateInterfaceFromJson(
244 systemConfiguration, jsonPointerPath, systemIface, item,
245 getPermission(itemType));
Alexander Hansen4fa40122025-07-30 18:26:35 +0200246 }
247
248 for (const auto& [name, config] : item.items())
249 {
250 jsonPointerPath = jsonPointerPathBoard;
251 jsonPointerPath.append(std::to_string(exposesIndex))
252 .append("/")
253 .append(name);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200254
255 if (!postConfigurationRecord(name, config, boardNameOrig, itemType,
256 jsonPointerPath, ifacePath))
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200257 {
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200258 break;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200259 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200260 }
261
Alexander Hansen4fa40122025-07-30 18:26:35 +0200262 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
263 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200264 ifacePath, "xyz.openbmc_project.Configuration." + itemType,
265 boardNameOrig);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200266
Alexander Hansen89737252025-08-04 15:15:13 +0200267 dbus_interface.populateInterfaceFromJson(
268 systemConfiguration, jsonPointerPath, itemIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200269 getPermission(itemType));
270
271 topology.addBoard(boardPath, boardType, boardNameOrig, item);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200272}
273
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200274bool EntityManager::postConfigurationRecord(
275 const std::string& name, nlohmann::json& config,
276 const std::string& boardNameOrig, const std::string& itemType,
277 const std::string& jsonPointerPath, const std::string& ifacePath)
278{
279 if (config.type() == nlohmann::json::value_t::object)
280 {
281 std::string ifaceName = "xyz.openbmc_project.Configuration.";
282 ifaceName.append(itemType).append(".").append(name);
283
284 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200285 dbus_interface.createInterface(ifacePath, ifaceName, boardNameOrig);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200286
Alexander Hansen89737252025-08-04 15:15:13 +0200287 dbus_interface.populateInterfaceFromJson(
288 systemConfiguration, jsonPointerPath, objectIface, config,
289 getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200290 }
291 else if (config.type() == nlohmann::json::value_t::array)
292 {
293 size_t index = 0;
294 if (config.empty())
295 {
296 return true;
297 }
298 bool isLegal = true;
299 auto type = config[0].type();
300 if (type != nlohmann::json::value_t::object)
301 {
302 return true;
303 }
304
305 // verify legal json
306 for (const auto& arrayItem : config)
307 {
308 if (arrayItem.type() != type)
309 {
310 isLegal = false;
311 break;
312 }
313 }
314 if (!isLegal)
315 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200316 lg2::error("dbus format error {JSON}", "JSON", config);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200317 return false;
318 }
319
320 for (auto& arrayItem : config)
321 {
322 std::string ifaceName = "xyz.openbmc_project.Configuration.";
323 ifaceName.append(itemType).append(".").append(name);
324 ifaceName.append(std::to_string(index));
325
326 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200327 dbus_interface.createInterface(ifacePath, ifaceName,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200328 boardNameOrig);
329
Alexander Hansen89737252025-08-04 15:15:13 +0200330 dbus_interface.populateInterfaceFromJson(
331 systemConfiguration,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200332 jsonPointerPath + "/" + std::to_string(index), objectIface,
Alexander Hansen89737252025-08-04 15:15:13 +0200333 arrayItem, getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200334 index++;
335 }
336 }
337
338 return true;
339}
340
Andrew Jeffery55192932022-03-24 12:29:27 +1030341static bool deviceRequiresPowerOn(const nlohmann::json& entity)
342{
343 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030344 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030345 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030346 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030347 }
348
Ed Tanous3013fb42022-07-09 08:27:06 -0700349 const auto* ptr = powerState->get_ptr<const std::string*>();
350 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030351 {
352 return false;
353 }
354
355 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030356}
357
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030358static void pruneDevice(const nlohmann::json& systemConfiguration,
359 const bool powerOff, const bool scannedPowerOff,
360 const std::string& name, const nlohmann::json& device)
361{
362 if (systemConfiguration.contains(name))
363 {
364 return;
365 }
366
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030367 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030368 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030369 return;
370 }
371
372 logDeviceRemoved(device);
373}
374
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200375void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
376 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700377{
James Feistfb00f392019-06-25 14:16:48 -0700378 if (systemConfiguration.empty() || lastJson.empty())
379 {
380 return; // not ready yet
381 }
James Feist1df06a42019-04-11 14:23:04 -0700382 if (scannedPowerOn)
383 {
384 return;
385 }
386
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200387 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700388 {
389 return;
390 }
391
James Feistb1728ca2020-04-30 15:40:55 -0700392 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030393 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200394 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400395 if (ec == boost::asio::error::operation_aborted)
396 {
397 return;
398 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030399
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200400 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400401 for (const auto& [name, device] : lastJson.items())
402 {
403 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
404 name, device);
405 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030406
Patrick Williamsb7077432024-08-16 15:22:21 -0400407 scannedPowerOff = true;
408 if (!powerOff)
409 {
410 scannedPowerOn = true;
411 }
412 });
James Feist1df06a42019-04-11 14:23:04 -0700413}
414
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200415void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
416 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030417{
418 if (powerOff && deviceRequiresPowerOn(device))
419 {
420 // power not on yet, don't know if it's there or not
421 return;
422 }
423
Alexander Hansen57604ed2025-06-27 13:22:28 +0200424 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030425 for (auto& iface : ifaces)
426 {
427 auto sharedPtr = iface.lock();
428 if (!!sharedPtr)
429 {
430 objServer.remove_interface(sharedPtr);
431 }
432 }
433
434 ifaces.clear();
435 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500436 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030437 logDeviceRemoved(device);
438}
439
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200440void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030441 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200442 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030443 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
444 //
445 // Discord discussion:
446 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
447 //
448 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200449 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030450{
Alexander Hansena555acf2025-06-27 11:59:10 +0200451 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030452
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200453 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200454 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030455 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200456 lg2::error("Error writing json files");
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030457 }
458 });
459
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200460 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
461 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030462 if (count == instance)
463 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200464 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030465 }
466 });
467}
468
James Feist8f2710a2018-05-09 17:18:55 -0700469// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200470void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700471{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200472 propertiesChangedInstance++;
473 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700474
Alexander Hansenb1340da2025-06-27 14:29:13 +0200475 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700476
477 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200478 propertiesChangedTimer.async_wait(
479 [this, count](const boost::system::error_code& ec) {
480 if (ec == boost::asio::error::operation_aborted)
481 {
482 // we were cancelled
483 return;
484 }
485 if (ec)
486 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200487 lg2::error("async wait error {ERR}", "ERR", ec.message());
Alexander Hansenb1340da2025-06-27 14:29:13 +0200488 return;
489 }
James Feist8f2710a2018-05-09 17:18:55 -0700490
Alexander Hansenb1340da2025-06-27 14:29:13 +0200491 if (propertiesChangedInProgress)
492 {
493 propertiesChangedCallback();
494 return;
495 }
496 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700497
Alexander Hansenb1340da2025-06-27 14:29:13 +0200498 nlohmann::json oldConfiguration = systemConfiguration;
499 auto missingConfigurations = std::make_shared<nlohmann::json>();
500 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700501
Alexander Hansenb1340da2025-06-27 14:29:13 +0200502 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200503 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200504 [this, count, oldConfiguration, missingConfigurations]() {
505 // this is something that since ac has been applied to the
506 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200507 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200508 for (const auto& [name, device] :
509 missingConfigurations->items())
510 {
511 pruneConfiguration(powerOff, name, device);
512 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200513 nlohmann::json newConfiguration = systemConfiguration;
514
Christopher Meisf7252572025-06-11 13:22:05 +0200515 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200516
517 for (const auto& [_, device] : newConfiguration.items())
518 {
519 logDeviceAdded(device);
520 }
521
522 propertiesChangedInProgress = false;
523
524 boost::asio::post(io, [this, newConfiguration, count] {
525 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200526 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200527 std::ref(propertiesChangedTimer), newConfiguration);
528 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200529 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200530 perfScan->run();
531 });
James Feist75fdeeb2018-02-20 14:26:16 -0800532}
533
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600534// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400535static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200536 sdbusplus::message_t& msg,
537 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600538{
539 sdbusplus::message::object_path path;
540 DBusObject interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600541 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800542 return std::ranges::any_of(interfaces | std::views::keys,
543 [&probeInterfaces](const auto& ifaceName) {
544 return probeInterfaces.contains(ifaceName);
545 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600546}
547
548// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400549static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200550 sdbusplus::message_t& msg,
551 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600552{
553 sdbusplus::message::object_path path;
Eldin Lee802cbf22025-08-22 14:44:04 +0800554 std::vector<std::string> interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600555 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800556 return std::ranges::any_of(interfaces,
557 [&probeInterfaces](const auto& ifaceName) {
558 return probeInterfaces.contains(ifaceName);
559 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600560}
561
Alexander Hansenad28e402025-06-25 12:38:20 +0200562void EntityManager::handleCurrentConfigurationJson()
563{
Alexander Hansene6651852025-01-21 16:22:05 +0100564 if (EM_CACHE_CONFIGURATION && em_utils::fwVersionIsSame())
Alexander Hansenad28e402025-06-25 12:38:20 +0200565 {
Christopher Meisf7252572025-06-11 13:22:05 +0200566 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200567 {
568 // this file could just be deleted, but it's nice for debug
569 std::filesystem::create_directory(tempConfigDir);
570 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200571 std::filesystem::copy(currentConfiguration, lastConfiguration);
572 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200573
574 std::ifstream jsonStream(lastConfiguration);
575 if (jsonStream.good())
576 {
577 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
578 if (data.is_discarded())
579 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200580 lg2::error("syntax error in {PATH}", "PATH",
581 lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200582 }
583 else
584 {
585 lastJson = std::move(data);
586 }
587 }
588 else
589 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200590 lg2::error("unable to open {PATH}", "PATH", lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200591 }
592 }
593 }
594 else
595 {
596 // not an error, just logging at this level to make it in the journal
Marc Olberdingb2f82822025-09-25 13:56:41 -0700597 std::error_code ec;
Alexander Hansen8feb0452025-09-15 14:29:20 +0200598 lg2::error("Clearing previous configuration");
Marc Olberdingb2f82822025-09-25 13:56:41 -0700599 std::filesystem::remove(currentConfiguration, ec);
Alexander Hansenad28e402025-06-25 12:38:20 +0200600 }
601}
602
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200603void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800604{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200605 if (dbusMatches.contains(path))
606 {
607 return;
608 }
Nan Zhoua3315672022-09-20 19:48:14 +0000609
Alexander Hansen60803182025-06-27 14:41:28 +0200610 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
611
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200612 std::function<void(sdbusplus::message_t & message)> eventHandler =
613 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700614
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200615 sdbusplus::bus::match_t match(
616 static_cast<sdbusplus::bus_t&>(*systemBus),
617 "type='signal',member='PropertiesChanged',path='" + path + "'",
618 eventHandler);
619 dbusMatches.emplace(path, std::move(match));
620}
James Feistfd1264a2018-05-03 12:10:00 -0700621
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200622// We need a poke from DBus for static providers that create all their
623// objects prior to claiming a well-known name, and thus don't emit any
624// org.freedesktop.DBus.Properties signals. Similarly if a process exits
625// for any reason, expected or otherwise, we'll need a poke to remove
626// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200627void EntityManager::initFilters(
628 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200629{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200630 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500631 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500632 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200633 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400634 auto [name, oldOwner,
635 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500636
Patrick Williamsb7077432024-08-16 15:22:21 -0400637 if (name.starts_with(':'))
638 {
639 // We should do nothing with unique-name connections.
640 return;
641 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500642
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200643 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400644 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200645
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500646 // We also need a poke from DBus when new interfaces are created or
647 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200648 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500649 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500650 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200651 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400652 if (iaContainsProbeInterface(msg, probeInterfaces))
653 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200654 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400655 }
656 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200657
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200658 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500659 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500660 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200661 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400662 if (irContainsProbeInterface(msg, probeInterfaces))
663 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200664 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400665 }
666 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200667}