blob: 852db33a93fd0fbcb8e4da872d4bca0bd101cf8a [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 Feist11be6672018-04-06 14:05:32 -070016#include <boost/algorithm/string/case_conv.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080017#include <boost/algorithm/string/replace.hpp>
James Feist02d2b932020-02-06 16:28:48 -080018#include <boost/asio/io_context.hpp>
Ed Tanous49a888c2023-03-06 13:44:51 -080019#include <boost/asio/post.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070020#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080021#include <boost/container/flat_map.hpp>
22#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070023#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070024#include <nlohmann/json.hpp>
Alexander Hansen8feb0452025-09-15 14:29:20 +020025#include <phosphor-logging/lg2.hpp>
James Feist8c505da2020-05-28 10:06:33 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28
James Feist637b3ef2019-04-15 16:35:30 -070029#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080030#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103031#include <functional>
Andrew Jeffery666583b2021-12-01 15:50:12 +103032#include <map>
James Feista465ccc2019-02-08 12:51:01 -080033#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070034constexpr const char* tempConfigDir = "/tmp/configuration/";
35constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
James Feistf1b14142019-04-10 15:22:09 -070036
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020037static constexpr std::array<const char*, 6> settableInterfaces = {
38 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist3cb5fec2018-01-23 14:41:51 -080039
Ed Tanous07d467b2021-02-23 14:48:37 -080040const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
41const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080042
James Feista465ccc2019-02-08 12:51:01 -080043sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -070044{
45 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
46 interface) != settableInterfaces.end()
47 ? sdbusplus::asio::PropertyPermission::readWrite
48 : sdbusplus::asio::PropertyPermission::readOnly;
49}
50
Christopher Meiscf6a75b2025-06-03 07:53:50 +020051EntityManager::EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020052 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
53 boost::asio::io_context& io) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020054 systemBus(systemBus),
55 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
56 lastJson(nlohmann::json::object()),
Alexander Hansenb1340da2025-06-27 14:29:13 +020057 systemConfiguration(nlohmann::json::object()), io(io),
Alexander Hansen89737252025-08-04 15:15:13 +020058 dbus_interface(io, objServer), powerStatus(*systemBus),
59 propertiesChangedTimer(io)
Christopher Meiscf6a75b2025-06-03 07:53:50 +020060{
61 // All other objects that EntityManager currently support are under the
62 // inventory subtree.
63 // See the discussion at
64 // https://discord.com/channels/775381525260664832/1018929092009144380
65 objServer.add_manager("/xyz/openbmc_project/inventory");
James Feist75fdeeb2018-02-20 14:26:16 -080066
Christopher Meiscf6a75b2025-06-03 07:53:50 +020067 entityIface = objServer.add_interface("/xyz/openbmc_project/EntityManager",
68 "xyz.openbmc_project.EntityManager");
69 entityIface->register_method("ReScan", [this]() {
70 propertiesChangedCallback();
71 });
72 dbus_interface::tryIfaceInitialize(entityIface);
Christopher Meisf7252572025-06-11 13:22:05 +020073
74 initFilters(configuration.probeInterfaces);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020075}
76
77void EntityManager::postToDbus(const nlohmann::json& newConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -080078{
Matt Spinler6eb60972023-08-14 16:36:20 -050079 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +000080
James Feist97a63f12018-05-17 13:50:57 -070081 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -050082 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -080083 {
Alexander Hansen4fcd9462025-07-30 17:44:44 +020084 postBoardToDBus(boardId, boardConfig, newBoards);
James Feist1b2e2242018-01-30 13:45:19 -080085 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000086
Matt Spinler6eb60972023-08-14 16:36:20 -050087 for (const auto& [assocPath, assocPropValue] :
Alexander Hansen32e74182025-08-20 16:16:00 +020088 topology.getAssocs(std::views::keys(newBoards)))
Benjamin Fairca2eb042022-09-13 06:40:42 +000089 {
Matt Spinler6eb60972023-08-14 16:36:20 -050090 auto findBoard = newBoards.find(assocPath);
91 if (findBoard == newBoards.end())
92 {
93 continue;
94 }
Benjamin Fairca2eb042022-09-13 06:40:42 +000095
Alexander Hansen57604ed2025-06-27 13:22:28 +020096 auto ifacePtr = dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +020097 assocPath, "xyz.openbmc_project.Association.Definitions",
Matt Spinler6eb60972023-08-14 16:36:20 -050098 findBoard->second);
99
100 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200101 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000102 }
James Feist1b2e2242018-01-30 13:45:19 -0800103}
104
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200105void EntityManager::postBoardToDBus(
106 const std::string& boardId, const nlohmann::json& boardConfig,
107 std::map<std::string, std::string>& newBoards)
108{
109 std::string boardName = boardConfig["Name"];
110 std::string boardNameOrig = boardConfig["Name"];
111 std::string jsonPointerPath = "/" + boardId;
112 // loop through newConfiguration, but use values from system
113 // configuration to be able to modify via dbus later
114 auto boardValues = systemConfiguration[boardId];
115 auto findBoardType = boardValues.find("Type");
116 std::string boardType;
117 if (findBoardType != boardValues.end() &&
118 findBoardType->type() == nlohmann::json::value_t::string)
119 {
120 boardType = findBoardType->get<std::string>();
121 std::regex_replace(boardType.begin(), boardType.begin(),
122 boardType.end(), illegalDbusMemberRegex, "_");
123 }
124 else
125 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200126 lg2::error("Unable to find type for {BOARD} reverting to Chassis.",
127 "BOARD", boardName);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200128 boardType = "Chassis";
129 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200130
Christopher Meis811160e2025-08-08 08:48:37 +0200131 const std::string boardPath =
132 em_utils::buildInventorySystemPath(boardName, boardType);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200133
134 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200135 dbus_interface.createInterface(
136 boardPath, "xyz.openbmc_project.Inventory.Item", boardName);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200137
138 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
139 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200140 boardPath, "xyz.openbmc_project.Inventory.Item." + boardType,
141 boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200142
Alexander Hansen89737252025-08-04 15:15:13 +0200143 dbus_interface.createAddObjectMethod(jsonPointerPath, boardPath,
144 systemConfiguration, boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200145
Alexander Hansen89737252025-08-04 15:15:13 +0200146 dbus_interface.populateInterfaceFromJson(
147 systemConfiguration, jsonPointerPath, boardIface, boardValues);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200148 jsonPointerPath += "/";
149 // iterate through board properties
150 for (const auto& [propName, propValue] : boardValues.items())
151 {
152 if (propValue.type() == nlohmann::json::value_t::object)
153 {
154 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen89737252025-08-04 15:15:13 +0200155 dbus_interface.createInterface(boardPath, propName,
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200156 boardNameOrig);
157
Alexander Hansen89737252025-08-04 15:15:13 +0200158 dbus_interface.populateInterfaceFromJson(
159 systemConfiguration, jsonPointerPath + propName, iface,
160 propValue);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200161 }
162 }
163
Alexander Hansen4fa40122025-07-30 18:26:35 +0200164 nlohmann::json::iterator exposes = boardValues.find("Exposes");
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200165 if (exposes == boardValues.end())
166 {
167 return;
168 }
169 // iterate through exposes
170 jsonPointerPath += "Exposes/";
171
172 // store the board level pointer so we can modify it on the way down
173 std::string jsonPointerPathBoard = jsonPointerPath;
174 size_t exposesIndex = -1;
Alexander Hansen4fa40122025-07-30 18:26:35 +0200175 for (nlohmann::json& item : *exposes)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200176 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200177 postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
178 jsonPointerPath, jsonPointerPathBoard,
179 boardPath, boardType);
180 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200181
Alexander Hansen4fa40122025-07-30 18:26:35 +0200182 newBoards.emplace(boardPath, boardNameOrig);
183}
184
185void EntityManager::postExposesRecordsToDBus(
186 nlohmann::json& item, size_t& exposesIndex,
187 const std::string& boardNameOrig, std::string jsonPointerPath,
188 const std::string& jsonPointerPathBoard, const std::string& boardPath,
189 const std::string& boardType)
190{
191 exposesIndex++;
192 jsonPointerPath = jsonPointerPathBoard;
193 jsonPointerPath += std::to_string(exposesIndex);
194
195 auto findName = item.find("Name");
196 if (findName == item.end())
197 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200198 lg2::error("cannot find name in field {ITEM}", "ITEM", item);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200199 return;
200 }
201 auto findStatus = item.find("Status");
202 // if status is not found it is assumed to be status = 'okay'
203 if (findStatus != item.end())
204 {
205 if (*findStatus == "disabled")
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200206 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200207 return;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200208 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200209 }
210 auto findType = item.find("Type");
211 std::string itemType;
212 if (findType != item.end())
213 {
214 itemType = findType->get<std::string>();
215 std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
216 illegalDbusPathRegex, "_");
217 }
218 else
219 {
220 itemType = "unknown";
221 }
222 std::string itemName = findName->get<std::string>();
223 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
224 illegalDbusMemberRegex, "_");
225 std::string ifacePath = boardPath;
226 ifacePath += "/";
227 ifacePath += itemName;
228
229 if (itemType == "BMC")
230 {
231 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
232 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200233 ifacePath, "xyz.openbmc_project.Inventory.Item.Bmc",
Alexander Hansen4fa40122025-07-30 18:26:35 +0200234 boardNameOrig);
Alexander Hansen89737252025-08-04 15:15:13 +0200235 dbus_interface.populateInterfaceFromJson(
236 systemConfiguration, jsonPointerPath, bmcIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200237 getPermission(itemType));
238 }
239 else if (itemType == "System")
240 {
241 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
242 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200243 ifacePath, "xyz.openbmc_project.Inventory.Item.System",
244 boardNameOrig);
245 dbus_interface.populateInterfaceFromJson(
246 systemConfiguration, jsonPointerPath, systemIface, item,
247 getPermission(itemType));
Alexander Hansen4fa40122025-07-30 18:26:35 +0200248 }
249
250 for (const auto& [name, config] : item.items())
251 {
252 jsonPointerPath = jsonPointerPathBoard;
253 jsonPointerPath.append(std::to_string(exposesIndex))
254 .append("/")
255 .append(name);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200256
257 if (!postConfigurationRecord(name, config, boardNameOrig, itemType,
258 jsonPointerPath, ifacePath))
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200259 {
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200260 break;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200261 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200262 }
263
Alexander Hansen4fa40122025-07-30 18:26:35 +0200264 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
265 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200266 ifacePath, "xyz.openbmc_project.Configuration." + itemType,
267 boardNameOrig);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200268
Alexander Hansen89737252025-08-04 15:15:13 +0200269 dbus_interface.populateInterfaceFromJson(
270 systemConfiguration, jsonPointerPath, itemIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200271 getPermission(itemType));
272
273 topology.addBoard(boardPath, boardType, boardNameOrig, item);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200274}
275
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200276bool EntityManager::postConfigurationRecord(
277 const std::string& name, nlohmann::json& config,
278 const std::string& boardNameOrig, const std::string& itemType,
279 const std::string& jsonPointerPath, const std::string& ifacePath)
280{
281 if (config.type() == nlohmann::json::value_t::object)
282 {
283 std::string ifaceName = "xyz.openbmc_project.Configuration.";
284 ifaceName.append(itemType).append(".").append(name);
285
286 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200287 dbus_interface.createInterface(ifacePath, ifaceName, boardNameOrig);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200288
Alexander Hansen89737252025-08-04 15:15:13 +0200289 dbus_interface.populateInterfaceFromJson(
290 systemConfiguration, jsonPointerPath, objectIface, config,
291 getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200292 }
293 else if (config.type() == nlohmann::json::value_t::array)
294 {
295 size_t index = 0;
296 if (config.empty())
297 {
298 return true;
299 }
300 bool isLegal = true;
301 auto type = config[0].type();
302 if (type != nlohmann::json::value_t::object)
303 {
304 return true;
305 }
306
307 // verify legal json
308 for (const auto& arrayItem : config)
309 {
310 if (arrayItem.type() != type)
311 {
312 isLegal = false;
313 break;
314 }
315 }
316 if (!isLegal)
317 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200318 lg2::error("dbus format error {JSON}", "JSON", config);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200319 return false;
320 }
321
322 for (auto& arrayItem : config)
323 {
324 std::string ifaceName = "xyz.openbmc_project.Configuration.";
325 ifaceName.append(itemType).append(".").append(name);
326 ifaceName.append(std::to_string(index));
327
328 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200329 dbus_interface.createInterface(ifacePath, ifaceName,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200330 boardNameOrig);
331
Alexander Hansen89737252025-08-04 15:15:13 +0200332 dbus_interface.populateInterfaceFromJson(
333 systemConfiguration,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200334 jsonPointerPath + "/" + std::to_string(index), objectIface,
Alexander Hansen89737252025-08-04 15:15:13 +0200335 arrayItem, getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200336 index++;
337 }
338 }
339
340 return true;
341}
342
Andrew Jeffery55192932022-03-24 12:29:27 +1030343static bool deviceRequiresPowerOn(const nlohmann::json& entity)
344{
345 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030346 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030347 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030348 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030349 }
350
Ed Tanous3013fb42022-07-09 08:27:06 -0700351 const auto* ptr = powerState->get_ptr<const std::string*>();
352 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030353 {
354 return false;
355 }
356
357 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030358}
359
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030360static void pruneDevice(const nlohmann::json& systemConfiguration,
361 const bool powerOff, const bool scannedPowerOff,
362 const std::string& name, const nlohmann::json& device)
363{
364 if (systemConfiguration.contains(name))
365 {
366 return;
367 }
368
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030369 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030370 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030371 return;
372 }
373
374 logDeviceRemoved(device);
375}
376
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200377void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
378 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700379{
James Feistfb00f392019-06-25 14:16:48 -0700380 if (systemConfiguration.empty() || lastJson.empty())
381 {
382 return; // not ready yet
383 }
James Feist1df06a42019-04-11 14:23:04 -0700384 if (scannedPowerOn)
385 {
386 return;
387 }
388
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200389 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700390 {
391 return;
392 }
393
James Feistb1728ca2020-04-30 15:40:55 -0700394 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030395 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200396 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400397 if (ec == boost::asio::error::operation_aborted)
398 {
399 return;
400 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030401
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200402 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400403 for (const auto& [name, device] : lastJson.items())
404 {
405 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
406 name, device);
407 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030408
Patrick Williamsb7077432024-08-16 15:22:21 -0400409 scannedPowerOff = true;
410 if (!powerOff)
411 {
412 scannedPowerOn = true;
413 }
414 });
James Feist1df06a42019-04-11 14:23:04 -0700415}
416
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200417void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
418 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030419{
420 if (powerOff && deviceRequiresPowerOn(device))
421 {
422 // power not on yet, don't know if it's there or not
423 return;
424 }
425
Alexander Hansen57604ed2025-06-27 13:22:28 +0200426 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030427 for (auto& iface : ifaces)
428 {
429 auto sharedPtr = iface.lock();
430 if (!!sharedPtr)
431 {
432 objServer.remove_interface(sharedPtr);
433 }
434 }
435
436 ifaces.clear();
437 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500438 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030439 logDeviceRemoved(device);
440}
441
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200442void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030443 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200444 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030445 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
446 //
447 // Discord discussion:
448 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
449 //
450 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200451 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030452{
Alexander Hansena555acf2025-06-27 11:59:10 +0200453 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030454
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200455 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200456 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030457 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200458 lg2::error("Error writing json files");
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030459 }
460 });
461
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200462 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
463 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030464 if (count == instance)
465 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200466 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030467 }
468 });
469}
470
James Feist8f2710a2018-05-09 17:18:55 -0700471// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200472void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700473{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200474 propertiesChangedInstance++;
475 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700476
Alexander Hansenb1340da2025-06-27 14:29:13 +0200477 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700478
479 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200480 propertiesChangedTimer.async_wait(
481 [this, count](const boost::system::error_code& ec) {
482 if (ec == boost::asio::error::operation_aborted)
483 {
484 // we were cancelled
485 return;
486 }
487 if (ec)
488 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200489 lg2::error("async wait error {ERR}", "ERR", ec.message());
Alexander Hansenb1340da2025-06-27 14:29:13 +0200490 return;
491 }
James Feist8f2710a2018-05-09 17:18:55 -0700492
Alexander Hansenb1340da2025-06-27 14:29:13 +0200493 if (propertiesChangedInProgress)
494 {
495 propertiesChangedCallback();
496 return;
497 }
498 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700499
Alexander Hansenb1340da2025-06-27 14:29:13 +0200500 nlohmann::json oldConfiguration = systemConfiguration;
501 auto missingConfigurations = std::make_shared<nlohmann::json>();
502 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700503
Alexander Hansenb1340da2025-06-27 14:29:13 +0200504 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200505 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200506 [this, count, oldConfiguration, missingConfigurations]() {
507 // this is something that since ac has been applied to the
508 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200509 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200510 for (const auto& [name, device] :
511 missingConfigurations->items())
512 {
513 pruneConfiguration(powerOff, name, device);
514 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200515 nlohmann::json newConfiguration = systemConfiguration;
516
Christopher Meisf7252572025-06-11 13:22:05 +0200517 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200518
519 for (const auto& [_, device] : newConfiguration.items())
520 {
521 logDeviceAdded(device);
522 }
523
524 propertiesChangedInProgress = false;
525
526 boost::asio::post(io, [this, newConfiguration, count] {
527 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200528 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200529 std::ref(propertiesChangedTimer), newConfiguration);
530 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200531 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200532 perfScan->run();
533 });
James Feist75fdeeb2018-02-20 14:26:16 -0800534}
535
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600536// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400537static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200538 sdbusplus::message_t& msg,
539 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600540{
541 sdbusplus::message::object_path path;
542 DBusObject interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600543 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800544 return std::ranges::any_of(interfaces | std::views::keys,
545 [&probeInterfaces](const auto& ifaceName) {
546 return probeInterfaces.contains(ifaceName);
547 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600548}
549
550// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400551static bool irContainsProbeInterface(
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;
Eldin Lee802cbf22025-08-22 14:44:04 +0800556 std::vector<std::string> 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,
559 [&probeInterfaces](const auto& ifaceName) {
560 return probeInterfaces.contains(ifaceName);
561 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600562}
563
Alexander Hansenad28e402025-06-25 12:38:20 +0200564void EntityManager::handleCurrentConfigurationJson()
565{
Alexander Hansene6651852025-01-21 16:22:05 +0100566 if (EM_CACHE_CONFIGURATION && em_utils::fwVersionIsSame())
Alexander Hansenad28e402025-06-25 12:38:20 +0200567 {
Christopher Meisf7252572025-06-11 13:22:05 +0200568 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200569 {
570 // this file could just be deleted, but it's nice for debug
571 std::filesystem::create_directory(tempConfigDir);
572 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200573 std::filesystem::copy(currentConfiguration, lastConfiguration);
574 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200575
576 std::ifstream jsonStream(lastConfiguration);
577 if (jsonStream.good())
578 {
579 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
580 if (data.is_discarded())
581 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200582 lg2::error("syntax error in {PATH}", "PATH",
583 lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200584 }
585 else
586 {
587 lastJson = std::move(data);
588 }
589 }
590 else
591 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200592 lg2::error("unable to open {PATH}", "PATH", lastConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200593 }
594 }
595 }
596 else
597 {
598 // not an error, just logging at this level to make it in the journal
Marc Olberdingb2f82822025-09-25 13:56:41 -0700599 std::error_code ec;
Alexander Hansen8feb0452025-09-15 14:29:20 +0200600 lg2::error("Clearing previous configuration");
Marc Olberdingb2f82822025-09-25 13:56:41 -0700601 std::filesystem::remove(currentConfiguration, ec);
Alexander Hansenad28e402025-06-25 12:38:20 +0200602 }
603}
604
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200605void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800606{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200607 if (dbusMatches.contains(path))
608 {
609 return;
610 }
Nan Zhoua3315672022-09-20 19:48:14 +0000611
Alexander Hansen60803182025-06-27 14:41:28 +0200612 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
613
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200614 std::function<void(sdbusplus::message_t & message)> eventHandler =
615 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700616
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200617 sdbusplus::bus::match_t match(
618 static_cast<sdbusplus::bus_t&>(*systemBus),
619 "type='signal',member='PropertiesChanged',path='" + path + "'",
620 eventHandler);
621 dbusMatches.emplace(path, std::move(match));
622}
James Feistfd1264a2018-05-03 12:10:00 -0700623
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200624// We need a poke from DBus for static providers that create all their
625// objects prior to claiming a well-known name, and thus don't emit any
626// org.freedesktop.DBus.Properties signals. Similarly if a process exits
627// for any reason, expected or otherwise, we'll need a poke to remove
628// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200629void EntityManager::initFilters(
630 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200631{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200632 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500633 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500634 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200635 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400636 auto [name, oldOwner,
637 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500638
Patrick Williamsb7077432024-08-16 15:22:21 -0400639 if (name.starts_with(':'))
640 {
641 // We should do nothing with unique-name connections.
642 return;
643 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500644
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200645 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400646 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200647
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500648 // We also need a poke from DBus when new interfaces are created or
649 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200650 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500651 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500652 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200653 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400654 if (iaContainsProbeInterface(msg, probeInterfaces))
655 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200656 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400657 }
658 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200659
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200660 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500661 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500662 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200663 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400664 if (irContainsProbeInterface(msg, probeInterfaces))
665 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200666 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400667 }
668 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200669}