blob: 7734abcbef1f97d9922a178be9907ec676f71a68 [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"
Alexander Hansen60803182025-06-27 14:41:28 +020013#include "phosphor-logging/lg2.hpp"
Benjamin Fairca2eb042022-09-13 06:40:42 +000014#include "topology.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +020015#include "utils.hpp"
James Feist481c5d52019-08-13 14:40:40 -070016
James Feist11be6672018-04-06 14:05:32 -070017#include <boost/algorithm/string/case_conv.hpp>
James Feistf5125b02019-06-06 11:27:43 -070018#include <boost/algorithm/string/classification.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/replace.hpp>
James Feistf5125b02019-06-06 11:27:43 -070021#include <boost/algorithm/string/split.hpp>
James Feist02d2b932020-02-06 16:28:48 -080022#include <boost/asio/io_context.hpp>
Ed Tanous49a888c2023-03-06 13:44:51 -080023#include <boost/asio/post.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070024#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080025#include <boost/container/flat_map.hpp>
26#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070027#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070028#include <nlohmann/json.hpp>
29#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31
James Feist637b3ef2019-04-15 16:35:30 -070032#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080033#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103034#include <functional>
James Feista465ccc2019-02-08 12:51:01 -080035#include <iostream>
Andrew Jeffery666583b2021-12-01 15:50:12 +103036#include <map>
James Feista465ccc2019-02-08 12:51:01 -080037#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070038constexpr const char* tempConfigDir = "/tmp/configuration/";
39constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
James Feistf1b14142019-04-10 15:22:09 -070040
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020041static constexpr std::array<const char*, 6> settableInterfaces = {
42 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist3cb5fec2018-01-23 14:41:51 -080043
Ed Tanous07d467b2021-02-23 14:48:37 -080044const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
45const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080046
James Feista465ccc2019-02-08 12:51:01 -080047sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -070048{
49 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
50 interface) != settableInterfaces.end()
51 ? sdbusplus::asio::PropertyPermission::readWrite
52 : sdbusplus::asio::PropertyPermission::readOnly;
53}
54
Christopher Meiscf6a75b2025-06-03 07:53:50 +020055EntityManager::EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020056 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
57 boost::asio::io_context& io) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020058 systemBus(systemBus),
59 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
60 lastJson(nlohmann::json::object()),
Alexander Hansenb1340da2025-06-27 14:29:13 +020061 systemConfiguration(nlohmann::json::object()), io(io),
Alexander Hansene1646272025-07-25 11:47:17 +020062 powerStatus(*systemBus), 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] :
91 topology.getAssocs(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(
Matt Spinler6eb60972023-08-14 16:36:20 -0500100 objServer, assocPath, "xyz.openbmc_project.Association.Definitions",
101 findBoard->second);
102
103 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200104 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000105 }
James Feist1b2e2242018-01-30 13:45:19 -0800106}
107
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200108void EntityManager::postBoardToDBus(
109 const std::string& boardId, const nlohmann::json& boardConfig,
110 std::map<std::string, std::string>& newBoards)
111{
112 std::string boardName = boardConfig["Name"];
113 std::string boardNameOrig = boardConfig["Name"];
114 std::string jsonPointerPath = "/" + boardId;
115 // loop through newConfiguration, but use values from system
116 // configuration to be able to modify via dbus later
117 auto boardValues = systemConfiguration[boardId];
118 auto findBoardType = boardValues.find("Type");
119 std::string boardType;
120 if (findBoardType != boardValues.end() &&
121 findBoardType->type() == nlohmann::json::value_t::string)
122 {
123 boardType = findBoardType->get<std::string>();
124 std::regex_replace(boardType.begin(), boardType.begin(),
125 boardType.end(), illegalDbusMemberRegex, "_");
126 }
127 else
128 {
129 std::cerr << "Unable to find type for " << boardName
130 << " reverting to Chassis.\n";
131 boardType = "Chassis";
132 }
133 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
134
135 std::regex_replace(boardName.begin(), boardName.begin(), boardName.end(),
136 illegalDbusMemberRegex, "_");
137 std::string boardPath = "/xyz/openbmc_project/inventory/system/";
138 boardPath += boardtypeLower;
139 boardPath += "/";
140 boardPath += boardName;
141
142 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
143 dbus_interface.createInterface(objServer, boardPath,
144 "xyz.openbmc_project.Inventory.Item",
145 boardName);
146
147 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
148 dbus_interface.createInterface(
149 objServer, boardPath,
150 "xyz.openbmc_project.Inventory.Item." + boardType, boardNameOrig);
151
152 dbus_interface.createAddObjectMethod(
153 io, jsonPointerPath, boardPath, systemConfiguration, objServer,
154 boardNameOrig);
155
156 dbus_interface::populateInterfaceFromJson(
157 io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
158 objServer);
159 jsonPointerPath += "/";
160 // iterate through board properties
161 for (const auto& [propName, propValue] : boardValues.items())
162 {
163 if (propValue.type() == nlohmann::json::value_t::object)
164 {
165 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
166 dbus_interface.createInterface(objServer, boardPath, propName,
167 boardNameOrig);
168
169 dbus_interface::populateInterfaceFromJson(
170 io, systemConfiguration, jsonPointerPath + propName, iface,
171 propValue, objServer);
172 }
173 }
174
175 auto exposes = boardValues.find("Exposes");
176 if (exposes == boardValues.end())
177 {
178 return;
179 }
180 // iterate through exposes
181 jsonPointerPath += "Exposes/";
182
183 // store the board level pointer so we can modify it on the way down
184 std::string jsonPointerPathBoard = jsonPointerPath;
185 size_t exposesIndex = -1;
186 for (auto& item : *exposes)
187 {
188 exposesIndex++;
189 jsonPointerPath = jsonPointerPathBoard;
190 jsonPointerPath += std::to_string(exposesIndex);
191
192 auto findName = item.find("Name");
193 if (findName == item.end())
194 {
195 std::cerr << "cannot find name in field " << item << "\n";
196 continue;
197 }
198 auto findStatus = item.find("Status");
199 // if status is not found it is assumed to be status = 'okay'
200 if (findStatus != item.end())
201 {
202 if (*findStatus == "disabled")
203 {
204 continue;
205 }
206 }
207 auto findType = item.find("Type");
208 std::string itemType;
209 if (findType != item.end())
210 {
211 itemType = findType->get<std::string>();
212 std::regex_replace(itemType.begin(), itemType.begin(),
213 itemType.end(), illegalDbusPathRegex, "_");
214 }
215 else
216 {
217 itemType = "unknown";
218 }
219 std::string itemName = findName->get<std::string>();
220 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
221 illegalDbusMemberRegex, "_");
222 std::string ifacePath = boardPath;
223 ifacePath += "/";
224 ifacePath += itemName;
225
226 if (itemType == "BMC")
227 {
228 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
229 dbus_interface.createInterface(
230 objServer, ifacePath,
231 "xyz.openbmc_project.Inventory.Item.Bmc", boardNameOrig);
232 dbus_interface::populateInterfaceFromJson(
233 io, systemConfiguration, jsonPointerPath, bmcIface, item,
234 objServer, getPermission(itemType));
235 }
236 else if (itemType == "System")
237 {
238 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
239 dbus_interface.createInterface(
240 objServer, ifacePath,
241 "xyz.openbmc_project.Inventory.Item.System", boardNameOrig);
242 dbus_interface::populateInterfaceFromJson(
243 io, systemConfiguration, jsonPointerPath, systemIface, item,
244 objServer, getPermission(itemType));
245 }
246
247 for (const auto& [name, config] : item.items())
248 {
249 jsonPointerPath = jsonPointerPathBoard;
250 jsonPointerPath.append(std::to_string(exposesIndex))
251 .append("/")
252 .append(name);
253 if (config.type() == nlohmann::json::value_t::object)
254 {
255 std::string ifaceName = "xyz.openbmc_project.Configuration.";
256 ifaceName.append(itemType).append(".").append(name);
257
258 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
259 dbus_interface.createInterface(objServer, ifacePath,
260 ifaceName, boardNameOrig);
261
262 dbus_interface::populateInterfaceFromJson(
263 io, systemConfiguration, jsonPointerPath, objectIface,
264 config, objServer, getPermission(name));
265 }
266 else if (config.type() == nlohmann::json::value_t::array)
267 {
268 size_t index = 0;
269 if (config.empty())
270 {
271 continue;
272 }
273 bool isLegal = true;
274 auto type = config[0].type();
275 if (type != nlohmann::json::value_t::object)
276 {
277 continue;
278 }
279
280 // verify legal json
281 for (const auto& arrayItem : config)
282 {
283 if (arrayItem.type() != type)
284 {
285 isLegal = false;
286 break;
287 }
288 }
289 if (!isLegal)
290 {
291 std::cerr << "dbus format error" << config << "\n";
292 break;
293 }
294
295 for (auto& arrayItem : config)
296 {
297 std::string ifaceName =
298 "xyz.openbmc_project.Configuration.";
299 ifaceName.append(itemType).append(".").append(name);
300 ifaceName.append(std::to_string(index));
301
302 std::shared_ptr<sdbusplus::asio::dbus_interface>
303 objectIface = dbus_interface.createInterface(
304 objServer, ifacePath, ifaceName, boardNameOrig);
305
306 dbus_interface::populateInterfaceFromJson(
307 io, systemConfiguration,
308 jsonPointerPath + "/" + std::to_string(index),
309 objectIface, arrayItem, objServer, getPermission(name));
310 index++;
311 }
312 }
313 }
314
315 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
316 dbus_interface.createInterface(
317 objServer, ifacePath,
318 "xyz.openbmc_project.Configuration." + itemType, boardNameOrig);
319
320 dbus_interface::populateInterfaceFromJson(
321 io, systemConfiguration, jsonPointerPath, itemIface, item,
322 objServer, getPermission(itemType));
323
324 topology.addBoard(boardPath, boardType, boardNameOrig, item);
325 }
326
327 newBoards.emplace(boardPath, boardNameOrig);
328}
329
Andrew Jeffery55192932022-03-24 12:29:27 +1030330static bool deviceRequiresPowerOn(const nlohmann::json& entity)
331{
332 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030333 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030334 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030335 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030336 }
337
Ed Tanous3013fb42022-07-09 08:27:06 -0700338 const auto* ptr = powerState->get_ptr<const std::string*>();
339 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030340 {
341 return false;
342 }
343
344 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030345}
346
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030347static void pruneDevice(const nlohmann::json& systemConfiguration,
348 const bool powerOff, const bool scannedPowerOff,
349 const std::string& name, const nlohmann::json& device)
350{
351 if (systemConfiguration.contains(name))
352 {
353 return;
354 }
355
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030356 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030357 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030358 return;
359 }
360
361 logDeviceRemoved(device);
362}
363
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200364void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
365 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700366{
James Feistfb00f392019-06-25 14:16:48 -0700367 if (systemConfiguration.empty() || lastJson.empty())
368 {
369 return; // not ready yet
370 }
James Feist1df06a42019-04-11 14:23:04 -0700371 if (scannedPowerOn)
372 {
373 return;
374 }
375
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200376 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700377 {
378 return;
379 }
380
James Feistb1728ca2020-04-30 15:40:55 -0700381 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030382 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200383 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400384 if (ec == boost::asio::error::operation_aborted)
385 {
386 return;
387 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030388
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200389 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400390 for (const auto& [name, device] : lastJson.items())
391 {
392 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
393 name, device);
394 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030395
Patrick Williamsb7077432024-08-16 15:22:21 -0400396 scannedPowerOff = true;
397 if (!powerOff)
398 {
399 scannedPowerOn = true;
400 }
401 });
James Feist1df06a42019-04-11 14:23:04 -0700402}
403
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200404void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
405 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030406{
407 if (powerOff && deviceRequiresPowerOn(device))
408 {
409 // power not on yet, don't know if it's there or not
410 return;
411 }
412
Alexander Hansen57604ed2025-06-27 13:22:28 +0200413 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030414 for (auto& iface : ifaces)
415 {
416 auto sharedPtr = iface.lock();
417 if (!!sharedPtr)
418 {
419 objServer.remove_interface(sharedPtr);
420 }
421 }
422
423 ifaces.clear();
424 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500425 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030426 logDeviceRemoved(device);
427}
428
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200429void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030430 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200431 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030432 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
433 //
434 // Discord discussion:
435 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
436 //
437 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200438 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030439{
Alexander Hansena555acf2025-06-27 11:59:10 +0200440 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030441
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200442 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200443 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030444 {
445 std::cerr << "Error writing json files\n";
446 }
447 });
448
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200449 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
450 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030451 if (count == instance)
452 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200453 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030454 }
455 });
456}
457
James Feist8f2710a2018-05-09 17:18:55 -0700458// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200459void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700460{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200461 propertiesChangedInstance++;
462 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700463
Alexander Hansenb1340da2025-06-27 14:29:13 +0200464 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700465
466 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200467 propertiesChangedTimer.async_wait(
468 [this, count](const boost::system::error_code& ec) {
469 if (ec == boost::asio::error::operation_aborted)
470 {
471 // we were cancelled
472 return;
473 }
474 if (ec)
475 {
476 std::cerr << "async wait error " << ec << "\n";
477 return;
478 }
James Feist8f2710a2018-05-09 17:18:55 -0700479
Alexander Hansenb1340da2025-06-27 14:29:13 +0200480 if (propertiesChangedInProgress)
481 {
482 propertiesChangedCallback();
483 return;
484 }
485 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700486
Alexander Hansenb1340da2025-06-27 14:29:13 +0200487 nlohmann::json oldConfiguration = systemConfiguration;
488 auto missingConfigurations = std::make_shared<nlohmann::json>();
489 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700490
Alexander Hansenb1340da2025-06-27 14:29:13 +0200491 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200492 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200493 [this, count, oldConfiguration, missingConfigurations]() {
494 // this is something that since ac has been applied to the
495 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200496 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200497 for (const auto& [name, device] :
498 missingConfigurations->items())
499 {
500 pruneConfiguration(powerOff, name, device);
501 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200502 nlohmann::json newConfiguration = systemConfiguration;
503
Christopher Meisf7252572025-06-11 13:22:05 +0200504 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200505
506 for (const auto& [_, device] : newConfiguration.items())
507 {
508 logDeviceAdded(device);
509 }
510
511 propertiesChangedInProgress = false;
512
513 boost::asio::post(io, [this, newConfiguration, count] {
514 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200515 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200516 std::ref(propertiesChangedTimer), newConfiguration);
517 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200518 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200519 perfScan->run();
520 });
James Feist75fdeeb2018-02-20 14:26:16 -0800521}
522
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600523// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400524static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200525 sdbusplus::message_t& msg,
526 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600527{
528 sdbusplus::message::object_path path;
529 DBusObject interfaces;
530 std::set<std::string> interfaceSet;
531 std::set<std::string> intersect;
532
533 msg.read(path, interfaces);
534
535 std::for_each(interfaces.begin(), interfaces.end(),
536 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400537 interfaceSet.insert(iface.first);
538 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600539
540 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
541 probeInterfaces.begin(), probeInterfaces.end(),
542 std::inserter(intersect, intersect.end()));
543 return !intersect.empty();
544}
545
546// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400547static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200548 sdbusplus::message_t& msg,
549 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600550{
551 sdbusplus::message::object_path path;
552 std::set<std::string> interfaces;
553 std::set<std::string> intersect;
554
555 msg.read(path, interfaces);
556
557 std::set_intersection(interfaces.begin(), interfaces.end(),
558 probeInterfaces.begin(), probeInterfaces.end(),
559 std::inserter(intersect, intersect.end()));
560 return !intersect.empty();
561}
562
Alexander Hansenad28e402025-06-25 12:38:20 +0200563void EntityManager::handleCurrentConfigurationJson()
564{
565 if (em_utils::fwVersionIsSame())
566 {
Christopher Meisf7252572025-06-11 13:22:05 +0200567 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200568 {
569 // this file could just be deleted, but it's nice for debug
570 std::filesystem::create_directory(tempConfigDir);
571 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200572 std::filesystem::copy(currentConfiguration, lastConfiguration);
573 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200574
575 std::ifstream jsonStream(lastConfiguration);
576 if (jsonStream.good())
577 {
578 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
579 if (data.is_discarded())
580 {
581 std::cerr
582 << "syntax error in " << lastConfiguration << "\n";
583 }
584 else
585 {
586 lastJson = std::move(data);
587 }
588 }
589 else
590 {
591 std::cerr << "unable to open " << lastConfiguration << "\n";
592 }
593 }
594 }
595 else
596 {
597 // not an error, just logging at this level to make it in the journal
598 std::cerr << "Clearing previous configuration\n";
Christopher Meisf7252572025-06-11 13:22:05 +0200599 std::filesystem::remove(currentConfiguration);
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}