blob: d66695f0cda1492ed08b4b9151f020caeedb3eef [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 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200133
Christopher Meis811160e2025-08-08 08:48:37 +0200134 const std::string boardPath =
135 em_utils::buildInventorySystemPath(boardName, boardType);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200136
137 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
138 dbus_interface.createInterface(objServer, boardPath,
139 "xyz.openbmc_project.Inventory.Item",
140 boardName);
141
142 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
143 dbus_interface.createInterface(
144 objServer, boardPath,
145 "xyz.openbmc_project.Inventory.Item." + boardType, boardNameOrig);
146
147 dbus_interface.createAddObjectMethod(
148 io, jsonPointerPath, boardPath, systemConfiguration, objServer,
149 boardNameOrig);
150
151 dbus_interface::populateInterfaceFromJson(
152 io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
153 objServer);
154 jsonPointerPath += "/";
155 // iterate through board properties
156 for (const auto& [propName, propValue] : boardValues.items())
157 {
158 if (propValue.type() == nlohmann::json::value_t::object)
159 {
160 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
161 dbus_interface.createInterface(objServer, boardPath, propName,
162 boardNameOrig);
163
164 dbus_interface::populateInterfaceFromJson(
165 io, systemConfiguration, jsonPointerPath + propName, iface,
166 propValue, objServer);
167 }
168 }
169
Alexander Hansen4fa40122025-07-30 18:26:35 +0200170 nlohmann::json::iterator exposes = boardValues.find("Exposes");
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200171 if (exposes == boardValues.end())
172 {
173 return;
174 }
175 // iterate through exposes
176 jsonPointerPath += "Exposes/";
177
178 // store the board level pointer so we can modify it on the way down
179 std::string jsonPointerPathBoard = jsonPointerPath;
180 size_t exposesIndex = -1;
Alexander Hansen4fa40122025-07-30 18:26:35 +0200181 for (nlohmann::json& item : *exposes)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200182 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200183 postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
184 jsonPointerPath, jsonPointerPathBoard,
185 boardPath, boardType);
186 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200187
Alexander Hansen4fa40122025-07-30 18:26:35 +0200188 newBoards.emplace(boardPath, boardNameOrig);
189}
190
191void EntityManager::postExposesRecordsToDBus(
192 nlohmann::json& item, size_t& exposesIndex,
193 const std::string& boardNameOrig, std::string jsonPointerPath,
194 const std::string& jsonPointerPathBoard, const std::string& boardPath,
195 const std::string& boardType)
196{
197 exposesIndex++;
198 jsonPointerPath = jsonPointerPathBoard;
199 jsonPointerPath += std::to_string(exposesIndex);
200
201 auto findName = item.find("Name");
202 if (findName == item.end())
203 {
204 std::cerr << "cannot find name in field " << item << "\n";
205 return;
206 }
207 auto findStatus = item.find("Status");
208 // if status is not found it is assumed to be status = 'okay'
209 if (findStatus != item.end())
210 {
211 if (*findStatus == "disabled")
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200212 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200213 return;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200214 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200215 }
216 auto findType = item.find("Type");
217 std::string itemType;
218 if (findType != item.end())
219 {
220 itemType = findType->get<std::string>();
221 std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
222 illegalDbusPathRegex, "_");
223 }
224 else
225 {
226 itemType = "unknown";
227 }
228 std::string itemName = findName->get<std::string>();
229 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
230 illegalDbusMemberRegex, "_");
231 std::string ifacePath = boardPath;
232 ifacePath += "/";
233 ifacePath += itemName;
234
235 if (itemType == "BMC")
236 {
237 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
238 dbus_interface.createInterface(
239 objServer, ifacePath, "xyz.openbmc_project.Inventory.Item.Bmc",
240 boardNameOrig);
241 dbus_interface::populateInterfaceFromJson(
242 io, systemConfiguration, jsonPointerPath, bmcIface, item, objServer,
243 getPermission(itemType));
244 }
245 else if (itemType == "System")
246 {
247 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
248 dbus_interface.createInterface(
249 objServer, ifacePath,
250 "xyz.openbmc_project.Inventory.Item.System", boardNameOrig);
251 dbus_interface::populateInterfaceFromJson(
252 io, systemConfiguration, jsonPointerPath, systemIface, item,
253 objServer, getPermission(itemType));
254 }
255
256 for (const auto& [name, config] : item.items())
257 {
258 jsonPointerPath = jsonPointerPathBoard;
259 jsonPointerPath.append(std::to_string(exposesIndex))
260 .append("/")
261 .append(name);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200262
263 if (!postConfigurationRecord(name, config, boardNameOrig, itemType,
264 jsonPointerPath, ifacePath))
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200265 {
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200266 break;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200267 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200268 }
269
Alexander Hansen4fa40122025-07-30 18:26:35 +0200270 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
271 dbus_interface.createInterface(
272 objServer, ifacePath,
273 "xyz.openbmc_project.Configuration." + itemType, boardNameOrig);
274
275 dbus_interface::populateInterfaceFromJson(
276 io, systemConfiguration, jsonPointerPath, itemIface, item, objServer,
277 getPermission(itemType));
278
279 topology.addBoard(boardPath, boardType, boardNameOrig, item);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200280}
281
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200282bool EntityManager::postConfigurationRecord(
283 const std::string& name, nlohmann::json& config,
284 const std::string& boardNameOrig, const std::string& itemType,
285 const std::string& jsonPointerPath, const std::string& ifacePath)
286{
287 if (config.type() == nlohmann::json::value_t::object)
288 {
289 std::string ifaceName = "xyz.openbmc_project.Configuration.";
290 ifaceName.append(itemType).append(".").append(name);
291
292 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
293 dbus_interface.createInterface(objServer, ifacePath, ifaceName,
294 boardNameOrig);
295
296 dbus_interface::populateInterfaceFromJson(
297 io, systemConfiguration, jsonPointerPath, objectIface, config,
298 objServer, getPermission(name));
299 }
300 else if (config.type() == nlohmann::json::value_t::array)
301 {
302 size_t index = 0;
303 if (config.empty())
304 {
305 return true;
306 }
307 bool isLegal = true;
308 auto type = config[0].type();
309 if (type != nlohmann::json::value_t::object)
310 {
311 return true;
312 }
313
314 // verify legal json
315 for (const auto& arrayItem : config)
316 {
317 if (arrayItem.type() != type)
318 {
319 isLegal = false;
320 break;
321 }
322 }
323 if (!isLegal)
324 {
325 std::cerr << "dbus format error" << config << "\n";
326 return false;
327 }
328
329 for (auto& arrayItem : config)
330 {
331 std::string ifaceName = "xyz.openbmc_project.Configuration.";
332 ifaceName.append(itemType).append(".").append(name);
333 ifaceName.append(std::to_string(index));
334
335 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
336 dbus_interface.createInterface(objServer, ifacePath, ifaceName,
337 boardNameOrig);
338
339 dbus_interface::populateInterfaceFromJson(
340 io, systemConfiguration,
341 jsonPointerPath + "/" + std::to_string(index), objectIface,
342 arrayItem, objServer, getPermission(name));
343 index++;
344 }
345 }
346
347 return true;
348}
349
Andrew Jeffery55192932022-03-24 12:29:27 +1030350static bool deviceRequiresPowerOn(const nlohmann::json& entity)
351{
352 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030353 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030354 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030355 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030356 }
357
Ed Tanous3013fb42022-07-09 08:27:06 -0700358 const auto* ptr = powerState->get_ptr<const std::string*>();
359 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030360 {
361 return false;
362 }
363
364 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030365}
366
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030367static void pruneDevice(const nlohmann::json& systemConfiguration,
368 const bool powerOff, const bool scannedPowerOff,
369 const std::string& name, const nlohmann::json& device)
370{
371 if (systemConfiguration.contains(name))
372 {
373 return;
374 }
375
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030376 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030377 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030378 return;
379 }
380
381 logDeviceRemoved(device);
382}
383
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200384void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
385 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700386{
James Feistfb00f392019-06-25 14:16:48 -0700387 if (systemConfiguration.empty() || lastJson.empty())
388 {
389 return; // not ready yet
390 }
James Feist1df06a42019-04-11 14:23:04 -0700391 if (scannedPowerOn)
392 {
393 return;
394 }
395
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200396 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700397 {
398 return;
399 }
400
James Feistb1728ca2020-04-30 15:40:55 -0700401 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030402 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200403 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400404 if (ec == boost::asio::error::operation_aborted)
405 {
406 return;
407 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030408
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200409 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400410 for (const auto& [name, device] : lastJson.items())
411 {
412 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
413 name, device);
414 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030415
Patrick Williamsb7077432024-08-16 15:22:21 -0400416 scannedPowerOff = true;
417 if (!powerOff)
418 {
419 scannedPowerOn = true;
420 }
421 });
James Feist1df06a42019-04-11 14:23:04 -0700422}
423
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200424void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
425 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030426{
427 if (powerOff && deviceRequiresPowerOn(device))
428 {
429 // power not on yet, don't know if it's there or not
430 return;
431 }
432
Alexander Hansen57604ed2025-06-27 13:22:28 +0200433 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030434 for (auto& iface : ifaces)
435 {
436 auto sharedPtr = iface.lock();
437 if (!!sharedPtr)
438 {
439 objServer.remove_interface(sharedPtr);
440 }
441 }
442
443 ifaces.clear();
444 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500445 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030446 logDeviceRemoved(device);
447}
448
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200449void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030450 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200451 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030452 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
453 //
454 // Discord discussion:
455 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
456 //
457 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200458 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030459{
Alexander Hansena555acf2025-06-27 11:59:10 +0200460 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030461
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200462 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200463 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030464 {
465 std::cerr << "Error writing json files\n";
466 }
467 });
468
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200469 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
470 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030471 if (count == instance)
472 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200473 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030474 }
475 });
476}
477
James Feist8f2710a2018-05-09 17:18:55 -0700478// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200479void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700480{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200481 propertiesChangedInstance++;
482 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700483
Alexander Hansenb1340da2025-06-27 14:29:13 +0200484 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700485
486 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200487 propertiesChangedTimer.async_wait(
488 [this, count](const boost::system::error_code& ec) {
489 if (ec == boost::asio::error::operation_aborted)
490 {
491 // we were cancelled
492 return;
493 }
494 if (ec)
495 {
496 std::cerr << "async wait error " << ec << "\n";
497 return;
498 }
James Feist8f2710a2018-05-09 17:18:55 -0700499
Alexander Hansenb1340da2025-06-27 14:29:13 +0200500 if (propertiesChangedInProgress)
501 {
502 propertiesChangedCallback();
503 return;
504 }
505 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700506
Alexander Hansenb1340da2025-06-27 14:29:13 +0200507 nlohmann::json oldConfiguration = systemConfiguration;
508 auto missingConfigurations = std::make_shared<nlohmann::json>();
509 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700510
Alexander Hansenb1340da2025-06-27 14:29:13 +0200511 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200512 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200513 [this, count, oldConfiguration, missingConfigurations]() {
514 // this is something that since ac has been applied to the
515 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200516 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200517 for (const auto& [name, device] :
518 missingConfigurations->items())
519 {
520 pruneConfiguration(powerOff, name, device);
521 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200522 nlohmann::json newConfiguration = systemConfiguration;
523
Christopher Meisf7252572025-06-11 13:22:05 +0200524 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200525
526 for (const auto& [_, device] : newConfiguration.items())
527 {
528 logDeviceAdded(device);
529 }
530
531 propertiesChangedInProgress = false;
532
533 boost::asio::post(io, [this, newConfiguration, count] {
534 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200535 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200536 std::ref(propertiesChangedTimer), newConfiguration);
537 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200538 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200539 perfScan->run();
540 });
James Feist75fdeeb2018-02-20 14:26:16 -0800541}
542
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600543// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400544static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200545 sdbusplus::message_t& msg,
546 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600547{
548 sdbusplus::message::object_path path;
549 DBusObject interfaces;
550 std::set<std::string> interfaceSet;
551 std::set<std::string> intersect;
552
553 msg.read(path, interfaces);
554
555 std::for_each(interfaces.begin(), interfaces.end(),
556 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400557 interfaceSet.insert(iface.first);
558 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600559
560 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
561 probeInterfaces.begin(), probeInterfaces.end(),
562 std::inserter(intersect, intersect.end()));
563 return !intersect.empty();
564}
565
566// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400567static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200568 sdbusplus::message_t& msg,
569 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600570{
571 sdbusplus::message::object_path path;
572 std::set<std::string> interfaces;
573 std::set<std::string> intersect;
574
575 msg.read(path, interfaces);
576
577 std::set_intersection(interfaces.begin(), interfaces.end(),
578 probeInterfaces.begin(), probeInterfaces.end(),
579 std::inserter(intersect, intersect.end()));
580 return !intersect.empty();
581}
582
Alexander Hansenad28e402025-06-25 12:38:20 +0200583void EntityManager::handleCurrentConfigurationJson()
584{
585 if (em_utils::fwVersionIsSame())
586 {
Christopher Meisf7252572025-06-11 13:22:05 +0200587 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200588 {
589 // this file could just be deleted, but it's nice for debug
590 std::filesystem::create_directory(tempConfigDir);
591 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200592 std::filesystem::copy(currentConfiguration, lastConfiguration);
593 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200594
595 std::ifstream jsonStream(lastConfiguration);
596 if (jsonStream.good())
597 {
598 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
599 if (data.is_discarded())
600 {
601 std::cerr
602 << "syntax error in " << lastConfiguration << "\n";
603 }
604 else
605 {
606 lastJson = std::move(data);
607 }
608 }
609 else
610 {
611 std::cerr << "unable to open " << lastConfiguration << "\n";
612 }
613 }
614 }
615 else
616 {
617 // not an error, just logging at this level to make it in the journal
618 std::cerr << "Clearing previous configuration\n";
Christopher Meisf7252572025-06-11 13:22:05 +0200619 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200620 }
621}
622
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200623void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800624{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200625 if (dbusMatches.contains(path))
626 {
627 return;
628 }
Nan Zhoua3315672022-09-20 19:48:14 +0000629
Alexander Hansen60803182025-06-27 14:41:28 +0200630 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
631
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200632 std::function<void(sdbusplus::message_t & message)> eventHandler =
633 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700634
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200635 sdbusplus::bus::match_t match(
636 static_cast<sdbusplus::bus_t&>(*systemBus),
637 "type='signal',member='PropertiesChanged',path='" + path + "'",
638 eventHandler);
639 dbusMatches.emplace(path, std::move(match));
640}
James Feistfd1264a2018-05-03 12:10:00 -0700641
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200642// We need a poke from DBus for static providers that create all their
643// objects prior to claiming a well-known name, and thus don't emit any
644// org.freedesktop.DBus.Properties signals. Similarly if a process exits
645// for any reason, expected or otherwise, we'll need a poke to remove
646// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200647void EntityManager::initFilters(
648 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200649{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200650 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500651 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500652 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200653 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400654 auto [name, oldOwner,
655 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500656
Patrick Williamsb7077432024-08-16 15:22:21 -0400657 if (name.starts_with(':'))
658 {
659 // We should do nothing with unique-name connections.
660 return;
661 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500662
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200663 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400664 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200665
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500666 // We also need a poke from DBus when new interfaces are created or
667 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200668 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500669 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500670 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200671 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400672 if (iaContainsProbeInterface(msg, probeInterfaces))
673 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200674 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400675 }
676 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200677
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200678 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500679 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500680 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200681 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400682 if (irContainsProbeInterface(msg, probeInterfaces))
683 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200684 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400685 }
686 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200687}